61 lines
2.3 KiB
C#
61 lines
2.3 KiB
C#
using RpcComponents;
|
|
using Unity.Burst;
|
|
using Unity.Collections;
|
|
using Unity.Entities;
|
|
using Unity.NetCode;
|
|
|
|
namespace Client
|
|
{
|
|
[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation | WorldSystemFilterFlags.ThinClientSimulation)]
|
|
partial struct ClientRequestGameEntrySystem : ISystem
|
|
{
|
|
private EntityQuery _pendingNetworkIdQuery;
|
|
|
|
[BurstCompile]
|
|
public void OnCreate(ref SystemState state)
|
|
{
|
|
//Get all entities connected to the server (having networkID), but not in game yet (no NetworkStreamInGame)
|
|
var builder = new EntityQueryBuilder(Allocator.Temp).WithAll<NetworkId>().WithNone<NetworkStreamInGame>();
|
|
_pendingNetworkIdQuery = state.GetEntityQuery(builder);
|
|
|
|
//only run system when entities with these components exist
|
|
state.RequireForUpdate(_pendingNetworkIdQuery);
|
|
state.RequireForUpdate<ClientTeamRequest>();
|
|
}
|
|
|
|
|
|
[BurstCompile]
|
|
public void OnUpdate(ref SystemState state)
|
|
{
|
|
//Get Requested team and all network ids that want to join
|
|
var teamrequest = SystemAPI.GetSingleton<ClientTeamRequest>();
|
|
|
|
var ecb = SystemAPI.GetSingletonRW<EndSimulationEntityCommandBufferSystem.Singleton>().ValueRW
|
|
.CreateCommandBuffer(state.WorldUnmanaged);
|
|
|
|
var pendingNetworkIds = _pendingNetworkIdQuery.ToEntityArray(Allocator.Temp);
|
|
|
|
foreach (var pendingNetworkId in pendingNetworkIds)
|
|
{
|
|
//mark network ID as joined in game - it will receive Snapshots
|
|
ecb.AddComponent<NetworkStreamInGame>(pendingNetworkId);
|
|
|
|
//create entity that holds request
|
|
var requestTeamEntity = ecb.CreateEntity();
|
|
|
|
//Give entity Rpc command request and team request so that the server can handle it
|
|
ecb.AddComponent(requestTeamEntity, new TdTeamRequest
|
|
{
|
|
Value = teamrequest.Value,
|
|
Color = teamrequest.Color
|
|
});
|
|
|
|
ecb.AddComponent(requestTeamEntity, new SendRpcCommandRequest()
|
|
{
|
|
TargetConnection = pendingNetworkId
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|