Files
Project-M/Assets/_Project/Scripts/Simulation/Player/PlayerMoveSystem.cs
T
2026-05-31 21:35:12 -07:00

42 lines
1.7 KiB
C#

using Unity.Burst;
using Unity.Entities;
using Unity.Mathematics;
using Unity.NetCode;
using Unity.Transforms;
namespace ProjectM.Simulation
{
/// <summary>
/// Canonical predicted system: advances each player's planar (XZ) position from twin-stick Move
/// input. Runs inside the prediction loop on the owning client (re-simulated on rollback) and
/// once per tick on the server; filtered to <see cref="Simulate"/> so only predicted ghosts move.
/// Deterministic by construction: uses <c>SystemAPI.Time.DeltaTime</c> (the fixed tick step)
/// only — no wall-clock, no <c>System.Random</c>. Move is clamped to unit length so diagonal
/// keyboard movement is not faster than cardinal. Move speed is the data-driven
/// <see cref="EffectiveCharacterStats.MoveSpeed"/> (authored base + active modifiers), recomputed
/// each tick by <see cref="StatRecomputeSystem"/> which runs before this system.
/// </summary>
[UpdateInGroup(typeof(PredictedSimulationSystemGroup))]
[BurstCompile]
public partial struct PlayerMoveSystem : ISystem
{
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
float dt = SystemAPI.Time.DeltaTime;
foreach (var (transform, input, stats) in
SystemAPI.Query<RefRW<LocalTransform>, RefRO<PlayerInput>, RefRO<EffectiveCharacterStats>>()
.WithAll<Simulate>())
{
float2 move = input.ValueRO.Move;
if (math.lengthsq(move) > 1f)
move = math.normalize(move);
float3 delta = new float3(move.x, 0f, move.y) * stats.ValueRO.MoveSpeed * dt;
transform.ValueRW.Position += delta;
}
}
}
}