Files
2026-06-03 13:46:13 -07:00

57 lines
2.3 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 upgrade-pickup spawner (mirrors TrainingDummySpawnSystem). On its first
/// update it reads the baked <see cref="UpgradePickupSpawner"/> singleton and instantiates
/// <c>Count</c> pickup ghosts in a row, spaced <c>Spacing</c> world-units apart along +X starting at
/// <c>Origin</c>, then destroys the singleton so the system idles (spawned exactly once). Runs in the
/// default <see cref="SimulationSystemGroup"/> (NOT the prediction loop); pickups replicate to clients
/// as interpolated ghosts. Structural changes are batched through an <see cref="EntityCommandBuffer"/>.
/// </summary>
[BurstCompile]
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)]
public partial struct UpgradePickupSpawnSystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate<UpgradePickupSpawner>();
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
var spawnerEntity = SystemAPI.GetSingletonEntity<UpgradePickupSpawner>();
var spawner = SystemAPI.GetComponent<UpgradePickupSpawner>(spawnerEntity);
var ecb = new EntityCommandBuffer(Allocator.Temp);
if (spawner.Prefab != Entity.Null)
{
// Preserve the prefab's baked scale/rotation (FromPosition would reset Scale to 1).
var baseXform = SystemAPI.GetComponent<LocalTransform>(spawner.Prefab);
for (int i = 0; i < spawner.Count; i++)
{
var pickup = ecb.Instantiate(spawner.Prefab);
var position = spawner.Origin + new float3(i * spawner.Spacing, 0f, 0f);
var xform = baseXform;
xform.Position = position;
ecb.SetComponent(pickup, xform);
}
}
// One-shot: remove the spawner so RequireForUpdate fails and the system idles.
ecb.DestroyEntity(spawnerEntity);
ecb.Playback(state.EntityManager);
}
}
}