Global Game Jam 2022
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
2.1 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.InputSystem;
  5. using Variables;
  6. using NaughtyAttributes;
  7. public class CharacterInputController : MonoBehaviour
  8. {
  9. [SerializeField, Header("References")]
  10. private CharacterController2D m_characterController;
  11. [SerializeField]
  12. private Reference<bool> m_playerInput;
  13. [SerializeField]
  14. private Reference<bool> m_noInputAllowed;
  15. [SerializeField]
  16. private Reference<bool> m_isPlayerDead;
  17. [SerializeField]
  18. private Reference<bool> m_isVictory;
  19. [SerializeField]
  20. private Animator m_animator;
  21. [SerializeField, BoxGroup("Settings")]
  22. private float m_speed = 2.0f;
  23. [ShowNonSerializedField]
  24. private Vector2 m_movement;
  25. [ShowNonSerializedField]
  26. private bool m_jump;
  27. private void OnEnable()
  28. {
  29. m_isPlayerDead.OnValueChanged += OnDeath;
  30. m_isVictory.OnValueChanged += OnVictory;
  31. }
  32. public void OnMove(InputAction.CallbackContext context)
  33. {
  34. m_movement = context.ReadValue<Vector2>();
  35. }
  36. public void OnJump(InputAction.CallbackContext context)
  37. {
  38. m_jump = context.ReadValueAsButton();
  39. }
  40. private void Update()
  41. {
  42. if (!m_noInputAllowed)
  43. {
  44. m_characterController.Move(m_movement.x * m_speed, false, m_jump);
  45. m_playerInput.Value = m_movement.magnitude != 0 || m_jump;
  46. }
  47. else
  48. {
  49. m_characterController.Move(0, false, false);
  50. m_playerInput.Value = false;
  51. }
  52. m_animator.SetBool("isMoving", m_playerInput);
  53. }
  54. public void OnLandHappened()
  55. {
  56. m_animator.SetTrigger("Land");
  57. }
  58. public void OnJumpHappened()
  59. {
  60. m_animator.SetTrigger("Land");
  61. }
  62. public void OnDeath(bool value)
  63. {
  64. if (value)
  65. m_animator.SetTrigger("Death");
  66. else
  67. m_animator.SetTrigger("Respawn");
  68. }
  69. public void OnVictory(bool value)
  70. {
  71. if (value)
  72. m_animator.SetTrigger("Victory");
  73. }
  74. }