@zylem/game-lib 0.3.15 → 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.
Files changed (119) hide show
  1. package/README.md +38 -13
  2. package/dist/.vite/manifest.json +665 -2
  3. package/dist/actions.d.ts +4 -0
  4. package/dist/actions.js +10 -0
  5. package/dist/behaviors.d.ts +3 -0
  6. package/dist/behaviors.js +8 -0
  7. package/dist/camera.d.ts +3 -0
  8. package/dist/camera.js +6 -0
  9. package/dist/core.d.ts +5 -0
  10. package/dist/core.js +11 -0
  11. package/dist/entities.d.ts +8 -0
  12. package/dist/entities.js +19 -0
  13. package/dist/lib/actions/behaviors/actions.js +35 -0
  14. package/dist/lib/actions/behaviors/boundaries/boundary.d.ts +1 -1
  15. package/dist/lib/actions/behaviors/boundaries/boundary.js +40 -0
  16. package/dist/lib/actions/behaviors/ricochet/ricochet-2d-collision.js +100 -0
  17. package/dist/lib/actions/behaviors/ricochet/ricochet-2d-in-bounds.js +37 -0
  18. package/dist/lib/actions/behaviors/ricochet/ricochet.d.ts +2 -0
  19. package/dist/lib/actions/behaviors/ricochet/ricochet.js +6 -0
  20. package/dist/lib/actions/capabilities/moveable.js +108 -0
  21. package/dist/lib/actions/capabilities/rotatable.js +82 -0
  22. package/dist/lib/actions/capabilities/transformable.js +9 -0
  23. package/dist/lib/actions/global-change.js +40 -0
  24. package/dist/lib/camera/camera.js +19 -0
  25. package/dist/lib/camera/fixed-2d.js +30 -0
  26. package/dist/lib/camera/perspective.js +10 -0
  27. package/dist/lib/camera/third-person.js +42 -0
  28. package/dist/lib/camera/zylem-camera.js +145 -0
  29. package/dist/lib/collision/collision-builder.js +46 -0
  30. package/dist/lib/collision/collision-delegate.js +6 -0
  31. package/dist/lib/collision/utils.js +24 -0
  32. package/dist/lib/collision/world.js +77 -0
  33. package/dist/lib/core/base-node-life-cycle.d.ts +14 -0
  34. package/dist/lib/core/base-node.d.ts +6 -2
  35. package/dist/lib/core/base-node.js +62 -0
  36. package/dist/lib/core/entity-asset-loader.js +57 -0
  37. package/dist/lib/core/lazy-loader.d.ts +2 -2
  38. package/dist/lib/core/lifecycle-base.js +20 -0
  39. package/dist/lib/core/preset-shader.js +30 -0
  40. package/dist/lib/core/three-addons/Timer.js +103 -0
  41. package/dist/lib/core/utility/nodes.d.ts +11 -0
  42. package/dist/lib/core/utility/nodes.js +27 -0
  43. package/dist/lib/core/utility/strings.d.ts +2 -0
  44. package/dist/lib/core/utility/strings.js +14 -0
  45. package/dist/lib/core/{utility.d.ts → utility/vector.d.ts} +0 -2
  46. package/dist/lib/core/utility/vector.js +8 -0
  47. package/dist/lib/core/vessel.d.ts +3 -1
  48. package/dist/lib/core/vessel.js +27 -0
  49. package/dist/lib/debug/console/console-state.js +11 -0
  50. package/dist/lib/debug/debug-state.js +40 -0
  51. package/dist/lib/device/aspect-ratio.d.ts +37 -0
  52. package/dist/lib/device/aspect-ratio.js +44 -0
  53. package/dist/lib/entities/actor.js +125 -0
  54. package/dist/lib/entities/box.js +68 -0
  55. package/dist/lib/entities/builder.js +79 -0
  56. package/dist/lib/entities/create.js +31 -0
  57. package/dist/lib/entities/delegates/animation.js +58 -0
  58. package/dist/lib/entities/delegates/debug.js +72 -0
  59. package/dist/lib/entities/delegates/loader.js +19 -0
  60. package/dist/lib/entities/destroy.js +15 -0
  61. package/dist/lib/entities/entity.d.ts +3 -1
  62. package/dist/lib/entities/entity.js +120 -0
  63. package/dist/lib/entities/plane.js +81 -0
  64. package/dist/lib/entities/rect.js +160 -0
  65. package/dist/lib/entities/sphere.js +68 -0
  66. package/dist/lib/entities/sprite.js +118 -0
  67. package/dist/lib/entities/text.js +111 -0
  68. package/dist/lib/entities/zone.js +103 -0
  69. package/dist/lib/game/game-blueprint.d.ts +44 -0
  70. package/dist/lib/game/game-canvas.d.ts +34 -0
  71. package/dist/lib/game/game-canvas.js +57 -0
  72. package/dist/lib/game/game-config.d.ts +47 -0
  73. package/dist/lib/game/game-config.js +53 -0
  74. package/dist/lib/game/game-default.d.ts +18 -0
  75. package/dist/lib/game/game-default.js +24 -0
  76. package/dist/lib/game/game-state.js +17 -0
  77. package/dist/lib/game/game.d.ts +2 -5
  78. package/dist/lib/game/game.js +133 -0
  79. package/dist/lib/game/zylem-game.d.ts +12 -1
  80. package/dist/lib/game/zylem-game.js +133 -0
  81. package/dist/lib/graphics/geometries/XZPlaneGeometry.js +34 -0
  82. package/dist/lib/graphics/material.d.ts +1 -1
  83. package/dist/lib/graphics/material.js +64 -0
  84. package/dist/lib/graphics/mesh.js +14 -0
  85. package/dist/lib/graphics/render-pass.js +56 -0
  86. package/dist/lib/graphics/shaders/fragment/debug.glsl.js +23 -0
  87. package/dist/lib/graphics/shaders/fragment/fire.glsl.js +52 -0
  88. package/dist/lib/graphics/shaders/fragment/standard.glsl.js +11 -0
  89. package/dist/lib/graphics/shaders/fragment/stars.glsl.js +44 -0
  90. package/dist/lib/graphics/shaders/vertex/debug.glsl.js +15 -0
  91. package/dist/lib/graphics/shaders/vertex/object-shader.glsl.js +11 -0
  92. package/dist/lib/graphics/shaders/vertex/standard.glsl.js +9 -0
  93. package/dist/lib/graphics/zylem-scene.d.ts +7 -7
  94. package/dist/lib/graphics/zylem-scene.js +75 -0
  95. package/dist/lib/input/gamepad-provider.js +58 -0
  96. package/dist/lib/input/input-manager.js +70 -0
  97. package/dist/lib/input/keyboard-provider.js +120 -0
  98. package/dist/lib/stage/debug-entity-cursor.js +53 -0
  99. package/dist/lib/stage/entity-spawner.js +27 -0
  100. package/dist/lib/stage/stage-blueprint.d.ts +44 -0
  101. package/dist/lib/stage/stage-blueprint.js +56 -0
  102. package/dist/lib/stage/stage-debug-delegate.js +100 -0
  103. package/dist/lib/stage/stage-default.d.ts +9 -0
  104. package/dist/lib/stage/stage-default.js +42 -0
  105. package/dist/lib/stage/stage-state.js +36 -0
  106. package/dist/lib/stage/stage.d.ts +2 -2
  107. package/dist/lib/stage/stage.js +60 -0
  108. package/dist/lib/stage/zylem-stage.d.ts +4 -3
  109. package/dist/lib/stage/zylem-stage.js +268 -0
  110. package/dist/lib/systems/transformable.system.js +43 -0
  111. package/dist/main.d.ts +6 -15
  112. package/dist/main.js +56 -45933
  113. package/dist/stage.d.ts +6 -0
  114. package/dist/stage.js +22 -0
  115. package/dist/tests/stage/stage-blueprint.spec.d.ts +1 -0
  116. package/package.json +35 -1
  117. package/dist/lib/actions/behaviors/index.d.ts +0 -1
  118. package/dist/lib/core/game-canvas.d.ts +0 -4
  119. package/dist/main.cjs +0 -4023
