112 lines
5.1 KiB
C#
112 lines
5.1 KiB
C#
using ProjectM.Simulation;
|
|
using Unity.Burst;
|
|
using Unity.Collections;
|
|
using Unity.Entities;
|
|
using Unity.Mathematics;
|
|
using Unity.NetCode;
|
|
using Unity.Transforms;
|
|
|
|
namespace ProjectM.Server
|
|
{
|
|
/// <summary>
|
|
/// Server-only Husk wave/threat director: a state machine that escalates the swarm. In <c>Lull</c> it waits
|
|
/// until the lull timer expires, then starts the next wave (count = <c>BaseCount + (wave-1)*CountPerWave</c>). In
|
|
/// <c>Spawning</c> it spawns one Husk every <c>SpawnIntervalTicks</c> at a deterministic ring slot around the
|
|
/// <see cref="BaseAnchor"/>, round-robin over the <see cref="WaveEnemyPrefab"/> pool, until the wave is fully
|
|
/// spawned; then it waits for the field to be cleared (no live <see cref="EnemyTag"/>) before returning to
|
|
/// <c>Lull</c>. Plain <see cref="SimulationSystemGroup"/>, server-authoritative (Husks are interpolated ghosts).
|
|
/// Replaces the flat <c>EnemySpawnSystem</c> sustain. Tick gating uses the wrap-safe <see cref="NetworkTick"/>
|
|
/// compare + <see cref="TickUtil.NonZero"/>.
|
|
/// </summary>
|
|
[BurstCompile]
|
|
[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)]
|
|
[UpdateInGroup(typeof(SimulationSystemGroup))]
|
|
public partial struct WaveSystem : ISystem
|
|
{
|
|
EntityQuery m_AliveHusks;
|
|
|
|
[BurstCompile]
|
|
public void OnCreate(ref SystemState state)
|
|
{
|
|
state.RequireForUpdate<WaveDirector>();
|
|
state.RequireForUpdate<WaveState>();
|
|
state.RequireForUpdate<NetworkTime>();
|
|
m_AliveHusks = state.GetEntityQuery(ComponentType.ReadOnly<EnemyTag>());
|
|
}
|
|
|
|
[BurstCompile]
|
|
public void OnUpdate(ref SystemState state)
|
|
{
|
|
var serverTick = SystemAPI.GetSingleton<NetworkTime>().ServerTick;
|
|
if (!serverTick.IsValid)
|
|
return;
|
|
uint now = serverTick.TickIndexForValidTick;
|
|
// M6 Aether Cycle: the base-defense wave only runs during the Defend phase.
|
|
if (SystemAPI.TryGetSingleton<CycleState>(out var cycle) && cycle.Phase != CyclePhase.Defend)
|
|
return;
|
|
|
|
var director = SystemAPI.GetSingleton<WaveDirector>();
|
|
var directorEntity = SystemAPI.GetSingletonEntity<WaveDirector>();
|
|
var prefabs = SystemAPI.GetBuffer<WaveEnemyPrefab>(directorEntity);
|
|
if (prefabs.Length == 0)
|
|
return;
|
|
|
|
var wave = SystemAPI.GetComponent<WaveState>(directorEntity);
|
|
|
|
// Ring centre on the base plot when present.
|
|
float3 center = new float3(0f, 1f, 0f);
|
|
if (SystemAPI.TryGetSingleton<BaseAnchor>(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);
|
|
}
|
|
}
|
|
}
|