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.

124 lines
2.6 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  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]
  22. private AudioClip m_DeathSound;
  23. [SerializeField]
  24. private AudioClip m_VictoryClip;
  25. [SerializeField]
  26. private AudioClip m_JumpSound;
  27. [SerializeField, BoxGroup("Settings")]
  28. private float m_speed = 2.0f;
  29. [ShowNonSerializedField]
  30. private Vector2 m_movement;
  31. [ShowNonSerializedField]
  32. private bool m_jump;
  33. private void OnEnable()
  34. {
  35. m_isPlayerDead.OnValueChanged += OnDeath;
  36. m_isVictory.OnValueChanged += OnVictory;
  37. }
  38. private void OnDisable()
  39. {
  40. m_isPlayerDead.OnValueChanged -= OnDeath;
  41. m_isVictory.OnValueChanged -= OnVictory;
  42. }
  43. public void OnMove(InputAction.CallbackContext context)
  44. {
  45. m_movement = context.ReadValue<Vector2>();
  46. }
  47. public void OnJump(InputAction.CallbackContext context)
  48. {
  49. m_jump = context.ReadValueAsButton();
  50. }
  51. private void Update()
  52. {
  53. if (!m_noInputAllowed)
  54. {
  55. m_characterController.Move(m_movement.x * m_speed, false, m_jump);
  56. m_playerInput.Value = m_movement.magnitude != 0 || m_jump;
  57. }
  58. else
  59. {
  60. m_characterController.Move(0, false, false);
  61. m_playerInput.Value = false;
  62. }
  63. m_animator.SetBool("isMoving", m_playerInput);
  64. }
  65. public void OnLandHappened()
  66. {
  67. m_animator.SetTrigger("Land");
  68. }
  69. public void OnJumpHappened()
  70. {
  71. m_animator.SetTrigger("Land");
  72. //AudioSource.PlayClipAtPoint(m_JumpSound, transform.position);
  73. }
  74. public void OnDeath(bool value)
  75. {
  76. if (value)
  77. {
  78. m_animator.SetTrigger("Death");
  79. AudioSource.PlayClipAtPoint(m_DeathSound, transform.position);
  80. }
  81. else
  82. m_animator.SetTrigger("Respawn");
  83. }
  84. public void OnVictory(bool value)
  85. {
  86. if (value)
  87. {
  88. m_animator.SetTrigger("Victory");
  89. AudioSource.PlayClipAtPoint(m_VictoryClip, transform.position);
  90. }
  91. }
  92. }