feat(enemy): PerceptionStateMachine 增加 ApproachAttack 风格(寻路逼近↔选招攻击)+测试

This commit is contained in:
2026-07-24 12:27:59 +08:00
parent 297f9d2968
commit 6cdd4ef513
2 changed files with 137 additions and 16 deletions
@@ -14,6 +14,9 @@ namespace BaseGames.Enemies
/// </summary>
public static class PerceptionStateMachine
{
/// <summary>交战风格:单一冲锋能力,或"寻路逼近 + 选招攻击"两态子机。</summary>
public enum EngagementStyle { ChaseAbility, ApproachAttack }
public sealed class Config
{
public string Idle = "Idle";
@@ -34,6 +37,11 @@ namespace BaseGames.Enemies
// 追击=带冷却的冲刺能力时开启(opt-in):冲刺打完→巡逻(CD 期),冷却结束且仍在追逐区→再冲刺。
// 由冷却门控 Patrol→Chase,避免"CD 期在追逐区每帧 Chase↔Patrol 来回抖"。
public bool PatrolBetweenChases = false;
// 交战风格(默认冲锋能力,保持现状);ApproachAttack 时启用 Approach↔Attack 两态子机。
public EngagementStyle Engagement = EngagementStyle.ChaseAbility;
public string ApproachState = "Approach";
public string AttackState = "Attack";
}
public static void Add(BrainBuilder b, Config c)
@@ -42,35 +50,66 @@ namespace BaseGames.Enemies
b.Global().To(c.Death).OnEvent(AiSignal.Died); // 受击致死 → 死亡(全局最高优先)
// 进追击的条件:默认"在追逐区"。PatrolBetweenChases 时加冷却门——CD 中不回追击,
// 使 Chase(冲刺)→Patrol(CD 巡逻)→Chase 循环不抖动。
// 使 Chase(冲刺)→Patrol(CD 巡逻)→Chase 循环不抖动。冷却门仅在冲锋能力风格下生效。
string chaseId = c.ChaseAbilityId;
System.Func<IAiContext, bool> canChase = c.PatrolBetweenChases
? (x => x.Sensor.InChaseZone() && x.Combat.CanUseAbility(chaseId))
: (x => x.Sensor.InChaseZone());
string chaseLabel = c.PatrolBetweenChases ? "InChaseZone+offCD" : "InChaseZone";
System.Func<IAiContext, bool> canChase;
string chaseLabel;
if (c.Engagement == EngagementStyle.ChaseAbility && c.PatrolBetweenChases)
{
canChase = x => x.Sensor.InChaseZone() && x.Combat.CanUseAbility(chaseId);
chaseLabel = "InChaseZone+offCD";
}
else
{
canChase = x => x.Sensor.InChaseZone();
chaseLabel = "InChaseZone";
}
// 交战入口态:冲锋风格=Chase;逼近风格=Approach
string combatEntry = c.Engagement == EngagementStyle.ApproachAttack ? c.ApproachState : c.Chase;
// 待机 / 巡逻:未发现态,升级转换相同(追逐优先、其次警觉)
AddLocomotionState(b, c.Idle, c.IdleMode)
.To(c.Chase).When(canChase, chaseLabel)
.To(combatEntry).When(canChase, chaseLabel)
.To(c.Alert).When(x => HasAlert(x) && x.Sensor.InVisionZone(), "InVision+HasAlert");
AddLocomotionState(b, c.Patrol, c.PatrolMode)
.To(c.Chase).When(canChase, chaseLabel)
.To(combatEntry).When(canChase, chaseLabel)
.To(c.Alert).When(x => HasAlert(x) && x.Sensor.InVisionZone(), "InVision+HasAlert");
// 警觉(仅升级路径出现):进追逐区→追击;脱离视野→R;在视野内保持(朝向玩家)
AddLocomotionState(b, c.Alert, c.AlertMode)
.To(c.Chase).When(canChase, chaseLabel)
.To(combatEntry).When(canChase, chaseLabel)
.To(c.Rest).When(x => !x.Sensor.InVisionZone(), "leftVision");
// 追击退出:
// PatrolBetweenChases: 冲刺打完(能力结束)→R(CD 期巡逻);committed 冲锋不被感知丢失中途打断,
// 是否再冲刺交由 Patrol 侧冷却门控。
// 默认: 脱离全部感知(追逐区且视野区都不在)→R(永不回警觉)。
var chase = AddAbilityState(b, c.Chase, chaseId);
if (c.PatrolBetweenChases)
chase.To(c.Rest).When(x => !x.Combat.IsAbilityRunning(chaseId), "dashDone→CD");
if (c.Engagement == EngagementStyle.ApproachAttack)
{
// Approach:寻路逼近;有招够得着→Attack;脱离全部感知→R
b.State(c.ApproachState)
.OnEnter(x => x.Locomotion.Pursue(x.Sensor.LastKnown))
.Tick(x => x.Locomotion.Pursue(x.Sensor.LastKnown))
.OnExit(x => x.Locomotion.Stop())
.To(c.AttackState).When(x => x.Combat.HasEligibleAttack(), "attackInRange")
.To(c.Rest).When(x => !x.Sensor.InChaseZone() && !x.Sensor.InVisionZone(), "leftAllZones");
// Attack:停逼近 + 选招攻击;打完回 Approach;脱离感知→R
b.State(c.AttackState)
.OnEnter(x => { x.Locomotion.Stop(); x.Combat.UseBestAttack(); })
.OnExit(x => x.Combat.InterruptAbilities())
.To(c.ApproachState).When(x => !x.Combat.IsAbilityRunning(), "attackDone")
.To(c.Rest).When(x => !x.Sensor.InChaseZone() && !x.Sensor.InVisionZone(), "leftAllZones");
}
else
chase.To(c.Rest).When(x => !x.Sensor.InChaseZone() && !x.Sensor.InVisionZone(), "leftAllZones");
{
// 追击退出:
// PatrolBetweenChases: 冲刺打完(能力结束)→R(CD 期巡逻);committed 冲锋不被感知丢失中途打断,
// 是否再冲刺交由 Patrol 侧冷却门控。
// 默认: 脱离全部感知(追逐区且视野区都不在)→R(永不回警觉)。
var chase = AddAbilityState(b, c.Chase, chaseId);
if (c.PatrolBetweenChases)
chase.To(c.Rest).When(x => !x.Combat.IsAbilityRunning(chaseId), "dashDone→CD");
else
chase.To(c.Rest).When(x => !x.Sensor.InChaseZone() && !x.Sensor.InVisionZone(), "leftAllZones");
}
// 死亡:终态(无转出);死亡演出走物理状态机
AddAbilityState(b, c.Death, c.DeathAbilityId);