53 lines
2.2 KiB
C#
53 lines
2.2 KiB
C#
using ProjectM.Simulation;
|
|
using Unity.Burst;
|
|
using Unity.Collections;
|
|
using Unity.Entities;
|
|
using Unity.Mathematics;
|
|
using Unity.Transforms;
|
|
|
|
namespace ProjectM.Server
|
|
{
|
|
/// <summary>
|
|
/// Server-only, one-shot dummy spawner. On its first update it reads the baked
|
|
/// <see cref="TrainingDummySpawner"/> singleton and instantiates <c>Count</c> training-dummy
|
|
/// ghosts in a row, spaced <c>Spacing</c> world-units apart along +X starting at <c>Origin</c>.
|
|
/// It then destroys the spawner singleton entity so <c>RequireForUpdate<TrainingDummySpawner></c>
|
|
/// is no longer satisfied and the system stops running (idempotent: dummies are spawned exactly once).
|
|
/// Runs in the default <see cref="SimulationSystemGroup"/> (NOT the prediction loop) since spawning is
|
|
/// a non-predicted, server-authoritative event; the dummies replicate to clients as interpolated ghosts.
|
|
/// All structural changes are batched through an <see cref="EntityCommandBuffer"/>.
|
|
/// </summary>
|
|
[BurstCompile]
|
|
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)]
|
|
public partial struct TrainingDummySpawnSystem : ISystem
|
|
{
|
|
[BurstCompile]
|
|
public void OnCreate(ref SystemState state)
|
|
{
|
|
state.RequireForUpdate<TrainingDummySpawner>();
|
|
}
|
|
|
|
[BurstCompile]
|
|
public void OnUpdate(ref SystemState state)
|
|
{
|
|
// Grab both the singleton entity (to destroy when done) and its baked config.
|
|
var spawnerEntity = SystemAPI.GetSingletonEntity<TrainingDummySpawner>();
|
|
var spawner = SystemAPI.GetComponent<TrainingDummySpawner>(spawnerEntity);
|
|
|
|
var ecb = new EntityCommandBuffer(Allocator.Temp);
|
|
|
|
for (int i = 0; i < spawner.Count; i++)
|
|
{
|
|
var dummy = ecb.Instantiate(spawner.Prefab);
|
|
var position = spawner.Origin + new float3(i * spawner.Spacing, 0f, 0f);
|
|
ecb.SetComponent(dummy, LocalTransform.FromPosition(position));
|
|
}
|
|
|
|
// One-shot: remove the spawner so RequireForUpdate fails and the system idles.
|
|
ecb.DestroyEntity(spawnerEntity);
|
|
|
|
ecb.Playback(state.EntityManager);
|
|
}
|
|
}
|
|
}
|