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: replace manual component mapping with World + loadComponent in EditorBridge

esenginefca7fc157660
diff --git a/editor/src/bridge/EditorBridge.ts b/editor/src/bridge/EditorBridge.ts
index 980ede023..4f4df300c 100644
--- a/editor/src/bridge/EditorBridge.ts
+++ b/editor/src/bridge/EditorBridge.ts
@@ -3,7 +3,7 @@
* @brief Bridge between editor and ESEngine runtime
*/
-import type { Entity, App, ESEngineModule, CppRegistry } from 'esengine';
+import { World, loadComponent, type Entity, type App, type ESEngineModule, type CppRegistry, type SceneComponentData } from 'esengine';
import type { SceneData, ComponentData as EditorComponentData } from '../types/SceneTypes';
import type { EditorStore } from '../store/EditorStore';
@@ -16,6 +16,7 @@ export class EditorBridge {
private store_: EditorStore;
private module_: ESEngineModule | null = null;
private registry_: CppRegistry | null = null;
+ private world_: World | null = null;
constructor(app: App, store: EditorStore) {
this.app_ = app;
@@ -24,6 +25,8 @@ export class EditorBridge {
if (this.module_) {
this.registry_ = new this.module_.Registry();
+ this.world_ = new World();
+ this.world_.connectCpp(this.registry_);
}
}
@@ -32,12 +35,12 @@ export class EditorBridge {
// =========================================================================
syncToRuntime(): void {
- if (!this.module_ || !this.registry_) return;
+ if (!this.module_ || !this.world_) return;
const scene = this.store_.scene;
for (const entityData of scene.entities) {
- const entity = this.registry_.create();
+ const entity = this.world_.spawn();
for (const comp of entityData.components) {
this.addComponentToRuntime(entity, comp);
@@ -106,91 +109,17 @@ export class EditorBridge {
// =========================================================================
private addComponentToRuntime(entity: Entity, comp: EditorComponentData): void {
- if (!this.registry_) return;
-
- switch (comp.type) {
- case 'LocalTransform':
- this.registry_.addLocalTransform(entity, {
- position: comp.data.position ?? { x: 0, y: 0, z: 0 },
- rotation: comp.data.rotation ?? { x: 0, y: 0, z: 0, w: 1 },
- scale: comp.data.scale ?? { x: 1, y: 1, z: 1 },
- });
- break;
-
- case 'Sprite':
- this.registry_.addSprite(entity, {
- texture: comp.data.texture ?? 0,
- color: comp.data.color ?? { r: 1, g: 1, b: 1, a: 1 },
- size: comp.data.size ?? { x: 100, y: 100 },
- uvOffset: comp.data.uvOffset ?? { x: 0, y: 0 },
- uvScale: comp.data.uvScale ?? { x: 1, y: 1 },
- layer: comp.data.layer ?? 0,
- flipX: comp.data.flipX ?? false,
- flipY: comp.data.flipY ?? false,
- });
- break;
-
- case 'Camera':
- this.registry_.addCamera(entity, {
- isActive: comp.data.isActive ?? true,
- projectionType: comp.data.projectionType ?? 0,
- fov: comp.data.fov ?? 60,
- orthoSize: comp.data.orthoSize ?? 5,
- nearPlane: comp.data.nearPlane ?? 0.1,
- farPlane: comp.data.farPlane ?? 1000,
- });
- break;
-
- case 'SpineAnimation':
- this.registry_.addSpineAnimation(entity, {
- skeletonPath: comp.data.skeletonPath ?? '',
- atlasPath: comp.data.atlasPath ?? '',
- skin: comp.data.skin ?? 'default',
- animation: comp.data.animation ?? '',
- timeScale: comp.data.timeScale ?? 1,
- loop: comp.data.loop ?? true,
- playing: comp.data.playing ?? true,
- flipX: comp.data.flipX ?? false,
- flipY: comp.data.flipY ?? false,
- color: comp.data.color ?? { r: 1, g: 1, b: 1, a: 1 },
- layer: comp.data.layer ?? 0,
- skeletonScale: comp.data.skeletonScale ?? 1,
- });
- break;
- }
+ if (!this.world_) return;
+ loadComponent(this.world_, entity, comp as SceneComponentData);
}
private updateComponentInRuntime(entity: Entity, comp: EditorComponentData): void {
- if (!this.registry_) return;
-
- switch (comp.type) {
- case 'LocalTransform':
- if (this.registry_.hasLocalTransform(entity)) {
- this.registry_.removeLocalTransform(entity);
- }
- this.addComponentToRuntime(entity, comp);
- break;
-
- case 'Sprite':
- if (this.registry_.hasSprite(entity)) {
- this.registry_.removeSprite(entity);
- }
- this.addComponentToRuntime(entity, comp);
- break;
-
- case 'Camera':
- if (this.registry_.hasCamera(entity)) {
- this.registry_.removeCamera(entity);
- }
- this.addComponentToRuntime(entity, comp);
- break;
-
- case 'SpineAnimation':
- if (this.registry_.hasSpineAnimation(entity)) {
- this.registry_.removeSpineAnimation(entity);
- }
- this.addComponentToRuntime(entity, comp);
- break;
+ if (!this.world_ || !this.registry_) return;
+ const has = this.registry_[`has${comp.type}`] as ((e: Entity) => boolean) | undefined;
+ const remove = this.registry_[`remove${comp.type}`] as ((e: Entity) => void) | undefined;
+ if (has?.call(this.registry_, entity)) {
+ remove?.call(this.registry_, entity);
}
+ this.addComponentToRuntime(entity, comp);
}
}