节点运行状态添加到黑板中;修复黑板清理数据的逻辑

This commit is contained in:
gongxh
2025-09-04 15:13:24 +08:00
parent 7ed015c6bf
commit 2ab47b2a7b
4 changed files with 24 additions and 52 deletions

View File

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

View File

@@ -12,9 +12,11 @@ export interface IBTNode {
*/
_initialize(root: IBlackboard, parent: IBlackboard): void;
/**
* @internal
*/
_execute(): Status;
tick(): Status;
cleanupAll(): void;
/**
* 优先写入自己的黑板数据, 如果没有则写入父节点的黑板数据
@@ -48,25 +50,14 @@ export abstract class BTNode implements IBTNode {
/** 树根节点的黑板引用 */
protected _root!: IBlackboard;
/** 本节点的的黑板引用 可能等于 _parent */
protected _local!: IBlackboard;
private _isRunning: boolean;
/**
* 创建
* @param children 子节点列表
*/
constructor(children?: IBTNode[]) {
this.children = children ? [...children] : [];
this._isRunning = false;
}
/**
* 打开节点
* @param tree 行为树
*
* @internal
*/
public _initialize(root: IBlackboard, parent: IBlackboard): void {
this._root = root;
// 在需要的节点中重写创建新的local
@@ -74,13 +65,13 @@ export abstract class BTNode implements IBTNode {
}
/**
* 执行节点
* @internal
*/
public _execute(): Status {
// 首次执行时初始化
if (!this._isRunning) {
this._isRunning = true;
const isRunning = this._local.openNodes.get(this) || false;
if (!isRunning) {
this._local.openNodes.set(this, true);
this.open();
}
@@ -89,7 +80,7 @@ export abstract class BTNode implements IBTNode {
// 执行完成时清理
if (status !== Status.RUNNING) {
this._isRunning = false;
this._local.openNodes.delete(this);
this.close();
}
@@ -115,20 +106,6 @@ export abstract class BTNode implements IBTNode {
*/
protected close(): void { }
/**
* 递归清理节点及其所有子节点的状态
* 用于行为树中断时清理所有节点状态
*/
public cleanupAll(): void {
// 清理基础状态
this._isRunning = false;
// 递归清理所有子节点
for (const child of this.children) {
child.cleanupAll();
}
}
public getEntity<T>(): T {
return this._local.getEntity();
}

View File

@@ -58,8 +58,6 @@ export class BehaviorTree<T> {
* 清空黑板并重置所有节点状态
*/
public reset(): void {
this._blackboard.clear();
// 重置所有节点的状态
this._root.cleanupAll();
this._blackboard.clean();
}
}

View File

@@ -6,6 +6,7 @@
* 专门用于存储和管理行为树执行过程中的共享数据
*/
import { IBTNode } from "../kunpocc-behaviortree";
/**
* 黑板数据接口
@@ -16,8 +17,10 @@ export interface IBlackboard {
set<T>(key: string, value: T): void;
delete(key: string): void;
has(key: string): boolean;
clear(): void;
clean(): void;
createChild(scope?: number): IBlackboard;
/** @internal */
openNodes: WeakMap<IBTNode, boolean>;
}
/**
@@ -28,6 +31,12 @@ export class Blackboard implements IBlackboard {
public parent?: Blackboard | undefined;
public children = new Set<Blackboard>();
/**
* 正在运行中的节点
* @internal
*/
public openNodes = new WeakMap<IBTNode, boolean>();
/** 实体 */
private readonly _entity: any;
public getEntity<T>(): T {
@@ -69,24 +78,12 @@ export class Blackboard implements IBlackboard {
return new Blackboard(this);
}
public clear(): void {
// 从父黑板中删除自己
if (this.parent) {
this.parent.children.delete(this);
}
// 清理所有子黑板
this.children.forEach(child => {
child.parent = undefined;
});
this.children.clear();
// 断开父级引用
this.parent = undefined;
public clean(): void {
// 清空当前黑板数据
this._data.clear();
// 重置运行状态
this.openNodes = new WeakMap<IBTNode, boolean>();
}
}