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.

100 lines
2.5 KiB

  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 = 10f;
  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 _time;
  13. private GameObject m_child;
  14. private GameObject m_body;
  15. private void Awake()
  16. {
  17. m_body = this.gameObject;
  18. }
  19. public void Hold(GameObject child)
  20. {
  21. _child = child;
  22. _child.transform.parent = parent.transform;
  23. yeetState = YeetState.Held;
  24. }
  25. public void Yeet()
  26. {
  27. _child.transform.parent = null;
  28. _child.transform.rotation = parent.transform.rotation;
  29. _child.GetComponent<Rigidbody>().velocity = _child.transform.forward * yeetVelocity + _child.transform.up * yeetVelocity;
  30. yeetState = YeetState.Yeeting;
  31. _time = yeetDuration;
  32. }
  33. // Start is called before the first frame update
  34. void Start()
  35. {
  36. }
  37. // Update is called once per frame
  38. void Update()
  39. {
  40. switch(yeetState)
  41. {
  42. case YeetState.Yeeting:
  43. //_child.transform.position += _child.transform.forward * yeetVelocity * Time.deltaTime;
  44. _time -= Time.deltaTime;
  45. if(_time <= 0f)
  46. {
  47. Debug.Log("YeetController.Update: Yeet finished");
  48. yeetState = YeetState.Unheld;
  49. }
  50. break;
  51. case YeetState.Held:
  52. break;
  53. case YeetState.Unheld:
  54. _child = null;
  55. break;
  56. }
  57. }
  58. private void OnTriggerEnter(Collider collider)
  59. {
  60. Debug.Log("PlayerInputController.OnTriggerEnter: arrived");
  61. m_child = collider.gameObject;
  62. }
  63. private void OnTriggerExit(Collider collider)
  64. {
  65. m_child = null;
  66. }
  67. private void OnYeet()
  68. {
  69. parent = m_body;
  70. switch (yeetState)
  71. {
  72. case YeetController.YeetState.Unheld:
  73. if(m_child)
  74. Hold(m_child);
  75. // Grab nearest baby
  76. break;
  77. case YeetController.YeetState.Held:
  78. Yeet();
  79. // Yeet baby
  80. break;
  81. case YeetController.YeetState.Yeeting:
  82. // Cooldown?
  83. break;
  84. }
  85. }
  86. }