Skip to content

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.

refactor: consolidate component registry and DRY up scene loading

esengine810ae3ea23e4
diff --git a/sdk/src/component.ts b/sdk/src/component.ts
index 8dbcef9c1..98a690af9 100644
--- a/sdk/src/component.ts
+++ b/sdk/src/component.ts
@@ -64,23 +64,23 @@ export function defineComponent<T extends object>(
name: string,
defaults: T
): ComponentDef<T> {
- const registry = getComponentRegistry();
- const existing = registry.get(name);
+ const existing = componentRegistry.get(name) ?? getComponentRegistry().get(name);
if (existing) return existing as ComponentDef<T>;
const def = createComponentDef(name, defaults);
- registry.set(name, def);
+ getComponentRegistry().set(name, def);
+ componentRegistry.set(name, def);
registerToEditor(name, defaults as Record<string, unknown>, false);
return def;
}
export function defineTag(name: string): ComponentDef<{}> {
- const registry = getComponentRegistry();
- const existing = registry.get(name);
+ const existing = componentRegistry.get(name) ?? getComponentRegistry().get(name);
if (existing) return existing as ComponentDef<{}>;
const def = createComponentDef(name, {});
- registry.set(name, def);
+ getComponentRegistry().set(name, def);
+ componentRegistry.set(name, def);
registerToEditor(name, {}, true);
return def;
}
@@ -90,11 +90,16 @@ export function getUserComponent(name: string): ComponentDef<any> | undefined {
}
export function clearUserComponents(): void {
- getComponentRegistry().clear();
+ const userRegistry = getComponentRegistry();
+ for (const name of userRegistry.keys()) {
+ componentRegistry.delete(name);
+ }
+ userRegistry.clear();
}
export function unregisterComponent(name: string): void {
getComponentRegistry().delete(name);
+ componentRegistry.delete(name);
}
function registerToEditor(
diff --git a/sdk/src/scene.ts b/sdk/src/scene.ts
index ff676776a..f3a7cd6d9 100644
--- a/sdk/src/scene.ts
+++ b/sdk/src/scene.ts
@@ -174,7 +174,7 @@ function remapEntityFields(compData: SceneComponentData, entityMap: Map<number,
// Scene Loader
// =============================================================================
-export function loadSceneData(world: World, sceneData: SceneData): Map<number, Entity> {
+function spawnAndLoadEntities(world: World, sceneData: SceneData): Map<number, Entity> {
const entityMap = new Map<number, Entity>();
for (const entityData of sceneData.entities) {
@@ -205,12 +205,15 @@ export function loadSceneData(world: World, sceneData: SceneData): Map<number, E
return entityMap;
}
+export function loadSceneData(world: World, sceneData: SceneData): Map<number, Entity> {
+ return spawnAndLoadEntities(world, sceneData);
+}
+
export async function loadSceneWithAssets(
world: World,
sceneData: SceneData,
options?: SceneLoadOptions
): Promise<Map<number, Entity>> {
- const entityMap = new Map<number, Entity>();
const assetServer = options?.assetServer;
const baseUrl = options?.assetBaseUrl;
const texturePathToUrl = new Map<string, string>();
@@ -219,30 +222,7 @@ export async function loadSceneWithAssets(
await preloadSceneAssets(sceneData, assetServer, baseUrl, texturePathToUrl);
}
- for (const entityData of sceneData.entities) {
- const entity = world.spawn();
- entityMap.set(entityData.id, entity);
- world.insert(entity, Name, { value: entityData.name });
- }
-
- for (const entityData of sceneData.entities) {
- if (entityData.visible === false) continue;
- const entity = entityMap.get(entityData.id)!;
- for (const compData of entityData.components) {
- remapEntityFields(compData, entityMap);
- loadComponent(world, entity, compData, entityData.name);
- }
- }
-
- for (const entityData of sceneData.entities) {
- if (entityData.parent !== null) {
- const entity = entityMap.get(entityData.id);
- const parentEntity = entityMap.get(entityData.parent);
- if (entity !== undefined && parentEntity !== undefined) {
- world.setParent(entity, parentEntity);
- }
- }
- }
+ const entityMap = spawnAndLoadEntities(world, sceneData);
if (assetServer && sceneData.textureMetadata) {
for (const [texturePath, metadata] of Object.entries(sceneData.textureMetadata)) {
diff --git a/sdk/src/sceneManager.ts b/sdk/src/sceneManager.ts
index 503376d49..ad32496fb 100644
--- a/sdk/src/sceneManager.ts
+++ b/sdk/src/sceneManager.ts
@@ -254,16 +254,20 @@ export class SceneManagerState {
const oldScene = this.activeScene_;
const doSwitch = async () => {
- if (oldScene && oldScene !== targetScene) {
- await this.unload(oldScene, options);
+ try {
+ if (oldScene && oldScene !== targetScene) {
+ await this.unload(oldScene, options);
+ }
+ await this.load(targetScene);
+ } catch (err) {
+ console.error('Scene transition failed:', err);
+ const { resolve: resolveTransition } = this.transition_!;
+ this.transition_ = null;
+ unregisterDrawCallback(TRANSITION_CALLBACK_ID);
+ resolveTransition();
}
- await this.load(targetScene);
};
- doSwitch().catch(err => {
- console.error('Scene transition failed:', err);
- this.transition_ = null;
- unregisterDrawCallback(TRANSITION_CALLBACK_ID);
- });
+ doSwitch();
}
if (this.transition_.phase === 'fade-in' && this.transition_.elapsed >= halfDuration) {
diff --git a/sdk/src/world.ts b/sdk/src/world.ts
index 296970aca..18c5cc737 100644
--- a/sdk/src/world.ts
+++ b/sdk/src/world.ts
@@ -248,11 +248,24 @@ export class World {
if (methods) return methods;
const reg = this.cppRegistry_!;
+ const addFn = reg[`add${cppName}`];
+ const getFn = reg[`get${cppName}`];
+ const hasFn = reg[`has${cppName}`];
+ const removeFn = reg[`remove${cppName}`];
+
+ if (typeof addFn !== 'function' || typeof getFn !== 'function' ||
+ typeof hasFn !== 'function' || typeof removeFn !== 'function') {
+ throw new Error(
+ `C++ Registry missing methods for component "${cppName}". ` +
+ `Expected: add${cppName}, get${cppName}, has${cppName}, remove${cppName}`
+ );
+ }
+
methods = {
- add: (reg[`add${cppName}`] as Function).bind(reg),
- get: (reg[`get${cppName}`] as Function).bind(reg),
- has: (reg[`has${cppName}`] as Function).bind(reg),
- remove: (reg[`remove${cppName}`] as Function).bind(reg),
+ add: addFn.bind(reg),
+ get: getFn.bind(reg),
+ has: hasFn.bind(reg),
+ remove: removeFn.bind(reg),
};
this.builtinMethodCache_.set(cppName, methods);
return methods;