Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
97 lines
4.9 KiB
C#
97 lines
4.9 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;
|
|
}
|
|
|
|
public static void Add(BrainBuilder b, Config c)
|
|
{
|
|
b.Entry(c.Entry);
|
|
b.Global().To(c.Death).OnEvent(AiSignal.Died); // 受击致死 → 死亡(全局最高优先)
|
|
|
|
// 待机 / 巡逻:未发现态,升级转换相同(追逐优先、其次警觉)
|
|
AddLocomotionState(b, c.Idle, c.IdleMode)
|
|
.To(c.Chase).When(x => x.Sensor.InChaseZone(), "InChaseZone")
|
|
.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.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.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");
|
|
|
|
// 死亡:终态(无转出);死亡演出走物理状态机
|
|
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;
|
|
}
|
|
}
|