using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using NaughtyAttributes;
///
/// Script to control player input and oars
///
public class OarController : MonoBehaviour
{
#region Inspector Fields
[SerializeField,BoxGroup("References")]
private HandController m_leftHand;
[SerializeField, BoxGroup("References")]
private HandController m_rightHand;
[SerializeField, BoxGroup("References")]
private Rigidbody m_oarRigidbody;
[SerializeField, BoxGroup("References")]
private Transform m_body;
[SerializeField]
private List m_forbiddenColliders;
#endregion Inspector Fields
#region Private Fields
float m_distanceFromRightHand;
bool undoDoneThisFrame = false;
#endregion Private Fields
#region Getters
public new Transform transform => m_oarRigidbody.transform;
#endregion Getters
#region MonoBehaviour Functions
private void Awake()
{
m_distanceFromRightHand = Vector3.Distance(m_rightHand.transform.position, transform.position);
m_oarRigidbody = GetComponent();
}
///
/// OnEnable is called when the object becomes enabled and active.
///
private void OnEnable()
{
}
///
/// OnDisable is called when the behaviour becomes disabled.
///
private void OnDisable()
{
}
///
/// Update is called once per frame
///
private void FixedUpdate()
{
undoDoneThisFrame = false;
UpdateTransform();
}
#endregion MonoBehaviour Functions
#region Class Functionality
private void UpdateTransform()
{
Vector3 direction =(m_rightHand.transform.position - m_leftHand.transform.position).normalized;
Vector3 forward = Vector3.Cross(direction, m_body.forward);
Quaternion rotation = Quaternion.LookRotation(forward, direction);
m_oarRigidbody.MovePosition(m_rightHand.transform.position - direction * m_distanceFromRightHand);
m_oarRigidbody.MoveRotation(rotation);
}
void OnCollisionEnter(Collision collision)
{
if (m_forbiddenColliders.Contains(collision.collider) && !undoDoneThisFrame)
{
Debug.Log($"Forbidden Collision: {collision.collider.gameObject}");
undoDoneThisFrame= true;
m_leftHand.UndoLastMovement();
m_rightHand.UndoLastMovement();
}
else
{
Debug.Log($"Ignored Collision: {collision.collider.gameObject}");
}
}
#endregion Class Functionality
#region Editor Functions
///
/// Called when the Component is created or Reset from the Inspector
///
private void Reset()
{
m_oarRigidbody = GetComponentInChildren();
}
#endregion Editor Functions
}