mirror of
https://gitee.com/dgflash/oops-plugin-framework.git
synced 2026-05-16 11:18:25 +08:00
2. 存储模块性能提升,添加LRU缓存、批量操作支持,优化内存使用 3. 多语言模块性能与内存管理优化,组件查询性能提升 4. 时间模块类型安全与性能优化,使用泛型替代any,对象池机制减少内存分配 5. 事件系统修复双重注册、重复注册等严重问题,实现EventData对象池减少GC压力 6. RandomManager修复4个逻辑BUG,包括边界问题和越界问题 7. 音频模块内存与性能优化,避免重复加载,优化数据结构,添加完整清理机制 8. CCView与CCViewVM合并,支持按需启用MVVM 9. Collection模块优化,AsyncQueue添加队列容量限制,Collection查询性能提升 10. ECS系统全面优化,对象池复用减少内存分配,循环性能提升 11. 优化MVVM组件性能
108 lines
2.8 KiB
TypeScript
108 lines
2.8 KiB
TypeScript
import { BTreeNode } from './BTreeNode';
|
|
import type { IControl } from './IControl';
|
|
|
|
let countUnnamed = 0;
|
|
|
|
/** 行为树 */
|
|
export class BehaviorTree implements IControl {
|
|
private readonly title: string;
|
|
|
|
/** 根节点 */
|
|
private readonly _root: BTreeNode;
|
|
/** 当前执行节点 */
|
|
private _current: BTreeNode | null = null;
|
|
/** 是否已开始执行 */
|
|
private _started = false;
|
|
/** 外部参数对象 */
|
|
private _blackboard: any;
|
|
|
|
/** 是否已开始执行 */
|
|
get started(): boolean {
|
|
return this._started;
|
|
}
|
|
|
|
/**
|
|
* 构造函数
|
|
* @param node 根节点
|
|
* @param blackboard 外部参数对象
|
|
*/
|
|
constructor(node: BTreeNode, blackboard?: any) {
|
|
countUnnamed += 1;
|
|
this.title = node.constructor.name + '(btree_' + (countUnnamed) + ')';
|
|
this._root = node;
|
|
this._blackboard = blackboard;
|
|
}
|
|
|
|
/** 设置行为逻辑中的共享数据 */
|
|
setObject(blackboard: any) {
|
|
this._blackboard = blackboard;
|
|
}
|
|
|
|
/** 执行行为树逻辑 */
|
|
run() {
|
|
if (this._started) {
|
|
console.error(`行为树【${this.title}】未调用步骤,在最后一次调用步骤时有一个任务未完成`);
|
|
}
|
|
|
|
this._started = true;
|
|
const node = BehaviorTree.getNode(this._root);
|
|
this._current = node;
|
|
node.setControl(this);
|
|
node.start(this._blackboard);
|
|
node.run(this._blackboard);
|
|
}
|
|
|
|
running(node: BTreeNode) {
|
|
this._started = false;
|
|
}
|
|
|
|
success() {
|
|
if (this._current) {
|
|
this._current.end(this._blackboard);
|
|
}
|
|
this._started = false;
|
|
}
|
|
|
|
fail() {
|
|
if (this._current) {
|
|
this._current.end(this._blackboard);
|
|
}
|
|
this._started = false;
|
|
}
|
|
|
|
/** 清理行为树资源 */
|
|
destroy() {
|
|
this._current = null;
|
|
this._blackboard = null;
|
|
this._started = false;
|
|
}
|
|
|
|
/** ---------------------------------------------------------------------------------------------------- */
|
|
|
|
static _registeredNodes: Map<string, BTreeNode> = new Map<string, BTreeNode>();
|
|
|
|
/** 注册节点 */
|
|
static register(name: string, node: BTreeNode) {
|
|
this._registeredNodes.set(name, node);
|
|
}
|
|
|
|
/** 注销节点 */
|
|
static unregister(name: string) {
|
|
this._registeredNodes.delete(name);
|
|
}
|
|
|
|
/** 清理所有注册的节点 */
|
|
static clearAll() {
|
|
this._registeredNodes.clear();
|
|
}
|
|
|
|
/** 获取节点 */
|
|
static getNode(name: string | BTreeNode): BTreeNode {
|
|
const node = name instanceof BTreeNode ? name : this._registeredNodes.get(name);
|
|
if (!node) {
|
|
throw new Error(`无法找到节点【${name}】,可能它没有注册过`);
|
|
}
|
|
return node;
|
|
}
|
|
}
|