using ProjectM.Simulation; using Unity.Burst; using Unity.Collections; using Unity.Entities; using Unity.Mathematics; using Unity.NetCode; using Unity.Transforms; namespace ProjectM.Server { /// /// Server-only Husk wave/threat director: a state machine that escalates the swarm. In Lull it waits /// until the lull timer expires, then starts the next wave (count = BaseCount + (wave-1)*CountPerWave). In /// Spawning it spawns one Husk every SpawnIntervalTicks at a deterministic ring slot around the /// , round-robin over the pool, until the wave is fully /// spawned; then it waits for the field to be cleared (no live ) before returning to /// Lull. Plain , server-authoritative (Husks are interpolated ghosts). /// Replaces the flat EnemySpawnSystem sustain. Tick gating uses the wrap-safe /// compare + . /// [BurstCompile] [WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)] [UpdateInGroup(typeof(SimulationSystemGroup))] public partial struct WaveSystem : ISystem { EntityQuery m_AliveHusks; [BurstCompile] public void OnCreate(ref SystemState state) { state.RequireForUpdate(); state.RequireForUpdate(); state.RequireForUpdate(); m_AliveHusks = state.GetEntityQuery(ComponentType.ReadOnly()); } [BurstCompile] public void OnUpdate(ref SystemState state) { var serverTick = SystemAPI.GetSingleton().ServerTick; if (!serverTick.IsValid) return; uint now = serverTick.TickIndexForValidTick; // Player-driven loop: the base-defense wave only spawns during a Siege. if (SystemAPI.TryGetSingleton(out var cycle) && cycle.Phase != CyclePhase.Siege) return; var director = SystemAPI.GetSingleton(); var directorEntity = SystemAPI.GetSingletonEntity(); var prefabs = SystemAPI.GetBuffer(directorEntity); if (prefabs.Length == 0) return; var wave = SystemAPI.GetComponent(directorEntity); // Ring centre on the base plot when present. float3 center = new float3(0f, 1f, 0f); if (SystemAPI.TryGetSingleton(out var baseAnchor)) center = BaseGridMath.PlotCenter(baseAnchor); // Due when no action is scheduled yet (NextActionTick 0) or the scheduled tick is at/behind now. bool dueNow = wave.NextActionTick == 0 || !new NetworkTick(wave.NextActionTick).IsNewerThan(serverTick); if (wave.Phase == WavePhase.Lull) { if (dueNow) { // Start the next (bigger) wave. wave.WaveNumber += 1; wave.RemainingToSpawn = math.max(1, director.BaseCount + (wave.WaveNumber - 1) * director.CountPerWave); wave.Phase = WavePhase.Spawning; wave.NextActionTick = TickUtil.NonZero(now); // spawn the first Husk this tick } } else // Spawning { if (wave.RemainingToSpawn > 0) { if (dueNow) { int slots = math.max(1, director.RingSlots); int prefabIdx = wave.SpawnCounter % prefabs.Length; float3 pos = EnemyAIMath.RingPosition(center, wave.SpawnCounter, slots, director.RingRadius); pos.y = center.y; var ecb = new EntityCommandBuffer(Allocator.Temp); var husk = ecb.Instantiate(prefabs[prefabIdx].Prefab); ecb.SetComponent(husk, LocalTransform.FromPosition(pos)); // Husks belong to the base region (hidden from expedition players by relevancy). ecb.AddComponent(husk, new RegionTag { Region = RegionId.Base }); ecb.Playback(state.EntityManager); ecb.Dispose(); wave.SpawnCounter += 1; wave.RemainingToSpawn -= 1; wave.NextActionTick = TickUtil.NonZero(now + (uint)math.max(1, director.SpawnIntervalTicks)); } } else if (m_AliveHusks.CalculateEntityCount() == 0) { // Wave cleared: calm before the next. wave.Phase = WavePhase.Lull; wave.NextActionTick = TickUtil.NonZero(now + (uint)math.max(1, director.LullTicks)); } } SystemAPI.SetComponent(directorEntity, wave); } } }