40 lines
1.9 KiB
C#
40 lines
1.9 KiB
C#
namespace GamePort.Game;
|
|
|
|
public readonly record struct AirSteeringResult(float Speed, float TurnRate);
|
|
|
|
/// <summary>Legacy stateless compatibility helper and shared steering constants.
|
|
/// Live ordinary input uses JimmyAirMotion, which retains the keyboard latch,
|
|
/// supported mode1 braking and original coast comparison precision.
|
|
/// </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);
|
|
}
|
|
}
|