UnityTowerDefense/Assets/_Project/Scripts/Gameplay/EnemyAbilities/SplitOnDeathAbilityDefinition.cs
2026-07-28 22:34:25 -07:00

52 lines
2.3 KiB
C#

// Assets/_Project/Scripts/Gameplay/EnemyAbilities/SplitOnDeathAbilityDefinition.cs
using UnityEngine;
using TD.Core;
namespace TD.Gameplay.EnemyAbilities
{
/// <summary>
/// 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
{
public override EnemyAbilityKind Kind => EnemyAbilityKind.SplitOnDeath;
[Header("Split")]
[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)]
public float ScatterRadius = 0.5f;
public override void ServerOnDeath(EnemyAbility instance, EnemyHealth health)
{
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;
WaveManager.Instance?.ServerSpawnSplitEnemies(
def, SplitCount, atTile, ownerSlot, ScatterRadius, HpMultiplier, ScaleMultiplier);
}
}
}