Files
Project-M/Assets/_Project/Scripts/Client/World/ReadySendSystem.cs
T
2026-07-02 20:41:43 -07:00

60 lines
2.3 KiB
C#

using ProjectM.Simulation;
using Unity.Entities;
using Unity.NetCode;
using UnityEngine;
namespace ProjectM.Client
{
/// <summary>
/// Client-side ready-toggle sender: a static enqueue (HUD button at Step 14 / the T dev key / execute_code)
/// drained into <see cref="ReadyToggleRequest"/> RPC entities — the BuildSendSystem queue+drain idiom. The local
/// bool tracks only the toggle DIRECTION; the server-replicated <see cref="PlayerReady"/> is the truth the HUD
/// renders. Statics reset on play-enter (statics survive fast-enter-playmode reloads — the stale-bridge hazard).
/// </summary>
[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation)]
public partial class ReadySendSystem : SystemBase
{
static int s_Pending; // queued explicit sets
static byte s_PendingValue;
static bool s_LocalReady; // last requested state (toggle direction only, not authority)
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
static void ResetStatics()
{
s_Pending = 0;
s_PendingValue = 0;
s_LocalReady = false;
}
/// <summary>Queue an explicit ready set (HUD button / execute_code).</summary>
public static void SetReady(bool ready)
{
s_PendingValue = (byte)(ready ? 1 : 0);
s_Pending++;
s_LocalReady = ready;
}
/// <summary>Queue a toggle of the last requested state (the T dev key; HUD replaces this at Step 14).</summary>
public static void ToggleReady() => SetReady(!s_LocalReady);
protected override void OnCreate()
{
RequireForUpdate<NetworkId>();
}
protected override void OnUpdate()
{
var keyboard = UnityEngine.InputSystem.Keyboard.current;
if (keyboard != null && keyboard.tKey.wasPressedThisFrame && !PauseMenuController.Open)
ToggleReady();
while (s_Pending > 0)
{
s_Pending--;
var req = EntityManager.CreateEntity(typeof(ReadyToggleRequest), typeof(SendRpcCommandRequest));
EntityManager.SetComponentData(req, new ReadyToggleRequest { Ready = s_PendingValue });
}
}
}
}