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.

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