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.

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