93 lines
4.1 KiB
C#
93 lines
4.1 KiB
C#
using System.Numerics;
|
|
|
|
namespace GamePort.Game;
|
|
|
|
public readonly record struct BubbleSteering(float Speed, float TurnRate, float TargetPitch, float TargetRoll);
|
|
public readonly record struct BubbleApply(Vector3 Velocity, float Yaw, float Speed, bool Boing);
|
|
|
|
/// <summary>
|
|
/// Keyboard subset of mode2 input439900 and apply4387E0. Body integration,
|
|
/// impulses, acceleration and gadget lifetime belong to their existing phases.
|
|
/// Source ownership and precision limits: docs/gameplay/bubble-motion.md.
|
|
/// </summary>
|
|
public static class BubbleMotion
|
|
{
|
|
public const float MaximumSpeed = 1350;
|
|
public const float TurnLimit = 50;
|
|
public const float ReleasedBounceSpeed = 300;
|
|
public const float HeldBounceSpeed = 900;
|
|
|
|
public static BubbleSteering StepInput(float speed, float turn, PlayerInput input,
|
|
bool supported, float seconds, float maximumSpeed = MaximumSpeed,
|
|
float forwardPitch=-10,float backwardPitch=10)
|
|
{
|
|
Validate(speed, turn, seconds);
|
|
if (!float.IsFinite(maximumSpeed) || maximumSpeed <= 0||!float.IsFinite(forwardPitch)||!float.IsFinite(backwardPitch))
|
|
throw new ArgumentOutOfRangeException(nameof(maximumSpeed));
|
|
|
|
var turnStep = AirSteering.TurnAcceleration * seconds;
|
|
if (input.Left) turn += turnStep;
|
|
else if (input.Right) turn -= turnStep;
|
|
else if (turn < 0) turn = MathF.Min(0, turn + turnStep);
|
|
else if (turn > 0) turn = MathF.Max(0, turn - turnStep);
|
|
|
|
//439ABB..439AE6: mode2 also uses the shared fire predicate as throttle.
|
|
var forward = input.Forward || input.Fire;
|
|
if (forward && speed < maximumSpeed)
|
|
speed = MathF.Min(maximumSpeed, speed + RecoveredMotion.SpeedIncrement);
|
|
if (!forward && speed > maximumSpeed * AirSteering.CoastThreshold)
|
|
speed -= RecoveredMotion.SpeedDecay * .5f;
|
|
|
|
//439BA2..439BED: this subtraction deliberately has no crossing clamp.
|
|
if (supported) speed = speed > 0 ? speed - RecoveredMotion.SpeedDecay * .5f : 0;
|
|
if (input.Backward)
|
|
{
|
|
if (speed > 0)
|
|
speed = MathF.Max(0, speed - RecoveredMotion.SpeedDecay * (supported ? 1 : .5f));
|
|
else if (speed < 0)
|
|
speed = MathF.Min(0, speed + RecoveredMotion.SpeedDecay);
|
|
}
|
|
|
|
turn = Math.Clamp(turn, -TurnLimit, TurnLimit);
|
|
return new(speed, turn, forward ? forwardPitch : input.Backward ? backwardPitch : 0, -.25f * turn);
|
|
}
|
|
|
|
public static BubbleApply StepApply(float speed, float turn, float yaw, float pitch,
|
|
Vector3 velocity, bool supported, bool jump, float seconds)
|
|
{
|
|
Validate(speed, turn, seconds);
|
|
if (!float.IsFinite(yaw) || !float.IsFinite(pitch)
|
|
|| !float.IsFinite(velocity.X) || !float.IsFinite(velocity.Y) || !float.IsFinite(velocity.Z))
|
|
throw new ArgumentOutOfRangeException(nameof(velocity));
|
|
|
|
float sideSpeed = 0;
|
|
//43884E..4388AE: test incoming velocity before replacing its X/Z.
|
|
if (velocity != Vector3.Zero)
|
|
{
|
|
yaw += turn * seconds;
|
|
const float friction = .8f;
|
|
sideSpeed = turn * .0181818176060915f * (1 - friction) * speed;
|
|
}
|
|
var (sin, cos) = LabSimulation.Trig(yaw);
|
|
var forwardSpeed = LabSimulation.Trig(pitch).Cos * speed;
|
|
velocity = new(-sin * forwardSpeed + cos * sideSpeed, velocity.Y,
|
|
cos * forwardSpeed + sin * sideSpeed);
|
|
|
|
//43899A..438A50: direct liveY assignment, after composing current X/Z.
|
|
//Only the published scalar halves; current velocity keeps pre-half X/Z.
|
|
if (supported)
|
|
{
|
|
velocity.Y = jump ? HeldBounceSpeed : ReleasedBounceSpeed;
|
|
if (!jump) speed *= .5f;
|
|
}
|
|
return new(velocity, yaw, speed, supported && jump);
|
|
}
|
|
|
|
private static void Validate(float speed, float turn, float seconds)
|
|
{
|
|
if (!float.IsFinite(speed) || !float.IsFinite(turn) || !float.IsFinite(seconds)
|
|
|| seconds < 0 || seconds >= 1)
|
|
throw new ArgumentOutOfRangeException(nameof(seconds));
|
|
}
|
|
}
|