first pass at enemy upgrades

This commit is contained in:
Ian Woods 2026-07-28 20:59:43 -07:00
parent a26d0acbfb
commit 8714885efa
13 changed files with 351 additions and 0 deletions

View file

@ -0,0 +1,55 @@
// Assets/_Project/Scripts/Gameplay/EnemyAbilities/EnemyAbilityDefinition.cs
using UnityEngine;
using TD.Core;
namespace TD.Gameplay.EnemyAbilities
{
/// <summary>
/// Base class for one enemy ability (e.g. "split into smaller enemies on death"). Rolled
/// at random for each spawned enemy by <see cref="EnemyAbilityPool.RollRandom"/> and applied
/// via <see cref="EnemyAbility.InitializeServer"/>.
/// </summary>
/// <remarks>
/// <para><b>One asset per kind.</b> <see cref="Kind"/> is fixed per subclass, same as
/// <see cref="BuilderSpells.BuilderSpellDefinition"/>. <see cref="EnemyAbilityPool"/> uses
/// it to build a fixed-size, enum-indexed lookup table.</para>
///
/// <para><b>Server-only hooks.</b> All three hooks below only ever run on the server —
/// <see cref="EnemyAbility"/> only calls them when <c>IsServer</c> is true. <see
/// cref="ServerOnSpawn"/> and <see cref="ServerTick"/> are no-ops for abilities that don't
/// need spawn-time setup or a per-frame timer (e.g. Split on Death uses neither); they exist
/// so a future cooldown-driven ability (disable towers, teleport) doesn't need a base-class
/// change.</para>
/// </remarks>
public abstract class EnemyAbilityDefinition : ScriptableObject
{
/// <summary>Which enemy ability this asset's data belongs to.</summary>
public abstract EnemyAbilityKind Kind { get; }
[Header("Presentation")]
[Tooltip("Name shown in debug logs and future enemy-info UI.")]
public string DisplayName;
[Tooltip("Short description shown in future enemy-info UI.")]
[TextArea(2, 4)]
public string Description;
[Header("Selection")]
[Tooltip("Relative weight of this ability being rolled, vs. the pool's other abilities " +
"and its no-ability chance. Same semantics as DraftOption.Weight.")]
[Min(0f)]
public float Weight = 1f;
/// <summary>Server-only: called once, right after this ability is assigned (before the
/// enemy's NetworkObject is spawned to clients). Default no-op.</summary>
public virtual void ServerOnSpawn(EnemyAbility instance) { }
/// <summary>Server-only: called every frame this ability is active on a live enemy.
/// Default no-op.</summary>
public virtual void ServerTick(EnemyAbility instance, float dt) { }
/// <summary>Server-only: called the instant the enemy's HP reaches zero, before the
/// death animation/despawn sequence plays. Default no-op.</summary>
public virtual void ServerOnDeath(EnemyAbility instance, EnemyHealth health) { }
}
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 855f7f0848d3086868816d25295fcdd6

View file

@ -0,0 +1,102 @@
// Assets/_Project/Scripts/Gameplay/EnemyAbilities/EnemyAbilityPool.cs
using System;
using UnityEngine;
using TD.Core;
namespace TD.Gameplay.EnemyAbilities
{
/// <summary>
/// Scene singleton holding every <see cref="EnemyAbilityDefinition"/> available this match,
/// authored as a flat array in the inspector for convenience. Internally it builds a
/// fixed-size table indexed by <see cref="EnemyAbilityKind"/> so lookups are a single array
/// access, not a scan. Mirrors <see cref="BuilderSpells.BuilderSpellPool"/> exactly.
/// </summary>
/// <remarks>
/// Plain MonoBehaviour: identical on every peer (same assets), so there is nothing to sync.
/// Only the server calls <see cref="RollRandom"/> (at enemy spawn time, in
/// <c>WaveManager.SpawnEnemy</c>).
/// </remarks>
public class EnemyAbilityPool : MonoBehaviour
{
public static EnemyAbilityPool Instance { get; private set; }
[Tooltip("Every EnemyAbilityDefinition asset available this match. One entry per " +
"EnemyAbilityKind — order doesn't matter, Kind on the asset itself decides " +
"its slot.")]
[SerializeField] private EnemyAbilityDefinition[] abilities;
[Tooltip("Relative weight of an enemy rolling no ability at all, vs. the combined " +
"weight of every entry in 'abilities'. Higher = abilities are rarer.")]
[Min(0f)]
[SerializeField] private float noAbilityWeight = 20f;
// Fixed-size, enum-indexed lookup built once in Awake. Sized to the enum's entry count,
// not authored count, so an out-of-range Kind is a compile-time impossibility rather
// than a bounds check we'd otherwise need on every Get().
private EnemyAbilityDefinition[] byKind;
private void Awake()
{
if (Instance != null && Instance != this)
{
Debug.LogError("[EnemyAbilityPool] Multiple instances detected. Only one per scene.");
return;
}
Instance = this;
int kindCount = Enum.GetValues(typeof(EnemyAbilityKind)).Length;
byKind = new EnemyAbilityDefinition[kindCount];
if (abilities == null) return;
for (int i = 0; i < abilities.Length; i++)
{
var def = abilities[i];
if (def == null) continue;
byKind[(int)def.Kind] = def;
}
}
private void OnDestroy()
{
if (Instance == this) Instance = null;
}
/// <summary>Returns the ability asset for <paramref name="kind"/>, or null if none is
/// authored in this pool.</summary>
public EnemyAbilityDefinition Get(EnemyAbilityKind kind)
{
int i = (int)kind;
return (byKind != null && i >= 0 && i < byKind.Length) ? byKind[i] : null;
}
/// <summary>
/// Server-only: weighted random draw across every authored ability plus the
/// no-ability bucket. Returns null when "no ability" is drawn, or when the pool has
/// nothing authored.
/// </summary>
public EnemyAbilityDefinition RollRandom()
{
if (abilities == null || abilities.Length == 0) return null;
float total = noAbilityWeight;
for (int i = 0; i < abilities.Length; i++)
if (abilities[i] != null) total += abilities[i].Weight;
if (total <= 0f) return null;
float roll = UnityEngine.Random.Range(0f, total);
if (roll < noAbilityWeight) return null;
roll -= noAbilityWeight;
for (int i = 0; i < abilities.Length; i++)
{
var def = abilities[i];
if (def == null) continue;
if (roll < def.Weight) return def;
roll -= def.Weight;
}
return null;
}
}
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 4409eb25a8be555de8dc3a73d4ed95db

View file

@ -0,0 +1,40 @@
// Assets/_Project/Scripts/Gameplay/EnemyAbilities/SplitOnDeathAbilityDefinition.cs
using UnityEngine;
using TD.Core;
namespace TD.Gameplay.EnemyAbilities
{
/// <summary>
/// On death, spawns <see cref="SplitCount"/> copies of <see cref="SplitInto"/> at the corpse's
/// position, continuing on toward the goal instead of restarting from the wave's spawner.
/// </summary>
[CreateAssetMenu(fileName = "SplitOnDeathAbility", menuName = "TD/Enemy Abilities/Split On Death")]
public class SplitOnDeathAbilityDefinition : EnemyAbilityDefinition
{
public override EnemyAbilityKind Kind => EnemyAbilityKind.SplitOnDeath;
[Header("Split")]
[Tooltip("Smaller enemy type spawned when this enemy dies.")]
public EnemyDefinition SplitInto;
[Tooltip("How many copies of SplitInto spawn on death.")]
[Min(1)]
public int SplitCount = 2;
[Tooltip("Random scatter radius (world units) applied to each split spawn so they don't " +
"spawn exactly on top of each other.")]
[Min(0f)]
public float ScatterRadius = 0.5f;
public override void ServerOnDeath(EnemyAbility instance, EnemyHealth health)
{
if (SplitInto == null) return;
var movement = instance.GetComponent<EnemyMovement>();
var atTile = GridCoordinates.WorldToGrid(instance.transform.position);
var ownerSlot = movement != null ? movement.OriginZone : PlayerSlot.None;
WaveManager.Instance?.ServerSpawnSplitEnemies(SplitInto, SplitCount, atTile, ownerSlot, ScatterRadius);
}
}
}

View file

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 21543cb1b54f0b16e9c80584f68988b3