@@ -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: ZylemGameConfig<Stage, ZylemGame<TGlobals>, TGlobals>, wrapperRef: Game<TGlobals>);
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;
@@ -0,0 +1,133 @@
1
+ import { state as o, getGlobalState as n, setGlobalState as l } from "./game-state.js";
2
+ import { setDebugFlag as u, isPaused as m } from "../debug/debug-state.js";
3
+ import { InputManager as c } from "../input/input-manager.js";
4
+ import { Timer as h } from "../core/three-addons/Timer.js";
5
+ import { resolveGameConfig as d } from "./game-config.js";
6
+ import { GameCanvas as g } from "./game-canvas.js";
7
+ import { subscribe as f } from "valtio/vanilla";
8
+ import p from "stats.js";
9
+ class r {
10
+ id;
11
+ initialGlobals = {};
12
+ customSetup = null;
13
+ customUpdate = null;
14
+ customDestroy = null;
15
+ stages = [];
16
+ stageMap = /* @__PURE__ */ new Map();
17
+ currentStageId = "";
18
+ previousTimeStamp = 0;
19
+ totalTime = 0;
20
+ timer;
21
+ inputManager;
22
+ wrapperRef;
23
+ statsRef = null;
24
+ defaultCamera = null;
25
+ container = null;
26
+ canvas = null;
27
+ aspectRatioDelegate = null;
28
+ resolvedConfig = null;
29
+ gameCanvas = null;
30
+ static FRAME_LIMIT = 120;
31
+ static FRAME_DURATION = 1e3 / r.FRAME_LIMIT;
32
+ static MAX_DELTA_SECONDS = 1 / 30;
33
+ constructor(t, e) {
34
+ this.wrapperRef = e, this.inputManager = new c(t.input), this.timer = new h(), this.timer.connect(document);
35
+ const a = d(t);
36
+ this.id = a.id, this.stages = a.stages || [], this.container = a.container, this.canvas = a.canvas ?? null, this.resolvedConfig = a, this.loadGameCanvas(a), this.loadDebugOptions(t), this.setGlobals(t);
37
+ }
38
+ loadGameCanvas(t) {
39
+ this.gameCanvas = new g({
40
+ id: t.id,
41
+ container: t.container,
42
+ containerId: t.containerId,
43
+ canvas: this.canvas ?? void 0,
44
+ bodyBackground: t.bodyBackground,
45
+ fullscreen: t.fullscreen,
46
+ aspectRatio: t.aspectRatio
47
+ }), this.gameCanvas.applyBodyBackground(), this.gameCanvas.mountCanvas(), this.gameCanvas.centerIfFullscreen();
48
+ }
49
+ loadDebugOptions(t) {
50
+ u(!!t.debug), t.debug && (this.statsRef = new p(), this.statsRef.showPanel(0), this.statsRef.dom.style.position = "absolute", this.statsRef.dom.style.bottom = "0", this.statsRef.dom.style.right = "0", this.statsRef.dom.style.top = "auto", this.statsRef.dom.style.left = "auto", document.body.appendChild(this.statsRef.dom));
51
+ }
52
+ async loadStage(t) {
53
+ this.unloadCurrentStage();
54
+ const e = t.options[0];
55
+ if (await t.load(this.id, e?.camera), this.stageMap.set(t.stageRef.uuid, t), this.currentStageId = t.stageRef.uuid, this.defaultCamera = t.stageRef.cameraRef, this.container && this.defaultCamera) {
56
+ const a = this.defaultCamera.getDomElement();
57
+ this.gameCanvas?.mountRenderer(a, (s, i) => this.defaultCamera?.resize(s, i));
58
+ }
59
+ }
60
+ unloadCurrentStage() {
61
+ if (!this.currentStageId)
62
+ return;
63
+ const t = this.getStage(this.currentStageId);
64
+ if (t) {
65
+ if (t?.stageRef)
66
+ try {
67
+ t.stageRef.nodeDestroy({
68
+ me: t.stageRef,
69
+ globals: o.globals
70
+ });
71
+ } catch (e) {
72
+ console.error("Failed to destroy previous stage", e);
73
+ }
74
+ this.stageMap.delete(this.currentStageId);
75
+ }
76
+ }
77
+ setGlobals(t) {
78
+ this.initialGlobals = { ...t.globals };
79
+ for (const e in this.initialGlobals) {
80
+ const a = this.initialGlobals[e];
81
+ a === void 0 && console.error(`global ${e} is undefined`), this.setGlobal(e, a);
82
+ }
83
+ }
84
+ params() {
85
+ const t = this.currentStage(), e = this.timer.getDelta(), a = this.inputManager.getInputs(e), s = t?.stageRef?.cameraRef || this.defaultCamera;
86
+ return {
87
+ delta: e,
88
+ inputs: a,
89
+ globals: o.globals,
90
+ me: this,
91
+ camera: s
92
+ };
93
+ }
94
+ start() {
95
+ const t = this.currentStage(), e = this.params();
96
+ t.start({ ...e, me: t.stageRef }), this.customSetup && this.customSetup(e), this.loop(0);
97
+ }
98
+ loop(t) {
99
+ if (this.statsRef && this.statsRef.begin(), !m()) {
100
+ this.timer.update(t);
101
+ const e = this.currentStage(), a = this.params(), s = Math.min(a.delta, r.MAX_DELTA_SECONDS), i = { ...a, delta: s };
102
+ this.customUpdate && this.customUpdate(i), e && e.stageRef.nodeUpdate({ ...i, me: e.stageRef }), this.totalTime += i.delta, o.time = this.totalTime, this.previousTimeStamp = t;
103
+ }
104
+ this.statsRef && this.statsRef.end(), this.outOfLoop(), requestAnimationFrame(this.loop.bind(this));
105
+ }
106
+ outOfLoop() {
107
+ const t = this.currentStage();
108
+ t && t.stageRef.outOfLoop();
109
+ }
110
+ getStage(t) {
111
+ return this.stageMap.get(t);
112
+ }
113
+ currentStage() {
114
+ return this.getStage(this.currentStageId);
115
+ }
116
+ getGlobal(t) {
117
+ return n(t);
118
+ }
119
+ setGlobal(t, e) {
120
+ l(t, e);
121
+ }
122
+ onGlobalChange(t, e) {
123
+ let a = n(t);
124
+ f(o, () => {
125
+ const s = n(t);
126
+ s !== a && (a = s, e(s));
127
+ });
128
+ }
129
+ }
130
+ export {
131
+ r as ZylemGame,
132
+ r as default
133
+ };
@@ -0,0 +1,34 @@
1
+ import { BufferGeometry as _, Float32BufferAttribute as f } from "three";
2
+ class b extends _ {
3
+ constructor(t = 1, h = 1, l = 1, a = 1) {
4
+ super(), this.type = "XZPlaneGeometry", this.parameters = {
5
+ width: t,
6
+ height: h,
7
+ widthSegments: l,
8
+ heightSegments: a
9
+ };
10
+ const d = t / 2, x = h / 2, n = Math.floor(l), i = Math.floor(a), r = n + 1, w = i + 1, A = t / n, v = h / i, u = [], m = [], g = [], p = [];
11
+ for (let s = 0; s < w; s++) {
12
+ const e = s * v - x;
13
+ for (let o = 0; o < r; o++) {
14
+ const c = o * A - d;
15
+ m.push(c, 0, e), g.push(0, 1, 0), p.push(o / n), p.push(1 - s / i);
16
+ }
17
+ }
18
+ for (let s = 0; s < i; s++)
19
+ for (let e = 0; e < n; e++) {
20
+ const o = e + r * s, c = e + r * (s + 1), X = e + 1 + r * (s + 1), y = e + 1 + r * s;
21
+ u.push(o, c, y), u.push(c, X, y);
22
+ }
23
+ this.setIndex(u), this.setAttribute("position", new f(m, 3)), this.setAttribute("normal", new f(g, 3)), this.setAttribute("uv", new f(p, 2));
24
+ }
25
+ copy(t) {
26
+ return super.copy(t), this.parameters = Object.assign({}, t.parameters), this;
27
+ }
28
+ static fromJSON(t) {
29
+ return new b(t.width, t.height, t.widthSegments, t.heightSegments);
30
+ }
31
+ }
32
+ export {
33
+ b as XZPlaneGeometry
34
+ };
@@ -1,5 +1,5 @@
1
1
  import { Color, Material, Vector2 } from 'three';
