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.

215 lines
6.1 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.InputSystem;
  5. public class YeetController : InputBehaviour
  6. {
  7. [Tooltip("This is where the babies are held")]
  8. public GameObject handCarryingPosition;
  9. public GameObject parent { get; set; }
  10. public AudioClip[] yeetClips;
  11. public AudioClip[] childClips;
  12. public float yeetVelocity = 5f;
  13. public float yeetDuration = 2f;
  14. public float windupSpeed = 0.01f;
  15. [SerializeField]
  16. private Animator m_YeetAnimator;
  17. public Material trajectoryLineMaterial;
  18. public enum YeetState { Unheld, Held, Preyeet, Yeeting };
  19. public YeetState yeetState { get; private set; } = YeetState.Unheld;
  20. private GameObject _child;
  21. private float m_time;
  22. private GameObject m_child;
  23. private GameObject m_body;
  24. private float m_velocityWindup;
  25. private LineRenderer m_lineRenderer;
  26. private AudioSource m_audioSource;
  27. private Transform m_OldChildParent;
  28. private Vector3 preyeetPosition;
  29. public PlayerInputController playerController;
  30. private void Awake()
  31. {
  32. m_body = this.gameObject;
  33. m_audioSource = gameObject.AddComponent<AudioSource>();
  34. m_audioSource.spatialBlend = 0f;
  35. }
  36. public void Hold(GameObject child)
  37. {
  38. m_audioSource.clip = childClips[Random.Range(0, childClips.Length)];
  39. _child = child;
  40. m_OldChildParent = _child.transform.parent;
  41. _child.transform.parent = parent.transform;
  42. _child.GetComponent<YeetHandle>().held = true;
  43. _child.GetComponent<YeetHandle>().lastHeld = parent;
  44. yeetState = YeetState.Held;
  45. m_YeetAnimator.SetBool("Carry", true);
  46. m_YeetAnimator.SetBool("Yeet", true);
  47. m_audioSource.Play();
  48. playerController.m_playerSpeed /= 3;
  49. }
  50. public void Preyeet()
  51. {
  52. preyeetPosition = parent.transform.position;
  53. m_lineRenderer = gameObject.AddComponent<LineRenderer>();
  54. m_lineRenderer.material = trajectoryLineMaterial;
  55. yeetState = YeetState.Preyeet;
  56. }
  57. public void Yeet()
  58. {
  59. m_audioSource.clip = yeetClips[Random.Range(0, yeetClips.Length)];
  60. m_YeetAnimator.SetBool("Yeet", false);
  61. m_YeetAnimator.SetBool("Carry", false);
  62. _child.transform.parent = m_OldChildParent;
  63. _child.transform.rotation = parent.transform.rotation;
  64. _child.GetComponent<YeetHandle>().held = false;
  65. _child.GetComponent<Rigidbody>().velocity = calculateVelocityVector();
  66. yeetState = YeetState.Yeeting;
  67. m_time = yeetDuration;
  68. m_velocityWindup = 0;
  69. Destroy(m_lineRenderer);
  70. m_audioSource.Play();
  71. playerController.m_playerSpeed *= 3;
  72. }
  73. void Start()
  74. {
  75. }
  76. void Update()
  77. {
  78. switch (yeetState)
  79. {
  80. case YeetState.Yeeting:
  81. m_time -= Time.deltaTime;
  82. if (m_time <= 0f)
  83. {
  84. Debug.Log("YeetController.Update: Yeet finished");
  85. yeetState = YeetState.Unheld;
  86. }
  87. break;
  88. case YeetState.Preyeet:
  89. //TODO: Stop player
  90. parent.transform.position = preyeetPosition;
  91. _child.transform.position = handCarryingPosition.transform.position;
  92. if (m_velocityWindup < yeetVelocity)
  93. {
  94. m_velocityWindup += windupSpeed;
  95. }
  96. Vector3 velocity = calculateVelocityVector();
  97. float t;
  98. t = (-1f * velocity.y) / Physics.gravity.y;
  99. t = 2f * t;
  100. m_lineRenderer.positionCount = 10;
  101. Vector3 trajectoryPoint;
  102. for (int i = 0; i < m_lineRenderer.positionCount; i++)
  103. {
  104. float time = t * i / (float)(m_lineRenderer.positionCount);
  105. trajectoryPoint = parent.transform.position + velocity * time + 0.5f * Physics.gravity * time * time;
  106. m_lineRenderer.SetPosition(i, trajectoryPoint);
  107. }
  108. break;
  109. case YeetState.Held:
  110. //TODO: slow player
  111. // Stop child from wriggling
  112. _child.transform.position = handCarryingPosition.transform.position;
  113. break;
  114. case YeetState.Unheld:
  115. _child = null;
  116. break;
  117. }
  118. }
  119. private Vector3 calculateVelocityVector()
  120. {
  121. return parent.transform.forward * m_velocityWindup + parent.transform.up * m_velocityWindup;
  122. }
  123. private void OnTriggerEnter(Collider collider)
  124. {
  125. m_child = collider.gameObject;
  126. }
  127. private void OnTriggerExit(Collider collider)
  128. {
  129. m_child = null;
  130. }
  131. private void OnYeet(InputAction.CallbackContext args)
  132. {
  133. Debug.Log($"Player Yeeting: {args.phase}");
  134. parent = m_body;
  135. switch (yeetState)
  136. {
  137. case YeetController.YeetState.Unheld:
  138. if (m_child/* && args.phase == InputActionPhase.Performed*/)
  139. Hold(m_child);
  140. // Grab nearest baby
  141. break;
  142. case YeetState.Preyeet:
  143. /*if (args.phase == InputActionPhase.Performed)*/
  144. Yeet();
  145. break;
  146. case YeetController.YeetState.Held:
  147. if (args.phase == InputActionPhase.Started)
  148. Preyeet();
  149. // Yeet baby
  150. break;
  151. case YeetController.YeetState.Yeeting:
  152. // Cooldown?
  153. break;
  154. }
  155. }
  156. public override void RegisterInput(PlayerInput playerInput)
  157. {
  158. playerInput.currentActionMap.FindAction("Yeet").canceled += OnYeet;
  159. playerInput.currentActionMap.FindAction("Yeet").started += OnYeet;
  160. }
  161. public override void UnregisterInput(PlayerInput playerInput)
  162. {
  163. playerInput.currentActionMap.FindAction("Yeet").canceled -= OnYeet;
  164. playerInput.currentActionMap.FindAction("Yeet").started -= OnYeet;
  165. }
  166. }