using Unity.Burst; using Unity.Collections; using Unity.Entities; using Unity.NetCode; using EE2Clone.Components; using EE2Clone.Core; namespace EE2Clone.NetCode { /// /// Server-side system: processes GoInGameRequest RPCs, marks connections as in-game, /// and creates a PlayerState entity for each connecting player. /// [BurstCompile] [WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)] public partial struct GoInGameServerSystem : ISystem { private int _nextPlayerId; [BurstCompile] public void OnCreate(ref SystemState state) { state.RequireForUpdate(); _nextPlayerId = 1; } [BurstCompile] public void OnUpdate(ref SystemState state) { var ecb = new EntityCommandBuffer(Allocator.Temp); foreach (var (request, requestSource, requestEntity) in SystemAPI.Query, RefRO>() .WithEntityAccess()) { var connectionEntity = requestSource.ValueRO.SourceConnection; // Mark the connection as in-game ecb.AddComponent(connectionEntity); // Get the NetworkId for this connection var networkId = state.EntityManager.GetComponentData(connectionEntity); // Create a PlayerState entity for this player var playerEntity = ecb.CreateEntity(); ecb.AddComponent(playerEntity, new PlayerStateComponent { PlayerId = _nextPlayerId, CurrentEpoch = Epoch.StoneAge, PopulationCurrent = 0, PopulationMax = GameConstants.StartingPopulationCap, CivilizationId = 0, IsAlive = true }); ecb.AddComponent(playerEntity, new PlayerResourcesComponent { Food = 200, Wood = 200, Stone = 100, Gold = 100, Tin = 0 }); ecb.AddComponent(playerEntity, new GhostOwner { NetworkId = networkId.Value }); UnityEngine.Debug.Log($"[Server] Player {_nextPlayerId} (NetworkId={networkId.Value}) entered game"); _nextPlayerId++; // Destroy the request entity ecb.DestroyEntity(requestEntity); } ecb.Playback(state.EntityManager); ecb.Dispose(); } } }