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:
@@ -1,15 +1,13 @@
|
||||
/**
|
||||
* Browser Runtime Entry Point
|
||||
* 浏览器运行时入口
|
||||
*
|
||||
* Uses the same Rust WASM engine as the editor
|
||||
* 使用与编辑器相同的 Rust WASM 引擎
|
||||
*/
|
||||
|
||||
import { Core, Scene, SceneSerializer } from '@esengine/ecs-framework';
|
||||
import { EngineBridge, EngineRenderSystem, CameraSystem } from '@esengine/ecs-engine-bindgen';
|
||||
import { TransformComponent, SpriteComponent, SpriteAnimatorComponent, SpriteAnimatorSystem, CameraComponent } from '@esengine/ecs-components';
|
||||
import { EngineBridge } from '@esengine/ecs-engine-bindgen';
|
||||
import { TransformComponent, SpriteComponent, SpriteAnimatorComponent, CameraComponent } from '@esengine/ecs-components';
|
||||
import { AssetManager, EngineIntegration } from '@esengine/asset-system';
|
||||
import { initializeRuntime, createRuntimeSystems, type RuntimeSystems } from './RuntimeSystems';
|
||||
|
||||
interface RuntimeConfig {
|
||||
canvasId: string;
|
||||
@@ -19,93 +17,63 @@ interface RuntimeConfig {
|
||||
|
||||
class BrowserRuntime {
|
||||
private bridge: EngineBridge;
|
||||
private cameraSystem: CameraSystem;
|
||||
private renderSystem: EngineRenderSystem;
|
||||
private animatorSystem: SpriteAnimatorSystem;
|
||||
private systems: RuntimeSystems | null = null;
|
||||
private animationId: number | null = null;
|
||||
private assetManager: AssetManager;
|
||||
private engineIntegration: EngineIntegration;
|
||||
|
||||
constructor(config: RuntimeConfig) {
|
||||
// Initialize Core if not already created
|
||||
if (!Core.Instance) {
|
||||
Core.create();
|
||||
}
|
||||
|
||||
// Initialize Core.scene if not already initialized
|
||||
if (!Core.scene) {
|
||||
const runtimeScene = new Scene({ name: 'Runtime Scene' });
|
||||
Core.setScene(runtimeScene);
|
||||
}
|
||||
|
||||
// Initialize Rust WASM engine bridge
|
||||
this.bridge = new EngineBridge({
|
||||
canvasId: config.canvasId,
|
||||
width: config.width || window.innerWidth,
|
||||
height: config.height || window.innerHeight
|
||||
});
|
||||
|
||||
// Initialize asset system
|
||||
// 初始化资产系统
|
||||
this.assetManager = new AssetManager();
|
||||
this.engineIntegration = new EngineIntegration(this.assetManager, this.bridge);
|
||||
|
||||
// Add camera system (updates before render)
|
||||
this.cameraSystem = new CameraSystem(this.bridge);
|
||||
Core.scene!.addSystem(this.cameraSystem);
|
||||
|
||||
// Add sprite animator system
|
||||
this.animatorSystem = new SpriteAnimatorSystem();
|
||||
Core.scene!.addSystem(this.animatorSystem);
|
||||
|
||||
// Add render system
|
||||
this.renderSystem = new EngineRenderSystem(this.bridge, TransformComponent);
|
||||
Core.scene!.addSystem(this.renderSystem);
|
||||
}
|
||||
|
||||
async initialize(wasmModule: any): Promise<void> {
|
||||
await this.bridge.initializeWithModule(wasmModule);
|
||||
|
||||
// Set path resolver for browser asset proxy
|
||||
// 设置浏览器资产代理的路径解析器
|
||||
this.bridge.setPathResolver((path: string) => {
|
||||
// If already a URL, return as-is
|
||||
if (path.startsWith('http://') || path.startsWith('https://') || path.startsWith('/asset?')) {
|
||||
return path;
|
||||
}
|
||||
// Use asset proxy endpoint for local file paths
|
||||
return `/asset?path=${encodeURIComponent(path)}`;
|
||||
});
|
||||
|
||||
// Disable editor tools for game runtime
|
||||
this.bridge.setShowGrid(false);
|
||||
this.bridge.setShowGizmos(false);
|
||||
|
||||
// 初始化模块系统
|
||||
await initializeRuntime(Core);
|
||||
|
||||
// 创建运行时系统
|
||||
this.systems = createRuntimeSystems(Core.scene!, this.bridge);
|
||||
}
|
||||
|
||||
async loadScene(sceneUrl: string): Promise<void> {
|
||||
try {
|
||||
const response = await fetch(sceneUrl);
|
||||
const sceneJson = await response.text();
|
||||
const response = await fetch(sceneUrl);
|
||||
const sceneJson = await response.text();
|
||||
|
||||
if (!Core.scene) {
|
||||
throw new Error('Core.scene not initialized');
|
||||
}
|
||||
|
||||
SceneSerializer.deserialize(Core.scene, sceneJson, {
|
||||
strategy: 'replace',
|
||||
preserveIds: true
|
||||
});
|
||||
|
||||
// Textures are now loaded automatically by EngineRenderSystem
|
||||
// via Rust engine's path-based texture loading
|
||||
// 纹理现在由EngineRenderSystem通过Rust引擎的路径加载自动处理
|
||||
|
||||
// Auto-play animations are started by SpriteAnimatorSystem.onAdded
|
||||
// 自动播放动画由SpriteAnimatorSystem.onAdded启动
|
||||
} catch (error) {
|
||||
console.error('Failed to load scene:', error);
|
||||
throw error;
|
||||
if (!Core.scene) {
|
||||
throw new Error('Core.scene not initialized');
|
||||
}
|
||||
|
||||
SceneSerializer.deserialize(Core.scene, sceneJson, {
|
||||
strategy: 'replace',
|
||||
preserveIds: true
|
||||
});
|
||||
}
|
||||
|
||||
start(): void {
|
||||
@@ -117,8 +85,6 @@ class BrowserRuntime {
|
||||
const deltaTime = (currentTime - lastTime) / 1000;
|
||||
lastTime = currentTime;
|
||||
|
||||
// Update Core (includes Time.update and all scenes)
|
||||
// Texture loading is handled automatically by EngineRenderSystem
|
||||
Core.update(deltaTime);
|
||||
|
||||
this.animationId = requestAnimationFrame(loop);
|
||||
@@ -145,14 +111,14 @@ class BrowserRuntime {
|
||||
getEngineIntegration(): EngineIntegration {
|
||||
return this.engineIntegration;
|
||||
}
|
||||
|
||||
getSystems(): RuntimeSystems | null {
|
||||
return this.systems;
|
||||
}
|
||||
}
|
||||
|
||||
// Export everything on a single object for IIFE bundle
|
||||
export default {
|
||||
create: (config: RuntimeConfig) => {
|
||||
const runtime = new BrowserRuntime(config);
|
||||
return runtime;
|
||||
},
|
||||
create: (config: RuntimeConfig) => new BrowserRuntime(config),
|
||||
BrowserRuntime,
|
||||
Core,
|
||||
TransformComponent,
|
||||
|
||||
Reference in New Issue
Block a user