Initial import
This commit is contained in:
32
Assets/Scripts/NetCode/ConnectionMonitorSystem.cs
Normal file
32
Assets/Scripts/NetCode/ConnectionMonitorSystem.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using Unity.Burst;
|
||||
using Unity.Collections;
|
||||
using Unity.Entities;
|
||||
using Unity.NetCode;
|
||||
|
||||
namespace EE2Clone.NetCode
|
||||
{
|
||||
/// <summary>
|
||||
/// Server-side system that monitors for disconnected players and logs events.
|
||||
/// </summary>
|
||||
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)]
|
||||
public partial struct ConnectionMonitorSystem : ISystem
|
||||
{
|
||||
public void OnCreate(ref SystemState state)
|
||||
{
|
||||
state.RequireForUpdate<NetworkId>();
|
||||
}
|
||||
|
||||
public void OnUpdate(ref SystemState state)
|
||||
{
|
||||
// Count active connections for logging/debugging
|
||||
int connectionCount = 0;
|
||||
foreach (var _ in SystemAPI.Query<RefRO<NetworkId>>()
|
||||
.WithAll<NetworkStreamInGame>())
|
||||
{
|
||||
connectionCount++;
|
||||
}
|
||||
|
||||
// Future: detect disconnections and handle AI takeover
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/NetCode/ConnectionMonitorSystem.cs.meta
Normal file
2
Assets/Scripts/NetCode/ConnectionMonitorSystem.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fb810a5f20d826844a1063b2a900fa0a
|
||||
23
Assets/Scripts/NetCode/GameBootstrap.cs
Normal file
23
Assets/Scripts/NetCode/GameBootstrap.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System.Collections.Generic;
|
||||
using Unity.NetCode;
|
||||
|
||||
namespace EE2Clone.NetCode
|
||||
{
|
||||
/// <summary>
|
||||
/// Custom bootstrap that prevents automatic world creation.
|
||||
/// We manually create client/server worlds when the player chooses to host or connect.
|
||||
/// </summary>
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
public class GameBootstrap : ClientServerBootstrap
|
||||
{
|
||||
public override bool Initialize(string defaultWorldName)
|
||||
{
|
||||
// Create only the default (local) world on startup.
|
||||
// Client and server worlds are created manually when the user
|
||||
// starts hosting or joins a game.
|
||||
AutoConnectPort = 0;
|
||||
CreateDefaultClientServerWorlds();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/NetCode/GameBootstrap.cs.meta
Normal file
2
Assets/Scripts/NetCode/GameBootstrap.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 278d5b5c203631c42a5f1eb814815e6c
|
||||
52
Assets/Scripts/NetCode/GoInGameClientSystem.cs
Normal file
52
Assets/Scripts/NetCode/GoInGameClientSystem.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using Unity.Burst;
|
||||
using Unity.Collections;
|
||||
using Unity.Entities;
|
||||
using Unity.NetCode;
|
||||
|
||||
namespace EE2Clone.NetCode
|
||||
{
|
||||
/// <summary>
|
||||
/// RPC sent by the client to request going in-game.
|
||||
/// </summary>
|
||||
public struct GoInGameRequest : IRpcCommand
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Client-side system: when a connection is established and the streaming is done,
|
||||
/// send a GoInGameRequest RPC to the server.
|
||||
/// </summary>
|
||||
[BurstCompile]
|
||||
[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation | WorldSystemFilterFlags.ThinClientSimulation)]
|
||||
public partial struct GoInGameClientSystem : ISystem
|
||||
{
|
||||
[BurstCompile]
|
||||
public void OnCreate(ref SystemState state)
|
||||
{
|
||||
state.RequireForUpdate<NetworkId>();
|
||||
}
|
||||
|
||||
[BurstCompile]
|
||||
public void OnUpdate(ref SystemState state)
|
||||
{
|
||||
var ecb = new EntityCommandBuffer(Allocator.Temp);
|
||||
|
||||
foreach (var (networkId, entity) in
|
||||
SystemAPI.Query<RefRO<NetworkId>>()
|
||||
.WithNone<NetworkStreamInGame>()
|
||||
.WithEntityAccess())
|
||||
{
|
||||
ecb.AddComponent<NetworkStreamInGame>(entity);
|
||||
|
||||
var requestEntity = ecb.CreateEntity();
|
||||
ecb.AddComponent(requestEntity, new GoInGameRequest());
|
||||
ecb.AddComponent(requestEntity, new SendRpcCommandRequest { TargetConnection = entity });
|
||||
|
||||
UnityEngine.Debug.Log($"[Client] Sending GoInGame request (NetworkId={networkId.ValueRO.Value})");
|
||||
}
|
||||
|
||||
ecb.Playback(state.EntityManager);
|
||||
ecb.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/NetCode/GoInGameClientSystem.cs.meta
Normal file
2
Assets/Scripts/NetCode/GoInGameClientSystem.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d2bae91c6664cbd4796e97de29d6b1a7
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/NetCode/GoInGameServerSystem.cs.meta
Normal file
2
Assets/Scripts/NetCode/GoInGameServerSystem.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1c5c4cafaa538c64da4f890ef8658f40
|
||||
95
Assets/Scripts/NetCode/RpcCommands.cs
Normal file
95
Assets/Scripts/NetCode/RpcCommands.cs
Normal file
@@ -0,0 +1,95 @@
|
||||
using Unity.Entities;
|
||||
using Unity.Mathematics;
|
||||
using Unity.NetCode;
|
||||
using EE2Clone.Core;
|
||||
|
||||
namespace EE2Clone.NetCode
|
||||
{
|
||||
/// <summary>
|
||||
/// Client requests to move selected units to a position.
|
||||
/// </summary>
|
||||
public struct MoveCommandRpc : IRpcCommand
|
||||
{
|
||||
public Entity UnitEntity;
|
||||
public float3 TargetPosition;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Client requests to attack a target with selected units.
|
||||
/// </summary>
|
||||
public struct AttackCommandRpc : IRpcCommand
|
||||
{
|
||||
public Entity AttackerEntity;
|
||||
public Entity TargetEntity;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Client requests to place a building.
|
||||
/// </summary>
|
||||
public struct PlaceBuildingRpc : IRpcCommand
|
||||
{
|
||||
public BuildingType Type;
|
||||
public float3 Position;
|
||||
public quaternion Rotation;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Client requests to set a rally point on a building.
|
||||
/// </summary>
|
||||
public struct SetRallyPointRpc : IRpcCommand
|
||||
{
|
||||
public Entity BuildingEntity;
|
||||
public float3 Position;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Client requests to queue unit production in a building.
|
||||
/// </summary>
|
||||
public struct QueueUnitProductionRpc : IRpcCommand
|
||||
{
|
||||
public Entity BuildingEntity;
|
||||
public int UnitDataId;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Client requests to gather from a resource node.
|
||||
/// </summary>
|
||||
public struct GatherCommandRpc : IRpcCommand
|
||||
{
|
||||
public Entity CitizenEntity;
|
||||
public Entity ResourceNodeEntity;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Client requests to assign citizens to build/repair a building.
|
||||
/// </summary>
|
||||
public struct BuildRepairCommandRpc : IRpcCommand
|
||||
{
|
||||
public Entity CitizenEntity;
|
||||
public Entity BuildingEntity;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Client requests to research a technology.
|
||||
/// </summary>
|
||||
public struct ResearchTechRpc : IRpcCommand
|
||||
{
|
||||
public Entity BuildingEntity;
|
||||
public int TechId;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Client requests to advance to the next epoch.
|
||||
/// </summary>
|
||||
public struct EpochAdvanceRpc : IRpcCommand
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Client requests to stop selected units.
|
||||
/// </summary>
|
||||
public struct StopCommandRpc : IRpcCommand
|
||||
{
|
||||
public Entity UnitEntity;
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/NetCode/RpcCommands.cs.meta
Normal file
2
Assets/Scripts/NetCode/RpcCommands.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 62f6a545bb8db214a97318bcba211f01
|
||||
Reference in New Issue
Block a user