Files
Project-M/Assets/_Project/Scripts/Client/Input/PlayerInputGatherSystem.cs
T
2026-05-31 21:35:12 -07:00

80 lines
3.4 KiB
C#

using ProjectM.Simulation;
using Unity.Entities;
using Unity.Mathematics;
using Unity.NetCode;
namespace ProjectM.Client
{
/// <summary>
/// Client-only twin-stick input gather. Samples the new Input System action map (the generated
/// <c>ProjectMInput</c> wrapper over <c>Assets/Settings/Project M Input.inputactions</c>) once per
/// frame and writes <see cref="PlayerInput"/> on the locally-owned player ghost (filtered to
/// <see cref="GhostOwnerIsLocal"/>). Runs in <see cref="GhostInputSystemGroup"/> — NOT the
/// prediction loop — so devices are read once per frame, never re-read during rollback.
/// <para>
/// Implemented as a managed <see cref="SystemBase"/> (not a Burst <c>ISystem</c>) because it holds
/// and reads the managed Input System wrapper. Fire is an <see cref="InputEvent"/>: the event field
/// is reset each frame and raised via <c>Set()</c> on the press edge, so a single click fires
/// exactly once; netcode accumulates the absolute <c>Count</c> into the command buffer across the
/// frame→tick boundary (read back in <c>AbilityFireSystem</c> as the predicted-spawn key).
/// </para>
/// <para>
/// NOTE: Input System types are fully qualified (e.g. <c>UnityEngine.Vector2</c>) and
/// <c>using UnityEngine.InputSystem;</c> is intentionally omitted — that namespace defines a
/// <c>PlayerInput</c> type that collides with <see cref="ProjectM.Simulation.PlayerInput"/> and
/// makes the Entities generator bind <c>RefRW&lt;PlayerInput&gt;</c> to the managed class (a
/// spurious CS8377). The generated <c>ProjectMInput</c> wrapper lives in this assembly.
/// </para>
/// </summary>
[UpdateInGroup(typeof(GhostInputSystemGroup))]
[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation)]
public partial class PlayerInputGatherSystem : SystemBase
{
private ProjectMInput _controls;
protected override void OnCreate()
{
RequireForUpdate<PlayerInput>();
_controls = new ProjectMInput();
_controls.Gameplay.Enable();
}
protected override void OnDestroy()
{
if (_controls != null)
{
_controls.Gameplay.Disable();
_controls.Dispose();
_controls = null;
}
}
protected override void OnUpdate()
{
var gameplay = _controls.Gameplay;
float2 move = (float2)gameplay.Move.ReadValue<UnityEngine.Vector2>();
float2 aim = (float2)gameplay.Aim.ReadValue<UnityEngine.Vector2>();
// Right-stick deadzone: a resting stick yields zero Aim so PlayerAimSystem falls back to the
// movement heading (controller-first directional aim).
if (math.lengthsq(aim) < 0.04f)
aim = float2.zero;
bool firePressed = gameplay.Fire.WasPressedThisFrame();
foreach (var input in SystemAPI.Query<RefRW<PlayerInput>>().WithAll<GhostOwnerIsLocal>())
{
input.ValueRW.Move = move;
input.ValueRW.Aim = aim;
// Reset the per-frame event, then raise it on the press edge. Netcode latches the
// absolute Count into the command buffer; AbilityFireSystem reads it as the SpawnId key.
input.ValueRW.Fire = default;
if (firePressed)
input.ValueRW.Fire.Set();
}
}
}
}