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.

108 lines
2.2 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. private void OnDisable()
  33. {
  34. m_isPlayerDead.OnValueChanged -= OnDeath;
  35. m_isVictory.OnValueChanged -= OnVictory;
  36. }
  37. public void OnMove(InputAction.CallbackContext context)
  38. {
  39. m_movement = context.ReadValue<Vector2>();
  40. }
  41. public void OnJump(InputAction.CallbackContext context)
  42. {
  43. m_jump = context.ReadValueAsButton();
  44. }
  45. private void Update()
  46. {
  47. if (!m_noInputAllowed)
  48. {
  49. m_characterController.Move(m_movement.x * m_speed, false, m_jump);
  50. m_playerInput.Value = m_movement.magnitude != 0 || m_jump;
  51. }
  52. else
  53. {
  54. m_characterController.Move(0, false, false);
  55. m_playerInput.Value = false;
  56. }
  57. m_animator.SetBool("isMoving", m_playerInput);
  58. }
  59. public void OnLandHappened()
  60. {
  61. m_animator.SetTrigger("Land");
  62. }
  63. public void OnJumpHappened()
  64. {
  65. m_animator.SetTrigger("Land");
  66. }
  67. public void OnDeath(bool value)
  68. {
  69. if (value)
  70. m_animator.SetTrigger("Death");
  71. else
  72. m_animator.SetTrigger("Respawn");
  73. }
  74. public void OnVictory(bool value)
  75. {
  76. if (value)
  77. m_animator.SetTrigger("Victory");
  78. }
  79. }