Files
Project-M/Assets/_Project/Scripts/Server/Combat/WaveSystem.cs
T
kronic 2fcff9a7a1 Animate enemies: client-derived Rukhanka rigs (Werewolf/Kaiju Husks)
Extends the DR-022 player pipeline to Husk enemies. A Husk is an ownerless
interpolated ghost = structurally a remote player, so the new client-only
EnemyAnimationDriveSystem mirrors PlayerAnimationDriveSystem's remote path:
velocity from LocalTransform-delta (prevPos cache, pruned every frame), facing
from LocalTransform.Rotation (AnimParamMath.PlanarForward), maxSpeed from baked
EnemyStats, IsAttacking from the already-replicated AttackWindup telegraph. No
new [GhostField], no server/asmdef/ghost-hash change.

Monster-mash roster: Werewolf (Grunt), Werewolf-Undead (Swarmer), Kaiju (Brute),
built by the reusable, GUID-preserving EnemyRigTools editor tool (materials +
AC_EnemyTopDown + EnemyAttackWindup clip + 3 rigged prefabs). WaveSystem now
preserves the baked variant Scale (was reset to 1 by LocalTransform.FromPosition).

See DR-023. EditMode 208/208; validated in Play (rigs skin, scales replicate,
locomotion + attack telegraph drive correctly).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-06 23:30:03 -07:00

115 lines
5.4 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;
// Player-driven loop: the base-defense wave only spawns during a Siege.
if (SystemAPI.TryGetSingleton<CycleState>(out var cycle) && cycle.Phase != CyclePhase.Siege)
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);
// Preserve the prefab's baked variant Scale (a replicated [GhostField]) + rotation;
// LocalTransform.FromPosition() would reset Scale->1, shrinking/growing animated variants.
var baked = state.EntityManager.GetComponentData<LocalTransform>(prefabs[prefabIdx].Prefab);
ecb.SetComponent(husk, baked.WithPosition(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);
}
}
}