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> @
115 lines
6.0 KiB
C#
115 lines
6.0 KiB
C#
using BaseGames.AI;
|
|
|
|
namespace BaseGames.Enemies
|
|
{
|
|
/// <summary>
|
|
/// 可复用的"待机/警觉/追击/巡逻"感知状态机(统一规则,见 spec《敌人感知↔状态》)。
|
|
/// 纯决策:AI 只决定"进哪个状态 / 转换条件 / 每状态的移动意图或攻击能力";
|
|
/// 移动/朝向/动画/伤害的实现全部在 EnemyLocomotion 或攻击能力里(AI 不 actuate)。
|
|
/// 规则:
|
|
/// 待机/巡逻: 在追逐区→追击(优先); 否则 有警觉且在视野→警觉; 否则保持。
|
|
/// 警觉: 在追逐区→追击; 否则 脱离视野→R; 否则保持警觉(朝向玩家)。
|
|
/// 追击: 脱离全部感知(追逐区且视野区都不在)→R(永不回警觉)。
|
|
/// 缺省自动降级:无警觉(HasAlert=false)→警觉分支不触发。
|
|
/// </summary>
|
|
public static class PerceptionStateMachine
|
|
{
|
|
public sealed class Config
|
|
{
|
|
public string Idle = "Idle";
|
|
public string Patrol = "Patrol";
|
|
public string Alert = "Alert";
|
|
public string Chase = "Chase";
|
|
public string Death = "Death";
|
|
public string Entry = "Idle"; // 初始态
|
|
public string Rest = "Patrol"; // R:降级(退出追击/警觉丢失)回到的静止态
|
|
// 待机/巡逻/警觉的移动意图(由 EnemyLocomotion 执行)
|
|
public LocomotionMode IdleMode = LocomotionMode.Idle;
|
|
public LocomotionMode PatrolMode = LocomotionMode.Patrol;
|
|
public LocomotionMode AlertMode = LocomotionMode.Face; // 停 + 朝向玩家
|
|
// 追击/死亡走能力(追击=接触伤害等战斗语义)
|
|
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)
|
|
{
|
|
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(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(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(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");
|
|
else
|
|
chase.To(c.Rest).When(x => !x.Sensor.InChaseZone() && !x.Sensor.InVisionZone(), "leftAllZones");
|
|
|
|
// 死亡:终态(无转出);死亡演出走物理状态机
|
|
AddAbilityState(b, c.Death, c.DeathAbilityId);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 一个"由 EnemyLocomotion 驱动"的状态:进入/每帧声明移动意图,离开时停。
|
|
/// Face 模式每帧朝向最后已知玩家位置(Sensor.LastKnown 由 EnemyBrainContext.Refresh 维护)。
|
|
/// </summary>
|
|
static BrainBuilder.StateBuilder AddLocomotionState(BrainBuilder b, string state, LocomotionMode mode)
|
|
{
|
|
return b.State(state)
|
|
.OnEnter(x => ApplyLocomotion(x, mode))
|
|
.Tick(x => ApplyLocomotion(x, mode))
|
|
.OnExit(x => x.Locomotion.Stop());
|
|
}
|
|
|
|
static void ApplyLocomotion(IAiContext x, LocomotionMode mode)
|
|
{
|
|
if (mode == LocomotionMode.Face) x.Locomotion.Face(x.Sensor.LastKnown);
|
|
else x.Locomotion.SetMode(mode);
|
|
}
|
|
|
|
/// <summary>一个"由攻击能力驱动"的状态:进入/每帧确保能力运行(受击打断后重触发),离开中断。</summary>
|
|
static BrainBuilder.StateBuilder AddAbilityState(BrainBuilder b, string state, string abilityId)
|
|
{
|
|
return b.State(state)
|
|
.OnEnter(x => EnsureAbility(x, abilityId))
|
|
.Tick(x => EnsureAbility(x, abilityId))
|
|
.OnExit(x => x.Combat.InterruptAbilities());
|
|
}
|
|
|
|
static void EnsureAbility(IAiContext x, string abilityId)
|
|
{
|
|
if (!string.IsNullOrEmpty(abilityId) && !x.Combat.IsAbilityRunning(abilityId))
|
|
x.Combat.UseAbility(abilityId);
|
|
}
|
|
|
|
static bool HasAlert(IAiContext x) => x is IEnemyActor a && a.HasAlertState;
|
|
}
|
|
}
|