Game
@zylem/game-lib / core / Game
Class: Game<TGlobals>
Defined in: packages/game-lib/src/lib/game/game.ts:21
Type Parameters
TGlobals
TGlobals extends BaseGlobals
Implements
IGame<TGlobals>
Constructors
Constructor
new Game<
TGlobals>(options):Game<TGlobals>
Defined in: packages/game-lib/src/lib/game/game.ts:46
Parameters
options
GameOptions<TGlobals>
Returns
Game<TGlobals>
Properties
experimental
readonlyexperimental:object
Defined in: packages/game-lib/src/lib/game/game.ts:219
Experimental escape hatches. APIs here are unstable and may change or be removed without a major version bump.
getRuntime
getRuntime: () =>
Simulation|null
The @zylem/behaviors Simulation backing the current stage's
physics world, or null before a stage is loaded. Intended for debug
tooling and the editor; games should not need it.
Returns
Simulation | null
options
options:
GameOptions<TGlobals>
Defined in: packages/game-lib/src/lib/game/game.ts:24
refErrorMessage
refErrorMessage:
string='lost reference to game'
Defined in: packages/game-lib/src/lib/game/game.ts:44
Methods
dispatch()
dispatch<
K>(event,payload):void
Defined in: packages/game-lib/src/lib/game/game.ts:385
Dispatch an event from the game. Events are emitted both locally and to the global event bus.
Type Parameters
K
K extends keyof GameEvents
Parameters
event
K
payload
GameEvents[K]
Returns
void
dispose()
dispose():
void
Defined in: packages/game-lib/src/lib/game/game.ts:357
Returns
void
end()
end():
Promise<void>
Defined in: packages/game-lib/src/lib/game/game.ts:355
Returns
Promise<void>
Implementation of
IGame.end
getCurrentStage()
getCurrentStage():
Stage|null
Defined in: packages/game-lib/src/lib/game/game.ts:211
Get the current stage wrapper.
Returns
Stage | null
goToStage()
goToStage():
Promise<void>
Defined in: packages/game-lib/src/lib/game/game.ts:353
Returns
Promise<void>
Implementation of
IGame.goToStage
listen()
listen<
K>(event,handler): () =>void
Defined in: packages/game-lib/src/lib/game/game.ts:395
Listen for events on this game instance.
Type Parameters
K
K extends keyof GameEvents
Parameters
event
K
handler
(payload) => void
Returns
Unsubscribe function
() => void
loadStageFromId()
loadStageFromId(
stageId):Promise<void>
Defined in: packages/game-lib/src/lib/game/game.ts:294
Parameters
stageId
string
Returns
Promise<void>
Implementation of
IGame.loadStageFromId
nextStage()
nextStage(
options?):void
Defined in: packages/game-lib/src/lib/game/game.ts:319
Go to the next stage. Pass \{ transition \} to blend from the current
stage instead of cutting.
Parameters
options?
Returns
void
Example
game.nextStage({
transition: { duration: 1.2, shader: createStageTransition({ pattern: 'noise' }) },
});
Implementation of
IGame.nextStage
onDestroy()
onDestroy(...
callbacks):this
Defined in: packages/game-lib/src/lib/game/game.ts:69
Parameters
callbacks
...DestroyFunction<ZylemGame<TGlobals>, TGlobals>[]
Returns
this
onGlobalChange()
onGlobalChange<
T>(path,callback):this
Defined in: packages/game-lib/src/lib/game/game.ts:173
Subscribe to changes on a global value. Subscriptions are registered when the game starts and cleaned up when disposed. The callback receives the value and the current stage.
Type Parameters
T
T = unknown
Parameters
path
string
callback
(value, stage) => void
Returns
this
Example
game.onGlobalChange('score', (val, stage) => console.log(val));
onGlobalChanges()
onGlobalChanges<
T>(paths,callback):this
Defined in: packages/game-lib/src/lib/game/game.ts:184
Subscribe to changes on multiple global paths. Subscriptions are registered when the game starts and cleaned up when disposed. The callback receives the values and the current stage.
Type Parameters
T
T extends unknown[] = unknown[]
Parameters
paths
string[]
callback
(values, stage) => void
Returns
this
Example
game.onGlobalChanges(['score', 'lives'], ([score, lives], stage) => console.log(score, lives));
onLoading()
onLoading(
callback): () =>void
Defined in: packages/game-lib/src/lib/game/game.ts:405
Subscribe to loading events from the game. Events include stage context (stageName, stageIndex).
Parameters
callback
(event) => void
Invoked for each loading event
Returns
Unsubscribe function
() => void
Implementation of
IGame.onLoading
onSetup()
onSetup(...
callbacks):this
Defined in: packages/game-lib/src/lib/game/game.ts:59
Parameters
callbacks
...SetupFunction<ZylemGame<TGlobals>, TGlobals>[]
Returns
this
onUpdate()
onUpdate(...
callbacks):this
Defined in: packages/game-lib/src/lib/game/game.ts:64
Parameters
callbacks
...UpdateFunction<ZylemGame<TGlobals>, TGlobals>[]
Returns
this
pause()
pause():
Promise<void>
Defined in: packages/game-lib/src/lib/game/game.ts:231
Returns
Promise<void>
Implementation of
IGame.pause
previousStage()
previousStage(
options?):void
Defined in: packages/game-lib/src/lib/game/game.ts:276
Go to the previous stage in the stage array. Pass \{ transition \} to
blend from the current stage instead of cutting.
Parameters
options?
Returns
void
Implementation of
IGame.previousStage
reset()
reset(
options?):Promise<void>
Defined in: packages/game-lib/src/lib/game/game.ts:261
Reload the first stage. Pass \{ transition \} to blend from the current
stage instead of cutting.
Parameters
options?
Returns
Promise<void>
Example
game.reset({ transition: { duration: 1 } });
Implementation of
IGame.reset
resume()
resume():
Promise<void>
Defined in: packages/game-lib/src/lib/game/game.ts:235
Returns
Promise<void>
Implementation of
IGame.resume
setDeviceProfile()
setDeviceProfile(
profile):this
Defined in: packages/game-lib/src/lib/game/game.ts:117
Parameters
profile
Returns
this
setDisplayRuntime()
setDisplayRuntime(
runtime):this
Defined in: packages/game-lib/src/lib/game/game.ts:106
Parameters
runtime
Partial<ResolveGameConfigRuntime>
Returns
this
setInputConfiguration()
setInputConfiguration(...
configs):this
Defined in: packages/game-lib/src/lib/game/game.ts:81
Set composable input configuration as global defaults for the entire game. Multiple configs are deep-merged (later configs win on key conflicts). Per-stage overrides (via stage.setInputConfiguration) are merged on top. Can be called before or after start().
Parameters
configs
...GameInputConfig[]
Returns
this
Example
game.setInputConfiguration(useArrowsForAxes('p1'), useWASDForDirections('p2'));
setViewportSize()
setViewportSize(
width,height):this
Defined in: packages/game-lib/src/lib/game/game.ts:121
Parameters
width
number
height
number
Returns
this
start()
start():
Promise<Game<TGlobals>>
Defined in: packages/game-lib/src/lib/game/game.ts:90
Returns
Promise<Game<TGlobals>>
Implementation of
IGame.start
step()
step(
deltaTime?):void
Defined in: packages/game-lib/src/lib/game/game.ts:248
Execute a single frame update. Useful for testing or manual frame stepping.
Parameters
deltaTime?
number = ...
Optional delta time in seconds (defaults to 1/60)
Returns
void