refactor(enemy): 退役 PerceptionStateMachine,E001 切换到两层模块组装
This commit is contained in:
@@ -3,35 +3,17 @@ using BaseGames.AI;
|
||||
namespace BaseGames.Enemies
|
||||
{
|
||||
/// <summary>
|
||||
/// E001(草蛭)AI —— 纯决策层,复用 PerceptionStateMachine 的"待机/警觉/追击/巡逻"统一规则。
|
||||
/// 只声明差异:每个状态触发哪个能力(待机=伪装静止、巡逻=游走、警觉=张口、追击=接触追击)。
|
||||
/// AI 只决策(进哪个状态/转换条件/触发哪个能力);移动/朝向/停/速度/动画/伤害/阶段声明全部在能力里实现。
|
||||
/// 依据 Docs/Game/Design/小怪设计-程序开发文档-01.md。
|
||||
/// E001(草蛭)AI —— 纯决策层,组装两层模块。
|
||||
/// 出生伪装静止(伪装成石头),一旦交战过就只回巡逻;追击=带冷却的接触冲锋,
|
||||
/// 起手即锁定、打完回巡逻走冷却;死亡演出走物理状态机(EnemyBase.PerformDeath)。
|
||||
/// AI 只决策;移动 / 朝向 / 速度 / 动画 / 伤害全部在能力与 EnemyLocomotion 里实现。
|
||||
/// 依据 Docs/Game/敌人/小怪/E001_草蛭.md。
|
||||
/// </summary>
|
||||
[AiDefinition("E001")]
|
||||
public sealed class E001CaoZhiAi : AiScript
|
||||
{
|
||||
protected override void Build(BrainBuilder b)
|
||||
{
|
||||
PerceptionStateMachine.Add(b, new PerceptionStateMachine.Config
|
||||
{
|
||||
Idle = "Idle_Disguise",
|
||||
Patrol = "Move_Patrol",
|
||||
Alert = "Alert",
|
||||
Chase = "Chase",
|
||||
Death = "Death",
|
||||
// [临时调试] 出生即巡逻,用于真机验证"中心悬空导致导航寻路失效"是否真实存在。
|
||||
// 验证完成后必须改回 "Idle_Disguise"(伪装静止)。
|
||||
Entry = "Move_Patrol",
|
||||
Rest = "Move_Patrol",
|
||||
|
||||
IdleMode = LocomotionMode.Idle,
|
||||
PatrolMode = LocomotionMode.Patrol,
|
||||
AlertMode = LocomotionMode.Face,
|
||||
ChaseAbilityId = "e001_chase",
|
||||
PatrolBetweenChases = true, // 冲刺=带冷却的冲锋:冲完→CD 期巡逻→冷却结束仍在追逐区→再冲
|
||||
// DeathAbilityId 留空:死亡演出走物理状态机(EnemyBase.PerformDeath)
|
||||
});
|
||||
}
|
||||
protected override void Build(BrainBuilder b) => PerceptionSkeleton.Add(b,
|
||||
unaware: new DisguiseThenPatrol(),
|
||||
engagement: new RushEngagement("e001_chase", RushExit.Committed));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,156 +0,0 @@
|
||||
using BaseGames.AI;
|
||||
|
||||
namespace BaseGames.Enemies
|
||||
{
|
||||
/// <summary>
|
||||
/// 可复用的"待机/警觉/追击/巡逻"感知状态机(统一规则,见 spec《敌人感知↔状态》)。
|
||||
/// 纯决策:AI 只决定"进哪个状态 / 转换条件 / 每状态的移动意图或攻击能力";
|
||||
/// 移动/朝向/动画/伤害的实现全部在 EnemyLocomotion 或攻击能力里(AI 不 actuate)。
|
||||
/// 规则:
|
||||
/// 待机/巡逻: 在追逐区→追击(优先); 否则 有警觉且在视野→警觉; 否则保持。
|
||||
/// 警觉: 在追逐区→追击; 否则 脱离视野→R; 否则保持警觉(朝向玩家)。
|
||||
/// 追击: 脱离全部感知(追逐区且视野区都不在)→R(永不回警觉)。
|
||||
/// 缺省自动降级:无警觉(HasAlert=false)→警觉分支不触发。
|
||||
/// 交战风格(Engagement):默认 ChaseAbility=单一冲锋能力节点;
|
||||
/// ApproachAttack=把追击换成 Approach(寻路逼近)↔Attack(到射程选招攻击、攻击时停逼近) 两态子机,
|
||||
/// 打完回逼近重判、脱离全部感知区→R。
|
||||
/// </summary>
|
||||
public static class PerceptionStateMachine
|
||||
{
|
||||
/// <summary>交战风格:单一冲锋能力,或"寻路逼近 + 选招攻击"两态子机。</summary>
|
||||
public enum EngagementStyle { ChaseAbility, ApproachAttack }
|
||||
|
||||
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;
|
||||
|
||||
// 交战风格(默认冲锋能力,保持现状);ApproachAttack 时启用 Approach↔Attack 两态子机。
|
||||
public EngagementStyle Engagement = EngagementStyle.ChaseAbility;
|
||||
public string ApproachState = "Approach";
|
||||
public string AttackState = "Attack";
|
||||
}
|
||||
|
||||
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;
|
||||
string chaseLabel;
|
||||
if (c.Engagement == EngagementStyle.ChaseAbility && c.PatrolBetweenChases)
|
||||
{
|
||||
canChase = x => x.Sensor.InChaseZone() && x.Combat.CanUseAbility(chaseId);
|
||||
chaseLabel = "InChaseZone+offCD";
|
||||
}
|
||||
else
|
||||
{
|
||||
canChase = x => x.Sensor.InChaseZone();
|
||||
chaseLabel = "InChaseZone";
|
||||
}
|
||||
|
||||
// 交战入口态:冲锋风格=Chase;逼近风格=Approach
|
||||
string combatEntry = c.Engagement == EngagementStyle.ApproachAttack ? c.ApproachState : c.Chase;
|
||||
|
||||
// 待机 / 巡逻:未发现态,升级转换相同(追逐优先、其次警觉)
|
||||
AddLocomotionState(b, c.Idle, c.IdleMode)
|
||||
.To(combatEntry).When(canChase, chaseLabel)
|
||||
.To(c.Alert).When(x => HasAlert(x) && x.Sensor.InVisionZone(), "InVision+HasAlert");
|
||||
AddLocomotionState(b, c.Patrol, c.PatrolMode)
|
||||
.To(combatEntry).When(canChase, chaseLabel)
|
||||
.To(c.Alert).When(x => HasAlert(x) && x.Sensor.InVisionZone(), "InVision+HasAlert");
|
||||
|
||||
// 警觉(仅升级路径出现):进追逐区→追击;脱离视野→R;在视野内保持(朝向玩家)
|
||||
AddLocomotionState(b, c.Alert, c.AlertMode)
|
||||
.To(combatEntry).When(canChase, chaseLabel)
|
||||
.To(c.Rest).When(x => !x.Sensor.InVisionZone(), "leftVision");
|
||||
|
||||
if (c.Engagement == EngagementStyle.ApproachAttack)
|
||||
{
|
||||
// Approach:寻路逼近;有招够得着→Attack;脱离全部感知→R
|
||||
b.State(c.ApproachState)
|
||||
.OnEnter(x => x.Locomotion.Pursue(x.Sensor.LastKnown))
|
||||
.Tick(x => x.Locomotion.Pursue(x.Sensor.LastKnown))
|
||||
.OnExit(x => x.Locomotion.Stop())
|
||||
.To(c.AttackState).When(x => x.Combat.HasEligibleAttack(), "attackInRange")
|
||||
.To(c.Rest).When(x => !x.Sensor.InChaseZone() && !x.Sensor.InVisionZone(), "leftAllZones");
|
||||
|
||||
// Attack:停逼近 + 选招攻击;打完回 Approach;脱离感知→R
|
||||
b.State(c.AttackState)
|
||||
.OnEnter(x => { x.Locomotion.Stop(); x.Combat.UseBestAttack(); })
|
||||
.OnExit(x => x.Combat.InterruptAbilities())
|
||||
.To(c.ApproachState).When(x => !x.Combat.IsAbilityRunning(), "attackDone")
|
||||
.To(c.Rest).When(x => !x.Sensor.InChaseZone() && !x.Sensor.InVisionZone(), "leftAllZones");
|
||||
}
|
||||
else
|
||||
{
|
||||
// 追击退出:
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d6bbbd575378d614da31905f1178dbf8
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user