Global Game Jam 2023
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.

142 lines
3.4 KiB

1 year ago
1 year ago
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using NaughtyAttributes;
  5. using Unity.VisualScripting;
  6. using UnityEngine.Events;
  7. /// <summary>
  8. ///
  9. /// </summary>
  10. public class BoatRowController : MonoBehaviour
  11. {
  12. #region Inspector Fields
  13. public Transform m_oarTip;
  14. public UnityEvent<float> m_OnRow;
  15. public UnityEvent m_OnBrake;
  16. public int velocityOverFrames = 8;
  17. #endregion Inspector Fields
  18. #region Private Fields
  19. private Vector3 m_lastKnownOarPosition;
  20. private Queue<float> m_velocityQueue = new Queue<float>();
  21. #endregion Private Fields
  22. #region Getters
  23. #endregion Getters
  24. #region MonoBehaviour Functions
  25. /// <summary>
  26. /// OnEnable is called when the object becomes enabled and active.
  27. /// </summary>
  28. private void OnEnable()
  29. {
  30. }
  31. /// <summary>
  32. /// OnDisable is called when the behaviour becomes disabled.
  33. /// </summary>
  34. private void OnDisable()
  35. {
  36. }
  37. /// <summary>
  38. /// Update is called once per frame
  39. /// </summary>
  40. private void Update()
  41. {
  42. }
  43. #endregion MonoBehaviour Functions
  44. #region Class Functionality
  45. private void OnTriggerEnter(Collider other)
  46. {
  47. OarController oar = other.GetComponentInParent<OarController>();
  48. if (oar != null)
  49. {
  50. m_lastKnownOarPosition = transform.InverseTransformPoint(m_oarTip.position);
  51. m_velocityQueue = new Queue<float>();
  52. }
  53. }
  54. void OnTriggerStay(Collider other)
  55. {
  56. OarController oar = other.GetComponentInParent<OarController>();
  57. if(oar != null)
  58. {
  59. Vector3 localOarTipPosition = transform.InverseTransformPoint(m_oarTip.position);
  60. Vector3 direction = m_lastKnownOarPosition - localOarTipPosition;
  61. float directionality = Vector3.Dot(direction,Vector3.forward);
  62. m_velocityQueue.Enqueue(directionality * Time.deltaTime);
  63. if(m_velocityQueue.Count > velocityOverFrames)
  64. {
  65. m_velocityQueue.Dequeue();
  66. }
  67. float averageVelocity = 0;
  68. if (m_velocityQueue.Count > 0)
  69. {
  70. var v_arr = m_velocityQueue.ToArray();
  71. for(int i = 0; i < v_arr.Length; i++)
  72. {
  73. averageVelocity += v_arr[i];
  74. }
  75. averageVelocity /= v_arr.Length;
  76. }
  77. if (averageVelocity > 0)
  78. {
  79. if(averageVelocity < 0.000001)
  80. {
  81. m_OnBrake.Invoke();
  82. Debug.Log($"Brake! ({averageVelocity})");
  83. }
  84. else
  85. {
  86. Debug.Log($"Row! ({averageVelocity})");
  87. m_OnRow.Invoke(averageVelocity);
  88. }
  89. }
  90. else
  91. {
  92. Debug.Log($"No Row: {averageVelocity}");
  93. m_OnBrake.Invoke();
  94. }
  95. m_lastKnownOarPosition = localOarTipPosition;
  96. }
  97. else
  98. {
  99. Debug.Log($"Not Oar: {other.gameObject}");
  100. }
  101. }
  102. #endregion Class Functionality
  103. #region Editor Functions
  104. /// <summary>
  105. /// Called when the Component is created or Reset from the Inspector
  106. /// </summary>
  107. private void Reset()
  108. {
  109. //useful for finding components on creation
  110. }
  111. #endregion Editor Functions
  112. }