Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
62 lines
2.2 KiB
C#
62 lines
2.2 KiB
C#
using System.Collections;
|
|
using Animancer;
|
|
using UnityEngine;
|
|
|
|
namespace BaseGames.Enemies.Abilities
|
|
{
|
|
/// <summary>
|
|
/// 循环追击 + 体接触伤害能力(纯执行器)。
|
|
/// 每帧 MoveTo 追向玩家、开接触伤害;**何时停止由 AI 决策层负责**——AI 在"脱离视野"时
|
|
/// 中断本能力(AI decides when to stop, ability implements the chase)。
|
|
/// 可复用于任何需要"追击+接触伤害循环"的敌人。
|
|
/// </summary>
|
|
public class ContactChaseAbility : EnemyAbilityBase
|
|
{
|
|
[Header("动画")]
|
|
[SerializeField] private ClipTransition _loopClip;
|
|
[SerializeField] private ClipTransition _endClip;
|
|
|
|
[Header("接触伤害")]
|
|
[SerializeField] private BodyContactDamage _contactDamage;
|
|
|
|
protected override IEnumerator ExecuteCoroutine()
|
|
{
|
|
Phase = AbilityRunState.Active;
|
|
|
|
// 追击行为的速度/阶段声明归本能力(AI 只触发能力,不做这些执行)
|
|
if (_enemy.Nav != null && _enemy.Stats != null)
|
|
_enemy.Nav.SetSpeed(_enemy.Stats.RunSpeed);
|
|
_enemy.SetAiPhase(AiPhase.Chase);
|
|
|
|
if (_loopClip.Clip != null)
|
|
_animancer.Play(_loopClip);
|
|
|
|
if (_contactDamage != null)
|
|
_contactDamage.enabled = true;
|
|
|
|
// 追击直到玩家消失或被 AI 中断(退出追击的决策归 AI,见 EnemyBrainContext.InterruptAbilities)。
|
|
// 小怪走 nav 寻路(EnemyBase.MoveTo → 导航),寻路正确性依赖脚手架把碰撞体底部对齐 y=0。
|
|
_enemy.Locomotion?.Approach(_enemy.PlayerTransform);
|
|
while (_enemy.PlayerTransform != null)
|
|
yield return null;
|
|
|
|
CleanupChase();
|
|
|
|
if (_endClip.Clip != null)
|
|
{
|
|
_animancer.Play(_endClip);
|
|
yield return EnemyAbilityWaits.Get(_endClip.Clip.length);
|
|
}
|
|
}
|
|
|
|
protected override void OnInterrupted(InterruptReason reason) => CleanupChase();
|
|
|
|
private void CleanupChase()
|
|
{
|
|
if (_contactDamage != null)
|
|
_contactDamage.enabled = false;
|
|
_enemy.Locomotion?.Stop();
|
|
}
|
|
}
|
|
}
|