Add controllable lab walking checkpoint with collision and chase camera
This commit is contained in:
1 parent
37fa18094f
commit
58424120bb
26 files changed
+1848
-110
No files matched your search
@@ -0,0 +1,145 @@
|
||||
using System.Numerics;
|
||||
using GamePort.Assets;
|
||||
|
||||
namespace GamePort.Game;
|
||||
|
||||
public readonly record struct PlayerInput(bool Forward, bool Backward, bool Left, bool Right, bool Jump);
|
||||
public enum PlayerPose { Idle, Run, Backpedal, Jump, Fall }
|
||||
|
||||
/// <summary>
|
||||
/// No-gadget walking checkpoint. Recovered input/arithmetic is described in
|
||||
/// movement-checkpoint.md. Capsule, floor-derived pivot and 60Hz are port policies;
|
||||
/// original collision, intro, object interactions and animation timing remain open.
|
||||
/// </summary>
|
||||
public sealed class LabSimulation
|
||||
{
|
||||
public const float BodyRadius = 24; // Provisional, not a recovered collider dimension.
|
||||
private readonly SceneCollision collision;
|
||||
private readonly Vector3 sourceStart;
|
||||
private readonly float startYaw, modelMinimumY, rootHeight;
|
||||
private float targetYaw, jumpHeldTime;
|
||||
private int jumpState;
|
||||
public float BodyHeight { get; }
|
||||
public Vector3 Position { get; private set; } // Feet, not original actor root.
|
||||
public Vector3 Velocity { get; private set; }
|
||||
public float Yaw { get; private set; }
|
||||
public float Speed { get; private set; }
|
||||
public bool Grounded { get; private set; }
|
||||
public PlayerPose Pose => Grounded ? Speed < -1 ? PlayerPose.Backpedal : Speed > 1 ? PlayerPose.Run : PlayerPose.Idle
|
||||
: Velocity.Y > 0 ? PlayerPose.Jump : PlayerPose.Fall;
|
||||
public int Steps { get; private set; }
|
||||
public int Jumps { get; private set; }
|
||||
public int Landings { get; private set; }
|
||||
public int WallHits { get; private set; }
|
||||
public float Travel { get; private set; }
|
||||
public float MaximumRise { get; private set; }
|
||||
public Vector3 ActorRoot => Position + Vector3.UnitY * rootHeight;
|
||||
public Vector3 CameraEye { get; private set; }
|
||||
public Vector3 CameraTarget => ActorRoot + new Vector3(0, 99, 0);
|
||||
public Matrix4x4 Model => Matrix4x4.CreateRotationY(-Yaw * MathF.PI / 180)
|
||||
* Matrix4x4.CreateTranslation(Position - new Vector3(0, modelMinimumY, 0));
|
||||
|
||||
public LabSimulation(SceneCollision collision, Mesh standingPose, Vector3 start, float yaw)
|
||||
{
|
||||
this.collision = collision;
|
||||
sourceStart = start;
|
||||
startYaw = yaw;
|
||||
var bounds = standingPose.Bounds();
|
||||
modelMinimumY = bounds.Min.Y;
|
||||
BodyHeight = bounds.Max.Y - bounds.Min.Y;
|
||||
var floor = collision.GroundHeight(start, 2500)
|
||||
?? throw new InvalidDataException("No support below GAM startpoint");
|
||||
rootHeight = start.Y - floor;
|
||||
Reset();
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
var floor = collision.GroundHeight(sourceStart, 2500)!.Value;
|
||||
Position = new(sourceStart.X, floor + SceneCollision.Skin, sourceStart.Z);
|
||||
Velocity = Vector3.Zero;
|
||||
Yaw = targetYaw = startYaw;
|
||||
Speed = jumpHeldTime = 0;
|
||||
jumpState = 0;
|
||||
Grounded = true;
|
||||
Steps = Jumps = Landings = WallHits = 0;
|
||||
Travel = MaximumRise = 0;
|
||||
CameraEye = DesiredEye();
|
||||
}
|
||||
|
||||
// EXE 0x47F940 truncates, then degree lookup masks to the 16384-entry table.
|
||||
// Table generated with modern trig; x87 bit-identical table recovery is pending.
|
||||
public static (float Sin, float Cos) Trig(float degrees)
|
||||
{
|
||||
var index = (int)(degrees * 45.5111122f) & 0x3fff;
|
||||
var radians = index * (Math.Tau / 16384);
|
||||
return ((float)Math.Sin(radians), (float)Math.Cos(radians));
|
||||
}
|
||||
|
||||
public void Step(PlayerInput input)
|
||||
{
|
||||
const float dt = FixedStepClock.Step;
|
||||
var wasGrounded = Grounded;
|
||||
float lateral = 0;
|
||||
if (Grounded)
|
||||
{
|
||||
if (!input.Jump) jumpState = 0;
|
||||
float yawDelta = 0;
|
||||
if (input.Left) { yawDelta += 20; lateral = -1000; Speed = RecoveredMotion.AddSpeed(Speed); }
|
||||
else if (input.Right) { yawDelta -= 20; lateral = 1000; Speed = RecoveredMotion.AddSpeed(Speed); }
|
||||
if (input.Forward) { yawDelta *= 0.5f; Speed = RecoveredMotion.AddSpeed(Speed); }
|
||||
if (yawDelta != 0) targetYaw = Yaw + yawDelta;
|
||||
if (input.Backward) Speed = MathF.Max(Speed - 52.5f, -300);
|
||||
else if (!input.Forward) Speed = RecoveredMotion.ReleaseSpeed(Speed);
|
||||
}
|
||||
var yawError = MathF.IEEERemainder(targetYaw - Yaw, 360);
|
||||
Yaw = (Yaw + yawError * dt * 3.7f) % 360;
|
||||
var (sin, cos) = Trig(Yaw);
|
||||
var velocity = new Vector3(-sin * Speed, Velocity.Y, cos * Speed);
|
||||
if (Grounded) velocity += new Vector3(cos, 0, sin) * (lateral * dt);
|
||||
var impulse = Vector3.Zero;
|
||||
if (Grounded && input.Jump && jumpState == 0)
|
||||
{
|
||||
impulse.Y = RecoveredMotion.JumpImpulse;
|
||||
jumpState = 1;
|
||||
jumpHeldTime = 0;
|
||||
Grounded = false;
|
||||
Jumps++;
|
||||
}
|
||||
if (!Grounded && jumpState == 1)
|
||||
{
|
||||
if (!input.Jump) jumpState = 2;
|
||||
else if ((jumpHeldTime += dt) > 0.4f) jumpState = 0; // Zero-fuel jetpack path resets to FALL.
|
||||
}
|
||||
else if (!Grounded && jumpState == 2 && input.Jump)
|
||||
jumpState = 0; // Renewed hold enters the same disabled-gadget reset path.
|
||||
var displacement = RecoveredMotion.Integrate(ref velocity, ref impulse,
|
||||
RecoveredMotion.Gravity * 0.33f, RecoveredMotion.Gravity, dt);
|
||||
var before = Position;
|
||||
var result = collision.Move(Position, displacement, BodyRadius, BodyHeight);
|
||||
Position = result.Position;
|
||||
Grounded = result.Grounded && velocity.Y <= 0;
|
||||
if (Grounded && velocity.Y < 0 || result.HitCeiling && velocity.Y > 0) velocity.Y = 0;
|
||||
Velocity = velocity;
|
||||
if (!wasGrounded && Grounded) Landings++;
|
||||
if (result.HitWall) WallHits++;
|
||||
Travel += Vector2.Distance(new(before.X, before.Z), new(Position.X, Position.Z));
|
||||
MaximumRise = Math.Max(MaximumRise, Position.Y - sourceStart.Y + rootHeight);
|
||||
Steps++;
|
||||
var desired = DesiredEye();
|
||||
var delta = (desired - CameraEye) * new Vector3(1.6f, 1.2f, 1.6f) * dt;
|
||||
CameraEye = collision.ClipCamera(CameraTarget, CameraEye + delta);
|
||||
}
|
||||
|
||||
private Vector3 DesiredEye()
|
||||
{
|
||||
var (sin, cos) = Trig(Yaw);
|
||||
return collision.ClipCamera(CameraTarget, ActorRoot + new Vector3(sin * 350, 180, -cos * 350));
|
||||
}
|
||||
|
||||
public static PlayerInput SmokeInput(int step) => new(step < 120, false, false,
|
||||
step >= 120 && step < 180, step >= 72 && step < 84);
|
||||
|
||||
public bool SmokePassed => Steps >= 240 && Travel > 50 && MaximumRise > 20 && Jumps > 0 && Landings > 0 && Grounded;
|
||||
public string Report => $"Walk checkpoint: steps={Steps}, travel={Travel:F2}, jumps={Jumps}, landings={Landings}, max rise={MaximumRise:F2}, wall hits={WallHits}, grounded={Grounded}, feet={Position}.";
|
||||
}
|
||||
Reference in new issue
Block a user