// ╔════════════════════════════════════════════════════════════════╗
// ║ Copyright © 2025 NWH Coding d.o.o. All rights reserved. ║
// ║ Licensed under Unity Asset Store Terms of Service: ║
// ║ https://unity.com/legal/as-terms ║
// ║ Use permitted only in compliance with the License. ║
// ║ Distributed "AS IS", without warranty of any kind. ║
// ╚════════════════════════════════════════════════════════════════╝
#region
using System;
using UnityEngine;
#endregion
namespace NWH.DWP2.ShipController
{
///
/// Represents a ship rudder that rotates based on steering input.
/// Provides visual rotation only - actual steering forces are applied by WaterObject components on the rudder itself.
/// Multiple rudders can be used on a single ship.
///
[Serializable]
public class Rudder
{
///
/// Axis around which the rudder will be rotated.
///
[Tooltip("Axis around which the rudder will be rotated.")]
public Vector3 localRotationAxis = new(0, 1, 0);
///
/// Max angle in degrees rudder will be able to reach.
///
[Tooltip("Max angle in degrees rudder will be able to reach.")]
public float maxAngle = 45f;
///
/// Name of the rudder. Can be any string.
///
[Tooltip("Name of the rudder. Can be any string.")]
public string name = "Rudder";
///
/// Rotation speed in degrees per second.
///
[Tooltip("Rotation speed in degrees per second.")]
public float rotationSpeed = 20f;
///
/// Transform representing the rudder.
///
[Tooltip("Transform representing the rudder.")]
public Transform rudderTransform;
private AdvancedShipController _sc;
///
/// Current angle of the rudder in degrees.
/// Positive angles indicate starboard turn, negative angles indicate port turn.
///
public float Angle { get; private set; }
///
/// Current rudder angle as a normalized value between -1 and 1.
///
public float AnglePercent
{
get { return Angle / maxAngle; }
}
///
/// Initializes the rudder with a reference to its parent ship controller.
///
/// The ship controller this rudder belongs to.
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);
}
}
}
}