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
feat: add user component registry, system registration and scene loading
esengine43ddf33bd47e
diff --git a/sdk/src/app.ts b/sdk/src/app.tsindex e6632cdeb..1df959dac 100644--- a/sdk/src/app.ts+++ b/sdk/src/app.ts@@ -265,6 +265,17 @@ export function createWebApp(module: ESEngineModule, options?: WebAppOptions): Areturn app;}+export function flushPendingSystems(app: App): void {+ if (typeof window === 'undefined') return;+ const pending = window.__esengine_pendingSystems;+ if (!pending || pending.length === 0) return;++ for (const entry of pending) {+ app.addSystemToSchedule(entry.schedule as Schedule, entry.system as SystemDef);+ }+ pending.length = 0;+}+// =============================================================================// View-Projection Computation// =============================================================================diff --git a/sdk/src/component.ts b/sdk/src/component.tsindex ef9994f67..8d9b5bef1 100644--- a/sdk/src/component.ts+++ b/sdk/src/component.ts@@ -35,21 +35,44 @@ function createComponentDef<T extends object>(};}+function getComponentRegistry(): Map<string, ComponentDef<any>> {+ if (typeof window === 'undefined') return new Map();+ return (window.__esengine_componentRegistry ??= new Map());+}+export function defineComponent<T extends object>(name: string,defaults: T): ComponentDef<T> {+ const registry = getComponentRegistry();+ const existing = registry.get(name);+ if (existing) return existing as ComponentDef<T>;+const def = createComponentDef(name, defaults);+ registry.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);+ if (existing) return existing as ComponentDef<{}>;+const def = createComponentDef(name, {});+ registry.set(name, def);registerToEditor(name, {}, true);return def;}+export function getUserComponent(name: string): ComponentDef<any> | undefined {+ return getComponentRegistry().get(name);+}++export function clearUserComponents(): void {+ getComponentRegistry().clear();+}+function registerToEditor(name: string,defaults: Record<string, unknown>,@@ -270,9 +293,11 @@ const builtinComponents: Record<string, BuiltinComponentDef<any>> = {};export function getComponentDefaults(typeName: string): Record<string, unknown> | null {- const component = builtinComponents[typeName];- if (component) {- return { ...component._default };- }+ const builtin = builtinComponents[typeName];+ if (builtin) return { ...builtin._default };++ const userComp = getUserComponent(typeName);+ if (userComp) return { ...userComp._default };+return null;}diff --git a/sdk/src/core.ts b/sdk/src/core.tsindex cd5d3d00e..808b0f688 100644--- a/sdk/src/core.ts+++ b/sdk/src/core.ts@@ -33,6 +33,8 @@ export {defineTag,isBuiltinComponent,getComponentDefaults,+ getUserComponent,+ clearUserComponents,type ComponentDef,type BuiltinComponentDef,type AnyComponentDef,@@ -106,6 +108,9 @@ export {export {Schedule,defineSystem,+ addSystem,+ addStartupSystem,+ addSystemToSchedule,SystemRunner,type SystemDef,type SystemParam,@@ -126,6 +131,7 @@ export { World } from './world';export {App,createWebApp,+ flushPendingSystems,type Plugin,type WebAppOptions,} from './app';diff --git a/sdk/src/globals.d.ts b/sdk/src/globals.d.tsindex 3c7139a91..d0459ad64 100644--- a/sdk/src/globals.d.ts+++ b/sdk/src/globals.d.ts@@ -1,4 +1,7 @@interface Window {__esengine_registerComponent?: (name: string, defaults: Record<string, unknown>, isTag: boolean) => void;+ __esengine_componentRegistry?: Map<string, unknown>;__esengine_spineEvent?: (entity: number, eventData: unknown) => void;+ __esengine_pendingSystems?: Array<{ schedule: number; system: unknown }>;+ __esengine_componentSourceMap?: Map<string, string>;}diff --git a/sdk/src/scene.ts b/sdk/src/scene.tsindex cd12be8f3..77d31dd24 100644--- a/sdk/src/scene.ts+++ b/sdk/src/scene.ts@@ -11,6 +11,7 @@ import {Camera,Canvas,SpineAnimation,+ getUserComponent,type LocalTransformData,type SpriteData,type CameraData,@@ -218,8 +219,15 @@ export function loadComponent(world: World, entity: Entity, compData: SceneCompocase 'UIRect':world.insert(entity, UIRect, data as UIRectData);break;- default:- console.warn(`Unknown component type: ${compData.type}`);+ default: {+ const userComp = getUserComponent(compData.type);+ if (userComp) {+ world.insert(entity, userComp, data);+ } else {+ console.warn(`Unknown component type: ${compData.type}`);+ }+ break;+ }}}diff --git a/sdk/src/system.ts b/sdk/src/system.tsindex fcb685a5a..5e5acaa3c 100644--- a/sdk/src/system.ts+++ b/sdk/src/system.ts@@ -80,6 +80,27 @@ export function defineSystem<P extends readonly SystemParam[]>(};}+// =============================================================================+// Global System Registration+// =============================================================================++function getPendingSystems(): Array<{ schedule: number; system: unknown }> {+ if (typeof window === 'undefined') return [];+ return (window.__esengine_pendingSystems ??= []);+}++export function addSystem(system: SystemDef): void {+ getPendingSystems().push({ schedule: Schedule.Update, system });+}++export function addStartupSystem(system: SystemDef): void {+ getPendingSystems().push({ schedule: Schedule.Startup, system });+}++export function addSystemToSchedule(schedule: Schedule, system: SystemDef): void {+ getPendingSystems().push({ schedule, system });+}+// =============================================================================// System Runner// =============================================================================