using Unity.Burst;
using Unity.Entities;
using Unity.Mathematics;
using Unity.NetCode;
using Unity.Transforms;
namespace ProjectM.Simulation
{
///
/// Predicted aim/facing: writes from twin-stick Aim, falling back to
/// the movement direction when Aim is zero (controller-first directional aim). Also turns the
/// ghost transform toward the facing direction for top-down presentation. When there is no input
/// this tick the previous facing is held. Deterministic (pure math); filtered to
/// so it runs only for predicted ghosts.
///
[UpdateInGroup(typeof(PredictedSimulationSystemGroup))]
[BurstCompile]
public partial struct PlayerAimSystem : ISystem
{
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
foreach (var (facing, transform, input) in
SystemAPI.Query, RefRW, RefRO>()
.WithAll())
{
float2 aim = input.ValueRO.Aim;
if (math.lengthsq(aim) < 1e-6f)
aim = input.ValueRO.Move; // fall back to movement heading
if (math.lengthsq(aim) < 1e-6f)
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);
transform.ValueRW.Rotation = quaternion.LookRotationSafe(forward, math.up());
}
}
}
}