feat(enemy): ContactChaseAbility 改造为 RushAbility(冲锋)
- 起手前锁定玩家 X 与方向(committed),终点改为身体整体越过锁定点(Body.HasPassed) - 终止条件:越过锁定点/撞墙(只判墙)/最大冲刺时长(SO 新增 maxDashDuration)/净位移卡死 - 冲锋期间经 EnemyMoveInput.AllowLedgeCross 放开悬崖夹紧,可冲下崖不悬停边缘 - 类与文件重命名(GUID 保留),脚手架与向导同步更新
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user