60 lines
2.2 KiB
C#
60 lines
2.2 KiB
C#
using UnityEngine;
|
||
|
||
namespace BaseGames.Enemies.Behaviors
|
||
{
|
||
/// <summary>
|
||
/// 出生 / 外部时机触发执行指定能力(零代码替代每敌人专属的出生触发脚本)。
|
||
/// <list type="bullet">
|
||
/// <item>勾选 <c>executeOnSpawn</c>:对象池取出并完成 <see cref="EnemyBase.OnSpawn"/> 重置后自动执行能力
|
||
/// (例如精英怪死亡时生成的小怪落地即下坠)。</item>
|
||
/// <item>公共方法 <see cref="Trigger"/>:供场景战斗触发器 / UnityEvent / 动画事件在外部时机调用
|
||
/// (例如场景预置的天花板敌人被战斗触发器激活下坠)。</item>
|
||
/// </list>
|
||
/// 能力本身由挂载的 <c>EnemyAbilityBase</c> 组件(按 <c>EnemyAbilitySO.abilityId</c> 注册)实现,
|
||
/// 本组件只负责"在某个时机调用某个能力"。
|
||
/// </summary>
|
||
[DisallowMultipleComponent]
|
||
public class EnemyAbilityTrigger : MonoBehaviour
|
||
{
|
||
[Header("能力")]
|
||
[Tooltip("要执行的能力 Id(须与某个 EnemyAbilityBase 的 EnemyAbilitySO.abilityId 一致)")]
|
||
[SerializeField] private string _abilityId = "ability_id";
|
||
|
||
[Header("触发时机")]
|
||
[Tooltip("对象池生成 / OnSpawn 重置完成后自动执行(用于被生成出来即触发的敌人)")]
|
||
[SerializeField] private bool _executeOnSpawn = true;
|
||
|
||
private EnemyBase _enemy;
|
||
|
||
private void Awake()
|
||
{
|
||
_enemy = GetComponentInParent<EnemyBase>();
|
||
if (_enemy == null)
|
||
Debug.LogError($"[EnemyAbilityTrigger] {name} 找不到 EnemyBase。", this);
|
||
}
|
||
|
||
private void OnEnable()
|
||
{
|
||
if (_enemy != null) _enemy.Spawned += OnEnemySpawned;
|
||
}
|
||
|
||
private void OnDisable()
|
||
{
|
||
if (_enemy != null) _enemy.Spawned -= OnEnemySpawned;
|
||
}
|
||
|
||
private void OnEnemySpawned()
|
||
{
|
||
if (_executeOnSpawn) Trigger();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 立即执行配置的能力(外部时机触发:场景触发器 / UnityEvent / 动画事件调用)。
|
||
/// </summary>
|
||
public void Trigger()
|
||
{
|
||
_enemy?.Abilities.Get(_abilityId)?.Execute();
|
||
}
|
||
}
|
||
}
|