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.

206 lines
5.9 KiB

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