A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
refactor(physics): move module state into PhysicsRuntime resource
esengine2b1407991f18
diff --git a/sdk/src/app.ts b/sdk/src/app.tsindex ada753997..8a6343550 100644--- a/sdk/src/app.ts+++ b/sdk/src/app.ts@@ -21,6 +21,7 @@ import { SceneManager } from './sceneManager';import { sceneManagerPlugin } from './scenePlugin';import { getDefaultContext } from './context';import { cameraPlugin } from './camera/CameraPlugin';+import { PhysicsRuntime } from './physics/PhysicsRuntime';// =============================================================================// Plugin Interface@@ -74,8 +75,6 @@ export class App {private module_: ESEngineModule | null = null;private pipeline_: RenderPipeline | null = null;private spineInitPromise_?: Promise<unknown>;- private physicsInitPromise_?: Promise<unknown>;- private physicsModule_?: unknown;private readonly installed_plugins_: Plugin[] = [];private readonly installedPluginSet_ = new Set<Plugin>();private readonly installedPluginNames_ = new Set<string>();@@ -252,32 +251,18 @@ export class App {this.spineInitPromise_ = p;}- get physicsInitPromise(): Promise<unknown> | undefined {- return this.physicsInitPromise_;- }-- set physicsInitPromise(p: Promise<unknown> | undefined) {- this.physicsInitPromise_ = p;- }-- get physicsModule(): unknown {- return this.physicsModule_;- }-- set physicsModule(m: unknown) {- this.physicsModule_ = m;- }-async waitForPhysics(): Promise<void> {- if (!this.physicsInitPromise_) {+ if (!this.hasResource(PhysicsRuntime)) {console.warn('[ESEngine] No PhysicsPlugin installed, waitForPhysics() is a no-op');return;}- await this.physicsInitPromise_;+ const promise = this.getResource(PhysicsRuntime).initPromise;+ if (promise) await promise;}get isPhysicsReady(): boolean {- return this.physicsModule_ != null;+ return this.hasResource(PhysicsRuntime)+ && this.getResource(PhysicsRuntime).module != null;}// =========================================================================diff --git a/sdk/src/physics/PhysicsPlugin.ts b/sdk/src/physics/PhysicsPlugin.tsindex dc04bcee4..ae463f55c 100644--- a/sdk/src/physics/PhysicsPlugin.ts+++ b/sdk/src/physics/PhysicsPlugin.ts@@ -26,6 +26,8 @@ import {type DistanceJointData, type PrismaticJointData, type WeldJointData, type WheelJointData,} from './PhysicsComponents';import { setupPhysicsDebugDraw, PhysicsDebugDraw, type PhysicsDebugDrawConfig } from './PhysicsDebugDraw';+import { PhysicsRuntime } from './PhysicsRuntime';+import { handleWasmError } from '../wasmError';// =============================================================================// Physics Config@@ -148,6 +150,7 @@ export class PhysicsPlugin implements Plugin {sensorEnters: [],sensorExits: []});+ app.insertResource(PhysicsRuntime, { module: null, initPromise: null });const trackedEntities = new Set<Entity>();const trackedJoints = new Set<Entity>();@@ -167,8 +170,6 @@ export class PhysicsPlugin implements Plugin {this.config_.contactSpeed);- const ppu = readPixelsPerUnit(app);- const invPpu = 1 / ppu;const world = app.world;world.onDespawn((entity: Entity) => {@@ -189,6 +190,13 @@ export class PhysicsPlugin implements Plugin {defineSystem([Res(Time)],(time: TimeData) => {+ // Read pixelsPerUnit live each tick so a Canvas+ // property change at runtime (editor: user edits+ // Canvas.pixelsPerUnit) propagates to physics+ // transforms instead of staying at the value+ // captured when the wasm module first loaded.+ const ppu = readPixelsPerUnit(app);+ const invPpu = 1 / ppu;const entities = world.getEntitiesWithComponents([RigidBody, Transform]);const currentEntities = new Set<Entity>();@@ -287,7 +295,7 @@ export class PhysicsPlugin implements Plugin {{ runIf: playModeOnly });- app.physicsModule = module;+ app.getResource(PhysicsRuntime).module = module;app.insertResource(PhysicsAPI, Physics._fromModule(module));setupPhysicsDebugDraw(app, PhysicsAPI, PhysicsEvents);app.setFixedTimestep(this.config_.fixedTimestep);@@ -295,9 +303,9 @@ export class PhysicsPlugin implements Plugin {);initPromise.catch((e) => {- console.error('[ESEngine] Physics initialization failed:', e);+ handleWasmError(e, 'PhysicsPlugin.init');});- app.physicsInitPromise = initPromise;+ app.getResource(PhysicsRuntime).initPromise = initPromise;}}@@ -716,10 +724,13 @@ export class Physics {private module_: PhysicsWasmModule;constructor(app: App) {- this.module_ = app.physicsModule as PhysicsWasmModule;- if (!this.module_) {+ const module = app.hasResource(PhysicsRuntime)+ ? app.getResource(PhysicsRuntime).module+ : null;+ if (!module) {throw new Error('Physics module not loaded. Ensure PhysicsPlugin init is complete.');}+ this.module_ = module;}/** @internal */diff --git a/sdk/src/physics/PhysicsRuntime.ts b/sdk/src/physics/PhysicsRuntime.tsnew file mode 100644index 000000000..11492718d--- /dev/null+++ b/sdk/src/physics/PhysicsRuntime.ts@@ -0,0 +1,24 @@+/**+ * @file PhysicsRuntime.ts+ * @brief Resource holding the physics wasm module + its load promise+ *+ * Decoupling this from App as a resource (instead of dynamic+ * properties) lets PhysicsPlugin own the lifetime of the module+ * without mutating App, and lets other systems read readiness+ * state in the same way they read any other resource.+ */++import { defineResource } from '../resource';+import type { PhysicsWasmModule } from './PhysicsModuleLoader';++export interface PhysicsRuntimeData {+ /** Loaded physics wasm module, null until the plugin's init promise resolves. */+ module: PhysicsWasmModule | null;+ /** Promise that resolves once the module is loaded; null before plugin install. */+ initPromise: Promise<void> | null;+}++export const PhysicsRuntime = defineResource<PhysicsRuntimeData>(+ { module: null, initPromise: null },+ 'PhysicsRuntime',+);