fix(physics-rapier2d): 修复物理插件组件注册

- PhysicsEditorPlugin 添加 runtimeModule 引用
- 适配 IComponentRegistry 接口
- 修复物理组件在场景加载时未注册的问题
This commit is contained in:
yhh
2025-12-16 11:12:50 +08:00
parent 39fa797299
commit 7834328ae0
4 changed files with 27 additions and 15 deletions

View File

@@ -6,7 +6,7 @@
* 用于编辑器中的组件序列化/反序列化 * 用于编辑器中的组件序列化/反序列化
*/ */
import { ComponentRegistry } from '@esengine/ecs-framework'; import type { IComponentRegistry } from '@esengine/ecs-framework';
import type { IRuntimeModule } from '@esengine/engine-core'; import type { IRuntimeModule } from '@esengine/engine-core';
// Components (no WASM dependency) // Components (no WASM dependency)
@@ -26,8 +26,9 @@ import { PolygonCollider2DComponent } from './components/PolygonCollider2DCompon
export class Physics2DComponentsModule implements IRuntimeModule { export class Physics2DComponentsModule implements IRuntimeModule {
/** /**
* 注册组件到 ComponentRegistry * 注册组件到 ComponentRegistry
* Register components to ComponentRegistry
*/ */
registerComponents(registry: typeof ComponentRegistry): void { registerComponents(registry: IComponentRegistry): void {
registry.register(Rigidbody2DComponent); registry.register(Rigidbody2DComponent);
registry.register(BoxCollider2DComponent); registry.register(BoxCollider2DComponent);
registry.register(CircleCollider2DComponent); registry.register(CircleCollider2DComponent);

View File

@@ -4,11 +4,14 @@
* 编辑器版本的物理插件,不包含 WASM 依赖。 * 编辑器版本的物理插件,不包含 WASM 依赖。
* Editor version of physics plugin, without WASM dependencies. * Editor version of physics plugin, without WASM dependencies.
* *
* 用于编辑器中注册插件清单,但不创建运行时模块。 * 使用轻量级 Physics2DComponentsModule 注册组件,
* 运行时使用 PhysicsPlugin from '@esengine/physics-rapier2d/runtime' * 使场景中的物理组件可以正确序列化/反序列化。
* Uses lightweight Physics2DComponentsModule to register components,
* enabling proper serialization/deserialization of physics components in scenes.
*/ */
import type { IRuntimePlugin, ModuleManifest } from '@esengine/engine-core'; import type { IRuntimePlugin, ModuleManifest } from '@esengine/engine-core';
import { Physics2DComponentsModule } from './Physics2DComponentsModule';
const manifest: ModuleManifest = { const manifest: ModuleManifest = {
id: '@esengine/physics-rapier2d', id: '@esengine/physics-rapier2d',
@@ -30,12 +33,15 @@ const manifest: ModuleManifest = {
}; };
/** /**
* 编辑器物理插件(运行时模块) * 编辑器物理插件(轻量级运行时模块)
* Editor physics plugin (no runtime module) * Editor physics plugin (lightweight runtime module)
* *
* 编辑器使用此版本注册件,运行时使用带 WASM 的完整版本 * 使用 Physics2DComponentsModule 注册件,用于场景反序列化
* 不包含 WASM 依赖,不创建物理系统。
* Uses Physics2DComponentsModule for component registration (scene deserialization).
* No WASM dependency, no physics system creation.
*/ */
export const Physics2DPlugin: IRuntimePlugin = { export const Physics2DPlugin: IRuntimePlugin = {
manifest manifest,
// No runtime module - editor doesn't need physics simulation runtimeModule: new Physics2DComponentsModule()
}; };

View File

@@ -4,8 +4,7 @@
* 提供 Rapier2D 物理引擎的 ECS 集成 * 提供 Rapier2D 物理引擎的 ECS 集成
*/ */
import type { IScene, ServiceContainer } from '@esengine/ecs-framework'; import type { IScene, ServiceContainer, IComponentRegistry } from '@esengine/ecs-framework';
import { ComponentRegistry } from '@esengine/ecs-framework';
import type { IRuntimeModule, IRuntimePlugin, ModuleManifest, SystemContext } from '@esengine/engine-core'; import type { IRuntimeModule, IRuntimePlugin, ModuleManifest, SystemContext } from '@esengine/engine-core';
import { WasmLibraryLoaderFactory } from '@esengine/platform-common'; import { WasmLibraryLoaderFactory } from '@esengine/platform-common';
import type * as RAPIER from '@esengine/rapier2d'; import type * as RAPIER from '@esengine/rapier2d';
@@ -101,10 +100,11 @@ class PhysicsRuntimeModule implements IRuntimeModule {
/** /**
* 注册物理组件 * 注册物理组件
* Register physics components
* *
* @param registry - 组件注册表 * @param registry - 组件注册表 | Component registry
*/ */
registerComponents(registry: typeof ComponentRegistry): void { registerComponents(registry: IComponentRegistry): void {
registry.register(Rigidbody2DComponent); registry.register(Rigidbody2DComponent);
registry.register(BoxCollider2DComponent); registry.register(BoxCollider2DComponent);
registry.register(CircleCollider2DComponent); registry.register(CircleCollider2DComponent);

View File

@@ -9,11 +9,16 @@ import { isEditorEnvironment } from '@esengine/platform-common';
/** /**
* 获取 WASM 路径 * 获取 WASM 路径
* Get WASM path based on environment * Get WASM path based on environment
*
* Editor: engine/rapier2d/pkg/rapier_wasm2d_bg.wasm (deployed by vite build plugin)
* Runtime: wasm/rapier_wasm2d_bg.wasm (deployed by game build)
*/ */
function getWasmPath(): string { function getWasmPath(): string {
const isEditor = isEditorEnvironment(); const isEditor = isEditorEnvironment();
// Editor uses dist/engine/rapier2d/pkg/ structure (from vite copy-engine-modules plugin)
// 编辑器使用 dist/engine/rapier2d/pkg/ 结构(来自 vite copy-engine-modules 插件)
const path = isEditor const path = isEditor
? 'engine/physics-rapier2d/rapier_wasm2d_bg.wasm' ? 'engine/rapier2d/pkg/rapier_wasm2d_bg.wasm'
: 'wasm/rapier_wasm2d_bg.wasm'; : 'wasm/rapier_wasm2d_bg.wasm';
console.log(`[Rapier2D] isEditor=${isEditor}, wasmPath=${path}`); console.log(`[Rapier2D] isEditor=${isEditor}, wasmPath=${path}`);
@@ -32,7 +37,7 @@ export const Rapier2DLoaderConfig: WasmLibraryConfig = {
web: { web: {
/** /**
* WASM 文件路径 * WASM 文件路径
* 编辑器: engine/physics-rapier2d/rapier_wasm2d_bg.wasm * 编辑器: engine/rapier2d/pkg/rapier_wasm2d_bg.wasm
* 运行时: wasm/rapier_wasm2d_bg.wasm * 运行时: wasm/rapier_wasm2d_bg.wasm
*/ */
get wasmPath(): string { get wasmPath(): string {