Feature/ecs behavior tree (#188)
* feat(behavior-tree): 完全 ECS 化的行为树系统 * feat(editor-app): 添加行为树可视化编辑器 * chore: 移除 Cocos Creator 扩展目录 * feat(editor-app): 行为树编辑器功能增强 * fix(editor-app): 修复 TypeScript 类型错误 * feat(editor-app): 使用 FlexLayout 重构面板系统并优化资产浏览器 * feat(editor-app): 改进编辑器UI样式并修复行为树执行顺序 * feat(behavior-tree,editor-app): 添加装饰器系统并优化编辑器性能 * feat(behavior-tree,editor-app): 添加属性绑定系统 * feat(editor-app,behavior-tree): 优化编辑器UI并改进行为树功能 * feat(editor-app,behavior-tree): 添加全局黑板系统并增强资产浏览器功能 * feat(behavior-tree,editor-app): 添加运行时资产导出系统 * feat(behavior-tree,editor-app): 添加SubTree系统和资产选择器 * feat(behavior-tree,editor-app): 优化系统架构并改进编辑器文件管理 * fix(behavior-tree,editor-app): 修复SubTree节点错误显示空节点警告 * fix(editor-app): 修复局部黑板类型定义文件扩展名错误
This commit is contained in:
183
packages/editor-app/src/plugins/BehaviorTreePlugin.ts
Normal file
183
packages/editor-app/src/plugins/BehaviorTreePlugin.ts
Normal file
@@ -0,0 +1,183 @@
|
||||
import type { Core, ServiceContainer } from '@esengine/ecs-framework';
|
||||
import { IEditorPlugin, EditorPluginCategory, PanelPosition, MessageHub } from '@esengine/editor-core';
|
||||
import type { MenuItem, ToolbarItem, PanelDescriptor, ISerializer } from '@esengine/editor-core';
|
||||
import { BehaviorTreePersistence } from '@esengine/behavior-tree';
|
||||
|
||||
/**
|
||||
* 行为树编辑器插件
|
||||
*
|
||||
* 提供行为树的可视化编辑功能
|
||||
*/
|
||||
export class BehaviorTreePlugin implements IEditorPlugin {
|
||||
readonly name = '@esengine/behavior-tree-editor';
|
||||
readonly version = '1.0.0';
|
||||
readonly displayName = 'Behavior Tree Editor';
|
||||
readonly category = EditorPluginCategory.Tool;
|
||||
readonly description = 'Visual behavior tree editor for AI development';
|
||||
readonly icon = 'Network';
|
||||
|
||||
private core?: Core;
|
||||
private services?: ServiceContainer;
|
||||
private messageHub?: MessageHub;
|
||||
|
||||
async install(core: Core, services: ServiceContainer): Promise<void> {
|
||||
this.core = core;
|
||||
this.services = services;
|
||||
this.messageHub = services.resolve(MessageHub);
|
||||
}
|
||||
|
||||
async uninstall(): Promise<void> {
|
||||
this.core = undefined;
|
||||
this.services = undefined;
|
||||
}
|
||||
|
||||
registerMenuItems(): MenuItem[] {
|
||||
return [
|
||||
{
|
||||
id: 'view-behavior-tree-editor',
|
||||
label: 'Behavior Tree Editor',
|
||||
parentId: 'window',
|
||||
onClick: () => {
|
||||
this.messageHub?.publish('ui:openWindow', { windowId: 'behavior-tree-editor' });
|
||||
},
|
||||
icon: 'Network',
|
||||
order: 50
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
registerToolbar(): ToolbarItem[] {
|
||||
return [
|
||||
{
|
||||
id: 'toolbar-new-behavior-tree',
|
||||
label: 'New Behavior Tree',
|
||||
groupId: 'behavior-tree-tools',
|
||||
icon: 'FilePlus',
|
||||
onClick: () => this.createNewBehaviorTree(),
|
||||
order: 10
|
||||
},
|
||||
{
|
||||
id: 'toolbar-save-behavior-tree',
|
||||
label: 'Save Behavior Tree',
|
||||
groupId: 'behavior-tree-tools',
|
||||
icon: 'Save',
|
||||
onClick: () => this.saveBehaviorTree(),
|
||||
order: 20
|
||||
},
|
||||
{
|
||||
id: 'toolbar-validate-behavior-tree',
|
||||
label: 'Validate Behavior Tree',
|
||||
groupId: 'behavior-tree-tools',
|
||||
icon: 'CheckCircle',
|
||||
onClick: () => this.validateBehaviorTree(),
|
||||
order: 30
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
registerPanels(): PanelDescriptor[] {
|
||||
return [
|
||||
{
|
||||
id: 'panel-behavior-tree-editor',
|
||||
title: 'Behavior Tree Editor',
|
||||
position: PanelPosition.Center,
|
||||
resizable: true,
|
||||
closable: true,
|
||||
icon: 'Network',
|
||||
order: 10
|
||||
},
|
||||
{
|
||||
id: 'panel-behavior-tree-nodes',
|
||||
title: 'Behavior Tree Nodes',
|
||||
position: PanelPosition.Left,
|
||||
defaultSize: 250,
|
||||
resizable: true,
|
||||
closable: true,
|
||||
icon: 'Package',
|
||||
order: 20
|
||||
},
|
||||
{
|
||||
id: 'panel-behavior-tree-properties',
|
||||
title: 'Node Properties',
|
||||
position: PanelPosition.Right,
|
||||
defaultSize: 300,
|
||||
resizable: true,
|
||||
closable: true,
|
||||
icon: 'Settings',
|
||||
order: 20
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
getSerializers(): ISerializer[] {
|
||||
return [
|
||||
{
|
||||
serialize: (data: any) => {
|
||||
// 使用行为树持久化工具
|
||||
const result = BehaviorTreePersistence.serialize(data.entity, data.pretty ?? true);
|
||||
if (typeof result === 'string') {
|
||||
const encoder = new TextEncoder();
|
||||
return encoder.encode(result);
|
||||
}
|
||||
return result;
|
||||
},
|
||||
deserialize: (data: Uint8Array) => {
|
||||
// 返回原始数据,让上层决定如何反序列化到场景
|
||||
return data;
|
||||
},
|
||||
getSupportedType: () => 'behavior-tree'
|
||||
}
|
||||
];
|
||||
}
|
||||
|
||||
async onEditorReady(): Promise<void> {
|
||||
console.log('[BehaviorTreePlugin] Editor is ready');
|
||||
}
|
||||
|
||||
async onProjectOpen(projectPath: string): Promise<void> {
|
||||
console.log(`[BehaviorTreePlugin] Project opened: ${projectPath}`);
|
||||
}
|
||||
|
||||
async onProjectClose(): Promise<void> {
|
||||
console.log('[BehaviorTreePlugin] Project closed');
|
||||
}
|
||||
|
||||
async onBeforeSave(filePath: string, data: any): Promise<void> {
|
||||
// 验证行为树数据
|
||||
if (filePath.endsWith('.behavior-tree.json')) {
|
||||
console.log('[BehaviorTreePlugin] Validating behavior tree before save');
|
||||
const isValid = BehaviorTreePersistence.validate(JSON.stringify(data));
|
||||
if (!isValid) {
|
||||
throw new Error('Invalid behavior tree data');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async onAfterSave(filePath: string): Promise<void> {
|
||||
if (filePath.endsWith('.behavior-tree.json')) {
|
||||
console.log(`[BehaviorTreePlugin] Behavior tree saved: ${filePath}`);
|
||||
}
|
||||
}
|
||||
|
||||
// 私有方法
|
||||
|
||||
private createNewBehaviorTree(): void {
|
||||
console.log('[BehaviorTreePlugin] Creating new behavior tree');
|
||||
// TODO: 实现创建新行为树的逻辑
|
||||
}
|
||||
|
||||
private openBehaviorTree(): void {
|
||||
console.log('[BehaviorTreePlugin] Opening behavior tree');
|
||||
// TODO: 实现打开行为树的逻辑
|
||||
}
|
||||
|
||||
private saveBehaviorTree(): void {
|
||||
console.log('[BehaviorTreePlugin] Saving behavior tree');
|
||||
// TODO: 实现保存行为树的逻辑
|
||||
}
|
||||
|
||||
private validateBehaviorTree(): void {
|
||||
console.log('[BehaviorTreePlugin] Validating behavior tree');
|
||||
// TODO: 实现验证行为树的逻辑
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user