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.

129 lines
3.4 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
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class YeetController : MonoBehaviour
  5. {
  6. public GameObject parent { get; set; }
  7. public float yeetVelocity = 5f;
  8. public float yeetDuration = 2f;
  9. public enum YeetState { Unheld, Held, Yeeting };
  10. public YeetState yeetState { get; private set; } = YeetState.Unheld;
  11. private GameObject _child;
  12. private float m_time;
  13. private GameObject m_child;
  14. private GameObject m_body;
  15. private float m_velocityWindup;
  16. private LineRenderer m_lineRenderer;
  17. private void Awake()
  18. {
  19. m_body = this.gameObject;
  20. }
  21. public void Hold(GameObject child)
  22. {
  23. _child = child;
  24. _child.transform.parent = parent.transform;
  25. yeetState = YeetState.Held;
  26. m_lineRenderer = gameObject.AddComponent<LineRenderer>();
  27. }
  28. public void Yeet()
  29. {
  30. _child.transform.parent = null;
  31. _child.transform.rotation = parent.transform.rotation;
  32. _child.GetComponent<Rigidbody>().velocity = _child.transform.forward * m_velocityWindup + _child.transform.up * m_velocityWindup;
  33. yeetState = YeetState.Yeeting;
  34. m_time = yeetDuration;
  35. m_velocityWindup = 0;
  36. Destroy(m_lineRenderer);
  37. }
  38. void Start()
  39. {
  40. }
  41. void Update()
  42. {
  43. switch(yeetState)
  44. {
  45. case YeetState.Yeeting:
  46. m_time -= Time.deltaTime;
  47. if(m_time <= 0f)
  48. {
  49. Debug.Log("YeetController.Update: Yeet finished");
  50. yeetState = YeetState.Unheld;
  51. }
  52. break;
  53. case YeetState.Held:
  54. // draw yeet lines
  55. if(m_velocityWindup < yeetVelocity)
  56. {
  57. m_velocityWindup += 0.01f;
  58. }
  59. Vector3 velocity = parent.transform.forward * m_velocityWindup + parent.transform.up * m_velocityWindup;
  60. float t;
  61. t = (-1f * velocity.y) / Physics.gravity.y;
  62. t = 2f * t;
  63. m_lineRenderer.positionCount = 10;
  64. Vector3 trajectoryPoint;
  65. for (int i = 0; i < m_lineRenderer.positionCount; i++)
  66. {
  67. float time = t * i / (float)(m_lineRenderer.positionCount);
  68. trajectoryPoint = parent.transform.position + velocity * time + 0.5f * Physics.gravity * time * time;
  69. m_lineRenderer.SetPosition(i, trajectoryPoint);
  70. }
  71. break;
  72. case YeetState.Unheld:
  73. _child = null;
  74. break;
  75. }
  76. }
  77. private void OnTriggerEnter(Collider collider)
  78. {
  79. Debug.Log("PlayerInputController.OnTriggerEnter: arrived");
  80. m_child = collider.gameObject;
  81. }
  82. private void OnTriggerExit(Collider collider)
  83. {
  84. m_child = null;
  85. }
  86. private void OnYeet()
  87. {
  88. parent = m_body;
  89. switch (yeetState)
  90. {
  91. case YeetController.YeetState.Unheld:
  92. if(m_child)
  93. Hold(m_child);
  94. // Grab nearest baby
  95. break;
  96. case YeetController.YeetState.Held:
  97. Yeet();
  98. // Yeet baby
  99. break;
  100. case YeetController.YeetState.Yeeting:
  101. // Cooldown?
  102. break;
  103. }
  104. }
  105. }