@zylem/game-lib 0.5.1 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,26 +1,11 @@
1
- import { G as GameEntity, U as UpdateContext, S as SetupFunction, b as UpdateFunction, D as DestroyFunction, c as SetupContext, d as DestroyContext, e as BaseNode, L as LoadingEvent } from './entity-bQElAdpo.js';
1
+ import { b as SetupFunction, G as GameEntity, c as UpdateFunction, D as DestroyFunction, S as SetupContext, U as UpdateContext, d as DestroyContext, e as BaseNode, L as LoadingEvent } from './entity-COvRtFNG.js';
2
2
  import * as bitecs from 'bitecs';
3
- import { Vector3, Scene, Color, Object3D, Group } from 'three';
4
- import RAPIER__default, { World, RigidBody, Collider } from '@dimforge/rapier3d-compat';
5
- import { E as Entity, Z as ZylemCamera, L as LifecycleFunction, C as CameraDebugDelegate, b as CameraDebugState, d as CameraWrapper } from './camera-Dk-fOVZE.js';
6
-
7
- declare class ZylemWorld implements Entity<ZylemWorld> {
8
- type: string;
9
- world: World;
10
- collisionMap: Map<string, GameEntity<any>>;
11
- collisionBehaviorMap: Map<string, GameEntity<any>>;
12
- _removalMap: Map<string, GameEntity<any>>;
13
- static loadPhysics(gravity: Vector3): Promise<RAPIER__default.World>;
14
- constructor(world: World);
15
- addEntity(entity: any): void;
16
- setForRemoval(entity: any): void;
17
- destroyEntity(entity: GameEntity<any>): void;
18
- setup(): void;
19
- update(params: UpdateContext<any>): void;
20
- updatePostCollisionBehaviors(delta: number): void;
21
- updateColliders(delta: number): void;
22
- destroy(): void;
23
- }
3
+ import { defineSystem, IWorld } from 'bitecs';
4
+ import { Scene, Color, Object3D, Vector3, Group } from 'three';
5
+ import { f as ZylemWorld, S as SPRITE_TYPE, g as ZylemSprite, d as SPHERE_TYPE, h as ZylemSphere, R as RECT_TYPE, i as ZylemRect, T as TEXT_TYPE, j as ZylemText, B as BOX_TYPE, Z as ZylemBox, P as PLANE_TYPE, k as ZylemPlane, e as ZONE_TYPE, l as ZylemZone, A as ACTOR_TYPE, m as ZylemActor } from './entities-BAxfJOkk.js';
6
+ import { E as Entity, L as LifecycleFunction, S as StageEntity } from './entity-Bq_eNEDI.js';
7
+ import { Z as ZylemCamera, C as CameraDebugDelegate, b as CameraDebugState, d as CameraWrapper } from './camera-CpbDr4-V.js';
8
+ import RAPIER__default, { RigidBody, Collider } from '@dimforge/rapier3d-compat';
24
9
 
