Global Game Jam 2021
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.

113 lines
2.8 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.InputSystem;
  5. [RequireComponent(typeof(PlayerInput), typeof(CharacterController))]
  6. public class PlayerInputController : MonoBehaviour
  7. {
  8. [SerializeField]
  9. private float m_playerSpeed = 1;
  10. [SerializeField]
  11. private float m_rotationSpeed = 1;
  12. [SerializeField]
  13. [Tooltip("Used to spin on spot\n"
  14. + "0 = back\n"
  15. + "0.5 = left/right\n"
  16. + "1.0 = forward")]
  17. private AnimationCurve m_turnRadius;
  18. private PlayerInput m_input;
  19. private CharacterController m_controller;
  20. private Vector3 m_desiredDirection;
  21. private bool m_recievedInput => m_desiredDirection.magnitude != 0;
  22. private YeetController m_yeetController;
  23. private GameObject m_child;
  24. private GameObject m_body;
  25. private void Awake()
  26. {
  27. m_input = GetComponent<PlayerInput>();
  28. m_controller = GetComponent<CharacterController>();
  29. m_yeetController = GetComponent<YeetController>();
  30. m_body = this.gameObject;
  31. }
  32. private void Update()
  33. {
  34. ApplyRotation();
  35. ApplyMovement();
  36. LockAxis(Vector3.up);
  37. }
  38. private void OnMovement(InputValue value)
  39. {
  40. Vector2 m_recievedInput = value.Get<Vector2>();
  41. m_desiredDirection = new Vector3(m_recievedInput.x, 0.0f, m_recievedInput.y);
  42. }
  43. private void OnTriggerEnter(Collider collider)
  44. {
  45. Debug.Log("PlayerInputController.OnTriggerEnter: arrived");
  46. m_child = collider.gameObject;
  47. }
  48. private void OnTriggerExit(Collider collider)
  49. {
  50. m_child = null;
  51. }
  52. private void OnYeet()
  53. {
  54. m_yeetController.parent = m_body;
  55. switch (m_yeetController.yeetState)
  56. {
  57. case YeetController.YeetState.Unheld:
  58. if(m_child)
  59. m_yeetController.Hold(m_child);
  60. // Grab nearest baby
  61. break;
  62. case YeetController.YeetState.Held:
  63. m_yeetController.Yeet();
  64. // Yeet baby
  65. break;
  66. case YeetController.YeetState.Yeeting:
  67. // Cooldown?
  68. break;
  69. }
  70. }
  71. private void ApplyRotation()
  72. {
  73. transform.forward = Vector3.Slerp(transform.forward, m_desiredDirection.normalized, m_rotationSpeed * Time.deltaTime);
  74. }
  75. private void ApplyMovement()
  76. {
  77. if (!m_recievedInput)
  78. return;
  79. float forwardRatio = (Vector3.Dot(transform.forward, m_desiredDirection.normalized) + 1) / 2;
  80. float speed = m_turnRadius.Evaluate(forwardRatio) * m_playerSpeed;
  81. m_controller.Move(transform.forward * speed * Time.deltaTime);
  82. }
  83. private void LockAxis(Vector3 axis)
  84. {
  85. transform.position = Vector3.ProjectOnPlane(transform.position, axis);
  86. }
  87. }