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.

168 lines
4.2 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using NaughtyAttributes;
  6. using TMPro;
  7. /// <summary>
  8. /// Script to control player input and oars
  9. /// </summary>
  10. public class OarController : MonoBehaviour
  11. {
  12. #region Inspector Fields
  13. [SerializeField,BoxGroup("References")]
  14. private HandController m_leftHand;
  15. [SerializeField, BoxGroup("References")]
  16. private HandController m_rightHand;
  17. [SerializeField, BoxGroup("References")]
  18. private Rigidbody m_oarRigidbody;
  19. [SerializeField, BoxGroup("References")]
  20. private Transform m_body;
  21. [SerializeField]
  22. private BoxCollider m_boxCollider;
  23. [SerializeField]
  24. private List<Collider> m_forbiddenColliders;
  25. #endregion Inspector Fields
  26. #region Private Fields
  27. float m_distanceFromRightHand;
  28. bool undoDoneThisFrame = false;
  29. #endregion Private Fields
  30. #region Getters
  31. public new Transform transform => m_oarRigidbody.transform;
  32. #endregion Getters
  33. #region MonoBehaviour Functions
  34. private void Awake()
  35. {
  36. m_boxCollider.enabled= false;
  37. m_distanceFromRightHand = Vector3.Distance(m_rightHand.transform.position, transform.position);
  38. m_oarRigidbody = GetComponent<Rigidbody>();
  39. }
  40. /// <summary>
  41. /// OnEnable is called when the object becomes enabled and active.
  42. /// </summary>
  43. private void OnEnable()
  44. {
  45. }
  46. /// <summary>
  47. /// OnDisable is called when the behaviour becomes disabled.
  48. /// </summary>
  49. private void OnDisable()
  50. {
  51. }
  52. /// <summary>
  53. /// Update is called once per frame
  54. /// </summary>
  55. private void FixedUpdate()
  56. {
  57. undoDoneThisFrame = false;
  58. UpdateTransform();
  59. }
  60. #endregion MonoBehaviour Functions
  61. #region Class Functionality
  62. private void UpdateTransform()
  63. {
  64. Vector3 direction =(m_rightHand.transform.position - m_leftHand.transform.position).normalized;
  65. Vector3 forward = Vector3.Cross(direction, m_body.forward);
  66. Quaternion rotation = Quaternion.LookRotation(forward, direction);
  67. MoveOarToPosition(m_rightHand.transform.position - direction * m_distanceFromRightHand);
  68. //m_oarRigidbody.MoveRotation(rotation);
  69. //m_oarRigidbody.MovePosition(m_rightHand.transform.position - direction * m_distanceFromRightHand);
  70. MoveOarToRotation(rotation);
  71. }
  72. private void MoveOarToPosition(Vector3 position)
  73. {
  74. m_oarRigidbody.velocity = (position - transform.position) * (1 / Time.fixedDeltaTime);
  75. }
  76. private void MoveOarToRotation(Quaternion rotation)
  77. {
  78. Quaternion difference = rotation * Quaternion.Inverse(transform.rotation);
  79. m_oarRigidbody.angularVelocity = (rotation.eulerAngles * Time.fixedDeltaTime);
  80. }
  81. public bool isColliding()
  82. {
  83. Vector3 direction = (m_rightHand.transform.position - m_leftHand.transform.position).normalized;
  84. Vector3 forward = Vector3.Cross(direction, m_body.forward);
  85. Quaternion rotation = Quaternion.LookRotation(forward, direction);
  86. Collider[] col = Physics.OverlapBox(transform.position + m_boxCollider.center, m_boxCollider.size / 2, rotation);
  87. if (col.Intersect(m_forbiddenColliders).Any())
  88. {
  89. Debug.Log($"Colliding with: {string.Join(", ", col.Intersect(m_forbiddenColliders))}");
  90. return true;
  91. }
  92. return false;
  93. }
  94. void OnCollisionEnter(Collision collision)
  95. {
  96. if (m_forbiddenColliders.Contains(collision.collider) && !undoDoneThisFrame)
  97. {
  98. Debug.Log($"Forbidden Collision: {collision.collider.gameObject}");
  99. undoDoneThisFrame= true;
  100. m_leftHand.UndoLastMovement();
  101. m_rightHand.UndoLastMovement();
  102. }
  103. else
  104. {
  105. Debug.Log($"Ignored Collision: {collision.collider.gameObject}");
  106. }
  107. }
  108. #endregion Class Functionality
  109. #region Editor Functions
  110. /// <summary>
  111. /// Called when the Component is created or Reset from the Inspector
  112. /// </summary>
  113. private void Reset()
  114. {
  115. m_oarRigidbody = GetComponentInChildren<Rigidbody>();
  116. }
  117. #endregion Editor Functions
  118. }