@zylem/game-lib 0.5.1 → 0.6.2

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.
@@ -0,0 +1,28 @@
1
+ import { RigidBody, Collider, KinematicCharacterController } from '@dimforge/rapier3d-compat';
2
+ import { Group, Mesh } from 'three';
3
+
4
+ type LifecycleFunction<T> = (params?: any) => void;
5
+ interface Entity<T = any> {
6
+ setup: (entity: T) => void;
7
+ destroy: () => void;
8
+ update: LifecycleFunction<T>;
9
+ type: string;
10
+ _collision?: (entity: any, other: any, globals?: any) => void;
11
+ _destroy?: (globals?: any) => void;
12
+ name?: string;
13
+ tag?: Set<string>;
14
+ }
15
+ interface StageEntity extends Entity {
16
+ uuid: string;
17
+ body: RigidBody;
18
+ group: Group;
19
+ mesh: Mesh;
20
+ instanceId: number;
21
+ collider: Collider;
22
+ controlledRotation: boolean;
23
+ characterController: KinematicCharacterController;
24
+ name: string;
25
+ markedForRemoval: boolean;
26
+ }
27
+
28
+ export type { Entity as E, LifecycleFunction as L, StageEntity as S };
@@ -0,0 +1,26 @@
1
+ import { Group } from 'three';
2
+ import { RigidBody, Collider } from '@dimforge/rapier3d-compat';
3
+
4
+ /**
5
+ * Base entity interface - shared across modules to prevent circular dependencies
6
+ */
7
+ interface BaseEntityInterface {
8
+ uuid: string;
9
+ name: string;
10
+ eid: number;
11
+ group: Group | null | undefined;
12
+ body: RigidBody | null | undefined;
13
+ collider: Collider | null | undefined;
14
+ }
15
+ /**
16
+ * Game entity interface - minimal interface to break circular dependencies
17
+ */
18
+ interface GameEntityInterface extends BaseEntityInterface {
19
+ type: string;
20
+ isStatic: boolean;
21
+ setPosition(x: number, y: number, z: number): void;
22
+ setRotation(x: number, y: number, z: number): void;
23
+ setScale(x: number, y: number, z: number): void;
24
+ }
25
+
26
+ export type { BaseEntityInterface as B, GameEntityInterface as G };
package/dist/main.d.ts CHANGED
@@ -1,13 +1,13 @@
1
- import { G as Game } from './core-C2mjetAd.js';
2
- export { V as Vect3, Z as ZylemGameConfig, c as createGame, g as gameConfig, a as globalChange, b as globalChanges, d as variableChange, e as variableChanges, v as vessel } from './core-C2mjetAd.js';
3
- export { S as StageOptions, c as createStage } from './stage-CrmY7V0i.js';
4
- export { e as entitySpawner } from './entity-spawner-DNnLYnZq.js';
5
- export { P as PerspectiveType, a as Perspectives, c as camera } from './camera-Dk-fOVZE.js';
6
- export { ZylemBox, actor, box, plane, rect, sphere, sprite, text, zone } from './entities.js';
7
- import { U as UpdateContext, B as BehaviorCallbackType } from './entity-bQElAdpo.js';
8
- export { a as Behavior, L as LoadingEvent } from './entity-bQElAdpo.js';
9
- export { boundary2d, ricochet2DCollision, ricochet2DInBounds } from './behaviors.js';
10
- import { M as MoveableEntity } from './moveable-B_vyA6cw.js';
1
+ import { G as Game } from './core-bO8TzV7u.js';
2
+ export { V as Vect3, Z as ZylemGameConfig, c as createGame, g as gameConfig, a as globalChange, b as globalChanges, d as variableChange, e as variableChanges, v as vessel } from './core-bO8TzV7u.js';
3
+ export { a as StageOptions, d as createDisk, c as createStage } from './stage-types-Bd-KtcYT.js';
4
+ import { S as StageBlueprint } from './blueprints-Cq3Ko6_G.js';
5
+ export { e as entitySpawner } from './blueprints-Cq3Ko6_G.js';
6
+ export { P as PerspectiveType, d as Perspectives, c as createCamera } from './camera-CeJPAgGg.js';
7
+ export { A as ACTOR_TYPE, B as BOX_TYPE, P as PLANE_TYPE, R as RECT_TYPE, a as SPHERE_TYPE, S as SPRITE_TYPE, T as TEXT_TYPE, g as ZONE_TYPE, e as ZylemBox, o as createActor, j as createBox, m as createPlane, q as createRect, k as createSphere, l as createSprite, p as createText, n as createZone } from './entities-DvByhMGU.js';
8
+ import { G as GameEntity, g as GameEntityOptions } from './world-C8tQ7Plj.js';
9
+ export { q as BehaviorDescriptor, s as BehaviorHandle, r as BehaviorRef, e as BehaviorSystem, f as BehaviorSystemFactory, t as DefineBehaviorConfig, o as EntityConfigPayload, k as EntityEvents, E as EventEmitterDelegate, j as GameEvents, l as GameLoadingPayload, L as LoadingEvent, a as SetupContext, n as StageConfigPayload, h as StageEvents, m as StateDispatchPayload, b as UpdateContext, i as ZylemEvents, Z as ZylemShaderObject, p as defineBehavior, z as zylemEventBus } from './world-C8tQ7Plj.js';
10
+ export { Behavior, BoundaryRicochetCoordinator, MovementSequence2DBehavior, MovementSequence2DCurrentStep, MovementSequence2DEvent, MovementSequence2DFSM, MovementSequence2DHandle, MovementSequence2DMovement, MovementSequence2DOptions, MovementSequence2DProgress, MovementSequence2DState, MovementSequence2DStep, PhysicsBodyComponent, PhysicsStepBehavior, PhysicsSyncBehavior, PlayerInput, Ricochet2DBehavior, Ricochet2DCollisionContext, Ricochet2DEvent, Ricochet2DFSM, Ricochet2DHandle, Ricochet2DOptions, Ricochet2DResult, Ricochet2DState, ScreenWrapBehavior, ScreenWrapEvent, ScreenWrapFSM, ScreenWrapOptions, ScreenWrapState, ThrusterBehavior, ThrusterBehaviorOptions, ThrusterEntity, ThrusterEvent, ThrusterFSM, ThrusterFSMContext, ThrusterInputComponent, ThrusterMovementBehavior, ThrusterMovementComponent, ThrusterState, ThrusterStateComponent, TransformComponent, WorldBoundary2DBehavior, WorldBoundary2DBounds, WorldBoundary2DEvent, WorldBoundary2DFSM, WorldBoundary2DHandle, WorldBoundary2DHit, WorldBoundary2DHits, WorldBoundary2DOptions, WorldBoundary2DPosition, WorldBoundary2DState, computeWorldBoundary2DHits, createPhysicsBodyComponent, createThrusterInputComponent, createThrusterMovementComponent, createThrusterStateComponent, createTransformComponent, hasAnyWorldBoundary2DHit, useBehavior } from './behaviors.js';
11
11
  export { m as makeMoveable, b as move, a as moveable, r as resetVelocity } from './moveable-B_vyA6cw.js';
