using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using NaughtyAttributes;
using TMPro;
///
/// 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 BoxCollider m_boxCollider;
[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_boxCollider.enabled= false;
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
public 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);
transform.rotation = rotation;
transform.position = m_rightHand.transform.position - direction * m_distanceFromRightHand;
//MoveOarToPosition(m_rightHand.transform.position - direction * m_distanceFromRightHand);
//m_oarRigidbody.MoveRotation(rotation);
//m_oarRigidbody.MovePosition(m_rightHand.transform.position - direction * m_distanceFromRightHand);
//MoveOarToRotation(rotation);
}
private void MoveOarToPosition(Vector3 position)
{
m_oarRigidbody.velocity = (position - transform.position) * (1 / Time.fixedDeltaTime);
}
private void MoveOarToRotation(Quaternion rotation)
{
Quaternion difference = rotation * Quaternion.Inverse(transform.rotation);
m_oarRigidbody.angularVelocity = (rotation.eulerAngles * Time.fixedDeltaTime);
}
public bool isColliding()
{
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);
Collider[] col = Physics.OverlapBox(transform.position + m_boxCollider.center, m_boxCollider.size / 2, rotation);
if (col.Intersect(m_forbiddenColliders).Any())
{
//Debug.Log($"Forbidden collision: {string.Join(", ", col.Intersect(m_forbiddenColliders))}");
return true;
}
else
{
// Debug.Log($"Ignored collision: {string.Join(", ", col.Except(m_forbiddenColliders))}");
}
return false;
}
#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
}