状态机和行为树的搭配使用

2024-06-20 12:52
文章标签 使用 行为 搭配 状态机

本文主要是介绍状态机和行为树的搭配使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

下面是一个完整的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()]}`);

在这个示例中,智能体类拥有一个状态机,通过行为树来控制智能体的状态。行为树会根据状态机的当前状态来决策执行某个行为,同时更新状态机的状态。 

这篇关于状态机和行为树的搭配使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1078192

相关文章

如何使用celery进行异步处理和定时任务(django)

《如何使用celery进行异步处理和定时任务(django)》文章介绍了Celery的基本概念、安装方法、如何使用Celery进行异步任务处理以及如何设置定时任务,通过Celery,可以在Web应用中... 目录一、celery的作用二、安装celery三、使用celery 异步执行任务四、使用celery

使用Python绘制蛇年春节祝福艺术图

《使用Python绘制蛇年春节祝福艺术图》:本文主要介绍如何使用Python的Matplotlib库绘制一幅富有创意的“蛇年有福”艺术图,这幅图结合了数字,蛇形,花朵等装饰,需要的可以参考下... 目录1. 绘图的基本概念2. 准备工作3. 实现代码解析3.1 设置绘图画布3.2 绘制数字“2025”3.3

Jsoncpp的安装与使用方式

《Jsoncpp的安装与使用方式》JsonCpp是一个用于解析和生成JSON数据的C++库,它支持解析JSON文件或字符串到C++对象,以及将C++对象序列化回JSON格式,安装JsonCpp可以通过... 目录安装jsoncppJsoncpp的使用Value类构造函数检测保存的数据类型提取数据对json数

python使用watchdog实现文件资源监控

《python使用watchdog实现文件资源监控》watchdog支持跨平台文件资源监控,可以检测指定文件夹下文件及文件夹变动,下面我们来看看Python如何使用watchdog实现文件资源监控吧... python文件监控库watchdogs简介随着Python在各种应用领域中的广泛使用,其生态环境也

Python中构建终端应用界面利器Blessed模块的使用

《Python中构建终端应用界面利器Blessed模块的使用》Blessed库作为一个轻量级且功能强大的解决方案,开始在开发者中赢得口碑,今天,我们就一起来探索一下它是如何让终端UI开发变得轻松而高... 目录一、安装与配置:简单、快速、无障碍二、基本功能:从彩色文本到动态交互1. 显示基本内容2. 创建链

springboot整合 xxl-job及使用步骤

《springboot整合xxl-job及使用步骤》XXL-JOB是一个分布式任务调度平台,用于解决分布式系统中的任务调度和管理问题,文章详细介绍了XXL-JOB的架构,包括调度中心、执行器和Web... 目录一、xxl-job是什么二、使用步骤1. 下载并运行管理端代码2. 访问管理页面,确认是否启动成功

使用Nginx来共享文件的详细教程

《使用Nginx来共享文件的详细教程》有时我们想共享电脑上的某些文件,一个比较方便的做法是,开一个HTTP服务,指向文件所在的目录,这次我们用nginx来实现这个需求,本文将通过代码示例一步步教你使用... 在本教程中,我们将向您展示如何使用开源 Web 服务器 Nginx 设置文件共享服务器步骤 0 —

Java中switch-case结构的使用方法举例详解

《Java中switch-case结构的使用方法举例详解》:本文主要介绍Java中switch-case结构使用的相关资料,switch-case结构是Java中处理多个分支条件的一种有效方式,它... 目录前言一、switch-case结构的基本语法二、使用示例三、注意事项四、总结前言对于Java初学者

Golang使用minio替代文件系统的实战教程

《Golang使用minio替代文件系统的实战教程》本文讨论项目开发中直接文件系统的限制或不足,接着介绍Minio对象存储的优势,同时给出Golang的实际示例代码,包括初始化客户端、读取minio对... 目录文件系统 vs Minio文件系统不足:对象存储:miniogolang连接Minio配置Min

使用Python绘制可爱的招财猫

《使用Python绘制可爱的招财猫》招财猫,也被称为“幸运猫”,是一种象征财富和好运的吉祥物,经常出现在亚洲文化的商店、餐厅和家庭中,今天,我将带你用Python和matplotlib库从零开始绘制一... 目录1. 为什么选择用 python 绘制?2. 绘图的基本概念3. 实现代码解析3.1 设置绘图画