Files
Neutron/src/GamePort/Game/AirSteering.cs
T

39 lines
1.8 KiB
C#

namespace GamePort.Game;
public readonly record struct AirSteeringResult(float Speed, float TurnRate);
/// <summary>Keyboard subset of normal airborne input439900 (mode1).
/// Modes1/2 share the handler; special flight mode2 and mouse/analog are separate.
/// </summary>
public static class AirSteering
{
public const float TurnAcceleration = 100; //48D96C, applied with dt.
public const float TurnLimit = 30; //439D81..439DB7, ordinary mode1.
public const float CoastThreshold = 0.9090909361839294f; //49D5DC.
public static AirSteeringResult Step(float speed, float turnRate, PlayerInput input, float seconds,
float maximumSpeed = RecoveredMotion.MaximumSpeed)
{
if (!float.IsFinite(speed) || !float.IsFinite(turnRate) || !float.IsFinite(seconds)
|| !float.IsFinite(maximumSpeed) || maximumSpeed<=0 || seconds < 0 || seconds >= 1)
throw new ArgumentOutOfRangeException(nameof(seconds));
var turnStep = TurnAcceleration * seconds;
if (input.Left) turnRate += turnStep;
else if (input.Right) turnRate -= turnStep;
else if (turnRate < 0) turnRate = MathF.Min(0, turnRate + turnStep);
else if (turnRate > 0) turnRate = MathF.Max(0, turnRate - turnStep);
turnRate = Math.Clamp(turnRate, -TurnLimit, TurnLimit);
if (input.Forward) speed = RecoveredMotion.AddSpeed(speed,maximumSpeed);
else if (speed > maximumSpeed * CoastThreshold)
speed -= RecoveredMotion.SpeedDecay * .5f;
//439C1D..439CB2 brakes to0, without starting reverse flight.
if (input.Backward)
{
if (speed > 0) speed = MathF.Max(0, speed - RecoveredMotion.SpeedDecay * .5f);
else if (speed < 0) speed = MathF.Min(0, speed + RecoveredMotion.SpeedDecay);
}
return new(speed, turnRate);
}
}