simpler enemy split

This commit is contained in:
Ian Woods 2026-07-28 22:34:25 -07:00
parent 11ad19992d
commit 0a70f36838
3 changed files with 43 additions and 22 deletions

View file

@ -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

View file

@ -5,8 +5,11 @@ 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.
/// On death, spawns <see cref="SplitCount"/> 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
/// <see cref="HpMultiplier"/>/<see cref="ScaleMultiplier"/>.
/// </summary>
[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<EnemyMovement>();
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);
}
}
}

View file

@ -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<EnemyHealth>();
var movement = go.GetComponent<EnemyMovement>();
@ -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<EnemyAbility>();
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 <see cref="SpawnEnemy"/> so split children get the same
/// <c>activeEnemyCount</c>/event-wiring bookkeeping as any other spawn.
/// </summary>
/// <remarks>
/// 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 <paramref name="def"/>.
/// </remarks>
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<EnemyAbility>();
Debug.Log($"[WaveManager] HandleEnemyKilled: ability={(ability != null)}, " +
$"definition={(ability != null ? ability.Definition?.name ?? "null" : "n/a")}");
ability?.Definition?.ServerOnDeath(ability, health);
UnsubscribeEnemy(health);