Initial import
This commit is contained in:
77
Assets/Scripts/NetCode/GoInGameServerSystem.cs
Normal file
77
Assets/Scripts/NetCode/GoInGameServerSystem.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
using Unity.Burst;
|
||||
using Unity.Collections;
|
||||
using Unity.Entities;
|
||||
using Unity.NetCode;
|
||||
using EE2Clone.Components;
|
||||
using EE2Clone.Core;
|
||||
|
||||
namespace EE2Clone.NetCode
|
||||
{
|
||||
/// <summary>
|
||||
/// Server-side system: processes GoInGameRequest RPCs, marks connections as in-game,
|
||||
/// and creates a PlayerState entity for each connecting player.
|
||||
/// </summary>
|
||||
[BurstCompile]
|
||||
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)]
|
||||
public partial struct GoInGameServerSystem : ISystem
|
||||
{
|
||||
private int _nextPlayerId;
|
||||
|
||||
[BurstCompile]
|
||||
public void OnCreate(ref SystemState state)
|
||||
{
|
||||
state.RequireForUpdate<GoInGameRequest>();
|
||||
_nextPlayerId = 1;
|
||||
}
|
||||
|
||||
[BurstCompile]
|
||||
public void OnUpdate(ref SystemState state)
|
||||
{
|
||||
var ecb = new EntityCommandBuffer(Allocator.Temp);
|
||||
|
||||
foreach (var (request, requestSource, requestEntity) in
|
||||
SystemAPI.Query<RefRO<GoInGameRequest>, RefRO<ReceiveRpcCommandRequest>>()
|
||||
.WithEntityAccess())
|
||||
{
|
||||
var connectionEntity = requestSource.ValueRO.SourceConnection;
|
||||
|
||||
// Mark the connection as in-game
|
||||
ecb.AddComponent<NetworkStreamInGame>(connectionEntity);
|
||||
|
||||
// Get the NetworkId for this connection
|
||||
var networkId = state.EntityManager.GetComponentData<NetworkId>(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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user