Files
kronic a7fdd6f71d Slice 2 (WIP): class carrier (GoInGameRequest.ClassId) + Warrior cone archetype
The per-player class travels on GoInGameRequest.ClassId (client reads a ClassSelection
singleton); GoInGameServerSystem seeds the class at spawn via ClassTraits (AbilityRef +
permanent trait StatModifiers on a reserved ClassSourceId; CharacterStatsRef stays Default
so the DRG-asymmetry deltas ride the replicated OwnerSendType.All buffer). AbilityFireSystem
gains the aim-directed Cone archetype: cooldown predicted both worlds, server-only cone
damage to living enemies (same-tick, SourceTick-stamped, like the melee cleave). 345/345.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-18 00:30:20 -07:00

75 lines
3.6 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, goReq, requestEntity) in
SystemAPI.Query<RefRO<ReceiveRpcCommandRequest>, RefRO<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 });
// Slice 2: seed the chosen class on the just-instantiated player. AbilityRef selects the Fire slot
// (Warrior = cone / Ranger = projectile); the DRG-asymmetry traits ride permanent StatModifiers
// (CharacterStatsRef stays Default -> deltas replicate via the OwnerSendType.All buffer). 0 -> Warrior.
byte classId = ClassTraits.Normalize(goReq.ValueRO.ClassId);
ecb.SetComponent(player, new AbilityRef { Id = ClassTraits.AbilityFor(classId) });
ClassTraits.AppendSeeds(classId, player, ecb);
// Auto-despawn the player when its owning connection is removed.
ecb.AppendToBuffer(connection, new LinkedEntityGroup { Value = player });
ecb.DestroyEntity(requestEntity);
}
ecb.Playback(state.EntityManager);
}
}
}