25
10
  interface SceneState {
26
11
  backgroundColor: Color | string;
@@ -104,6 +89,17 @@ declare abstract class LifeCycleBase<TSelf> {
104
89
  nodeDestroy(context: DestroyContext<TSelf>): void;
105
90
  }
106
91
 
92
+ type StageSystem = {
93
+ _childrenMap: Map<number, StageEntity & {
94
+ body: RAPIER__default.RigidBody;
95
+ }>;
96
+ };
97
+ type TransformSystemResult = {
98
+ system: ReturnType<typeof defineSystem>;
99
+ destroy: (world: IWorld) => void;
100
+ };
101
+ declare function createTransformSystem(stage: StageSystem): TransformSystemResult;
102
+
107
103
  type AddEntityFactory = (params: {
108
104
  position: Vector3;
109
105
  normal?: Vector3;
@@ -179,39 +175,45 @@ declare class ZylemStage extends LifeCycleBase<ZylemStage> {
179
175
  private isLoaded;
180
176
  _debugMap: Map<string, BaseNode>;
181
177
  private entityAddedHandlers;
182
- private loadingHandlers;
183
178
  ecs: bitecs.IWorld;
184
179
  testSystem: any;
185
- transformSystem: any;
180
+ transformSystem: ReturnType<typeof createTransformSystem> | null;
186
181
  debugDelegate: StageDebugDelegate | null;
187
182
  cameraDebugDelegate: StageCameraDebugDelegate | null;
183
+ private debugStateUnsubscribe;
188
184
  uuid: string;
189
185
  wrapperRef: Stage | null;
190
186
  camera?: CameraWrapper;
191
187
  cameraRef?: ZylemCamera | null;
188
+ private cameraDelegate;
189
+ private loadingDelegate;
192
190
  /**
193
191
  * Create a new stage.
194
192
  * @param options Stage options: partial config, camera, and initial entities or factories
195
193
  */
196
194
  constructor(options?: StageOptions);
197
- private parseOptions;
198
- private isZylemStageConfig;
199
- private isBaseNode;
200
- private isCameraWrapper;
201
- private isEntityInput;
202
- private isThenable;
203
195
  private handleEntityImmediatelyOrQueue;
204
196
  private handlePromiseWithSpawnOnResolve;
205
197
  private saveState;
206
198
  private setState;
207
199
  /**
208
200
  * Load and initialize the stage's scene and world.
201
+ * Uses generator pattern to yield control to event loop for real-time progress.
209
202
  * @param id DOM element id for the renderer container
210
203
  * @param camera Optional camera override
211
204
  */
212
205
  load(id: string, camera?: ZylemCamera | null): Promise<void>;
213
- private createDefaultCamera;
206
+ /**
207
+ * Generator that yields between entity loads for real-time progress updates.
208
+ */
209
+ private entityLoadGenerator;
210
+ /**
211
+ * Runs the entity load generator, yielding to the event loop between loads.
212
+ * This allows the browser to process events and update the UI in real-time.
213
+ */
214
+ private runEntityLoadGenerator;
214
215
  protected _setup(params: SetupContext<ZylemStage>): void;
216
+ private updateDebugDelegate;
215
217
  protected _update(params: UpdateContext<ZylemStage>): void;
216
218
  outOfLoop(): void;
217
219
  /** Update debug overlays and helpers if enabled. */
@@ -236,7 +238,6 @@ declare class ZylemStage extends LifeCycleBase<ZylemStage> {
236
238
  replayExisting?: boolean;
237
239
  }): () => void;
238
240
  onLoading(callback: (event: LoadingEvent) => void): () => void;
239
- private emitLoading;
240
241
  /**
241
242
  * Remove an entity and its resources by its UUID.
242
243
  * @returns true if removed, false if not found or stage not ready
@@ -256,6 +257,21 @@ declare class ZylemStage extends LifeCycleBase<ZylemStage> {
256
257
  enqueue(...items: StageEntityInput[]): void;
257
258
  }
258
259
 
260
+ /**
261
+ * Maps entity type symbols to their class types.
262
+ * Used by getEntityByName to infer return types.
263
+ */
264
+ interface EntityTypeMap {
265
+ [SPRITE_TYPE]: ZylemSprite;
266
+ [SPHERE_TYPE]: ZylemSphere;
267
+ [RECT_TYPE]: ZylemRect;
268
+ [TEXT_TYPE]: ZylemText;
269
+ [BOX_TYPE]: ZylemBox;
270
+ [PLANE_TYPE]: ZylemPlane;
271
+ [ZONE_TYPE]: ZylemZone;
272
+ [ACTOR_TYPE]: ZylemActor;
273
+ }
274
+
259
275
  type NodeLike = {
260
276
  create: Function;
261
277
  };
@@ -264,24 +280,59 @@ type EntityInput = AnyNode | (() => AnyNode) | (() => Promise<any>);
264
280
  declare class Stage {
265
281
  wrappedStage: ZylemStage | null;
266
282
  options: StageOptionItem[];
267
- update: UpdateFunction<ZylemStage>;
268
- setup: SetupFunction<ZylemStage>;
269
- destroy: DestroyFunction<ZylemStage>;
283
+ private _pendingEntities;
284
+ private setupCallbacks;
285
+ private updateCallbacks;
286
+ private destroyCallbacks;
287
+ private pendingLoadingCallbacks;
270
288
  constructor(options: StageOptions);
271
289
  load(id: string, camera?: ZylemCamera | CameraWrapper | null): Promise<void>;
290
+ private applyLifecycleCallbacks;
272
291
  addEntities(entities: BaseNode[]): Promise<void>;
273
292
  add(...inputs: Array<EntityInput>): void;
274
293
  private addToBlueprints;
275
294
  private addToStage;
276
295
  start(params: SetupContext<ZylemStage>): void;
277
- onUpdate(...callbacks: UpdateFunction<ZylemStage>[]): void;
278
- onSetup(callback: SetupFunction<ZylemStage>): void;
279
- onDestroy(callback: DestroyFunction<ZylemStage>): void;
296
+ onUpdate(...callbacks: UpdateFunction<ZylemStage>[]): this;
297
+ onSetup(...callbacks: SetupFunction<ZylemStage>[]): this;
298
+ onDestroy(...callbacks: DestroyFunction<ZylemStage>[]): this;
280
299
  onLoading(callback: (event: LoadingEvent) => void): () => void;
300
+ /**
301
+ * Find an entity by name on the current stage.
302
+ * @param name The name of the entity to find
303
+ * @param type Optional type symbol for type inference (e.g., TEXT_TYPE, SPRITE_TYPE)
304
+ * @returns The entity if found, or undefined
305
+ * @example stage.getEntityByName('scoreText', TEXT_TYPE)
306
+ */
307
+ getEntityByName<T extends symbol | void = void>(name: string, type?: T): T extends keyof EntityTypeMap ? EntityTypeMap[T] | undefined : BaseNode | undefined;
281
308
  }
282
309
  /**
283
310
  * Create a stage with optional camera
284
311
  */
285
312
  declare function createStage(...options: StageOptions): Stage;
286
313
 
287
- export { type BaseEntityInterface as B, type StageOptions as S, Stage as a, createStage as c };
314
+ /**
315
+ * Stage state interface - minimal to prevent circular dependencies
316
+ */
317
+ interface StageStateInterface {
318
+ backgroundColor: Color;
319
+ backgroundImage: string | null;
320
+ inputs: {
321
+ p1: string[];
322
+ p2: string[];
323
+ };
324
+ variables: Record<string, any>;
325
+ gravity: Vector3;
326
+ entities: Partial<BaseEntityInterface>[];
327
+ stageRef?: any;
328
+ }
329
+ /**
330
+ * Minimal stage interface to break circular dependencies
331
+ */
332
+ interface StageInterface {
333
+ uuid: string;
334
+ children: any[];
335
+ state: StageStateInterface;
336
+ }
337
+
338
+ export { type StageStateInterface as S, type StageOptions as a, Stage as b, createStage as c, type StageInterface as d };
package/dist/stage.d.ts CHANGED
@@ -1,26 +1,46 @@
1
- export { S as StageOptions, c as createStage } from './stage-CrmY7V0i.js';
2
- export { e as entitySpawner } from './entity-spawner-DNnLYnZq.js';
3
- import * as _sinclair_typebox from '@sinclair/typebox';
4
- import { Static } from '@sinclair/typebox';
5
- import './entity-bQElAdpo.js';
1
+ import { S as StageStateInterface } from './stage-types-CD21XoIU.js';
2
+ export { a as StageOptions, c as createStage } from './stage-types-CD21XoIU.js';
3
+ export { S as StageBlueprint, e as entitySpawner } from './blueprints-BOCc3Wve.js';
4
+ import './entity-COvRtFNG.js';
6
5
  import 'three';
7
6
  import '@dimforge/rapier3d-compat';
8
7
  import 'bitecs';
9
- import './camera-Dk-fOVZE.js';
10
- import 'three/addons/controls/OrbitControls.js';
8
+ import './entities-BAxfJOkk.js';
9
+ import './entity-Bq_eNEDI.js';
10
+ import './camera-CpbDr4-V.js';
11
11
  import 'three/examples/jsm/postprocessing/EffectComposer.js';
12
+ import '@sinclair/typebox';
12
13
 
13
- declare const StageSchema: _sinclair_typebox.TObject<{
14
- id: _sinclair_typebox.TString;
15
- name: _sinclair_typebox.TOptional<_sinclair_typebox.TString>;
16
- entities: _sinclair_typebox.TArray<_sinclair_typebox.TObject<{
17
- id: _sinclair_typebox.TString;
18
- type: _sinclair_typebox.TString;
19
- position: _sinclair_typebox.TOptional<_sinclair_typebox.TTuple<[_sinclair_typebox.TNumber, _sinclair_typebox.TNumber]>>;
20
- data: _sinclair_typebox.TOptional<_sinclair_typebox.TRecord<_sinclair_typebox.TString, _sinclair_typebox.TAny>>;
21
- }>>;
22
- assets: _sinclair_typebox.TOptional<_sinclair_typebox.TArray<_sinclair_typebox.TString>>;
23
- }>;
24
- type StageBlueprint = Static<typeof StageSchema>;
14
+ /**
15
+ * Event name for stage state changes.
16
+ * Dispatched when the stage state proxy is updated.
17
+ */
18
+ declare const STAGE_STATE_CHANGE = "STAGE_STATE_CHANGE";
19
+ /**
20
+ * Event detail payload for STAGE_STATE_CHANGE events.
21
+ */
22
+ interface StageStateChangeEvent {
23
+ entities: StageStateInterface['entities'];
24
+ variables: StageStateInterface['variables'];
25
+ }
26
+ /**
27
+ * Initialize the stage state dispatcher.
28
+ * Subscribes to stageState changes and dispatches STAGE_STATE_CHANGE events to the window.
29
+ *
30
+ * @returns Unsubscribe function to stop dispatching events.
31
+ *
32
+ * @example
33
+ * // Start dispatching stage state changes
34
+ * const unsubscribe = initStageStateDispatcher();
35
+ *
36
+ * // Later, stop dispatching
37
+ * unsubscribe();
38
+ */
39
+ declare function initStageStateDispatcher(): () => void;
40
+ /**
41
+ * Manually dispatch the current stage state.
42
+ * Useful for initial sync when a listener is added.
43
+ */
44
+ declare function dispatchStageState(): void;
25
45
 
26
- export type { StageBlueprint };
46
+ export { STAGE_STATE_CHANGE, type StageStateChangeEvent, dispatchStageState, initStageStateDispatcher };