using ProjectM.Simulation;
using Unity.Entities;
using Unity.NetCode;
using UnityEngine;
namespace ProjectM.Client
{
///
/// Client-side ready-toggle sender: a static enqueue (HUD button at Step 14 / the T dev key / execute_code)
/// drained into RPC entities — the BuildSendSystem queue+drain idiom. The local
/// bool tracks only the toggle DIRECTION; the server-replicated is the truth the HUD
/// renders. Statics reset on play-enter (statics survive fast-enter-playmode reloads — the stale-bridge hazard).
///
[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;
}
/// Queue an explicit ready set (HUD button / execute_code).
public static void SetReady(bool ready)
{
s_PendingValue = (byte)(ready ? 1 : 0);
s_Pending++;
s_LocalReady = ready;
}
/// Queue a toggle of the last requested state (the T dev key; HUD replaces this at Step 14).
public static void ToggleReady() => SetReady(!s_LocalReady);
protected override void OnCreate()
{
RequireForUpdate();
}
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 });
}
}
}
}