72 lines
3.1 KiB
C#
72 lines
3.1 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 handler for <see cref="RegionTransitRequest"/> RPCs. Resolves the sender's player
|
|
/// (via the source connection's <see cref="NetworkId"/> -> <see cref="GhostOwner"/>), flips its
|
|
/// <see cref="RegionTag"/> to the requested region, and teleports it to that region's origin
|
|
/// (<see cref="RegionMath.RegionOrigin"/>, centered on the base via <see cref="BaseGridMath.PlotCenter"/>).
|
|
/// Runs in the default server SimulationSystemGroup (NOT the prediction loop) so the transit applies once;
|
|
/// the next snapshot reconciles the owner-predicted client and <see cref="RegionRelevancySystem"/> re-scopes
|
|
/// which region's ghosts the connection receives. Mirrors the <see cref="StorageOpReceiveSystem"/> RPC shape.
|
|
/// </summary>
|
|
[BurstCompile]
|
|
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)]
|
|
public partial struct RegionTransitSystem : ISystem
|
|
{
|
|
[BurstCompile]
|
|
public void OnCreate(ref SystemState state)
|
|
{
|
|
state.RequireForUpdate<BaseAnchor>();
|
|
|
|
var builder = new EntityQueryBuilder(Allocator.Temp)
|
|
.WithAll<RegionTransitRequest, ReceiveRpcCommandRequest>();
|
|
state.RequireForUpdate(state.GetEntityQuery(builder));
|
|
}
|
|
|
|
[BurstCompile]
|
|
public void OnUpdate(ref SystemState state)
|
|
{
|
|
var baseCenter = BaseGridMath.PlotCenter(SystemAPI.GetSingleton<BaseAnchor>());
|
|
|
|
// Map connection NetworkId -> player entity.
|
|
var playerByConn = new NativeHashMap<int, Entity>(8, Allocator.Temp);
|
|
foreach (var (owner, entity) in
|
|
SystemAPI.Query<RefRO<GhostOwner>>().WithAll<PlayerTag, RegionTag>().WithEntityAccess())
|
|
{
|
|
playerByConn[owner.ValueRO.NetworkId] = entity;
|
|
}
|
|
|
|
var ecb = new EntityCommandBuffer(Allocator.Temp);
|
|
|
|
foreach (var (request, receive, requestEntity) in
|
|
SystemAPI.Query<RefRO<RegionTransitRequest>, RefRO<ReceiveRpcCommandRequest>>().WithEntityAccess())
|
|
{
|
|
var connEntity = receive.ValueRO.SourceConnection;
|
|
if (SystemAPI.HasComponent<NetworkId>(connEntity))
|
|
{
|
|
int connId = SystemAPI.GetComponent<NetworkId>(connEntity).Value;
|
|
if (playerByConn.TryGetValue(connId, out var player))
|
|
{
|
|
byte target = request.ValueRO.TargetRegion;
|
|
SystemAPI.GetComponentRW<RegionTag>(player).ValueRW.Region = target;
|
|
SystemAPI.GetComponentRW<LocalTransform>(player).ValueRW.Position =
|
|
RegionMath.RegionOrigin(target, baseCenter);
|
|
}
|
|
}
|
|
|
|
ecb.DestroyEntity(requestEntity);
|
|
}
|
|
|
|
ecb.Playback(state.EntityManager);
|
|
playerByConn.Dispose();
|
|
}
|
|
}
|
|
}
|