AI 决策层(E001CaoZhiAi + PerceptionStateMachine)彻底移除所有 actuation: 不再有 Mover.Stop/FacePlayer/UseChaseSpeed/WalkRandom、也不再 SetPhase。 每个 AI 状态只声明触发哪个能力 id,行为(移动/朝向/停/速度/动画/伤害/ 阶段声明)全部下沉到对应能力里。 - 新增 IdleAbility/PatrolAbility/AlertAbility:待机静止、巡逻游走、 警觉朝向玩家,各自 SetAiPhase 驱动阶段动画。 - ContactChaseAbility 追击进入时自设 RunSpeed + Chase 阶段(原在 AI)。 - PerceptionStateMachine 改为 AddAbilityState:进入/每帧 EnsureAbility (被打断/受击后自动重触发)、离开 InterruptAbilities;转换规则不变。 - IEnemyActor/EnemyBrainContext 移除已无用的 SetPhase,保留 HasAlertState。 - 新增 SO:ABL_E001_Idle(e001_idle)、ABL_E001_Patrol(e001_patrol)。 - 脚手架 PlaceE001 与 ENM_CaoZhi 预制体同步为四能力结构;TestRoomA 实例替换为规范预制体实例。 PlayMode 验证:Chase→ContactChaseAbility、脱离感知→Move_Patrol→ PatrolAbility,阶段由能力设置,无报错。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
81 lines
4.2 KiB
C#
81 lines
4.2 KiB
C#
using BaseGames.AI;
|
|
|
|
namespace BaseGames.Enemies
|
|
{
|
|
/// <summary>
|
|
/// 可复用的"待机/警觉/追击/巡逻"感知状态机(统一规则,见 spec《敌人感知↔状态》)。
|
|
/// 纯决策:AI 只决定"进哪个状态 / 转换条件 / 每状态触发哪个能力";
|
|
/// 每个状态的行为(移动/朝向/动画/伤害/阶段声明)全部封装在对应能力里(AI 不做任何 actuation)。
|
|
/// 规则:
|
|
/// 待机/巡逻: 在追逐区→追击(优先); 否则 有警觉且在视野→警觉; 否则保持。
|
|
/// 警觉: 在追逐区→追击; 否则 脱离视野→R; 否则保持警觉。
|
|
/// 追击: 脱离全部感知(追逐区且视野区都不在)→R(永不回警觉)。
|
|
/// 缺省自动降级:无警觉(HasAlert=false)→警觉分支不触发;某状态无能力 id→该状态不触发能力。
|
|
/// </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:降级(退出追击/警觉丢失)回到的静止态
|
|
// 每状态对应的行为能力 id(AI 进入状态即触发、离开即中断)
|
|
public string IdleAbilityId;
|
|
public string PatrolAbilityId;
|
|
public string AlertAbilityId;
|
|
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); // 受击致死 → 死亡(全局最高优先)
|
|
|
|
// 待机 / 巡逻:未发现态,升级转换相同(追逐优先、其次警觉)
|
|
AddAbilityState(b, c.Idle, c.IdleAbilityId)
|
|
.To(c.Chase).When(x => x.Sensor.InChaseZone(), "InChaseZone")
|
|
.To(c.Alert).When(x => HasAlert(x) && x.Sensor.InVisionZone(), "InVision+HasAlert");
|
|
AddAbilityState(b, c.Patrol, c.PatrolAbilityId)
|
|
.To(c.Chase).When(x => x.Sensor.InChaseZone(), "InChaseZone")
|
|
.To(c.Alert).When(x => HasAlert(x) && x.Sensor.InVisionZone(), "InVision+HasAlert");
|
|
|
|
// 警觉(仅升级路径出现):进追逐区→追击;脱离视野→R;在视野内保持
|
|
AddAbilityState(b, c.Alert, c.AlertAbilityId)
|
|
.To(c.Chase).When(x => x.Sensor.InChaseZone(), "InChaseZone")
|
|
.To(c.Rest).When(x => !x.Sensor.InVisionZone(), "leftVision");
|
|
|
|
// 追击:脱离全部感知(追逐区 且 视野区都不在)→R(永不回警觉)。
|
|
// 维持用"追逐区 OR 视野区"任一:视野(los)可能被墙挡漏检,仅靠视野会在近身追逐区内反复进出追击(抖动)。
|
|
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>
|
|
/// 一个"由能力驱动行为"的状态:进入即确保能力运行;每帧确保能力运行(被中断/受击后自动重触发);离开即中断。
|
|
/// </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;
|
|
}
|
|
}
|