组件注册与添加

This commit is contained in:
YHH
2025-10-14 23:42:06 +08:00
parent 1cf5641c4c
commit 3a5e73266e
10 changed files with 481 additions and 4 deletions

View File

@@ -0,0 +1,47 @@
import { Injectable, IService, Component } from '@esengine/ecs-framework';
export interface ComponentTypeInfo {
name: string;
type: new (...args: any[]) => Component;
category?: string;
description?: string;
}
/**
* 管理编辑器中可用的组件类型
*/
@Injectable()
export class ComponentRegistry implements IService {
private components: Map<string, ComponentTypeInfo> = new Map();
public dispose(): void {
this.components.clear();
}
public register(info: ComponentTypeInfo): void {
this.components.set(info.name, info);
}
public unregister(name: string): void {
this.components.delete(name);
}
public getComponent(name: string): ComponentTypeInfo | undefined {
return this.components.get(name);
}
public getAllComponents(): ComponentTypeInfo[] {
return Array.from(this.components.values());
}
public getComponentsByCategory(category: string): ComponentTypeInfo[] {
return this.getAllComponents().filter(c => c.category === category);
}
public createInstance(name: string, ...args: any[]): Component | null {
const info = this.components.get(name);
if (!info) return null;
return new info.type(...args);
}
}

View File

@@ -11,5 +11,6 @@ export * from './Services/UIRegistry';
export * from './Services/MessageHub';
export * from './Services/SerializerRegistry';
export * from './Services/EntityStoreService';
export * from './Services/ComponentRegistry';
export * from './Types/UITypes';