59 lines
2.9 KiB
C#
59 lines
2.9 KiB
C#
using System.Numerics;
|
|
namespace GamePort.Game;
|
|
|
|
public readonly record struct JimmyAirInputState(float Speed,float TurnRate,bool KeyboardTurn);
|
|
public readonly record struct JimmyAirApply(Vector3 Velocity,Vector3 Rotation,float AppliedTurnRate);
|
|
|
|
/// <summary>Ordinary mode1 keyboard input439900 and437F90 velocity prefix.
|
|
/// Body integration, facing interpolation, jump suffix and input ownership are
|
|
/// caller phases. Source constraints and observations: jimmy-air-motion.md.</summary>
|
|
public static class JimmyAirMotion
|
|
{
|
|
public static JimmyAirInputState Input(JimmyAirInputState state,PlayerInput input,bool supported,
|
|
float seconds,float maximumSpeed=600)
|
|
{
|
|
if(!float.IsFinite(state.Speed)||!float.IsFinite(state.TurnRate)||!float.IsFinite(seconds)
|
|
||seconds<=0||seconds>=1||!float.IsFinite(maximumSpeed)||maximumSpeed<=0)
|
|
throw new ArgumentOutOfRangeException(nameof(seconds));
|
|
float turn=state.TurnRate;bool keyboard=state.KeyboardTurn;float turnStep=100*seconds;
|
|
if(input.Left){turn+=turnStep;keyboard=true;}
|
|
else if(input.Right){turn-=turnStep;keyboard=true;}
|
|
else if(keyboard)turn=turn<0?MathF.Min(0,turn+turnStep):MathF.Max(0,turn-turnStep);
|
|
float speed=state.Speed;
|
|
if(input.Forward)
|
|
{
|
|
if(speed<maximumSpeed)speed=MathF.Min(maximumSpeed,speed+30);
|
|
}
|
|
//439B77 compares the unrounded x87 product against stored speed.
|
|
else if(speed>(double)maximumSpeed*.9090909361839294f)speed-=17.5f;
|
|
//A stored air mode may still receive input while body218 is supported.
|
|
//This independent subtraction intentionally has no crossing clamp.
|
|
if(supported)speed=speed>0?speed-17.5f:0;
|
|
if(input.Backward)
|
|
{
|
|
if(speed>0)speed=MathF.Max(0,speed-(supported?35:17.5f));
|
|
else if(speed<0)speed=MathF.Min(0,speed+35);
|
|
}
|
|
return new(speed,Math.Clamp(turn,-30,30),keyboard);
|
|
}
|
|
|
|
public static JimmyAirApply Apply(float speed,float turn,Vector3 rotation,Vector3 velocity,
|
|
float seconds,float friction=.8f)
|
|
{
|
|
if(!float.IsFinite(speed)||!float.IsFinite(turn)||!float.IsFinite(seconds)||seconds<0||seconds>=1
|
|
||!float.IsFinite(friction)||!float.IsFinite(velocity.X)||!float.IsFinite(velocity.Y)||!float.IsFinite(velocity.Z))
|
|
throw new ArgumentOutOfRangeException(nameof(seconds));
|
|
float side=0,applied=0;
|
|
if(velocity!=Vector3.Zero)
|
|
{
|
|
applied=turn;
|
|
rotation=RocketOrientation.Add(rotation,new(0,turn*seconds,0));
|
|
side=(float)((double)turn*.0181818176060915f*(1.0-friction)*speed);
|
|
}
|
|
var forward=RocketOrientation.Rotate(new(0,0,speed),rotation);
|
|
var(sin,cos)=RocketOrientation.Trig(rotation.Y);
|
|
return new(new((float)(forward.X+(double)cos*side),velocity.Y,
|
|
(float)(forward.Z+(double)sin*side)),rotation,applied);
|
|
}
|
|
}
|