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.

118 lines
2.5 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. public void OnMove(InputAction.CallbackContext context)
  39. {
  40. m_movement = context.ReadValue<Vector2>();
  41. }
  42. public void OnJump(InputAction.CallbackContext context)
  43. {
  44. m_jump = context.ReadValueAsButton();
  45. }
  46. private void Update()
  47. {
  48. if (!m_noInputAllowed)
  49. {
  50. m_characterController.Move(m_movement.x * m_speed, false, m_jump);
  51. m_playerInput.Value = m_movement.magnitude != 0 || m_jump;
  52. }
  53. else
  54. {
  55. m_characterController.Move(0, false, false);
  56. m_playerInput.Value = false;
  57. }
  58. m_animator.SetBool("isMoving", m_playerInput);
  59. }
  60. public void OnLandHappened()
  61. {
  62. m_animator.SetTrigger("Land");
  63. }
  64. public void OnJumpHappened()
  65. {
  66. m_animator.SetTrigger("Land");
  67. //AudioSource.PlayClipAtPoint(m_JumpSound, transform.position);
  68. }
  69. public void OnDeath(bool value)
  70. {
  71. if (value)
  72. {
  73. m_animator.SetTrigger("Death");
  74. AudioSource.PlayClipAtPoint(m_DeathSound, transform.position);
  75. }
  76. else
  77. m_animator.SetTrigger("Respawn");
  78. }
  79. public void OnVictory(bool value)
  80. {
  81. if (value)
  82. {
  83. m_animator.SetTrigger("Victory");
  84. AudioSource.PlayClipAtPoint(m_VictoryClip, transform.position);
  85. }
  86. }
  87. }