From 8714885efa1c1a8af7f5dc2bbe2ba3de8e58682c Mon Sep 17 00:00:00 2001 From: Ian Woods Date: Tue, 28 Jul 2026 20:59:43 -0700 Subject: [PATCH 1/3] first pass at enemy upgrades --- .../_Project/Scripts/Core/EnemyAbilityKind.cs | 16 +++ .../Scripts/Core/EnemyAbilityKind.cs.meta | 2 + .../Scripts/Gameplay/EnemyAbilities.meta | 8 ++ .../EnemyAbilities/EnemyAbilityDefinition.cs | 55 ++++++++++ .../EnemyAbilityDefinition.cs.meta | 2 + .../EnemyAbilities/EnemyAbilityPool.cs | 102 ++++++++++++++++++ .../EnemyAbilities/EnemyAbilityPool.cs.meta | 2 + .../SplitOnDeathAbilityDefinition.cs | 40 +++++++ .../SplitOnDeathAbilityDefinition.cs.meta | 2 + .../_Project/Scripts/Gameplay/EnemyAbility.cs | 84 +++++++++++++++ .../Scripts/Gameplay/EnemyAbility.cs.meta | 2 + .../Scripts/Gameplay/EnemyMovement.cs | 5 + .../_Project/Scripts/Gameplay/WaveManager.cs | 31 ++++++ 13 files changed, 351 insertions(+) create mode 100644 Assets/_Project/Scripts/Core/EnemyAbilityKind.cs create mode 100644 Assets/_Project/Scripts/Core/EnemyAbilityKind.cs.meta create mode 100644 Assets/_Project/Scripts/Gameplay/EnemyAbilities.meta create mode 100644 Assets/_Project/Scripts/Gameplay/EnemyAbilities/EnemyAbilityDefinition.cs create mode 100644 Assets/_Project/Scripts/Gameplay/EnemyAbilities/EnemyAbilityDefinition.cs.meta create mode 100644 Assets/_Project/Scripts/Gameplay/EnemyAbilities/EnemyAbilityPool.cs create mode 100644 Assets/_Project/Scripts/Gameplay/EnemyAbilities/EnemyAbilityPool.cs.meta create mode 100644 Assets/_Project/Scripts/Gameplay/EnemyAbilities/SplitOnDeathAbilityDefinition.cs create mode 100644 Assets/_Project/Scripts/Gameplay/EnemyAbilities/SplitOnDeathAbilityDefinition.cs.meta create mode 100644 Assets/_Project/Scripts/Gameplay/EnemyAbility.cs create mode 100644 Assets/_Project/Scripts/Gameplay/EnemyAbility.cs.meta diff --git a/Assets/_Project/Scripts/Core/EnemyAbilityKind.cs b/Assets/_Project/Scripts/Core/EnemyAbilityKind.cs new file mode 100644 index 0000000..3bc7bd1 --- /dev/null +++ b/Assets/_Project/Scripts/Core/EnemyAbilityKind.cs @@ -0,0 +1,16 @@ +namespace TD.Core +{ + /// + /// Identifies which enemy ability a + /// represents. Mirrors + /// — a stable, enum-indexed identifier used by + /// instead of an asset reference. + /// + public enum EnemyAbilityKind : byte + { + /// No ability rolled. Never a real pool entry — only the sentinel value + /// reports before/absent a roll. + None = 0, + SplitOnDeath = 1, + } +} diff --git a/Assets/_Project/Scripts/Core/EnemyAbilityKind.cs.meta b/Assets/_Project/Scripts/Core/EnemyAbilityKind.cs.meta new file mode 100644 index 0000000..18dd995 --- /dev/null +++ b/Assets/_Project/Scripts/Core/EnemyAbilityKind.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: ec52a6b404fed65dfa48a63b5535373d \ No newline at end of file diff --git a/Assets/_Project/Scripts/Gameplay/EnemyAbilities.meta b/Assets/_Project/Scripts/Gameplay/EnemyAbilities.meta new file mode 100644 index 0000000..099a1ab --- /dev/null +++ b/Assets/_Project/Scripts/Gameplay/EnemyAbilities.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 1c918164b61faf1d5af9f782c5237a60 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/_Project/Scripts/Gameplay/EnemyAbilities/EnemyAbilityDefinition.cs b/Assets/_Project/Scripts/Gameplay/EnemyAbilities/EnemyAbilityDefinition.cs new file mode 100644 index 0000000..349b0ee --- /dev/null +++ b/Assets/_Project/Scripts/Gameplay/EnemyAbilities/EnemyAbilityDefinition.cs @@ -0,0 +1,55 @@ +// Assets/_Project/Scripts/Gameplay/EnemyAbilities/EnemyAbilityDefinition.cs +using UnityEngine; +using TD.Core; + +namespace TD.Gameplay.EnemyAbilities +{ + /// + /// Base class for one enemy ability (e.g. "split into smaller enemies on death"). Rolled + /// at random for each spawned enemy by and applied + /// via . + /// + /// + /// One asset per kind. is fixed per subclass, same as + /// . uses + /// it to build a fixed-size, enum-indexed lookup table. + /// + /// Server-only hooks. All three hooks below only ever run on the server — + /// only calls them when IsServer is true. and 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. + /// + public abstract class EnemyAbilityDefinition : ScriptableObject + { + /// Which enemy ability this asset's data belongs to. + 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; + + /// Server-only: called once, right after this ability is assigned (before the + /// enemy's NetworkObject is spawned to clients). Default no-op. + public virtual void ServerOnSpawn(EnemyAbility instance) { } + + /// Server-only: called every frame this ability is active on a live enemy. + /// Default no-op. + public virtual void ServerTick(EnemyAbility instance, float dt) { } + + /// Server-only: called the instant the enemy's HP reaches zero, before the + /// death animation/despawn sequence plays. Default no-op. + public virtual void ServerOnDeath(EnemyAbility instance, EnemyHealth health) { } + } +} diff --git a/Assets/_Project/Scripts/Gameplay/EnemyAbilities/EnemyAbilityDefinition.cs.meta b/Assets/_Project/Scripts/Gameplay/EnemyAbilities/EnemyAbilityDefinition.cs.meta new file mode 100644 index 0000000..b94f8dd --- /dev/null +++ b/Assets/_Project/Scripts/Gameplay/EnemyAbilities/EnemyAbilityDefinition.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 855f7f0848d3086868816d25295fcdd6 \ No newline at end of file diff --git a/Assets/_Project/Scripts/Gameplay/EnemyAbilities/EnemyAbilityPool.cs b/Assets/_Project/Scripts/Gameplay/EnemyAbilities/EnemyAbilityPool.cs new file mode 100644 index 0000000..cc9270e --- /dev/null +++ b/Assets/_Project/Scripts/Gameplay/EnemyAbilities/EnemyAbilityPool.cs @@ -0,0 +1,102 @@ +// Assets/_Project/Scripts/Gameplay/EnemyAbilities/EnemyAbilityPool.cs +using System; +using UnityEngine; +using TD.Core; + +namespace TD.Gameplay.EnemyAbilities +{ + /// + /// Scene singleton holding every available this match, + /// authored as a flat array in the inspector for convenience. Internally it builds a + /// fixed-size table indexed by so lookups are a single array + /// access, not a scan. Mirrors exactly. + /// + /// + /// Plain MonoBehaviour: identical on every peer (same assets), so there is nothing to sync. + /// Only the server calls (at enemy spawn time, in + /// WaveManager.SpawnEnemy). + /// + 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; + } + + /// Returns the ability asset for , or null if none is + /// authored in this pool. + public EnemyAbilityDefinition Get(EnemyAbilityKind kind) + { + int i = (int)kind; + return (byKind != null && i >= 0 && i < byKind.Length) ? byKind[i] : null; + } + + /// + /// 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. + /// + 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; + } + } +} diff --git a/Assets/_Project/Scripts/Gameplay/EnemyAbilities/EnemyAbilityPool.cs.meta b/Assets/_Project/Scripts/Gameplay/EnemyAbilities/EnemyAbilityPool.cs.meta new file mode 100644 index 0000000..a951ada --- /dev/null +++ b/Assets/_Project/Scripts/Gameplay/EnemyAbilities/EnemyAbilityPool.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 4409eb25a8be555de8dc3a73d4ed95db \ No newline at end of file diff --git a/Assets/_Project/Scripts/Gameplay/EnemyAbilities/SplitOnDeathAbilityDefinition.cs b/Assets/_Project/Scripts/Gameplay/EnemyAbilities/SplitOnDeathAbilityDefinition.cs new file mode 100644 index 0000000..364e16b --- /dev/null +++ b/Assets/_Project/Scripts/Gameplay/EnemyAbilities/SplitOnDeathAbilityDefinition.cs @@ -0,0 +1,40 @@ +// Assets/_Project/Scripts/Gameplay/EnemyAbilities/SplitOnDeathAbilityDefinition.cs +using UnityEngine; +using TD.Core; + +namespace TD.Gameplay.EnemyAbilities +{ + /// + /// On death, spawns copies of at the corpse's + /// position, continuing on toward the goal instead of restarting from the wave's spawner. + /// + [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(); + var atTile = GridCoordinates.WorldToGrid(instance.transform.position); + var ownerSlot = movement != null ? movement.OriginZone : PlayerSlot.None; + + WaveManager.Instance?.ServerSpawnSplitEnemies(SplitInto, SplitCount, atTile, ownerSlot, ScatterRadius); + } + } +} diff --git a/Assets/_Project/Scripts/Gameplay/EnemyAbilities/SplitOnDeathAbilityDefinition.cs.meta b/Assets/_Project/Scripts/Gameplay/EnemyAbilities/SplitOnDeathAbilityDefinition.cs.meta new file mode 100644 index 0000000..8c2a4f3 --- /dev/null +++ b/Assets/_Project/Scripts/Gameplay/EnemyAbilities/SplitOnDeathAbilityDefinition.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 21543cb1b54f0b16e9c80584f68988b3 \ No newline at end of file diff --git a/Assets/_Project/Scripts/Gameplay/EnemyAbility.cs b/Assets/_Project/Scripts/Gameplay/EnemyAbility.cs new file mode 100644 index 0000000..34c7d79 --- /dev/null +++ b/Assets/_Project/Scripts/Gameplay/EnemyAbility.cs @@ -0,0 +1,84 @@ +// Assets/_Project/Scripts/Gameplay/EnemyAbility.cs +using Unity.Netcode; +using UnityEngine; +using TD.Core; +using TD.Gameplay.EnemyAbilities; + +namespace TD.Gameplay +{ + /// + /// Per-enemy ability slot. Holds the rolled for this + /// instance (if any) and drives its server-only hooks. + /// + /// + /// Initialization: Call on the server immediately after + /// Instantiate and before NetworkObject.Spawn(), following the same pattern as + /// EnemyHealth.InitializeServer / EnemyMovement.InitializeServer. + /// + /// Optional component: Not required by EnemyHealth or EnemyMovement — + /// WaveManager.SpawnEnemy only rolls and assigns an ability when this component is + /// present on the prefab, so existing enemy prefabs are unaffected until it's added to them. + /// + /// On-death hook is not event-driven: WaveManager.HandleEnemyKilled calls + /// directly (via ) + /// rather than this component subscribing to EnemyHealth.OnDied itself — that keeps a + /// split-spawn's activeEnemyCount increment ordered before the triggering kill's + /// decrement, regardless of NetworkBehaviour subscription order. + /// + [RequireComponent(typeof(NetworkObject))] + public class EnemyAbility : NetworkBehaviour + { + private readonly NetworkVariable kind = new NetworkVariable( + (byte)EnemyAbilityKind.None, + NetworkVariableReadPermission.Everyone, + NetworkVariableWritePermission.Server); + + // ----- Pre-spawn init (server-local) ---------------------------------- + + private EnemyAbilityDefinition pendingDefinition; + private bool hasPendingInit; + + // ----- Public state ----------------------------------------------------- + + /// The ability rolled for this enemy, or null if none was rolled. + public EnemyAbilityDefinition Definition { get; private set; } + + /// Replicated kind of the rolled ability. if + /// none was rolled. + public EnemyAbilityKind Kind => (EnemyAbilityKind)kind.Value; + + // ----- Server-only pre-spawn init ------------------------------------- + + /// + /// Called by WaveManager on the server after Instantiate and before + /// NetworkObject.Spawn(). may be null, meaning no + /// ability was rolled for this enemy. + /// + public void InitializeServer(EnemyAbilityDefinition definition) + { + pendingDefinition = definition; + hasPendingInit = true; + } + + // ----- NGO lifecycle -------------------------------------------------- + + public override void OnNetworkSpawn() + { + if (!IsServer || !hasPendingInit) return; + + Definition = pendingDefinition; + kind.Value = (byte)(Definition != null ? Definition.Kind : EnemyAbilityKind.None); + hasPendingInit = false; + + Definition?.ServerOnSpawn(this); + } + + // ----- Server tick ------------------------------------------------ + + private void Update() + { + if (!IsServer || Definition == null) return; + Definition.ServerTick(this, Time.deltaTime); + } + } +} diff --git a/Assets/_Project/Scripts/Gameplay/EnemyAbility.cs.meta b/Assets/_Project/Scripts/Gameplay/EnemyAbility.cs.meta new file mode 100644 index 0000000..3562455 --- /dev/null +++ b/Assets/_Project/Scripts/Gameplay/EnemyAbility.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 9d1a26a32e8969cd29bce6010126a5f6 \ No newline at end of file diff --git a/Assets/_Project/Scripts/Gameplay/EnemyMovement.cs b/Assets/_Project/Scripts/Gameplay/EnemyMovement.cs index 96a6e30..7eec0d1 100644 --- a/Assets/_Project/Scripts/Gameplay/EnemyMovement.cs +++ b/Assets/_Project/Scripts/Gameplay/EnemyMovement.cs @@ -73,6 +73,11 @@ namespace TD.Gameplay // the goal). Without this, every zone crossing was counted as a leak; only // the originating player should be credited a leak per the design. private PlayerSlot originZone = PlayerSlot.None; + + /// The zone (player) this enemy was originally spawned for — see + /// . Used by on-death abilities (e.g. Split on Death) so + /// spawned children preserve the same leak attribution as their parent. + public PlayerSlot OriginZone => originZone; // Latches once the enemy has crossed its origin zone's leak volume, so we // never double-count a leak if the enemy re-enters its origin (rare but // possible if pathing is dynamic). diff --git a/Assets/_Project/Scripts/Gameplay/WaveManager.cs b/Assets/_Project/Scripts/Gameplay/WaveManager.cs index d1d889b..67beb6c 100644 --- a/Assets/_Project/Scripts/Gameplay/WaveManager.cs +++ b/Assets/_Project/Scripts/Gameplay/WaveManager.cs @@ -5,6 +5,7 @@ using UnityEngine; using TD.Core; using TD.Gameplay.BuilderEffects; using TD.Gameplay.Draft; +using TD.Gameplay.EnemyAbilities; using TD.Levels; using TD.UI; @@ -503,6 +504,13 @@ namespace TD.Gameplay health.InitializeServer(def.MaxHp, def.LivesCost, def.IsFlying, held); movement.InitializeServer(def.MoveSpeed, spawnerTile, ownerSlot, def.IsFlying); + + // Optional — only prefabs with an EnemyAbility component roll an ability. See + // EnemyAbility's remarks for why on-death abilities aren't event-subscription driven. + var ability = go.GetComponent(); + if (ability != null) + ability.InitializeServer(EnemyAbilityPool.Instance?.RollRandom()); + if (held) heldEnemies.Add(health); health.OnDied += HandleEnemyKilled; @@ -514,6 +522,22 @@ namespace TD.Gameplay go.GetComponent().Spawn(); } + /// + /// Server-only: spawns copies of at + /// , continuing that tile's A* path onward rather than + /// restarting from a wave spawner. Used by on-death abilities (e.g. + /// SplitOnDeathAbilityDefinition) to spawn smaller enemies from a corpse's + /// position. Reuses so split children get the same + /// activeEnemyCount/event-wiring bookkeeping as any other spawn. + /// + public void ServerSpawnSplitEnemies(EnemyDefinition def, int count, Vector2Int atTile, + PlayerSlot ownerSlot, float scatterRadius) + { + if (!IsServer) return; + for (int i = 0; i < count; i++) + SpawnEnemy(def, atTile, ownerSlot, scatterRadius, scatterRadius); + } + // ----- Enemy event handlers (server-only) ------------------------- private void HandleEnemyKilled(EnemyHealth health) @@ -551,6 +575,13 @@ namespace TD.Gameplay if (totalReward > 0) ShowGoldRewardClientRpc(health.transform.position, totalReward); + // Resolve any on-death ability BEFORE unsubscribing/decrementing. A split-spawn's + // activeEnemyCount++ (inside ServerSpawnSplitEnemies -> SpawnEnemy) must land before + // this kill's activeEnemyCount-- below, or a split on a wave's last enemy could let + // CheckWaveComplete see activeEnemyCount hit 0 and advance the wave prematurely. + var ability = health.GetComponent(); + ability?.Definition?.ServerOnDeath(ability, health); + UnsubscribeEnemy(health); DecrementAndCheckComplete(); } From 11ad19992d29badc7b47a5826b75f35711a2e303 Mon Sep 17 00:00:00 2001 From: Ian Woods Date: Tue, 28 Jul 2026 21:40:59 -0700 Subject: [PATCH 2/3] enemies split, but it needs a bit of work --- .../BuilderSpells/FireballSpell.asset | 2 +- .../_Project/Definitions/EnemyAbilities.meta | 8 ++++ .../EnemyAbilities/SplitOnDeathAbility.asset | 20 ++++++++ .../SplitOnDeathAbility.asset.meta | 8 ++++ .../Enemies/Enemy_CrystalGolem_Blue.prefab | 16 ++++++- Assets/_Project/Scenes/Levels/9Player.unity | 48 +++++++++++++++++++ .../EnemyAbilities/EnemyAbilityPool.cs | 2 +- .../SplitOnDeathAbilityDefinition.cs | 5 ++ .../_Project/Scripts/Gameplay/EnemyAbility.cs | 3 ++ .../_Project/Scripts/Gameplay/WaveManager.cs | 2 + 10 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 Assets/_Project/Definitions/EnemyAbilities.meta create mode 100644 Assets/_Project/Definitions/EnemyAbilities/SplitOnDeathAbility.asset create mode 100644 Assets/_Project/Definitions/EnemyAbilities/SplitOnDeathAbility.asset.meta diff --git a/Assets/_Project/Definitions/BuilderSpells/FireballSpell.asset b/Assets/_Project/Definitions/BuilderSpells/FireballSpell.asset index b63d958..e713412 100644 --- a/Assets/_Project/Definitions/BuilderSpells/FireballSpell.asset +++ b/Assets/_Project/Definitions/BuilderSpells/FireballSpell.asset @@ -15,7 +15,7 @@ MonoBehaviour: DisplayName: Fireball Description: Shoot a fireball Icon: {fileID: 21300000, guid: eff45e5e3dab24f438c054a095d34578, type: 3} - Cooldown: 1 + Cooldown: 15 TargetType: 0 Radius: 5 enemyLayerMask: diff --git a/Assets/_Project/Definitions/EnemyAbilities.meta b/Assets/_Project/Definitions/EnemyAbilities.meta new file mode 100644 index 0000000..d927cf3 --- /dev/null +++ b/Assets/_Project/Definitions/EnemyAbilities.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 37cbc8b63ce7ae2e4b4abcfd7274bee7 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/_Project/Definitions/EnemyAbilities/SplitOnDeathAbility.asset b/Assets/_Project/Definitions/EnemyAbilities/SplitOnDeathAbility.asset new file mode 100644 index 0000000..6ca1f9e --- /dev/null +++ b/Assets/_Project/Definitions/EnemyAbilities/SplitOnDeathAbility.asset @@ -0,0 +1,20 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!114 &11400000 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 0} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 21543cb1b54f0b16e9c80584f68988b3, type: 3} + m_Name: SplitOnDeathAbility + m_EditorClassIdentifier: Assembly-CSharp::TD.Gameplay.EnemyAbilities.SplitOnDeathAbilityDefinition + DisplayName: Split on Death + Description: The enemy splits when it dies. + Weight: 100 + SplitInto: {fileID: 11400000, guid: a05af37e2256f164d80a66cc2a582466, type: 2} + SplitCount: 2 + ScatterRadius: 0.5 diff --git a/Assets/_Project/Definitions/EnemyAbilities/SplitOnDeathAbility.asset.meta b/Assets/_Project/Definitions/EnemyAbilities/SplitOnDeathAbility.asset.meta new file mode 100644 index 0000000..fae7041 --- /dev/null +++ b/Assets/_Project/Definitions/EnemyAbilities/SplitOnDeathAbility.asset.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 0305468bc8f912d429a8b20bf23f947c +NativeFormatImporter: + externalObjects: {} + mainObjectFileID: 11400000 + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/_Project/Prefabs/Enemies/Enemy_CrystalGolem_Blue.prefab b/Assets/_Project/Prefabs/Enemies/Enemy_CrystalGolem_Blue.prefab index 546fd71..cef9c0c 100644 --- a/Assets/_Project/Prefabs/Enemies/Enemy_CrystalGolem_Blue.prefab +++ b/Assets/_Project/Prefabs/Enemies/Enemy_CrystalGolem_Blue.prefab @@ -17,6 +17,7 @@ GameObject: - component: {fileID: 3756613737869398948} - component: {fileID: 6036713598566100742} - component: {fileID: -1575784752119152190} + - component: {fileID: 154042129325690845} m_Layer: 10 m_Name: Enemy_CrystalGolem_Blue m_TagString: Untagged @@ -53,7 +54,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: d5a57f767e5e46a458fc5d3c628d0cbb, type: 3} m_Name: m_EditorClassIdentifier: Unity.Netcode.Runtime::Unity.Netcode.NetworkObject - GlobalObjectIdHash: 2814637749 + GlobalObjectIdHash: 1115713368 InScenePlacedSourceGlobalObjectIdHash: 0 DeferredDespawnTick: 0 Ownership: 1 @@ -224,6 +225,19 @@ MonoBehaviour: volume: 0.75 minPitch: 0.95 maxPitch: 1.05 +--- !u!114 &154042129325690845 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1455822126534880203} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 9d1a26a32e8969cd29bce6010126a5f6, type: 3} + m_Name: + m_EditorClassIdentifier: Assembly-CSharp::TD.Gameplay.EnemyAbility + ShowTopMostFoldoutHeaderGroup: 1 --- !u!1 &5361867751622119598 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/_Project/Scenes/Levels/9Player.unity b/Assets/_Project/Scenes/Levels/9Player.unity index 9a4d028..dd7bef0 100644 --- a/Assets/_Project/Scenes/Levels/9Player.unity +++ b/Assets/_Project/Scenes/Levels/9Player.unity @@ -2276,6 +2276,53 @@ Transform: m_Children: [] m_Father: {fileID: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!1 &181002639 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 181002641} + - component: {fileID: 181002640} + m_Layer: 0 + m_Name: EnemyAbilityPool + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!114 &181002640 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 181002639} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 4409eb25a8be555de8dc3a73d4ed95db, type: 3} + m_Name: + m_EditorClassIdentifier: Assembly-CSharp::TD.Gameplay.EnemyAbilities.EnemyAbilityPool + abilities: + - {fileID: 11400000, guid: 0305468bc8f912d429a8b20bf23f947c, type: 2} + noAbilityWeight: 100 +--- !u!4 &181002641 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 181002639} + serializedVersion: 2 + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 15.94346, y: 0, z: 194.22044} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!43 &203092944 Mesh: m_ObjectHideFlags: 0 @@ -29189,3 +29236,4 @@ SceneRoots: - {fileID: 993870445} - {fileID: 759470735} - {fileID: 954434161} + - {fileID: 181002641} diff --git a/Assets/_Project/Scripts/Gameplay/EnemyAbilities/EnemyAbilityPool.cs b/Assets/_Project/Scripts/Gameplay/EnemyAbilities/EnemyAbilityPool.cs index cc9270e..30fe26c 100644 --- a/Assets/_Project/Scripts/Gameplay/EnemyAbilities/EnemyAbilityPool.cs +++ b/Assets/_Project/Scripts/Gameplay/EnemyAbilities/EnemyAbilityPool.cs @@ -28,7 +28,7 @@ namespace TD.Gameplay.EnemyAbilities [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; + [SerializeField] private float noAbilityWeight = 100f; // 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 diff --git a/Assets/_Project/Scripts/Gameplay/EnemyAbilities/SplitOnDeathAbilityDefinition.cs b/Assets/_Project/Scripts/Gameplay/EnemyAbilities/SplitOnDeathAbilityDefinition.cs index 364e16b..687e676 100644 --- a/Assets/_Project/Scripts/Gameplay/EnemyAbilities/SplitOnDeathAbilityDefinition.cs +++ b/Assets/_Project/Scripts/Gameplay/EnemyAbilities/SplitOnDeathAbilityDefinition.cs @@ -28,12 +28,17 @@ namespace TD.Gameplay.EnemyAbilities public override void ServerOnDeath(EnemyAbility instance, EnemyHealth health) { + Debug.Log($"[SplitOnDeathAbility] ServerOnDeath fired for {instance.name}. " + + $"SplitInto={(SplitInto != null ? SplitInto.name : "null")}, " + + $"WaveManager.Instance={(WaveManager.Instance != null)}"); + if (SplitInto == null) return; var movement = instance.GetComponent(); var atTile = GridCoordinates.WorldToGrid(instance.transform.position); var ownerSlot = movement != null ? movement.OriginZone : PlayerSlot.None; + Debug.Log($"[SplitOnDeathAbility] Spawning {SplitCount}x {SplitInto.name} at tile {atTile}."); WaveManager.Instance?.ServerSpawnSplitEnemies(SplitInto, SplitCount, atTile, ownerSlot, ScatterRadius); } } diff --git a/Assets/_Project/Scripts/Gameplay/EnemyAbility.cs b/Assets/_Project/Scripts/Gameplay/EnemyAbility.cs index 34c7d79..93a577c 100644 --- a/Assets/_Project/Scripts/Gameplay/EnemyAbility.cs +++ b/Assets/_Project/Scripts/Gameplay/EnemyAbility.cs @@ -70,6 +70,9 @@ namespace TD.Gameplay kind.Value = (byte)(Definition != null ? Definition.Kind : EnemyAbilityKind.None); hasPendingInit = false; + if (Definition != null) + Debug.Log($"[EnemyAbility] {name} rolled {Definition.Kind}."); + Definition?.ServerOnSpawn(this); } diff --git a/Assets/_Project/Scripts/Gameplay/WaveManager.cs b/Assets/_Project/Scripts/Gameplay/WaveManager.cs index 67beb6c..c4af7e3 100644 --- a/Assets/_Project/Scripts/Gameplay/WaveManager.cs +++ b/Assets/_Project/Scripts/Gameplay/WaveManager.cs @@ -580,6 +580,8 @@ namespace TD.Gameplay // this kill's activeEnemyCount-- below, or a split on a wave's last enemy could let // CheckWaveComplete see activeEnemyCount hit 0 and advance the wave prematurely. var ability = health.GetComponent(); + Debug.Log($"[WaveManager] HandleEnemyKilled: ability={(ability != null)}, " + + $"definition={(ability != null ? ability.Definition?.name ?? "null" : "n/a")}"); ability?.Definition?.ServerOnDeath(ability, health); UnsubscribeEnemy(health); From 0a70f36838186e71617d62f8f24938ea9147dfca Mon Sep 17 00:00:00 2001 From: Ian Woods Date: Tue, 28 Jul 2026 22:34:25 -0700 Subject: [PATCH 3/3] simpler enemy split --- .../Enemies/Enemy_CrystalGolem_Blue.prefab | 4 +-- .../SplitOnDeathAbilityDefinition.cs | 33 +++++++++++-------- .../_Project/Scripts/Gameplay/WaveManager.cs | 28 ++++++++++++---- 3 files changed, 43 insertions(+), 22 deletions(-) diff --git a/Assets/_Project/Prefabs/Enemies/Enemy_CrystalGolem_Blue.prefab b/Assets/_Project/Prefabs/Enemies/Enemy_CrystalGolem_Blue.prefab index cef9c0c..b4613dd 100644 --- a/Assets/_Project/Prefabs/Enemies/Enemy_CrystalGolem_Blue.prefab +++ b/Assets/_Project/Prefabs/Enemies/Enemy_CrystalGolem_Blue.prefab @@ -54,7 +54,7 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: d5a57f767e5e46a458fc5d3c628d0cbb, type: 3} m_Name: m_EditorClassIdentifier: Unity.Netcode.Runtime::Unity.Netcode.NetworkObject - GlobalObjectIdHash: 1115713368 + GlobalObjectIdHash: 2814637749 InScenePlacedSourceGlobalObjectIdHash: 0 DeferredDespawnTick: 0 Ownership: 1 @@ -126,7 +126,7 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: Assembly-CSharp::TD.Gameplay.EnemyHealth ShowTopMostFoldoutHeaderGroup: 1 - definition: {fileID: 0} + definition: {fileID: 11400000, guid: a05af37e2256f164d80a66cc2a582466, type: 2} deathAnimator: {fileID: 1819643351848917197} deathAnimationDuration: 1.5 sinkDuration: 1.5 diff --git a/Assets/_Project/Scripts/Gameplay/EnemyAbilities/SplitOnDeathAbilityDefinition.cs b/Assets/_Project/Scripts/Gameplay/EnemyAbilities/SplitOnDeathAbilityDefinition.cs index 687e676..48bf7c6 100644 --- a/Assets/_Project/Scripts/Gameplay/EnemyAbilities/SplitOnDeathAbilityDefinition.cs +++ b/Assets/_Project/Scripts/Gameplay/EnemyAbilities/SplitOnDeathAbilityDefinition.cs @@ -5,8 +5,11 @@ using TD.Core; namespace TD.Gameplay.EnemyAbilities { /// - /// On death, spawns copies of at the corpse's - /// position, continuing on toward the goal instead of restarting from the wave's spawner. + /// On death, spawns smaller copies of the dying enemy's own type at + /// the corpse's position, continuing on toward the goal instead of restarting from the wave's + /// spawner. There's no separate "minion" EnemyDefinition to author — the mini copies reuse + /// whichever EnemyDefinition/prefab the dying enemy itself was spawned from, scaled down by + /// /. /// [CreateAssetMenu(fileName = "SplitOnDeathAbility", menuName = "TD/Enemy Abilities/Split On Death")] public class SplitOnDeathAbilityDefinition : EnemyAbilityDefinition @@ -14,13 +17,20 @@ namespace TD.Gameplay.EnemyAbilities 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.")] + [Tooltip("How many mini copies spawn on death.")] [Min(1)] public int SplitCount = 2; + [Tooltip("Each mini copy's MaxHp = the dying enemy's own MaxHp * this multiplier.")] + [Range(0.01f, 1f)] + public float HpMultiplier = 0.5f; + + [Tooltip("Uniform transform scale applied to each mini copy, relative to the normal " + + "prefab scale. Requires the enemy prefab's NetworkTransform to sync scale, " + + "or the mini will render full-size on remote peers.")] + [Range(0.1f, 1f)] + public float ScaleMultiplier = 0.6f; + [Tooltip("Random scatter radius (world units) applied to each split spawn so they don't " + "spawn exactly on top of each other.")] [Min(0f)] @@ -28,18 +38,15 @@ namespace TD.Gameplay.EnemyAbilities public override void ServerOnDeath(EnemyAbility instance, EnemyHealth health) { - Debug.Log($"[SplitOnDeathAbility] ServerOnDeath fired for {instance.name}. " + - $"SplitInto={(SplitInto != null ? SplitInto.name : "null")}, " + - $"WaveManager.Instance={(WaveManager.Instance != null)}"); - - if (SplitInto == null) return; + var def = health.Definition; + if (def == null) return; var movement = instance.GetComponent(); var atTile = GridCoordinates.WorldToGrid(instance.transform.position); var ownerSlot = movement != null ? movement.OriginZone : PlayerSlot.None; - Debug.Log($"[SplitOnDeathAbility] Spawning {SplitCount}x {SplitInto.name} at tile {atTile}."); - WaveManager.Instance?.ServerSpawnSplitEnemies(SplitInto, SplitCount, atTile, ownerSlot, ScatterRadius); + WaveManager.Instance?.ServerSpawnSplitEnemies( + def, SplitCount, atTile, ownerSlot, ScatterRadius, HpMultiplier, ScaleMultiplier); } } } diff --git a/Assets/_Project/Scripts/Gameplay/WaveManager.cs b/Assets/_Project/Scripts/Gameplay/WaveManager.cs index c4af7e3..ce61ce2 100644 --- a/Assets/_Project/Scripts/Gameplay/WaveManager.cs +++ b/Assets/_Project/Scripts/Gameplay/WaveManager.cs @@ -457,7 +457,8 @@ namespace TD.Gameplay private void SpawnEnemy(EnemyDefinition def, Vector2Int spawnerTile, PlayerSlot ownerSlot, float xHalfExtent = 0f, float zHalfExtent = 0f, bool held = false, - Direction facing = Direction.South) + Direction facing = Direction.South, bool canRollAbility = true, + float hpMultiplier = 1f, float visualScale = 1f) { if (def.EnemyPrefab == null) { @@ -491,6 +492,12 @@ namespace TD.Gameplay spawnPos, Quaternion.Euler(0f, yaw, 0f)); + // Scales the whole prefab hierarchy uniformly (used for split-on-death minions). + // Relies on the enemy prefab's NetworkTransform syncing scale to clients — verify + // that's enabled if a scaled spawn doesn't look smaller on remote peers. + if (visualScale != 1f) + go.transform.localScale *= visualScale; + var health = go.GetComponent(); var movement = go.GetComponent(); @@ -502,14 +509,16 @@ namespace TD.Gameplay return; } - health.InitializeServer(def.MaxHp, def.LivesCost, def.IsFlying, held); + health.InitializeServer(def.MaxHp * hpMultiplier, def.LivesCost, def.IsFlying, held); movement.InitializeServer(def.MoveSpeed, spawnerTile, ownerSlot, def.IsFlying); // Optional — only prefabs with an EnemyAbility component roll an ability. See // EnemyAbility's remarks for why on-death abilities aren't event-subscription driven. + // canRollAbility is false for split-spawned minions (see ServerSpawnSplitEnemies) so + // they can never chain into further splits or pick up any other ability. var ability = go.GetComponent(); if (ability != null) - ability.InitializeServer(EnemyAbilityPool.Instance?.RollRandom()); + ability.InitializeServer(canRollAbility ? EnemyAbilityPool.Instance?.RollRandom() : null); if (held) heldEnemies.Add(health); @@ -530,12 +539,19 @@ namespace TD.Gameplay /// position. Reuses so split children get the same /// activeEnemyCount/event-wiring bookkeeping as any other spawn. /// + /// + /// Split-spawned minions never roll an ability of their own — they're always plain, + /// so a split can't chain into further splits (or any other ability) regardless of + /// which EnemyDefinition/prefab is used for . + /// public void ServerSpawnSplitEnemies(EnemyDefinition def, int count, Vector2Int atTile, - PlayerSlot ownerSlot, float scatterRadius) + PlayerSlot ownerSlot, float scatterRadius, + float hpMultiplier = 1f, float visualScale = 1f) { if (!IsServer) return; for (int i = 0; i < count; i++) - SpawnEnemy(def, atTile, ownerSlot, scatterRadius, scatterRadius); + SpawnEnemy(def, atTile, ownerSlot, scatterRadius, scatterRadius, canRollAbility: false, + hpMultiplier: hpMultiplier, visualScale: visualScale); } // ----- Enemy event handlers (server-only) ------------------------- @@ -580,8 +596,6 @@ namespace TD.Gameplay // this kill's activeEnemyCount-- below, or a split on a wave's last enemy could let // CheckWaveComplete see activeEnemyCount hit 0 and advance the wave prematurely. var ability = health.GetComponent(); - Debug.Log($"[WaveManager] HandleEnemyKilled: ability={(ability != null)}, " + - $"definition={(ability != null ? ability.Definition?.name ?? "null" : "n/a")}"); ability?.Definition?.ServerOnDeath(ability, health); UnsubscribeEnemy(health);