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.

170 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 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. public 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. transform.rotation = rotation;
  68. transform.position = m_rightHand.transform.position - direction * m_distanceFromRightHand;
  69. //MoveOarToPosition(m_rightHand.transform.position - direction * m_distanceFromRightHand);
  70. m_oarRigidbody.MoveRotation(rotation);
  71. m_oarRigidbody.MovePosition(m_rightHand.transform.position - direction * m_distanceFromRightHand);
  72. //MoveOarToRotation(rotation);
  73. }
  74. private void MoveOarToPosition(Vector3 position)
  75. {
  76. m_oarRigidbody.velocity = (position - transform.position) * (1 / Time.fixedDeltaTime);
  77. }
  78. private void MoveOarToRotation(Quaternion rotation)
  79. {
  80. Quaternion difference = rotation * Quaternion.Inverse(transform.rotation);
  81. m_oarRigidbody.angularVelocity = (rotation.eulerAngles * Time.fixedDeltaTime);
  82. }
  83. public bool isColliding()
  84. {
  85. Vector3 direction = (m_rightHand.transform.position - m_leftHand.transform.position).normalized;
  86. Vector3 forward = Vector3.Cross(direction, m_body.forward);
  87. Quaternion rotation = Quaternion.LookRotation(forward, direction);
  88. Collider[] col = Physics.OverlapBox(transform.position + m_boxCollider.center, m_boxCollider.size / 2, rotation);
  89. if (col.Intersect(m_forbiddenColliders).Any())
  90. {
  91. return true;
  92. }
  93. return false;
  94. }
  95. /*
  96. void OnCollisionEnter(Collision collision)
  97. {
  98. if (m_forbiddenColliders.Contains(collision.collider) && !undoDoneThisFrame)
  99. {
  100. Debug.Log($"Forbidden Collision: {collision.collider.gameObject}");
  101. undoDoneThisFrame= true;
  102. m_leftHand.UndoLastMovement();
  103. m_rightHand.UndoLastMovement();
  104. }
  105. else
  106. {
  107. Debug.Log($"Ignored Collision: {collision.collider.gameObject}");
  108. }
  109. }*/
  110. #endregion Class Functionality
  111. #region Editor Functions
  112. /// <summary>
  113. /// Called when the Component is created or Reset from the Inspector
  114. /// </summary>
  115. private void Reset()
  116. {
  117. m_oarRigidbody = GetComponentInChildren<Rigidbody>();
  118. }
  119. #endregion Editor Functions
  120. }