12
12
  export { m as makeRotatable, a as makeTransformable, r as rotatable, b as rotateInDirection } from './transformable-CUhvyuYO.js';
13
13
  export { Howl } from 'howler';
@@ -15,30 +15,75 @@ import * as three from 'three';
15
15
  export { three as THREE };
16
16
  import * as RAPIER from '@dimforge/rapier3d-compat';
17
17
  export { RAPIER };
18
+ export { S as StageEntity } from './entity-Bq_eNEDI.js';
18
19
  import 'bitecs';
19
- import 'three/addons/controls/OrbitControls.js';
20
+ import './entity-types-DAu8sGJH.js';
21
+ import '@sinclair/typebox';
20
22
  import 'three/examples/jsm/postprocessing/EffectComposer.js';
23
+ import 'mitt';
24
+ import 'typescript-fsm';
21
25
 
22
- interface MovementSequence2DStep {
23
- name: string;
24
- moveX?: number;
25
- moveY?: number;
26
- timeInSeconds: number;
27
- }
28
- interface MovementSequence2DOptions {
29
- sequence: MovementSequence2DStep[];
30
- loop?: boolean;
26
+ declare const stageState: {
27
+ previous: StageBlueprint | null;
28
+ current: StageBlueprint | null;
29
+ next: StageBlueprint | null;
30
+ isLoading: boolean;
31
+ };
32
+ declare const StageManager: {
33
+ staticRegistry: Map<string, {
34
+ name?: string | undefined;
35
+ assets?: string[] | undefined;
36
+ id: string;
37
+ entities: {
38
+ position?: [number, number] | undefined;
39
+ data?: {
40
+ [x: string]: any;
41
+ } | undefined;
42
+ type: string;
43
+ id: string;
44
+ }[];
45
+ }>;
46
+ registerStaticStage(id: string, blueprint: StageBlueprint): void;
47
+ loadStageData(stageId: string): Promise<StageBlueprint>;
48
+ transitionForward(nextStageId: string, loadStaticStage?: (id: string) => Promise<StageBlueprint>): Promise<void>;
49
+ /**
50
+ * Manually set the next stage to pre-load it.
51
+ */
52
+ preloadNext(stageId: string, loadStaticStage?: (id: string) => Promise<StageBlueprint>): Promise<void>;
53
+ };
54
+
55
+ /**
56
+ * Factory interface for generating entity copies
57
+ */
58
+ interface TemplateFactory<E extends GameEntity<O>, O extends GameEntityOptions = GameEntityOptions> {
59
+ /** The template entity used for cloning */
60
+ readonly template: E;
61
+ /**
62
+ * Generate multiple copies of the template entity
63
+ * @param count Number of copies to generate
64
+ * @returns Array of new entity instances
65
+ */
66
+ generate(count: number): E[];
31
67
  }
32
- type MovementSequence2DCallback = (current: MovementSequence2DStep, index: number, ctx: UpdateContext<MoveableEntity>) => void;
33
68
  /**
34
- * Behavior that sequences 2D movements over time.
35
- * Each step sets linear velocity via `moveXY` for a duration, then advances.
36
- * Defaults to looping when the end is reached.
69
+ * Create an entity factory from a template entity.
70
+ *
71
+ * @param template The entity to use as a template for cloning
72
+ * @returns Factory object with generate() method
73
+ *
74
+ * @example
75
+ * ```typescript
76
+ * const asteroidTemplate = createSprite({ images: [{ name: 'asteroid', file: asteroidImg }] });
77
+ * const asteroidFactory = createEntityFactory(asteroidTemplate);
78
+ * const asteroids = asteroidFactory.generate(10);
79
+ *
80
+ * asteroids.forEach((asteroid, i) => {
81
+ * asteroid.setPosition(Math.random() * 20 - 10, Math.random() * 15 - 7.5, 0);
82
+ * stage.add(asteroid);
83
+ * });
84
+ * ```
37
85
  */
38
- declare function movementSequence2D(opts: MovementSequence2DOptions, onStep?: MovementSequence2DCallback): {
39
- type: BehaviorCallbackType;
40
- handler: (ctx: UpdateContext<MoveableEntity>) => void;
41
- };
86
+ declare function createEntityFactory<E extends GameEntity<O>, O extends GameEntityOptions = GameEntityOptions>(template: E): TemplateFactory<E, O>;
42
87
 
43
88
  declare function destroy(entity: any, globals?: any): void;
44
89
 
@@ -54,6 +99,7 @@ declare function pingPongBeep(frequency?: number, duration?: number): void;
54
99
 
55
100
  /**
56
101
  * Set a global value by path.
102
+ * Emits a 'game:state:updated' event when the value changes.
57
103
  * @example setGlobal('score', 100)
58
104
  * @example setGlobal('player.health', 50)
59
105
  */
@@ -74,14 +120,15 @@ declare function createGlobal<T>(path: string, defaultValue: T): T;
74
120
  declare function getGlobal<T = unknown>(path: string): T | undefined;
75
121
  /**
76
122
  * Subscribe to changes on a global value at a specific path.
77
- * Returns an unsubscribe function.
123
+ * Returns an unsubscribe function. Subscriptions are automatically
124
+ * cleaned up when the game is disposed.
78
125
  * @example const unsub = onGlobalChange('score', (val) => console.log(val));
79
126
  */
80
127
  declare function onGlobalChange<T = unknown>(path: string, callback: (value: T) => void): () => void;
81
128
  /**
82
129
  * Subscribe to changes on multiple global paths.
83
130
  * Callback fires when any of the paths change, receiving all current values.
84
- * Returns an unsubscribe function.
131
+ * Subscriptions are automatically cleaned up when the game is disposed.
85
132
  * @example const unsub = onGlobalChanges(['score', 'lives'], ([score, lives]) => console.log(score, lives));
86
133
  */
87
134
  declare function onGlobalChanges<T extends unknown[] = unknown[]>(paths: string[], callback: (values: T) => void): () => void;
@@ -89,6 +136,11 @@ declare function onGlobalChanges<T extends unknown[] = unknown[]>(paths: string[
89
136
  * Get the entire globals object (read-only snapshot).
90
137
  */
91
138
  declare function getGlobals<T = Record<string, unknown>>(): T;
139
+ /**
140
+ * Unsubscribe all active global subscriptions.
141
+ * Used internally on game dispose to prevent old callbacks from firing.
142
+ */
143
+ declare function clearGlobalSubscriptions(): void;
92
144
 
93
145
  /**
94
146
  * Set a variable on an object by path.
@@ -123,13 +175,77 @@ declare function onVariableChange<T = unknown>(target: object, path: string, cal
123
175
  */
124
176
  declare function onVariableChanges<T extends unknown[] = unknown[]>(target: object, paths: string[], callback: (values: T) => void): () => void;
125
177
 
178
+ type DebugTools = 'select' | 'translate' | 'rotate' | 'scale' | 'delete' | 'none';
179
+ interface DebugState {
180
+ enabled: boolean;
181
+ paused: boolean;
182
+ tool: DebugTools;
183
+ selectedEntity: GameEntity<any> | null;
184
+ hoveredEntity: GameEntity<any> | null;
185
+ flags: Set<string>;
186
+ }
187
+ declare const debugState: DebugState;
188
+ declare function setPaused(paused: boolean): void;
189
+ declare function setDebugTool(tool: DebugTools): void;
190
+
191
+ /**
192
+ * State interface for editor-to-game communication
193
+ */
194
+ interface ZylemGameState {
195
+ gameState?: {
196
+ debugFlag?: boolean;
197
+ [key: string]: unknown;
198
+ };
199
+ toolbarState?: {
200
+ tool?: DebugTools;
201
+ paused?: boolean;
202
+ };
203
+ [key: string]: unknown;
204
+ }
126
205
  declare class ZylemGameElement extends HTMLElement {
127
206
  private _game;
207
+ private _state;
128
208
  private container;
129
209
  constructor();
210
+ /**
211
+ * Focus the game container for keyboard input
212
+ */
213
+ focus(): void;
130
214
  set game(game: Game<any>);
131
215
  get game(): Game<any> | null;
216
+ set state(value: ZylemGameState);
217
+ get state(): ZylemGameState;
218
+ /**
219
+ * Sync the web component's state with the game-lib's internal debug state
220
+ */
221
+ private syncDebugState;
222
+ /**
223
+ * Sync toolbar state with game-lib's debug state
224
+ */
225
+ private syncToolbarState;
132
226
  disconnectedCallback(): void;
133
227
  }
134
228
 
135
- export { Game, ZylemGameElement, createGlobal, createVariable, destroy, getGlobal, getGlobals, getVariable, movementSequence2D, onGlobalChange, onGlobalChanges, onVariableChange, onVariableChanges, pingPongBeep, ricochetSound, setGlobal, setVariable };
229
+ declare const fireShader: {
230
+ vertex: string;
231
+ fragment: string;
232
+ };
233
+
234
+ declare const starShader: {
235
+ vertex: string;
236
+ fragment: string;
237
+ };
238
+
239
+ declare const standardShader: {
240
+ vertex: string;
241
+ fragment: string;
242
+ };
243
+
244
+ declare const debugShader: {
245
+ vertex: string;
246
+ fragment: string;
247
+ };
248
+
249
+ declare const objectVertexShader = "\nuniform vec2 uvScale;\nvarying vec2 vUv;\n\nvoid main() {\n\tvUv = uv;\n\tvec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );\n\tgl_Position = projectionMatrix * mvPosition;\n}\n";
250
+
251
+ export { type DebugTools, Game, StageBlueprint, StageManager, type TemplateFactory, ZylemGameElement, type ZylemGameState, clearGlobalSubscriptions, createEntityFactory, createGlobal, createVariable, debugShader, debugState, destroy, fireShader, getGlobal, getGlobals, getVariable, objectVertexShader, onGlobalChange, onGlobalChanges, onVariableChange, onVariableChanges, pingPongBeep, ricochetSound, setDebugTool, setGlobal, setPaused, setVariable, stageState, standardShader, starShader };