@zylem/game-lib 0.3.14 → 0.3.16
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 +17 -14
- package/dist/.vite/manifest.json +595 -2
- package/dist/actions.d.ts +4 -0
- package/dist/actions.js +10 -0
- package/dist/behaviors.d.ts +3 -0
- package/dist/behaviors.js +8 -0
- package/dist/camera.d.ts +3 -0
- package/dist/camera.js +6 -0
- package/dist/core.d.ts +5 -0
- package/dist/core.js +11 -0
- package/dist/entities.d.ts +8 -0
- package/dist/entities.js +19 -0
- package/dist/lib/actions/behaviors/actions.js +35 -0
- package/dist/lib/actions/behaviors/boundaries/boundary.d.ts +1 -1
- package/dist/lib/actions/behaviors/boundaries/boundary.js +40 -0
- package/dist/lib/actions/behaviors/ricochet/ricochet-2d-collision.js +100 -0
- package/dist/lib/actions/behaviors/ricochet/ricochet-2d-in-bounds.js +37 -0
- package/dist/lib/actions/behaviors/ricochet/ricochet.d.ts +2 -0
- package/dist/lib/actions/behaviors/ricochet/ricochet.js +6 -0
- package/dist/lib/actions/capabilities/moveable.js +108 -0
- package/dist/lib/actions/capabilities/rotatable.js +82 -0
- package/dist/lib/actions/capabilities/transformable.js +9 -0
- package/dist/lib/actions/global-change.d.ts +10 -0
- package/dist/lib/actions/global-change.js +40 -0
- package/dist/lib/camera/camera.js +19 -0
- package/dist/lib/camera/fixed-2d.js +30 -0
- package/dist/lib/camera/perspective.js +10 -0
- package/dist/lib/camera/third-person.js +42 -0
- package/dist/lib/camera/zylem-camera.js +145 -0
- package/dist/lib/collision/collision-builder.d.ts +0 -2
- package/dist/lib/collision/collision-builder.js +46 -0
- package/dist/lib/collision/collision-delegate.js +6 -0
- package/dist/lib/collision/collision-group.d.ts +0 -17
- package/dist/lib/collision/utils.js +24 -0
- package/dist/lib/collision/world.js +77 -0
- package/dist/lib/core/base-node.js +58 -0
- package/dist/lib/core/entity-asset-loader.js +57 -0
- package/dist/lib/core/lazy-loader.d.ts +0 -2
- package/dist/lib/core/lifecycle-base.js +20 -0
- package/dist/lib/core/preset-shader.d.ts +2 -1
- package/dist/lib/core/preset-shader.js +30 -0
- package/dist/lib/core/three-addons/Timer.js +103 -0
- package/dist/lib/core/utility.js +20 -0
- package/dist/lib/core/vessel.js +23 -0
- package/dist/lib/debug/console/console-state.js +11 -0
- package/dist/lib/debug/debug-state.js +40 -0
- package/dist/lib/entities/actor.js +125 -0
- package/dist/lib/entities/box.js +68 -0
- package/dist/lib/entities/builder.js +79 -0
- package/dist/lib/entities/create.js +31 -0
- package/dist/lib/entities/delegates/animation.js +58 -0
- package/dist/lib/entities/delegates/debug.js +72 -0
- package/dist/lib/entities/delegates/loader.js +19 -0
- package/dist/lib/entities/destroy.js +15 -0
- package/dist/lib/entities/entity.d.ts +0 -2
- package/dist/lib/entities/entity.js +116 -0
- package/dist/lib/entities/plane.js +81 -0
- package/dist/lib/entities/rect.js +160 -0
- package/dist/lib/entities/sphere.js +68 -0
- package/dist/lib/entities/sprite.js +118 -0
- package/dist/lib/entities/text.js +111 -0
- package/dist/lib/entities/zone.js +103 -0
- package/dist/lib/game/game-state.js +17 -0
- package/dist/lib/game/game.js +157 -0
- package/dist/lib/game/zylem-game.d.ts +2 -0
- package/dist/lib/game/zylem-game.js +107 -0
- package/dist/lib/graphics/geometries/XZPlaneGeometry.js +34 -0
- package/dist/lib/graphics/material.js +64 -0
- package/dist/lib/graphics/mesh.js +14 -0
- package/dist/lib/graphics/render-pass.js +56 -0
- package/dist/lib/graphics/shaders/fragment/debug.glsl.js +23 -0
- package/dist/lib/graphics/shaders/fragment/fire.glsl.js +52 -0
- package/dist/lib/graphics/shaders/fragment/standard.glsl.js +11 -0
- package/dist/lib/graphics/shaders/fragment/stars.glsl.js +44 -0
- package/dist/lib/graphics/shaders/vertex/debug.glsl.js +15 -0
- package/dist/lib/graphics/shaders/vertex/object-shader.glsl.js +11 -0
- package/dist/lib/graphics/shaders/vertex/standard.glsl.js +9 -0
- package/dist/lib/graphics/zylem-scene.d.ts +2 -2
- package/dist/lib/graphics/zylem-scene.js +89 -0
- package/dist/lib/input/gamepad-provider.js +58 -0
- package/dist/lib/input/input-manager.js +70 -0
- package/dist/lib/input/keyboard-provider.js +120 -0
- package/dist/lib/stage/debug-entity-cursor.js +53 -0
- package/dist/lib/stage/entity-spawner.js +27 -0
- package/dist/lib/stage/stage-debug-delegate.js +100 -0
- package/dist/lib/stage/stage-state.d.ts +5 -1
- package/dist/lib/stage/stage-state.js +36 -0
- package/dist/lib/stage/stage.d.ts +0 -1
- package/dist/lib/stage/stage.js +58 -0
- package/dist/lib/stage/zylem-stage.d.ts +1 -3
- package/dist/lib/stage/zylem-stage.js +279 -0
- package/dist/lib/systems/transformable.system.js +43 -0
- package/dist/main.d.ts +5 -15
- package/dist/main.js +54 -35581
- package/dist/stage.d.ts +3 -0
- package/dist/stage.js +6 -0
- package/package.json +51 -17
- package/dist/assets/bounce.wav +0 -0
- package/dist/assets/coin-sound.mp3 +0 -0
- package/dist/lib/actions/behaviors/index.d.ts +0 -1
- package/dist/lib/collision/physics.d.ts +0 -4
- package/dist/lib/entities/object.d.ts +0 -1
- package/dist/lib/interfaces/game.d.ts +0 -19
- package/dist/main.cjs +0 -3980
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { ColliderDesc as B } from "@dimforge/rapier3d-compat";
|
|
2
|
+
import { Color as M, Vector3 as u, Vector2 as d, PlaneGeometry as C } from "three";
|
|
3
|
+
import { GameEntity as D } from "./entity.js";
|
|
4
|
+
import { EntityCollisionBuilder as E, EntityBuilder as P, EntityMeshBuilder as v } from "./builder.js";
|
|
5
|
+
import { XZPlaneGeometry as z } from "../graphics/geometries/XZPlaneGeometry.js";
|
|
6
|
+
import { createEntity as A } from "./create.js";
|
|
7
|
+
const r = 4, p = {
|
|
8
|
+
tile: new d(10, 10),
|
|
9
|
+
repeat: new d(1, 1),
|
|
10
|
+
position: new u(0, 0, 0),
|
|
11
|
+
collision: {
|
|
12
|
+
static: !0
|
|
13
|
+
},
|
|
14
|
+
material: {
|
|
15
|
+
color: new M("#ffffff"),
|
|
16
|
+
shader: "standard"
|
|
17
|
+
},
|
|
18
|
+
subdivisions: r
|
|
19
|
+
};
|
|
20
|
+
class G extends E {
|
|
21
|
+
collider(e) {
|
|
22
|
+
const o = e.tile ?? new d(1, 1), s = e.subdivisions ?? r, t = new u(o.x, 1, o.y), i = e._builders?.meshBuilder?.heightData, h = new u(t.x, 1, t.z);
|
|
23
|
+
return B.heightfield(s, s, i, h);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
class R extends v {
|
|
27
|
+
heightData = new Float32Array();
|
|
28
|
+
columnsRows = /* @__PURE__ */ new Map();
|
|
29
|
+
build(e) {
|
|
30
|
+
const o = e.tile ?? new d(1, 1), s = e.subdivisions ?? r, t = new u(o.x, 1, o.y), i = new z(t.x, t.z, s, s), h = new C(t.x, t.z, s, s), w = t.x / s, g = t.z / s, x = i.attributes.position.array, a = h.attributes.position.array, c = /* @__PURE__ */ new Map();
|
|
31
|
+
for (let n = 0; n < a.length; n += 3) {
|
|
32
|
+
let b = Math.floor(Math.abs(a[n] + t.x / 2) / w), m = Math.floor(Math.abs(a[n + 1] - t.z / 2) / g);
|
|
33
|
+
const y = Math.random() * 4;
|
|
34
|
+
a[n + 2] = y, x[n + 1] = y, c.get(m) || c.set(m, /* @__PURE__ */ new Map()), c.get(m).set(b, y);
|
|
35
|
+
}
|
|
36
|
+
return this.columnsRows = c, i;
|
|
37
|
+
}
|
|
38
|
+
postBuild() {
|
|
39
|
+
const e = [];
|
|
40
|
+
for (let o = 0; o <= r; ++o)
|
|
41
|
+
for (let s = 0; s <= r; ++s) {
|
|
42
|
+
const t = this.columnsRows.get(s);
|
|
43
|
+
if (!t)
|
|
44
|
+
continue;
|
|
45
|
+
const i = t.get(o);
|
|
46
|
+
e.push(i);
|
|
47
|
+
}
|
|
48
|
+
this.heightData = new Float32Array(e);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
class S extends P {
|
|
52
|
+
createEntity(e) {
|
|
53
|
+
return new f(e);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
const V = Symbol("Plane");
|
|
57
|
+
class f extends D {
|
|
58
|
+
static type = V;
|
|
59
|
+
constructor(e) {
|
|
60
|
+
super(), this.options = { ...p, ...e };
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
async function U(...l) {
|
|
64
|
+
return A({
|
|
65
|
+
args: l,
|
|
66
|
+
defaultConfig: p,
|
|
67
|
+
EntityClass: f,
|
|
68
|
+
BuilderClass: S,
|
|
69
|
+
MeshBuilderClass: R,
|
|
70
|
+
CollisionBuilderClass: G,
|
|
71
|
+
entityType: f.type
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
export {
|
|
75
|
+
V as PLANE_TYPE,
|
|
76
|
+
S as PlaneBuilder,
|
|
77
|
+
G as PlaneCollisionBuilder,
|
|
78
|
+
R as PlaneMeshBuilder,
|
|
79
|
+
f as ZylemPlane,
|
|
80
|
+
U as plane
|
|
81
|
+
};
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { Vector2 as f, Group as D, CanvasTexture as R, LinearFilter as x, SpriteMaterial as V, Sprite as Y, ClampToEdgeWrapping as T, ShaderMaterial as y, Color as S, Mesh as z, PlaneGeometry as B, Vector3 as W } from "three";
|
|
2
|
+
import { GameEntity as I } from "./entity.js";
|
|
3
|
+
import { EntityBuilder as X } from "./builder.js";
|
|
4
|
+
import { createEntity as q } from "./create.js";
|
|
5
|
+
import { DebugDelegate as U } from "./delegates/debug.js";
|
|
6
|
+
const k = {
|
|
7
|
+
position: void 0,
|
|
8
|
+
width: 120,
|
|
9
|
+
height: 48,
|
|
10
|
+
fillColor: "#FFFFFF",
|
|
11
|
+
strokeColor: null,
|
|
12
|
+
strokeWidth: 0,
|
|
13
|
+
radius: 0,
|
|
14
|
+
padding: 0,
|
|
15
|
+
stickToViewport: !0,
|
|
16
|
+
screenPosition: new f(24, 24),
|
|
17
|
+
zDistance: 1,
|
|
18
|
+
anchor: new f(0, 0)
|
|
19
|
+
};
|
|
20
|
+
class G extends X {
|
|
21
|
+
createEntity(t) {
|
|
22
|
+
return new _(t);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
const O = Symbol("Rect");
|
|
26
|
+
class _ extends I {
|
|
27
|
+
static type = O;
|
|
28
|
+
_sprite = null;
|
|
29
|
+
_mesh = null;
|
|
30
|
+
_texture = null;
|
|
31
|
+
_canvas = null;
|
|
32
|
+
_ctx = null;
|
|
33
|
+
_cameraRef = null;
|
|
34
|
+
_lastCanvasW = 0;
|
|
35
|
+
_lastCanvasH = 0;
|
|
36
|
+
constructor(t) {
|
|
37
|
+
super(), this.options = { ...k, ...t }, this.group = new D(), this.createSprite(), this.lifeCycleDelegate = {
|
|
38
|
+
setup: [this.rectSetup.bind(this)],
|
|
39
|
+
update: [this.rectUpdate.bind(this)]
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
createSprite() {
|
|
43
|
+
this._canvas = document.createElement("canvas"), this._ctx = this._canvas.getContext("2d"), this._texture = new R(this._canvas), this._texture.minFilter = x, this._texture.magFilter = x;
|
|
44
|
+
const t = new V({
|
|
45
|
+
map: this._texture,
|
|
46
|
+
transparent: !0,
|
|
47
|
+
depthTest: !1,
|
|
48
|
+
depthWrite: !1,
|
|
49
|
+
alphaTest: 0.5
|
|
50
|
+
});
|
|
51
|
+
this._sprite = new Y(t), this.group?.add(this._sprite), this.redrawRect();
|
|
52
|
+
}
|
|
53
|
+
redrawRect() {
|
|
54
|
+
if (!this._canvas || !this._ctx)
|
|
55
|
+
return;
|
|
56
|
+
const t = Math.max(2, Math.floor(this.options.width ?? 120)), e = Math.max(2, Math.floor(this.options.height ?? 48)), s = this.options.padding ?? 0, i = this.options.strokeWidth ?? 0, a = t + s * 2 + i, r = e + s * 2 + i, o = Math.max(2, a), n = Math.max(2, r), h = o !== this._lastCanvasW || n !== this._lastCanvasH;
|
|
57
|
+
this._canvas.width = o, this._canvas.height = n, this._lastCanvasW = o, this._lastCanvasH = n, this._ctx.clearRect(0, 0, this._canvas.width, this._canvas.height);
|
|
58
|
+
const u = Math.max(0, this.options.radius ?? 0), d = Math.floor(s + i / 2), m = Math.floor(s + i / 2), g = Math.floor(t), p = Math.floor(e);
|
|
59
|
+
if (this._ctx.beginPath(), u > 0 ? this.roundedRectPath(this._ctx, d, m, g, p, u) : this._ctx.rect(d, m, g, p), this.options.fillColor && (this._ctx.fillStyle = this.toCssColor(this.options.fillColor), this._ctx.fill()), this.options.strokeColor && i > 0 && (this._ctx.lineWidth = i, this._ctx.strokeStyle = this.toCssColor(this.options.strokeColor), this._ctx.stroke()), this._texture) {
|
|
60
|
+
if (h && (this._texture.dispose(), this._texture = new R(this._canvas), this._texture.minFilter = x, this._texture.magFilter = x, this._texture.wrapS = T, this._texture.wrapT = T, this._sprite && this._sprite.material instanceof y)) {
|
|
61
|
+
const c = this._sprite.material;
|
|
62
|
+
c.uniforms?.tDiffuse && (c.uniforms.tDiffuse.value = this._texture), c.uniforms?.iResolution && c.uniforms.iResolution.value.set(this._canvas.width, this._canvas.height, 1);
|
|
63
|
+
}
|
|
64
|
+
this._texture.image = this._canvas, this._texture.needsUpdate = !0, this._sprite && this._sprite.material && (this._sprite.material.map = this._texture, this._sprite.material.needsUpdate = !0);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
roundedRectPath(t, e, s, i, a, r) {
|
|
68
|
+
const o = Math.min(r, Math.floor(Math.min(i, a) / 2));
|
|
69
|
+
t.moveTo(e + o, s), t.lineTo(e + i - o, s), t.quadraticCurveTo(e + i, s, e + i, s + o), t.lineTo(e + i, s + a - o), t.quadraticCurveTo(e + i, s + a, e + i - o, s + a), t.lineTo(e + o, s + a), t.quadraticCurveTo(e, s + a, e, s + a - o), t.lineTo(e, s + o), t.quadraticCurveTo(e, s, e + o, s);
|
|
70
|
+
}
|
|
71
|
+
toCssColor(t) {
|
|
72
|
+
return typeof t == "string" ? t : `#${(t instanceof S ? t : new S(t)).getHexString()}`;
|
|
73
|
+
}
|
|
74
|
+
rectSetup(t) {
|
|
75
|
+
if (this._cameraRef = t.camera, this.options.stickToViewport && this._cameraRef && this._cameraRef.camera.add(this.group), this.materials?.length && this._sprite) {
|
|
76
|
+
const e = this.materials[0];
|
|
77
|
+
e instanceof y && (e.transparent = !0, e.depthTest = !1, e.depthWrite = !1, this._texture && (e.uniforms?.tDiffuse && (e.uniforms.tDiffuse.value = this._texture), e.uniforms?.iResolution && this._canvas && e.uniforms.iResolution.value.set(this._canvas.width, this._canvas.height, 1)), this._mesh = new z(new B(1, 1), e), this.group?.add(this._mesh), this._sprite.visible = !1);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
rectUpdate(t) {
|
|
81
|
+
if (this._sprite) {
|
|
82
|
+
if (this._cameraRef && this.options.bounds) {
|
|
83
|
+
this._cameraRef.renderer.domElement;
|
|
84
|
+
const e = this.computeScreenBoundsFromOptions(this.options.bounds);
|
|
85
|
+
if (e) {
|
|
86
|
+
const { x: s, y: i, width: a, height: r } = e, o = Math.max(2, Math.floor(a)), n = Math.max(2, Math.floor(r)), h = o !== (this.options.width ?? 0) || n !== (this.options.height ?? 0);
|
|
87
|
+
this.options.screenPosition = new f(Math.floor(s), Math.floor(i)), this.options.width = o, this.options.height = n, this.options.anchor = new f(0, 0), h && this.redrawRect();
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
this.options.stickToViewport && this._cameraRef && this.updateStickyTransform();
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
updateStickyTransform() {
|
|
94
|
+
if (!this._sprite || !this._cameraRef)
|
|
95
|
+
return;
|
|
96
|
+
const t = this._cameraRef.camera, e = this._cameraRef.renderer.domElement, s = e.clientWidth, i = e.clientHeight, a = (this.options.screenPosition ?? new f(24, 24)).x, r = (this.options.screenPosition ?? new f(24, 24)).y, o = Math.max(1e-3, this.options.zDistance ?? 1);
|
|
97
|
+
let n = 1, h = 1;
|
|
98
|
+
if (t.isPerspectiveCamera) {
|
|
99
|
+
const l = t, w = Math.tan(l.fov * Math.PI / 180 / 2) * o;
|
|
100
|
+
n = w * l.aspect, h = w;
|
|
101
|
+
} else if (t.isOrthographicCamera) {
|
|
102
|
+
const l = t;
|
|
103
|
+
n = (l.right - l.left) / 2, h = (l.top - l.bottom) / 2;
|
|
104
|
+
}
|
|
105
|
+
const u = a / s * 2 - 1, d = 1 - r / i * 2, m = u * n, g = d * h;
|
|
106
|
+
let p = 1, c = 1;
|
|
107
|
+
if (this._canvas) {
|
|
108
|
+
const w = h * 2 / i, C = this._canvas.height;
|
|
109
|
+
c = Math.max(1e-4, C * w);
|
|
110
|
+
const P = this._canvas.width / this._canvas.height;
|
|
111
|
+
p = c * P, this._sprite.scale.set(p, c, 1), this._mesh && this._mesh.scale.set(p, c, 1);
|
|
112
|
+
}
|
|
113
|
+
const M = this.options.anchor ?? new f(0, 0), H = Math.min(100, Math.max(0, M.x)) / 100, b = Math.min(100, Math.max(0, M.y)) / 100, E = (0.5 - H) * p, F = (b - 0.5) * c;
|
|
114
|
+
this.group?.position.set(m + E, g + F, -o);
|
|
115
|
+
}
|
|
116
|
+
worldToScreen(t) {
|
|
117
|
+
if (!this._cameraRef)
|
|
118
|
+
return { x: 0, y: 0 };
|
|
119
|
+
const e = this._cameraRef.camera, s = this._cameraRef.renderer.domElement, i = t.clone().project(e), a = (i.x + 1) / 2 * s.clientWidth, r = (1 - i.y) / 2 * s.clientHeight;
|
|
120
|
+
return { x: a, y: r };
|
|
121
|
+
}
|
|
122
|
+
computeScreenBoundsFromOptions(t) {
|
|
123
|
+
if (!this._cameraRef)
|
|
124
|
+
return null;
|
|
125
|
+
if (this._cameraRef.renderer.domElement, t.screen)
|
|
126
|
+
return { ...t.screen };
|
|
127
|
+
if (t.world) {
|
|
128
|
+
const { left: e, right: s, top: i, bottom: a, z: r = 0 } = t.world, o = this.worldToScreen(new W(e, i, r)), n = this.worldToScreen(new W(s, a, r)), h = Math.min(o.x, n.x), u = Math.min(o.y, n.y), d = Math.abs(n.x - o.x), m = Math.abs(n.y - o.y);
|
|
129
|
+
return { x: h, y: u, width: d, height: m };
|
|
130
|
+
}
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
updateRect(t) {
|
|
134
|
+
this.options = { ...this.options, ...t }, this.redrawRect(), this.options.stickToViewport && this._cameraRef && this.updateStickyTransform();
|
|
135
|
+
}
|
|
136
|
+
buildInfo() {
|
|
137
|
+
return {
|
|
138
|
+
...new U(this).buildDebugInfo(),
|
|
139
|
+
type: String(_.type),
|
|
140
|
+
width: this.options.width ?? 0,
|
|
141
|
+
height: this.options.height ?? 0,
|
|
142
|
+
sticky: this.options.stickToViewport
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
async function K(...v) {
|
|
147
|
+
return q({
|
|
148
|
+
args: v,
|
|
149
|
+
defaultConfig: { ...k },
|
|
150
|
+
EntityClass: _,
|
|
151
|
+
BuilderClass: G,
|
|
152
|
+
entityType: _.type
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
export {
|
|
156
|
+
O as RECT_TYPE,
|
|
157
|
+
G as RectBuilder,
|
|
158
|
+
_ as ZylemRect,
|
|
159
|
+
K as rect
|
|
160
|
+
};
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { ColliderDesc as l } from "@dimforge/rapier3d-compat";
|
|
2
|
+
import { Color as n, Vector3 as a, SphereGeometry as d } from "three";
|
|
3
|
+
import { GameEntity as u } from "./entity.js";
|
|
4
|
+
import { EntityCollisionBuilder as c, EntityBuilder as p, EntityMeshBuilder as f } from "./builder.js";
|
|
5
|
+
import { DebugDelegate as m } from "./delegates/debug.js";
|
|
6
|
+
import { createEntity as y } from "./create.js";
|
|
7
|
+
const o = {
|
|
8
|
+
radius: 1,
|
|
9
|
+
position: new a(0, 0, 0),
|
|
10
|
+
collision: {
|
|
11
|
+
static: !1
|
|
12
|
+
},
|
|
13
|
+
material: {
|
|
14
|
+
color: new n("#ffffff"),
|
|
15
|
+
shader: "standard"
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
class h extends c {
|
|
19
|
+
collider(e) {
|
|
20
|
+
const t = e.radius ?? 1;
|
|
21
|
+
return l.ball(t);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
class C extends f {
|
|
25
|
+
build(e) {
|
|
26
|
+
const t = e.radius ?? 1;
|
|
27
|
+
return new d(t);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
class E extends p {
|
|
31
|
+
createEntity(e) {
|
|
32
|
+
return new r(e);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
const B = Symbol("Sphere");
|
|
36
|
+
class r extends u {
|
|
37
|
+
static type = B;
|
|
38
|
+
constructor(e) {
|
|
39
|
+
super(), this.options = { ...o, ...e };
|
|
40
|
+
}
|
|
41
|
+
buildInfo() {
|
|
42
|
+
const t = new m(this).buildDebugInfo(), i = this.options.radius ?? 1;
|
|
43
|
+
return {
|
|
44
|
+
...t,
|
|
45
|
+
type: String(r.type),
|
|
46
|
+
radius: i.toFixed(2)
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
async function I(...s) {
|
|
51
|
+
return y({
|
|
52
|
+
args: s,
|
|
53
|
+
defaultConfig: o,
|
|
54
|
+
EntityClass: r,
|
|
55
|
+
BuilderClass: E,
|
|
56
|
+
MeshBuilderClass: C,
|
|
57
|
+
CollisionBuilderClass: h,
|
|
58
|
+
entityType: r.type
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
export {
|
|
62
|
+
B as SPHERE_TYPE,
|
|
63
|
+
E as SphereBuilder,
|
|
64
|
+
h as SphereCollisionBuilder,
|
|
65
|
+
C as SphereMeshBuilder,
|
|
66
|
+
r as ZylemSphere,
|
|
67
|
+
I as sphere
|
|
68
|
+
};
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { ColliderDesc as h } from "@dimforge/rapier3d-compat";
|
|
2
|
+
import { Color as d, Vector3 as c, TextureLoader as f, SpriteMaterial as y, Sprite as A, Group as x, Quaternion as S, Euler as I } from "three";
|
|
3
|
+
import { GameEntity as w } from "./entity.js";
|
|
4
|
+
import { EntityCollisionBuilder as z, EntityBuilder as E } from "./builder.js";
|
|
5
|
+
import { createEntity as g } from "./create.js";
|
|
6
|
+
const l = {
|
|
7
|
+
size: new c(1, 1, 1),
|
|
8
|
+
position: new c(0, 0, 0),
|
|
9
|
+
collision: {
|
|
10
|
+
static: !1
|
|
11
|
+
},
|
|
12
|
+
material: {
|
|
13
|
+
color: new d("#ffffff"),
|
|
14
|
+
shader: "standard"
|
|
15
|
+
},
|
|
16
|
+
images: [],
|
|
17
|
+
animations: []
|
|
18
|
+
};
|
|
19
|
+
class C extends z {
|
|
20
|
+
collider(t) {
|
|
21
|
+
const i = t.collisionSize || t.size || new c(1, 1, 1), e = { x: i.x / 2, y: i.y / 2, z: i.z / 2 };
|
|
22
|
+
return h.cuboid(e.x, e.y, e.z);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
class T extends E {
|
|
26
|
+
createEntity(t) {
|
|
27
|
+
return new p(t);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
const b = Symbol("Sprite");
|
|
31
|
+
class p extends w {
|
|
32
|
+
static type = b;
|
|
33
|
+
sprites = [];
|
|
34
|
+
spriteMap = /* @__PURE__ */ new Map();
|
|
35
|
+
currentSpriteIndex = 0;
|
|
36
|
+
animations = /* @__PURE__ */ new Map();
|
|
37
|
+
currentAnimation = null;
|
|
38
|
+
currentAnimationFrame = "";
|
|
39
|
+
currentAnimationIndex = 0;
|
|
40
|
+
currentAnimationTime = 0;
|
|
41
|
+
constructor(t) {
|
|
42
|
+
super(), this.options = { ...l, ...t }, this.createSpritesFromImages(t?.images || []), this.createAnimations(t?.animations || []), this.lifeCycleDelegate = {
|
|
43
|
+
update: [this.spriteUpdate.bind(this)],
|
|
44
|
+
destroy: [this.spriteDestroy.bind(this)]
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
createSpritesFromImages(t) {
|
|
48
|
+
const i = new f();
|
|
49
|
+
t.forEach((e, n) => {
|
|
50
|
+
const r = i.load(e.file), s = new y({
|
|
51
|
+
map: r,
|
|
52
|
+
transparent: !0
|
|
53
|
+
}), a = new A(s);
|
|
54
|
+
a.position.normalize(), this.sprites.push(a), this.spriteMap.set(e.name, n);
|
|
55
|
+
}), this.group = new x(), this.group.add(...this.sprites);
|
|
56
|
+
}
|
|
57
|
+
createAnimations(t) {
|
|
58
|
+
t.forEach((i) => {
|
|
59
|
+
const { name: e, frames: n, loop: r = !1, speed: s = 1 } = i, a = {
|
|
60
|
+
frames: n.map((u, m) => ({
|
|
61
|
+
key: u,
|
|
62
|
+
index: m,
|
|
63
|
+
time: (typeof s == "number" ? s : s[m]) * (m + 1),
|
|
64
|
+
duration: typeof s == "number" ? s : s[m]
|
|
65
|
+
})),
|
|
66
|
+
loop: r
|
|
67
|
+
};
|
|
68
|
+
this.animations.set(e, a);
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
setSprite(t) {
|
|
72
|
+
const e = this.spriteMap.get(t) ?? 0;
|
|
73
|
+
this.currentSpriteIndex = e, this.sprites.forEach((n, r) => {
|
|
74
|
+
n.visible = this.currentSpriteIndex === r;
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
setAnimation(t, i) {
|
|
78
|
+
const e = this.animations.get(t);
|
|
79
|
+
if (!e)
|
|
80
|
+
return;
|
|
81
|
+
const { loop: n, frames: r } = e, s = r[this.currentAnimationIndex];
|
|
82
|
+
t === this.currentAnimation ? (this.currentAnimationFrame = s.key, this.currentAnimationTime += i, this.setSprite(this.currentAnimationFrame)) : this.currentAnimation = t, this.currentAnimationTime > s.time && this.currentAnimationIndex++, this.currentAnimationIndex >= r.length && (n ? (this.currentAnimationIndex = 0, this.currentAnimationTime = 0) : this.currentAnimationTime = r[this.currentAnimationIndex].time);
|
|
83
|
+
}
|
|
84
|
+
async spriteUpdate(t) {
|
|
85
|
+
this.sprites.forEach((i) => {
|
|
86
|
+
if (i.material) {
|
|
87
|
+
const e = this.body?.rotation();
|
|
88
|
+
if (e) {
|
|
89
|
+
const n = new S(e.x, e.y, e.z, e.w), r = new I().setFromQuaternion(n, "XYZ");
|
|
90
|
+
i.material.rotation = r.z;
|
|
91
|
+
}
|
|
92
|
+
i.scale.set(this.options.size?.x ?? 1, this.options.size?.y ?? 1, this.options.size?.z ?? 1);
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
async spriteDestroy(t) {
|
|
97
|
+
this.sprites.forEach((i) => {
|
|
98
|
+
i.removeFromParent();
|
|
99
|
+
}), this.group?.remove(...this.sprites), this.group?.removeFromParent();
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
async function P(...o) {
|
|
103
|
+
return g({
|
|
104
|
+
args: o,
|
|
105
|
+
defaultConfig: l,
|
|
106
|
+
EntityClass: p,
|
|
107
|
+
BuilderClass: T,
|
|
108
|
+
CollisionBuilderClass: C,
|
|
109
|
+
entityType: p.type
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
export {
|
|
113
|
+
b as SPRITE_TYPE,
|
|
114
|
+
T as SpriteBuilder,
|
|
115
|
+
C as SpriteCollisionBuilder,
|
|
116
|
+
p as ZylemSprite,
|
|
117
|
+
P as sprite
|
|
118
|
+
};
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { Vector2 as u, Group as H, CanvasTexture as w, LinearFilter as l, SpriteMaterial as k, Sprite as M, ClampToEdgeWrapping as C, Color as v } from "three";
|
|
2
|
+
import { GameEntity as R } from "./entity.js";
|
|
3
|
+
import { EntityBuilder as W } from "./builder.js";
|
|
4
|
+
import { createEntity as E } from "./create.js";
|
|
5
|
+
import { DebugDelegate as D } from "./delegates/debug.js";
|
|
6
|
+
const x = {
|
|
7
|
+
position: void 0,
|
|
8
|
+
text: "",
|
|
9
|
+
fontFamily: 'Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace',
|
|
10
|
+
fontSize: 18,
|
|
11
|
+
fontColor: "#FFFFFF",
|
|
12
|
+
backgroundColor: null,
|
|
13
|
+
padding: 4,
|
|
14
|
+
stickToViewport: !0,
|
|
15
|
+
screenPosition: new u(24, 24),
|
|
16
|
+
zDistance: 1
|
|
17
|
+
};
|
|
18
|
+
class P extends W {
|
|
19
|
+
createEntity(t) {
|
|
20
|
+
return new o(t);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
const z = Symbol("Text");
|
|
24
|
+
class o extends R {
|
|
25
|
+
static type = z;
|
|
26
|
+
_sprite = null;
|
|
27
|
+
_texture = null;
|
|
28
|
+
_canvas = null;
|
|
29
|
+
_ctx = null;
|
|
30
|
+
_cameraRef = null;
|
|
31
|
+
_lastCanvasW = 0;
|
|
32
|
+
_lastCanvasH = 0;
|
|
33
|
+
constructor(t) {
|
|
34
|
+
super(), this.options = { ...x, ...t }, this.group = new H(), this.createSprite(), this.lifeCycleDelegate = {
|
|
35
|
+
setup: [this.textSetup.bind(this)],
|
|
36
|
+
update: [this.textUpdate.bind(this)]
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
createSprite() {
|
|
40
|
+
this._canvas = document.createElement("canvas"), this._ctx = this._canvas.getContext("2d"), this._texture = new w(this._canvas), this._texture.minFilter = l, this._texture.magFilter = l;
|
|
41
|
+
const t = new k({
|
|
42
|
+
map: this._texture,
|
|
43
|
+
transparent: !0,
|
|
44
|
+
depthTest: !1,
|
|
45
|
+
depthWrite: !1,
|
|
46
|
+
alphaTest: 0.5
|
|
47
|
+
});
|
|
48
|
+
this._sprite = new M(t), this.group?.add(this._sprite), this.redrawText(this.options.text ?? "");
|
|
49
|
+
}
|
|
50
|
+
redrawText(t) {
|
|
51
|
+
if (!this._canvas || !this._ctx)
|
|
52
|
+
return;
|
|
53
|
+
const e = this.options.fontSize ?? 18, r = this.options.fontFamily ?? x.fontFamily, a = this.options.padding ?? 4;
|
|
54
|
+
this._ctx.font = `${e}px ${r}`;
|
|
55
|
+
const p = this._ctx.measureText(t), _ = Math.ceil(p.width), h = Math.ceil(e * 1.4), n = Math.max(2, _ + a * 2), s = Math.max(2, h + a * 2), f = n !== this._lastCanvasW || s !== this._lastCanvasH;
|
|
56
|
+
this._canvas.width = n, this._canvas.height = s, this._lastCanvasW = n, this._lastCanvasH = s, this._ctx.font = `${e}px ${r}`, this._ctx.textBaseline = "top", this._ctx.clearRect(0, 0, this._canvas.width, this._canvas.height), this.options.backgroundColor && (this._ctx.fillStyle = this.toCssColor(this.options.backgroundColor), this._ctx.fillRect(0, 0, this._canvas.width, this._canvas.height)), this._ctx.fillStyle = this.toCssColor(this.options.fontColor ?? "#FFFFFF"), this._ctx.fillText(t, a, a), this._texture && (f && (this._texture.dispose(), this._texture = new w(this._canvas), this._texture.minFilter = l, this._texture.magFilter = l, this._texture.wrapS = C, this._texture.wrapT = C), this._texture.image = this._canvas, this._texture.needsUpdate = !0, this._sprite && this._sprite.material && (this._sprite.material.map = this._texture, this._sprite.material.needsUpdate = !0));
|
|
57
|
+
}
|
|
58
|
+
toCssColor(t) {
|
|
59
|
+
return typeof t == "string" ? t : `#${(t instanceof v ? t : new v(t)).getHexString()}`;
|
|
60
|
+
}
|
|
61
|
+
textSetup(t) {
|
|
62
|
+
this._cameraRef = t.camera, this.options.stickToViewport && this._cameraRef && this._cameraRef.camera.add(this.group);
|
|
63
|
+
}
|
|
64
|
+
textUpdate(t) {
|
|
65
|
+
this._sprite && this.options.stickToViewport && this._cameraRef && this.updateStickyTransform();
|
|
66
|
+
}
|
|
67
|
+
updateStickyTransform() {
|
|
68
|
+
if (!this._sprite || !this._cameraRef)
|
|
69
|
+
return;
|
|
70
|
+
const t = this._cameraRef.camera, e = this._cameraRef.renderer.domElement, r = e.clientWidth, a = e.clientHeight, p = (this.options.screenPosition ?? new u(24, 24)).x, _ = (this.options.screenPosition ?? new u(24, 24)).y, h = Math.max(1e-3, this.options.zDistance ?? 1);
|
|
71
|
+
let n = 1, s = 1;
|
|
72
|
+
if (t.isPerspectiveCamera) {
|
|
73
|
+
const i = t, c = Math.tan(i.fov * Math.PI / 180 / 2) * h;
|
|
74
|
+
n = c * i.aspect, s = c;
|
|
75
|
+
} else if (t.isOrthographicCamera) {
|
|
76
|
+
const i = t;
|
|
77
|
+
n = (i.right - i.left) / 2, s = (i.top - i.bottom) / 2;
|
|
78
|
+
}
|
|
79
|
+
const f = p / r * 2 - 1, y = 1 - _ / a * 2, T = f * n, F = y * s;
|
|
80
|
+
if (this.group?.position.set(T, F, -h), this._canvas) {
|
|
81
|
+
const c = s * 2 / a, m = this._canvas.height, g = Math.max(1e-4, m * c), S = this._canvas.width / this._canvas.height, b = g * S;
|
|
82
|
+
this._sprite.scale.set(b, g, 1);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
updateText(t) {
|
|
86
|
+
this.options.text = t, this.redrawText(t), this.options.stickToViewport && this._cameraRef && this.updateStickyTransform();
|
|
87
|
+
}
|
|
88
|
+
buildInfo() {
|
|
89
|
+
return {
|
|
90
|
+
...new D(this).buildDebugInfo(),
|
|
91
|
+
type: String(o.type),
|
|
92
|
+
text: this.options.text ?? "",
|
|
93
|
+
sticky: this.options.stickToViewport
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
async function X(...d) {
|
|
98
|
+
return E({
|
|
99
|
+
args: d,
|
|
100
|
+
defaultConfig: { ...x },
|
|
101
|
+
EntityClass: o,
|
|
102
|
+
BuilderClass: P,
|
|
103
|
+
entityType: o.type
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
export {
|
|
107
|
+
z as TEXT_TYPE,
|
|
108
|
+
P as TextBuilder,
|
|
109
|
+
o as ZylemText,
|
|
110
|
+
X as text
|
|
111
|
+
};
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { ColliderDesc as a, ActiveCollisionTypes as h } from "@dimforge/rapier3d-compat";
|
|
2
|
+
import { Vector3 as l } from "three";
|
|
3
|
+
import { GameEntity as u } from "./entity.js";
|
|
4
|
+
import { EntityCollisionBuilder as c, EntityBuilder as p } from "./builder.js";
|
|
5
|
+
import { createEntity as E } from "./create.js";
|
|
6
|
+
import { state as o } from "../game/game-state.js";
|
|
7
|
+
const d = {
|
|
8
|
+
size: new l(1, 1, 1),
|
|
9
|
+
position: new l(0, 0, 0),
|
|
10
|
+
collision: {
|
|
11
|
+
static: !0
|
|
12
|
+
},
|
|
13
|
+
material: {
|
|
14
|
+
shader: "standard"
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
class f extends c {
|
|
18
|
+
collider(e) {
|
|
19
|
+
const t = e.size || new l(1, 1, 1), i = { x: t.x / 2, y: t.y / 2, z: t.z / 2 };
|
|
20
|
+
let s = a.cuboid(i.x, i.y, i.z);
|
|
21
|
+
return s.setSensor(!0), s.activeCollisionTypes = h.KINEMATIC_FIXED, s;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
class x extends p {
|
|
25
|
+
createEntity(e) {
|
|
26
|
+
return new r(e);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const Z = Symbol("Zone");
|
|
30
|
+
class r extends u {
|
|
31
|
+
static type = Z;
|
|
32
|
+
_enteredZone = /* @__PURE__ */ new Map();
|
|
33
|
+
_exitedZone = /* @__PURE__ */ new Map();
|
|
34
|
+
_zoneEntities = /* @__PURE__ */ new Map();
|
|
35
|
+
constructor(e) {
|
|
36
|
+
super(), this.options = { ...d, ...e };
|
|
37
|
+
}
|
|
38
|
+
handlePostCollision({ delta: e }) {
|
|
39
|
+
return this._enteredZone.forEach((t, i) => {
|
|
40
|
+
this.exited(e, i);
|
|
41
|
+
}), this._enteredZone.size > 0;
|
|
42
|
+
}
|
|
43
|
+
handleIntersectionEvent({ other: e, delta: t }) {
|
|
44
|
+
this._enteredZone.get(e.uuid) ? this.held(t, e) : (this.entered(e), this._zoneEntities.set(e.uuid, e));
|
|
45
|
+
}
|
|
46
|
+
onEnter(e) {
|
|
47
|
+
return this.options.onEnter = e, this;
|
|
48
|
+
}
|
|
49
|
+
onHeld(e) {
|
|
50
|
+
return this.options.onHeld = e, this;
|
|
51
|
+
}
|
|
52
|
+
onExit(e) {
|
|
53
|
+
return this.options.onExit = e, this;
|
|
54
|
+
}
|
|
55
|
+
entered(e) {
|
|
56
|
+
this._enteredZone.set(e.uuid, 1), this.options.onEnter && this.options.onEnter({
|
|
57
|
+
self: this,
|
|
58
|
+
visitor: e,
|
|
59
|
+
globals: o.globals
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
exited(e, t) {
|
|
63
|
+
const i = this._exitedZone.get(t);
|
|
64
|
+
if (i && i > 1 + e) {
|
|
65
|
+
this._exitedZone.delete(t), this._enteredZone.delete(t);
|
|
66
|
+
const s = this._zoneEntities.get(t);
|
|
67
|
+
this.options.onExit && this.options.onExit({
|
|
68
|
+
self: this,
|
|
69
|
+
visitor: s,
|
|
70
|
+
globals: o.globals
|
|
71
|
+
});
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
this._exitedZone.set(t, 1 + e);
|
|
75
|
+
}
|
|
76
|
+
held(e, t) {
|
|
77
|
+
const i = this._enteredZone.get(t.uuid) ?? 0;
|
|
78
|
+
this._enteredZone.set(t.uuid, i + e), this._exitedZone.set(t.uuid, 1), this.options.onHeld && this.options.onHeld({
|
|
79
|
+
delta: e,
|
|
80
|
+
self: this,
|
|
81
|
+
visitor: t,
|
|
82
|
+
globals: o.globals,
|
|
83
|
+
heldTime: i
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
async function b(...n) {
|
|
88
|
+
return E({
|
|
89
|
+
args: n,
|
|
90
|
+
defaultConfig: d,
|
|
91
|
+
EntityClass: r,
|
|
92
|
+
BuilderClass: x,
|
|
93
|
+
CollisionBuilderClass: f,
|
|
94
|
+
entityType: r.type
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
export {
|
|
98
|
+
Z as ZONE_TYPE,
|
|
99
|
+
x as ZoneBuilder,
|
|
100
|
+
f as ZoneCollisionBuilder,
|
|
101
|
+
r as ZylemZone,
|
|
102
|
+
b as zone
|
|
103
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { proxy as a } from "valtio/vanilla";
|
|
2
|
+
const o = a({
|
|
3
|
+
id: "",
|
|
4
|
+
globals: {},
|
|
5
|
+
time: 0
|
|
6
|
+
});
|
|
7
|
+
function r(t, l) {
|
|
8
|
+
o.globals[t] = l;
|
|
9
|
+
}
|
|
10
|
+
function i(t) {
|
|
11
|
+
return t !== void 0 ? o.globals[t] : o.globals;
|
|
12
|
+
}
|
|
13
|
+
export {
|
|
14
|
+
i as getGlobalState,
|
|
15
|
+
r as setGlobalState,
|
|
16
|
+
o as state
|
|
17
|
+
};
|