feat(enemy): ContactChaseAbility 改造为 RushAbility(冲锋)

- 起手前锁定玩家 X 与方向(committed),终点改为身体整体越过锁定点(Body.HasPassed)
- 终止条件:越过锁定点/撞墙(只判墙)/最大冲刺时长(SO 新增 maxDashDuration)/净位移卡死
- 冲锋期间经 EnemyMoveInput.AllowLedgeCross 放开悬崖夹紧,可冲下崖不悬停边缘
- 类与文件重命名(GUID 保留),脚手架与向导同步更新
This commit is contained in:
2026-07-27 13:38:05 +08:00
parent 7cba58919f
commit 4808077fc6
11 changed files with 215 additions and 146 deletions
@@ -1,19 +0,0 @@
using Animancer;
using UnityEngine;
namespace BaseGames.Enemies.Abilities
{
/// <summary>ContactChaseAbility 的配置:起手/循环/收招动画 + 冲刺速度。</summary>
[CreateAssetMenu(menuName = "BaseGames/Enemies/Abilities/Contact Chase Ability", fileName = "ABL_")]
public class ContactChaseAbilitySO : EnemyAbilitySO
{
[Header("动画(起手 / 循环 / 收招)")]
public ClipTransition startClip; // Skill_Start:张口(一次性)
public ClipTransition loopClip; // Skill_Loop:快速爬行追击(循环)
public ClipTransition endClip; // Skill_End:收招(一次性)
[Header("冲刺")]
[Tooltip("冲刺速度;<=0 时回退用 EnemyStatsSO.RunSpeed(追击速度)")]
public float dashSpeed = 0f;
}
}
@@ -0,0 +1,23 @@
using Animancer;
using UnityEngine;
namespace BaseGames.Enemies.Abilities
{
/// <summary>RushAbility(冲锋)的配置:起手/循环/收招动画 + 冲刺速度与时长上限。</summary>
[CreateAssetMenu(menuName = "BaseGames/Enemies/Abilities/Rush Ability", fileName = "ABL_")]
public class RushAbilitySO : EnemyAbilitySO
{
[Header("动画(起手 / 循环 / 收招)")]
public ClipTransition startClip; // Skill_Start:起手(一次性)
public ClipTransition loopClip; // Skill_Loop:冲锋推进(循环)
public ClipTransition endClip; // Skill_End:收招(一次性)
[Header("冲刺")]
[Tooltip("冲刺速度;<=0 时回退用 EnemyStatsSO.RunSpeed(追击速度)")]
public float dashSpeed = 0f;
[Tooltip("单次冲锋的最长时长(秒)。够不到锁定点时(掉坑/对岸/被非检测层挡住)由它收尾。" +
"0 = 不限时长(仅靠越过锁定点/撞墙/卡死检测终止)。")]
[Min(0f)] public float maxDashDuration = 2f;
}
}
@@ -1,113 +0,0 @@
using System.Collections;
using Animancer;
using UnityEngine;
namespace BaseGames.Enemies.Abilities
{
/// <summary>
/// 冲刺追击能力:起手(Skill_Start 张口) → 朝玩家方向直冲(Skill_Loop 快速爬行)
/// **直到撞墙/到平台边缘或玩家消失即停** → 收招(Skill_End)。一次冲刺为一次能力执行,
/// 结束后进入冷却(EnemyAbilitySO.cooldown)AI 追击态在冷却结束时经 EnsureAbility 重触发下一次冲刺。
/// 冲刺方向在起手后按玩家所在侧一次性确定(committed 冲锋,会越过跳开的玩家,可被躲)。
/// 追击动画由本能力全权驱动(Approach 步态不抢动画)。
/// 身体接触伤害与本能力解耦:由常驻的 <see cref="BodyContactDamage"/> 全程负责,本能力不再开关它。
/// </summary>
public class ContactChaseAbility : EnemyAbilityBase
{
private ContactChaseAbilitySO _cfg;
protected override void Awake()
{
base.Awake();
_cfg = ResolveConfig<ContactChaseAbilitySO>();
}
protected override IEnumerator ExecuteCoroutine()
{
if (_cfg == null) yield break;
Phase = AbilityRunState.Active;
_enemy.SetEngaged(true);
// 冲刺方向:按玩家所在侧一次性确定(committed 冲锋)
float dir = (_enemy.PlayerTransform != null &&
_enemy.PlayerTransform.position.x < _enemy.transform.position.x) ? -1f : 1f;
var mv = _enemy.Movement;
// 先转身朝玩家(背对时播 Flip 转身动画),转身完成后再张口
if (mv != null)
{
mv.FaceDirection((int)dir);
while (mv.IsTurning)
yield return null;
}
// 起手:张口(一次性)。期间静止、不伤人;锁住动画防被步态覆盖。
if (_cfg.startClip.Clip != null)
{
_enemy.LockAnim(_cfg.startClip.Clip.length);
_animancer.Play(_cfg.startClip);
yield return EnemyAbilityWaits.Get(_cfg.startClip.Clip.length);
}
// 冲刺速度:优先能力配置的 _cfg.dashSpeed,否则回退 Stats.RunSpeed
float speed = _cfg.dashSpeed > 0f ? _cfg.dashSpeed
: (_enemy.Stats != null ? _enemy.Stats.RunSpeed : 0f);
// 冲刺:朝该方向直冲、播 Skill_Loop,直到撞墙/到边缘或玩家消失即停。
// (身体接触伤害由常驻 BodyContactDamage 全程负责,此处不再开关。)
if (_cfg.loopClip.Clip != null)
_animancer.Play(_cfg.loopClip);
float winX = _enemy.transform.position.x;
float winT = 0f;
while (_enemy.PlayerTransform != null && !(mv != null && mv.WouldBlockAhead(dir)))
{
_enemy.MoveInDirectionWithSpeed(dir, speed); // 速度直冲(非 nav)MoveWithSpeed 悬崖夹紧兜底
yield return null;
// 兜底:撞上未在检测层的碰撞体(玩家/边界墙)导致原地不动 → 结束本次冲刺(转收招+冷却)。
// 用窗口净位移判定,避免"贴住微抖"每帧重置。
winT += Time.deltaTime;
if (winT >= 0.15f)
{
if (Mathf.Abs(_enemy.transform.position.x - winX) < 0.05f) break;
winX = _enemy.transform.position.x;
winT = 0f;
}
}
_enemy.StopMovement();
// 收招 Skill_End;随后基类按 EnemyAbilitySO.cooldown 施加冷却。
yield return PlaySkillEnd();
Cleanup();
}
protected override void OnInterrupted(InterruptReason reason)
{
// 丢失目标 = AI 主动退出追击态(ExternalRequest) → 播收招 Skill_End
// 受击/死亡/硬直等硬打断不播收招。协程已停,fire-and-forget;动画锁保证 Skill_End
// 在其时长内不被巡逻步态盖掉。
if (reason == InterruptReason.ExternalRequest && _cfg != null && _cfg.endClip.Clip != null)
{
_enemy.LockAnim(_cfg.endClip.Clip.length);
_animancer.Play(_cfg.endClip);
}
Cleanup();
}
private IEnumerator PlaySkillEnd()
{
if (_cfg == null || _cfg.endClip.Clip == null) yield break;
_enemy.LockAnim(_cfg.endClip.Clip.length);
_animancer.Play(_cfg.endClip);
yield return EnemyAbilityWaits.Get(_cfg.endClip.Clip.length);
}
private void Cleanup()
{
_enemy.SetEngaged(false);
_enemy.Locomotion?.Stop();
}
}
}
@@ -0,0 +1,156 @@
using System.Collections;
using Animancer;
using UnityEngine;
namespace BaseGames.Enemies.Abilities
{
/// <summary>
/// 冲锋能力:起手(Skill_Start) → 朝**锁定点**直冲(Skill_Loop) → 收招(Skill_End)。
/// 一次冲锋为一次能力执行,结束后进入冷却(EnemyAbilitySO.cooldown)。
///
/// 冲锋目标在**起手动画之前**锁定:记下玩家当时的 X 与由此确定的方向,此后玩家怎么动都不改变本次冲锋
/// committed 冲锋——起手即预警,玩家可在起手期间跑开躲掉)。
///
/// 终点判据是"**身体整体越过**锁定点"<see cref="IEnemyBody.HasPassed"/>),即碰撞体后缘越过锁定 X,
/// 而非中心到达——身体宽度取自身体几何权威 <see cref="EnemyBase.Body"/>,本能力不自行解析碰撞体。
///
/// 终止条件(任一满足即收招并进入冷却):身体整体越过锁定点(正常完成)/前方有墙(只判墙,不判悬崖)/
/// 超过 <see cref="RushAbilitySO.maxDashDuration"/>(够不到锁定点时收尾:掉坑、对岸、被非检测层挡住)/
/// 净位移卡死(撞上未纳入墙层的碰撞体时兜底)。
///
/// 悬崖:冲锋期间**允许越过崖沿**(经 <see cref="EnemyMoveInput.AllowLedgeCross"/> 放开移动层的悬崖夹紧),
/// 敌人会冲出平台边缘并按重力下落、空中保持水平冲速,而不是停在崖边悬空。
///
/// 冲锋动画由本能力全权驱动(步态不抢动画)。身体接触伤害与本能力解耦:
/// 由常驻的 <see cref="BodyContactDamage"/> 全程负责,本能力不开关它。
/// </summary>
public class RushAbility : EnemyAbilityBase
{
private RushAbilitySO _cfg;
// 卡死检测:窗口时长与窗口内最小净位移(撞上未纳入墙层的碰撞体时兜底结束冲锋)
private const float StuckWindowSeconds = 0.15f;
private const float StuckMinProgress = 0.05f;
protected override void Awake()
{
base.Awake();
_cfg = ResolveConfig<RushAbilitySO>();
}
protected override IEnumerator ExecuteCoroutine()
{
if (_cfg == null) yield break;
Phase = AbilityRunState.Active;
_enemy.SetEngaged(true);
// ── 锁定:起手之前记下玩家 X 与方向,此后不再跟随(committed 冲锋,可被躲)──
var player = _enemy.PlayerTransform;
float dir = (player != null && player.position.x < _enemy.transform.position.x) ? -1f : 1f;
float lockX = player != null
? player.position.x
: _enemy.transform.position.x + dir; // 无玩家:朝当前朝向冲一个身位,仍走完整流程
var mv = _enemy.Movement;
// 先转身朝锁定方向(背对时播转身动画),转身完成后再起手
if (mv != null)
{
mv.FaceDirection((int)dir);
while (mv.IsTurning)
yield return null;
}
// 起手:一次性预警动画。期间静止;锁住动画防被步态覆盖。
if (_cfg.startClip.Clip != null)
{
_enemy.LockAnim(_cfg.startClip.Clip.length);
_animancer.Play(_cfg.startClip);
yield return EnemyAbilityWaits.Get(_cfg.startClip.Clip.length);
}
// 冲刺速度:优先能力配置的 dashSpeed,否则回退 Stats.RunSpeed
float speed = _cfg.dashSpeed > 0f ? _cfg.dashSpeed
: (_enemy.Stats != null ? _enemy.Stats.RunSpeed : 0f);
if (_cfg.loopClip.Clip != null)
_animancer.Play(_cfg.loopClip);
yield return RushRoutine(dir, lockX, speed, mv);
_enemy.StopMovement();
// 收招 Skill_End;随后基类按 EnemyAbilitySO.cooldown 施加冷却。
yield return PlaySkillEnd();
Cleanup();
}
/// <summary>
/// 冲锋推进:每帧朝锁定方向施加冲速(允许越崖),
/// 直到身体整体越过锁定点/撞墙/超时/卡死。
/// </summary>
private IEnumerator RushRoutine(float dir, float lockX, float speed, EnemyMovement mv)
{
var body = _enemy.Body;
float elapsed = 0f;
float winX = _enemy.transform.position.x;
float winT = 0f;
while (true)
{
// 终点:身体整体越过锁定点(后缘越过;宽度由身体权威提供)
if (body != null && body.HasPassed(lockX, dir)) break;
// 撞墙即止(悬崖不再终止——冲锋要能冲下去)
if (mv != null && mv.WouldHitWallAhead(dir)) break;
// 超时收尾:够不到锁定点(掉坑/对岸/被非检测层挡住)
if (_cfg.maxDashDuration > 0f && elapsed >= _cfg.maxDashDuration) break;
_enemy.MoveInDirectionWithSpeed(dir, speed, allowLedgeCross: true);
yield return null;
float dt = Time.deltaTime;
elapsed += dt;
winT += dt;
// 兜底:撞上未纳入墙层的碰撞体(玩家/边界墙)导致原地不动 → 结束本次冲锋。
// 用窗口净位移判定,避免"贴住微抖"每帧重置。
if (winT >= StuckWindowSeconds)
{
if (Mathf.Abs(_enemy.transform.position.x - winX) < StuckMinProgress) break;
winX = _enemy.transform.position.x;
winT = 0f;
}
}
}
protected override void OnInterrupted(InterruptReason reason)
{
// 丢失目标 = AI 主动退出(ExternalRequest) → 播收招 Skill_End
// 受击/死亡/硬直等硬打断不播收招。协程已停,fire-and-forget;动画锁保证 Skill_End
// 在其时长内不被巡逻步态盖掉。
if (reason == InterruptReason.ExternalRequest && _cfg != null && _cfg.endClip.Clip != null)
{
_enemy.LockAnim(_cfg.endClip.Clip.length);
_animancer.Play(_cfg.endClip);
}
Cleanup();
}
private IEnumerator PlaySkillEnd()
{
if (_cfg == null || _cfg.endClip.Clip == null) yield break;
_enemy.LockAnim(_cfg.endClip.Clip.length);
_animancer.Play(_cfg.endClip);
yield return EnemyAbilityWaits.Get(_cfg.endClip.Clip.length);
}
private void Cleanup()
{
_enemy.SetEngaged(false);
_enemy.Locomotion?.Stop(); // 经 StopMovement 路径一并复位 AllowLedgeCross
}
}
}
+12 -3
View File
@@ -266,11 +266,20 @@ namespace BaseGames.Enemies
}
public virtual void MoveInDirectionWithSpeed(float dir, float speed)
=> MoveInDirectionWithSpeed(dir, speed, false);
/// <summary>
/// 显式速度移动;<paramref name="allowLedgeCross"/> = true 时允许冲出崖沿并自由落体
/// committed 冲锋用,避免被移动层的悬崖夹紧停在边缘)。
/// 该许可随移动意图存续,StopMovement() 时自动复位。
/// </summary>
public virtual void MoveInDirectionWithSpeed(float dir, float speed, bool allowLedgeCross)
{
if (_movement == null) return;
_movement.PendingInput.MoveDir = dir;
_movement.PendingInput.MoveSpeed = speed;
_movement.PendingInput.WantStop = false; // 移动意图覆盖停止脉冲
_movement.PendingInput.MoveDir = dir;
_movement.PendingInput.MoveSpeed = speed;
_movement.PendingInput.AllowLedgeCross = allowLedgeCross;
_movement.PendingInput.WantStop = false; // 移动意图覆盖停止脉冲
}
public virtual void StopMovement()
@@ -34,5 +34,13 @@ namespace BaseGames.Enemies
/// <summary>直接指定朝向方向:+1 右 / -1 左 / 0 表示由 FaceTargetPos 计算。</summary>
public int FaceDir;
/// <summary>
/// 本次移动允许越过崖沿(committed 冲锋用):置 true 时 <see cref="EnemyMovement.MoveWithSpeed"/>
/// 不再做悬崖夹紧,敌人会冲出平台边缘并按重力下落,而非停在崖边。
/// <b>持久</b>——随 MoveDir/MoveSpeed 同步清零(WantStop 时复位),故 StopMovement() 即自动关闭,
/// 不会因能力被打断而残留。
/// </summary>
public bool AllowLedgeCross;
}
}
@@ -404,12 +404,13 @@ namespace BaseGames.Enemies
{
PendingInput.MoveDir = 0f;
PendingInput.MoveSpeed = 0f;
PendingInput.AllowLedgeCross = false; // 随移动意图一并复位,避免越崖许可残留
StopHorizontal();
}
else if (PendingInput.MoveDir != 0f)
{
if (PendingInput.MoveSpeed > 0f)
MoveWithSpeed(PendingInput.MoveDir, PendingInput.MoveSpeed);
MoveWithSpeed(PendingInput.MoveDir, PendingInput.MoveSpeed, PendingInput.AllowLedgeCross);
else
MoveHorizontal(PendingInput.MoveDir);
}
@@ -435,15 +436,19 @@ namespace BaseGames.Enemies
_rb.velocity = vel;
}
/// <summary>显式指定速度(BD 追击任务调用)。转身动画期间调用无效。</summary>
public void MoveWithSpeed(float dir, float speed)
/// <summary>
/// 显式指定速度移动。转身动画期间调用无效。
/// <paramref name="allowLedgeCross"/> = true 时跳过悬崖夹紧,允许冲出平台边缘后自由落体
/// committed 冲锋用;见 <see cref="EnemyMoveInput.AllowLedgeCross"/>)。
/// </summary>
public void MoveWithSpeed(float dir, float speed, bool allowLedgeCross = false)
{
if (_isTurning) return;
if (dir != 0f) UpdateFacing(dir);
// 地面怪追击/导航:前缘将越过地形边缘(悬崖)则不前进——碰撞体边缘停在地形边缘、不越界。
// 只夹"悬崖"不夹"墙"(墙交给寻路绕行,免得贴墙路径被误夹停);跨沟的 nav-link 跳/落
// 不经本方法(EnemyNavAgent 仅在 IsMovingOnSegment 时写 MoveSpeed),故不影响跳跃/下落过沟。
if (dir != 0f && IsGrounded && WouldFallAhead(dir)) dir = 0f;
if (dir != 0f && !allowLedgeCross && IsGrounded && WouldFallAhead(dir)) dir = 0f;
var vel = _rb.velocity;
vel.x = dir * speed;
_rb.velocity = vel;