@zylem/game-lib 0.3.16 → 0.3.17
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.
- package/README.md +38 -13
- package/dist/.vite/manifest.json +82 -12
- package/dist/core.d.ts +1 -1
- package/dist/lib/core/base-node-life-cycle.d.ts +14 -0
- package/dist/lib/core/base-node.d.ts +6 -2
- package/dist/lib/core/base-node.js +6 -2
- package/dist/lib/core/lazy-loader.d.ts +2 -2
- package/dist/lib/core/utility/nodes.d.ts +11 -0
- package/dist/lib/core/utility/nodes.js +27 -0
- package/dist/lib/core/utility/strings.d.ts +2 -0
- package/dist/lib/core/utility/strings.js +14 -0
- package/dist/lib/core/{utility.d.ts → utility/vector.d.ts} +0 -2
- package/dist/lib/core/utility/vector.js +8 -0
- package/dist/lib/core/vessel.d.ts +3 -1
- package/dist/lib/core/vessel.js +13 -9
- package/dist/lib/device/aspect-ratio.d.ts +37 -0
- package/dist/lib/device/aspect-ratio.js +44 -0
- package/dist/lib/entities/entity.d.ts +3 -1
- package/dist/lib/entities/entity.js +9 -5
- package/dist/lib/game/game-blueprint.d.ts +44 -0
- package/dist/lib/game/game-canvas.d.ts +34 -0
- package/dist/lib/game/game-canvas.js +57 -0
- package/dist/lib/game/game-config.d.ts +47 -0
- package/dist/lib/game/game-config.js +53 -0
- package/dist/lib/game/game-default.d.ts +18 -0
- package/dist/lib/game/game-default.js +24 -0
- package/dist/lib/game/game.d.ts +2 -5
- package/dist/lib/game/game.js +48 -72
- package/dist/lib/game/zylem-game.d.ts +12 -1
- package/dist/lib/game/zylem-game.js +55 -29
- package/dist/lib/graphics/material.d.ts +1 -1
- package/dist/lib/graphics/material.js +1 -1
- package/dist/lib/graphics/zylem-scene.d.ts +7 -7
- package/dist/lib/graphics/zylem-scene.js +26 -40
- package/dist/lib/stage/stage-blueprint.d.ts +44 -0
- package/dist/lib/stage/stage-blueprint.js +56 -0
- package/dist/lib/stage/stage-default.d.ts +9 -0
- package/dist/lib/stage/stage-default.js +42 -0
- package/dist/lib/stage/stage.d.ts +2 -2
- package/dist/lib/stage/stage.js +7 -5
- package/dist/lib/stage/zylem-stage.d.ts +4 -3
- package/dist/lib/stage/zylem-stage.js +38 -49
- package/dist/main.d.ts +2 -1
- package/dist/main.js +50 -48
- package/dist/stage.d.ts +3 -0
- package/dist/stage.js +20 -4
- package/dist/tests/stage/stage-blueprint.spec.d.ts +1 -0
- package/package.json +1 -1
- package/dist/lib/core/game-canvas.d.ts +0 -4
- package/dist/lib/core/utility.js +0 -20
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { Stage } from '../stage/stage';
|
|
2
|
+
import type { BasicTypes, GlobalVariablesType, ZylemGameConfig } from './game-interfaces';
|
|
3
|
+
import type { GameOptions } from '../core/utility/nodes';
|
|
4
|
+
/**
|
|
5
|
+
* A lightweight, serializable blueprint representing the initial configuration
|
|
6
|
+
* of a `Game`. It should not include runtime references. Use blueprints only to
|
|
7
|
+
* build games.
|
|
8
|
+
*/
|
|
9
|
+
export interface GameBlueprint<TGlobals extends Record<string, BasicTypes> = GlobalVariablesType> {
|
|
10
|
+
id: string;
|
|
11
|
+
name?: string;
|
|
12
|
+
config: Partial<ZylemGameConfig<Stage, any, TGlobals>>;
|
|
13
|
+
}
|
|
14
|
+
export declare const gameBlueprintsState: {
|
|
15
|
+
byId: Record<string, GameBlueprint>;
|
|
16
|
+
order: string[];
|
|
17
|
+
currentId: string | null;
|
|
18
|
+
};
|
|
19
|
+
/** Reset the blueprints store back to its initial empty state. */
|
|
20
|
+
export declare function resetGameBlueprints(): void;
|
|
21
|
+
/** Create and register a new `GameBlueprint`. */
|
|
22
|
+
export declare function createGameBlueprint<TGlobals extends Record<string, BasicTypes> = GlobalVariablesType>(config: Partial<ZylemGameConfig<Stage, any, TGlobals>>, options?: {
|
|
23
|
+
id?: string;
|
|
24
|
+
name?: string;
|
|
25
|
+
setCurrent?: boolean;
|
|
26
|
+
}): GameBlueprint<TGlobals>;
|
|
27
|
+
/** Upsert a blueprint into the store. */
|
|
28
|
+
export declare function upsertGameBlueprint<TGlobals extends Record<string, BasicTypes> = GlobalVariablesType>(blueprint: GameBlueprint<TGlobals>): void;
|
|
29
|
+
/** Remove a blueprint by id. */
|
|
30
|
+
export declare function removeGameBlueprint(id: string): void;
|
|
31
|
+
/** Get a blueprint by id. */
|
|
32
|
+
export declare function getGameBlueprint(id: string): GameBlueprint | undefined;
|
|
33
|
+
/** List all blueprints in insertion order. */
|
|
34
|
+
export declare function listGameBlueprints(): GameBlueprint[];
|
|
35
|
+
/** Set the current blueprint id (or clear by passing null). */
|
|
36
|
+
export declare function setCurrentGameBlueprint(id: string | null): void;
|
|
37
|
+
/** Get the current blueprint object, if any. */
|
|
38
|
+
export declare function getCurrentGameBlueprint(): GameBlueprint | null;
|
|
39
|
+
/**
|
|
40
|
+
* Build a `Game` instance from a blueprint and optional extra `GameOptions`
|
|
41
|
+
* (e.g., additional stages or nodes). This returns a `Game` wrapper instance;
|
|
42
|
+
* call `.start()` to run it.
|
|
43
|
+
*/
|
|
44
|
+
export declare function buildGameFromBlueprint<TGlobals extends Record<string, BasicTypes> = GlobalVariablesType>(input: string | GameBlueprint<TGlobals>, ...extras: GameOptions<TGlobals>): import("./game").Game<TGlobals>;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { AspectRatioValue } from '../device/aspect-ratio';
|
|
2
|
+
export interface GameCanvasOptions {
|
|
3
|
+
id: string;
|
|
4
|
+
container?: HTMLElement;
|
|
5
|
+
containerId?: string;
|
|
6
|
+
canvas?: HTMLCanvasElement;
|
|
7
|
+
bodyBackground?: string;
|
|
8
|
+
fullscreen?: boolean;
|
|
9
|
+
aspectRatio: AspectRatioValue | number;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* GameCanvas is a DOM delegate that owns:
|
|
13
|
+
* - container lookup/creation and styling (including fullscreen centering)
|
|
14
|
+
* - body background application
|
|
15
|
+
* - canvas mounting into container
|
|
16
|
+
* - aspect ratio sizing via AspectRatioDelegate
|
|
17
|
+
*/
|
|
18
|
+
export declare class GameCanvas {
|
|
19
|
+
id: string;
|
|
20
|
+
container: HTMLElement;
|
|
21
|
+
canvas: HTMLCanvasElement;
|
|
22
|
+
bodyBackground?: string;
|
|
23
|
+
fullscreen: boolean;
|
|
24
|
+
aspectRatio: number;
|
|
25
|
+
private ratioDelegate;
|
|
26
|
+
constructor(options: GameCanvasOptions);
|
|
27
|
+
applyBodyBackground(): void;
|
|
28
|
+
mountCanvas(): void;
|
|
29
|
+
mountRenderer(rendererDom: HTMLCanvasElement, onResize: (width: number, height: number) => void): void;
|
|
30
|
+
centerIfFullscreen(): void;
|
|
31
|
+
attachAspectRatio(onResize: (width: number, height: number) => void): void;
|
|
32
|
+
destroy(): void;
|
|
33
|
+
private ensureContainer;
|
|
34
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { AspectRatioDelegate as s } from "../device/aspect-ratio.js";
|
|
2
|
+
class o {
|
|
3
|
+
id;
|
|
4
|
+
container;
|
|
5
|
+
canvas;
|
|
6
|
+
bodyBackground;
|
|
7
|
+
fullscreen;
|
|
8
|
+
aspectRatio;
|
|
9
|
+
ratioDelegate = null;
|
|
10
|
+
constructor(t) {
|
|
11
|
+
this.id = t.id, this.container = this.ensureContainer(t.containerId ?? t.id, t.container), this.canvas = t.canvas ?? document.createElement("canvas"), this.bodyBackground = t.bodyBackground, this.fullscreen = !!t.fullscreen, this.aspectRatio = (typeof t.aspectRatio == "number", t.aspectRatio);
|
|
12
|
+
}
|
|
13
|
+
applyBodyBackground() {
|
|
14
|
+
this.bodyBackground && (document.body.style.background = this.bodyBackground);
|
|
15
|
+
}
|
|
16
|
+
mountCanvas() {
|
|
17
|
+
for (; this.container.firstChild; )
|
|
18
|
+
this.container.removeChild(this.container.firstChild);
|
|
19
|
+
this.container.appendChild(this.canvas);
|
|
20
|
+
}
|
|
21
|
+
mountRenderer(t, i) {
|
|
22
|
+
for (; this.container.firstChild; )
|
|
23
|
+
this.container.removeChild(this.container.firstChild);
|
|
24
|
+
this.container.appendChild(t), this.canvas = t, this.attachAspectRatio(i);
|
|
25
|
+
}
|
|
26
|
+
centerIfFullscreen() {
|
|
27
|
+
if (!this.fullscreen)
|
|
28
|
+
return;
|
|
29
|
+
const t = this.container.style;
|
|
30
|
+
t.display = "flex", t.alignItems = "center", t.justifyContent = "center", t.position = "fixed", t.inset = "0";
|
|
31
|
+
}
|
|
32
|
+
attachAspectRatio(t) {
|
|
33
|
+
this.ratioDelegate ? this.ratioDelegate.apply() : (this.ratioDelegate = new s({
|
|
34
|
+
container: this.container,
|
|
35
|
+
canvas: this.canvas,
|
|
36
|
+
aspectRatio: this.aspectRatio,
|
|
37
|
+
onResize: t
|
|
38
|
+
}), this.ratioDelegate.attach());
|
|
39
|
+
}
|
|
40
|
+
destroy() {
|
|
41
|
+
this.ratioDelegate?.detach(), this.ratioDelegate = null;
|
|
42
|
+
}
|
|
43
|
+
ensureContainer(t, i) {
|
|
44
|
+
if (i)
|
|
45
|
+
return i;
|
|
46
|
+
if (t) {
|
|
47
|
+
const a = document.getElementById(t);
|
|
48
|
+
if (a)
|
|
49
|
+
return a;
|
|
50
|
+
}
|
|
51
|
+
const n = t || this.id || "zylem-root", e = document.createElement("main");
|
|
52
|
+
return e.setAttribute("id", n), e.style.position = "relative", e.style.width = "100%", e.style.height = "100%", document.body.appendChild(e), e;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
export {
|
|
56
|
+
o as GameCanvas
|
|
57
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { StageInterface } from "../types";
|
|
2
|
+
import { GameInputConfig } from "./game-interfaces";
|
|
3
|
+
import { AspectRatio, AspectRatioValue } from "../device/aspect-ratio";
|
|
4
|
+
export type GameConfigLike = Partial<{
|
|
5
|
+
id: string;
|
|
6
|
+
globals: Record<string, any>;
|
|
7
|
+
stages: StageInterface[];
|
|
8
|
+
debug: boolean;
|
|
9
|
+
time: number;
|
|
10
|
+
input: GameInputConfig;
|
|
11
|
+
/** numeric value or key in AspectRatio */
|
|
12
|
+
aspectRatio: AspectRatioValue | keyof typeof AspectRatio;
|
|
13
|
+
fullscreen: boolean;
|
|
14
|
+
/** CSS background value for document body */
|
|
15
|
+
bodyBackground: string;
|
|
16
|
+
/** existing container by reference */
|
|
17
|
+
container: HTMLElement;
|
|
18
|
+
/** create/find container by id */
|
|
19
|
+
containerId: string;
|
|
20
|
+
/** optional canvas if caller wants to manage it */
|
|
21
|
+
canvas: HTMLCanvasElement;
|
|
22
|
+
}>;
|
|
23
|
+
export declare class GameConfig {
|
|
24
|
+
id: string;
|
|
25
|
+
globals: Record<string, any>;
|
|
26
|
+
stages: StageInterface[];
|
|
27
|
+
debug: boolean;
|
|
28
|
+
time: number;
|
|
29
|
+
input: GameInputConfig | undefined;
|
|
30
|
+
aspectRatio: number;
|
|
31
|
+
fullscreen: boolean;
|
|
32
|
+
bodyBackground: string | undefined;
|
|
33
|
+
container: HTMLElement;
|
|
34
|
+
containerId?: string | undefined;
|
|
35
|
+
canvas?: HTMLCanvasElement | undefined;
|
|
36
|
+
constructor(id: string, globals: Record<string, any>, stages: StageInterface[], debug: boolean, time: number, input: GameInputConfig | undefined, aspectRatio: number, fullscreen: boolean, bodyBackground: string | undefined, container: HTMLElement, containerId?: string | undefined, canvas?: HTMLCanvasElement | undefined);
|
|
37
|
+
}
|
|
38
|
+
export declare function createDefaultGameConfig(base?: Partial<Pick<GameConfig, 'id' | 'debug' | 'time' | 'input'>> & {
|
|
39
|
+
stages?: StageInterface[];
|
|
40
|
+
globals?: Record<string, any>;
|
|
41
|
+
}): GameConfig;
|
|
42
|
+
export declare function resolveGameConfig(user?: GameConfigLike): GameConfig;
|
|
43
|
+
/**
|
|
44
|
+
* Factory for authoring configuration objects in user code.
|
|
45
|
+
* Returns a plain object that can be passed to `game(...)`.
|
|
46
|
+
*/
|
|
47
|
+
export declare function gameConfig(config: GameConfigLike): GameConfigLike;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { AspectRatio as d } from "../device/aspect-ratio.js";
|
|
2
|
+
class g {
|
|
3
|
+
id;
|
|
4
|
+
globals;
|
|
5
|
+
stages;
|
|
6
|
+
debug;
|
|
7
|
+
time;
|
|
8
|
+
input;
|
|
9
|
+
aspectRatio;
|
|
10
|
+
fullscreen;
|
|
11
|
+
bodyBackground;
|
|
12
|
+
container;
|
|
13
|
+
containerId;
|
|
14
|
+
canvas;
|
|
15
|
+
constructor(n, o, e, i, a, c, s, l, u, m, f, p) {
|
|
16
|
+
this.id = n, this.globals = o, this.stages = e, this.debug = i, this.time = a, this.input = c, this.aspectRatio = s, this.fullscreen = l, this.bodyBackground = u, this.container = m, this.containerId = f, this.canvas = p;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function r(t, n) {
|
|
20
|
+
if (n)
|
|
21
|
+
return n;
|
|
22
|
+
if (t) {
|
|
23
|
+
const i = document.getElementById(t);
|
|
24
|
+
if (i)
|
|
25
|
+
return i;
|
|
26
|
+
}
|
|
27
|
+
const o = t || "zylem-root", e = document.createElement("main");
|
|
28
|
+
return e.setAttribute("id", o), e.style.position = "relative", e.style.width = "100%", e.style.height = "100%", document.body.appendChild(e), e;
|
|
29
|
+
}
|
|
30
|
+
function b(t) {
|
|
31
|
+
const n = t?.id ?? "zylem", o = r(n);
|
|
32
|
+
return new g(n, t?.globals ?? {}, t?.stages ?? [], !!t?.debug, t?.time ?? 0, t?.input, d.SixteenByNine, !0, "#000000", o, n, void 0);
|
|
33
|
+
}
|
|
34
|
+
function y(t) {
|
|
35
|
+
const n = b({
|
|
36
|
+
id: t?.id ?? "zylem",
|
|
37
|
+
debug: !!t?.debug,
|
|
38
|
+
time: t?.time ?? 0,
|
|
39
|
+
input: t?.input,
|
|
40
|
+
stages: t?.stages ?? [],
|
|
41
|
+
globals: t?.globals ?? {}
|
|
42
|
+
}), o = t?.containerId ?? n.containerId, e = r(o, t?.container ?? null), i = t?.aspectRatio ?? n.aspectRatio, a = typeof i == "number" ? i : d[i] ?? d.SixteenByNine, c = t?.fullscreen ?? n.fullscreen, s = t?.bodyBackground ?? n.bodyBackground, l = t?.canvas ?? void 0;
|
|
43
|
+
return new g(t?.id ?? n.id, t?.globals ?? n.globals, t?.stages ?? n.stages, !!(t?.debug ?? n.debug), t?.time ?? n.time, t?.input ?? n.input, a, c, s, e, o, l);
|
|
44
|
+
}
|
|
45
|
+
function B(t) {
|
|
46
|
+
return { ...t };
|
|
47
|
+
}
|
|
48
|
+
export {
|
|
49
|
+
g as GameConfig,
|
|
50
|
+
b as createDefaultGameConfig,
|
|
51
|
+
B as gameConfig,
|
|
52
|
+
y as resolveGameConfig
|
|
53
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Stage } from '../stage/stage';
|
|
2
|
+
import type { BasicTypes, GlobalVariablesType, ZylemGameConfig, GameInputConfig } from './game-interfaces';
|
|
3
|
+
export declare const gameDefaultsState: Partial<ZylemGameConfig<Stage, any, GlobalVariablesType>>;
|
|
4
|
+
/** Replace multiple defaults at once (shallow merge). */
|
|
5
|
+
export declare function setGameDefaults(partial: Partial<ZylemGameConfig<Stage, any, GlobalVariablesType>>): void;
|
|
6
|
+
/** Reset defaults back to library defaults. */
|
|
7
|
+
export declare function resetGameDefaults(): void;
|
|
8
|
+
/**
|
|
9
|
+
* Get a plain object copy of the current defaults.
|
|
10
|
+
*/
|
|
11
|
+
export declare function getGameDefaultConfig<TGlobals extends Record<string, BasicTypes> = GlobalVariablesType>(): {
|
|
12
|
+
id: string;
|
|
13
|
+
globals: TGlobals;
|
|
14
|
+
stages: Stage[];
|
|
15
|
+
debug?: boolean;
|
|
16
|
+
time?: number;
|
|
17
|
+
input?: GameInputConfig;
|
|
18
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { proxy as i } from "valtio/vanilla";
|
|
2
|
+
import { stage as e } from "../stage/stage.js";
|
|
3
|
+
const a = () => ({
|
|
4
|
+
id: "zylem",
|
|
5
|
+
globals: {},
|
|
6
|
+
stages: [e()],
|
|
7
|
+
debug: !1,
|
|
8
|
+
time: 0,
|
|
9
|
+
input: void 0
|
|
10
|
+
}), t = i({ ...a() });
|
|
11
|
+
function g() {
|
|
12
|
+
return {
|
|
13
|
+
id: t.id ?? "zylem",
|
|
14
|
+
globals: t.globals ?? {},
|
|
15
|
+
stages: t.stages ?? [e()],
|
|
16
|
+
debug: t.debug,
|
|
17
|
+
time: t.time,
|
|
18
|
+
input: t.input
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
export {
|
|
22
|
+
t as gameDefaultsState,
|
|
23
|
+
g as getGameDefaultConfig
|
|
24
|
+
};
|
package/dist/lib/game/game.d.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { BaseNode } from '../core/base-node';
|
|
2
1
|
import { ZylemGame } from './zylem-game';
|
|
3
2
|
import { Stage } from '../stage/stage';
|
|
4
3
|
import { DestroyFunction, IGame, SetupFunction, UpdateFunction } from '../core/base-node-life-cycle';
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
4
|
+
import { BasicTypes, GlobalVariablesType } from './game-interfaces';
|
|
5
|
+
import { GameOptions } from '../core/utility/nodes';
|
|
7
6
|
export declare class Game<TGlobals extends Record<string, BasicTypes> = GlobalVariablesType> implements IGame<TGlobals> {
|
|
8
7
|
gameRef: ZylemGame<TGlobals> | null;
|
|
9
8
|
options: GameOptions<TGlobals>;
|
|
@@ -28,7 +27,6 @@ export declare class Game<TGlobals extends Record<string, BasicTypes> = GlobalVa
|
|
|
28
27
|
setGlobal<K extends keyof TGlobals>(key: K, value: TGlobals[K]): void;
|
|
29
28
|
onGlobalChange<K extends keyof TGlobals>(key: K, callback: (value: TGlobals[K]) => void): void;
|
|
30
29
|
}
|
|
31
|
-
type GameOptions<TGlobals extends Record<string, BasicTypes> = GlobalVariablesType> = Array<ZylemGameConfig<Stage, Game<TGlobals>, TGlobals> | Stage | GameEntityLifeCycle | BaseNode>;
|
|
32
30
|
/**
|
|
33
31
|
* create a new game
|
|
34
32
|
* @param options GameOptions - Array of IGameOptions, Stage, GameEntity, or BaseNode objects
|
|
@@ -38,4 +36,3 @@ type GameOptions<TGlobals extends Record<string, BasicTypes> = GlobalVariablesTy
|
|
|
38
36
|
* @returns Game
|
|
39
37
|
*/
|
|
40
38
|
export declare function game<TGlobals extends Record<string, BasicTypes> = GlobalVariablesType>(...options: GameOptions<TGlobals>): Game<TGlobals>;
|
|
41
|
-
export {};
|
package/dist/lib/game/game.js
CHANGED
|
@@ -1,37 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { Stage as o, stage as l } from "../stage/stage.js";
|
|
1
|
+
import { ZylemGame as n } from "./zylem-game.js";
|
|
2
|
+
import { Stage as o } from "../stage/stage.js";
|
|
4
3
|
import { setPaused as r } from "../debug/debug-state.js";
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
globals: {},
|
|
10
|
-
stages: [
|
|
11
|
-
l()
|
|
12
|
-
]
|
|
13
|
-
};
|
|
14
|
-
function d(n) {
|
|
15
|
-
let e = { ...f };
|
|
16
|
-
const t = [], s = [], a = [];
|
|
17
|
-
return Object.values(n).forEach((i) => {
|
|
18
|
-
if (i instanceof o)
|
|
19
|
-
s.push(i);
|
|
20
|
-
else if (i instanceof u)
|
|
21
|
-
a.push(i);
|
|
22
|
-
else if (i instanceof h)
|
|
23
|
-
a.push(i);
|
|
24
|
-
else if (i.constructor.name === "Object" && typeof i == "object") {
|
|
25
|
-
const g = Object.assign(f, { ...i });
|
|
26
|
-
t.push(g);
|
|
27
|
-
}
|
|
28
|
-
}), t.forEach((i) => {
|
|
29
|
-
e = Object.assign(e, { ...i });
|
|
30
|
-
}), s.forEach((i) => {
|
|
31
|
-
i.addEntities(a);
|
|
32
|
-
}), s.length ? e.stages = s : e.stages[0].addEntities(a), e;
|
|
33
|
-
}
|
|
34
|
-
class R {
|
|
4
|
+
import { getGlobalState as g, setGlobalState as f } from "./game-state.js";
|
|
5
|
+
import { convertNodes as h } from "../core/utility/nodes.js";
|
|
6
|
+
import { resolveGameConfig as l } from "./game-config.js";
|
|
7
|
+
class u {
|
|
35
8
|
gameRef = null;
|
|
36
9
|
options;
|
|
37
10
|
pendingGlobalChangeHandlers = [];
|
|
@@ -42,17 +15,20 @@ class R {
|
|
|
42
15
|
destroy = () => {
|
|
43
16
|
};
|
|
44
17
|
refErrorMessage = "lost reference to game";
|
|
45
|
-
constructor(
|
|
46
|
-
this.options =
|
|
18
|
+
constructor(t) {
|
|
19
|
+
this.options = t;
|
|
47
20
|
}
|
|
48
21
|
async start() {
|
|
49
|
-
const
|
|
50
|
-
return this.gameRef =
|
|
22
|
+
const t = await this.load();
|
|
23
|
+
return this.gameRef = t, this.setOverrides(), t.start(), this;
|
|
51
24
|
}
|
|
52
25
|
async load() {
|
|
53
26
|
console.log("loading game", this.options);
|
|
54
|
-
const
|
|
55
|
-
|
|
27
|
+
const t = await h(this.options), s = l(t), e = new n({
|
|
28
|
+
...t,
|
|
29
|
+
...s
|
|
30
|
+
}, this);
|
|
31
|
+
return await e.loadStage(t.stages[0]), e;
|
|
56
32
|
}
|
|
57
33
|
setOverrides() {
|
|
58
34
|
if (!this.gameRef) {
|
|
@@ -60,8 +36,8 @@ class R {
|
|
|
60
36
|
return;
|
|
61
37
|
}
|
|
62
38
|
if (this.gameRef.customSetup = this.setup, this.gameRef.customUpdate = this.update, this.gameRef.customDestroy = this.destroy, this.pendingGlobalChangeHandlers.length) {
|
|
63
|
-
for (const { key:
|
|
64
|
-
this.gameRef.onGlobalChange(
|
|
39
|
+
for (const { key: t, callback: s } of this.pendingGlobalChangeHandlers)
|
|
40
|
+
this.gameRef.onGlobalChange(t, s);
|
|
65
41
|
this.pendingGlobalChangeHandlers = [];
|
|
66
42
|
}
|
|
67
43
|
}
|
|
@@ -83,75 +59,75 @@ class R {
|
|
|
83
59
|
console.error(this.refErrorMessage);
|
|
84
60
|
return;
|
|
85
61
|
}
|
|
86
|
-
const
|
|
87
|
-
if (!
|
|
62
|
+
const t = this.gameRef.currentStageId, s = this.gameRef.stages.findIndex((a) => a.stageRef.uuid === t), e = this.gameRef.stages[s + 1];
|
|
63
|
+
if (!e) {
|
|
88
64
|
console.error("next stage called on last stage");
|
|
89
65
|
return;
|
|
90
66
|
}
|
|
91
|
-
await this.gameRef.loadStage(
|
|
67
|
+
await this.gameRef.loadStage(e);
|
|
92
68
|
}
|
|
93
69
|
async previousStage() {
|
|
94
70
|
if (!this.gameRef) {
|
|
95
71
|
console.error(this.refErrorMessage);
|
|
96
72
|
return;
|
|
97
73
|
}
|
|
98
|
-
const
|
|
99
|
-
if (!
|
|
74
|
+
const t = this.gameRef.currentStageId, s = this.gameRef.stages.findIndex((a) => a.stageRef.uuid === t), e = this.gameRef.stages[s - 1];
|
|
75
|
+
if (!e) {
|
|
100
76
|
console.error("previous stage called on first stage");
|
|
101
77
|
return;
|
|
102
78
|
}
|
|
103
|
-
await this.gameRef.loadStage(
|
|
79
|
+
await this.gameRef.loadStage(e);
|
|
104
80
|
}
|
|
105
81
|
async goToStage() {
|
|
106
82
|
}
|
|
107
83
|
async end() {
|
|
108
84
|
}
|
|
109
|
-
add(...
|
|
110
|
-
for (const
|
|
111
|
-
if (
|
|
112
|
-
if (
|
|
113
|
-
this.gameRef ? (this.gameRef.stages.push(
|
|
85
|
+
add(...t) {
|
|
86
|
+
for (const s of t)
|
|
87
|
+
if (s) {
|
|
88
|
+
if (s instanceof o) {
|
|
89
|
+
this.gameRef ? (this.gameRef.stages.push(s), this.gameRef.stageMap.set(s.stageRef.uuid, s)) : this.options.push(s);
|
|
114
90
|
continue;
|
|
115
91
|
}
|
|
116
|
-
if (typeof
|
|
92
|
+
if (typeof s == "function") {
|
|
117
93
|
try {
|
|
118
|
-
const
|
|
119
|
-
|
|
94
|
+
const e = s();
|
|
95
|
+
e instanceof o ? this.gameRef ? (this.gameRef.stages.push(e), this.gameRef.stageMap.set(e.stageRef.uuid, e)) : this.options.push(e) : e && typeof e.then == "function" && e.then((a) => {
|
|
120
96
|
a instanceof o && (this.gameRef ? (this.gameRef.stages.push(a), this.gameRef.stageMap.set(a.stageRef.uuid, a)) : this.options.push(a));
|
|
121
97
|
}).catch((a) => console.error("Failed to add async stage", a));
|
|
122
|
-
} catch (
|
|
123
|
-
console.error("Error executing stage factory",
|
|
98
|
+
} catch (e) {
|
|
99
|
+
console.error("Error executing stage factory", e);
|
|
124
100
|
}
|
|
125
101
|
continue;
|
|
126
102
|
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
}).catch((
|
|
103
|
+
s && typeof s.then == "function" && s.then((e) => {
|
|
104
|
+
e instanceof o && (this.gameRef ? (this.gameRef.stages.push(e), this.gameRef.stageMap.set(e.stageRef.uuid, e)) : this.options.push(e));
|
|
105
|
+
}).catch((e) => console.error("Failed to add async stage", e));
|
|
130
106
|
}
|
|
131
107
|
return this;
|
|
132
108
|
}
|
|
133
|
-
getGlobal(
|
|
134
|
-
return this.gameRef ? this.gameRef.getGlobal(
|
|
109
|
+
getGlobal(t) {
|
|
110
|
+
return this.gameRef ? this.gameRef.getGlobal(t) : g(t);
|
|
135
111
|
}
|
|
136
|
-
setGlobal(
|
|
112
|
+
setGlobal(t, s) {
|
|
137
113
|
if (this.gameRef) {
|
|
138
|
-
this.gameRef.setGlobal(
|
|
114
|
+
this.gameRef.setGlobal(t, s);
|
|
139
115
|
return;
|
|
140
116
|
}
|
|
141
|
-
|
|
117
|
+
f(t, s);
|
|
142
118
|
}
|
|
143
|
-
onGlobalChange(
|
|
119
|
+
onGlobalChange(t, s) {
|
|
144
120
|
if (this.gameRef) {
|
|
145
|
-
this.gameRef.onGlobalChange(
|
|
121
|
+
this.gameRef.onGlobalChange(t, s);
|
|
146
122
|
return;
|
|
147
123
|
}
|
|
148
|
-
this.pendingGlobalChangeHandlers.push({ key:
|
|
124
|
+
this.pendingGlobalChangeHandlers.push({ key: t, callback: s });
|
|
149
125
|
}
|
|
150
126
|
}
|
|
151
|
-
function
|
|
152
|
-
return new
|
|
127
|
+
function S(...i) {
|
|
128
|
+
return new u(i);
|
|
153
129
|
}
|
|
154
130
|
export {
|
|
155
|
-
|
|
156
|
-
|
|
131
|
+
u as Game,
|
|
132
|
+
S as game
|
|
157
133
|
};
|
|
@@ -5,6 +5,10 @@ import { Timer } from '../core/three-addons/Timer';
|
|
|
5
5
|
import { ZylemCamera } from '~/lib/camera/zylem-camera';
|
|
6
6
|
import { Stage } from '../stage/stage';
|
|
7
7
|
import { BasicTypes, GlobalVariablesType, ZylemGameConfig } from './game-interfaces';
|
|
8
|
+
import { GameConfig } from './game-config';
|
|
9
|
+
import { AspectRatioDelegate } from '../device/aspect-ratio';
|
|
10
|
+
import { GameCanvas } from './game-canvas';
|
|
11
|
+
type ZylemGameOptions<TGlobals extends Record<string, BasicTypes> = GlobalVariablesType> = ZylemGameConfig<Stage, ZylemGame<TGlobals>, TGlobals> & Partial<GameConfig>;
|
|
8
12
|
export declare class ZylemGame<TGlobals extends Record<string, BasicTypes> = GlobalVariablesType> {
|
|
9
13
|
id: string;
|
|
10
14
|
initialGlobals: TGlobals;
|
|
@@ -26,10 +30,17 @@ export declare class ZylemGame<TGlobals extends Record<string, BasicTypes> = Glo
|
|
|
26
30
|
dom: HTMLElement;
|
|
27
31
|
} | null;
|
|
28
32
|
defaultCamera: ZylemCamera | null;
|
|
33
|
+
container: HTMLElement | null;
|
|
34
|
+
canvas: HTMLCanvasElement | null;
|
|
35
|
+
aspectRatioDelegate: AspectRatioDelegate | null;
|
|
36
|
+
resolvedConfig: GameConfig | null;
|
|
37
|
+
gameCanvas: GameCanvas | null;
|
|
29
38
|
static FRAME_LIMIT: number;
|
|
30
39
|
static FRAME_DURATION: number;
|
|
31
40
|
static MAX_DELTA_SECONDS: number;
|
|
32
|
-
constructor(options:
|
|
41
|
+
constructor(options: ZylemGameOptions<TGlobals>, wrapperRef: Game<TGlobals>);
|
|
42
|
+
loadGameCanvas(config: GameConfig): void;
|
|
43
|
+
loadDebugOptions(options: ZylemGameOptions<TGlobals>): void;
|
|
33
44
|
loadStage(stage: Stage): Promise<void>;
|
|
34
45
|
unloadCurrentStage(): void;
|
|
35
46
|
setGlobals(options: ZylemGameConfig<Stage, ZylemGame<TGlobals>, TGlobals>): void;
|