mirror of
https://github.com/gongxh0901/kunpocc-behaviortree.git
synced 2025-12-27 00:58:18 +00:00
Compare commits
21 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
80a2438f6e | ||
|
|
50e29feeb8 | ||
|
|
f60bf869a1 | ||
|
|
eb6934ce6a | ||
|
|
e50bb3ba34 | ||
|
|
6b9e7dbdda | ||
|
|
b20cf3fd41 | ||
|
|
249022a300 | ||
|
|
9a3e7028d2 | ||
|
|
63d9855658 | ||
|
|
b1107805d0 | ||
|
|
b582a5d1da | ||
|
|
d7fc1e49ae | ||
|
|
1c5f9de608 | ||
|
|
3071611cd0 | ||
|
|
6ae0b4200a | ||
|
|
6d6162031a | ||
|
|
10ca8fd2a8 | ||
|
|
2ab47b2a7b | ||
|
|
7ed015c6bf | ||
|
|
e9a0a15035 |
356
README.md
356
README.md
@@ -1,187 +1,237 @@
|
||||
# 行为树
|
||||
|
||||
一个轻量级、高性能的 TypeScript 行为树库,专为游戏AI和决策系统设计。
|
||||
[](https://badge.fury.io/js/kunpocc-behaviortree)
|
||||
[](https://opensource.org/licenses/ISC)
|
||||
|
||||
一个简洁、高效的 TypeScript 行为树库。
|
||||
该库无任何依赖,理论上可用于所有js、ts项目。
|
||||
作者主要使用此库作为游戏AI的实现。
|
||||
|
||||
## 什么是行为树
|
||||
行为树(Behavior Tree)是一种用于描述和控制复杂行为逻辑的数据结构,最初在游戏AI领域大放异彩,现在已经广泛应用于机器人控制、自动化系统等领域。它简单、直观、可扩展,是真正解决实际问题的好工具。
|
||||
|
||||
行为树本质上是一个**有向无环图**,用树状结构来组织和执行行为逻辑。每个节点代表一个行为或决策,通过父子关系形成层次化的控制流。
|
||||
|
||||
## 特性
|
||||
|
||||
- 🚀 **高性能**: 优化的节点执行机制,最小化运行时开销
|
||||
- 🎯 **类型安全**: 完整的 TypeScript 支持,严格的类型检查
|
||||
- 🧩 **模块化**: 清晰的节点类型体系,易于扩展
|
||||
- 🔄 **记忆节点**: 支持记忆型组合节点,优化复杂决策流程
|
||||
- 📦 **零依赖**: 不依赖任何第三方库
|
||||
- 🎮 **游戏优化**: 专为游戏场景优化的黑板系统和状态管理
|
||||
- 🔧 **类型安全**: 完整 TypeScript 支持
|
||||
- 📦 **零依赖**: 纯净实现,无第三方依赖
|
||||
- 🔄 **状态管理**: 分层黑板系统,数据隔离清晰
|
||||
|
||||
## 安装
|
||||
## 快速开始
|
||||
|
||||
#### 安装
|
||||
|
||||
```bash
|
||||
npm install kunpocc-behaviortree
|
||||
```
|
||||
|
||||
## 快速开始
|
||||
#### 内置demo (基于cocos creator 3.8.6)
|
||||
|
||||
```typescript
|
||||
import {
|
||||
BehaviorTree,
|
||||
Action,
|
||||
Condition,
|
||||
Sequence,
|
||||
Selector,
|
||||
Status
|
||||
} from 'kunpocc-behaviortree';
|
||||
项目根目录下的 `bt-demo`文件夹
|
||||
|
||||
// 定义AI角色
|
||||
interface Character {
|
||||
health: number;
|
||||
hasWeapon: boolean;
|
||||
}
|
||||
|
||||
const character: Character = {
|
||||
health: 80,
|
||||
hasWeapon: true
|
||||
};
|
||||
## GUI编辑器
|
||||
|
||||
// 创建条件节点
|
||||
const isHealthLow = new Condition((char: Character) => char.health < 30);
|
||||
const hasWeapon = new Condition((char: Character) => char.hasWeapon);
|
||||
查看详情: [GUI编辑器](./docs/USED.md)
|
||||
|
||||
// 创建行动节点
|
||||
const flee = new Action(() => {
|
||||
console.log("逃跑!");
|
||||
return Status.SUCCESS;
|
||||
});
|
||||

|
||||
|
||||
const attack = new Action(() => {
|
||||
console.log("攻击!");
|
||||
return Status.SUCCESS;
|
||||
});
|
||||
## 核心概念
|
||||
|
||||
// 构建行为树:生命值低时逃跑,否则攻击
|
||||
const tree = new BehaviorTree(character,
|
||||
new Selector(
|
||||
new Sequence(isHealthLow, flee),
|
||||
new Sequence(hasWeapon, attack)
|
||||
)
|
||||
);
|
||||
|
||||
// 执行行为树
|
||||
tree.tick(); // 输出: "攻击!"
|
||||
```
|
||||
|
||||
#### 基本概念
|
||||
|
||||
1. 节点状态
|
||||
#### 状态类型
|
||||
```typescript
|
||||
enum Status {
|
||||
SUCCESS, // 成功
|
||||
FAILURE, // 失败
|
||||
RUNNING // 运行中
|
||||
SUCCESS, // 成功
|
||||
FAILURE, // 失败
|
||||
RUNNING // 运行中
|
||||
}
|
||||
```
|
||||
|
||||
2. 节点类型
|
||||
- **动作节点 (Action)**:执行具体行为的叶子节点
|
||||
- **组合节点 (Composite)**:控制子节点执行顺序的节点
|
||||
- **条件节点 (Condition)**:判断条件的节点
|
||||
- **装饰节点 (Decorator)**:修饰其他节点行为的节点
|
||||
#### 节点类型
|
||||
- **组合节点**: 包含多个子节点 (Composite)
|
||||
- **装饰节点**: 有且只有一个子节点(Decorator)
|
||||
- **叶子节点**: 不能包含子节点 (LeafNode)
|
||||
- **条件节点**: 特殊的叶子节点 (Condition)
|
||||
|
||||
#### 常用节点
|
||||
|
||||
1. 组合节点
|
||||
## 装饰器
|
||||
|
||||
```typescript
|
||||
// 顺序节点:按顺序执行所有子节点,直到遇到失败或运行中的节点
|
||||
new Sequence(childNode1, childNode2, childNode3);
|
||||
|
||||
// 选择节点:选择第一个成功或运行中的子节点
|
||||
new Selector(childNode1, childNode2, childNode3);
|
||||
|
||||
// 并行节点:同时执行所有子节点,全部成功才成功
|
||||
new Parallel(childNode1, childNode2, childNode3);
|
||||
|
||||
// 并行任一成功节点:同时执行所有子节点,任一成功即成功
|
||||
new ParallelAnySuccess(childNode1, childNode2, childNode3);
|
||||
|
||||
// 记忆顺序节点:记住上次执行的位置
|
||||
new MemSequence(childNode1, childNode2, childNode3);
|
||||
|
||||
// 记忆选择节点:记住上次执行的位置
|
||||
new MemSelector(childNode1, childNode2, childNode3);
|
||||
|
||||
// 随机选择节点:随机选择一个子节点执行
|
||||
new RandomSelector(childNode1, childNode2, childNode3);
|
||||
```
|
||||
> **自行实现的节点,通过装饰器把数据暴露给行为树编辑器**
|
||||
|
||||
2. 动作节点
|
||||
##### ClassAction - 行为节点装饰器
|
||||
|
||||
```typescript
|
||||
// 行动节点 - 返回指定状态
|
||||
new Action(() => {
|
||||
console.log("执行动作");
|
||||
return Status.SUCCESS; // 或 Status.FAILURE, Status.RUNNING
|
||||
});
|
||||
|
||||
// 条件节点 - 检查条件返回成功或失败
|
||||
new Condition((subject) => {
|
||||
return subject.health > 50; // 返回 true 表示成功,false 表示失败
|
||||
});
|
||||
|
||||
// 等待节点
|
||||
new WaitTime(2); // 等待2秒
|
||||
new WaitTicks(5); // 等待5个tick
|
||||
```
|
||||
##### ClassCondition - 条件节点装饰器
|
||||
|
||||
3. 装饰节点
|
||||
##### ClassComposite - 组合节点装饰器
|
||||
|
||||
```typescript
|
||||
// 反转节点 - 反转子节点的成功/失败状态
|
||||
new Inverter(childNode);
|
||||
|
||||
// 重复节点 - 重复执行子节点指定次数
|
||||
new Repeat(childNode, 3);
|
||||
|
||||
// 重复直到失败 - 重复执行直到子节点失败
|
||||
new RepeatUntilFailure(childNode, 5);
|
||||
|
||||
// 重复直到成功 - 重复执行直到子节点成功
|
||||
new RepeatUntilSuccess(childNode, 5);
|
||||
|
||||
// 时间限制节点 - 限制子节点执行时间
|
||||
new LimitTime(childNode, 5); // 5秒
|
||||
|
||||
// 次数限制节点 - 限制子节点执行次数
|
||||
new LimitTimes(childNode, 3);
|
||||
```
|
||||
##### ClassDecorator - 装饰节点装饰器
|
||||
|
||||
4. 使用黑板共享数据
|
||||
##### prop - 属性装饰器
|
||||
|
||||
```typescript
|
||||
// 在节点中使用黑板
|
||||
class CustomAction extends BaseNode {
|
||||
tick<T>(tree: BehaviorTree<T>): Status {
|
||||
// 获取数据 - 使用节点实例作为命名空间
|
||||
const data = tree.blackboard.get<string>("key", this);
|
||||
|
||||
// 设置数据 - 使用节点实例作为命名空间
|
||||
tree.blackboard.set("key", "value", this);
|
||||
|
||||
return Status.SUCCESS;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
#### 注意事项
|
||||
|
||||
1. 节点状态说明:
|
||||
- `SUCCESS`:节点执行成功
|
||||
- `FAILURE`:节点执行失败
|
||||
- `RUNNING`:节点正在执行中
|
||||
2. 组合节点特性:
|
||||
- `Sequence`:所有子节点返回 SUCCESS 才返回 SUCCESS
|
||||
- `Selector`:任一子节点返回 SUCCESS 就返回 SUCCESS
|
||||
- `Parallel`:并行执行所有子节点
|
||||
- `MemSequence/MemSelector`:会记住上次执行位置
|
||||
3. 性能优化:
|
||||
- 使用黑板共享数据,避免重复计算
|
||||
- 合理使用记忆节点,减少重复执行
|
||||
- 控制行为树的深度,避免过于复杂
|
||||
## 内置节点
|
||||
|
||||
### 组合节点 (Composite)
|
||||
|
||||
##### Selector - 选择节点
|
||||
* 选择第一个成功的子节点
|
||||
|
||||
##### Sequence - 顺序节点
|
||||
* 按顺序执行子节点,执行过程中子节点返回非SUCCESS,则返回子节点状态,全部成功返回SUCCESS
|
||||
|
||||
##### Parallel - 并行节点
|
||||
* 依次执行所有子节点(从左到右),全部成功才成功
|
||||
* 注意:这里的"并行"是逻辑概念,实际是顺序执行
|
||||
|
||||
##### RandomSelector - 随机选择节点
|
||||
* 随机选择一个子节点执行
|
||||
|
||||
##### ParallelAnySuccess - 并行任一成功
|
||||
* 依次执行所有子节点(从左到右),任一成功就成功
|
||||
|
||||
|
||||
|
||||
### 装饰节点 (Decorator)
|
||||
|
||||
##### ConditionDecorator - 条件装饰节点
|
||||
|
||||
* 子类需实现
|
||||
|
||||
```typescript
|
||||
/**
|
||||
* 判断是否满足条件
|
||||
* @returns 是否满足条件
|
||||
*/
|
||||
protected abstract isEligible(): boolean;
|
||||
```
|
||||
|
||||
##### Inverter - 反转节点
|
||||
* 反转子节点的成功/失败状态
|
||||
|
||||
##### LimitTime - 时间限制
|
||||
|
||||
* 规定时间内, 向父节点返回子节点的结果,超时后返回失败
|
||||
|
||||
##### LimitTicks - 次数限制
|
||||
|
||||
* 执行次数(子节点非RUNNNG状态)内,向父节点返回子节点的结果,超过次数后返回失败
|
||||
|
||||
##### Repeat - 重复节点
|
||||
* 重复执行指定次数
|
||||
|
||||
##### RepeatUntilSuccess - 重复直到成功
|
||||
|
||||
* 设置最大重试次数
|
||||
|
||||
##### RepeatUntilFailure - 重复直到失败
|
||||
|
||||
* 设置最大重试次数
|
||||
|
||||
##### WeightDecorator - 权重装饰节点
|
||||
|
||||
* 用于随机选择节点的子节点的按权重随机
|
||||
|
||||
|
||||
|
||||
### 叶子节点 (LeafNode)
|
||||
|
||||
##### LeafNode - 叶子节点基类
|
||||
|
||||
##### WaitTicks - 次数等待节点
|
||||
|
||||
##### WaitTime - 时间等待节点
|
||||
|
||||
|
||||
|
||||
### 条件节点 (Condition)
|
||||
|
||||
##### Condition - 条件节点基类
|
||||
|
||||
* 特殊的叶子节点,子类需实现
|
||||
|
||||
```typescript
|
||||
/**
|
||||
* 判断是否满足条件
|
||||
* @returns 是否满足条件
|
||||
*/
|
||||
protected abstract isEligible(): boolean;
|
||||
```
|
||||
|
||||
|
||||
|
||||
## 黑板系统
|
||||
|
||||
### 黑板系统的作用
|
||||
|
||||
黑板系统(Blackboard System)是行为树中的数据共享中心,类似于传统 AI 系统中的黑板概念。它解决了行为树中节点间数据传递和状态共享的核心问题:
|
||||
|
||||
- **数据传递**:在不同节点间传递运行时数据
|
||||
- **状态管理**:维护游戏对象的状态信息
|
||||
- **解耦设计**:避免节点间直接依赖,提高代码可维护性
|
||||
- **上下文感知**:让节点能够感知周围环境和历史状态
|
||||
|
||||
### 数据隔离层次
|
||||
|
||||
黑板系统采用三层数据隔离设计,形成清晰的数据作用域:
|
||||
|
||||
#### 1. 本地数据(Node Level)
|
||||
- **作用域**:当前节点可见
|
||||
- **生命周期**:随节点创建和销毁
|
||||
- **用途**:节点内部状态、临时变量、私有数据
|
||||
|
||||
#### 2. 树级数据(Tree Level)
|
||||
- **作用域**:整棵行为树内所有节点可见
|
||||
- **生命周期**:随行为树创建和销毁
|
||||
- **用途**:树内节点间共享的状态、任务进度、策略参数
|
||||
|
||||
#### 3. 全局数据(Global Level)
|
||||
- **作用域**:所有行为树实例可见
|
||||
- **生命周期**:应用程序生命周期
|
||||
- **用途**:全局配置、静态数据、跨树共享状态
|
||||
|
||||
|
||||
### 黑板的使用
|
||||
```typescript
|
||||
import * as BT from "kunpocc-behaviortree";
|
||||
|
||||
// 设置全局数据 所有行为树实例可见
|
||||
BT.globalBlackboard.set("playerPosition", {x: 100, y: 100});
|
||||
|
||||
// 在节点中使用黑板数据
|
||||
export class BTAction extends BT.LeafNode {
|
||||
public tick(): BT.Status {
|
||||
// 获取全局数据
|
||||
const playerPosition = this.getGlobal<{x: number, y: number}>("playerPosition");
|
||||
console.log(playerPosition);
|
||||
|
||||
// 设置树级数据
|
||||
this.setRoot("isDead", true);
|
||||
|
||||
// 获取树级数据
|
||||
const isDead = this.getRoot<boolean>("isDead");
|
||||
console.log(isDead);
|
||||
|
||||
// 设置节点数据
|
||||
this.set<boolean>("finished", true);
|
||||
|
||||
// 获取节点数据
|
||||
const finished = this.get<boolean>("finished");
|
||||
console.log(finished);
|
||||
|
||||
return BT.Status.SUCCESS;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 贡献
|
||||
|
||||
欢迎提交 Issue 和 Pull Request。请确保:
|
||||
1. 代码风格一致
|
||||
2. 添加适当的测试
|
||||
3. 更新相关文档
|
||||
|
||||
---
|
||||
|
||||
*"好的程序员关心数据结构,而不是代码。"* - 这个库遵循简洁设计原则,专注于解决实际问题。
|
||||
30
bt-demo/.gitignore
vendored
Normal file
30
bt-demo/.gitignore
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
|
||||
#///////////////////////////
|
||||
# Cocos Creator 3D Project
|
||||
#///////////////////////////
|
||||
library/
|
||||
temp/
|
||||
local/
|
||||
build/
|
||||
profiles/
|
||||
extensions/
|
||||
publish/
|
||||
#//////////////////////////
|
||||
# NPM
|
||||
#//////////////////////////
|
||||
node_modules/
|
||||
|
||||
#//////////////////////////
|
||||
# VSCode
|
||||
#//////////////////////////
|
||||
.vscode/
|
||||
.creator/
|
||||
|
||||
#//////////////////////////
|
||||
# WebStorm
|
||||
#//////////////////////////
|
||||
.idea/
|
||||
|
||||
package-lock.json
|
||||
|
||||
**/.DS_Store
|
||||
10
bt-demo/README.md
Normal file
10
bt-demo/README.md
Normal file
@@ -0,0 +1,10 @@
|
||||
# 项目说明
|
||||
|
||||
clone项目后,到项目根目录,执行以下命令,安装项目依赖库
|
||||
```bash
|
||||
npm i
|
||||
```
|
||||
|
||||
重新用creator打开项目
|
||||
|
||||
本项目使用的creator版本为3.8.6
|
||||
9
bt-demo/assets/res.meta
Normal file
9
bt-demo/assets/res.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "1.2.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "de9afb2e-952c-4e0b-96df-cc676989bed9",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
9
bt-demo/assets/res/spine.meta
Normal file
9
bt-demo/assets/res/spine.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "1.2.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "d5a536b5-db1b-42ac-8654-5f6a81341c3a",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
286
bt-demo/assets/res/spine/spineboy-pro.atlas
Normal file
286
bt-demo/assets/res/spine/spineboy-pro.atlas
Normal file
@@ -0,0 +1,286 @@
|
||||
|
||||
spineboy-pro.png
|
||||
size: 1534,529
|
||||
format: RGBA8888
|
||||
filter: Linear,Linear
|
||||
repeat: none
|
||||
crosshair
|
||||
rotate: false
|
||||
xy: 449, 18
|
||||
size: 89, 89
|
||||
orig: 89, 89
|
||||
offset: 0, 0
|
||||
index: -1
|
||||
eye-indifferent
|
||||
rotate: false
|
||||
xy: 695, 10
|
||||
size: 93, 89
|
||||
orig: 93, 89
|
||||
offset: 0, 0
|
||||
index: -1
|
||||
eye-surprised
|
||||
rotate: true
|
||||
xy: 985, 178
|
||||
size: 93, 89
|
||||
orig: 93, 89
|
||||
offset: 0, 0
|
||||
index: -1
|
||||
front-bracer
|
||||
rotate: true
|
||||
xy: 1407, 103
|
||||
size: 58, 80
|
||||
orig: 58, 80
|
||||
offset: 0, 0
|
||||
index: -1
|
||||
front-fist-closed
|
||||
rotate: true
|
||||
xy: 1208, 203
|
||||
size: 75, 82
|
||||
orig: 75, 82
|
||||
offset: 0, 0
|
||||
index: -1
|
||||
front-fist-open
|
||||
rotate: false
|
||||
xy: 989, 89
|
||||
size: 86, 87
|
||||
orig: 86, 87
|
||||
offset: 0, 0
|
||||
index: -1
|
||||
front-foot
|
||||
rotate: false
|
||||
xy: 1077, 58
|
||||
size: 126, 69
|
||||
orig: 126, 69
|
||||
offset: 0, 0
|
||||
index: -1
|
||||
front-shin
|
||||
rotate: true
|
||||
xy: 803, 89
|
||||
size: 82, 184
|
||||
orig: 82, 184
|
||||
offset: 0, 0
|
||||
index: -1
|
||||
front-thigh
|
||||
rotate: true
|
||||
xy: 1062, 11
|
||||
size: 45, 112
|
||||
orig: 45, 112
|
||||
offset: 0, 0
|
||||
index: -1
|
||||
front-upper-arm
|
||||
rotate: true
|
||||
xy: 1205, 33
|
||||
size: 46, 97
|
||||
orig: 46, 97
|
||||
offset: 0, 0
|
||||
index: -1
|
||||
goggles
|
||||
rotate: false
|
||||
xy: 540, 101
|
||||
size: 261, 166
|
||||
orig: 261, 166
|
||||
offset: 0, 0
|
||||
index: -1
|
||||
gun
|
||||
rotate: false
|
||||
xy: 1301, 324
|
||||
size: 209, 203
|
||||
orig: 210, 203
|
||||
offset: 0, 0
|
||||
index: -1
|
||||
head
|
||||
rotate: false
|
||||
xy: 2, 75
|
||||
size: 271, 298
|
||||
orig: 271, 298
|
||||
offset: 0, 0
|
||||
index: -1
|
||||
hoverboard-board
|
||||
rotate: false
|
||||
xy: 2, 375
|
||||
size: 492, 152
|
||||
orig: 492, 152
|
||||
offset: 0, 0
|
||||
index: -1
|
||||
hoverboard-thruster
|
||||
rotate: false
|
||||
xy: 1472, 38
|
||||
size: 60, 63
|
||||
orig: 60, 64
|
||||
offset: 0, 0
|
||||
index: -1
|
||||
hoverglow-small
|
||||
rotate: false
|
||||
xy: 2, 2
|
||||
size: 258, 71
|
||||
orig: 274, 75
|
||||
offset: 7, 2
|
||||
index: -1
|
||||
mouth-grind
|
||||
rotate: false
|
||||
xy: 1203, 142
|
||||
size: 93, 59
|
||||
orig: 93, 59
|
||||
offset: 0, 0
|
||||
index: -1
|
||||
mouth-oooo
|
||||
rotate: false
|
||||
xy: 1205, 81
|
||||
size: 93, 59
|
||||
orig: 93, 59
|
||||
offset: 0, 0
|
||||
index: -1
|
||||
mouth-smile
|
||||
rotate: false
|
||||
xy: 1300, 98
|
||||
size: 93, 59
|
||||
orig: 93, 59
|
||||
offset: 0, 0
|
||||
index: -1
|
||||
muzzle-glow
|
||||
rotate: false
|
||||
xy: 496, 485
|
||||
size: 42, 42
|
||||
orig: 50, 50
|
||||
offset: 4, 4
|
||||
index: -1
|
||||
muzzle-ring
|
||||
rotate: true
|
||||
xy: 1301, 276
|
||||
size: 46, 206
|
||||
orig: 49, 209
|
||||
offset: 1, 2
|
||||
index: -1
|
||||
muzzle01
|
||||
rotate: false
|
||||
xy: 1077, 129
|
||||
size: 124, 74
|
||||
orig: 133, 79
|
||||
offset: 3, 2
|
||||
index: -1
|
||||
muzzle02
|
||||
rotate: false
|
||||
xy: 934, 12
|
||||
size: 126, 75
|
||||
orig: 135, 84
|
||||
offset: 4, 5
|
||||
index: -1
|
||||
muzzle03
|
||||
rotate: false
|
||||
xy: 540, 6
|
||||
size: 153, 93
|
||||
orig: 166, 106
|
||||
offset: 7, 7
|
||||
index: -1
|
||||
muzzle04
|
||||
rotate: false
|
||||
xy: 790, 5
|
||||
size: 142, 82
|
||||
orig: 149, 90
|
||||
offset: 4, 4
|
||||
index: -1
|
||||
muzzle05
|
||||
rotate: false
|
||||
xy: 1076, 205
|
||||
size: 130, 73
|
||||
orig: 135, 75
|
||||
offset: 2, 1
|
||||
index: -1
|
||||
neck
|
||||
rotate: false
|
||||
xy: 1489, 120
|
||||
size: 35, 41
|
||||
orig: 36, 41
|
||||
offset: 0, 0
|
||||
index: -1
|
||||
portal-bg
|
||||
rotate: false
|
||||
xy: 275, 109
|
||||
size: 263, 264
|
||||
orig: 266, 266
|
||||
offset: 2, 1
|
||||
index: -1
|
||||
portal-flare1
|
||||
rotate: false
|
||||
xy: 1407, 163
|
||||
size: 103, 54
|
||||
orig: 111, 60
|
||||
offset: 4, 3
|
||||
index: -1
|
||||
portal-flare2
|
||||
rotate: false
|
||||
xy: 1407, 219
|
||||
size: 107, 55
|
||||
orig: 114, 61
|
||||
offset: 4, 3
|
||||
index: -1
|
||||
portal-flare3
|
||||
rotate: false
|
||||
xy: 1298, 159
|
||||
size: 107, 53
|
||||
orig: 115, 59
|
||||
offset: 5, 3
|
||||
index: -1
|
||||
portal-shade
|
||||
rotate: false
|
||||
xy: 540, 269
|
||||
size: 258, 258
|
||||
orig: 266, 266
|
||||
offset: 4, 4
|
||||
index: -1
|
||||
portal-streaks1
|
||||
rotate: false
|
||||
xy: 800, 273
|
||||
size: 249, 254
|
||||
orig: 252, 256
|
||||
offset: 1, 1
|
||||
index: -1
|
||||
portal-streaks2
|
||||
rotate: false
|
||||
xy: 1051, 280
|
||||
size: 248, 247
|
||||
orig: 250, 249
|
||||
offset: 1, 1
|
||||
index: -1
|
||||
rear-bracer
|
||||
rotate: true
|
||||
xy: 1400, 46
|
||||
size: 55, 70
|
||||
orig: 56, 72
|
||||
offset: 0, 2
|
||||
index: -1
|
||||
rear-foot
|
||||
rotate: false
|
||||
xy: 1292, 214
|
||||
size: 113, 60
|
||||
orig: 113, 60
|
||||
offset: 0, 0
|
||||
index: -1
|
||||
rear-shin
|
||||
rotate: true
|
||||
xy: 275, 33
|
||||
size: 74, 172
|
||||
orig: 75, 178
|
||||
offset: 1, 4
|
||||
index: -1
|
||||
rear-thigh
|
||||
rotate: true
|
||||
xy: 1304, 41
|
||||
size: 55, 94
|
||||
orig: 55, 94
|
||||
offset: 0, 0
|
||||
index: -1
|
||||
rear-upper-arm
|
||||
rotate: false
|
||||
xy: 496, 396
|
||||
size: 40, 87
|
||||
orig: 40, 87
|
||||
offset: 0, 0
|
||||
index: -1
|
||||
torso
|
||||
rotate: true
|
||||
xy: 803, 173
|
||||
size: 98, 180
|
||||
orig: 98, 180
|
||||
offset: 0, 0
|
||||
index: -1
|
||||
12
bt-demo/assets/res/spine/spineboy-pro.atlas.meta
Normal file
12
bt-demo/assets/res/spine/spineboy-pro.atlas.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"ver": "1.0.0",
|
||||
"importer": "*",
|
||||
"imported": true,
|
||||
"uuid": "e6a17488-4c37-468e-bf09-a613cf272d3e",
|
||||
"files": [
|
||||
".atlas",
|
||||
".json"
|
||||
],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
4304
bt-demo/assets/res/spine/spineboy-pro.json
Normal file
4304
bt-demo/assets/res/spine/spineboy-pro.json
Normal file
File diff suppressed because it is too large
Load Diff
13
bt-demo/assets/res/spine/spineboy-pro.json.meta
Normal file
13
bt-demo/assets/res/spine/spineboy-pro.json.meta
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"ver": "1.2.7",
|
||||
"importer": "spine-data",
|
||||
"imported": true,
|
||||
"uuid": "39a7d8cd-533a-479a-b909-9575bf720338",
|
||||
"files": [
|
||||
".json"
|
||||
],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"atlasUuid": "e6a17488-4c37-468e-bf09-a613cf272d3e"
|
||||
}
|
||||
}
|
||||
BIN
bt-demo/assets/res/spine/spineboy-pro.png
Normal file
BIN
bt-demo/assets/res/spine/spineboy-pro.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 824 KiB |
42
bt-demo/assets/res/spine/spineboy-pro.png.meta
Normal file
42
bt-demo/assets/res/spine/spineboy-pro.png.meta
Normal file
@@ -0,0 +1,42 @@
|
||||
{
|
||||
"ver": "1.0.27",
|
||||
"importer": "image",
|
||||
"imported": true,
|
||||
"uuid": "74f9105a-c38b-4f5b-b7f2-f59cc6374074",
|
||||
"files": [
|
||||
".json",
|
||||
".png"
|
||||
],
|
||||
"subMetas": {
|
||||
"6c48a": {
|
||||
"importer": "texture",
|
||||
"uuid": "74f9105a-c38b-4f5b-b7f2-f59cc6374074@6c48a",
|
||||
"displayName": "spineboy-pro",
|
||||
"id": "6c48a",
|
||||
"name": "texture",
|
||||
"userData": {
|
||||
"wrapModeS": "repeat",
|
||||
"wrapModeT": "repeat",
|
||||
"minfilter": "linear",
|
||||
"magfilter": "linear",
|
||||
"mipfilter": "none",
|
||||
"anisotropy": 0,
|
||||
"isUuid": true,
|
||||
"imageUuidOrDatabaseUri": "74f9105a-c38b-4f5b-b7f2-f59cc6374074",
|
||||
"visible": false
|
||||
},
|
||||
"ver": "1.0.22",
|
||||
"imported": true,
|
||||
"files": [
|
||||
".json"
|
||||
],
|
||||
"subMetas": {}
|
||||
}
|
||||
},
|
||||
"userData": {
|
||||
"type": "texture",
|
||||
"hasAlpha": true,
|
||||
"fixAlphaTransparencyArtifacts": false,
|
||||
"redirect": "74f9105a-c38b-4f5b-b7f2-f59cc6374074@6c48a"
|
||||
}
|
||||
}
|
||||
14
bt-demo/assets/resources.meta
Normal file
14
bt-demo/assets/resources.meta
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"ver": "1.2.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "9d91ce52-ffe8-43c0-a118-9ace6bd9cf45",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"isBundle": true,
|
||||
"bundleConfigID": "default",
|
||||
"bundleName": "resources",
|
||||
"priority": 8
|
||||
}
|
||||
}
|
||||
9
bt-demo/assets/resources/config.meta
Normal file
9
bt-demo/assets/resources/config.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "1.2.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "1ea023b5-0ab6-4613-b157-3098b11c379b",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
29
bt-demo/assets/resources/config/bt_config.json
Normal file
29
bt-demo/assets/resources/config/bt_config.json
Normal file
@@ -0,0 +1,29 @@
|
||||
{
|
||||
"bt-tree2": [
|
||||
{
|
||||
"id": "1758206972710_bhxebhy7o",
|
||||
"className": "Sequence",
|
||||
"parameters": {},
|
||||
"children": [
|
||||
"1758090634327_mf36nwkdt"
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "1758090634327_mf36nwkdt",
|
||||
"className": "Selector",
|
||||
"parameters": {},
|
||||
"children": [
|
||||
"1758206988178_55b7kk5va"
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "1758206988178_55b7kk5va",
|
||||
"className": "BTAnimation",
|
||||
"parameters": {
|
||||
"_name": "",
|
||||
"_loop": false
|
||||
},
|
||||
"children": []
|
||||
}
|
||||
]
|
||||
}
|
||||
11
bt-demo/assets/resources/config/bt_config.json.meta
Normal file
11
bt-demo/assets/resources/config/bt_config.json.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"ver": "2.0.1",
|
||||
"importer": "json",
|
||||
"imported": true,
|
||||
"uuid": "c8aeef5d-6d0e-4093-848e-7d8f1ca30261",
|
||||
"files": [
|
||||
".json"
|
||||
],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
9
bt-demo/assets/resources/prefab.meta
Normal file
9
bt-demo/assets/resources/prefab.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "1.2.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "6b9b2da1-08c2-4c40-ab35-e7cb5bb30872",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
147
bt-demo/assets/resources/prefab/spineboy.prefab
Normal file
147
bt-demo/assets/resources/prefab/spineboy.prefab
Normal file
@@ -0,0 +1,147 @@
|
||||
[
|
||||
{
|
||||
"__type__": "cc.Prefab",
|
||||
"_name": "spineboy",
|
||||
"_objFlags": 0,
|
||||
"__editorExtras__": {},
|
||||
"_native": "",
|
||||
"data": {
|
||||
"__id__": 1
|
||||
},
|
||||
"optimizationPolicy": 0,
|
||||
"persistent": false
|
||||
},
|
||||
{
|
||||
"__type__": "cc.Node",
|
||||
"_name": "spineboy",
|
||||
"_objFlags": 0,
|
||||
"__editorExtras__": {},
|
||||
"_parent": null,
|
||||
"_children": [],
|
||||
"_active": true,
|
||||
"_components": [
|
||||
{
|
||||
"__id__": 2
|
||||
},
|
||||
{
|
||||
"__id__": 4
|
||||
}
|
||||
],
|
||||
"_prefab": {
|
||||
"__id__": 6
|
||||
},
|
||||
"_lpos": {
|
||||
"__type__": "cc.Vec3",
|
||||
"x": 0,
|
||||
"y": 0,
|
||||
"z": -1000
|
||||
},
|
||||
"_lrot": {
|
||||
"__type__": "cc.Quat",
|
||||
"x": 0,
|
||||
"y": 0,
|
||||
"z": 0,
|
||||
"w": 1
|
||||
},
|
||||
"_lscale": {
|
||||
"__type__": "cc.Vec3",
|
||||
"x": 0.3,
|
||||
"y": 0.3,
|
||||
"z": 1
|
||||
},
|
||||
"_mobility": 0,
|
||||
"_layer": 1073741824,
|
||||
"_euler": {
|
||||
"__type__": "cc.Vec3",
|
||||
"x": 0,
|
||||
"y": 0,
|
||||
"z": 0
|
||||
},
|
||||
"_id": ""
|
||||
},
|
||||
{
|
||||
"__type__": "cc.UITransform",
|
||||
"_name": "",
|
||||
"_objFlags": 0,
|
||||
"__editorExtras__": {},
|
||||
"node": {
|
||||
"__id__": 1
|
||||
},
|
||||
"_enabled": true,
|
||||
"__prefab": {
|
||||
"__id__": 3
|
||||
},
|
||||
"_contentSize": {
|
||||
"__type__": "cc.Size",
|
||||
"width": 419.8399963378906,
|
||||
"height": 686.0800170898438
|
||||
},
|
||||
"_anchorPoint": {
|
||||
"__type__": "cc.Vec2",
|
||||
"x": 0.45412539378136013,
|
||||
"y": 0.011660447470739235
|
||||
},
|
||||
"_id": ""
|
||||
},
|
||||
{
|
||||
"__type__": "cc.CompPrefabInfo",
|
||||
"fileId": "dfVeZdqm9E15k7OBD615QP"
|
||||
},
|
||||
{
|
||||
"__type__": "sp.Skeleton",
|
||||
"_name": "",
|
||||
"_objFlags": 0,
|
||||
"__editorExtras__": {},
|
||||
"node": {
|
||||
"__id__": 1
|
||||
},
|
||||
"_enabled": true,
|
||||
"__prefab": {
|
||||
"__id__": 5
|
||||
},
|
||||
"_customMaterial": null,
|
||||
"_srcBlendFactor": 2,
|
||||
"_dstBlendFactor": 4,
|
||||
"_color": {
|
||||
"__type__": "cc.Color",
|
||||
"r": 255,
|
||||
"g": 255,
|
||||
"b": 255,
|
||||
"a": 255
|
||||
},
|
||||
"_skeletonData": {
|
||||
"__uuid__": "39a7d8cd-533a-479a-b909-9575bf720338",
|
||||
"__expectedType__": "sp.SkeletonData"
|
||||
},
|
||||
"defaultSkin": "default",
|
||||
"defaultAnimation": "jump",
|
||||
"_premultipliedAlpha": true,
|
||||
"_timeScale": 1,
|
||||
"_preCacheMode": 0,
|
||||
"_cacheMode": 0,
|
||||
"_sockets": [],
|
||||
"_useTint": false,
|
||||
"_debugMesh": false,
|
||||
"_debugBones": false,
|
||||
"_debugSlots": false,
|
||||
"_enableBatch": false,
|
||||
"loop": true,
|
||||
"_id": ""
|
||||
},
|
||||
{
|
||||
"__type__": "cc.CompPrefabInfo",
|
||||
"fileId": "deHPJ9jpdJZq/2PP1E2haI"
|
||||
},
|
||||
{
|
||||
"__type__": "cc.PrefabInfo",
|
||||
"root": {
|
||||
"__id__": 1
|
||||
},
|
||||
"asset": {
|
||||
"__id__": 0
|
||||
},
|
||||
"fileId": "fcg4LyhU9MpITaQy7lW8Ru",
|
||||
"instance": null,
|
||||
"targetOverrides": null
|
||||
}
|
||||
]
|
||||
13
bt-demo/assets/resources/prefab/spineboy.prefab.meta
Normal file
13
bt-demo/assets/resources/prefab/spineboy.prefab.meta
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"ver": "1.1.50",
|
||||
"importer": "prefab",
|
||||
"imported": true,
|
||||
"uuid": "610db270-416d-42a9-a228-67b0fe1beee4",
|
||||
"files": [
|
||||
".json"
|
||||
],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"syncNodeName": "spineboy"
|
||||
}
|
||||
}
|
||||
9
bt-demo/assets/scene.meta
Normal file
9
bt-demo/assets/scene.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "1.2.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "6f3166a3-36ba-4512-bae8-889c2a7d7d98",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
726
bt-demo/assets/scene/GameEntry.scene
Normal file
726
bt-demo/assets/scene/GameEntry.scene
Normal file
@@ -0,0 +1,726 @@
|
||||
[
|
||||
{
|
||||
"__type__": "cc.SceneAsset",
|
||||
"_name": "GameEntry",
|
||||
"_objFlags": 0,
|
||||
"__editorExtras__": {},
|
||||
"_native": "",
|
||||
"scene": {
|
||||
"__id__": 1
|
||||
}
|
||||
},
|
||||
{
|
||||
"__type__": "cc.Scene",
|
||||
"_name": "GameEntry",
|
||||
"_objFlags": 0,
|
||||
"__editorExtras__": {},
|
||||
"_parent": null,
|
||||
"_children": [
|
||||
{
|
||||
"__id__": 2
|
||||
}
|
||||
],
|
||||
"_active": true,
|
||||
"_components": [],
|
||||
"_prefab": {
|
||||
"__id__": 22
|
||||
},
|
||||
"_lpos": {
|
||||
"__type__": "cc.Vec3",
|
||||
"x": 0,
|
||||
"y": 0,
|
||||
"z": 0
|
||||
},
|
||||
"_lrot": {
|
||||
"__type__": "cc.Quat",
|
||||
"x": 0,
|
||||
"y": 0,
|
||||
"z": 0,
|
||||
"w": 1
|
||||
},
|
||||
"_lscale": {
|
||||
"__type__": "cc.Vec3",
|
||||
"x": 1,
|
||||
"y": 1,
|
||||
"z": 1
|
||||
},
|
||||
"_mobility": 0,
|
||||
"_layer": 1073741824,
|
||||
"_euler": {
|
||||
"__type__": "cc.Vec3",
|
||||
"x": 0,
|
||||
"y": 0,
|
||||
"z": 0
|
||||
},
|
||||
"autoReleaseAssets": false,
|
||||
"_globals": {
|
||||
"__id__": 25
|
||||
},
|
||||
"_id": "bef93422-3e63-4c0f-a5cf-d926e7360673"
|
||||
},
|
||||
{
|
||||
"__type__": "cc.Node",
|
||||
"_name": "Canvas",
|
||||
"_objFlags": 0,
|
||||
"__editorExtras__": {},
|
||||
"_parent": {
|
||||
"__id__": 1
|
||||
},
|
||||
"_children": [
|
||||
{
|
||||
"__id__": 3
|
||||
},
|
||||
{
|
||||
"__id__": 6
|
||||
},
|
||||
{
|
||||
"__id__": 8
|
||||
}
|
||||
],
|
||||
"_active": true,
|
||||
"_components": [
|
||||
{
|
||||
"__id__": 19
|
||||
},
|
||||
{
|
||||
"__id__": 20
|
||||
},
|
||||
{
|
||||
"__id__": 21
|
||||
}
|
||||
],
|
||||
"_prefab": null,
|
||||
"_lpos": {
|
||||
"__type__": "cc.Vec3",
|
||||
"x": 375,
|
||||
"y": 667,
|
||||
"z": 0
|
||||
},
|
||||
"_lrot": {
|
||||
"__type__": "cc.Quat",
|
||||
"x": 0,
|
||||
"y": 0,
|
||||
"z": 0,
|
||||
"w": 1
|
||||
},
|
||||
"_lscale": {
|
||||
"__type__": "cc.Vec3",
|
||||
"x": 1,
|
||||
"y": 1,
|
||||
"z": 1
|
||||
},
|
||||
"_mobility": 0,
|
||||
"_layer": 1073741824,
|
||||
"_euler": {
|
||||
"__type__": "cc.Vec3",
|
||||
"x": 0,
|
||||
"y": 0,
|
||||
"z": 0
|
||||
},
|
||||
"_id": "beI88Z2HpFELqR4T5EMHpg"
|
||||
},
|
||||
{
|
||||
"__type__": "cc.Node",
|
||||
"_name": "entry",
|
||||
"_objFlags": 0,
|
||||
"__editorExtras__": {},
|
||||
"_parent": {
|
||||
"__id__": 2
|
||||
},
|
||||
"_children": [],
|
||||
"_active": true,
|
||||
"_components": [
|
||||
{
|
||||
"__id__": 4
|
||||
},
|
||||
{
|
||||
"__id__": 5
|
||||
}
|
||||
],
|
||||
"_prefab": null,
|
||||
"_lpos": {
|
||||
"__type__": "cc.Vec3",
|
||||
"x": 0,
|
||||
"y": 0,
|
||||
"z": 1000
|
||||
},
|
||||
"_lrot": {
|
||||
"__type__": "cc.Quat",
|
||||
"x": 0,
|
||||
"y": 0,
|
||||
"z": 0,
|
||||
"w": 1
|
||||
},
|
||||
"_lscale": {
|
||||
"__type__": "cc.Vec3",
|
||||
"x": 1,
|
||||
"y": 1,
|
||||
"z": 1
|
||||
},
|
||||
"_mobility": 0,
|
||||
"_layer": 1073741824,
|
||||
"_euler": {
|
||||
"__type__": "cc.Vec3",
|
||||
"x": 0,
|
||||
"y": 0,
|
||||
"z": 0
|
||||
},
|
||||
"_id": "6eSMYbFu9DJL1bKl9DnMo6"
|
||||
},
|
||||
{
|
||||
"__type__": "cc.UITransform",
|
||||
"_name": "",
|
||||
"_objFlags": 0,
|
||||
"__editorExtras__": {},
|
||||
"node": {
|
||||
"__id__": 3
|
||||
},
|
||||
"_enabled": true,
|
||||
"__prefab": null,
|
||||
"_contentSize": {
|
||||
"__type__": "cc.Size",
|
||||
"width": 100,
|
||||
"height": 100
|
||||
},
|
||||
"_anchorPoint": {
|
||||
"__type__": "cc.Vec2",
|
||||
"x": 0.5,
|
||||
"y": 0.5
|
||||
},
|
||||
"_id": "8ekQXh8+BP/6BzVmjL7OPr"
|
||||
},
|
||||
{
|
||||
"__type__": "e5804qewX9N9op0d4aH4r7B",
|
||||
"_name": "",
|
||||
"_objFlags": 0,
|
||||
"__editorExtras__": {},
|
||||
"node": {
|
||||
"__id__": 3
|
||||
},
|
||||
"_enabled": true,
|
||||
"__prefab": null,
|
||||
"skeleton": null,
|
||||
"btConfig": {
|
||||
"__uuid__": "c8aeef5d-6d0e-4093-848e-7d8f1ca30261",
|
||||
"__expectedType__": "cc.JsonAsset"
|
||||
},
|
||||
"_id": "69LhmWaZRIUpmYvdiN82Ha"
|
||||
},
|
||||
{
|
||||
"__type__": "cc.Node",
|
||||
"_name": "Camera",
|
||||
"_objFlags": 0,
|
||||
"__editorExtras__": {},
|
||||
"_parent": {
|
||||
"__id__": 2
|
||||
},
|
||||
"_children": [],
|
||||
"_active": true,
|
||||
"_components": [
|
||||
{
|
||||
"__id__": 7
|
||||
}
|
||||
],
|
||||
"_prefab": null,
|
||||
"_lpos": {
|
||||
"__type__": "cc.Vec3",
|
||||
"x": 0,
|
||||
"y": 0,
|
||||
"z": 1000
|
||||
},
|
||||
"_lrot": {
|
||||
"__type__": "cc.Quat",
|
||||
"x": 0,
|
||||
"y": 0,
|
||||
"z": 0,
|
||||
"w": 1
|
||||
},
|
||||
"_lscale": {
|
||||
"__type__": "cc.Vec3",
|
||||
"x": 1,
|
||||
"y": 1,
|
||||
"z": 1
|
||||
},
|
||||
"_mobility": 0,
|
||||
"_layer": 1073741824,
|
||||
"_euler": {
|
||||
"__type__": "cc.Vec3",
|
||||
"x": 0,
|
||||
"y": 0,
|
||||
"z": 0
|
||||
},
|
||||
"_id": "ebFwiq8gBFaYpqYbdoDODe"
|
||||
},
|
||||
{
|
||||
"__type__": "cc.Camera",
|
||||
"_name": "",
|
||||
"_objFlags": 0,
|
||||
"__editorExtras__": {},
|
||||
"node": {
|
||||
"__id__": 6
|
||||
},
|
||||
"_enabled": true,
|
||||
"__prefab": null,
|
||||
"_projection": 0,
|
||||
"_priority": 1,
|
||||
"_fov": 45,
|
||||
"_fovAxis": 0,
|
||||
"_orthoHeight": 667,
|
||||
"_near": 0,
|
||||
"_far": 2000,
|
||||
"_color": {
|
||||
"__type__": "cc.Color",
|
||||
"r": 0,
|
||||
"g": 0,
|
||||
"b": 0,
|
||||
"a": 255
|
||||
},
|
||||
"_depth": 1,
|
||||
"_stencil": 0,
|
||||
"_clearFlags": 0,
|
||||
"_rect": {
|
||||
"__type__": "cc.Rect",
|
||||
"x": 0,
|
||||
"y": 0,
|
||||
"width": 1,
|
||||
"height": 1
|
||||
},
|
||||
"_aperture": 19,
|
||||
"_shutter": 7,
|
||||
"_iso": 0,
|
||||
"_screenScale": 1,
|
||||
"_visibility": 1107296259,
|
||||
"_targetTexture": null,
|
||||
"_postProcess": null,
|
||||
"_usePostProcess": false,
|
||||
"_cameraType": -1,
|
||||
"_trackingType": 0,
|
||||
"_id": "63WIch3o5BEYRlXzTT0oWc"
|
||||
},
|
||||
{
|
||||
"__type__": "cc.Node",
|
||||
"_objFlags": 0,
|
||||
"_parent": {
|
||||
"__id__": 2
|
||||
},
|
||||
"_prefab": {
|
||||
"__id__": 9
|
||||
},
|
||||
"__editorExtras__": {}
|
||||
},
|
||||
{
|
||||
"__type__": "cc.PrefabInfo",
|
||||
"root": {
|
||||
"__id__": 8
|
||||
},
|
||||
"asset": {
|
||||
"__uuid__": "610db270-416d-42a9-a228-67b0fe1beee4",
|
||||
"__expectedType__": "cc.Prefab"
|
||||
},
|
||||
"fileId": "fcg4LyhU9MpITaQy7lW8Ru",
|
||||
"instance": {
|
||||
"__id__": 10
|
||||
},
|
||||
"targetOverrides": null,
|
||||
"nestedPrefabInstanceRoots": null
|
||||
},
|
||||
{
|
||||
"__type__": "cc.PrefabInstance",
|
||||
"fileId": "2eYzhZYv5Mi5OETcYel3W3",
|
||||
"prefabRootNode": null,
|
||||
"mountedChildren": [],
|
||||
"mountedComponents": [],
|
||||
"propertyOverrides": [
|
||||
{
|
||||
"__id__": 11
|
||||
},
|
||||
{
|
||||
"__id__": 13
|
||||
},
|
||||
{
|
||||
"__id__": 14
|
||||
},
|
||||
{
|
||||
"__id__": 15
|
||||
},
|
||||
{
|
||||
"__id__": 16
|
||||
},
|
||||
{
|
||||
"__id__": 18
|
||||
}
|
||||
],
|
||||
"removedComponents": []
|
||||
},
|
||||
{
|
||||
"__type__": "CCPropertyOverrideInfo",
|
||||
"targetInfo": {
|
||||
"__id__": 12
|
||||
},
|
||||
"propertyPath": [
|
||||
"_lpos"
|
||||
],
|
||||
"value": {
|
||||
"__type__": "cc.Vec3",
|
||||
"x": 0,
|
||||
"y": 0,
|
||||
"z": 0
|
||||
}
|
||||
},
|
||||
{
|
||||
"__type__": "cc.TargetInfo",
|
||||
"localID": [
|
||||
"fcg4LyhU9MpITaQy7lW8Ru"
|
||||
]
|
||||
},
|
||||
{
|
||||
"__type__": "CCPropertyOverrideInfo",
|
||||
"targetInfo": {
|
||||
"__id__": 12
|
||||
},
|
||||
"propertyPath": [
|
||||
"_name"
|
||||
],
|
||||
"value": "spineboy"
|
||||
},
|
||||
{
|
||||
"__type__": "CCPropertyOverrideInfo",
|
||||
"targetInfo": {
|
||||
"__id__": 12
|
||||
},
|
||||
"propertyPath": [
|
||||
"_lrot"
|
||||
],
|
||||
"value": {
|
||||
"__type__": "cc.Quat",
|
||||
"x": 0,
|
||||
"y": 0,
|
||||
"z": 0,
|
||||
"w": 1
|
||||
}
|
||||
},
|
||||
{
|
||||
"__type__": "CCPropertyOverrideInfo",
|
||||
"targetInfo": {
|
||||
"__id__": 12
|
||||
},
|
||||
"propertyPath": [
|
||||
"_euler"
|
||||
],
|
||||
"value": {
|
||||
"__type__": "cc.Vec3",
|
||||
"x": 0,
|
||||
"y": 0,
|
||||
"z": 0
|
||||
}
|
||||
},
|
||||
{
|
||||
"__type__": "CCPropertyOverrideInfo",
|
||||
"targetInfo": {
|
||||
"__id__": 17
|
||||
},
|
||||
"propertyPath": [
|
||||
"defaultAnimation"
|
||||
],
|
||||
"value": "idle"
|
||||
},
|
||||
{
|
||||
"__type__": "cc.TargetInfo",
|
||||
"localID": [
|
||||
"deHPJ9jpdJZq/2PP1E2haI"
|
||||
]
|
||||
},
|
||||
{
|
||||
"__type__": "CCPropertyOverrideInfo",
|
||||
"targetInfo": {
|
||||
"__id__": 17
|
||||
},
|
||||
"propertyPath": [
|
||||
"_premultipliedAlpha"
|
||||
],
|
||||
"value": true
|
||||
},
|
||||
{
|
||||
"__type__": "cc.UITransform",
|
||||
"_name": "",
|
||||
"_objFlags": 0,
|
||||
"__editorExtras__": {},
|
||||
"node": {
|
||||
"__id__": 2
|
||||
},
|
||||
"_enabled": true,
|
||||
"__prefab": null,
|
||||
"_contentSize": {
|
||||
"__type__": "cc.Size",
|
||||
"width": 0,
|
||||
"height": 0
|
||||
},
|
||||
"_anchorPoint": {
|
||||
"__type__": "cc.Vec2",
|
||||
"x": 0.5,
|
||||
"y": 0.5
|
||||
},
|
||||
"_id": "d6rUX5yfhMlKoWX2bSbawx"
|
||||
},
|
||||
{
|
||||
"__type__": "cc.Canvas",
|
||||
"_name": "",
|
||||
"_objFlags": 0,
|
||||
"__editorExtras__": {},
|
||||
"node": {
|
||||
"__id__": 2
|
||||
},
|
||||
"_enabled": true,
|
||||
"__prefab": null,
|
||||
"_cameraComponent": {
|
||||
"__id__": 7
|
||||
},
|
||||
"_alignCanvasWithScreen": true,
|
||||
"_id": "12O/ljcVlEqLmVm3U2gEOQ"
|
||||
},
|
||||
{
|
||||
"__type__": "cc.Widget",
|
||||
"_name": "",
|
||||
"_objFlags": 0,
|
||||
"__editorExtras__": {},
|
||||
"node": {
|
||||
"__id__": 2
|
||||
},
|
||||
"_enabled": true,
|
||||
"__prefab": null,
|
||||
"_alignFlags": 18,
|
||||
"_target": null,
|
||||
"_left": 375,
|
||||
"_right": 375,
|
||||
"_top": 667,
|
||||
"_bottom": 667,
|
||||
"_horizontalCenter": 0,
|
||||
"_verticalCenter": 0,
|
||||
"_isAbsLeft": true,
|
||||
"_isAbsRight": true,
|
||||
"_isAbsTop": true,
|
||||
"_isAbsBottom": true,
|
||||
"_isAbsHorizontalCenter": true,
|
||||
"_isAbsVerticalCenter": true,
|
||||
"_originalWidth": 0,
|
||||
"_originalHeight": 0,
|
||||
"_alignMode": 2,
|
||||
"_lockFlags": 0,
|
||||
"_id": "c5V1EV8IpMtrIvY1OE9t2u"
|
||||
},
|
||||
{
|
||||
"__type__": "cc.PrefabInfo",
|
||||
"root": null,
|
||||
"asset": null,
|
||||
"fileId": "bef93422-3e63-4c0f-a5cf-d926e7360673",
|
||||
"instance": null,
|
||||
"targetOverrides": [
|
||||
{
|
||||
"__id__": 23
|
||||
}
|
||||
],
|
||||
"nestedPrefabInstanceRoots": [
|
||||
{
|
||||
"__id__": 8
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"__type__": "cc.TargetOverrideInfo",
|
||||
"source": {
|
||||
"__id__": 5
|
||||
},
|
||||
"sourceInfo": null,
|
||||
"propertyPath": [
|
||||
"skeleton"
|
||||
],
|
||||
"target": {
|
||||
"__id__": 8
|
||||
},
|
||||
"targetInfo": {
|
||||
"__id__": 24
|
||||
}
|
||||
},
|
||||
{
|
||||
"__type__": "cc.TargetInfo",
|
||||
"localID": [
|
||||
"deHPJ9jpdJZq/2PP1E2haI"
|
||||
]
|
||||
},
|
||||
{
|
||||
"__type__": "cc.SceneGlobals",
|
||||
"ambient": {
|
||||
"__id__": 26
|
||||
},
|
||||
"shadows": {
|
||||
"__id__": 27
|
||||
},
|
||||
"_skybox": {
|
||||
"__id__": 28
|
||||
},
|
||||
"fog": {
|
||||
"__id__": 29
|
||||
},
|
||||
"octree": {
|
||||
"__id__": 30
|
||||
},
|
||||
"skin": {
|
||||
"__id__": 31
|
||||
},
|
||||
"lightProbeInfo": {
|
||||
"__id__": 32
|
||||
},
|
||||
"postSettings": {
|
||||
"__id__": 33
|
||||
},
|
||||
"bakedWithStationaryMainLight": false,
|
||||
"bakedWithHighpLightmap": false
|
||||
},
|
||||
{
|
||||
"__type__": "cc.AmbientInfo",
|
||||
"_skyColorHDR": {
|
||||
"__type__": "cc.Vec4",
|
||||
"x": 0,
|
||||
"y": 0,
|
||||
"z": 0,
|
||||
"w": 0.520833125
|
||||
},
|
||||
"_skyColor": {
|
||||
"__type__": "cc.Vec4",
|
||||
"x": 0,
|
||||
"y": 0,
|
||||
"z": 0,
|
||||
"w": 0.520833125
|
||||
},
|
||||
"_skyIllumHDR": 20000,
|
||||
"_skyIllum": 20000,
|
||||
"_groundAlbedoHDR": {
|
||||
"__type__": "cc.Vec4",
|
||||
"x": 0,
|
||||
"y": 0,
|
||||
"z": 0,
|
||||
"w": 0
|
||||
},
|
||||
"_groundAlbedo": {
|
||||
"__type__": "cc.Vec4",
|
||||
"x": 0,
|
||||
"y": 0,
|
||||
"z": 0,
|
||||
"w": 0
|
||||
},
|
||||
"_skyColorLDR": {
|
||||
"__type__": "cc.Vec4",
|
||||
"x": 0.2,
|
||||
"y": 0.5,
|
||||
"z": 0.8,
|
||||
"w": 1
|
||||
},
|
||||
"_skyIllumLDR": 20000,
|
||||
"_groundAlbedoLDR": {
|
||||
"__type__": "cc.Vec4",
|
||||
"x": 0.2,
|
||||
"y": 0.2,
|
||||
"z": 0.2,
|
||||
"w": 1
|
||||
}
|
||||
},
|
||||
{
|
||||
"__type__": "cc.ShadowsInfo",
|
||||
"_enabled": false,
|
||||
"_type": 0,
|
||||
"_normal": {
|
||||
"__type__": "cc.Vec3",
|
||||
"x": 0,
|
||||
"y": 1,
|
||||
"z": 0
|
||||
},
|
||||
"_distance": 0,
|
||||
"_planeBias": 1,
|
||||
"_shadowColor": {
|
||||
"__type__": "cc.Color",
|
||||
"r": 76,
|
||||
"g": 76,
|
||||
"b": 76,
|
||||
"a": 255
|
||||
},
|
||||
"_maxReceived": 4,
|
||||
"_size": {
|
||||
"__type__": "cc.Vec2",
|
||||
"x": 512,
|
||||
"y": 512
|
||||
}
|
||||
},
|
||||
{
|
||||
"__type__": "cc.SkyboxInfo",
|
||||
"_envLightingType": 0,
|
||||
"_envmapHDR": null,
|
||||
"_envmap": null,
|
||||
"_envmapLDR": null,
|
||||
"_diffuseMapHDR": null,
|
||||
"_diffuseMapLDR": null,
|
||||
"_enabled": false,
|
||||
"_useHDR": true,
|
||||
"_editableMaterial": null,
|
||||
"_reflectionHDR": null,
|
||||
"_reflectionLDR": null,
|
||||
"_rotationAngle": 0
|
||||
},
|
||||
{
|
||||
"__type__": "cc.FogInfo",
|
||||
"_type": 0,
|
||||
"_fogColor": {
|
||||
"__type__": "cc.Color",
|
||||
"r": 200,
|
||||
"g": 200,
|
||||
"b": 200,
|
||||
"a": 255
|
||||
},
|
||||
"_enabled": false,
|
||||
"_fogDensity": 0.3,
|
||||
"_fogStart": 0.5,
|
||||
"_fogEnd": 300,
|
||||
"_fogAtten": 5,
|
||||
"_fogTop": 1.5,
|
||||
"_fogRange": 1.2,
|
||||
"_accurate": false
|
||||
},
|
||||
{
|
||||
"__type__": "cc.OctreeInfo",
|
||||
"_enabled": false,
|
||||
"_minPos": {
|
||||
"__type__": "cc.Vec3",
|
||||
"x": -1024,
|
||||
"y": -1024,
|
||||
"z": -1024
|
||||
},
|
||||
"_maxPos": {
|
||||
"__type__": "cc.Vec3",
|
||||
"x": 1024,
|
||||
"y": 1024,
|
||||
"z": 1024
|
||||
},
|
||||
"_depth": 8
|
||||
},
|
||||
{
|
||||
"__type__": "cc.SkinInfo",
|
||||
"_enabled": false,
|
||||
"_blurRadius": 0.01,
|
||||
"_sssIntensity": 3
|
||||
},
|
||||
{
|
||||
"__type__": "cc.LightProbeInfo",
|
||||
"_giScale": 1,
|
||||
"_giSamples": 1024,
|
||||
"_bounces": 2,
|
||||
"_reduceRinging": 0,
|
||||
"_showProbe": true,
|
||||
"_showWireframe": true,
|
||||
"_showConvex": false,
|
||||
"_data": null,
|
||||
"_lightProbeSphereVolume": 1
|
||||
},
|
||||
{
|
||||
"__type__": "cc.PostSettingsInfo",
|
||||
"_toneMappingType": 0
|
||||
}
|
||||
]
|
||||
11
bt-demo/assets/scene/GameEntry.scene.meta
Normal file
11
bt-demo/assets/scene/GameEntry.scene.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"ver": "1.1.50",
|
||||
"importer": "scene",
|
||||
"imported": true,
|
||||
"uuid": "bef93422-3e63-4c0f-a5cf-d926e7360673",
|
||||
"files": [
|
||||
".json"
|
||||
],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
9
bt-demo/assets/script.meta
Normal file
9
bt-demo/assets/script.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "1.2.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "448b0525-daf3-4ad4-be4a-04a7e181f028",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
118
bt-demo/assets/script/BTNode.ts
Normal file
118
bt-demo/assets/script/BTNode.ts
Normal file
@@ -0,0 +1,118 @@
|
||||
/**
|
||||
* @Author: Gongxh
|
||||
* @Date: 2025-09-17
|
||||
* @Description: 定义一些行为节点
|
||||
*/
|
||||
|
||||
import { sp } from "cc";
|
||||
import { BT } from "./Header";
|
||||
|
||||
@BT.ClassAction("BTAnimation", { name: "播放动画", group: "动画", desc: "通过动画名播放动画,播放完成后返回成功" })
|
||||
export class BTAnimation extends BT.LeafNode {
|
||||
@BT.prop({ type: BT.ParamType.string, description: "动画名" })
|
||||
private _name: string = "";
|
||||
|
||||
@BT.prop({ type: BT.ParamType.bool, description: "是否循环" })
|
||||
private _loop: boolean = false;
|
||||
|
||||
private _complete: boolean = false;
|
||||
|
||||
protected open(): void {
|
||||
super.open();
|
||||
this._complete = false;
|
||||
|
||||
console.log("open", this._name, this._loop);
|
||||
|
||||
let skeleton = this.getEntity<sp.Skeleton>();
|
||||
skeleton.setAnimation(0, this._name, this._loop);
|
||||
|
||||
if (!this._loop) {
|
||||
skeleton.setCompleteListener(() => {
|
||||
this._complete = true;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public tick(): BT.Status {
|
||||
if (!this._loop && this._complete) {
|
||||
return BT.Status.SUCCESS;
|
||||
}
|
||||
return BT.Status.RUNNING;
|
||||
}
|
||||
|
||||
protected close(): void {
|
||||
super.close();
|
||||
console.log("close", this._name, this._loop);
|
||||
}
|
||||
}
|
||||
|
||||
/** 条件节点 */
|
||||
@BT.ClassCondition("BTConditionRandom", { name: "随机条件节点", group: "基础条件节点", desc: "随机0-1的值,大于设置值返回成功,否则返回失败" })
|
||||
export class BTConditionRandom extends BT.Condition {
|
||||
|
||||
@BT.prop({ type: BT.ParamType.float, description: "值", defaultValue: 0.5 })
|
||||
private _value: number = 0.5;
|
||||
|
||||
public isEligible(): boolean {
|
||||
return Math.random() < this._value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/************************ 下方是几个编辑器中测试用的节点,删除即可 *************************/
|
||||
@BT.ClassAction("BTTestNode2", { name: "空行为节点", group: "测试", desc: "测试节点" })
|
||||
export class BTTestNode2 extends BT.LeafNode {
|
||||
public tick(): BT.Status {
|
||||
return BT.Status.SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
@BT.ClassAction("BTTestNode", { name: "嵌套数据测试节点", group: "测试", desc: "测试节点" })
|
||||
export class BTTestNode extends BT.LeafNode {
|
||||
@BT.prop({
|
||||
type: BT.ParamType.object,
|
||||
properties: {
|
||||
x: { type: BT.ParamType.int, min: 0 },
|
||||
y: { type: BT.ParamType.int, min: 0 }
|
||||
}
|
||||
})
|
||||
position: { x: number, y: number };
|
||||
|
||||
// 对象数组参数
|
||||
@BT.prop({
|
||||
type: BT.ParamType.array,
|
||||
itemType: BT.ParamType.object,
|
||||
itemProperties: {
|
||||
name: { type: BT.ParamType.string },
|
||||
value: { type: BT.ParamType.int }
|
||||
}
|
||||
})
|
||||
configs: Array<{ name: string, value: number }>;
|
||||
|
||||
public tick(): BT.Status {
|
||||
return BT.Status.SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
/** 条件节点 */
|
||||
@BT.ClassCondition("BTConditionTest", { name: "测试条件节点", group: "基础条件节点", desc: "" })
|
||||
export class BTConditionRandomTest extends BT.Condition {
|
||||
|
||||
public isEligible(): boolean {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** 条件装饰节点 */
|
||||
@BT.ClassDecorator("BTCondition", { name: "条件装饰节点", group: "基础装饰节点", desc: "随机0-1的值,大于设置值返回成功,否则返回失败" })
|
||||
export class BTCondition extends BT.ConditionDecorator {
|
||||
|
||||
@BT.prop({ type: BT.ParamType.float, description: "值" })
|
||||
private _value: number = 0.5;
|
||||
|
||||
public isEligible(): boolean {
|
||||
return Math.random() > this._value;
|
||||
}
|
||||
}
|
||||
9
bt-demo/assets/script/BTNode.ts.meta
Normal file
9
bt-demo/assets/script/BTNode.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "6c8cc47c-1976-432a-aa59-932cb74f41a2",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
23
bt-demo/assets/script/GameEntry.ts
Normal file
23
bt-demo/assets/script/GameEntry.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { _decorator, Component, JsonAsset, sp } from 'cc';
|
||||
import { BT } from './Header';
|
||||
const { ccclass, property, menu } = _decorator;
|
||||
@ccclass("GameEntry")
|
||||
@menu("kunpo/GameEntry")
|
||||
export class GameEntry extends Component {
|
||||
@property(sp.Skeleton)
|
||||
private skeleton: sp.Skeleton = null;
|
||||
|
||||
@property(JsonAsset)
|
||||
private btConfig: JsonAsset = null;
|
||||
|
||||
private _tree: BT.BehaviorTree<sp.Skeleton> = null;
|
||||
start(): void {
|
||||
console.log("btConfig", this.btConfig);
|
||||
let btTree1: BT.INodeConfig[] = this.btConfig.json["bt-tree1"]
|
||||
this._tree = BT.createBehaviorTree(btTree1, this.skeleton);
|
||||
}
|
||||
|
||||
protected update(dt: number): void {
|
||||
this._tree.tick(dt);
|
||||
}
|
||||
}
|
||||
9
bt-demo/assets/script/GameEntry.ts.meta
Normal file
9
bt-demo/assets/script/GameEntry.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "e5804a9e-c17f-4df6-8a74-778687e2bec1",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
9
bt-demo/assets/script/Header.ts
Normal file
9
bt-demo/assets/script/Header.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
/**
|
||||
* @Author: Gongxh
|
||||
* @Date: 2025-09-17
|
||||
* @Description: 头文件
|
||||
*/
|
||||
|
||||
import * as BT from "kunpocc-behaviortree";
|
||||
export { BT };
|
||||
|
||||
9
bt-demo/assets/script/Header.ts.meta
Normal file
9
bt-demo/assets/script/Header.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "17dd8d23-3e47-454a-9e47-69e371273e3b",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
73
bt-demo/assets/script/Math.ts
Normal file
73
bt-demo/assets/script/Math.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
interface Math {
|
||||
/**
|
||||
* 限制值
|
||||
* @param value 当前值
|
||||
* @param min 最小值
|
||||
* @param max 最大值
|
||||
*/
|
||||
clampf(value: number, min: number, max: number): number;
|
||||
|
||||
/**
|
||||
* 随机从 min 到 max 的整数(包含min和max)
|
||||
* @param min
|
||||
* @param max
|
||||
*/
|
||||
rand(min: number, max: number): number;
|
||||
|
||||
/**
|
||||
* 随机从 min 到 max的数
|
||||
* @param min
|
||||
* @param max
|
||||
*/
|
||||
randRange(min: number, max: number): number;
|
||||
|
||||
/**
|
||||
* 角度转弧度
|
||||
* @param angle 角度
|
||||
*/
|
||||
rad(angle: number): number;
|
||||
|
||||
/**
|
||||
* 弧度转角度
|
||||
* @param radian 弧度
|
||||
*/
|
||||
deg(radian: number): number;
|
||||
|
||||
/**
|
||||
* 数值平滑渐变
|
||||
* @param num1
|
||||
* @param num2
|
||||
* @param elapsedTime
|
||||
* @param responseTime
|
||||
*/
|
||||
smooth(num1: number, num2: number, elapsedTime: number, responseTime: number): number;
|
||||
}
|
||||
|
||||
Math.clampf = function (value: number, min: number, max: number): number {
|
||||
return Math.min(Math.max(value, min), max);
|
||||
};
|
||||
|
||||
Math.rand = function (min: number, max: number): number {
|
||||
return Math.floor(Math.random() * (max - min + 1) + min);
|
||||
};
|
||||
|
||||
Math.randRange = function (min: number, max: number): number {
|
||||
return Math.random() * (max - min) + min;
|
||||
};
|
||||
|
||||
Math.rad = function (angle: number): number {
|
||||
return (angle * Math.PI) / 180;
|
||||
};
|
||||
|
||||
Math.deg = function (radian: number): number {
|
||||
return (radian * 180) / Math.PI;
|
||||
};
|
||||
|
||||
Math.smooth = function (num1: number, num2: number, elapsedTime: number, responseTime: number): number {
|
||||
let out: number = num1;
|
||||
if (elapsedTime > 0) {
|
||||
out = out + (num2 - num1) * (elapsedTime / (elapsedTime + responseTime));
|
||||
}
|
||||
return out;
|
||||
};
|
||||
|
||||
9
bt-demo/assets/script/Math.ts.meta
Normal file
9
bt-demo/assets/script/Math.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.24",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "a336ce23-5d73-4280-b2e9-084389a3877e",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
1
bt-demo/extensions-config/.gitignore
vendored
Normal file
1
bt-demo/extensions-config/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
local/
|
||||
337
bt-demo/extensions-config/bt-editor/bt-tree1.json
Normal file
337
bt-demo/extensions-config/bt-editor/bt-tree1.json
Normal file
@@ -0,0 +1,337 @@
|
||||
{
|
||||
"name": "bt-tree2",
|
||||
"description": "这是一个描述",
|
||||
"nodes": [
|
||||
{
|
||||
"id": "1759488688188_qejfcso50",
|
||||
"className": "Selector",
|
||||
"name": "选择节点",
|
||||
"position": {
|
||||
"x": -60,
|
||||
"y": -200
|
||||
},
|
||||
"parameters": {},
|
||||
"children": [
|
||||
"1759488707759_2bmdm1fqt",
|
||||
"1759488725107_v8u160t95",
|
||||
"1759488737637_axpz9aqaz",
|
||||
"1759482034741_cf3mqaqdj",
|
||||
"1758190139303_t5o7vv3ak"
|
||||
],
|
||||
"alias": "根选择节点"
|
||||
},
|
||||
{
|
||||
"id": "1758190139303_t5o7vv3ak",
|
||||
"className": "BTTestNode",
|
||||
"name": "嵌套数据测试节点",
|
||||
"position": {
|
||||
"x": 440,
|
||||
"y": -80
|
||||
},
|
||||
"parameters": {
|
||||
"position": {
|
||||
"x": 10,
|
||||
"y": 20
|
||||
},
|
||||
"configs": [
|
||||
{
|
||||
"name": "hahaa",
|
||||
"value": 1
|
||||
}
|
||||
]
|
||||
},
|
||||
"children": []
|
||||
},
|
||||
{
|
||||
"id": "1759479318405_bptb8ltcp",
|
||||
"className": "LimitTime",
|
||||
"name": "时间限制器",
|
||||
"position": {
|
||||
"x": -120,
|
||||
"y": 40
|
||||
},
|
||||
"parameters": {
|
||||
"_max": 2
|
||||
},
|
||||
"children": [
|
||||
"1758089736854_t55n54hkh"
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "1759479295671_jflit2ek8",
|
||||
"className": "LimitTime",
|
||||
"name": "时间限制器",
|
||||
"position": {
|
||||
"x": -400,
|
||||
"y": 40
|
||||
},
|
||||
"parameters": {
|
||||
"_max": 2
|
||||
},
|
||||
"children": [
|
||||
"1758089659917_vjumiu9hy"
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "1758089659917_vjumiu9hy",
|
||||
"className": "BTAnimation",
|
||||
"name": "播放动画",
|
||||
"position": {
|
||||
"x": -400,
|
||||
"y": 160
|
||||
},
|
||||
"parameters": {
|
||||
"_name": "walk",
|
||||
"_loop": true
|
||||
},
|
||||
"children": []
|
||||
},
|
||||
{
|
||||
"id": "1758089736854_t55n54hkh",
|
||||
"className": "BTAnimation",
|
||||
"name": "播放动画",
|
||||
"position": {
|
||||
"x": -120,
|
||||
"y": 160
|
||||
},
|
||||
"parameters": {
|
||||
"_name": "run",
|
||||
"_loop": true
|
||||
},
|
||||
"children": []
|
||||
},
|
||||
{
|
||||
"id": "1758089757615_dp9tw9ka1",
|
||||
"className": "BTAnimation",
|
||||
"name": "播放动画",
|
||||
"position": {
|
||||
"x": 160,
|
||||
"y": 40
|
||||
},
|
||||
"parameters": {
|
||||
"_name": "jump",
|
||||
"_loop": false
|
||||
},
|
||||
"children": []
|
||||
},
|
||||
{
|
||||
"id": "1759478407706_w30m4btux",
|
||||
"className": "BTAnimation",
|
||||
"name": "播放动画",
|
||||
"position": {
|
||||
"x": 300,
|
||||
"y": 40
|
||||
},
|
||||
"parameters": {
|
||||
"_name": "idle",
|
||||
"_loop": true
|
||||
},
|
||||
"children": []
|
||||
},
|
||||
{
|
||||
"id": "1759481172259_xou25wj2n",
|
||||
"className": "BTConditionRandom",
|
||||
"name": "随机条件节点",
|
||||
"position": {
|
||||
"x": -540,
|
||||
"y": 40
|
||||
},
|
||||
"parameters": {
|
||||
"_value": 0.3
|
||||
},
|
||||
"children": []
|
||||
},
|
||||
{
|
||||
"id": "1759481282875_5orqavi5y",
|
||||
"className": "BTConditionRandom",
|
||||
"name": "随机条件节点",
|
||||
"position": {
|
||||
"x": -260,
|
||||
"y": 40
|
||||
},
|
||||
"parameters": {
|
||||
"_value": 0.4
|
||||
},
|
||||
"children": []
|
||||
},
|
||||
{
|
||||
"id": "1759481307863_ja6q4q9bz",
|
||||
"className": "BTConditionRandom",
|
||||
"name": "随机条件节点",
|
||||
"position": {
|
||||
"x": 20,
|
||||
"y": 40
|
||||
},
|
||||
"parameters": {
|
||||
"_value": 0.3
|
||||
},
|
||||
"children": []
|
||||
},
|
||||
{
|
||||
"id": "1759482034741_cf3mqaqdj",
|
||||
"className": "LimitTime",
|
||||
"name": "时间限制器",
|
||||
"position": {
|
||||
"x": 300,
|
||||
"y": -80
|
||||
},
|
||||
"parameters": {
|
||||
"_max": 2
|
||||
},
|
||||
"children": [
|
||||
"1759478407706_w30m4btux"
|
||||
],
|
||||
"alias": "待机动画"
|
||||
},
|
||||
{
|
||||
"id": "1759488707759_2bmdm1fqt",
|
||||
"className": "Sequence",
|
||||
"name": "顺序节点",
|
||||
"position": {
|
||||
"x": -480,
|
||||
"y": -80
|
||||
},
|
||||
"parameters": {},
|
||||
"children": [
|
||||
"1759481172259_xou25wj2n",
|
||||
"1759479295671_jflit2ek8"
|
||||
],
|
||||
"alias": "行走动画分支"
|
||||
},
|
||||
{
|
||||
"id": "1759488725107_v8u160t95",
|
||||
"className": "Sequence",
|
||||
"name": "顺序节点",
|
||||
"position": {
|
||||
"x": -200,
|
||||
"y": -80
|
||||
},
|
||||
"parameters": {},
|
||||
"children": [
|
||||
"1759481282875_5orqavi5y",
|
||||
"1759479318405_bptb8ltcp"
|
||||
],
|
||||
"alias": "奔跑动画"
|
||||
},
|
||||
{
|
||||
"id": "1759488737637_axpz9aqaz",
|
||||
"className": "Sequence",
|
||||
"name": "顺序节点",
|
||||
"position": {
|
||||
"x": 80,
|
||||
"y": -80
|
||||
},
|
||||
"parameters": {},
|
||||
"children": [
|
||||
"1759481307863_ja6q4q9bz",
|
||||
"1758089757615_dp9tw9ka1"
|
||||
],
|
||||
"alias": "跳跃动画分支"
|
||||
}
|
||||
],
|
||||
"connections": [
|
||||
{
|
||||
"id": "conn_1759479306749_wnwlz1638",
|
||||
"sourceNodeId": "1759479295671_jflit2ek8",
|
||||
"targetNodeId": "1758089659917_vjumiu9hy",
|
||||
"sourcePointType": "child",
|
||||
"targetPointType": "parent"
|
||||
},
|
||||
{
|
||||
"id": "conn_1759479325803_ln42r7198",
|
||||
"sourceNodeId": "1759479318405_bptb8ltcp",
|
||||
"targetNodeId": "1758089736854_t55n54hkh",
|
||||
"sourcePointType": "child",
|
||||
"targetPointType": "parent"
|
||||
},
|
||||
{
|
||||
"id": "conn_1759482041141_ok8gnqp0o",
|
||||
"sourceNodeId": "1759482034741_cf3mqaqdj",
|
||||
"targetNodeId": "1759478407706_w30m4btux",
|
||||
"sourcePointType": "child",
|
||||
"targetPointType": "parent"
|
||||
},
|
||||
{
|
||||
"id": "conn_1759488698126_dou7vxvo0",
|
||||
"sourceNodeId": "1759488688188_qejfcso50",
|
||||
"targetNodeId": "1759482034741_cf3mqaqdj",
|
||||
"sourcePointType": "child",
|
||||
"targetPointType": "parent"
|
||||
},
|
||||
{
|
||||
"id": "conn_1759488712210_gn0eom3zv",
|
||||
"sourceNodeId": "1759488688188_qejfcso50",
|
||||
"targetNodeId": "1759488707759_2bmdm1fqt",
|
||||
"sourcePointType": "child",
|
||||
"targetPointType": "parent"
|
||||
},
|
||||
{
|
||||
"id": "conn_1759488719553_mag45k9dx",
|
||||
"sourceNodeId": "1759488707759_2bmdm1fqt",
|
||||
"targetNodeId": "1759481172259_xou25wj2n",
|
||||
"sourcePointType": "child",
|
||||
"targetPointType": "parent"
|
||||
},
|
||||
{
|
||||
"id": "conn_1759488720799_ksfwij12z",
|
||||
"sourceNodeId": "1759488707759_2bmdm1fqt",
|
||||
"targetNodeId": "1759479295671_jflit2ek8",
|
||||
"sourcePointType": "child",
|
||||
"targetPointType": "parent"
|
||||
},
|
||||
{
|
||||
"id": "conn_1759488728262_shaymep9m",
|
||||
"sourceNodeId": "1759488688188_qejfcso50",
|
||||
"targetNodeId": "1759488725107_v8u160t95",
|
||||
"sourcePointType": "child",
|
||||
"targetPointType": "parent"
|
||||
},
|
||||
{
|
||||
"id": "conn_1759488732106_yg23eiw3l",
|
||||
"sourceNodeId": "1759488725107_v8u160t95",
|
||||
"targetNodeId": "1759481282875_5orqavi5y",
|
||||
"sourcePointType": "child",
|
||||
"targetPointType": "parent"
|
||||
},
|
||||
{
|
||||
"id": "conn_1759488733833_08kf67zp1",
|
||||
"sourceNodeId": "1759488725107_v8u160t95",
|
||||
"targetNodeId": "1759479318405_bptb8ltcp",
|
||||
"sourcePointType": "child",
|
||||
"targetPointType": "parent"
|
||||
},
|
||||
{
|
||||
"id": "conn_1759488741448_2in7yz3v7",
|
||||
"sourceNodeId": "1759488688188_qejfcso50",
|
||||
"targetNodeId": "1759488737637_axpz9aqaz",
|
||||
"sourcePointType": "child",
|
||||
"targetPointType": "parent"
|
||||
},
|
||||
{
|
||||
"id": "conn_1759488742689_15z7fchvc",
|
||||
"sourceNodeId": "1759488737637_axpz9aqaz",
|
||||
"targetNodeId": "1759481307863_ja6q4q9bz",
|
||||
"sourcePointType": "child",
|
||||
"targetPointType": "parent"
|
||||
},
|
||||
{
|
||||
"id": "conn_1759488745870_5rllaj2oe",
|
||||
"sourceNodeId": "1759488737637_axpz9aqaz",
|
||||
"targetNodeId": "1758089757615_dp9tw9ka1",
|
||||
"sourcePointType": "child",
|
||||
"targetPointType": "parent"
|
||||
},
|
||||
{
|
||||
"id": "conn_1758204108181_90iaioyvg",
|
||||
"sourceNodeId": "1759488688188_qejfcso50",
|
||||
"targetNodeId": "1758190139303_t5o7vv3ak",
|
||||
"sourcePointType": "child",
|
||||
"targetPointType": "parent"
|
||||
}
|
||||
],
|
||||
"canvasScale": 1.0006385665653545,
|
||||
"canvasOffset": {
|
||||
"x": 584.9936143343465,
|
||||
"y": 498.99074078480237
|
||||
}
|
||||
}
|
||||
68
bt-demo/extensions-config/bt-editor/bt-tree2.json
Normal file
68
bt-demo/extensions-config/bt-editor/bt-tree2.json
Normal file
@@ -0,0 +1,68 @@
|
||||
{
|
||||
"name": "bt-tree2",
|
||||
"description": "",
|
||||
"nodes": [
|
||||
{
|
||||
"id": "1758206972710_bhxebhy7o",
|
||||
"className": "Sequence",
|
||||
"name": "顺序节点",
|
||||
"position": {
|
||||
"x": 80,
|
||||
"y": -320
|
||||
},
|
||||
"parameters": {},
|
||||
"children": [
|
||||
"1758090634327_mf36nwkdt"
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "1758090634327_mf36nwkdt",
|
||||
"className": "Selector",
|
||||
"name": "选择节点",
|
||||
"position": {
|
||||
"x": -80,
|
||||
"y": -220
|
||||
},
|
||||
"parameters": {},
|
||||
"children": [
|
||||
"1758206988178_55b7kk5va"
|
||||
],
|
||||
"alias": "是的发放是的发放"
|
||||
},
|
||||
{
|
||||
"id": "1758206988178_55b7kk5va",
|
||||
"className": "BTAnimation",
|
||||
"name": "播放动画",
|
||||
"position": {
|
||||
"x": -20,
|
||||
"y": -40
|
||||
},
|
||||
"parameters": {
|
||||
"_name": "",
|
||||
"_loop": false
|
||||
},
|
||||
"children": []
|
||||
}
|
||||
],
|
||||
"connections": [
|
||||
{
|
||||
"id": "conn_1758206976733_208tneycs",
|
||||
"sourceNodeId": "1758206972710_bhxebhy7o",
|
||||
"targetNodeId": "1758090634327_mf36nwkdt",
|
||||
"sourcePointType": "child",
|
||||
"targetPointType": "parent"
|
||||
},
|
||||
{
|
||||
"id": "conn_1758206989897_46hw88z7h",
|
||||
"sourceNodeId": "1758090634327_mf36nwkdt",
|
||||
"targetNodeId": "1758206988178_55b7kk5va",
|
||||
"sourcePointType": "child",
|
||||
"targetPointType": "parent"
|
||||
}
|
||||
],
|
||||
"canvasScale": 1.139190980775211,
|
||||
"canvasOffset": {
|
||||
"x": 549.4323607689915,
|
||||
"y": 698.6185343759718
|
||||
}
|
||||
}
|
||||
331
bt-demo/extensions-config/bt-editor/bttest.json
Normal file
331
bt-demo/extensions-config/bt-editor/bttest.json
Normal file
@@ -0,0 +1,331 @@
|
||||
{
|
||||
"name": "bttest",
|
||||
"description": "死亡顺序节点",
|
||||
"nodes": [
|
||||
{
|
||||
"id": "1758261718850_lh2zeww5x",
|
||||
"className": "Selector",
|
||||
"name": "选择节点",
|
||||
"position": {
|
||||
"x": -60,
|
||||
"y": -220
|
||||
},
|
||||
"parameters": {},
|
||||
"children": [
|
||||
"1758523039812_tjcddh9ze",
|
||||
"1758253809172_7ug7k3z91",
|
||||
"1758363111204_lop2a6plc",
|
||||
"1758523349295_96r7men3n"
|
||||
],
|
||||
"alias": "根选择节点"
|
||||
},
|
||||
{
|
||||
"id": "1758253809172_7ug7k3z91",
|
||||
"className": "Sequence",
|
||||
"name": "顺序节点",
|
||||
"position": {
|
||||
"x": -60,
|
||||
"y": -120
|
||||
},
|
||||
"parameters": {},
|
||||
"children": [
|
||||
"1758253982404_6rhda0crn",
|
||||
"1758363223180_wgl2lftj9"
|
||||
],
|
||||
"alias": "战斗顺序节点"
|
||||
},
|
||||
{
|
||||
"id": "1758253982404_6rhda0crn",
|
||||
"className": "BTConditionTest",
|
||||
"name": "测试条件节点",
|
||||
"position": {
|
||||
"x": -260,
|
||||
"y": -60
|
||||
},
|
||||
"parameters": {},
|
||||
"children": [],
|
||||
"alias": "攻击范围内"
|
||||
},
|
||||
{
|
||||
"id": "1758363111204_lop2a6plc",
|
||||
"className": "Sequence",
|
||||
"name": "顺序节点",
|
||||
"position": {
|
||||
"x": 360,
|
||||
"y": -120
|
||||
},
|
||||
"parameters": {},
|
||||
"children": [
|
||||
"1758523389760_eimzn4sqi",
|
||||
"1758523381506_arxf3pn6e"
|
||||
],
|
||||
"alias": "巡逻顺序节点"
|
||||
},
|
||||
{
|
||||
"id": "1758363223180_wgl2lftj9",
|
||||
"className": "Selector",
|
||||
"name": "选择节点",
|
||||
"position": {
|
||||
"x": 20,
|
||||
"y": -40
|
||||
},
|
||||
"parameters": {},
|
||||
"children": [
|
||||
"1758371105178_0cdpe0b8s",
|
||||
"1758371282480_wtl4l8yy4"
|
||||
],
|
||||
"alias": "攻击选择"
|
||||
},
|
||||
{
|
||||
"id": "1758371105178_0cdpe0b8s",
|
||||
"className": "Sequence",
|
||||
"name": "顺序节点",
|
||||
"position": {
|
||||
"x": -60,
|
||||
"y": 60
|
||||
},
|
||||
"parameters": {},
|
||||
"children": [
|
||||
"1758371168774_oeixpztqv",
|
||||
"1758371186379_nl05q6e4w"
|
||||
],
|
||||
"alias": "技能攻击"
|
||||
},
|
||||
{
|
||||
"id": "1758371168774_oeixpztqv",
|
||||
"className": "BTConditionTest",
|
||||
"name": "测试条件节点",
|
||||
"position": {
|
||||
"x": -120,
|
||||
"y": 120
|
||||
},
|
||||
"parameters": {},
|
||||
"children": [],
|
||||
"alias": "可以释放技能"
|
||||
},
|
||||
{
|
||||
"id": "1758371186379_nl05q6e4w",
|
||||
"className": "BTTestNode2",
|
||||
"name": "空行为节点",
|
||||
"position": {
|
||||
"x": 20,
|
||||
"y": 140
|
||||
},
|
||||
"parameters": {},
|
||||
"children": [],
|
||||
"alias": "释放技能"
|
||||
},
|
||||
{
|
||||
"id": "1758371282480_wtl4l8yy4",
|
||||
"className": "BTTestNode2",
|
||||
"name": "空行为节点",
|
||||
"position": {
|
||||
"x": 160,
|
||||
"y": 60
|
||||
},
|
||||
"parameters": {},
|
||||
"children": [],
|
||||
"alias": "普通攻击"
|
||||
},
|
||||
{
|
||||
"id": "1758523039812_tjcddh9ze",
|
||||
"className": "Sequence",
|
||||
"name": "顺序节点",
|
||||
"position": {
|
||||
"x": -540,
|
||||
"y": -120
|
||||
},
|
||||
"parameters": {},
|
||||
"children": [
|
||||
"1758523078993_5vt56w1fv",
|
||||
"1758523095101_kc0taam2a",
|
||||
"1758523118932_tv2q9zeij"
|
||||
],
|
||||
"alias": "死亡顺序节点"
|
||||
},
|
||||
{
|
||||
"id": "1758523078993_5vt56w1fv",
|
||||
"className": "BTConditionTest",
|
||||
"name": "测试条件节点",
|
||||
"position": {
|
||||
"x": -680,
|
||||
"y": -60
|
||||
},
|
||||
"parameters": {},
|
||||
"children": [],
|
||||
"alias": "血量小于0"
|
||||
},
|
||||
{
|
||||
"id": "1758523095101_kc0taam2a",
|
||||
"className": "BTTestNode2",
|
||||
"name": "空行为节点",
|
||||
"position": {
|
||||
"x": -540,
|
||||
"y": -40
|
||||
},
|
||||
"parameters": {},
|
||||
"children": [],
|
||||
"alias": "播放死亡动画"
|
||||
},
|
||||
{
|
||||
"id": "1758523118932_tv2q9zeij",
|
||||
"className": "BTTestNode2",
|
||||
"name": "空行为节点",
|
||||
"position": {
|
||||
"x": -400,
|
||||
"y": -40
|
||||
},
|
||||
"parameters": {},
|
||||
"children": [],
|
||||
"alias": "删除实体"
|
||||
},
|
||||
{
|
||||
"id": "1758523349295_96r7men3n",
|
||||
"className": "BTTestNode2",
|
||||
"name": "空行为节点",
|
||||
"position": {
|
||||
"x": 580,
|
||||
"y": -120
|
||||
},
|
||||
"parameters": {},
|
||||
"children": [],
|
||||
"alias": "随便走走"
|
||||
},
|
||||
{
|
||||
"id": "1758523381506_arxf3pn6e",
|
||||
"className": "BTTestNode2",
|
||||
"name": "空行为节点",
|
||||
"position": {
|
||||
"x": 440,
|
||||
"y": -40
|
||||
},
|
||||
"parameters": {},
|
||||
"children": [],
|
||||
"alias": "追击敌人"
|
||||
},
|
||||
{
|
||||
"id": "1758523389760_eimzn4sqi",
|
||||
"className": "BTConditionTest",
|
||||
"name": "测试条件节点",
|
||||
"position": {
|
||||
"x": 300,
|
||||
"y": -60
|
||||
},
|
||||
"parameters": {},
|
||||
"children": [],
|
||||
"alias": "发现敌人"
|
||||
}
|
||||
],
|
||||
"connections": [
|
||||
{
|
||||
"id": "conn_1758253994001_5wea6k553",
|
||||
"sourceNodeId": "1758253809172_7ug7k3z91",
|
||||
"targetNodeId": "1758253982404_6rhda0crn",
|
||||
"sourcePointType": "child",
|
||||
"targetPointType": "parent"
|
||||
},
|
||||
{
|
||||
"id": "conn_1758261733816_q28lthyfv",
|
||||
"sourceNodeId": "1758261718850_lh2zeww5x",
|
||||
"targetNodeId": "1758253809172_7ug7k3z91",
|
||||
"sourcePointType": "child",
|
||||
"targetPointType": "parent"
|
||||
},
|
||||
{
|
||||
"id": "conn_1758363162954_dj8hnv7wt",
|
||||
"sourceNodeId": "1758261718850_lh2zeww5x",
|
||||
"targetNodeId": "1758363111204_lop2a6plc",
|
||||
"sourcePointType": "child",
|
||||
"targetPointType": "parent"
|
||||
},
|
||||
{
|
||||
"id": "conn_1758363226473_93afkajso",
|
||||
"sourceNodeId": "1758253809172_7ug7k3z91",
|
||||
"targetNodeId": "1758363223180_wgl2lftj9",
|
||||
"sourcePointType": "child",
|
||||
"targetPointType": "parent"
|
||||
},
|
||||
{
|
||||
"id": "conn_1758371112294_k4do2tfeq",
|
||||
"sourceNodeId": "1758363223180_wgl2lftj9",
|
||||
"targetNodeId": "1758371105178_0cdpe0b8s",
|
||||
"sourcePointType": "child",
|
||||
"targetPointType": "parent"
|
||||
},
|
||||
{
|
||||
"id": "conn_1758371171824_ltxuzvkkw",
|
||||
"sourceNodeId": "1758371105178_0cdpe0b8s",
|
||||
"targetNodeId": "1758371168774_oeixpztqv",
|
||||
"sourcePointType": "child",
|
||||
"targetPointType": "parent"
|
||||
},
|
||||
{
|
||||
"id": "conn_1758371193389_atj5h7bca",
|
||||
"sourceNodeId": "1758371105178_0cdpe0b8s",
|
||||
"targetNodeId": "1758371186379_nl05q6e4w",
|
||||
"sourcePointType": "child",
|
||||
"targetPointType": "parent"
|
||||
},
|
||||
{
|
||||
"id": "conn_1758523042573_2gbahqv3s",
|
||||
"sourceNodeId": "1758261718850_lh2zeww5x",
|
||||
"targetNodeId": "1758523039812_tjcddh9ze",
|
||||
"sourcePointType": "child",
|
||||
"targetPointType": "parent"
|
||||
},
|
||||
{
|
||||
"id": "conn_1758523080725_xqtkpgq2z",
|
||||
"sourceNodeId": "1758523039812_tjcddh9ze",
|
||||
"targetNodeId": "1758523078993_5vt56w1fv",
|
||||
"sourcePointType": "child",
|
||||
"targetPointType": "parent"
|
||||
},
|
||||
{
|
||||
"id": "conn_1758523097500_azabfun5e",
|
||||
"sourceNodeId": "1758523039812_tjcddh9ze",
|
||||
"targetNodeId": "1758523095101_kc0taam2a",
|
||||
"sourcePointType": "child",
|
||||
"targetPointType": "parent"
|
||||
},
|
||||
{
|
||||
"id": "conn_1758523121341_2j7mu0ja3",
|
||||
"sourceNodeId": "1758523039812_tjcddh9ze",
|
||||
"targetNodeId": "1758523118932_tv2q9zeij",
|
||||
"sourcePointType": "child",
|
||||
"targetPointType": "parent"
|
||||
},
|
||||
{
|
||||
"id": "conn_1758523303417_xm4pwy7dz",
|
||||
"sourceNodeId": "1758363223180_wgl2lftj9",
|
||||
"targetNodeId": "1758371282480_wtl4l8yy4",
|
||||
"sourcePointType": "child",
|
||||
"targetPointType": "parent"
|
||||
},
|
||||
{
|
||||
"id": "conn_1758523351101_9nhuxfajs",
|
||||
"sourceNodeId": "1758261718850_lh2zeww5x",
|
||||
"targetNodeId": "1758523349295_96r7men3n",
|
||||
"sourcePointType": "child",
|
||||
"targetPointType": "parent"
|
||||
},
|
||||
{
|
||||
"id": "conn_1758523391171_weiqaojvf",
|
||||
"sourceNodeId": "1758363111204_lop2a6plc",
|
||||
"targetNodeId": "1758523389760_eimzn4sqi",
|
||||
"sourcePointType": "child",
|
||||
"targetPointType": "parent"
|
||||
},
|
||||
{
|
||||
"id": "conn_1758523392747_mg2deaf3w",
|
||||
"sourceNodeId": "1758363111204_lop2a6plc",
|
||||
"targetNodeId": "1758523381506_arxf3pn6e",
|
||||
"sourcePointType": "child",
|
||||
"targetPointType": "parent"
|
||||
}
|
||||
],
|
||||
"canvasScale": 0.8315100681556811,
|
||||
"canvasOffset": {
|
||||
"x": 669.6848993184432,
|
||||
"y": 527.9726510223352
|
||||
}
|
||||
}
|
||||
11
bt-demo/package.json
Executable file
11
bt-demo/package.json
Executable file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"name": "kunpocreator",
|
||||
"uuid": "f5d24040-9cd4-4a5a-8559-38bf55e621f7",
|
||||
"creator": {
|
||||
"version": "3.8.6"
|
||||
},
|
||||
"dependencies": {
|
||||
"kunpocc-behaviortree": "^0.1.1",
|
||||
"ts-node": "^10.9.2"
|
||||
}
|
||||
}
|
||||
80
bt-demo/settings/v2/packages/builder.json
Normal file
80
bt-demo/settings/v2/packages/builder.json
Normal file
@@ -0,0 +1,80 @@
|
||||
{
|
||||
"__version__": "1.3.9",
|
||||
"bundleConfig": {
|
||||
"custom": {
|
||||
"default": {
|
||||
"displayName": "i18n:builder.asset_bundle.defaultConfig",
|
||||
"configs": {
|
||||
"native": {
|
||||
"preferredOptions": {
|
||||
"isRemote": false,
|
||||
"compressionType": "merge_all_json"
|
||||
}
|
||||
},
|
||||
"web": {
|
||||
"preferredOptions": {
|
||||
"isRemote": false,
|
||||
"compressionType": "merge_dep"
|
||||
},
|
||||
"fallbackOptions": {
|
||||
"compressionType": "merge_dep"
|
||||
}
|
||||
},
|
||||
"miniGame": {
|
||||
"fallbackOptions": {
|
||||
"isRemote": true,
|
||||
"compressionType": "merge_all_json"
|
||||
},
|
||||
"configMode": "fallback",
|
||||
"overwriteSettings": {
|
||||
"alipay-mini-game": {
|
||||
"isRemote": true,
|
||||
"compressionType": "merge_all_json"
|
||||
},
|
||||
"bytedance-mini-game": {
|
||||
"isRemote": true,
|
||||
"compressionType": "merge_all_json"
|
||||
},
|
||||
"fb-instant-games": {
|
||||
"isRemote": true,
|
||||
"compressionType": "merge_all_json"
|
||||
},
|
||||
"huawei-quick-game": {
|
||||
"isRemote": true,
|
||||
"compressionType": "merge_all_json"
|
||||
},
|
||||
"migu-mini-game": {
|
||||
"isRemote": false,
|
||||
"compressionType": "merge_all_json"
|
||||
},
|
||||
"oppo-mini-game": {
|
||||
"isRemote": true,
|
||||
"compressionType": "merge_all_json"
|
||||
},
|
||||
"taobao-mini-game": {
|
||||
"isRemote": true,
|
||||
"compressionType": "merge_all_json"
|
||||
},
|
||||
"vivo-mini-game": {
|
||||
"isRemote": true,
|
||||
"compressionType": "merge_all_json"
|
||||
},
|
||||
"wechatgame": {
|
||||
"isRemote": true,
|
||||
"compressionType": "merge_all_json"
|
||||
},
|
||||
"xiaomi-quick-game": {
|
||||
"isRemote": true,
|
||||
"compressionType": "merge_all_json"
|
||||
},
|
||||
"taobao-creative-app": {
|
||||
"isRemote": true,
|
||||
"compressionType": "merge_all_json"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
23
bt-demo/settings/v2/packages/cocos-service.json
Normal file
23
bt-demo/settings/v2/packages/cocos-service.json
Normal file
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"__version__": "3.0.9",
|
||||
"game": {
|
||||
"name": "未知游戏",
|
||||
"app_id": "UNKNOW",
|
||||
"c_id": "0"
|
||||
},
|
||||
"appConfigMaps": [
|
||||
{
|
||||
"app_id": "UNKNOW",
|
||||
"config_id": "8c18cb"
|
||||
}
|
||||
],
|
||||
"configs": [
|
||||
{
|
||||
"app_id": "UNKNOW",
|
||||
"config_id": "8c18cb",
|
||||
"config_name": "Default",
|
||||
"config_remarks": "",
|
||||
"services": []
|
||||
}
|
||||
]
|
||||
}
|
||||
3
bt-demo/settings/v2/packages/device.json
Normal file
3
bt-demo/settings/v2/packages/device.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"__version__": "1.0.1"
|
||||
}
|
||||
451
bt-demo/settings/v2/packages/engine.json
Normal file
451
bt-demo/settings/v2/packages/engine.json
Normal file
@@ -0,0 +1,451 @@
|
||||
{
|
||||
"__version__": "1.0.12",
|
||||
"modules": {
|
||||
"graphics": {
|
||||
"pipeline": "legacy-pipeline"
|
||||
},
|
||||
"configs": {
|
||||
"migrationsConfig": {
|
||||
"cache": {
|
||||
"base": {
|
||||
"_value": true
|
||||
},
|
||||
"gfx-webgl": {
|
||||
"_value": true
|
||||
},
|
||||
"gfx-webgl2": {
|
||||
"_value": true
|
||||
},
|
||||
"gfx-webgpu": {
|
||||
"_value": false
|
||||
},
|
||||
"animation": {
|
||||
"_value": true
|
||||
},
|
||||
"skeletal-animation": {
|
||||
"_value": true
|
||||
},
|
||||
"3d": {
|
||||
"_value": false
|
||||
},
|
||||
"meshopt": {
|
||||
"_value": false
|
||||
},
|
||||
"2d": {
|
||||
"_value": true
|
||||
},
|
||||
"xr": {
|
||||
"_value": false
|
||||
},
|
||||
"rich-text": {
|
||||
"_value": true
|
||||
},
|
||||
"mask": {
|
||||
"_value": true
|
||||
},
|
||||
"graphics": {
|
||||
"_value": true
|
||||
},
|
||||
"ui-skew": {
|
||||
"_value": false
|
||||
},
|
||||
"affine-transform": {
|
||||
"_value": true
|
||||
},
|
||||
"ui": {
|
||||
"_value": true
|
||||
},
|
||||
"particle": {
|
||||
"_value": false
|
||||
},
|
||||
"physics": {
|
||||
"_value": false,
|
||||
"_option": "physics-ammo"
|
||||
},
|
||||
"physics-ammo": {
|
||||
"_value": false,
|
||||
"_flags": {
|
||||
"LOAD_BULLET_MANUALLY": false
|
||||
}
|
||||
},
|
||||
"physics-cannon": {
|
||||
"_value": false
|
||||
},
|
||||
"physics-physx": {
|
||||
"_value": false,
|
||||
"_flags": {
|
||||
"LOAD_PHYSX_MANUALLY": false
|
||||
}
|
||||
},
|
||||
"physics-builtin": {
|
||||
"_value": false
|
||||
},
|
||||
"physics-2d": {
|
||||
"_value": true,
|
||||
"_option": "physics-2d-builtin"
|
||||
},
|
||||
"physics-2d-box2d": {
|
||||
"_value": false
|
||||
},
|
||||
"physics-2d-box2d-wasm": {
|
||||
"_value": false,
|
||||
"_flags": {
|
||||
"LOAD_BOX2D_MANUALLY": false
|
||||
}
|
||||
},
|
||||
"physics-2d-builtin": {
|
||||
"_value": false
|
||||
},
|
||||
"physics-2d-box2d-jsb": {
|
||||
"_value": false
|
||||
},
|
||||
"intersection-2d": {
|
||||
"_value": true
|
||||
},
|
||||
"primitive": {
|
||||
"_value": false
|
||||
},
|
||||
"profiler": {
|
||||
"_value": true
|
||||
},
|
||||
"occlusion-query": {
|
||||
"_value": false
|
||||
},
|
||||
"geometry-renderer": {
|
||||
"_value": false
|
||||
},
|
||||
"debug-renderer": {
|
||||
"_value": false
|
||||
},
|
||||
"particle-2d": {
|
||||
"_value": true
|
||||
},
|
||||
"audio": {
|
||||
"_value": true
|
||||
},
|
||||
"video": {
|
||||
"_value": true
|
||||
},
|
||||
"webview": {
|
||||
"_value": true
|
||||
},
|
||||
"tween": {
|
||||
"_value": true
|
||||
},
|
||||
"websocket": {
|
||||
"_value": true
|
||||
},
|
||||
"websocket-server": {
|
||||
"_value": false
|
||||
},
|
||||
"terrain": {
|
||||
"_value": false
|
||||
},
|
||||
"light-probe": {
|
||||
"_value": false
|
||||
},
|
||||
"tiled-map": {
|
||||
"_value": false
|
||||
},
|
||||
"vendor-google": {
|
||||
"_value": false
|
||||
},
|
||||
"spine": {
|
||||
"_value": true,
|
||||
"_option": "spine-3.8"
|
||||
},
|
||||
"spine-3.8": {
|
||||
"_value": true,
|
||||
"_flags": {
|
||||
"LOAD_SPINE_MANUALLY": false
|
||||
}
|
||||
},
|
||||
"spine-4.2": {
|
||||
"_value": false,
|
||||
"_flags": {
|
||||
"LOAD_SPINE_MANUALLY": false
|
||||
}
|
||||
},
|
||||
"dragon-bones": {
|
||||
"_value": false
|
||||
},
|
||||
"marionette": {
|
||||
"_value": true
|
||||
},
|
||||
"procedural-animation": {
|
||||
"_value": true
|
||||
},
|
||||
"custom-pipeline-post-process": {
|
||||
"_value": false
|
||||
},
|
||||
"render-pipeline": {
|
||||
"_value": true,
|
||||
"_option": "legacy-pipeline"
|
||||
},
|
||||
"custom-pipeline": {
|
||||
"_value": true
|
||||
},
|
||||
"legacy-pipeline": {
|
||||
"_value": false
|
||||
}
|
||||
},
|
||||
"flags": {
|
||||
"LOAD_SPINE_MANUALLY": false
|
||||
},
|
||||
"name": "迁移生成的配置",
|
||||
"includeModules": [
|
||||
"2d",
|
||||
"affine-transform",
|
||||
"animation",
|
||||
"audio",
|
||||
"base",
|
||||
"gfx-webgl",
|
||||
"gfx-webgl2",
|
||||
"graphics",
|
||||
"intersection-2d",
|
||||
"legacy-pipeline",
|
||||
"marionette",
|
||||
"mask",
|
||||
"particle-2d",
|
||||
"physics-2d-builtin",
|
||||
"procedural-animation",
|
||||
"profiler",
|
||||
"rich-text",
|
||||
"skeletal-animation",
|
||||
"spine-3.8",
|
||||
"tween",
|
||||
"ui",
|
||||
"video",
|
||||
"websocket",
|
||||
"webview"
|
||||
],
|
||||
"noDeprecatedFeatures": {
|
||||
"value": true,
|
||||
"version": "<=3.8.0"
|
||||
}
|
||||
},
|
||||
"defaultConfig": {
|
||||
"name": "默认配置",
|
||||
"cache": {
|
||||
"base": {
|
||||
"_value": true
|
||||
},
|
||||
"gfx-webgl": {
|
||||
"_value": true
|
||||
},
|
||||
"gfx-webgl2": {
|
||||
"_value": false
|
||||
},
|
||||
"gfx-webgpu": {
|
||||
"_value": false
|
||||
},
|
||||
"animation": {
|
||||
"_value": true
|
||||
},
|
||||
"skeletal-animation": {
|
||||
"_value": true
|
||||
},
|
||||
"3d": {
|
||||
"_value": false
|
||||
},
|
||||
"meshopt": {
|
||||
"_value": false
|
||||
},
|
||||
"2d": {
|
||||
"_value": true
|
||||
},
|
||||
"xr": {
|
||||
"_value": false
|
||||
},
|
||||
"rich-text": {
|
||||
"_value": true
|
||||
},
|
||||
"mask": {
|
||||
"_value": true
|
||||
},
|
||||
"graphics": {
|
||||
"_value": true
|
||||
},
|
||||
"ui-skew": {
|
||||
"_value": false
|
||||
},
|
||||
"affine-transform": {
|
||||
"_value": true
|
||||
},
|
||||
"ui": {
|
||||
"_value": true
|
||||
},
|
||||
"particle": {
|
||||
"_value": false
|
||||
},
|
||||
"physics": {
|
||||
"_value": false,
|
||||
"_option": "physics-ammo"
|
||||
},
|
||||
"physics-ammo": {
|
||||
"_value": true,
|
||||
"_flags": {
|
||||
"LOAD_BULLET_MANUALLY": false
|
||||
}
|
||||
},
|
||||
"physics-cannon": {
|
||||
"_value": false
|
||||
},
|
||||
"physics-physx": {
|
||||
"_value": false,
|
||||
"_flags": {
|
||||
"LOAD_PHYSX_MANUALLY": false
|
||||
}
|
||||
},
|
||||
"physics-builtin": {
|
||||
"_value": false
|
||||
},
|
||||
"physics-2d": {
|
||||
"_value": true,
|
||||
"_option": "physics-2d-builtin"
|
||||
},
|
||||
"physics-2d-box2d": {
|
||||
"_value": true
|
||||
},
|
||||
"physics-2d-box2d-wasm": {
|
||||
"_value": false,
|
||||
"_flags": {
|
||||
"LOAD_BOX2D_MANUALLY": false
|
||||
}
|
||||
},
|
||||
"physics-2d-builtin": {
|
||||
"_value": false
|
||||
},
|
||||
"physics-2d-box2d-jsb": {
|
||||
"_value": false
|
||||
},
|
||||
"intersection-2d": {
|
||||
"_value": true
|
||||
},
|
||||
"primitive": {
|
||||
"_value": false
|
||||
},
|
||||
"profiler": {
|
||||
"_value": true
|
||||
},
|
||||
"occlusion-query": {
|
||||
"_value": false
|
||||
},
|
||||
"geometry-renderer": {
|
||||
"_value": false
|
||||
},
|
||||
"debug-renderer": {
|
||||
"_value": false
|
||||
},
|
||||
"particle-2d": {
|
||||
"_value": true
|
||||
},
|
||||
"audio": {
|
||||
"_value": true
|
||||
},
|
||||
"video": {
|
||||
"_value": true
|
||||
},
|
||||
"webview": {
|
||||
"_value": true
|
||||
},
|
||||
"tween": {
|
||||
"_value": true
|
||||
},
|
||||
"websocket": {
|
||||
"_value": true
|
||||
},
|
||||
"websocket-server": {
|
||||
"_value": false
|
||||
},
|
||||
"terrain": {
|
||||
"_value": false
|
||||
},
|
||||
"light-probe": {
|
||||
"_value": false
|
||||
},
|
||||
"tiled-map": {
|
||||
"_value": false
|
||||
},
|
||||
"vendor-google": {
|
||||
"_value": false
|
||||
},
|
||||
"spine": {
|
||||
"_value": true,
|
||||
"_option": "spine-3.8"
|
||||
},
|
||||
"spine-3.8": {
|
||||
"_value": true,
|
||||
"_flags": {
|
||||
"LOAD_SPINE_MANUALLY": false
|
||||
}
|
||||
},
|
||||
"spine-4.2": {
|
||||
"_value": false,
|
||||
"_flags": {
|
||||
"LOAD_SPINE_MANUALLY": false
|
||||
}
|
||||
},
|
||||
"dragon-bones": {
|
||||
"_value": false
|
||||
},
|
||||
"marionette": {
|
||||
"_value": true
|
||||
},
|
||||
"procedural-animation": {
|
||||
"_value": true
|
||||
},
|
||||
"custom-pipeline-post-process": {
|
||||
"_value": false
|
||||
},
|
||||
"render-pipeline": {
|
||||
"_value": true,
|
||||
"_option": "custom-pipeline"
|
||||
},
|
||||
"custom-pipeline": {
|
||||
"_value": true
|
||||
},
|
||||
"legacy-pipeline": {
|
||||
"_value": false
|
||||
}
|
||||
},
|
||||
"flags": {
|
||||
"LOAD_SPINE_MANUALLY": false
|
||||
},
|
||||
"includeModules": [
|
||||
"2d",
|
||||
"affine-transform",
|
||||
"animation",
|
||||
"audio",
|
||||
"base",
|
||||
"custom-pipeline",
|
||||
"gfx-webgl",
|
||||
"graphics",
|
||||
"intersection-2d",
|
||||
"marionette",
|
||||
"mask",
|
||||
"particle-2d",
|
||||
"physics-2d-builtin",
|
||||
"procedural-animation",
|
||||
"profiler",
|
||||
"rich-text",
|
||||
"skeletal-animation",
|
||||
"spine-3.8",
|
||||
"tween",
|
||||
"ui",
|
||||
"video",
|
||||
"websocket",
|
||||
"webview"
|
||||
],
|
||||
"noDeprecatedFeatures": {
|
||||
"value": true,
|
||||
"version": "<=3.8.0"
|
||||
}
|
||||
}
|
||||
},
|
||||
"globalConfigKey": "defaultConfig"
|
||||
},
|
||||
"macroConfig": {
|
||||
"BATCHER2D_MEM_INCREMENT": 288
|
||||
}
|
||||
}
|
||||
23
bt-demo/settings/v2/packages/information.json
Normal file
23
bt-demo/settings/v2/packages/information.json
Normal file
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"__version__": "1.0.1",
|
||||
"information": {
|
||||
"customSplash": {
|
||||
"id": "customSplash",
|
||||
"label": "customSplash",
|
||||
"enable": true,
|
||||
"customSplash": {
|
||||
"complete": false,
|
||||
"form": "https://creator-api.cocos.com/api/form/show?sid=39d299030f31eb42b71bc53d67bdc54e"
|
||||
}
|
||||
},
|
||||
"removeSplash": {
|
||||
"id": "removeSplash",
|
||||
"label": "removeSplash",
|
||||
"enable": true,
|
||||
"removeSplash": {
|
||||
"complete": false,
|
||||
"form": "https://creator-api.cocos.com/api/form/show?sid=39d299030f31eb42b71bc53d67bdc54e"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
bt-demo/settings/v2/packages/ios.json
Normal file
11
bt-demo/settings/v2/packages/ios.json
Normal file
File diff suppressed because one or more lines are too long
3
bt-demo/settings/v2/packages/program.json
Normal file
3
bt-demo/settings/v2/packages/program.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"__version__": "1.0.4"
|
||||
}
|
||||
29
bt-demo/settings/v2/packages/project.json
Normal file
29
bt-demo/settings/v2/packages/project.json
Normal file
@@ -0,0 +1,29 @@
|
||||
{
|
||||
"__version__": "1.0.6",
|
||||
"general": {
|
||||
"designResolution": {
|
||||
"height": 1334,
|
||||
"width": 750,
|
||||
"fitHeight": true
|
||||
},
|
||||
"highQuality": false
|
||||
},
|
||||
"custom_joint_texture_layouts": [],
|
||||
"script": {
|
||||
"preserveSymlinks": true
|
||||
},
|
||||
"layer": [
|
||||
{
|
||||
"name": "Window",
|
||||
"value": 1
|
||||
},
|
||||
{
|
||||
"name": "Game",
|
||||
"value": 2
|
||||
},
|
||||
{
|
||||
"name": "Graphics",
|
||||
"value": 4
|
||||
}
|
||||
]
|
||||
}
|
||||
4
bt-demo/settings/v2/packages/scene.json
Normal file
4
bt-demo/settings/v2/packages/scene.json
Normal file
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"__version__": "1.0.3",
|
||||
"current-scene": "bef93422-3e63-4c0f-a5cf-d926e7360673"
|
||||
}
|
||||
10
bt-demo/tsconfig.json
Normal file
10
bt-demo/tsconfig.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
/* Base configuration. Do not edit this field. */
|
||||
"extends": "./temp/tsconfig.cocos.json",
|
||||
/* Add your custom configuration here. */
|
||||
"compilerOptions": {
|
||||
"strict": false,
|
||||
"module": "ES6",
|
||||
"target": "ES6"
|
||||
}
|
||||
}
|
||||
335
docs/USED.md
Normal file
335
docs/USED.md
Normal file
@@ -0,0 +1,335 @@
|
||||
# 行为树使用指南
|
||||
|
||||

|
||||
|
||||
本指南将详细介绍如何使用 kunpocc-behaviortree 库和行为树编辑器。
|
||||
|
||||
## 一、开发环境
|
||||
- 引擎版本:Cocos Creator 3.8.6
|
||||
- 编程语言:TypeScript
|
||||
|
||||
|
||||
- 支持引擎版本:Cocos Creator 3.7+
|
||||
|
||||
|
||||
## 二、安装
|
||||
1. 安装 kunpocc-behaviortree
|
||||
```
|
||||
npm install kunpocc-behaviortree
|
||||
```
|
||||
|
||||
2. 下载扩展插件(bt-editor)
|
||||
|
||||
3. 项目脚本中引入库文件
|
||||
```typescript
|
||||
// 比如在项目代码目录下添加一个文件 header.ts
|
||||
// 写上如下代码
|
||||
import * as BT from "kunpocc-behaviortree";
|
||||
export { BT };
|
||||
```
|
||||
|
||||
4. 重启creator
|
||||
|
||||
5. 启用插件
|
||||
* 在 Cocos Creator 中选择 `扩展` -> `扩展管理器` -> `已安装扩展`
|
||||
* 找到 `bt-editor` 并启用
|
||||
|
||||
6. 打开扩展面板
|
||||
* 在 Cocos Creator 顶部菜单栏中选择 `kunpo` -> `行为树编辑器`
|
||||
|
||||
7. 创建第一颗行为树 (见下方 `导出文件使用` 部分)
|
||||
|
||||
|
||||
|
||||
## 三、自定义节点 (扩展编辑器节点池)
|
||||
|
||||
### 节点装饰器
|
||||
|
||||
装饰器系统是连接自定义节点和编辑器的桥梁
|
||||
只有通过装饰器装饰的节点,才能在编辑器中的节点池中显示
|
||||
|
||||
#### @ClassAction - 行为节点装饰器
|
||||
|
||||
用于装饰执行具体逻辑的叶子节点。
|
||||
|
||||
```typescript
|
||||
@BT.ClassAction("NodeName", { name: "显示名称", group: "节点分组", desc: "节点描述" })
|
||||
export class MyActionNode extends BT.LeafNode {
|
||||
public tick(): BT.Status {
|
||||
// 执行逻辑
|
||||
return BT.Status.SUCCESS;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### @ClassCondition - 条件节点装饰器
|
||||
|
||||
用于装饰判断条件的节点。
|
||||
|
||||
```typescript
|
||||
@BT.ClassCondition("ConditionName", { name: "条件名称", group: "条件分组", desc: "节点描述" })
|
||||
export class MyCondition extends BT.Condition {
|
||||
protected isEligible(): boolean {
|
||||
// 返回判断结果
|
||||
return true;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### @ClassComposite - 组合节点装饰器
|
||||
|
||||
用于装饰控制子节点执行流程的组合节点。
|
||||
|
||||
```typescript
|
||||
@BT.ClassComposite("CompositeName", { name: "组合名称", group: "组合分组", desc: "节点描述" })
|
||||
export class MyComposite extends BT.Composite {
|
||||
public tick(dt: number): BT.Status {
|
||||
// 控制子节点执行逻辑
|
||||
for (const child of this.children) {
|
||||
const status = child._execute(dt);
|
||||
if (status !== BT.Status.SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
}
|
||||
return BT.Status.SUCCESS;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### @ClassDecorator - 装饰节点装饰器
|
||||
|
||||
用于装饰修改单个子节点行为的装饰节点。
|
||||
|
||||
```typescript
|
||||
@BT.ClassDecorator("DecoratorName", { name: "装饰名称", group: "装饰分组", desc: "节点描述" })
|
||||
export class MyDecorator extends BT.Decorator {
|
||||
public tick(dt: number): BT.Status {
|
||||
// 装饰逻辑,修改子节点行为
|
||||
return this.children[0]._execute(dt);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 节点属性装饰器 (扩展节点参数)
|
||||
|
||||
为节点添加可在编辑器中配置的参数。
|
||||
|
||||
```typescript
|
||||
@BT.ClassAction("NodeName", { name: "显示名称", group: "节点分组", desc: "节点描述" })
|
||||
export class MyNode extends BT.LeafNode {
|
||||
// 基础类型参数
|
||||
@BT.prop({ type: BT.ParamType.string, description: "动画名称", defaultValue: "idle" })
|
||||
private animationName: string = "idle";
|
||||
|
||||
@BT.prop({ type: BT.ParamType.float, description: "速度", min: 0, max: 10, step: 0.1, defaultValue: 1.0 })
|
||||
private speed: number = 1.0;
|
||||
|
||||
@BT.prop({ type: BT.ParamType.bool, description: "是否循环" })
|
||||
private loop: boolean = false;
|
||||
|
||||
// 对象参数
|
||||
@BT.prop({
|
||||
type: BT.ParamType.object,
|
||||
description: "位置信息",
|
||||
properties: {
|
||||
x: { type: BT.ParamType.int, min: 0 },
|
||||
y: { type: BT.ParamType.int, min: 0 }
|
||||
}
|
||||
})
|
||||
private position: { x: number, y: number };
|
||||
|
||||
// 数组参数
|
||||
@BT.prop({
|
||||
type: BT.ParamType.array,
|
||||
description: "巡逻点列表",
|
||||
itemType: BT.ParamType.object,
|
||||
itemProperties: {
|
||||
x: { type: BT.ParamType.float },
|
||||
y: { type: BT.ParamType.float },
|
||||
name: { type: BT.ParamType.string }
|
||||
}
|
||||
})
|
||||
private patrolPoints: Array<{ x: number, y: number, name: string }>;
|
||||
}
|
||||
```
|
||||
|
||||
#### 参数类型详解
|
||||
|
||||
| 类型 | BT.ParamType | 描述 | 支持属性 |
|
||||
|------|--------------|------|----------|
|
||||
| 整数 | `int` | 整数类型 | `min`, `max`, `step`, `defaultValue` |
|
||||
| 浮点数 | `float` | 浮点数类型 | `min`, `max`, `step`, `defaultValue` |
|
||||
| 字符串 | `string` | 字符串类型 | `defaultValue` |
|
||||
| 布尔 | `bool` | 布尔类型 | `defaultValue` |
|
||||
| 对象 | `object` | 对象类型 | `properties` |
|
||||
| 数组 | `array` | 数组类型 | `itemType`, `itemProperties` |
|
||||
|
||||
|
||||
## 四、编辑器介绍
|
||||
|
||||
#### 快捷键
|
||||
|
||||
- **打开编辑器**: `Ctrl+Shift+K` (Windows) / `Cmd+Shift+K` (Mac)
|
||||
- **导出配置**: `Ctrl+Shift+L` (Windows) / `Cmd+Shift+L` (Mac)
|
||||
|
||||
#### 菜单访问
|
||||
|
||||
在 Cocos Creator 顶部菜单栏中选择 `kunpo` -> `行为树编辑器`
|
||||
|
||||
|
||||
### 编辑器功能介绍
|
||||
|
||||
行为树编辑器提供了一个直观的可视化界面,让你可以轻松创建和管理复杂的行为树结构。
|
||||
|
||||
#### 核心功能
|
||||
|
||||
##### 可视化节点编辑
|
||||
- **拖拽创建**:从左侧节点库拖拽节点到画布中
|
||||
- **分组管理**:节点按功能分组显示,便于查找
|
||||
- **实时预览**:节点显示类型图标和描述信息
|
||||
|
||||
##### 属性参数配置
|
||||
- **智能表单**:根据`@prop`装饰器自动生成配置界面
|
||||
- **类型校验**:支持数字、字符串、布尔值、对象、数组等类型
|
||||
- **默认值**:自动填充装饰器中定义的默认值
|
||||
- **约束验证**:支持最小值、最大值、步长等约束条件
|
||||
|
||||
##### 连接线管理
|
||||
- **可视连接**:通过拖拽连接点创建父子关系
|
||||
- **自动布局**:连接线自动避让,保持界面整洁
|
||||
- **连接验证**:防止创建非法的节点连接关系
|
||||
|
||||
##### 画布操作
|
||||
- **缩放平移**:鼠标滚轮缩放,拖拽平移画布
|
||||
- **多选操作**:支持框选多个节点进行批量操作
|
||||
|
||||
##### 节点管理
|
||||
- **别名设置**:为节点设置有意义的别名,便于理解
|
||||
- **复制粘贴**:快速复制节点及其子树结构
|
||||
- **删除操作**:删除节点时自动清理相关连接
|
||||
|
||||
##### 编辑器界面布局
|
||||
|
||||
```
|
||||
┌─────────────────────────────────────────────────────────────────────────────┐
|
||||
│ 顶部工具栏 │
|
||||
│ [设置导出路径] [过滤行为树] [选择行为树▼] [导出配置] │
|
||||
├─────────────┬─────────────────────────────────────────────┬─────────────────┤
|
||||
│ │ │ │
|
||||
│ 节点库 │ │ 右侧面板 │
|
||||
│ (左侧) │ │ │
|
||||
│ │ ┌───────────────────────────────────────┐ │ ┌──────────────┐│
|
||||
│ • 行为节点 │ │ 画布工具栏 │ │ │ 行为树名称 │ │
|
||||
│ • 条件节点 │ │ [缩放] [重置] [清空] [复制] [粘贴] │ │ │ 行为树描述 │ │
|
||||
│ • 组合节点 │ └─────────────────────────────────────────┘ │ └──────────────┘ │
|
||||
│ • 装饰节点 │ │ │
|
||||
│ • 内置节点 │ ┌─────────────────────────────────────────┐ │ ┌──────────────┐ │
|
||||
│ │ │ │ │ │ │ │
|
||||
│ │ │ 画布操作区 │ │ │ 节点参数区 │ │
|
||||
│ │ │ │ │ │ │ │
|
||||
│ │ │ ┌─────┐ │ │ │ • 节点名称 │ │
|
||||
│ │ │ │根节点│ │ │ │ • 参数配置 │ │
|
||||
│ │ │ └──┬──┘ │ │ │ • 描述信息 │ │
|
||||
│ │ │ │ │ │ │ • 别名设置 │ │
|
||||
│ │ │ ┌──▼──┐ ┌──────┐ │ │ │ │ │
|
||||
│ │ │ │子节点│ │子节点│ │ │ └──────────────┘ │
|
||||
│ │ │ └─────┘ └──────┘ │ │ │
|
||||
│ │ │ │ │ ┌──────────────┐ │
|
||||
│ │ │ │ │ │ [删除行为树] │ │
|
||||
│ │ └────────────────────────────────────────┘ │ │ [创建新树] │ │
|
||||
│ │ │ └──────────────┘ │
|
||||
└────────────┴──────────────────────────────────────────────┴──────────────────┘
|
||||
```
|
||||
|
||||
### 导出文件使用
|
||||
|
||||
#### 在项目中使用导出配置
|
||||
|
||||
##### 1. 配置文件格式
|
||||
|
||||
```json
|
||||
{
|
||||
"boss1": [
|
||||
{
|
||||
"id": "1758206972710_bhxebhy7o",
|
||||
"className": "Sequence",
|
||||
"parameters": {},
|
||||
"children": [
|
||||
"1758090634327_mf36nwkdt"
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "1758090634327_mf36nwkdt",
|
||||
"className": "Selector",
|
||||
"parameters": {},
|
||||
"children": [
|
||||
"1758206988178_55b7kk5va"
|
||||
]
|
||||
},
|
||||
{
|
||||
"id": "1758206988178_55b7kk5va",
|
||||
"className": "BTAnimation",
|
||||
"parameters": {
|
||||
"_name": "",
|
||||
"_loop": false
|
||||
},
|
||||
"children": []
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
##### 2. 配置文件放入项目资源目录
|
||||
将从编辑器导出的JSON文件放入项目资源目录
|
||||
自行加载配置数据
|
||||
|
||||
```
|
||||
assets/
|
||||
├── resources/
|
||||
│ └── config/
|
||||
│ ├── bt_config.json // 所有行为树的信息都在这个里边
|
||||
```
|
||||
|
||||
##### 3. 创建行为树实例
|
||||
|
||||
* BT.createBehaviorTree(config["boss1"], entity);
|
||||
|
||||
* 函数详解
|
||||
|
||||
```typescript
|
||||
// 工厂函数签名
|
||||
function createBehaviorTree<T>(config: INodeConfig[], entity: T): BehaviorTree<T>
|
||||
|
||||
// 内部工作流程:
|
||||
// 1. 验证配置格式
|
||||
// 2. 创建节点映射表 (id -> config)
|
||||
// 3. 递归创建节点树
|
||||
// - 通过className查找构造函数
|
||||
// - 根据节点类型选择创建方式
|
||||
// - 设置节点参数
|
||||
// - 建立父子关系
|
||||
// 4. 返回行为树实例
|
||||
```
|
||||
|
||||
## 五、更新声明
|
||||
|
||||
## 0.0.1 (2025-09-23)
|
||||
- 首版本
|
||||
|
||||
## 六、联系作者
|
||||
|
||||
* 邮箱: gong.xinhai@163.com
|
||||
* 微信: G0900901
|
||||
* 扫码加微信:
|
||||
|
||||
|
||||
## 七、版权声明
|
||||
此插件源代码可商业使用
|
||||
|
||||
商业授权范围仅限于在您自行开发的游戏作品中使用
|
||||
|
||||
不得进行任何形式的转售、租赁、传播等
|
||||
|
||||
|
||||
## 八、购买须知
|
||||
本产品为付费虚拟商品,一经购买成功概不退款,请在购买前谨慎确认购买内容。
|
||||
BIN
image/bt-gui.png
Normal file
BIN
image/bt-gui.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 370 KiB |
BIN
image/image_tree.png
Normal file
BIN
image/image_tree.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 122 KiB |
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "kunpocc-behaviortree",
|
||||
"version": "0.0.3",
|
||||
"version": "0.1.2",
|
||||
"description": "行为树",
|
||||
"main": "./dist/kunpocc-behaviortree.cjs",
|
||||
"module": "./dist/kunpocc-behaviortree.mjs",
|
||||
@@ -19,7 +19,8 @@
|
||||
},
|
||||
"scripts": {
|
||||
"clean": "rm -rf dist",
|
||||
"build": "npm run clean && rollup -c rollup.config.mjs"
|
||||
"copy": "cp -r dist/* ./bt-demo/node_modules/kunpocc-behaviortree/dist/",
|
||||
"build": "npm run clean && rollup -c rollup.config.mjs && npm run copy"
|
||||
},
|
||||
"files": [
|
||||
"dist/kunpocc-behaviortree.cjs",
|
||||
@@ -50,4 +51,4 @@
|
||||
"ts-node": "^10.9.2",
|
||||
"tslib": "^2.6.2"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ import dts from 'rollup-plugin-dts';
|
||||
export default [
|
||||
{
|
||||
// 生成未压缩的 JS 文件
|
||||
input: 'src/kunpocc-behaviortree.ts',
|
||||
input: 'src/index.ts',
|
||||
external: ['cc', 'fairygui-cc'],
|
||||
output: [
|
||||
{
|
||||
@@ -38,7 +38,7 @@ export default [
|
||||
},
|
||||
{
|
||||
// 生成压缩的 JS 文件
|
||||
input: 'src/kunpocc-behaviortree.ts',
|
||||
input: 'src/index.ts',
|
||||
external: ['cc', 'fairygui-cc'],
|
||||
output: [
|
||||
{
|
||||
@@ -72,7 +72,7 @@ export default [
|
||||
},
|
||||
{
|
||||
// 生成声明文件的配置
|
||||
input: 'src/kunpocc-behaviortree.ts',
|
||||
input: 'src/index.ts',
|
||||
output: {
|
||||
file: 'dist/kunpocc-behaviortree.d.ts',
|
||||
format: 'es'
|
||||
|
||||
224
src/behaviortree/BT.ts
Normal file
224
src/behaviortree/BT.ts
Normal file
@@ -0,0 +1,224 @@
|
||||
/**
|
||||
* 行为树装饰器和元数据管理
|
||||
* 用于编辑器显示和配置节点信息
|
||||
*/
|
||||
|
||||
export namespace BT {
|
||||
/**
|
||||
* 参数类型枚举
|
||||
*/
|
||||
export enum ParamType {
|
||||
int = "number",
|
||||
float = "float",
|
||||
string = "string",
|
||||
bool = "boolean",
|
||||
object = "object",
|
||||
array = "array"
|
||||
}
|
||||
|
||||
/**
|
||||
* 节点类型枚举
|
||||
*/
|
||||
export enum Type {
|
||||
/** 行为节点 */
|
||||
Action = "action",
|
||||
/** 条件节点 */
|
||||
Condition = "condition",
|
||||
/** 组合节点 */
|
||||
Composite = "composite",
|
||||
/** 装饰节点 */
|
||||
Decorator = "decorator"
|
||||
}
|
||||
|
||||
/**
|
||||
* 参数描述接口
|
||||
*/
|
||||
export interface ParameterInfo {
|
||||
/** 参数名称 */
|
||||
name: string;
|
||||
/** 参数类型 */
|
||||
type: ParamType;
|
||||
/** 参数描述 */
|
||||
description?: string;
|
||||
/** 默认值 */
|
||||
defaultValue?: any;
|
||||
/** 步进 针对数字类型的变更的最小单位 */
|
||||
step?: number,
|
||||
/** 最小值 */
|
||||
min?: number,
|
||||
/** 最大值 */
|
||||
max?: number,
|
||||
/** 对象属性定义 - 仅当type为object时使用 */
|
||||
properties?: { [key: string]: Omit<ParameterInfo, "name"> };
|
||||
/** 数组元素类型 - 仅当type为array时使用 */
|
||||
itemType?: ParamType;
|
||||
/** 数组元素对象定义 - 仅当type为array且itemType为object时使用 */
|
||||
itemProperties?: { [key: string]: Omit<ParameterInfo, "name"> };
|
||||
}
|
||||
|
||||
/**
|
||||
* 节点元数据接口
|
||||
*/
|
||||
export interface NodeMetadata {
|
||||
/** 节点名称 */
|
||||
name: string;
|
||||
/** 节点类名 */
|
||||
className: string;
|
||||
/** 节点分组 */
|
||||
group: string;
|
||||
/** 节点类型 */
|
||||
type: Type;
|
||||
/** 节点描述 */
|
||||
description: string;
|
||||
/** 参数列表 */
|
||||
parameters: ParameterInfo[];
|
||||
/** 最大子节点数量:0=不允许子节点,1=最多一个子节点,-1=无限制 */
|
||||
maxChildren: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册节点名 到 节点构造函数的映射
|
||||
*/
|
||||
const NODE_NAME_TO_CONSTRUCTOR_MAP = new Map<string, any>();
|
||||
|
||||
/**
|
||||
* 节点元数据存储
|
||||
*/
|
||||
const NODE_METADATA_MAP = new Map<any, NodeMetadata>();
|
||||
|
||||
/**
|
||||
* 节点参数存储
|
||||
*/
|
||||
const NODE_PARAMETERS_MAP = new Map<any, ParameterInfo[]>();
|
||||
|
||||
/**
|
||||
* 节点属性装饰器
|
||||
*/
|
||||
export function prop(paramInfo: Omit<ParameterInfo, "name">) {
|
||||
return function (target: any, propertyKey: string) {
|
||||
const ctor = target.constructor;
|
||||
if (!NODE_PARAMETERS_MAP.has(ctor)) {
|
||||
NODE_PARAMETERS_MAP.set(ctor, []);
|
||||
}
|
||||
const parameters = NODE_PARAMETERS_MAP.get(ctor)!;
|
||||
parameters.push({
|
||||
...paramInfo,
|
||||
name: propertyKey
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 行为节点装饰器
|
||||
* @param name 节点的类名 编辑器导出数据中的节点名字
|
||||
* @param info.group 节点在编辑器中的分组
|
||||
* @param info.name 节点在编辑器中的中文名
|
||||
* @param info.desc 节点描述信息
|
||||
*/
|
||||
export function ClassAction(name: string, info?: { group?: string, name?: string, desc?: string }) {
|
||||
return function <T extends new (...args: any[]) => any>(constructor: T) {
|
||||
const parameters = NODE_PARAMETERS_MAP.get(constructor) || [];
|
||||
const fullMetadata: NodeMetadata = {
|
||||
className: name,
|
||||
group: info?.group || '未分组',
|
||||
name: info?.name || '',
|
||||
description: info?.desc || '',
|
||||
type: Type.Action,
|
||||
maxChildren: 0,
|
||||
parameters
|
||||
};
|
||||
NODE_METADATA_MAP.set(constructor, fullMetadata);
|
||||
NODE_NAME_TO_CONSTRUCTOR_MAP.set(name, constructor);
|
||||
return constructor;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 条件节点装饰器
|
||||
*/
|
||||
export function ClassCondition(name: string, info?: { group?: string, name?: string, desc?: string }) {
|
||||
return function <T extends new (...args: any[]) => any>(constructor: T) {
|
||||
const parameters = NODE_PARAMETERS_MAP.get(constructor) || [];
|
||||
const fullMetadata: NodeMetadata = {
|
||||
className: name,
|
||||
group: info?.group || '未分组',
|
||||
name: info?.name || '',
|
||||
description: info?.desc || '',
|
||||
type: Type.Condition,
|
||||
maxChildren: 0,
|
||||
parameters
|
||||
};
|
||||
NODE_METADATA_MAP.set(constructor, fullMetadata);
|
||||
NODE_NAME_TO_CONSTRUCTOR_MAP.set(name, constructor);
|
||||
return constructor;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 组合节点装饰器
|
||||
*/
|
||||
export function ClassComposite(name: string, info?: { group?: string, name?: string, desc?: string }) {
|
||||
return function <T extends new (...args: any[]) => any>(constructor: T) {
|
||||
const parameters = NODE_PARAMETERS_MAP.get(constructor) || [];
|
||||
const fullMetadata: NodeMetadata = {
|
||||
className: name,
|
||||
group: info?.group || '未分组',
|
||||
name: info?.name || '',
|
||||
description: info?.desc || '',
|
||||
type: Type.Composite,
|
||||
maxChildren: -1,
|
||||
parameters
|
||||
};
|
||||
NODE_METADATA_MAP.set(constructor, fullMetadata);
|
||||
NODE_NAME_TO_CONSTRUCTOR_MAP.set(name, constructor);
|
||||
return constructor;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 装饰节点装饰器
|
||||
*/
|
||||
export function ClassDecorator(name: string, info?: { group?: string, name?: string, desc?: string }) {
|
||||
return function <T extends new (...args: any[]) => any>(constructor: T) {
|
||||
const parameters = NODE_PARAMETERS_MAP.get(constructor) || [];
|
||||
const fullMetadata: NodeMetadata = {
|
||||
className: name,
|
||||
group: info?.group || '未分组',
|
||||
name: info?.name || '',
|
||||
description: info?.desc || '',
|
||||
type: Type.Decorator,
|
||||
maxChildren: 1,
|
||||
parameters
|
||||
};
|
||||
NODE_METADATA_MAP.set(constructor, fullMetadata);
|
||||
NODE_NAME_TO_CONSTRUCTOR_MAP.set(name, constructor);
|
||||
return constructor;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有节点元数据
|
||||
*/
|
||||
export function getAllNodeMetadata(): Map<any, NodeMetadata> {
|
||||
return new Map(NODE_METADATA_MAP);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过节点名 获取 节点构造函数
|
||||
*/
|
||||
export function getNodeConstructor(name: string): any {
|
||||
return NODE_NAME_TO_CONSTRUCTOR_MAP.get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过节点构造函数 找到节点元数据
|
||||
*/
|
||||
export function getNodeMetadata(ctor: any): NodeMetadata {
|
||||
return NODE_METADATA_MAP.get(ctor)!;
|
||||
}
|
||||
}
|
||||
|
||||
let _global = globalThis || window || global;
|
||||
(_global as any)["getKunpoBTNodeMaps"] = function () {
|
||||
return BT.getAllNodeMetadata();
|
||||
};
|
||||
@@ -1,70 +0,0 @@
|
||||
/**
|
||||
* @Author: Gongxh
|
||||
* @Date: 2025-09-01
|
||||
* @Description: 抽象节点基类
|
||||
*/
|
||||
|
||||
import { BehaviorTree } from "../BehaviorTree";
|
||||
import { BaseNode } from "./BaseNode";
|
||||
|
||||
/**
|
||||
* 可以包含多个节点的集合装饰器基类
|
||||
*/
|
||||
export abstract class Composite extends BaseNode {
|
||||
constructor(...children: BaseNode[]) {
|
||||
super(children);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 修饰节点基类
|
||||
* 只能包含一个子节点
|
||||
*/
|
||||
export abstract class Decorator extends BaseNode {
|
||||
constructor(child: BaseNode) {
|
||||
super([child]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 数值型装饰节点基类
|
||||
* 包含最大值和当前值的通用逻辑,适用于所有需要数值计数的装饰节点
|
||||
*/
|
||||
export abstract class NumericDecorator extends Decorator {
|
||||
protected readonly _max: number;
|
||||
protected _value: number = 0;
|
||||
|
||||
constructor(child: BaseNode, max: number = 1) {
|
||||
super(child);
|
||||
this._max = max;
|
||||
}
|
||||
|
||||
protected override initialize<T>(tree: BehaviorTree<T>): void {
|
||||
super.initialize(tree);
|
||||
this._value = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 记忆装饰节点基类
|
||||
*/
|
||||
export abstract class MemoryComposite extends Composite {
|
||||
protected runningIndex = 0;
|
||||
|
||||
protected override initialize<T>(tree: BehaviorTree<T>): void {
|
||||
super.initialize(tree);
|
||||
// 检查是否需要重置记忆
|
||||
const shouldReset = tree.blackboard.get(`reset_memory`, this);
|
||||
if (shouldReset) {
|
||||
this.runningIndex = 0;
|
||||
tree.blackboard.delete(`reset_memory`, this);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置记忆状态,下次执行时将从第一个子节点开始
|
||||
*/
|
||||
public resetMemory(): void {
|
||||
this.runningIndex = 0;
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,14 @@
|
||||
import type { BehaviorTree } from "../BehaviorTree";
|
||||
import { BT } from "../BT";
|
||||
import { Status } from "../header";
|
||||
import { BaseNode } from "./BaseNode";
|
||||
import { BTNode } from "./BTNode";
|
||||
|
||||
export class Action extends BaseNode {
|
||||
protected _func: (subject?: any) => Status;
|
||||
constructor(func: (subject?: any) => Status) {
|
||||
super();
|
||||
this._func = func;
|
||||
}
|
||||
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
return this._func?.(tree.subject) ?? Status.SUCCESS;
|
||||
/**
|
||||
* 叶子节点 基类
|
||||
* 没有子节点
|
||||
*/
|
||||
export abstract class LeafNode extends BTNode {
|
||||
constructor() {
|
||||
super([]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +17,9 @@ export class Action extends BaseNode {
|
||||
* 次数内,返回RUNNING
|
||||
* 超次,返回SUCCESS
|
||||
*/
|
||||
export class WaitTicks extends BaseNode {
|
||||
@BT.ClassAction("WaitTicks", { name: "次数等待节点", group: "基础行为节点", desc: "指定次数后返回成功, 否则返回执行中" })
|
||||
export class WaitTicks extends LeafNode {
|
||||
@BT.prop({ type: BT.ParamType.int, description: "最大等待次数", defaultValue: 0, step: 1 })
|
||||
private _max: number;
|
||||
private _value: number;
|
||||
|
||||
@@ -29,12 +29,12 @@ export class WaitTicks extends BaseNode {
|
||||
this._value = 0;
|
||||
}
|
||||
|
||||
protected override initialize<T>(tree: BehaviorTree<T>): void {
|
||||
super.initialize(tree);
|
||||
protected override open(): void {
|
||||
super.open();
|
||||
this._value = 0;
|
||||
}
|
||||
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
public tick(): Status {
|
||||
if (++this._value >= this._max) {
|
||||
return Status.SUCCESS;
|
||||
}
|
||||
@@ -46,22 +46,24 @@ export class WaitTicks extends BaseNode {
|
||||
* 时间等待节点 时间(秒)
|
||||
* 时间到后返回SUCCESS,否则返回RUNNING
|
||||
*/
|
||||
export class WaitTime extends BaseNode {
|
||||
@BT.ClassAction("WaitTime", { name: "时间等待节点", group: "基础行为节点", desc: "等待指定时间(秒)后返回成功, 否则返回执行中" })
|
||||
export class WaitTime extends LeafNode {
|
||||
@BT.prop({ type: BT.ParamType.float, description: "等待时间(秒)", defaultValue: 0, step: 0.01 })
|
||||
private _max: number;
|
||||
private _value: number = 0;
|
||||
constructor(duration: number = 0) {
|
||||
super();
|
||||
this._max = duration * 1000;
|
||||
this._max = duration;
|
||||
}
|
||||
|
||||
protected override initialize<T>(tree: BehaviorTree<T>): void {
|
||||
super.initialize(tree);
|
||||
this._value = new Date().getTime();
|
||||
protected override open(): void {
|
||||
super.open();
|
||||
this._value = 0;
|
||||
}
|
||||
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
const currTime = new Date().getTime();
|
||||
if (currTime - this._value >= this._max) {
|
||||
public tick(dt: number): Status {
|
||||
this._value += dt;
|
||||
if (this._value >= this._max) {
|
||||
return Status.SUCCESS;
|
||||
}
|
||||
return Status.RUNNING;
|
||||
|
||||
154
src/behaviortree/BTNode/BTNode.ts
Normal file
154
src/behaviortree/BTNode/BTNode.ts
Normal file
@@ -0,0 +1,154 @@
|
||||
import { globalBlackboard, IBlackboard } from "../Blackboard";
|
||||
import { Status } from "../header";
|
||||
|
||||
export interface IBTNode {
|
||||
readonly children: IBTNode[];
|
||||
/** 本节点的的黑板引用 */
|
||||
local: IBlackboard;
|
||||
/**
|
||||
* 初始化节点
|
||||
* @param root 树根节点的黑板
|
||||
* @param parent 父节点的黑板
|
||||
*/
|
||||
_initialize(root: IBlackboard, parent: IBlackboard): void;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
_execute(dt: number): Status;
|
||||
tick(dt: number): Status;
|
||||
|
||||
/**
|
||||
* 优先写入自己的黑板数据, 如果没有则写入父节点的黑板数据
|
||||
*/
|
||||
set<T>(key: string, value: T): void;
|
||||
get<T>(key: string): T;
|
||||
|
||||
/**
|
||||
* 写入树根节点的黑板数据
|
||||
*/
|
||||
setRoot<T>(key: string, value: T): void;
|
||||
getRoot<T>(key: string): T;
|
||||
|
||||
/**
|
||||
* 写入全局黑板数据
|
||||
*/
|
||||
setGlobal<T>(key: string, value: T): void;
|
||||
getGlobal<T>(key: string): T;
|
||||
|
||||
/** 获取关联的实体 */
|
||||
getEntity<T>(): T;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 基础节点
|
||||
* 每个节点只管理自己需要的状态
|
||||
*/
|
||||
export abstract class BTNode implements IBTNode {
|
||||
public readonly children: IBTNode[];
|
||||
|
||||
/** 树根节点的黑板引用 */
|
||||
protected _root!: IBlackboard;
|
||||
|
||||
/** 本节点的的黑板引用 可能等于 _parent */
|
||||
protected _local!: IBlackboard;
|
||||
|
||||
constructor(children?: IBTNode[]) {
|
||||
this.children = children ? [...children] : [];
|
||||
}
|
||||
|
||||
public _initialize(root: IBlackboard, parent: IBlackboard): void {
|
||||
this._root = root;
|
||||
// 在需要的节点中重写,创建新的local
|
||||
this._local = parent;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
public _execute(dt: number): Status {
|
||||
// 首次执行时初始化
|
||||
const isRunning = this._local.openNodes.get(this) || false;
|
||||
if (!isRunning) {
|
||||
this._local.openNodes.set(this, true);
|
||||
this.open();
|
||||
}
|
||||
|
||||
// 执行核心逻辑
|
||||
const status = this.tick(dt);
|
||||
|
||||
// 执行完成时清理
|
||||
if (status !== Status.RUNNING) {
|
||||
this._local.openNodes.delete(this);
|
||||
this.close();
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化节点(首次执行时调用)
|
||||
* 子类重写此方法进行状态初始化
|
||||
*/
|
||||
protected open(): void { }
|
||||
protected close(): void { }
|
||||
/**
|
||||
* 清理子节点的打开状态
|
||||
* 一般用于装饰节点的非子节点关闭时, 用来清理子节点的打开状态
|
||||
*/
|
||||
protected cleanupChild(): void {
|
||||
const child = this.children[0];
|
||||
if (child && this._local.openNodes.has(child)) {
|
||||
this._local.openNodes.delete(child);
|
||||
(child as BTNode).close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行节点逻辑
|
||||
* 子类必须实现此方法
|
||||
* @returns 执行状态
|
||||
*/
|
||||
public abstract tick(dt: number): Status;
|
||||
|
||||
public getEntity<T>(): T {
|
||||
return this._local.getEntity();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置获取全局黑板数据
|
||||
*/
|
||||
public set<T>(key: string, value: T): void {
|
||||
this._local.set(key, value);
|
||||
}
|
||||
|
||||
public get<T>(key: string): T {
|
||||
return this._local.get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置获取树根节点的黑板数据
|
||||
*/
|
||||
public setRoot<T>(key: string, value: T): void {
|
||||
this._root.set(key, value);
|
||||
}
|
||||
|
||||
public getRoot<T>(key: string): T {
|
||||
return this._root.get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置全局黑板数据
|
||||
*/
|
||||
public setGlobal<T>(key: string, value: T): void {
|
||||
globalBlackboard.set(key, value);
|
||||
}
|
||||
|
||||
public getGlobal<T>(key: string): T {
|
||||
return globalBlackboard.get(key);
|
||||
}
|
||||
|
||||
public get local(): IBlackboard {
|
||||
return this._local;
|
||||
}
|
||||
}
|
||||
@@ -1,86 +0,0 @@
|
||||
import { BehaviorTree } from "../BehaviorTree";
|
||||
import { Status } from "../header";
|
||||
|
||||
|
||||
/**
|
||||
* 基础节点
|
||||
* 每个节点只管理自己需要的状态
|
||||
*/
|
||||
export abstract class BaseNode {
|
||||
public readonly children: BaseNode[];
|
||||
private _id: string;
|
||||
private _isRunning: boolean;
|
||||
|
||||
set id(id: string) { this._id = id; }
|
||||
get id(): string { return this._id }
|
||||
|
||||
/**
|
||||
* 创建
|
||||
* @param children 子节点列表
|
||||
*/
|
||||
constructor(children?: BaseNode[]) {
|
||||
this._id = ""; // 临时值,将在树构造时被正确设置
|
||||
this.children = children ? [...children] : [];
|
||||
this._isRunning = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行节点
|
||||
* @param tree 行为树
|
||||
* @returns 状态
|
||||
*/
|
||||
public _execute<T>(tree: BehaviorTree<T>): Status {
|
||||
// 首次执行时初始化
|
||||
if (!this._isRunning) {
|
||||
this._isRunning = true;
|
||||
this.initialize(tree);
|
||||
}
|
||||
|
||||
// 执行核心逻辑
|
||||
const status = this.tick(tree);
|
||||
|
||||
// 执行完成时清理
|
||||
if (status !== Status.RUNNING) {
|
||||
this._isRunning = false;
|
||||
this.cleanup(tree);
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化节点(首次执行时调用)
|
||||
* 子类重写此方法进行状态初始化
|
||||
* @param tree 行为树
|
||||
*/
|
||||
protected initialize<T>(tree: BehaviorTree<T>): void { }
|
||||
|
||||
/**
|
||||
* 清理节点(执行完成时调用)
|
||||
* 子类重写此方法进行状态清理
|
||||
* @param tree 行为树
|
||||
*/
|
||||
protected cleanup<T>(tree: BehaviorTree<T>): void { }
|
||||
|
||||
/**
|
||||
* 执行节点逻辑
|
||||
* 子类必须实现此方法
|
||||
* @param tree 行为树
|
||||
* @returns 执行状态
|
||||
*/
|
||||
public abstract tick<T>(tree: BehaviorTree<T>): Status;
|
||||
|
||||
/**
|
||||
* 递归清理节点及其所有子节点的状态
|
||||
* 用于行为树中断时清理所有节点状态
|
||||
*/
|
||||
public cleanupAll(): void {
|
||||
// 清理基础状态
|
||||
this._isRunning = false;
|
||||
|
||||
// 递归清理所有子节点
|
||||
for (const child of this.children) {
|
||||
child.cleanupAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,115 +1,105 @@
|
||||
import type { BehaviorTree } from "../BehaviorTree";
|
||||
import { IBlackboard } from "../Blackboard";
|
||||
import { BT } from "../BT";
|
||||
import { Status } from "../header";
|
||||
import { Composite, MemoryComposite } from "./AbstractNodes";
|
||||
import { BTNode, IBTNode } from "./BTNode";
|
||||
import { WeightDecorator } from "./Decorator";
|
||||
|
||||
/**
|
||||
* 记忆选择节点
|
||||
* 选择不为 FAILURE 的节点,记住上次运行的子节点位置
|
||||
* 任意一个Child Node返回不为 FAILURE, 本Node向自己的Parent Node也返回Child Node状态
|
||||
* 组合节点基类
|
||||
* 有多个子节点
|
||||
*/
|
||||
export class MemSelector extends MemoryComposite {
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
for (let i = this.runningIndex; i < this.children.length; i++) {
|
||||
let status = this.children[i]!._execute(tree);
|
||||
if (status !== Status.FAILURE) {
|
||||
if (status === Status.RUNNING) {
|
||||
this.runningIndex = i;
|
||||
}
|
||||
return status;
|
||||
}
|
||||
}
|
||||
return Status.FAILURE;
|
||||
export abstract class Composite extends BTNode {
|
||||
constructor(...children: IBTNode[]) {
|
||||
super(children);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 记忆顺序节点
|
||||
* 如果上次执行到 RUNNING 的节点, 下次进入节点后, 直接从 RUNNING 节点开始
|
||||
* 遇到 SUCCESS 或者 FAILURE 停止迭代
|
||||
* 任意一个Child Node返回不为 SUCCESS, 本Node向自己的Parent Node也返回Child Node状态
|
||||
* 所有节点都返回 SUCCESS, 本节点才返回 SUCCESS
|
||||
*/
|
||||
export class MemSequence extends MemoryComposite {
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
for (let i = this.runningIndex; i < this.children.length; i++) {
|
||||
let status = this.children[i]!._execute(tree);
|
||||
if (status !== Status.SUCCESS) {
|
||||
if (status === Status.RUNNING) {
|
||||
this.runningIndex = i;
|
||||
}
|
||||
return status;
|
||||
}
|
||||
}
|
||||
return Status.SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 随机选择节点
|
||||
* 从Child Node中随机选择一个执行
|
||||
*/
|
||||
export class RandomSelector extends Composite {
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
if (this.children.length === 0) {
|
||||
return Status.FAILURE;
|
||||
}
|
||||
|
||||
const childIndex = Math.floor(Math.random() * this.children.length);
|
||||
const status = this.children[childIndex]!._execute(tree);
|
||||
return status;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 选择节点,选择不为 FAILURE 的节点
|
||||
* 当执行本Node时,它将从begin到end迭代执行自己的Child Node:
|
||||
* 如遇到一个Child Node执行后返回 SUCCESS 或者 RUNNING,那停止迭代,本Node向自己的Parent Node也返回 SUCCESS 或 RUNNING
|
||||
* 记忆选择节点 从上到下执行
|
||||
* 遇到 FAILURE 继续下一个
|
||||
* 遇到 SUCCESS 返回 SUCCESS 下次重新开始
|
||||
*
|
||||
* 遇到 RUNNING 返回 RUNNING 下次从该节点开始
|
||||
*/
|
||||
@BT.ClassComposite("Selector", { name: "选择节点", group: "基础组合节点", desc: "子节点从左到右执行, 子节点状态: 成功则选择成立, 失败继续下一个, 执行中则返回执行中, 下次从该节点开始" })
|
||||
export class Selector extends Composite {
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
for (let i = 0; i < this.children.length; i++) {
|
||||
let status = this.children[i]!._execute(tree);
|
||||
if (status !== Status.FAILURE) {
|
||||
public override _initialize(global: IBlackboard, branch: IBlackboard): void {
|
||||
super._initialize(global, branch);
|
||||
this._local = branch.createChild();
|
||||
}
|
||||
|
||||
protected override open(): void {
|
||||
super.open();
|
||||
this.set(`__nRunningIndex`, 0);
|
||||
}
|
||||
|
||||
public tick(dt: number): Status {
|
||||
let index = this.get<number>(`__nRunningIndex`);
|
||||
for (let i = index; i < this.children.length; i++) {
|
||||
let status = this.children[i]!._execute(dt);
|
||||
if (status === Status.FAILURE) {
|
||||
continue;
|
||||
}
|
||||
if (status === Status.SUCCESS) {
|
||||
return status;
|
||||
}
|
||||
this.set(`__nRunningIndex`, i);
|
||||
return Status.RUNNING;
|
||||
}
|
||||
return Status.FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 顺序节点
|
||||
* 当执行本类型Node时,它将从begin到end迭代执行自己的Child Node:
|
||||
* 遇到 FAILURE 或 RUNNING, 那停止迭代,返回FAILURE 或 RUNNING
|
||||
* 所有节点都返回 SUCCESS, 本节点才返回 SUCCESS
|
||||
* 顺序节点 从上到下执行
|
||||
* 遇到 SUCCESS 继续下一个
|
||||
* 遇到 FAILURE 停止迭代 返回 FAILURE 下次重新开始
|
||||
*
|
||||
* 遇到 RUNNING 返回 RUNNING 下次从该节点开始
|
||||
*/
|
||||
@BT.ClassComposite("Sequence", { name: "顺序节点", group: "基础组合节点", desc: "子节点从左到右执行, 子节点状态: 成功则继续下一个, 失败则停止迭代返回失败, 执行中返回执行中, 下次从该节点开始" })
|
||||
export class Sequence extends Composite {
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
for (let i = 0; i < this.children.length; i++) {
|
||||
let status = this.children[i]!._execute(tree);
|
||||
if (status !== Status.SUCCESS) {
|
||||
return status;
|
||||
public override _initialize(global: IBlackboard, branch: IBlackboard): void {
|
||||
super._initialize(global, branch);
|
||||
this._local = branch.createChild();
|
||||
}
|
||||
|
||||
protected override open(): void {
|
||||
super.open();
|
||||
this.set(`__nRunningIndex`, 0);
|
||||
}
|
||||
|
||||
public tick(dt: number): Status {
|
||||
let index = this.get<number>(`__nRunningIndex`);
|
||||
for (let i = index; i < this.children.length; i++) {
|
||||
let status = this.children[i]!._execute(dt);
|
||||
if (status === Status.SUCCESS) {
|
||||
continue;
|
||||
}
|
||||
if (status === Status.FAILURE) {
|
||||
return Status.FAILURE;
|
||||
}
|
||||
this.set(`__nRunningIndex`, i);
|
||||
return Status.RUNNING;
|
||||
}
|
||||
return Status.SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 并行节点 每次进入全部重新执行一遍
|
||||
* 当执行本类型Node时,它将从begin到end迭代执行自己的Child Node:
|
||||
* 1. 当存在Child Node执行后返回 FAILURE, 本节点返回 FAILURE
|
||||
* 2. 当存在Child Node执行后返回 RUNNING, 本节点返回 RUNNING
|
||||
* 所有节点都返回 SUCCESS, 本节点才返回 SUCCESS
|
||||
* 并行节点 从左到右依次执行所有子节点
|
||||
* 注意:这里的"并行"是逻辑概念,实际是顺序执行
|
||||
* 返回优先级 FAILURE > RUNNING > SUCCESS
|
||||
*/
|
||||
@BT.ClassComposite("Parallel", { name: "并行节点", group: "基础组合节点", desc: "依次执行所有子节点(从左到右), 子节点状态: 任意失败则失败 > 任意执行中则执行中 > 全部成功则成功" })
|
||||
export class Parallel extends Composite {
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
public tick(dt: number): Status {
|
||||
let result = Status.SUCCESS;
|
||||
for (let i = 0; i < this.children.length; i++) {
|
||||
let status = this.children[i]!._execute(tree);
|
||||
if (status == Status.FAILURE) {
|
||||
let status = this.children[i]!._execute(dt);
|
||||
if (result === Status.FAILURE || status === Status.FAILURE) {
|
||||
result = Status.FAILURE;
|
||||
} else if (result == Status.SUCCESS && status == Status.RUNNING) {
|
||||
} else if (status === Status.RUNNING) {
|
||||
result = Status.RUNNING;
|
||||
}
|
||||
}
|
||||
@@ -117,22 +107,75 @@ export class Parallel extends Composite {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 并行节点 每次进入全部重新执行一遍
|
||||
* 当执行本类型Node时,它将从begin到end迭代执行自己的Child Node:
|
||||
* 1. 当存在Child Node执行后返回 FAILURE, 本节点返回 FAILURE
|
||||
* 2. 任意 Child Node 返回 SUCCESS, 本节点返回 SUCCESS
|
||||
* 否则返回 RUNNING
|
||||
* 随机选择节点
|
||||
* 随机选择一个子节点执行
|
||||
* 返回子节点状态
|
||||
*/
|
||||
@BT.ClassComposite("RandomSelector", { name: "随机选择节点", group: "基础组合节点", desc: "随机选择一个子节点执行, 返回子节点状态" })
|
||||
export class RandomSelector extends Composite {
|
||||
private _totalWeight: number = 0;
|
||||
private _weights: number[] = [];
|
||||
|
||||
constructor(...children: IBTNode[]) {
|
||||
super(...children);
|
||||
this._totalWeight = 0;
|
||||
this._weights = [];
|
||||
|
||||
for (const child of children) {
|
||||
const weight = this.getChildWeight(child);
|
||||
this._totalWeight += weight;
|
||||
this._weights.push(this._totalWeight);
|
||||
}
|
||||
}
|
||||
|
||||
private getChildWeight(child: IBTNode): number {
|
||||
return (child instanceof WeightDecorator) ? (child.weight) : 1;
|
||||
}
|
||||
|
||||
public tick(dt: number): Status {
|
||||
if (this.children.length === 0) {
|
||||
return Status.FAILURE;
|
||||
}
|
||||
|
||||
// 基于权重的随机选择
|
||||
const randomValue = Math.random() * this._totalWeight;
|
||||
|
||||
// 使用二分查找找到对应的子节点索引(O(log n)复杂度)
|
||||
let left = 0;
|
||||
let right = this._weights.length - 1;
|
||||
let childIndex = 0;
|
||||
|
||||
while (left <= right) {
|
||||
const mid = Math.floor((left + right) / 2);
|
||||
if (this._weights[mid]! > randomValue) {
|
||||
childIndex = mid;
|
||||
right = mid - 1;
|
||||
} else {
|
||||
left = mid + 1;
|
||||
}
|
||||
}
|
||||
const status = this.children[childIndex]!._execute(dt);
|
||||
return status;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 并行任意成功节点 从左到右依次执行所有子节点
|
||||
* 注意:这里的"并行"是逻辑概念,实际是顺序执行
|
||||
* 返回优先级 SUCCESS > RUNNING > FAILURE
|
||||
*/
|
||||
@BT.ClassComposite("ParallelAnySuccess", { name: "并行任意成功节点", group: "基础组合节点", desc: "依次执行所有子节点(从左到右), 任意一个成功则成功 > 任意一个执行中则执行中 > 全部失败则失败" })
|
||||
export class ParallelAnySuccess extends Composite {
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
let result = Status.RUNNING;
|
||||
public tick(dt: number): Status {
|
||||
let result = Status.FAILURE;
|
||||
for (let i = 0; i < this.children.length; i++) {
|
||||
let status = this.children[i]!._execute(tree);
|
||||
if (status == Status.FAILURE) {
|
||||
result = Status.FAILURE;
|
||||
} else if (result == Status.RUNNING && status == Status.SUCCESS) {
|
||||
let status = this.children[i]!._execute(dt);
|
||||
if (result === Status.SUCCESS || status === Status.SUCCESS) {
|
||||
result = Status.SUCCESS;
|
||||
} else if (status === Status.RUNNING) {
|
||||
result = Status.RUNNING;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
|
||||
@@ -1,20 +1,21 @@
|
||||
import type { BehaviorTree } from "../BehaviorTree";
|
||||
import { Status } from "../header";
|
||||
import { BaseNode } from "./BaseNode";
|
||||
|
||||
/**
|
||||
* 条件节点
|
||||
* 根据条件函数返回SUCCESS或FAILURE
|
||||
* @Author: Gongxh
|
||||
* @Date: 2025-09-17
|
||||
* @Description: 条件节点基类
|
||||
*/
|
||||
export class Condition extends BaseNode {
|
||||
/** 执行函数 @internal */
|
||||
private readonly _func: (subject: any) => boolean;
|
||||
constructor(func: (subject: any) => boolean) {
|
||||
super();
|
||||
this._func = func;
|
||||
}
|
||||
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
return this._func?.(tree.subject) ? Status.SUCCESS : Status.FAILURE;
|
||||
import { Status } from "../header";
|
||||
import { LeafNode } from "./Action";
|
||||
|
||||
/** 条件叶子节点 */
|
||||
export abstract class Condition extends LeafNode {
|
||||
/**
|
||||
* 判断是否满足条件
|
||||
* @returns 是否满足条件
|
||||
*/
|
||||
protected abstract isEligible(): boolean;
|
||||
|
||||
public tick(): Status {
|
||||
return this.isEligible() ? Status.SUCCESS : Status.FAILURE;
|
||||
}
|
||||
}
|
||||
@@ -4,10 +4,35 @@
|
||||
* @Description: 装饰节点 装饰节点下必须包含子节点
|
||||
*/
|
||||
|
||||
import type { BehaviorTree } from "../BehaviorTree";
|
||||
import { BT } from "../BT";
|
||||
import { Status } from "../header";
|
||||
import { Decorator, NumericDecorator } from "./AbstractNodes";
|
||||
import { BaseNode } from "./BaseNode";
|
||||
import { BTNode, IBTNode } from "./BTNode";
|
||||
|
||||
/**
|
||||
* 修饰节点 基类
|
||||
* 有且仅有一个子节点
|
||||
*/
|
||||
export abstract class Decorator extends BTNode {
|
||||
constructor(child: IBTNode) {
|
||||
super([child]);
|
||||
}
|
||||
}
|
||||
|
||||
/** 条件装饰节点基类 */
|
||||
export abstract class ConditionDecorator extends Decorator {
|
||||
/**
|
||||
* 判断是否满足条件
|
||||
* @returns 是否满足条件
|
||||
*/
|
||||
protected abstract isEligible(): boolean;
|
||||
|
||||
public tick(dt: number): Status {
|
||||
if (this.isEligible()) {
|
||||
return this.children[0]!._execute(dt);
|
||||
}
|
||||
return Status.FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 结果反转节点
|
||||
@@ -15,9 +40,10 @@ import { BaseNode } from "./BaseNode";
|
||||
* 第一个Child Node节点, 返回 FAILURE, 本Node向自己的Parent Node也返回 SUCCESS
|
||||
* 第一个Child Node节点, 返回 SUCCESS, 本Node向自己的Parent Node也返回 FAILURE
|
||||
*/
|
||||
@BT.ClassDecorator("Inverter", { name: "结果反转节点", group: "基础装饰节点", desc: "反转子节点的执行结果, 成功变失败, 失败变成功, 执行中保持不变" })
|
||||
export class Inverter extends Decorator {
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
const status = this.children[0]!._execute(tree);
|
||||
public tick(dt: number): Status {
|
||||
const status = this.children[0]!._execute(dt);
|
||||
|
||||
if (status === Status.SUCCESS) {
|
||||
return Status.FAILURE;
|
||||
@@ -35,48 +61,64 @@ export class Inverter extends Decorator {
|
||||
* 规定时间内, 根据Child Node的结果, 本节点向自己的父节点也返回相同的结果
|
||||
* 超时后, 直接返回 FAILURE
|
||||
*/
|
||||
export class LimitTime extends NumericDecorator {
|
||||
@BT.ClassDecorator("LimitTime", { name: "时间限制节点", group: "基础装饰节点", desc: "限制时间内返回子节点状态, 超时后返回失败" })
|
||||
export class LimitTime extends Decorator {
|
||||
@BT.prop({ type: BT.ParamType.float, description: "最大时间(秒)", defaultValue: 1 })
|
||||
protected _max: number = 1;
|
||||
|
||||
private _value: number = 0;
|
||||
/**
|
||||
* 时间限制节点
|
||||
* @param child 子节点
|
||||
* @param max 最大时间 (秒) 默认1秒
|
||||
*/
|
||||
constructor(child: BaseNode, max: number = 1) {
|
||||
super(child, max * 1000);
|
||||
constructor(child: IBTNode, max: number = 1) {
|
||||
super(child);
|
||||
this._max = max;
|
||||
}
|
||||
|
||||
protected override initialize<T>(tree: BehaviorTree<T>): void {
|
||||
super.initialize(tree);
|
||||
this._value = Date.now();
|
||||
protected override open(): void {
|
||||
this._value = 0;
|
||||
}
|
||||
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
const currentTime = Date.now();
|
||||
|
||||
if (currentTime - this._value > this._max) {
|
||||
public tick(dt: number): Status {
|
||||
this._value += dt;
|
||||
if (this._value > this._max) {
|
||||
this.cleanupChild();
|
||||
return Status.FAILURE;
|
||||
}
|
||||
|
||||
return this.children[0]!._execute(tree);
|
||||
return this.children[0]!._execute(dt);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 次数限制节点
|
||||
* 必须且只能包含一个子节点
|
||||
* 次数限制内, 返回子节点的状态, 次数达到后, 直接返回失败
|
||||
* 次数超过后, 直接返回失败; 次数未超过, 返回子节点状态
|
||||
*/
|
||||
export class LimitTimes extends NumericDecorator {
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
if (this._value >= this._max) {
|
||||
@BT.ClassDecorator("LimitTicks", { name: "次数限制节点", group: "基础装饰节点", desc: "子节点成功, 次数+1, 限制次数内返回子节点状态, 超过限制次数返回失败" })
|
||||
export class LimitTicks extends Decorator {
|
||||
@BT.prop({ type: BT.ParamType.int, description: "最大次数", defaultValue: 1 })
|
||||
protected _max: number = 1;
|
||||
|
||||
private _value: number = 0;
|
||||
constructor(child: IBTNode, max: number = 1) {
|
||||
super(child);
|
||||
this._max = max;
|
||||
}
|
||||
|
||||
protected override open(): void {
|
||||
this._value = 0;
|
||||
}
|
||||
|
||||
public tick(dt: number): Status {
|
||||
if (this._value > this._max) {
|
||||
this.cleanupChild();
|
||||
return Status.FAILURE;
|
||||
}
|
||||
const status = this.children[0]!._execute(tree);
|
||||
let status = this.children[0]!._execute(dt);
|
||||
if (status !== Status.RUNNING) {
|
||||
this._value++;
|
||||
if (this._value < this._max) {
|
||||
return Status.RUNNING;
|
||||
}
|
||||
}
|
||||
return status;
|
||||
}
|
||||
@@ -88,12 +130,26 @@ export class LimitTimes extends NumericDecorator {
|
||||
* 子节点是成功或失败,累加计数
|
||||
* 次数超过之后返回子节点状态,否则返回 RUNNING
|
||||
*/
|
||||
export class Repeat extends NumericDecorator {
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
@BT.ClassDecorator("Repeat", { name: "重复节点", group: "基础装饰节点", desc: "子节点成功或失败次数+1, 重复执行指定次数" })
|
||||
export class Repeat extends Decorator {
|
||||
@BT.prop({ type: BT.ParamType.int, description: "重复次数", defaultValue: 1, min: 1 })
|
||||
protected _max: number = 1;
|
||||
|
||||
private _value: number = 0;
|
||||
constructor(child: IBTNode, max: number = 1) {
|
||||
super(child);
|
||||
this._max = max;
|
||||
}
|
||||
|
||||
protected override open(): void {
|
||||
this._value = 0;
|
||||
}
|
||||
|
||||
public tick(dt: number): Status {
|
||||
// 执行子节点
|
||||
const status = this.children[0]!._execute(tree);
|
||||
const status = this.children[0]!._execute(dt);
|
||||
// 如果子节点完成(成功或失败),增加计数
|
||||
if (status === Status.SUCCESS || status === Status.FAILURE) {
|
||||
if (status !== Status.RUNNING) {
|
||||
this._value++;
|
||||
// 检查是否达到最大次数
|
||||
if (this._value >= this._max) {
|
||||
@@ -111,9 +167,23 @@ export class Repeat extends NumericDecorator {
|
||||
*
|
||||
* 子节点成功 计数+1
|
||||
*/
|
||||
export class RepeatUntilFailure extends NumericDecorator {
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
const status = this.children[0]!._execute(tree);
|
||||
@BT.ClassDecorator("RepeatUntilFailure", { name: "重复直到失败", group: "基础装饰节点", desc: "子节点成功则次数+1, 限制次数内返回执行中, 重复执行子节点直到子节点返回失败, 超过限制次数返回失败" })
|
||||
export class RepeatUntilFailure extends Decorator {
|
||||
@BT.prop({ type: BT.ParamType.int, description: "最大重试次数", defaultValue: 1, min: 1 })
|
||||
protected _max: number = 1;
|
||||
|
||||
private _value: number = 0;
|
||||
constructor(child: IBTNode, max: number = 1) {
|
||||
super(child);
|
||||
this._max = max;
|
||||
}
|
||||
|
||||
protected override open(): void {
|
||||
this._value = 0;
|
||||
}
|
||||
|
||||
public tick(dt: number): Status {
|
||||
const status = this.children[0]!._execute(dt);
|
||||
if (status === Status.FAILURE) {
|
||||
return Status.FAILURE;
|
||||
}
|
||||
@@ -135,10 +205,24 @@ export class RepeatUntilFailure extends NumericDecorator {
|
||||
*
|
||||
* 子节点失败, 计数+1
|
||||
*/
|
||||
export class RepeatUntilSuccess extends NumericDecorator {
|
||||
public tick<T>(tree: BehaviorTree<T>): Status {
|
||||
@BT.ClassDecorator("RepeatUntilSuccess", { name: "重复直到成功", group: "基础装饰节点", desc: "子节点失败则次数+1, 限制次数内返回执行中, 重复执行子节点直到子节点返回成功, 超过限制次数返回失败" })
|
||||
export class RepeatUntilSuccess extends Decorator {
|
||||
@BT.prop({ type: BT.ParamType.int, description: "最大重试次数", defaultValue: 1, step: 1 })
|
||||
protected _max: number = 1;
|
||||
|
||||
private _value: number = 0;
|
||||
constructor(child: IBTNode, max: number = 1) {
|
||||
super(child);
|
||||
this._max = max;
|
||||
}
|
||||
|
||||
protected override open(): void {
|
||||
this._value = 0;
|
||||
}
|
||||
|
||||
public tick(dt: number): Status {
|
||||
// 执行子节点
|
||||
const status = this.children[0]!._execute(tree);
|
||||
const status = this.children[0]!._execute(dt);
|
||||
if (status === Status.SUCCESS) {
|
||||
return Status.SUCCESS;
|
||||
}
|
||||
@@ -151,4 +235,27 @@ export class RepeatUntilSuccess extends NumericDecorator {
|
||||
}
|
||||
return Status.RUNNING;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 权重装饰节点
|
||||
*/
|
||||
@BT.ClassDecorator("WeightDecorator", { name: "权重装饰节点", group: "基础装饰节点", desc: "根据权重随机选择子节点执行, 用于随机选择节点的子节点" })
|
||||
export class WeightDecorator extends Decorator {
|
||||
@BT.prop({ type: BT.ParamType.int, description: "权重", defaultValue: 1, step: 1 })
|
||||
private _weight: number;
|
||||
|
||||
constructor(child: IBTNode, weight?: number) {
|
||||
super(child);
|
||||
// 优先使用构造函数参数,否则使用装饰器默认参数
|
||||
this._weight = weight || 1;
|
||||
}
|
||||
|
||||
public tick(dt: number): Status {
|
||||
return this.children[0]!._execute(dt);
|
||||
}
|
||||
|
||||
public get weight(): number {
|
||||
return this._weight;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import { Blackboard } from "./Blackboard";
|
||||
import { BaseNode } from "./BTNode/BaseNode";
|
||||
import { Blackboard, IBlackboard } from "./Blackboard";
|
||||
import { IBTNode } from "./BTNode/BTNode";
|
||||
import { Status } from "./header";
|
||||
|
||||
/**
|
||||
* 行为树
|
||||
@@ -9,36 +10,23 @@ export class BehaviorTree<T> {
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
private _root: BaseNode;
|
||||
private _root: IBTNode;
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
private _blackboard: Blackboard;
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
private _subject: T;
|
||||
private _blackboard: IBlackboard;
|
||||
|
||||
/**
|
||||
* 节点ID计数器,每个树实例独立管理
|
||||
* @internal
|
||||
*/
|
||||
private _nodeIdCounter: number = 0;
|
||||
|
||||
get root(): BaseNode { return this._root; }
|
||||
get blackboard() { return this._blackboard }
|
||||
get subject(): T { return this._subject; }
|
||||
get root(): IBTNode { return this._root; }
|
||||
get blackboard(): IBlackboard { return this._blackboard }
|
||||
|
||||
/**
|
||||
* constructor
|
||||
* @param subject 主体
|
||||
* @param entity 实体
|
||||
* @param root 根节点
|
||||
*/
|
||||
constructor(subject: T, root: BaseNode) {
|
||||
constructor(entity: T, root: IBTNode) {
|
||||
this._root = root;
|
||||
this._blackboard = new Blackboard();
|
||||
this._subject = subject;
|
||||
|
||||
this._blackboard = new Blackboard(undefined, entity);
|
||||
// 构造时就初始化所有节点ID,避免运行时检查
|
||||
this._initializeAllNodeIds(this._root);
|
||||
}
|
||||
@@ -46,17 +34,8 @@ export class BehaviorTree<T> {
|
||||
/**
|
||||
* 执行行为树
|
||||
*/
|
||||
public tick(): void {
|
||||
this._root._execute(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成节点ID
|
||||
* 每个树实例独立管理节点ID,避免全局状态污染
|
||||
* @internal
|
||||
*/
|
||||
private _generateNodeId(): string {
|
||||
return `${++this._nodeIdCounter}`;
|
||||
public tick(dt: number): Status {
|
||||
return this._root._execute(dt);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -65,13 +44,12 @@ export class BehaviorTree<T> {
|
||||
* @param node 要初始化的节点
|
||||
* @internal
|
||||
*/
|
||||
private _initializeAllNodeIds(node: BaseNode): void {
|
||||
private _initializeAllNodeIds(node: IBTNode, parent?: IBTNode): void {
|
||||
// 设置当前节点ID
|
||||
node.id = this._generateNodeId();
|
||||
|
||||
node._initialize(this._blackboard, parent ? parent.local : this._blackboard);
|
||||
// 递归设置所有子节点ID
|
||||
for (const child of node.children) {
|
||||
this._initializeAllNodeIds(child);
|
||||
this._initializeAllNodeIds(child, node);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,18 +58,6 @@ export class BehaviorTree<T> {
|
||||
* 清空黑板并重置所有节点状态
|
||||
*/
|
||||
public reset(): void {
|
||||
this._blackboard.clear();
|
||||
// 重置所有节点的状态
|
||||
this._root.cleanupAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* 重置指定记忆节点的记忆状态
|
||||
* 用于精确控制记忆节点的重置,而不影响其他状态
|
||||
* @param node 记忆节点
|
||||
*/
|
||||
public resetMemoryNode(node: BaseNode): void {
|
||||
// 通过黑板标记该节点需要重置记忆
|
||||
this._blackboard.set(`reset_memory`, true, node);
|
||||
this._blackboard.clean();
|
||||
}
|
||||
}
|
||||
@@ -4,67 +4,93 @@
|
||||
* @Description: 行为树共享数据
|
||||
*
|
||||
* 专门用于存储和管理行为树执行过程中的共享数据
|
||||
* 使用 Symbol 作为键实现高性能且安全的键值存储
|
||||
*/
|
||||
|
||||
// 为了避免循环依赖,我们定义一个最小接口
|
||||
interface IBlackboardNode {
|
||||
readonly id: string;
|
||||
import { IBTNode } from "./BTNode/BTNode";
|
||||
|
||||
/**
|
||||
* 黑板数据接口
|
||||
*/
|
||||
export interface IBlackboard {
|
||||
getEntity<T>(): T;
|
||||
get<T>(key: string): T;
|
||||
set<T>(key: string, value: T): void;
|
||||
delete(key: string): void;
|
||||
has(key: string): boolean;
|
||||
clean(): void;
|
||||
createChild(scope?: number): IBlackboard;
|
||||
/** @internal */
|
||||
openNodes: WeakMap<IBTNode, boolean>;
|
||||
}
|
||||
|
||||
export class Blackboard {
|
||||
private readonly _data = new Map<IBlackboardNode, Map<string, any>>();
|
||||
/**
|
||||
* 黑板类
|
||||
*/
|
||||
export class Blackboard implements IBlackboard {
|
||||
private readonly _data = new Map<string, any>();
|
||||
public parent?: Blackboard | undefined;
|
||||
public children = new Set<Blackboard>();
|
||||
|
||||
public clear(): void {
|
||||
/**
|
||||
* 正在运行中的节点
|
||||
* @internal
|
||||
*/
|
||||
public openNodes = new WeakMap<IBTNode, boolean>();
|
||||
|
||||
/** 实体 */
|
||||
private readonly _entity: any;
|
||||
public getEntity<T>(): T {
|
||||
return this._entity;
|
||||
}
|
||||
|
||||
constructor(parent?: Blackboard, entity?: any) {
|
||||
this.parent = parent;
|
||||
if (parent) {
|
||||
parent.children.add(this);
|
||||
}
|
||||
// 优先使用传入的 entity,如果没有则从父级继承
|
||||
this._entity = entity !== undefined ? entity : (parent?._entity ?? null);
|
||||
}
|
||||
|
||||
/** 核心: 查找链实现 */
|
||||
public get<T>(key: string): T {
|
||||
if (this._data.has(key)) {
|
||||
return this._data.get(key) as T;
|
||||
}
|
||||
return this.parent?.get(key) as T;
|
||||
}
|
||||
|
||||
/** 写入: 只在当前层 */
|
||||
public set<T>(key: string, value: T): void {
|
||||
this._data.set(key, value);
|
||||
}
|
||||
|
||||
/** 检查: 沿链查找 */
|
||||
public has(key: string): boolean {
|
||||
return this._data.has(key) || (this.parent?.has(key) ?? false);
|
||||
}
|
||||
|
||||
public delete(key: string): void {
|
||||
this._data.delete(key);
|
||||
}
|
||||
|
||||
public createChild(): Blackboard {
|
||||
return new Blackboard(this);
|
||||
}
|
||||
|
||||
public clean(): void {
|
||||
// 清空当前黑板数据
|
||||
this._data.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置数据
|
||||
* @param key 键名
|
||||
* @param value 值
|
||||
* @param node 节点实例(用于生成唯一 Symbol)
|
||||
*/
|
||||
public set<T>(key: string, value: T, node: IBlackboardNode): void {
|
||||
let map = this._data.get(node);
|
||||
if (!map) {
|
||||
map = new Map();
|
||||
this._data.set(node, map);
|
||||
// 重置运行状态
|
||||
this.openNodes = new WeakMap<IBTNode, boolean>();
|
||||
|
||||
// 递归清理所有子黑板
|
||||
for (const child of this.children) {
|
||||
child.clean();
|
||||
}
|
||||
map.set(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据
|
||||
* @param key 键名
|
||||
* @param node 节点实例
|
||||
* @returns 值
|
||||
*/
|
||||
public get<T>(key: string, node: IBlackboardNode): T | undefined {
|
||||
return this._data.get(node)?.get(key) as T;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否存在指定键
|
||||
* @param key 键名
|
||||
* @param node 节点实例
|
||||
* @returns 是否存在
|
||||
*/
|
||||
public has(key: string, node: IBlackboardNode): boolean {
|
||||
return this._data.has(node) ? this._data.get(node)?.has(key) || false : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除指定键的数据
|
||||
* @param key 键名
|
||||
* @param node 节点实例
|
||||
* @returns 是否删除成功
|
||||
*/
|
||||
public delete(key: string, node: IBlackboardNode): boolean {
|
||||
if (this.has(key, node)) {
|
||||
this._data.get(node)?.delete(key);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// 全局共享的黑板实例
|
||||
export const globalBlackboard = new Blackboard();
|
||||
71
src/behaviortree/Factory.ts
Normal file
71
src/behaviortree/Factory.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
/**
|
||||
* @Author: Gongxh
|
||||
* @Date: 2025-09-16
|
||||
* @Description: 根据数据创建一颗行为树
|
||||
*/
|
||||
|
||||
import { BehaviorTree } from "./BehaviorTree";
|
||||
import { BT } from "./BT";
|
||||
import { IBTNode } from "./BTNode/BTNode";
|
||||
|
||||
export interface INodeConfig {
|
||||
id: string,
|
||||
className: string,
|
||||
parameters: Record<string, any>,
|
||||
children: string[]
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据节点配置递归创建节点
|
||||
* @param info 节点配置
|
||||
* @param nodeMap 所有节点配置的映射表
|
||||
* @returns 创建的节点实例
|
||||
*/
|
||||
function createNodeRecursively(info: INodeConfig, nodeMap: Map<string, INodeConfig>): IBTNode {
|
||||
// 获取节点构造函数
|
||||
const ctor = BT.getNodeConstructor(info.className);
|
||||
if (!ctor) {
|
||||
throw new Error(`未找到节点【${info.className}】的构造函数`);
|
||||
}
|
||||
|
||||
// 递归创建子节点
|
||||
const childNodes: IBTNode[] = [];
|
||||
for (const childId of info.children || []) {
|
||||
const childInfo = nodeMap.get(childId);
|
||||
if (!childInfo) {
|
||||
throw new Error(`未找到子节点【${childId}】,行为树配置导出信息错误`);
|
||||
}
|
||||
const childNode = createNodeRecursively(childInfo, nodeMap);
|
||||
childNodes.push(childNode);
|
||||
}
|
||||
|
||||
// 创建节点实例
|
||||
let btnode: IBTNode;
|
||||
const metadata = BT.getNodeMetadata(ctor);
|
||||
if (metadata.type === BT.Type.Action || metadata.type === BT.Type.Condition) {
|
||||
btnode = new ctor();
|
||||
} else if (metadata.type === BT.Type.Decorator) {
|
||||
btnode = new ctor(childNodes[0]!);
|
||||
} else {
|
||||
btnode = new ctor(...childNodes);
|
||||
}
|
||||
// 设置节点参数
|
||||
for (const key in info.parameters) {
|
||||
(btnode as any)[key] = info.parameters[key];
|
||||
}
|
||||
return btnode;
|
||||
}
|
||||
|
||||
export function createBehaviorTree<T>(config: INodeConfig[], entity: T): BehaviorTree<T> {
|
||||
// 验证配置
|
||||
if (!config || !Array.isArray(config) || config.length === 0) {
|
||||
throw new Error("Config is empty or invalid");
|
||||
}
|
||||
|
||||
// 创建配置映射表
|
||||
const nodeMap = new Map<string, INodeConfig>();
|
||||
for (const info of config) {
|
||||
nodeMap.set(info.id, info);
|
||||
}
|
||||
return new BehaviorTree(entity, createNodeRecursively(config[0]!, nodeMap));
|
||||
}
|
||||
@@ -2,4 +2,4 @@ export enum Status {
|
||||
FAILURE,
|
||||
SUCCESS,
|
||||
RUNNING,
|
||||
}
|
||||
}
|
||||
|
||||
15
src/index.ts
Normal file
15
src/index.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
|
||||
/** 行为树 */
|
||||
export { BehaviorTree } from "./behaviortree/BehaviorTree";
|
||||
export { Blackboard, globalBlackboard } from "./behaviortree/Blackboard";
|
||||
export * from "./behaviortree/BTNode/Action";
|
||||
export { IBTNode } from "./behaviortree/BTNode/BTNode";
|
||||
export * from "./behaviortree/BTNode/Composite";
|
||||
export * from "./behaviortree/BTNode/Condition";
|
||||
export * from "./behaviortree/BTNode/Decorator";
|
||||
export { createBehaviorTree, INodeConfig } from "./behaviortree/Factory";
|
||||
export { Status } from "./behaviortree/header";
|
||||
|
||||
// 导出装饰器内容
|
||||
import { BT } from "./behaviortree/BT";
|
||||
export const { ClassAction, ClassCondition, ClassComposite, ClassDecorator, prop, ParamType } = BT;
|
||||
@@ -1,13 +0,0 @@
|
||||
|
||||
/** 行为树 */
|
||||
export { BehaviorTree } from "./behaviortree/BehaviorTree";
|
||||
export { Blackboard } from "./behaviortree/Blackboard";
|
||||
export * from "./behaviortree/BTNode/AbstractNodes";
|
||||
export * from "./behaviortree/BTNode/Action";
|
||||
export { BaseNode as Node } from "./behaviortree/BTNode/BaseNode";
|
||||
export * from "./behaviortree/BTNode/Composite";
|
||||
export { Condition } from "./behaviortree/BTNode/Condition";
|
||||
export * from "./behaviortree/BTNode/Decorator";
|
||||
export { Status } from "./behaviortree/header";
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
"lib": ["es6", "dom"],
|
||||
"module": "commonjs",
|
||||
"experimentalDecorators": true, // 启用ES装饰器
|
||||
"emitDecoratorMetadata": true, // 启用装饰器元数据
|
||||
"strict": true,
|
||||
"noImplicitAny": true,
|
||||
"strictNullChecks": true,
|
||||
@@ -20,7 +21,6 @@
|
||||
},
|
||||
"include": [
|
||||
"./src/**/*"
|
||||
// "libs"
|
||||
],
|
||||
// 排除
|
||||
"exclude": [
|
||||
|
||||
Reference in New Issue
Block a user