2
- import { shortHash } from '../core/utility';
2
+ import { shortHash } from '../core/utility/strings';
3
3
  import { ZylemShaderType } from '../core/preset-shader';
4
4
  export interface MaterialOptions {
5
5
  path?: string;
@@ -0,0 +1,64 @@
1
+ import { Color as n, Vector2 as m, TextureLoader as c, RepeatWrapping as o, MeshPhongMaterial as p, MeshStandardMaterial as u, ShaderMaterial as f, Vector3 as d } from "three";
2
+ import { shortHash as M, sortedStringify as w } from "../core/utility/strings.js";
3
+ import h from "../core/preset-shader.js";
4
+ class i {
5
+ static batchMaterialMap = /* @__PURE__ */ new Map();
6
+ materials = [];
7
+ batchMaterial(t, a) {
8
+ const r = M(w(t)), e = i.batchMaterialMap.get(r);
9
+ if (e) {
10
+ const s = e.geometryMap.get(a);
11
+ s ? e.geometryMap.set(a, s + 1) : e.geometryMap.set(a, 1);
12
+ } else
13
+ i.batchMaterialMap.set(r, {
14
+ geometryMap: /* @__PURE__ */ new Map([[a, 1]]),
15
+ material: this.materials[0]
16
+ });
17
+ }
18
+ async build(t, a) {
19
+ const { path: r, repeat: e, color: s, shader: l } = t;
20
+ l && this.withShader(l), s && this.withColor(s), await this.setTexture(r ?? null, e), this.materials.length === 0 && this.setColor(new n("#ffffff")), this.batchMaterial(t, a);
21
+ }
22
+ withColor(t) {
23
+ return this.setColor(t), this;
24
+ }
25
+ withShader(t) {
26
+ return this.setShader(t), this;
27
+ }
28
+ async setTexture(t = null, a = new m(1, 1)) {
29
+ if (!t)
30
+ return;
31
+ const e = await new c().loadAsync(t);
32
+ e.repeat = a, e.wrapS = o, e.wrapT = o;
33
+ const s = new p({
34
+ map: e
35
+ });
36
+ this.materials.push(s);
37
+ }
38
+ setColor(t) {
39
+ const a = new u({
40
+ color: t,
41
+ emissiveIntensity: 0.5,
42
+ lightMapIntensity: 0.5,
43
+ fog: !0
44
+ });
45
+ this.materials.push(a);
46
+ }
47
+ setShader(t) {
48
+ const { fragment: a, vertex: r } = h.get(t) ?? h.get("standard"), e = new f({
49
+ uniforms: {
50
+ iResolution: { value: new d(1, 1, 1) },
51
+ iTime: { value: 0 },
52
+ tDiffuse: { value: null },
53
+ tDepth: { value: null },
54
+ tNormal: { value: null }
55
+ },
56
+ vertexShader: r,
57
+ fragmentShader: a
58
+ });
59
+ this.materials.push(e);
60
+ }
61
+ }
62
+ export {
63
+ i as MaterialBuilder
64
+ };
@@ -0,0 +1,14 @@
1
+ import { Mesh as n } from "three";
2
+ class h {
3
+ _build(t, r, s) {
4
+ const { batched: i, material: a } = t;
5
+ i && console.warn("warning: mesh batching is not implemented");
6
+ const e = new n(r, s.at(-1));
7
+ return e.position.set(0, 0, 0), e.castShadow = !0, e.receiveShadow = !0, e;
8
+ }
9
+ _postBuild() {
10
+ }
11
+ }
12
+ export {
13
+ h as MeshBuilder
14
+ };
@@ -0,0 +1,56 @@
1
+ import * as s from "three";
2
+ import { WebGLRenderTarget as i } from "three";
3
+ import n from "./shaders/fragment/standard.glsl.js";
4
+ import l from "./shaders/vertex/standard.glsl.js";
5
+ import { Pass as o, FullScreenQuad as d } from "three/addons/postprocessing/Pass.js";
6
+ class f extends o {
7
+ fsQuad;
8
+ resolution;
9
+ scene;
10
+ camera;
11
+ rgbRenderTarget;
12
+ normalRenderTarget;
13
+ normalMaterial;
14
+ constructor(e, r, a) {
15
+ super(), this.resolution = e, this.fsQuad = new d(this.material()), this.scene = r, this.camera = a, this.rgbRenderTarget = new i(e.x * 4, e.y * 4), this.normalRenderTarget = new i(e.x * 4, e.y * 4), this.normalMaterial = new s.MeshNormalMaterial();
16
+ }
17
+ render(e, r) {
18
+ e.setRenderTarget(this.rgbRenderTarget), e.render(this.scene, this.camera);
19
+ const a = this.scene.overrideMaterial;
20
+ e.setRenderTarget(this.normalRenderTarget), this.scene.overrideMaterial = this.normalMaterial, e.render(this.scene, this.camera), this.scene.overrideMaterial = a;
21
+ const t = this.fsQuad.material.uniforms;
22
+ t.tDiffuse.value = this.rgbRenderTarget.texture, t.tDepth.value = this.rgbRenderTarget.depthTexture, t.tNormal.value = this.normalRenderTarget.texture, t.iTime.value += 0.01, this.renderToScreen ? e.setRenderTarget(null) : e.setRenderTarget(r), this.fsQuad.render(e);
23
+ }
24
+ material() {
25
+ return new s.ShaderMaterial({
26
+ uniforms: {
27
+ iTime: { value: 0 },
28
+ tDiffuse: { value: null },
29
+ tDepth: { value: null },
30
+ tNormal: { value: null },
31
+ resolution: {
32
+ value: new s.Vector4(this.resolution.x, this.resolution.y, 1 / this.resolution.x, 1 / this.resolution.y)
33
+ }
34
+ },
35
+ vertexShader: l,
36
+ fragmentShader: n
37
+ });
38
+ }
39
+ dispose() {
40
+ try {
41
+ this.fsQuad?.dispose?.();
42
+ } catch {
43
+ }
44
+ try {
45
+ this.rgbRenderTarget?.dispose?.(), this.normalRenderTarget?.dispose?.();
46
+ } catch {
47
+ }
48
+ try {
49
+ this.normalMaterial?.dispose?.();
50
+ } catch {
51
+ }
52
+ }
53
+ }
54
+ export {
55
+ f as default
56
+ };
@@ -0,0 +1,23 @@
1
+ var e = `varying vec3 vBarycentric;
2
+ uniform vec3 baseColor;
3
+ uniform vec3 wireframeColor;
4
+ uniform float wireframeThickness;
5
+
6
+ float edgeFactor() {
7
+ vec3 d = fwidth(vBarycentric);
8
+ vec3 a3 = smoothstep(vec3(0.0), d * wireframeThickness, vBarycentric);
9
+ return min(min(a3.x, a3.y), a3.z);
10
+ }
11
+
12
+ void main() {
13
+ float edge = edgeFactor();
14
+
15
+ vec3 wireColor = wireframeColor;
16
+
17
+ vec3 finalColor = mix(wireColor, baseColor, edge);
18
+
19
+ gl_FragColor = vec4(finalColor, 1.0);
20
+ }`;
21
+ export {
22
+ e as default
23
+ };
@@ -0,0 +1,52 @@
1
+ var n = `#include <common>
2
+
3
+ uniform vec3 iResolution;
4
+ uniform float iTime;
5
+ uniform vec2 iOffset;
6
+ varying vec2 vUv;
7
+
8
+ float snoise(vec3 uv, float res)
9
+ {
10
+ const vec3 s = vec3(1e0, 1e2, 1e3);
11
+
12
+ uv *= res;
13
+
14
+ vec3 uv0 = floor(mod(uv, res))*s;
15
+ vec3 uv1 = floor(mod(uv+vec3(1.), res))*s;
16
+
17
+ vec3 f = fract(uv); f = f*f*(3.0-2.0*f);
18
+
19
+ vec4 v = vec4(uv0.x+uv0.y+uv0.z, uv1.x+uv0.y+uv0.z,
20
+ uv0.x+uv1.y+uv0.z, uv1.x+uv1.y+uv0.z);
21
+
22
+ vec4 r = fract(sin(v*1e-1)*1e3);
23
+ float r0 = mix(mix(r.x, r.y, f.x), mix(r.z, r.w, f.x), f.y);
24
+
25
+ r = fract(sin((v + uv1.z - uv0.z)*1e-1)*1e3);
26
+ float r1 = mix(mix(r.x, r.y, f.x), mix(r.z, r.w, f.x), f.y);
27
+
28
+ return mix(r0, r1, f.z)*2.-1.;
29
+ }
30
+
31
+ void mainImage( out vec4 fragColor, in vec2 fragCoord ) {
32
+ vec2 p = -.5 + fragCoord.xy / iResolution.xy;
33
+ p.x *= iResolution.x/iResolution.y;
34
+
35
+ float color = 3.0 - (3.*length(2.*p));
36
+
37
+ vec3 coord = vec3(atan(p.x,p.y)/6.2832+.5, length(p)*.4, .5);
38
+
39
+ for(int i = 1; i <= 7; i++)
40
+ {
41
+ float power = pow(2.0, float(i));
42
+ color += (1.5 / power) * snoise(coord + vec3(0.,-iTime*.05, iTime*.01), power*16.);
43
+ }
44
+ fragColor = vec4( color, pow(max(color,0.),2.)*0.4, pow(max(color,0.),3.)*0.15 , 1.0);
45
+ }
46
+
47
+ void main() {
48
+ mainImage(gl_FragColor, vUv);
49
+ }`;
50
+ export {
51
+ n as default
52
+ };
@@ -0,0 +1,11 @@
1
+ var e = `uniform sampler2D tDiffuse;
2
+ varying vec2 vUv;
3
+
4
+ void main() {
5
+ vec4 texel = texture2D( tDiffuse, vUv );
6
+
7
+ gl_FragColor = texel;
8
+ }`;
9
+ export {
10
+ e as default
11
+ };
@@ -0,0 +1,44 @@
1
+ var n = `#include <common>
2
+
3
+ uniform vec3 iResolution;
4
+ uniform float iTime;
5
+ varying vec2 vUv;
6
+
7
+ vec3 palette( float t ) {
8
+ vec3 a = vec3(0.5, 0.5, 0.5);
9
+ vec3 b = vec3(0.5, 0.5, 0.5);
10
+ vec3 c = vec3(1.0, 1.0, 1.0);
11
+ vec3 d = vec3(0.263,0.416,0.557);
12
+
13
+ return a + b*cos( 6.28318*(c*t+d) );
14
+ }
15
+
16
+ void mainImage( out vec4 fragColor, in vec2 fragCoord ) {
17
+ vec2 uv = (fragCoord * 2.0 - iResolution.xy) / iResolution.y;
18
+ vec2 uv0 = uv;
19
+ vec3 finalColor = vec3(0.0);
20
+
21
+ for (float i = 0.0; i < 4.0; i++) {
22
+ uv = fract(uv * 1.5) - 0.5;
23
+
24
+ float d = length(uv) * exp(-length(uv0));
25
+
26
+ vec3 col = palette(length(uv0) + i*.4 + iTime*.4);
27
+
28
+ d = sin(d*5. + iTime)/5.;
29
+ d = abs(d);
30
+
31
+ d = pow(0.01 / d, 1.2);
32
+
33
+ finalColor += col * d;
34
+ }
35
+
36
+ fragColor = vec4(finalColor, 1.0);
37
+ }
38
+
39
+ void main() {
40
+ mainImage(gl_FragColor, vUv);
41
+ }`;
42
+ export {
43
+ n as default
44
+ };
@@ -0,0 +1,15 @@
1
+ var e = `varying vec3 vBarycentric;
2
+
3
+ void main() {
4
+ vec3 barycentric = vec3(0.0);
5
+ int index = gl_VertexID % 3;
6
+ if (index == 0) barycentric = vec3(1.0, 0.0, 0.0);
7
+ else if (index == 1) barycentric = vec3(0.0, 1.0, 0.0);
8
+ else barycentric = vec3(0.0, 0.0, 1.0);
9
+ vBarycentric = barycentric;
10
+
11
+ gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
12
+ }`;
13
+ export {
14
+ e as default
15
+ };
@@ -0,0 +1,11 @@
1
+ var i = `uniform vec2 uvScale;
2
+ varying vec2 vUv;
3
+
4
+ void main() {
5
+ vUv = uv;
6
+ vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
7
+ gl_Position = projectionMatrix * mvPosition;
8
+ }`;
9
+ export {
10
+ i as default
11
+ };
@@ -0,0 +1,9 @@
1
+ var i = `varying vec2 vUv;
2
+
3
+ void main() {
4
+ vUv = uv;
5
+ gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
6
+ }`;
7
+ export {
8
+ i as default
9
+ };
@@ -1,10 +1,10 @@
1
1
  import { Scene, Color, Object3D, Vector3 } from 'three';
2
- import { Entity } from '../interfaces/entity';
2
+ import { Entity, LifecycleFunction } from '../interfaces/entity';
3
3
  import { GameEntity } from '../entities/entity';
4
4
  import { ZylemCamera } from '../camera/zylem-camera';
5
5
  import { SetupFunction } from '../core/base-node-life-cycle';
6
6
  interface SceneState {
7
- backgroundColor: Color;
7
+ backgroundColor: Color | string;
8
8
  backgroundImage: string | null;
9
9
  }
10
10
  export declare class ZylemScene implements Entity<ZylemScene> {
@@ -13,14 +13,14 @@ export declare class ZylemScene implements Entity<ZylemScene> {
13
13
  scene: Scene;
14
14
  zylemCamera: ZylemCamera;
15
15
  containerElement: HTMLElement | null;
16
+ update: LifecycleFunction<ZylemScene>;
17
+ _collision?: ((entity: any, other: any, globals?: any) => void) | undefined;
18
+ _destroy?: ((globals?: any) => void) | undefined;
19
+ name?: string | undefined;
20
+ tag?: Set<string> | undefined;
16
21
  constructor(id: string, camera: ZylemCamera, state: SceneState);
17
- /**
18
- * Setup the container element and append camera's renderer
19
- */
20
- private setupContainer;
21
22
  setup(): void;
22
23
  destroy(): void;
23
- update({ delta }: Partial<any>): void;
24
24
  /**
25
25
  * Setup camera with the scene
26
26
  */
@@ -0,0 +1,75 @@
1
+ import { Scene as d, Color as i, TextureLoader as n, AmbientLight as m, DirectionalLight as c, Vector3 as h, GridHelper as l } from "three";
2
+ import { debugState as g } from "../debug/debug-state.js";
3
+ import { getGlobalState as p } from "../game/game-state.js";
4
+ class b {
5
+ type = "Scene";
6
+ _setup;
7
+ scene;
8
+ zylemCamera;
9
+ containerElement = null;
10
+ update = () => {
11
+ };
12
+ _collision;
13
+ _destroy;
14
+ name;
15
+ tag;
16
+ constructor(e, s, a) {
17
+ const o = new d(), t = a.backgroundColor instanceof i ? a.backgroundColor : new i(a.backgroundColor);
18
+ if (o.background = t, a.backgroundImage) {
19
+ const r = new n().load(a.backgroundImage);
20
+ o.background = r;
21
+ }
22
+ this.scene = o, this.zylemCamera = s, this.setupLighting(o), this.setupCamera(o, s), g.on && this.debugScene();
23
+ }
24
+ setup() {
25
+ this._setup && this._setup({ me: this, camera: this.zylemCamera, globals: p() });
26
+ }
27
+ destroy() {
28
+ this.zylemCamera && this.zylemCamera.destroy && this.zylemCamera.destroy(), this.scene && this.scene.traverse((e) => {
29
+ e.geometry && e.geometry.dispose?.(), e.material && (Array.isArray(e.material) ? e.material.forEach((s) => s.dispose?.()) : e.material.dispose?.());
30
+ });
31
+ }
32
+ /**
33
+ * Setup camera with the scene
34
+ */
35
+ setupCamera(e, s) {
36
+ e.add(s.cameraRig), s.setup(e);
37
+ }
38
+ /**
39
+ * Setup scene lighting
40
+ */
41
+ setupLighting(e) {
42
+ const s = new m(16777215, 2);
43
+ e.add(s);
44
+ const a = new c(16777215, 2);
45
+ a.name = "Light", a.position.set(0, 100, 0), a.castShadow = !0, a.shadow.camera.near = 0.1, a.shadow.camera.far = 2e3, a.shadow.camera.left = -100, a.shadow.camera.right = 100, a.shadow.camera.top = 100, a.shadow.camera.bottom = -100, a.shadow.mapSize.width = 2048, a.shadow.mapSize.height = 2048, e.add(a);
46
+ }
47
+ /**
48
+ * Update renderer size - delegates to camera
49
+ */
50
+ updateRenderer(e, s) {
51
+ this.zylemCamera.resize(e, s);
52
+ }
53
+ /**
54
+ * Add object to scene
55
+ */
56
+ add(e, s = new h(0, 0, 0)) {
57
+ e.position.set(s.x, s.y, s.z), this.scene.add(e);
58
+ }
59
+ /**
60
+ * Add game entity to scene
61
+ */
62
+ addEntity(e) {
63
+ e.group ? this.add(e.group, e.options.position) : e.mesh && this.add(e.mesh, e.options.position);
64
+ }
65
+ /**
66
+ * Add debug helpers to scene
67
+ */
68
+ debugScene() {
69
+ const a = new l(1e3, 100);
70
+ this.scene.add(a);
71
+ }
72
+ }
73
+ export {
74
+ b as ZylemScene
75
+ };
@@ -0,0 +1,58 @@
1
+ class h {
2
+ gamepadIndex;
3
+ connected = !1;
4
+ buttonStates = {};
5
+ constructor(t) {
6
+ this.gamepadIndex = t, window.addEventListener("gamepadconnected", (e) => {
7
+ e.gamepad.index === this.gamepadIndex && (this.connected = !0);
8
+ }), window.addEventListener("gamepaddisconnected", (e) => {
9
+ e.gamepad.index === this.gamepadIndex && (this.connected = !1);
10
+ });
11
+ }
12
+ handleButtonState(t, e, a) {
13
+ const s = e.buttons[t].pressed;
14
+ let n = this.buttonStates[t];
15
+ return n || (n = { pressed: !1, released: !1, held: 0 }, this.buttonStates[t] = n), s ? (n.held === 0 ? n.pressed = !0 : n.pressed = !1, n.held += a, n.released = !1) : (n.held > 0 ? (n.released = !0, n.held = 0) : n.released = !1, n.pressed = !1), n;
16
+ }
17
+ handleAnalogState(t, e, a) {
18
+ return { value: e.axes[t], held: a };
19
+ }
20
+ getInput(t) {
21
+ const e = navigator.getGamepads()[this.gamepadIndex];
22
+ return e ? {
23
+ buttons: {
24
+ A: this.handleButtonState(0, e, t),
25
+ B: this.handleButtonState(1, e, t),
26
+ X: this.handleButtonState(2, e, t),
27
+ Y: this.handleButtonState(3, e, t),
28
+ Start: this.handleButtonState(9, e, t),
29
+ Select: this.handleButtonState(8, e, t),
30
+ L: this.handleButtonState(4, e, t),
31
+ R: this.handleButtonState(5, e, t)
32
+ },
33
+ directions: {
34
+ Up: this.handleButtonState(12, e, t),
35
+ Down: this.handleButtonState(13, e, t),
36
+ Left: this.handleButtonState(14, e, t),
37
+ Right: this.handleButtonState(15, e, t)
38
+ },
39
+ axes: {
40
+ Horizontal: this.handleAnalogState(0, e, t),
41
+ Vertical: this.handleAnalogState(1, e, t)
42
+ },
43
+ shoulders: {
44
+ LTrigger: this.handleButtonState(6, e, t),
45
+ RTrigger: this.handleButtonState(7, e, t)
46
+ }
47
+ } : {};
48
+ }
49
+ getName() {
50
+ return `gamepad-${this.gamepadIndex + 1}`;
51
+ }
52
+ isConnected() {
53
+ return this.connected;
54
+ }
55
+ }
56
+ export {
57
+ h as GamepadProvider
58
+ };