using System;
using UnityEngine;
namespace NWH.DWP2.ShipController
{
///
/// Represents a single rudder. If rudder has a floating object component it will also be used for steering and not be
/// visual-only.
///
[Serializable]
public class Rudder
{
///
/// Name of the rudder. Can be any string.
///
public string name = "Rudder";
///
/// Transform representing the rudder.
///
public Transform rudderTransform;
///
/// Max angle in degrees rudder will be able to reach.
///
public float maxAngle = 45f;
///
/// Rotation speed in degrees per second.
///
public float rotationSpeed = 20f;
///
/// Axis around which the rudder will be rotated.
///
public Vector3 localRotationAxis = new Vector3(0, 1, 0);
private AdvancedShipController _sc;
public float Angle { get; private set; }
public float AnglePercent
{
get { return Angle / maxAngle; }
}
public void Initialize(AdvancedShipController sc)
{
_sc = sc;
}
public virtual void Update()
{
if (rudderTransform != null)
{
float targetAngle = -_sc.input.Steering * maxAngle;
Angle = Mathf.MoveTowardsAngle(Angle, targetAngle, rotationSpeed * Time.fixedDeltaTime);
rudderTransform.localRotation = Quaternion.Euler(Angle * localRotationAxis.x,
Angle * localRotationAxis.y,
Angle * localRotationAxis.z);
}
}
}
}