Initial Combat Implementation

This commit is contained in:
Luis Gonzalez
2026-05-31 21:35:12 -07:00
parent 7fa77ce821
commit 1f647dd5e1
166 changed files with 93337 additions and 91 deletions
@@ -6,62 +6,73 @@ using Unity.NetCode;
namespace ProjectM.Client
{
/// <summary>
/// Client-only twin-stick input gather. Samples the Input System once per frame (WASD /
/// left-stick -&gt; Move, right-stick -&gt; Aim) 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. Implemented as a non-Burst <see cref="ISystem"/>
/// because it reads the managed Input System.
/// 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>
/// NOTE: the Input System device types are fully qualified rather than imported via
/// <c>using UnityEngine.InputSystem;</c> on purpose — that namespace also defines a
/// <c>PlayerInput</c> type which would collide with <see cref="ProjectM.Simulation.PlayerInput"/>
/// and make the Entities source generator bind <c>RefRW&lt;PlayerInput&gt;</c> to the managed
/// class (a spurious CS8377 "must be unmanaged").
/// 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 struct PlayerInputGatherSystem : ISystem
public partial class PlayerInputGatherSystem : SystemBase
{
public void OnCreate(ref SystemState state)
private ProjectMInput _controls;
protected override void OnCreate()
{
state.RequireForUpdate<PlayerInput>();
RequireForUpdate<PlayerInput>();
_controls = new ProjectMInput();
_controls.Gameplay.Enable();
}
public void OnUpdate(ref SystemState state)
protected override void OnDestroy()
{
float2 move = float2.zero;
float2 aim = float2.zero;
var keyboard = UnityEngine.InputSystem.Keyboard.current;
if (keyboard != null)
if (_controls != null)
{
if (keyboard.wKey.isPressed) move.y += 1f;
if (keyboard.sKey.isPressed) move.y -= 1f;
if (keyboard.dKey.isPressed) move.x += 1f;
if (keyboard.aKey.isPressed) move.x -= 1f;
_controls.Gameplay.Disable();
_controls.Dispose();
_controls = null;
}
}
var gamepad = UnityEngine.InputSystem.Gamepad.current;
if (gamepad != null)
{
float2 leftStick = gamepad.leftStick.ReadValue();
if (math.lengthsq(leftStick) > math.lengthsq(move))
move = leftStick;
protected override void OnUpdate()
{
var gameplay = _controls.Gameplay;
aim = gamepad.rightStick.ReadValue();
}
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).
// 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();
}
}
}