feat(enemy): AiStateFragments 建态原语,新增 AbilityOnce 修死亡态重触发隐患

从 PerceptionStateMachine 的 private static 辅助方法提升为公开原语,供后续
模块化重构(骨架+可插拔模块)与定制 AiScript 复用。新增 AbilityOnce:
只在进入时触发能力、不每帧重触发,修复用 Ability() 驱动死亡态时演出播完
被 Tick 无限重播的隐患(现有 PerceptionStateMachine 因 E001 死亡能力 id
为空未暴露,本次不改它,留待 T11 整体退役)。
This commit is contained in:
2026-07-29 11:35:47 +08:00
parent 084354fa6c
commit 77a32cbf8a
6 changed files with 161 additions and 2 deletions
@@ -0,0 +1,68 @@
using NUnit.Framework;
using BaseGames.AI;
using BaseGames.Enemies;
namespace BaseGames.Tests.EditMode.AI
{
public class AiStateFragmentsTests
{
static AiRuntime Run(System.Action<BrainBuilder> build, FakeAiContext ctx)
{
var b = new BrainBuilder();
b.Entry("S");
build(b);
return new AiRuntime(b.Build(), ctx);
}
[Test]
public void Locomotion_SetsModeOnEnter()
{
var ctx = new FakeAiContext();
Run(b => AiStateFragments.Locomotion(b, "S", LocomotionMode.Patrol), ctx);
Assert.AreEqual(LocomotionMode.Patrol, ctx.L.CurrentMode);
}
[Test]
public void Locomotion_FaceMode_FacesLastKnown()
{
var ctx = new FakeAiContext();
ctx.S.Last = new UnityEngine.Vector2(3f, 0f);
Run(b => AiStateFragments.Locomotion(b, "S", LocomotionMode.Face), ctx);
Assert.AreEqual(new UnityEngine.Vector2(3f, 0f), ctx.L.FacedAt);
}
[Test]
public void Ability_RetriggersOnTick_WhenNotRunning()
{
var ctx = new FakeAiContext();
var rt = Run(b => AiStateFragments.Ability(b, "S", "atk"), ctx);
Assert.AreEqual(1, ctx.C.Used.Count);
ctx.C.Running = null; // 模拟被受击打断
rt.Tick(0.1f);
Assert.AreEqual(2, ctx.C.Used.Count); // 重新触发
}
[Test]
public void AbilityOnce_DoesNotRetrigger()
{
var ctx = new FakeAiContext();
var rt = Run(b => AiStateFragments.AbilityOnce(b, "S", "death"), ctx);
Assert.AreEqual(1, ctx.C.Used.Count);
ctx.C.Running = null; // 演出播完
rt.Tick(0.1f);
rt.Tick(0.1f);
Assert.AreEqual(1, ctx.C.Used.Count); // 不重播
}
[Test]
public void LostAllZones_TrueOnlyWhenBothZonesEmpty()
{
var ctx = new FakeAiContext();
Assert.IsTrue(AiStateFragments.LostAllZones(ctx));
ctx.S.Chase = true;
Assert.IsFalse(AiStateFragments.LostAllZones(ctx));
ctx.S.Chase = false; ctx.S.Vision = true;
Assert.IsFalse(AiStateFragments.LostAllZones(ctx));
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 7119f55162bc4944d93f017720546cc2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -11,9 +11,11 @@ namespace BaseGames.Tests.EditMode.AI
public bool IsMoving { get; set; }
public void SetMode(LocomotionMode mode) { CurrentMode = mode; Calls.Add("SetMode:" + mode); }
public void Approach(Transform t) { CurrentMode = LocomotionMode.Approach; Calls.Add("Approach"); }
public void Pursue(Vector2 p) { CurrentMode = LocomotionMode.Approach; Calls.Add("Pursue"); }
public Vector2 PursuedTo;
public void Pursue(Vector2 p) { PursuedTo = p; CurrentMode = LocomotionMode.Approach; Calls.Add("Pursue"); }
public void MoveTo(Vector2 p) { Calls.Add("MoveTo"); }
public void Face(Vector2 p) { CurrentMode = LocomotionMode.Face; Calls.Add("Face"); }
public Vector2 FacedAt;
public void Face(Vector2 p) { FacedAt = p; CurrentMode = LocomotionMode.Face; Calls.Add("Face"); }
public void Stop() { CurrentMode = LocomotionMode.Idle; Calls.Add("Stop"); }
}
}