kunpolibrary/src/behaviortree/Blackboard.ts

105 lines
2.6 KiB
TypeScript
Raw Normal View History

2025-02-20 11:27:28 +08:00
/**
*
*/
interface ITreeData {
nodeMemory: { [nodeScope: string]: any };
openNodes: any[];
}
/** 平台 */
export class Blackboard {
2025-03-07 16:02:00 +08:00
/** 行为树打断保护 */
public interruptDefend: boolean = false;
/** 打断行为树的标记 */
public interrupt: boolean = false;
/** 基础记忆 @internal */
2025-02-20 11:27:28 +08:00
private _baseMemory: any;
2025-03-07 16:02:00 +08:00
/** 树记忆 @internal */
2025-02-20 11:27:28 +08:00
private _treeMemory: { [treeScope: string]: ITreeData };
constructor() {
this._baseMemory = {};
this._treeMemory = {};
}
2025-03-07 16:02:00 +08:00
/**
*
*/
public clear(): void {
2025-02-20 11:27:28 +08:00
this._baseMemory = {};
this._treeMemory = {};
}
2025-03-07 16:02:00 +08:00
/**
*
* @param key
* @param value
* @param treeScope
* @param nodeScope
*/
public set(key: string, value: any, treeScope?: string, nodeScope?: string): void {
2025-02-20 11:27:28 +08:00
let memory = this._getMemory(treeScope, nodeScope);
memory[key] = value;
}
2025-03-07 16:02:00 +08:00
/**
*
* @param key
* @param treeScope
* @param nodeScope
* @returns
*/
public get(key: string, treeScope?: string, nodeScope?: string): any {
2025-02-20 11:27:28 +08:00
let memory = this._getMemory(treeScope, nodeScope);
return memory[key];
}
2025-03-07 16:02:00 +08:00
/**
*
* @param treeScope
* @returns
* @internal
*/
2025-02-20 11:27:28 +08:00
private _getTreeMemory(treeScope: string): ITreeData {
if (!this._treeMemory[treeScope]) {
this._treeMemory[treeScope] = {
nodeMemory: {},
openNodes: [],
};
}
return this._treeMemory[treeScope];
}
2025-03-07 16:02:00 +08:00
/**
*
* @param treeMemory
* @param nodeScope
* @returns
* @internal
*/
2025-02-20 11:27:28 +08:00
private _getNodeMemory(treeMemory: ITreeData, nodeScope: string): { [key: string]: any } {
let memory = treeMemory.nodeMemory;
if (!memory[nodeScope]) {
memory[nodeScope] = {};
}
return memory[nodeScope];
}
2025-03-07 16:02:00 +08:00
/**
*
* @param treeScope
* @param nodeScope
* @returns
* @internal
*/
2025-02-20 11:27:28 +08:00
private _getMemory(treeScope?: string, nodeScope?: string): { [key: string]: any } {
let memory = this._baseMemory;
if (treeScope) {
memory = this._getTreeMemory(treeScope);
if (nodeScope) {
memory = this._getNodeMemory(memory, nodeScope);
}
}
return memory;
}
}