using ProjectM.Simulation;
using Unity.Entities;
using Unity.NetCode;
namespace ProjectM.Client
{
///
/// Client-only sender for RPCs (move the local player's PERSONAL
/// inventory into the shared base stockpile / global ledger). A one-off action (not per-tick predicted
/// input), so it is an RPC: on the deposit key edge (G = deposit ALL) it creates the request entity
/// targeted at the server connection, and the server applies it authoritatively in
/// . Managed SystemBase because it reads the managed
/// Input System; Input System types are fully qualified and using UnityEngine.InputSystem; is
/// intentionally omitted (that namespace defines a PlayerInput type that collides with
/// ). An editor-only static hook ()
/// drives the same path from execute_code for headless validation without a focused Game view. The wire
/// type is unconditional; only this send SYSTEM is build-time managed.
///
[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation)]
public partial class InventoryDepositSendSystem : SystemBase
{
#if UNITY_EDITOR
struct PendingDeposit { public ushort ItemId; public int Count; }
static readonly System.Collections.Generic.Queue s_Pending =
new System.Collections.Generic.Queue();
/// EDITOR / execute_code hook: queue a deposit (ItemId 0 = deposit all; Count <= 0 = all of that item).
public static void Deposit(ushort itemId = 0, int count = 0) =>
s_Pending.Enqueue(new PendingDeposit { ItemId = itemId, Count = count });
#endif
protected override void OnCreate()
{
RequireForUpdate();
}
protected override void OnUpdate()
{
// Need the server connection to target the RPC; bail (keeping any queued ops) until connected.
if (!SystemAPI.TryGetSingletonEntity(out var connection))
return;
var keyboard = UnityEngine.InputSystem.Keyboard.current;
if (keyboard != null && keyboard.gKey.wasPressedThisFrame)
Send(connection, 0, 0); // G -> deposit everything to the base stockpile
#if UNITY_EDITOR
while (s_Pending.Count > 0)
{
var d = s_Pending.Dequeue();
Send(connection, d.ItemId, d.Count);
}
#endif
}
void Send(Entity connection, ushort itemId, int count)
{
var request = EntityManager.CreateEntity();
EntityManager.AddComponentData(request, new InventoryDepositRequest { ItemId = itemId, Count = count });
EntityManager.AddComponentData(request, new SendRpcCommandRequest { TargetConnection = connection });
}
}
}