using ProjectM.Simulation;
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
namespace ProjectM.Server
{
///
/// Server-only, one-shot upgrade-pickup spawner (mirrors TrainingDummySpawnSystem). On its first
/// update it reads the baked singleton and instantiates
/// Count pickup ghosts in a row, spaced Spacing world-units apart along +X starting at
/// Origin, then destroys the singleton so the system idles (spawned exactly once). Runs in the
/// default (NOT the prediction loop); pickups replicate to clients
/// as interpolated ghosts. Structural changes are batched through an .
///
[BurstCompile]
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)]
public partial struct UpgradePickupSpawnSystem : ISystem
{
[BurstCompile]
public void OnCreate(ref SystemState state)
{
state.RequireForUpdate();
}
[BurstCompile]
public void OnUpdate(ref SystemState state)
{
var spawnerEntity = SystemAPI.GetSingletonEntity();
var spawner = SystemAPI.GetComponent(spawnerEntity);
var ecb = new EntityCommandBuffer(Allocator.Temp);
if (spawner.Prefab != Entity.Null)
{
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);
ecb.SetComponent(pickup, LocalTransform.FromPosition(position));
}
}
// One-shot: remove the spawner so RequireForUpdate fails and the system idles.
ecb.DestroyEntity(spawnerEntity);
ecb.Playback(state.EntityManager);
}
}
}