Feature/runtime cdn and plugin loader (#240)
* feat(ui): 完善 UI 布局系统和编辑器可视化工具 * refactor: 移除 ModuleRegistry,统一使用 PluginManager 插件系统 * fix: 修复 CodeQL 警告并提升测试覆盖率 * refactor: 分离运行时入口点,解决 runtime bundle 包含 React 的问题 * fix(ci): 添加 editor-core 和 editor-runtime 到 CI 依赖构建步骤 * docs: 完善 ServiceContainer 文档,新增 Symbol.for 模式和 @InjectProperty 说明 * fix(ci): 修复 type-check 失败问题 * fix(ci): 修复类型检查失败问题 * fix(ci): 修复类型检查失败问题 * fix(ci): behavior-tree 构建添加 @tauri-apps 外部依赖 * fix(ci): behavior-tree 添加 @tauri-apps/plugin-fs 类型依赖 * fix(ci): platform-web 添加缺失的 behavior-tree 依赖 * fix(lint): 移除正则表达式中不必要的转义字符
This commit is contained in:
128
packages/editor-core/src/Services/SceneTemplateRegistry.ts
Normal file
128
packages/editor-core/src/Services/SceneTemplateRegistry.ts
Normal file
@@ -0,0 +1,128 @@
|
||||
import type { Scene, Entity } from '@esengine/ecs-framework';
|
||||
|
||||
/**
|
||||
* 默认实体创建函数类型
|
||||
* Default entity creator function type
|
||||
*/
|
||||
export type DefaultEntityCreator = (scene: Scene) => Entity | null;
|
||||
|
||||
/**
|
||||
* 场景模板配置
|
||||
* Scene template configuration
|
||||
*/
|
||||
export interface SceneTemplate {
|
||||
/** 模板名称 / Template name */
|
||||
name: string;
|
||||
/** 模板描述 / Template description */
|
||||
description?: string;
|
||||
/** 默认实体创建器列表 / Default entity creators */
|
||||
defaultEntities: DefaultEntityCreator[];
|
||||
}
|
||||
|
||||
/**
|
||||
* 场景模板注册表
|
||||
* Registry for scene templates that define default entities
|
||||
*
|
||||
* This allows the editor-app to register what entities should be created
|
||||
* when a new scene is created, without editor-core needing to know about
|
||||
* specific components like CameraComponent.
|
||||
*
|
||||
* 这允许 editor-app 注册新建场景时应该创建的实体,
|
||||
* 而 editor-core 不需要知道具体的组件如 CameraComponent。
|
||||
*/
|
||||
export class SceneTemplateRegistry {
|
||||
private static templates: Map<string, SceneTemplate> = new Map();
|
||||
private static defaultTemplateName = 'default';
|
||||
|
||||
/**
|
||||
* 注册场景模板
|
||||
* Register a scene template
|
||||
*/
|
||||
static registerTemplate(template: SceneTemplate): void {
|
||||
this.templates.set(template.name, template);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册默认实体创建器到默认模板
|
||||
* Register a default entity creator to the default template
|
||||
*/
|
||||
static registerDefaultEntity(creator: DefaultEntityCreator): void {
|
||||
let defaultTemplate = this.templates.get(this.defaultTemplateName);
|
||||
if (!defaultTemplate) {
|
||||
defaultTemplate = {
|
||||
name: this.defaultTemplateName,
|
||||
description: 'Default scene template',
|
||||
defaultEntities: []
|
||||
};
|
||||
this.templates.set(this.defaultTemplateName, defaultTemplate);
|
||||
}
|
||||
defaultTemplate.defaultEntities.push(creator);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取场景模板
|
||||
* Get a scene template by name
|
||||
*/
|
||||
static getTemplate(name: string): SceneTemplate | undefined {
|
||||
return this.templates.get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取默认模板
|
||||
* Get the default template
|
||||
*/
|
||||
static getDefaultTemplate(): SceneTemplate | undefined {
|
||||
return this.templates.get(this.defaultTemplateName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置默认模板名称
|
||||
* Set the default template name
|
||||
*/
|
||||
static setDefaultTemplateName(name: string): void {
|
||||
this.defaultTemplateName = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有模板名称
|
||||
* Get all template names
|
||||
*/
|
||||
static getTemplateNames(): string[] {
|
||||
return Array.from(this.templates.keys());
|
||||
}
|
||||
|
||||
/**
|
||||
* 为场景创建默认实体
|
||||
* Create default entities for a scene using a template
|
||||
*
|
||||
* @param scene - 目标场景 / Target scene
|
||||
* @param templateName - 模板名称,默认使用默认模板 / Template name, uses default if not specified
|
||||
* @returns 创建的实体列表 / List of created entities
|
||||
*/
|
||||
static createDefaultEntities(scene: Scene, templateName?: string): Entity[] {
|
||||
const template = templateName
|
||||
? this.templates.get(templateName)
|
||||
: this.getDefaultTemplate();
|
||||
|
||||
if (!template) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const entities: Entity[] = [];
|
||||
for (const creator of template.defaultEntities) {
|
||||
const entity = creator(scene);
|
||||
if (entity) {
|
||||
entities.push(entity);
|
||||
}
|
||||
}
|
||||
return entities;
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除所有模板
|
||||
* Clear all templates
|
||||
*/
|
||||
static clear(): void {
|
||||
this.templates.clear();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user