Files
Project-M/Assets/_Project/Scripts/Authoring/Combat/EnemyAuthoring.cs
T
kronic 56cf60cce3 Slice Combat Depth (MC-2): enemy-variety server spine — Spitter, Swarmer, 4-type mix (DR-041)
Adds the server-authoritative mechanics for three new enemy archetypes on top of
the Grunt/Charger base, plus the weighted wave-composition that introduces them:

- Spitter: a ranged Husk variant (SpitterState) that holds a preferred range-band
  (advance/retreat/hold via EnemyAIMath.BandVelocity) and fires a telegraphed,
  dodgeable EnemyProjectile. New server EnemyProjectileMoveSystem (integrate +
  store LastStep) + EnemyProjectileDamageSystem (region-filtered swept hit-test
  rebuilt from LastStep — DR-018 anti-tunnelling; players use HitRadius, structures
  a const radius; at-most-once destroy). Concurrent-spit soft cap, soft-fail retry.
- Swarmer: marker tag + deterministic cluster spawn (1 slot = 1 pack;
  EnemyAIMath.ClusterOffset), MaxAlive counts ENTITIES so a pack defers if it
  won't fit.
- 4-type weighted mix: MixBands -> ZoneEnemyMath.WaveSlots/KindForSlot/
  PackSizeForSlot drives both the expedition director and (fork-4a) the base siege,
  with a mandatory MaxAlive cap. Legacy WaveSize/IsChargerSlot kept + parity-tested.
- Discriminator stays component-presence (no enum in Bursted systems): query-
  partition guards keep each enemy moved by exactly one EnemyAISystem pass
  (sole-Position-writer). EnemyTelegraph.IsCharger -> Kind byte for the client cue.

New authoring (Spitter/Swarmer/EnemyProjectile) + expanded director authorings with
tunable mix/cluster defaults. 13 new EditMode tests (mix composition + legacy parity,
band/cluster math, projectile move + cross-region + swept anti-tunnelling regressions);
full suite green before commit.

Dormant until the prefab/subscene wiring lands (next): the new systems guard on
TryGetSingleton/RequireForUpdate, so with no prefabs wired the new types stay inert.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-24 20:06:56 -07:00

72 lines
4.0 KiB
C#

using ProjectM.Simulation;
using Unity.Entities;
using UnityEngine;
namespace ProjectM.Authoring
{
/// <summary>
/// Authoring for the Husk enemy prefab. Bakes the gameplay components onto a prefab whose ghost setup
/// (GhostAuthoringComponent: interpolated, ownerless) is inherited by DUPLICATING an existing interpolated
/// ghost (UpgradePickup.prefab) — so the Husk replicates to all clients with no hand-written <c>[GhostField]</c>
/// (the stock LocalTransform variant carries its server-driven position). Damageable exactly like the training
/// dummy (Health/HitRadius/DamageEvent) plus the Husk AI tunables. <c>GetEntity(Dynamic)</c> gives a
/// runtime-mutable LocalTransform for server-authoritative movement and hit tests.
/// </summary>
public class EnemyAuthoring : MonoBehaviour
{
[Min(0f), Tooltip("Starting and maximum health for the Husk.")]
public float MaxHealth = 30f;
[Min(0f), Tooltip("World-unit radius used by the projectile hit test.")]
public float HitRadius = 0.7f;
[Min(0f), Tooltip("Planar seek speed toward the nearest player, units/second.")]
public float MoveSpeed = 3.5f;
[Min(0f), Tooltip("Centre-to-centre distance at which the Husk can strike a player.")]
public float AttackRange = 1.6f;
[Min(0f), Tooltip("Damage dealt per strike.")]
public float AttackDamage = 8f;
[Min(1), Tooltip("Simulation ticks between strikes (~60 ticks/sec).")]
public int AttackCooldownTicks = 36;
private class EnemyBaker : Baker<EnemyAuthoring>
{
public override void Bake(EnemyAuthoring authoring)
{
var entity = GetEntity(authoring, TransformUsageFlags.Dynamic);
AddComponent<EnemyTag>(entity);
AddComponent(entity, new Health { Current = authoring.MaxHealth, Max = authoring.MaxHealth });
AddComponent(entity, new HitRadius { Value = authoring.HitRadius });
AddBuffer<DamageEvent>(entity);
AddComponent(entity, new EnemyStats
{
MoveSpeed = authoring.MoveSpeed,
AttackRange = authoring.AttackRange,
AttackDamage = authoring.AttackDamage,
AttackCooldownTicks = authoring.AttackCooldownTicks,
});
AddComponent(entity, new EnemyAttackCooldown { NextAttackTick = 0 });
AddComponent<KnockbackState>(entity); // server-only recoil state (zero = not knocked)
AddComponent<AttackWindup>(entity); // replicated telegraph signal (zero = not winding up)
// Slice 1 (Feature C): client-safe baked telegraph metadata. EnemyBaker is the SOLE writer of
// EnemyTelegraph even on a Charger (the prefab composes both authorings on one entity); reading the
// sibling ChargerAuthoring here avoids a double-AddComponent. WindupTicks = the client danger-ramp
// denominator per variant; IsCharger lets the client pick the Charger look (LungeState is server-only).
// Kind byte (client telegraph look) — derived from the sibling variant authoring (EnemyBaker is the
// SOLE EnemyTelegraph writer). Grunt=0 / Charger=1 / Spitter=2 / Swarmer=3 (ZoneEnemyMath.Kind*).
byte kind = ZoneEnemyMath.KindGrunt;
byte windup = (byte)Tuning.AttackWindupTicks;
var spitter = GetComponent<SpitterAuthoring>();
if (GetComponent<ChargerAuthoring>() != null) { kind = ZoneEnemyMath.KindCharger; windup = 30; }
else if (spitter != null) { kind = ZoneEnemyMath.KindSpitter; windup = (byte)Mathf.Clamp(spitter.WindupTicks, 1, 255); }
else if (GetComponent<SwarmerAuthoring>() != null) { kind = ZoneEnemyMath.KindSwarmer; windup = 6; }
AddComponent(entity, new EnemyTelegraph { WindupTicks = windup, Kind = kind });
}
}
}
}