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

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

相关文章

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(

Linux脚本(shell)的使用方式

《Linux脚本(shell)的使用方式》:本文主要介绍Linux脚本(shell)的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录概述语法详解数学运算表达式Shell变量变量分类环境变量Shell内部变量自定义变量:定义、赋值自定义变量:引用、修改、删

Java使用HttpClient实现图片下载与本地保存功能

《Java使用HttpClient实现图片下载与本地保存功能》在当今数字化时代,网络资源的获取与处理已成为软件开发中的常见需求,其中,图片作为网络上最常见的资源之一,其下载与保存功能在许多应用场景中都... 目录引言一、Apache HttpClient简介二、技术栈与环境准备三、实现图片下载与保存功能1.

Python中使用uv创建环境及原理举例详解

《Python中使用uv创建环境及原理举例详解》uv是Astral团队开发的高性能Python工具,整合包管理、虚拟环境、Python版本控制等功能,:本文主要介绍Python中使用uv创建环境及... 目录一、uv工具简介核心特点:二、安装uv1. 通过pip安装2. 通过脚本安装验证安装:配置镜像源(可

LiteFlow轻量级工作流引擎使用示例详解

《LiteFlow轻量级工作流引擎使用示例详解》:本文主要介绍LiteFlow是一个灵活、简洁且轻量的工作流引擎,适合用于中小型项目和微服务架构中的流程编排,本文给大家介绍LiteFlow轻量级工... 目录1. LiteFlow 主要特点2. 工作流定义方式3. LiteFlow 流程示例4. LiteF

使用Python开发一个现代化屏幕取色器

《使用Python开发一个现代化屏幕取色器》在UI设计、网页开发等场景中,颜色拾取是高频需求,:本文主要介绍如何使用Python开发一个现代化屏幕取色器,有需要的小伙伴可以参考一下... 目录一、项目概述二、核心功能解析2.1 实时颜色追踪2.2 智能颜色显示三、效果展示四、实现步骤详解4.1 环境配置4.

使用jenv工具管理多个JDK版本的方法步骤

《使用jenv工具管理多个JDK版本的方法步骤》jenv是一个开源的Java环境管理工具,旨在帮助开发者在同一台机器上轻松管理和切换多个Java版本,:本文主要介绍使用jenv工具管理多个JD... 目录一、jenv到底是干啥的?二、jenv的核心功能(一)管理多个Java版本(二)支持插件扩展(三)环境隔

SQL中JOIN操作的条件使用总结与实践

《SQL中JOIN操作的条件使用总结与实践》在SQL查询中,JOIN操作是多表关联的核心工具,本文将从原理,场景和最佳实践三个方面总结JOIN条件的使用规则,希望可以帮助开发者精准控制查询逻辑... 目录一、ON与WHERE的本质区别二、场景化条件使用规则三、最佳实践建议1.优先使用ON条件2.WHERE用

Java中Map.Entry()含义及方法使用代码

《Java中Map.Entry()含义及方法使用代码》:本文主要介绍Java中Map.Entry()含义及方法使用的相关资料,Map.Entry是Java中Map的静态内部接口,用于表示键值对,其... 目录前言 Map.Entry作用核心方法常见使用场景1. 遍历 Map 的所有键值对2. 直接修改 Ma