This commit is contained in:
2026-06-03 18:35:05 -07:00
parent 9091388bc2
commit 79ff06a7df
55 changed files with 107 additions and 3563 deletions
@@ -20,8 +20,9 @@ namespace ProjectM.Simulation
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
foreach (var (facing, transform, input) in
SystemAPI.Query<RefRW<PlayerFacing>, RefRW<LocalTransform>, RefRO<PlayerInput>>()
float dt = SystemAPI.Time.DeltaTime;
foreach (var (facing, transform, input, stats) in
SystemAPI.Query<RefRW<PlayerFacing>, RefRW<LocalTransform>, RefRO<PlayerInput>, RefRO<EffectiveCharacterStats>>()
.WithAll<Simulate>().WithDisabled<Dead>())
{
float2 aim = input.ValueRO.Aim;
@@ -31,9 +32,36 @@ namespace ProjectM.Simulation
continue; // no input this tick: keep last facing
aim = math.normalize(aim);
facing.ValueRW.Direction = aim;
float3 forward = new float3(aim.x, 0f, aim.y);
// Rate-limited turn: rotate the current facing toward the aim target by at most
// TurnRateRadiansPerSec * dt this tick. Deterministic (pure planar math, fixed-step dt)
// so it replays correctly on rollback; the first tick (uninitialized facing) snaps.
float2 cur = facing.ValueRO.Direction;
float2 dir;
if (math.lengthsq(cur) < 1e-6f)
{
dir = aim; // uninitialized facing -> snap to target
}
else
{
cur = math.normalize(cur);
float maxStep = stats.ValueRO.TurnRateRadiansPerSec * dt;
float angle = math.acos(math.clamp(math.dot(cur, aim), -1f, 1f));
if (angle <= maxStep)
{
dir = aim; // within reach this tick
}
else
{
float sign = (cur.x * aim.y - cur.y * aim.x) >= 0f ? 1f : -1f;
math.sincos(maxStep * sign, out float sn, out float cs);
dir = math.normalize(new float2(cur.x * cs - cur.y * sn, cur.x * sn + cur.y * cs));
}
}
facing.ValueRW.Direction = dir;
float3 forward = new float3(dir.x, 0f, dir.y);
transform.ValueRW.Rotation = quaternion.LookRotationSafe(forward, math.up());
}
}