diff --git a/Assets/Tests/EditMode/AI/PerceptionStateMachineTests.cs b/Assets/Tests/EditMode/AI/PerceptionStateMachineTests.cs index 38bc4259..b1a7b572 100644 --- a/Assets/Tests/EditMode/AI/PerceptionStateMachineTests.cs +++ b/Assets/Tests/EditMode/AI/PerceptionStateMachineTests.cs @@ -275,6 +275,88 @@ namespace BaseGames.Tests.EditMode.AI Assert.AreEqual(LocomotionMode.Patrol, ctx.L.CurrentMode); // 落地即巡逻移动意图 } + // ── ApproachAttack 交战风格 ─────────────────────────────────────── + static AiGraph GraphApproach(string entry = "Idle") + { + var b = new BrainBuilder(); + PerceptionStateMachine.Add(b, new PerceptionStateMachine.Config + { + Idle = "Idle", Patrol = "Patrol", Alert = "Alert", Death = "Death", + Entry = entry, Rest = "Patrol", + IdleMode = LocomotionMode.Idle, PatrolMode = LocomotionMode.Patrol, AlertMode = LocomotionMode.Face, + Engagement = PerceptionStateMachine.EngagementStyle.ApproachAttack, + ApproachState = "Approach", AttackState = "Attack", + }); + return b.Build(); + } + + [Test] + public void Approach_EnteredFromChaseZone_UsesPursue() + { + var ctx = new Ctx(); + var rt = new AiRuntime(GraphApproach(), ctx); + ctx.S.Chase = true; + rt.Tick(0.1f); + Assert.AreEqual("Approach", rt.CurrentStateName); + CollectionAssert.Contains(ctx.L.Calls, "Pursue"); + } + + [Test] + public void Approach_ToAttack_WhenEligibleAttack() + { + var ctx = new Ctx(); + var rt = new AiRuntime(GraphApproach(), ctx); + ctx.S.Chase = true; rt.Tick(0.1f); + ctx.C.Eligible = true; + rt.Tick(0.1f); + Assert.AreEqual("Attack", rt.CurrentStateName); + CollectionAssert.Contains(ctx.C.Used, "best"); + } + + [Test] + public void Attack_StopsLocomotion_OnEnter() + { + var ctx = new Ctx(); + var rt = new AiRuntime(GraphApproach(), ctx); + ctx.S.Chase = true; rt.Tick(0.1f); + ctx.C.Eligible = true; rt.Tick(0.1f); + CollectionAssert.Contains(ctx.L.Calls, "Stop"); + } + + [Test] + public void Attack_ToApproach_WhenAttackDone() + { + var ctx = new Ctx(); + var rt = new AiRuntime(GraphApproach(), ctx); + ctx.S.Chase = true; rt.Tick(0.1f); + ctx.C.Eligible = true; rt.Tick(0.1f); + Assert.AreEqual("Attack", rt.CurrentStateName); + ctx.C.Running = null; + rt.Tick(0.1f); + Assert.AreEqual("Approach", rt.CurrentStateName); + } + + [Test] + public void Approach_ToRest_WhenLeftAllZones() + { + var ctx = new Ctx(); + var rt = new AiRuntime(GraphApproach(), ctx); + ctx.S.Chase = true; rt.Tick(0.1f); + ctx.S.Chase = false; ctx.S.Vision = false; + rt.Tick(0.1f); + Assert.AreEqual("Patrol", rt.CurrentStateName); + } + + [Test] + public void ApproachAttack_Death_IsGlobal() + { + var ctx = new Ctx(); + var rt = new AiRuntime(GraphApproach(), ctx); + ctx.S.Chase = true; rt.Tick(0.1f); + rt.Send(AiSignal.Died); rt.Tick(0.1f); + Assert.AreEqual("Death", rt.CurrentStateName); + } + // ── 死亡(全局事件,任意态可达)─────────────────────────────────── [Test] diff --git a/Assets/_Game/Scripts/Enemies/AIBrain/PerceptionStateMachine.cs b/Assets/_Game/Scripts/Enemies/AIBrain/PerceptionStateMachine.cs index ac49a131..4865b2b9 100644 --- a/Assets/_Game/Scripts/Enemies/AIBrain/PerceptionStateMachine.cs +++ b/Assets/_Game/Scripts/Enemies/AIBrain/PerceptionStateMachine.cs @@ -14,6 +14,9 @@ namespace BaseGames.Enemies /// public static class PerceptionStateMachine { + /// 交战风格:单一冲锋能力,或"寻路逼近 + 选招攻击"两态子机。 + 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 canChase = c.PatrolBetweenChases - ? (x => x.Sensor.InChaseZone() && x.Combat.CanUseAbility(chaseId)) - : (x => x.Sensor.InChaseZone()); - string chaseLabel = c.PatrolBetweenChases ? "InChaseZone+offCD" : "InChaseZone"; + System.Func 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);