下面是一个完整的TypeScript代码示例,展示了如何将状态机和行为树结合起来使用。状态机控制智能体的状态,行为树负责智能体的智能决策。在执行决策前,行为树会检查状态机的当前状态。
// 定义状态枚举
enum BodyState {
IDLE,
MOVING,
ATTACKING
}
// 状态机类,控制智能体的状态
class StateMachine {
private state: BodyState;
constructor() {
this.state = BodyState.IDLE;
}
getState(): BodyState {
return this.state;
}
setState(state: BodyState): void {
this.state = state;
}
}
// 智能体类,拥有一个状态机
class Entity {
private stateMachine: StateMachine;
constructor() {
this.stateMachine = new StateMachine();
}
getStateMachine(): StateMachine {
return this.stateMachine;
}
getState(): BodyState {
return this.stateMachine.getState();
}
// 智能体可以拥有更多的属性和方法
}
// 行为树的相关代码
enum Status {
SUCCESS,
FAILURE,
RUNNING
}
abstract class Node {
abstract execute(entity: Entity): Status;
}
abstract class CompositeNode extends Node {
protected children: Node[] = [];
addChild(child: Node): void {
this.children.push(child);
}
}
class SelectorNode extends CompositeNode {
execute(entity: Entity): Status {
for (const child of this.children) {
const status = child.execute(entity);
if (status === Status.SUCCESS) {
return Status.SUCCESS;
}
}
return Status.FAILURE;
}
}
class SequenceNode extends CompositeNode {
execute(entity: Entity): Status {
for (const child of this.children) {
const status = child.execute(entity);
if (status === Status.FAILURE) {
return Status.FAILURE;
}
}
return Status.SUCCESS;
}
}
abstract class ConditionNode extends Node {
abstract check(entity: Entity): boolean;
execute(entity: Entity): Status {
return this.check(entity) ? Status.SUCCESS : Status.FAILURE;
}
}
abstract class ActionNode extends Node {
abstract performAction(entity: Entity): boolean;
execute(entity: Entity): Status {
return this.performAction(entity) ? Status.SUCCESS : Status.FAILURE;
}
}
abstract class DecoratorNode extends Node {
protected child: Node;
constructor(child: Node) {
super();
this.child = child;
}
}
class InverterNode extends DecoratorNode {
execute(entity: Entity): Status {
const status = this.child.execute(entity);
if (status === Status.SUCCESS) {
return Status.FAILURE;
} else if (status === Status.FAILURE) {
return Status.SUCCESS;
} else {
return status;
}
}
}
class RepeaterNode extends DecoratorNode {
private maxRepeats: number;
private stopOnSuccess: boolean;
private stopOnFailure: boolean;
constructor(child: Node, maxRepeats: number, stopOnSuccess: boolean = false, stopOnFailure: boolean = false) {
super(child);
this.maxRepeats = maxRepeats;
this.stopOnSuccess = stopOnSuccess;
this.stopOnFailure = stopOnFailure;
}
execute(entity: Entity): Status {
let count = 0;
while (this.maxRepeats === -1 || count < this.maxRepeats) {
const status = this.child.execute(entity);
if (this.stopOnSuccess && status === Status.SUCCESS) {
return Status.SUCCESS;
}
if (this.stopOnFailure && status === Status.FAILURE) {
return Status.FAILURE;
}
count++;
}
return Status.SUCCESS;
}
}
// 定义行为树节点
class IsEnemyInRange extends ConditionNode {
check(entity: Entity): boolean {
// 判断敌人是否在范围内
return Math.random() < 0.5; // 50% 概率敌人在范围内
}
}
class ChaseEnemy extends ActionNode {
performAction(entity: Entity): boolean {
const stateMachine = entity.getStateMachine();
if (stateMachine.getState() === BodyState.IDLE || stateMachine.getState() === BodyState.MOVING) {
console.log("Chasing the enemy!");
stateMachine.setState(BodyState.MOVING);
return true; // 假设追击成功
}
return false;
}
}
class AttackEnemy extends ActionNode {
performAction(entity: Entity): boolean {
const stateMachine = entity.getStateMachine();
if (stateMachine.getState() === BodyState.MOVING) {
console.log("Attacking the enemy!");
stateMachine.setState(BodyState.ATTACKING);
return true; // 假设攻击成功
}
return false;
}
}
// 创建智能体实例
const entity = new Entity();
// 创建行为树
const root = new SelectorNode();
const sequence = new SequenceNode();
root.addChild(sequence);
const condition = new IsEnemyInRange();
sequence.addChild(condition);
const chase = new ChaseEnemy();
const repeater = new RepeaterNode(chase, 3); // 重复执行ChaseEnemy 3次
sequence.addChild(repeater);
const attack = new AttackEnemy();
sequence.addChild(attack);
// 执行为树
const status = root.execute(entity);
console.log(`Behavior tree execution status: ${Status[status]}`);
console.log(`Current body state: ${BodyState[entity.getState()]}`);
在这个示例中,智能体类拥有一个状态机,通过行为树来控制智能体的状态。行为树会根据状态机的当前状态来决策执行某个行为,同时更新状态机的状态。