修改节点装饰器和md文档

This commit is contained in:
gongxh
2025-09-17 22:47:09 +08:00
parent b20cf3fd41
commit 6b9e7dbdda
7 changed files with 117 additions and 457 deletions

525
README.md
View File

@@ -10,64 +10,24 @@
- 🎯 **简洁设计**: 零废话,直接解决问题
- 🔧 **类型安全**: 完整 TypeScript 支持
- 🚀 **高性能**: 优化的执行机制,最小开销
- 🧠 **记忆节点**: 智能状态记忆,避免重复计算
- 📦 **零依赖**: 纯净实现,无第三方依赖
- 🔄 **状态管理**: 分层黑板系统,数据隔离清晰
## 快速开始
### 安装
#### 安装
```bash
npm install kunpocc-behaviortree
```
### 基础示例
#### 内置demo
```typescript
import {
BehaviorTree, Status, Action, Condition,
Sequence, Selector
} from 'kunpocc-behaviortree';
项目根目录下的 `bt-demo`文件夹
// 定义实体
interface Enemy {
health: number;
hasWeapon: boolean;
position: { x: number, y: number };
}
demo是基于`cocos creator3.8.6`制作的
const enemy: Enemy = {
health: 30,
hasWeapon: true,
position: { x: 100, y: 200 }
};
// 创建行为树
const tree = new BehaviorTree(enemy,
new Selector(
// 生命值低时逃跑
new Sequence(
new Condition((node) => {
const entity = node.getEntity<Enemy>();
return entity.health < 50;
}),
new Action((node) => {
console.log("血量低,逃跑!");
return Status.SUCCESS;
})
),
// 否则攻击
new Action((node) => {
console.log("发起攻击!");
return Status.SUCCESS;
})
)
);
// 执行
tree.tick(); // 输出: "血量低,逃跑!"
```
## 核心概念
@@ -81,196 +41,116 @@ enum Status {
```
### 节点类型
- **组合节点**: 控制子节点执行逻辑Sequence、Selector、Parallel等
- **装饰节点**: 修饰单个子节点Inverter、Repeat、Limit等
- **叶子节点**: 执行具体逻辑Action、Condition、Wait等
- **组合节点**: 包含多个子节点 (Composite)
- **装饰节点**: 有且只有一个子节点Decorator
- **叶子节点**: 不能包含子节点 (LeafNode)
- **条件节点**: 特殊的叶子节点 (Condition)
## 节点详解
## 装饰器
> **自行实现的节点,通过装饰器把数据暴露给行为树编辑器**
##### ClassAction - 行为节点装饰器
##### ClassCondition - 条件节点装饰器
##### ClassComposite - 组合节点装饰器
##### ClassDecorator - 装饰节点装饰器
##### prop - 属性装饰器
## 内置节点
### 组合节点 (Composite)
#### Sequence - 顺序节点
按顺序执行子节点,全部成功才成功:
```typescript
new Sequence(
checkAmmo, // 检查弹药
aim, // 瞄准
shoot // 射击
)
// 只有全部成功才返回SUCCESS
```
##### Selector - 选择节点
* 选择第一个成功的子节点
#### Selector - 选择节点
选择第一个成功的子节点:
```typescript
new Selector(
tryMeleeAttack, // 尝试近战
tryRangedAttack, // 尝试远程
retreat // 撤退
)
// 任一成功就返回SUCCESS
```
##### Sequence - 顺序节点
* 按顺序执行子节点执行过程中子节点返回非SUCCESS则返回子节点状态全部成功返回SUCCESS
#### Parallel - 并行节点
同时执行所有子节点,全部成功才成功
```typescript
new Parallel(
moveToTarget, // 移动到目标
playAnimation, // 播放动画
updateUI // 更新UI
)
// 任一失败返回FAILURE有RUNNING返回RUNNING全部SUCCESS才返回SUCCESS
```
##### Parallel - 并行节点
* 执行所有子节点,全部成功才成功
* 并不是真正的并行,也有执行顺序
#### ParallelAnySuccess - 并行任一成功
同时执行所有子节点,任一成功就成功:
```typescript
new ParallelAnySuccess(
findCover, // 寻找掩体
callForHelp, // 呼叫支援
counterAttack // 反击
)
// 任一SUCCESS就返回SUCCESS
```
##### RandomSelector - 随机选择节点
* 随机选择一个子节点执行
#### Memory节点 - 状态记忆
记忆节点会记住上次执行位置,避免重复执行:
##### ParallelAnySuccess - 并行任一成功
* 同时执行所有子节点,任一成功就成功
```typescript
// MemSequence - 记忆顺序节点
new MemSequence(
longTask1, // 第一次SUCCESS继续下一个
longTask2, // 第一次RUNNING记住这个位置 第二次从longTask2开始继续执行
longTask3
)
// MemSelector - 记忆选择节点
new MemSelector(
expensiveCheck1, // 第一次FAILURE继续下一个
expensiveCheck2, // 第一次RUNNING记住这个位置 第二次从expensiveCheck2开始执行
fallback // 如果前面都是FAILURE才会执行到这里
)
```
#### RandomSelector - 随机选择
随机选择一个子节点执行:
```typescript
new RandomSelector(
idleBehavior1,
idleBehavior2,
idleBehavior3
)
```
### 装饰节点 (Decorator)
#### Inverter - 反转节点
反转子节点的成功/失败状态:
```typescript
new Inverter(
new Condition((node) => {
const enemy = node.getEntity<Enemy>();
return enemy.isAlive;
})
) // 敌人死亡时返回SUCCESS
```
##### ConditionDecorator - 条件装饰节点
#### Repeat - 重复节点
重复执行子节点指定次数:
```typescript
new Repeat(
new Action((node) => {
console.log("射击");
return Status.SUCCESS;
}),
3 // 射击3次
)
```
* 子类需实现
#### RepeatUntilSuccess - 重复直到成功
```typescript
new RepeatUntilSuccess(
new Action((node) => {
console.log("尝试开门");
return Math.random() > 0.5 ? Status.SUCCESS : Status.FAILURE;
}),
5 // 最多尝试5次
)
```
```typescript
/**
* 判断是否满足条件
* @returns 是否满足条件
*/
protected abstract isEligible(): boolean;
```
#### RepeatUntilFailure - 重复直到失败
```typescript
new RepeatUntilFailure(
new Action((node) => {
console.log("收集资源");
return Status.SUCCESS; // 持续收集直到失败
}),
10 // 最多收集10次
)
```
##### Inverter - 反转节点
* 反转子节点的成功/失败状态
#### LimitTime - 时间限制
```typescript
new LimitTime(
new Action((node) => {
console.log("执行复杂计算");
return Status.SUCCESS;
}),
2.0 // 最多执行2秒
)
```
##### LimitTime - 时间限制
#### LimitTicks - 次数限制
```typescript
new LimitTicks(
new Action((node) => {
console.log("尝试操作");
return Status.SUCCESS;
}),
5 // 最多执行5次
)
```
* 规定时间内, 向父节点返回子节点的结果,超时后返回失败
### 叶子节点 (Leaf)
##### LimitTicks - 次数限制
#### Action - 动作节点
执行自定义逻辑:
```typescript
new Action((node) => {
// 直接获取实体
const target = node.getEntity<Character>();
// 访问黑板数据
const ammo = node.get<number>('ammo');
if (target && ammo > 0) {
console.log("攻击目标");
node.set('ammo', ammo - 1);
return Status.SUCCESS;
}
return Status.FAILURE;
})
```
* 执行次数(子节点非RUNNNG状态)内,向父节点返回子节点的结果,超过次数后返回失败
#### Condition - 条件节点
检查条件:
```typescript
new Condition((node) => {
const player = node.getEntity<Player>();
const health = player.health;
return health > 50; // true->SUCCESS, false->FAILURE
})
```
##### Repeat - 重复节点
* 重复执行指定次数
#### WaitTime - 时间等待
```typescript
new WaitTime(2.5) // 等待2.5秒
```
##### RepeatUntilSuccess - 重复直到成功
#### WaitTicks - 帧数等待
```typescript
new WaitTicks(60) // 等待60帧
```
* 设置最大重试次数
##### RepeatUntilFailure - 重复直到失败
* 设置最大重试次数
##### WeightDecorator - 权重装饰节点
* 用于随机选择节点的子节点的按权重随机
### 叶子节点 (LeafNode)
##### LeafNode - 叶子节点基类
##### WaitTicks - 次数等待节点
##### WaitTime - 时间等待节点
### 条件节点 (Condition)
##### Condition - 条件节点基类
* 特殊的叶子节点,子类需实现
```typescript
/**
* 判断是否满足条件
* @returns 是否满足条件
*/
protected abstract isEligible(): boolean;
```
## 黑板系统
@@ -298,230 +178,7 @@ new Action((node) => {
})
```
### 数据查找链
黑板数据按以下顺序查找:
1. 当前节点的本地黑板
2. 父节点的黑板
3. 递归向上查找到根节点
### Memory节点的数据隔离
Memory节点会创建独立的子黑板确保状态隔离
```typescript
const mem1 = new MemSequence(/* ... */);
const mem2 = new MemSequence(/* ... */);
// mem1 和 mem2 的记忆状态完全独立
```
## 完整示例
```typescript
import {
BehaviorTree, Status, Action, Condition,
Sequence, Selector, MemSelector, Parallel,
Inverter, RepeatUntilSuccess, LimitTime
} from 'kunpocc-behaviortree';
interface Character {
health: number;
mana: number;
hasWeapon: boolean;
isInCombat: boolean;
position: { x: number, y: number };
}
const character: Character = {
health: 80,
mana: 50,
hasWeapon: true,
isInCombat: false,
position: { x: 0, y: 0 }
};
// 构建复杂行为树
const behaviorTree = new BehaviorTree(character,
new Selector(
// 战斗行为
new Sequence(
new Condition((node) => {
const char = node.getEntity<Character>();
return char.isInCombat;
}),
new Selector(
// 生命值低时治疗
new Sequence(
new Condition((node) => {
const char = node.getEntity<Character>();
return char.health < 30;
}),
new RepeatUntilSuccess(
new Action((node) => {
const char = node.getEntity<Character>();
if (char.mana >= 10) {
char.health += 20;
char.mana -= 10;
console.log("治疗完成");
return Status.SUCCESS;
}
return Status.FAILURE;
}),
3 // 最多尝试3次
)
),
// 正常攻击
new Sequence(
new Condition((node) => {
const char = node.getEntity<Character>();
return char.hasWeapon;
}),
new LimitTime(
new Action((node) => {
console.log("发起攻击");
return Status.SUCCESS;
}),
1.0 // 攻击最多1秒
)
)
)
),
// 非战斗行为 - 巡逻
new MemSelector(
new Action((node) => {
console.log("巡逻点A");
return Status.SUCCESS;
}),
new Action((node) => {
console.log("巡逻点B");
return Status.SUCCESS;
}),
new Action((node) => {
console.log("巡逻点C");
return Status.SUCCESS;
})
)
)
);
// 执行行为树
console.log("=== 执行行为树 ===");
behaviorTree.tick(); // 输出: "巡逻点A"
// 进入战斗状态
character.isInCombat = true;
character.health = 20; // 低血量
behaviorTree.tick(); // 输出: "治疗完成"
```
## 最佳实践
### 1. 节点设计原则
- **单一职责**: 每个节点只做一件事
- **状态明确**: 明确定义SUCCESS/FAILURE/RUNNING的含义
- **避免副作用**: 尽量避免节点间的隐式依赖
### 2. 性能优化
```typescript
// ✅ 好的做法 - 使用记忆节点避免重复计算
new MemSelector(
expensivePathfinding, // 复杂寻路只计算一次
fallbackBehavior
)
// ❌ 避免 - 每次都重新计算
new Selector(
expensivePathfinding, // 每次tick都会重新计算
fallbackBehavior
)
```
### 3. 黑板使用
```typescript
// ✅ 好的做法 - 合理使用数据层级
new Action((node) => {
// 获取实体
const player = node.getEntity<Player>();
// 临时数据用本地黑板
node.set('temp_result', calculation());
// 共享数据用树级黑板
node.setRoot('current_target', target);
// 配置数据用全局黑板
node.setGlobal('game_config', config);
})
```
### 4. 错误处理
```typescript
// ✅ 明确的错误处理
new Action((node) => {
try {
const result = riskyOperation();
return result ? Status.SUCCESS : Status.FAILURE;
} catch (error) {
console.error('Operation failed:', error);
return Status.FAILURE;
}
})
```
## 测试覆盖
本库包含全面的测试用例,覆盖:
- ✅ 17种节点类型 (100%覆盖)
- ✅ Memory节点状态管理
- ✅ 黑板数据隔离
- ✅ 边界条件处理
- ✅ 复杂嵌套场景
运行测试:
```bash
npm test
```
## API 参考
### 核心类
#### `BehaviorTree<T>`
```typescript
constructor(entity: T, root: IBTNode)
tick(): Status // 执行一次行为树
reset(): void // 重置所有状态
```
#### `Status`
```typescript
enum Status {
SUCCESS = 0,
FAILURE = 1,
RUNNING = 2
}
```
### 节点接口
```typescript
interface IBTNode {
readonly children: IBTNode[];
// 节点黑板
local: IBlackboard;
tick(): 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;
}
```
## 许可证

View File

@@ -1,6 +1,6 @@
{
"name": "kunpocc-behaviortree",
"version": "0.0.6",
"version": "0.0.7",
"description": "行为树",
"main": "./dist/kunpocc-behaviortree.cjs",
"module": "./dist/kunpocc-behaviortree.mjs",

View File

@@ -107,7 +107,7 @@ export namespace BT {
* @param info.name 节点在编辑器中的中文名
* @param info.desc 节点描述信息
*/
export function ActionNode(name: string, info?: { group?: string, name?: string, desc?: string }) {
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 = {
@@ -128,7 +128,7 @@ export namespace BT {
/**
* 条件节点装饰器
*/
export function ConditionNode(name: string, info?: { group?: string, name?: string, desc?: string }) {
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 = {
@@ -149,7 +149,7 @@ export namespace BT {
/**
* 组合节点装饰器
*/
export function CompositeNode(name: string, info?: { group?: string, name?: string, desc?: string }) {
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 = {
@@ -170,7 +170,7 @@ export namespace BT {
/**
* 装饰节点装饰器
*/
export function DecoratorNode(name: string, info?: { group?: string, name?: string, desc?: string }) {
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 = {

View File

@@ -17,7 +17,7 @@ export abstract class LeafNode extends BTNode {
* 次数内返回RUNNING
* 超次返回SUCCESS
*/
@BT.ActionNode("WaitTicks", {
@BT.ClassAction("WaitTicks", {
name: "等待次数",
group: "基础行为节点",
desc: "等待指定次数后返回成功",
@@ -50,7 +50,7 @@ export class WaitTicks extends LeafNode {
* 时间等待节点 时间(秒)
* 时间到后返回SUCCESS否则返回RUNNING
*/
@BT.ActionNode("WaitTime", {
@BT.ClassAction("WaitTime", {
name: "等待时间",
group: "基础行为节点",
desc: "等待指定时间(秒)后返回成功",

View File

@@ -21,7 +21,7 @@ export abstract class Composite extends BTNode {
*
* 遇到 RUNNING 返回 RUNNING 下次从该节点开始
*/
@BT.CompositeNode("Selector", { name: "选择节点", group: "基础组合节点", desc: "选择节点" })
@BT.ClassComposite("Selector", { name: "选择节点", group: "基础组合节点", desc: "选择节点" })
export class Selector extends Composite {
public override _initialize(global: IBlackboard, branch: IBlackboard): void {
super._initialize(global, branch);
@@ -57,7 +57,7 @@ export class Selector extends Composite {
*
* 遇到 RUNNING 返回 RUNNING 下次从该节点开始
*/
@BT.CompositeNode("Sequence", { name: "顺序节点", group: "基础组合节点", desc: "顺序节点" })
@BT.ClassComposite("Sequence", { name: "顺序节点", group: "基础组合节点", desc: "顺序节点" })
export class Sequence extends Composite {
public override _initialize(global: IBlackboard, branch: IBlackboard): void {
super._initialize(global, branch);
@@ -90,7 +90,7 @@ export class Sequence extends Composite {
* 并行节点 从上到下执行 全部执行一遍
* 返回优先级 FAILURE > RUNNING > SUCCESS
*/
@BT.CompositeNode("Parallel", { name: "并行节点", group: "基础组合节点", desc: "同时执行所有子节点,全部成功才返回成功" })
@BT.ClassComposite("Parallel", { name: "并行节点", group: "基础组合节点", desc: "同时执行所有子节点,全部成功才返回成功" })
export class Parallel extends Composite {
public tick(dt: number): Status {
let result = Status.SUCCESS;
@@ -112,7 +112,7 @@ export class Parallel extends Composite {
* 随机选择一个子节点执行
* 返回子节点状态
*/
@BT.CompositeNode("RandomSelector", {
@BT.ClassComposite("RandomSelector", {
name: "随机选择节点",
group: "基础组合节点",
desc: "随机选择一个子节点执行",
@@ -168,7 +168,7 @@ export class RandomSelector extends Composite {
* 并行节点 从上到下执行 全部执行一遍
* 返回优先级 SUCCESS > RUNNING > FAILURE
*/
@BT.CompositeNode("ParallelAnySuccess", {
@BT.ClassComposite("ParallelAnySuccess", {
name: "并行任意成功",
group: "基础组合节点",
desc: "同时执行所有子节点,任意一个成功即返回成功",

View File

@@ -40,7 +40,7 @@ export abstract class ConditionDecorator extends Decorator {
* 第一个Child Node节点, 返回 FAILURE, 本Node向自己的Parent Node也返回 SUCCESS
* 第一个Child Node节点, 返回 SUCCESS, 本Node向自己的Parent Node也返回 FAILURE
*/
@BT.DecoratorNode("Inverter", { name: "反转器", group: "基础装饰节点", desc: "反转子节点的执行结果,成功变失败,失败变成功" })
@BT.ClassDecorator("Inverter", { name: "反转器", group: "基础装饰节点", desc: "反转子节点的执行结果,成功变失败,失败变成功" })
export class Inverter extends Decorator {
public tick(dt: number): Status {
const status = this.children[0]!._execute(dt);
@@ -61,7 +61,7 @@ export class Inverter extends Decorator {
* 规定时间内, 根据Child Node的结果, 本节点向自己的父节点也返回相同的结果
* 超时后, 直接返回 FAILURE
*/
@BT.DecoratorNode("LimitTime", { name: "时间限制器", group: "基础装饰节点", desc: "限制子节点执行时间,超时返回失败" })
@BT.ClassDecorator("LimitTime", { name: "时间限制器", group: "基础装饰节点", desc: "限制子节点执行时间,超时返回失败" })
export class LimitTime extends Decorator {
@BT.prop({ type: BT.ParamType.float, description: "最大时间(秒)", defaultValue: 1 })
protected _max: number = 1;
@@ -96,7 +96,7 @@ export class LimitTime extends Decorator {
* 必须且只能包含一个子节点
* 次数超过后, 直接返回失败; 次数未超过, 返回子节点状态
*/
@BT.DecoratorNode("LimitTicks", { name: "次数限制器", group: "基础装饰节点", desc: "限制子节点执行次数,超过次数返回失败" })
@BT.ClassDecorator("LimitTicks", { name: "次数限制器", group: "基础装饰节点", desc: "限制子节点执行次数,超过次数返回失败" })
export class LimitTicks extends Decorator {
@BT.prop({ type: BT.ParamType.int, description: "最大次数", defaultValue: 1 })
protected _max: number = 1;
@@ -112,12 +112,15 @@ export class LimitTicks extends Decorator {
}
public tick(dt: number): Status {
this._value++;
if (this._value > this._max) {
this.cleanupChild();
return Status.FAILURE;
}
return this.children[0]!._execute(dt);
let status = this.children[0]!._execute(dt);
if (status !== Status.RUNNING) {
this._value++;
}
return status;
}
}
@@ -127,7 +130,7 @@ export class LimitTicks extends Decorator {
* 子节点是成功或失败,累加计数
* 次数超过之后返回子节点状态,否则返回 RUNNING
*/
@BT.DecoratorNode("Repeat", { name: "重复节点", group: "基础装饰节点", desc: "重复执行子节点指定次数" })
@BT.ClassDecorator("Repeat", { name: "重复节点", group: "基础装饰节点", desc: "重复执行子节点指定次数" })
export class Repeat extends Decorator {
@BT.prop({ type: BT.ParamType.int, description: "重复次数", defaultValue: 1, min: 1 })
protected _max: number = 1;
@@ -146,7 +149,7 @@ export class Repeat extends Decorator {
// 执行子节点
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) {
@@ -164,7 +167,7 @@ export class Repeat extends Decorator {
*
* 子节点成功 计数+1
*/
@BT.DecoratorNode("RepeatUntilFailure", { name: "重复直到失败", group: "基础装饰节点", desc: "重复执行子节点直到失败或达到最大次数" })
@BT.ClassDecorator("RepeatUntilFailure", { name: "重复直到失败", group: "基础装饰节点", desc: "重复执行子节点直到失败或达到最大次数" })
export class RepeatUntilFailure extends Decorator {
@BT.prop({ type: BT.ParamType.int, description: "最大重试次数", defaultValue: 1, min: 1 })
protected _max: number = 1;
@@ -202,7 +205,7 @@ export class RepeatUntilFailure extends Decorator {
*
* 子节点失败, 计数+1
*/
@BT.DecoratorNode("RepeatUntilSuccess", { name: "重复直到成功", group: "基础装饰节点", desc: "重复执行子节点直到成功或达到最大次数" })
@BT.ClassDecorator("RepeatUntilSuccess", { name: "重复直到成功", group: "基础装饰节点", desc: "重复执行子节点直到成功或达到最大次数" })
export class RepeatUntilSuccess extends Decorator {
@BT.prop({ type: BT.ParamType.int, description: "最大重试次数", defaultValue: 1, step: 1 })
protected _max: number = 1;
@@ -237,7 +240,7 @@ export class RepeatUntilSuccess extends Decorator {
/**
* 权重装饰节点
*/
@BT.DecoratorNode("WeightDecorator", { name: "权重装饰器", group: "基础装饰节点", desc: "权重装饰节点" })
@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;

View File

@@ -12,4 +12,4 @@ export { Status } from "./behaviortree/header";
// 导出装饰器内容
import { BT } from "./behaviortree/BT";
export const { ActionNode, ConditionNode, CompositeNode, DecoratorNode, prop, ParamType } = BT;
export const { ClassAction, ClassCondition, ClassComposite, ClassDecorator, prop, ParamType } = BT;