From 2ab47b2a7bea0b288b07d7997883eb1c08227907 Mon Sep 17 00:00:00 2001 From: gongxh Date: Thu, 4 Sep 2025 15:13:24 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8A=82=E7=82=B9=E8=BF=90=E8=A1=8C=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E6=B7=BB=E5=8A=A0=E5=88=B0=E9=BB=91=E6=9D=BF=E4=B8=AD?= =?UTF-8?q?;=E4=BF=AE=E5=A4=8D=E9=BB=91=E6=9D=BF=E6=B8=85=E7=90=86?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=9A=84=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/behaviortree/BTNode/BTNode.ts | 39 +++++++------------------------ src/behaviortree/BehaviorTree.ts | 4 +--- src/behaviortree/Blackboard.ts | 31 +++++++++++------------- 4 files changed, 24 insertions(+), 52 deletions(-) diff --git a/package.json b/package.json index 212f3dd..8b3f9d5 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/behaviortree/BTNode/BTNode.ts b/src/behaviortree/BTNode/BTNode.ts index 2eb2431..a77bb78 100644 --- a/src/behaviortree/BTNode/BTNode.ts +++ b/src/behaviortree/BTNode/BTNode.ts @@ -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 { return this._local.getEntity(); } diff --git a/src/behaviortree/BehaviorTree.ts b/src/behaviortree/BehaviorTree.ts index 1c11f71..3598a35 100644 --- a/src/behaviortree/BehaviorTree.ts +++ b/src/behaviortree/BehaviorTree.ts @@ -58,8 +58,6 @@ export class BehaviorTree { * 清空黑板并重置所有节点状态 */ public reset(): void { - this._blackboard.clear(); - // 重置所有节点的状态 - this._root.cleanupAll(); + this._blackboard.clean(); } } \ No newline at end of file diff --git a/src/behaviortree/Blackboard.ts b/src/behaviortree/Blackboard.ts index d692de3..8ea7973 100644 --- a/src/behaviortree/Blackboard.ts +++ b/src/behaviortree/Blackboard.ts @@ -6,6 +6,7 @@ * 专门用于存储和管理行为树执行过程中的共享数据 */ +import { IBTNode } from "../kunpocc-behaviortree"; /** * 黑板数据接口 @@ -16,8 +17,10 @@ export interface IBlackboard { set(key: string, value: T): void; delete(key: string): void; has(key: string): boolean; - clear(): void; + clean(): void; createChild(scope?: number): IBlackboard; + /** @internal */ + openNodes: WeakMap; } /** @@ -28,6 +31,12 @@ export class Blackboard implements IBlackboard { public parent?: Blackboard | undefined; public children = new Set(); + /** + * 正在运行中的节点 + * @internal + */ + public openNodes = new WeakMap(); + /** 实体 */ private readonly _entity: any; public getEntity(): 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(); } }