Files
Project-M/Assets/_Project/Scripts/Client/Input/PlayerInputGatherSystem.cs
T
kronic 3409c53148 Combat: MC-4 combo-chain melee as the primary verb (DR-030)
Melee combo (left-click / pad-West) becomes the player's primary verb; the ranged projectile is demoted to right-click / pad-left-trigger. Predicted, owner-replicated combo Step (path-dependent -> [GhostField] anchor + absolute-write idempotency, NOT derived like the dash), server-only cleave mirroring ProjectileDamageSystem (SourceTick-stamped DamageEvent + KnockbackState), dash-cancellable movement-commit, 9 live TuningConfig knobs, and swing juice scaling with the combo step. The MC-6 archetype byte is deferred (the melee is its own verb). See DR-030.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-10 17:22:57 -07:00

188 lines
9.9 KiB
C#

using ProjectM.Simulation;
using Unity.Entities;
using Unity.Mathematics;
using Unity.NetCode;
using Unity.Transforms;
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;
// Active input scheme (last meaningful actuation wins). Selects KBM-cursor vs gamepad-stick aim and
// is published to AimPresentation for the on-screen cursor / world reticle. Starts on mouse+keyboard.
private byte _scheme = InputSchemeId.KeyboardMouse;
// Last valid KBM cursor aim, held when the projection is degenerate (cursor over the player / above the
// horizon) so facing never snaps to zero while the mouse is the active device.
private float2 _lastKbmAim = new float2(0f, 1f);
// Main camera for cursor -> ground projection; re-resolved when null (e.g. after a domain reload).
private UnityEngine.Camera _camera;
// World radius around the player inside which mouse facing is HELD (no spin when the cursor passes
// over/near the character). Passed to AimMath.PlanarAimFromRay's deadZoneRadius.
private const float KbmAimDeadZone = 0.6f;
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;
// Movement is source-agnostic (WASD or left stick) — read from the merged action.
float2 move = (float2)gameplay.Move.ReadValue<UnityEngine.Vector2>();
// MC-4: melee (left-click / pad West) is the PRIMARY verb; ranged (right-click / pad left-trigger) the secondary poke - both read as direct device reads below (after the device locals).
// --- Active-device detection: last meaningful actuation wins; hold last when idle ---
var gamepad = UnityEngine.InputSystem.Gamepad.current;
var mouse = UnityEngine.InputSystem.Mouse.current;
var keyboard = UnityEngine.InputSystem.Keyboard.current;
bool dashPressed = ((keyboard != null && keyboard.leftShiftKey.wasPressedThisFrame) || (gamepad != null && gamepad.buttonEast.wasPressedThisFrame)) && !BuildPaletteState.Active;
// MC-4 offense rebind: melee combo = PRIMARY (left-click / pad West); ranged projectile demoted to right-click / pad left-trigger. Both suppressed while placing a build (like dash/old fire).
bool attackPressed = ((mouse != null && mouse.leftButton.wasPressedThisFrame) || (gamepad != null && gamepad.buttonWest.wasPressedThisFrame)) && !BuildPaletteState.Active;
bool firePressed = ((mouse != null && mouse.rightButton.wasPressedThisFrame) || (gamepad != null && gamepad.leftTrigger.wasPressedThisFrame)) && !BuildPaletteState.Active;
float2 rightStick = float2.zero;
bool gamepadActive = false;
if (gamepad != null)
{
rightStick = (float2)gamepad.rightStick.ReadValue();
float2 leftStick = (float2)gamepad.leftStick.ReadValue();
gamepadActive =
math.lengthsq(rightStick) > 0.04f || math.lengthsq(leftStick) > 0.04f ||
gamepad.rightTrigger.isPressed || gamepad.leftTrigger.isPressed ||
gamepad.buttonSouth.isPressed || gamepad.buttonEast.isPressed ||
gamepad.buttonWest.isPressed || gamepad.buttonNorth.isPressed;
}
bool kbmActive = false;
if (mouse != null)
{
float2 delta = (float2)mouse.delta.ReadValue();
kbmActive = math.lengthsq(delta) > 0.5f || mouse.leftButton.isPressed || mouse.rightButton.isPressed;
}
if (!kbmActive && keyboard != null)
{
kbmActive =
keyboard.wKey.isPressed || keyboard.aKey.isPressed || keyboard.sKey.isPressed || keyboard.dKey.isPressed ||
keyboard.upArrowKey.isPressed || keyboard.downArrowKey.isPressed ||
keyboard.leftArrowKey.isPressed || keyboard.rightArrowKey.isPressed || keyboard.spaceKey.isPressed || keyboard.leftShiftKey.isPressed;
}
if (gamepadActive && kbmActive)
{
// Both actuated this frame: break the tie by whichever device updated most recently.
double gpT = gamepad != null ? gamepad.lastUpdateTime : double.MinValue;
double kbmT = math.max(
mouse != null ? mouse.lastUpdateTime : double.MinValue,
keyboard != null ? keyboard.lastUpdateTime : double.MinValue);
_scheme = gpT > kbmT ? InputSchemeId.Gamepad : InputSchemeId.KeyboardMouse;
}
else if (gamepadActive) _scheme = InputSchemeId.Gamepad;
else if (kbmActive) _scheme = InputSchemeId.KeyboardMouse;
// else: neither actuated — keep the previous scheme.
// Publish for the presentation layer (cursor visibility / world reticle).
AimPresentation.Scheme = _scheme;
// Mouse cursor projection needs the camera; resolve/cache it (Camera.main or the rig's camera).
if (_camera == null) _camera = ResolveCamera();
UnityEngine.Vector2 cursorScreen = mouse != null ? mouse.position.ReadValue() : default;
foreach (var (input, xform) in
SystemAPI.Query<RefRW<PlayerInput>, RefRO<LocalTransform>>().WithAll<GhostOwnerIsLocal>())
{
float2 aim;
if (_scheme == InputSchemeId.Gamepad)
{
// Right-stick directional aim. A resting stick yields zero so PlayerAimSystem falls back to
// the movement heading (existing controller-first behaviour).
aim = rightStick;
if (math.lengthsq(aim) < 0.04f) aim = float2.zero;
}
else
{
// Mouse: face the cursor. Project the cursor ray onto the player's movement plane and aim
// from the player toward that point. Held to the last valid aim when degenerate, so the
// character keeps facing the cursor even while standing still (the core jank fix).
float3 playerPos = xform.ValueRO.Position;
if (_camera != null && mouse != null)
{
UnityEngine.Ray ray = _camera.ScreenPointToRay(new UnityEngine.Vector3(cursorScreen.x, cursorScreen.y, 0f));
_lastKbmAim = AimMath.PlanarAimFromRay(
(float3)ray.origin, (float3)ray.direction, playerPos, playerPos.y, _lastKbmAim, KbmAimDeadZone);
}
aim = _lastKbmAim;
}
input.ValueRW.Move = move;
input.ValueRW.Aim = aim;
input.ValueRW.Scheme = _scheme;
// 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();
input.ValueRW.Dash = default;
if (dashPressed)
input.ValueRW.Dash.Set();
input.ValueRW.Attack = default;
if (attackPressed)
input.ValueRW.Attack.Set();
}
}
private UnityEngine.Camera ResolveCamera()
{
var cam = UnityEngine.Camera.main;
if (cam == null)
{
var rig = UnityEngine.Object.FindAnyObjectByType<PrototypeCameraRig>();
if (rig != null) cam = rig.GetComponent<UnityEngine.Camera>();
}
return cam;
}
}
}