Files
cruiseSimulator/Assets/Soundings/Runtime/HelmNavigation.cs

107 lines
5.0 KiB
C#

using UnityEngine;
namespace Soundings
{
internal sealed class HelmNavigation
{
private const float RudderLimit = 35f;
private const float RudderSlew = 15f;
private const float IntegrationStep = .1f;
private readonly float duration, cruiseKnots, turnRateLimit, inertia, arrivalRadius;
private float turnRate;
private Vector2 approachWaypoint;
private bool extendingApproach;
public HelmNavigation(float duration, int hullIndex)
{
this.duration = Mathf.Max(1, duration);
cruiseKnots = 17f + 2f * hullIndex;
turnRateLimit = 4.2f - .5f * hullIndex;
inertia = 1f + .16f * hullIndex;
arrivalRadius = Mathf.Min(this.duration * .15f, Mathf.Clamp(this.duration * .006f, .6f, 1.8f));
}
public bool Manual { get; set; }
public bool PropulsionStopped { get; set; }
public float Heading { get; private set; }
public float Rudder { get; private set; }
public float Speed { get; private set; }
public float Progress { get; private set; }
public Vector2 Position { get; private set; }
public bool Arrived { get; private set; }
public float RemainingSeconds => Arrived ? 0 : (new Vector2(duration, 0) - Position).magnitude;
public void Advance(float seconds, float rudderInput, float engineSpeedFactor, WeatherCondition weather)
{
if (Arrived || seconds <= 0)
return;
// Simulated X/Y use nominal route seconds. The route duration already includes the sea-state penalty.
float cruising = cruiseKnots / (weather switch
{
WeatherCondition.Swell => 1.14f,
WeatherCondition.Storm => 1.32f,
_ => 1f
});
float targetSpeed = PropulsionStopped ? 0 : cruising * Mathf.Max(0, engineSpeedFactor);
float requestedRudder = Mathf.Clamp(rudderInput, -1f, 1f) * RudderLimit;
if (Manual)
extendingApproach = false;
for (int step = 0, steps = Mathf.CeilToInt(seconds / IntegrationStep); step < steps && !Arrived; step++)
{
float delta = Mathf.Min(IntegrationStep, seconds - step * IntegrationStep);
Vector2 toPort = new Vector2(duration, 0) - Position;
float rudderDemand = Manual ? requestedRudder : CourseRudder(toPort);
float previousSpeed = Speed;
float acceleration = targetSpeed > Speed ? 1.55f : 1.02f;
Speed = Mathf.MoveTowards(Speed, targetSpeed, delta * acceleration / inertia);
Rudder = Mathf.MoveTowards(Rudder, rudderDemand, delta * RudderSlew);
float waterFlow = Mathf.Clamp01((previousSpeed + Speed) * .5f / cruising);
float desiredTurnRate = Rudder / RudderLimit * turnRateLimit * waterFlow;
turnRate = Mathf.MoveTowards(turnRate, desiredTurnRate, delta * 3.2f / inertia);
float yawStep = turnRate * delta;
float course = (Heading + yawStep * .5f) * Mathf.Deg2Rad;
Heading = Mathf.Repeat(Heading + yawStep + 180f, 360f) - 180f;
float distance = (previousSpeed + Speed) * .5f / cruising * delta;
Position += new Vector2(Mathf.Cos(course), Mathf.Sin(course)) * distance;
Arrived = RemainingSeconds <= arrivalRadius;
Progress = Arrived ? 1 : Mathf.Max(Progress, Mathf.Clamp01(1 - RemainingSeconds / duration));
}
}
private float CourseRudder(Vector2 toPort)
{
Vector2 toWaypoint = approachWaypoint - Position;
if (extendingApproach && toWaypoint.magnitude < 6)
extendingApproach = false;
float course = Mathf.Atan2(toPort.y, toPort.x) * Mathf.Rad2Deg;
float cruiseTurnRadius = Mathf.Rad2Deg / turnRateLimit;
if (!extendingApproach && toPort.magnitude < cruiseTurnRadius * 1.5f
&& Mathf.Abs(Mathf.DeltaAngle(Heading, course)) > 55f)
{
// Make room for a full turn if the destination is already inside the ship's turning circle.
float heading = Heading * Mathf.Deg2Rad;
toWaypoint = new Vector2(Mathf.Cos(heading), Mathf.Sin(heading)) * (cruiseTurnRadius * 4f);
approachWaypoint = Position + toWaypoint;
extendingApproach = true;
}
if (extendingApproach)
course = Mathf.Atan2(toWaypoint.y, toWaypoint.x) * Mathf.Rad2Deg;
float courseError = Mathf.DeltaAngle(Heading, course);
return Mathf.Clamp(courseError * 1.55f - turnRate * 3.6f, -RudderLimit, RudderLimit);
}
internal void SetProgress(float progress)
{
Progress = Mathf.Clamp01(progress);
Position = new Vector2(duration * Progress, 0);
Heading = Rudder = turnRate = 0;
extendingApproach = false;
Arrived = Progress >= 1;
}
}
}