48 lines
1.8 KiB
C#
48 lines
1.8 KiB
C#
using ProjectM.Simulation;
|
|
using Unity.Burst;
|
|
using Unity.Collections;
|
|
using Unity.Entities;
|
|
using Unity.NetCode;
|
|
|
|
namespace ProjectM.Client
|
|
{
|
|
/// <summary>
|
|
/// Client-side connection handshake: for every connection that has been assigned a
|
|
/// <see cref="NetworkId"/> but is not yet <see cref="NetworkStreamInGame"/>, mark it in-game and
|
|
/// fire a <see cref="GoInGameRequest"/> RPC so the server spawns this client's player ghost.
|
|
/// Adding NetworkStreamInGame is what gates snapshot/command flow on. Mirrors the netcode
|
|
/// "networked-cube" go-in-game sample.
|
|
/// </summary>
|
|
[BurstCompile]
|
|
[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation | WorldSystemFilterFlags.ThinClientSimulation)]
|
|
public partial struct GoInGameClientSystem : ISystem
|
|
{
|
|
[BurstCompile]
|
|
public void OnCreate(ref SystemState state)
|
|
{
|
|
var builder = new EntityQueryBuilder(Allocator.Temp)
|
|
.WithAll<NetworkId>()
|
|
.WithNone<NetworkStreamInGame>();
|
|
state.RequireForUpdate(state.GetEntityQuery(builder));
|
|
}
|
|
|
|
[BurstCompile]
|
|
public void OnUpdate(ref SystemState state)
|
|
{
|
|
var ecb = new EntityCommandBuffer(Allocator.Temp);
|
|
|
|
foreach (var (_, connection) in
|
|
SystemAPI.Query<RefRO<NetworkId>>().WithNone<NetworkStreamInGame>().WithEntityAccess())
|
|
{
|
|
ecb.AddComponent<NetworkStreamInGame>(connection);
|
|
|
|
var request = ecb.CreateEntity();
|
|
ecb.AddComponent<GoInGameRequest>(request);
|
|
ecb.AddComponent(request, new SendRpcCommandRequest { TargetConnection = connection });
|
|
}
|
|
|
|
ecb.Playback(state.EntityManager);
|
|
}
|
|
}
|
|
}
|