69 lines
3.0 KiB
C#
69 lines
3.0 KiB
C#
using ProjectM.Simulation;
|
|
using Unity.Burst;
|
|
using Unity.Collections;
|
|
using Unity.Entities;
|
|
using Unity.NetCode;
|
|
using Unity.Transforms;
|
|
|
|
namespace ProjectM.Server
|
|
{
|
|
/// <summary>
|
|
/// Server-authoritative player spawn. On each received <see cref="GoInGameRequest"/>: mark the
|
|
/// source connection in-game, instantiate the player ghost from the baked
|
|
/// <see cref="PlayerSpawner"/>, stamp <see cref="GhostOwner"/> with the connection's
|
|
/// <see cref="NetworkId"/>, place it at the spawn point, and link it to the connection's
|
|
/// LinkedEntityGroup so it auto-despawns on disconnect. Mirrors the netcode "networked-cube"
|
|
/// ModifiedGoInGameServer sample. All structural changes are batched through an
|
|
/// <see cref="EntityCommandBuffer"/>.
|
|
/// </summary>
|
|
[BurstCompile]
|
|
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)]
|
|
public partial struct GoInGameServerSystem : ISystem
|
|
{
|
|
[BurstCompile]
|
|
public void OnCreate(ref SystemState state)
|
|
{
|
|
state.RequireForUpdate<PlayerSpawner>();
|
|
|
|
var builder = new EntityQueryBuilder(Allocator.Temp)
|
|
.WithAll<GoInGameRequest, ReceiveRpcCommandRequest>();
|
|
state.RequireForUpdate(state.GetEntityQuery(builder));
|
|
}
|
|
|
|
[BurstCompile]
|
|
public void OnUpdate(ref SystemState state)
|
|
{
|
|
var spawner = SystemAPI.GetSingleton<PlayerSpawner>();
|
|
|
|
// M5 home base: re-root the spawn ring on the baked BaseAnchor when present; fall back
|
|
// to the spawner's SpawnPoint if the base subscene hasn't streamed in yet.
|
|
var center = spawner.SpawnPoint;
|
|
if (SystemAPI.TryGetSingleton<BaseAnchor>(out var baseAnchor))
|
|
center = BaseGridMath.PlotCenter(baseAnchor);
|
|
var ecb = new EntityCommandBuffer(Allocator.Temp);
|
|
|
|
foreach (var (receive, requestEntity) in
|
|
SystemAPI.Query<RefRO<ReceiveRpcCommandRequest>>().WithAll<GoInGameRequest>().WithEntityAccess())
|
|
{
|
|
var connection = receive.ValueRO.SourceConnection;
|
|
ecb.AddComponent<NetworkStreamInGame>(connection);
|
|
|
|
var networkId = SystemAPI.GetComponent<NetworkId>(connection);
|
|
|
|
var player = ecb.Instantiate(spawner.PlayerPrefab);
|
|
ecb.SetComponent(player, LocalTransform.FromPosition(center + PlayerSpawnMath.SpawnOffset(networkId.Value, spawner.SpawnRingRadius, spawner.RingSlots)));
|
|
ecb.SetComponent(player, new GhostOwner { NetworkId = networkId.Value });
|
|
// Tag the player into the base region (M6 region/relevancy split).
|
|
ecb.AddComponent(player, new RegionTag { Region = RegionId.Base });
|
|
|
|
// Auto-despawn the player when its owning connection is removed.
|
|
ecb.AppendToBuffer(connection, new LinkedEntityGroup { Value = player });
|
|
|
|
ecb.DestroyEntity(requestEntity);
|
|
}
|
|
|
|
ecb.Playback(state.EntityManager);
|
|
}
|
|
}
|
|
}
|