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.

153 lines
4.2 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. using UnityEngine.InputSystem;
  5. public class YeetController : InputBehaviour
  6. {
  7. public GameObject parent { get; set; }
  8. public float yeetVelocity = 5f;
  9. public float yeetDuration = 2f;
  10. public float windupSpeed = 0.01f;
  11. [SerializeField]
  12. private Animator m_YeetAnimator;
  13. public enum YeetState { Unheld, Held, Yeeting };
  14. public YeetState yeetState { get; private set; } = YeetState.Unheld;
  15. private GameObject _child;
  16. private float m_time;
  17. private GameObject m_child;
  18. private GameObject m_body;
  19. private float m_velocityWindup;
  20. private LineRenderer m_lineRenderer;
  21. private void Awake()
  22. {
  23. m_body = this.gameObject;
  24. }
  25. public void Hold(GameObject child)
  26. {
  27. _child = child;
  28. _child.transform.parent = parent.transform;
  29. _child.GetComponent<YeetHandle>().held = true;
  30. yeetState = YeetState.Held;
  31. m_lineRenderer = gameObject.AddComponent<LineRenderer>();
  32. m_YeetAnimator.SetBool("Carry", true);
  33. m_YeetAnimator.SetBool("Yeet", true);
  34. }
  35. public void Yeet()
  36. {
  37. m_YeetAnimator.SetBool("Yeet", false);
  38. m_YeetAnimator.SetBool("Carry", false);
  39. _child.transform.parent = null;
  40. _child.transform.rotation = parent.transform.rotation;
  41. _child.GetComponent<YeetHandle>().held = true;
  42. _child.GetComponent<Rigidbody>().velocity = _child.transform.forward * m_velocityWindup + _child.transform.up * m_velocityWindup;
  43. yeetState = YeetState.Yeeting;
  44. m_time = yeetDuration;
  45. m_velocityWindup = 0;
  46. Destroy(m_lineRenderer);
  47. }
  48. void Start()
  49. {
  50. }
  51. void Update()
  52. {
  53. switch(yeetState)
  54. {
  55. case YeetState.Yeeting:
  56. m_time -= Time.deltaTime;
  57. if(m_time <= 0f)
  58. {
  59. Debug.Log("YeetController.Update: Yeet finished");
  60. yeetState = YeetState.Unheld;
  61. }
  62. break;
  63. case YeetState.Held:
  64. // draw yeet lines
  65. if(m_velocityWindup < yeetVelocity)
  66. {
  67. m_velocityWindup += windupSpeed;
  68. }
  69. Vector3 velocity = parent.transform.forward * m_velocityWindup + parent.transform.up * m_velocityWindup;
  70. float t;
  71. t = (-1f * velocity.y) / Physics.gravity.y;
  72. t = 2f * t;
  73. m_lineRenderer.positionCount = 10;
  74. Vector3 trajectoryPoint;
  75. for (int i = 0; i < m_lineRenderer.positionCount; i++)
  76. {
  77. float time = t * i / (float)(m_lineRenderer.positionCount);
  78. trajectoryPoint = parent.transform.position + velocity * time + 0.5f * Physics.gravity * time * time;
  79. m_lineRenderer.SetPosition(i, trajectoryPoint);
  80. }
  81. break;
  82. case YeetState.Unheld:
  83. _child = null;
  84. break;
  85. }
  86. }
  87. private void OnTriggerEnter(Collider collider)
  88. {
  89. Debug.Log("PlayerInputController.OnTriggerEnter: arrived");
  90. m_child = collider.gameObject;
  91. }
  92. private void OnTriggerExit(Collider collider)
  93. {
  94. m_child = null;
  95. }
  96. private void OnYeet(InputAction.CallbackContext args)
  97. {
  98. parent = m_body;
  99. switch (yeetState)
  100. {
  101. case YeetController.YeetState.Unheld:
  102. if(m_child)
  103. Hold(m_child);
  104. // Grab nearest baby
  105. break;
  106. case YeetController.YeetState.Held:
  107. Yeet();
  108. // Yeet baby
  109. break;
  110. case YeetController.YeetState.Yeeting:
  111. // Cooldown?
  112. break;
  113. }
  114. }
  115. public override void RegisterInput(PlayerInput playerInput)
  116. {
  117. playerInput.currentActionMap.FindAction("Yeet").performed += OnYeet;
  118. }
  119. public override void UnregisterInput(PlayerInput playerInput)
  120. {
  121. playerInput.currentActionMap.FindAction("Yeet").performed -= OnYeet;
  122. }
  123. }