* 初始化运行时 * Initialize runtime
()
| 355 | * Initialize runtime |
| 356 | */ |
| 357 | async initialize(): Promise<void> { |
| 358 | if (this._state.initialized) { |
| 359 | return; |
| 360 | } |
| 361 | |
| 362 | try { |
| 363 | // 1. 初始化平台 |
| 364 | await this._platform.initialize({ |
| 365 | canvasId: this._config.canvasId, |
| 366 | width: this._config.width, |
| 367 | height: this._config.height, |
| 368 | isEditor: this._platform.isEditorMode() |
| 369 | }); |
| 370 | |
| 371 | // 2. 获取 WASM 模块并创建引擎桥接 |
| 372 | const wasmModule = await this._platform.getWasmModule(); |
| 373 | this._bridge = new EngineBridge({ |
| 374 | canvasId: this._config.canvasId, |
| 375 | width: this._config.width, |
| 376 | height: this._config.height |
| 377 | }); |
| 378 | await this._bridge.initializeWithModule(wasmModule); |
| 379 | |
| 380 | // 3. 设置路径解析器 |
| 381 | this._bridge.setPathResolver((path: string) => { |
| 382 | return this._platform.pathResolver.resolve(path); |
| 383 | }); |
| 384 | |
| 385 | // 4. 初始化 ECS Core |
| 386 | if (!Core.Instance) { |
| 387 | Core.create({ debug: false }); |
| 388 | } |
| 389 | |
| 390 | // 5. 创建或获取场景 |
| 391 | if (Core.scene) { |
| 392 | this._scene = Core.scene as Scene; |
| 393 | } else { |
| 394 | this._scene = new Scene({ name: 'GameScene' }); |
| 395 | Core.setScene(this._scene); |
| 396 | } |
| 397 | |
| 398 | // 编辑器模式下设置 isEditorMode,延迟组件生命周期回调 |
| 399 | // Set isEditorMode in editor mode to defer component lifecycle callbacks |
| 400 | if (this._platform.isEditorMode()) { |
| 401 | this._scene.isEditorMode = true; |
| 402 | } |
| 403 | |
| 404 | // 6. 添加基础系统 |
| 405 | this._scene.addSystem(new HierarchySystem()); |
| 406 | this._scene.addSystem(new TransformSystem()); |
| 407 | |
| 408 | // 7. 添加输入系统(最先更新,以便其他系统可以读取输入状态) |
| 409 | // Add input system (updates first so other systems can read input state) |
| 410 | this._inputSystem = new InputSystem({ |
| 411 | disableInEditor: true // 编辑器模式下禁用,避免与编辑器输入冲突 |
| 412 | }); |
| 413 | this._scene.addSystem(this._inputSystem); |
| 414 |
nothing calls this directly
no test coverage detected