@
feat(ai): 追击冲刺带冷却——CD 期巡逻,冷却结束仍在追逐区则再冲(PatrolBetweenChases) 按需求:冲刺打完 → CD 期切到巡逻(Move_Patrol) → 冷却结束且玩家仍在追逐区 → 再冲刺。 PerceptionStateMachine 加 opt-in 开关 PatrolBetweenChases: - Chase → Patrol When !IsAbilityRunning(chase):冲刺是 committed 冲锋,感知丢失 也不中途打断,打完(能力结束)即降巡逻。 - Idle/Patrol/Alert → Chase 加冷却门 CanUseAbility(chase):CD 中不回追击, 避免"CD 期在追逐区每帧 Chase↔Patrol 来回抖"(用户担心的错切/抖动)。 - 默认关:不影响其他敌人(leftAllZones 旧行为保留)。 E001CaoZhiAi 打开开关。追击能力保持单次冲刺不变,冷却=ABL_E001_Chase.cooldown(1s)。 测试:PerceptionStateMachineTests +5(CD 转换矩阵含"CD 期多帧不抖动"负例), FakeCombat 加可控 CanUse。全量 75/75 绿。PlayMode 实测循环 Move_Patrol(CD)→Chase(冲)→Move_Patrol,inChase 全程 true 亦不抖,0 报错。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> @
This commit is contained in:
@@ -19,10 +19,11 @@ namespace BaseGames.Tests.EditMode.AI
|
||||
public sealed class FakeCombat : ICombatant
|
||||
{
|
||||
public string Running; public List<string> Used = new List<string>();
|
||||
public bool CanUse = true; // 供 CD 门控测试控制(默认可用,向后兼容)
|
||||
public bool UseAbility(string id) { Used.Add(id); Running = id; return true; }
|
||||
public bool IsAbilityRunning(string id = null) => id == null ? Running != null : Running == id;
|
||||
public bool NoAbilityRunning => Running == null;
|
||||
public bool CanUseAbility(string id) => true;
|
||||
public bool CanUseAbility(string id) => CanUse;
|
||||
public void InterruptAbilities() => Running = null;
|
||||
}
|
||||
|
||||
|
||||
@@ -47,6 +47,21 @@ namespace BaseGames.Tests.EditMode.AI
|
||||
return b.Build();
|
||||
}
|
||||
|
||||
// PatrolBetweenChases 变体:冲刺打完→巡逻(CD),冷却结束仍在追逐区→再冲刺。
|
||||
static AiGraph GraphCD()
|
||||
{
|
||||
var b = new BrainBuilder();
|
||||
PerceptionStateMachine.Add(b, new PerceptionStateMachine.Config
|
||||
{
|
||||
Idle = "Idle", Patrol = "Patrol", Alert = "Alert", Chase = "Chase", Death = "Death",
|
||||
Entry = "Idle", Rest = "Patrol",
|
||||
IdleMode = LocomotionMode.Idle, PatrolMode = LocomotionMode.Patrol, AlertMode = LocomotionMode.Face,
|
||||
ChaseAbilityId = "chase",
|
||||
PatrolBetweenChases = true,
|
||||
});
|
||||
return b.Build();
|
||||
}
|
||||
|
||||
// ── 入口 / 待机 ───────────────────────────────────────────────────
|
||||
|
||||
[Test]
|
||||
@@ -292,5 +307,74 @@ namespace BaseGames.Tests.EditMode.AI
|
||||
rt.Tick(0.1f);
|
||||
Assert.AreEqual("Death", rt.CurrentStateName);
|
||||
}
|
||||
|
||||
// ── PatrolBetweenChases(CD 期巡逻)───────────────────────────────
|
||||
|
||||
// 驱动到 Chase 后模拟冲刺进行中(能力 running)。
|
||||
Ctx InCD_Chase(out AiRuntime rt)
|
||||
{
|
||||
var ctx = new Ctx();
|
||||
rt = new AiRuntime(GraphCD(), ctx);
|
||||
ctx.S.Chase = true; ctx.C.CanUse = true;
|
||||
rt.Tick(0.1f); // Idle → Chase,OnEnter 触发冲刺(Running="chase")
|
||||
Assert.AreEqual("Chase", rt.CurrentStateName);
|
||||
Assert.AreEqual("chase", ctx.C.Running);
|
||||
return ctx;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CD_Chase_Stays_WhileDashRunning()
|
||||
{
|
||||
var ctx = InCD_Chase(out var rt);
|
||||
ctx.S.Chase = false; ctx.S.Vision = false; // 冲刺是 committed 冲锋:感知丢失也不中途打断
|
||||
rt.Tick(0.1f);
|
||||
Assert.AreEqual("Chase", rt.CurrentStateName);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CD_Chase_ToPatrol_WhenDashDone()
|
||||
{
|
||||
var ctx = InCD_Chase(out var rt);
|
||||
ctx.C.Running = null; ctx.C.CanUse = false; // 冲刺打完,进入冷却
|
||||
rt.Tick(0.1f);
|
||||
Assert.AreEqual("Patrol", rt.CurrentStateName); // 冲完→CD 期巡逻
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CD_Patrol_Stays_DuringCooldown_NoOscillation()
|
||||
{
|
||||
var ctx = InCD_Chase(out var rt);
|
||||
ctx.C.Running = null; ctx.C.CanUse = false;
|
||||
rt.Tick(0.1f); // → Patrol
|
||||
Assert.AreEqual("Patrol", rt.CurrentStateName);
|
||||
// CD 中仍在追逐区:不能回追击,多帧保持巡逻(不抖)
|
||||
ctx.S.Chase = true; ctx.C.CanUse = false;
|
||||
for (int i = 0; i < 5; i++) rt.Tick(0.1f);
|
||||
Assert.AreEqual("Patrol", rt.CurrentStateName);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CD_Patrol_ToChase_WhenCooldownEnds_AndStillInChaseZone()
|
||||
{
|
||||
var ctx = InCD_Chase(out var rt);
|
||||
ctx.C.Running = null; ctx.C.CanUse = false;
|
||||
rt.Tick(0.1f); // → Patrol
|
||||
ctx.S.Chase = true; ctx.C.CanUse = true; // 冷却结束 + 仍在追逐区
|
||||
rt.Tick(0.1f);
|
||||
Assert.AreEqual("Chase", rt.CurrentStateName); // 再次冲刺
|
||||
Assert.AreEqual("chase", ctx.C.Running);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CD_Patrol_StaysHome_WhenPlayerLost()
|
||||
{
|
||||
var ctx = InCD_Chase(out var rt);
|
||||
ctx.C.Running = null; ctx.C.CanUse = false;
|
||||
rt.Tick(0.1f); // → Patrol
|
||||
// 玩家丢失(脱离追逐区):即便冷却结束也不回追击,留在巡逻(降级回家)
|
||||
ctx.S.Chase = false; ctx.S.Vision = false; ctx.C.CanUse = true;
|
||||
rt.Tick(0.1f);
|
||||
Assert.AreEqual("Patrol", rt.CurrentStateName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ namespace BaseGames.Enemies
|
||||
PatrolMode = LocomotionMode.Patrol,
|
||||
AlertMode = LocomotionMode.Face,
|
||||
ChaseAbilityId = "e001_chase",
|
||||
PatrolBetweenChases = true, // 冲刺=带冷却的冲锋:冲完→CD 期巡逻→冷却结束仍在追逐区→再冲
|
||||
// DeathAbilityId 留空:死亡演出走物理状态机(EnemyBase.PerformDeath)
|
||||
});
|
||||
}
|
||||
|
||||
@@ -30,6 +30,10 @@ namespace BaseGames.Enemies
|
||||
// 追击/死亡走能力(追击=接触伤害等战斗语义)
|
||||
public string ChaseAbilityId;
|
||||
public string DeathAbilityId;
|
||||
|
||||
// 追击=带冷却的冲刺能力时开启(opt-in):冲刺打完→巡逻(CD 期),冷却结束且仍在追逐区→再冲刺。
|
||||
// 由冷却门控 Patrol→Chase,避免"CD 期在追逐区每帧 Chase↔Patrol 来回抖"。
|
||||
public bool PatrolBetweenChases = false;
|
||||
}
|
||||
|
||||
public static void Add(BrainBuilder b, Config c)
|
||||
@@ -37,22 +41,36 @@ namespace BaseGames.Enemies
|
||||
b.Entry(c.Entry);
|
||||
b.Global().To(c.Death).OnEvent(AiSignal.Died); // 受击致死 → 死亡(全局最高优先)
|
||||
|
||||
// 进追击的条件:默认"在追逐区"。PatrolBetweenChases 时加冷却门——CD 中不回追击,
|
||||
// 使 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";
|
||||
|
||||
// 待机 / 巡逻:未发现态,升级转换相同(追逐优先、其次警觉)
|
||||
AddLocomotionState(b, c.Idle, c.IdleMode)
|
||||
.To(c.Chase).When(x => x.Sensor.InChaseZone(), "InChaseZone")
|
||||
.To(c.Chase).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(x => x.Sensor.InChaseZone(), "InChaseZone")
|
||||
.To(c.Chase).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(x => x.Sensor.InChaseZone(), "InChaseZone")
|
||||
.To(c.Chase).When(canChase, chaseLabel)
|
||||
.To(c.Rest).When(x => !x.Sensor.InVisionZone(), "leftVision");
|
||||
|
||||
// 追击:脱离全部感知(追逐区 且 视野区都不在)→R(永不回警觉)。
|
||||
AddAbilityState(b, c.Chase, c.ChaseAbilityId)
|
||||
.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);
|
||||
|
||||
Reference in New Issue
Block a user