refactor(enemy): 敌人专属子类改为零代码配置型行为组件

This commit is contained in:
2026-06-09 15:56:44 +08:00
parent 7781ac4755
commit ebf0c97320
22 changed files with 496 additions and 242 deletions

View File

@@ -0,0 +1,59 @@
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();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 3198979f0bd38e1429478e7937b280b3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,29 @@
namespace BaseGames.Enemies.Behaviors
{
/// <summary>
/// 死亡前摇演出组件接口(零代码替代每敌人专属的 <c>Die()</c> 重写)。
/// <para>
/// <see cref="EnemyBase.Die"/> 检测到子物体挂载此接口组件时,委托其播放无敌前摇演出,
/// 演出结束后必须回调 <paramref name="onComplete"/>(即 <see cref="EnemyBase.PerformDeath"/>
/// 执行真正的死亡清理。演出期间 <see cref="EnemyBase.IsInvincible"/> 为 true。
/// </para>
/// </summary>
public interface IEnemyDeathSequence
{
/// <summary>开始播放死亡前摇演出;演出结束后必须调用 <paramref name="onComplete"/>。</summary>
void Play(System.Action onComplete);
}
/// <summary>
/// 动画事件生成处理器接口(零代码替代每敌人专属的 <c>SpawnProjectile()</c> 重写)。
/// <para>
/// <see cref="EnemyBase.SpawnProjectile"/> 会把 payload 路由到所有挂载的此接口组件,
/// 由组件自行按 payload 匹配决定是否生成(如从对象池生成小怪 / 弹幕)。
/// </para>
/// </summary>
public interface IEnemySpawnEventHandler
{
/// <summary>处理一次动画事件生成请求。<paramref name="payload"/> 为动画事件携带的配置 Id。</summary>
void HandleSpawn(string payload);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6757468022586aa41aab08314ca5a24f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,86 @@
using System;
using System.Collections;
using Animancer;
using BaseGames.Combat;
using UnityEngine;
namespace BaseGames.Enemies.Behaviors
{
/// <summary>
/// 死亡前摇无敌演出(零代码替代每敌人专属的 <c>Die()</c> 重写)。
/// <para>
/// <see cref="EnemyBase.Die"/> 会委托本组件:停行为树 / 停移动、停用受击框,播放前摇动画并等待,
/// 期间敌人处于无敌(<see cref="EnemyBase.IsInvincible"/>),演出结束后回调真正的死亡清理。
/// 前摇动画上可放置 SpawnProjectile 动画事件,配合 <see cref="EnemySpawnerOnEvent"/> 在演出中生成小怪。
/// </para>
/// </summary>
[DisallowMultipleComponent]
public class EnemyDeathSequence : MonoBehaviour, IEnemyDeathSequence
{
[Header("前摇动画")]
[Tooltip("死亡前摇动画(无敌演出);为空则跳过演出直接进入死亡清理")]
[SerializeField] private ClipTransition _deathPreClip;
[Tooltip("前摇演出时长(秒)")]
[Min(0f)][SerializeField] private float _duration = 3f;
[Header("演出期间")]
[Tooltip("演出期间停用的受击框(防止演出中被打断或二次受伤);对象池复用时 OnSpawn 自动恢复")]
[SerializeField] private HurtBox[] _hurtBoxesToDisable;
[Tooltip("演出开始时停止行为树(防止 BT 继续 Tick 覆盖演出动画)")]
[SerializeField] private bool _stopBehaviorTree = true;
[Tooltip("演出开始时停止移动")]
[SerializeField] private bool _stopMovement = true;
private EnemyBase _enemy;
private AnimancerComponent _animancer;
private void Awake()
{
_enemy = GetComponentInParent<EnemyBase>();
_animancer = _enemy != null ? _enemy.Animancer : GetComponentInParent<AnimancerComponent>();
if (_enemy == null)
Debug.LogError($"[EnemyDeathSequence] {name} 找不到 EnemyBase。", this);
}
// 对象池复用:出生时恢复受击框(演出中曾被停用)
private void OnEnable()
{
if (_enemy != null) _enemy.Spawned += RestoreHurtBoxes;
}
private void OnDisable()
{
if (_enemy != null) _enemy.Spawned -= RestoreHurtBoxes;
}
public void Play(Action onComplete)
{
StartCoroutine(Sequence(onComplete));
}
private IEnumerator Sequence(Action onComplete)
{
if (_stopBehaviorTree) _enemy?.StopBehaviorTree();
if (_stopMovement) _enemy?.StopMovement();
SetHurtBoxesEnabled(false);
if (_deathPreClip.Clip != null && _animancer != null)
{
_animancer.Play(_deathPreClip);
if (_duration > 0f) yield return new WaitForSeconds(_duration);
}
onComplete?.Invoke();
}
private void RestoreHurtBoxes() => SetHurtBoxesEnabled(true);
private void SetHurtBoxesEnabled(bool enabled)
{
if (_hurtBoxesToDisable == null) return;
foreach (var hb in _hurtBoxesToDisable)
if (hb != null) hb.enabled = enabled;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4a4a8aad8881b4543a0918321e7efe3e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,46 @@
using BaseGames.Core;
using BaseGames.Core.Pool;
using UnityEngine;
namespace BaseGames.Enemies.Behaviors
{
/// <summary>
/// 动画事件触发的对象池生成(零代码替代每敌人专属的 <c>SpawnProjectile()</c> 重写)。
/// <para>
/// <see cref="EnemyBase.SpawnProjectile"/> 会把 payload 路由到本组件;当 payload 与
/// <c>payloadKey</c> 匹配时,在自身周围随机位置从对象池生成 <c>count</c> 个指定 key 的对象
/// (例如精英怪死亡前摇中生成 N 个小怪)。
/// </para>
/// 使用方式:在动画(如死亡前摇)相应帧放置 SpawnProjectile 动画事件payload 填 <c>payloadKey</c>。
/// 同一敌人可挂多个本组件(不同 payloadKey以生成不同对象。
/// </summary>
public class EnemySpawnerOnEvent : MonoBehaviour, IEnemySpawnEventHandler
{
[Header("触发匹配")]
[Tooltip("匹配的动画事件 payload如 \"spawn_e003\");留空则匹配任意 payload")]
[SerializeField] private string _payloadKey = "";
[Header("生成")]
[Tooltip("对象池 keyIObjectPoolService.Spawn 的 key须经 AddressKeys 常量约定,如 \"ENM_YouZhi\"")]
[SerializeField] private string _poolKey = "";
[Tooltip("生成数量")]
[Min(1)][SerializeField] private int _count = 3;
[Tooltip("生成点相对自身的随机半径")]
[Min(0f)][SerializeField] private float _radius = 1.5f;
public void HandleSpawn(string payload)
{
if (!string.IsNullOrEmpty(_payloadKey) && payload != _payloadKey) return;
if (string.IsNullOrEmpty(_poolKey)) return;
var pool = ServiceLocator.GetOrDefault<IObjectPoolService>();
if (pool == null) return;
for (int i = 0; i < _count; i++)
{
Vector2 offset = Random.insideUnitCircle * _radius;
pool.Spawn(_poolKey, (Vector2)transform.position + offset, Quaternion.identity);
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: fd9abafdb6bedcc4b8fd011bde9208b6
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: