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.

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