@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
@@ -0,0 +1,19 @@
1
+ function n(e) {
2
+ return typeof e?.load == "function" && typeof e?.data == "function";
3
+ }
4
+ class a {
5
+ entityReference;
6
+ constructor(t) {
7
+ this.entityReference = t;
8
+ }
9
+ async load() {
10
+ this.entityReference.load && await this.entityReference.load();
11
+ }
12
+ async data() {
13
+ return this.entityReference.data ? this.entityReference.data() : null;
14
+ }
15
+ }
16
+ export {
17
+ a as EntityLoader,
18
+ n as isLoadable
19
+ };
@@ -0,0 +1,15 @@
1
+ import { getGlobalState as n } from "../game/game-state.js";
2
+ function r(o, t, e) {
3
+ e({
4
+ me: o,
5
+ globals: t
6
+ });
7
+ }
8
+ function d(o, t) {
9
+ const e = t ?? n();
10
+ r(o, e, o.nodeDestroy.bind(o));
11
+ }
12
+ export {
13
+ d as destroy,
14
+ r as destroyEntity
15
+ };
@@ -4,7 +4,7 @@ import { Vec3 } from "../core/vector";
4
4
  import { MaterialBuilder, MaterialOptions } from "../graphics/material";
5
5
  import { CollisionOptions } from "../collision/collision";
6
6
  import { BaseNode } from "../core/base-node";
7
- import { DestroyContext, SetupContext, UpdateContext } from "../core/base-node-life-cycle";
7
+ import { DestroyContext, SetupContext, UpdateContext, LoadedContext, CleanupContext } from "../core/base-node-life-cycle";
8
8
  import type { EntityMeshBuilder, EntityCollisionBuilder } from "./builder";
9
9
  export declare abstract class AbstractEntity {
10
10
  abstract uuid: string;
@@ -82,8 +82,10 @@ export declare class GameEntity<O extends GameEntityOptions> extends BaseNode<O>
82
82
  onDestroy(...callbacks: ((params: DestroyContext<this>) => void)[]): this;
83
83
  onCollision(...callbacks: ((params: CollisionContext<this, O>) => void)[]): this;
84
84
  _setup(params: SetupContext<this>): void;
85
+ protected _loaded(_params: LoadedContext<this>): Promise<void>;
85
86
  _update(params: UpdateContext<this>): void;
86
87
  _destroy(params: DestroyContext<this>): void;
88
+ protected _cleanup(_params: CleanupContext<this>): Promise<void>;
87
89
  _collision(other: GameEntity<O>, globals?: any): void;
88
90
  addBehavior(behaviorCallback: ({
89
91
  type: BehaviorCallbackType;
@@ -0,0 +1,120 @@
1
+ import { ShaderMaterial as s } from "three";
2
+ import { position as a, scale as o, rotation as n } from "../systems/transformable.system.js";
3
+ import { BaseNode as c } from "../core/base-node.js";
4
+ class f extends c {
5
+ group;
6
+ mesh;
7
+ materials;
8
+ bodyDesc = null;
9
+ body = null;
10
+ colliderDesc;
11
+ collider;
12
+ custom = {};
13
+ debugInfo = {};
14
+ debugMaterial;
15
+ lifeCycleDelegate = {
16
+ setup: [],
17
+ update: [],
18
+ destroy: []
19
+ };
20
+ collisionDelegate = {
21
+ collision: []
22
+ };
23
+ collisionType;
24
+ behaviorCallbackMap = {
25
+ setup: [],
26
+ update: [],
27
+ destroy: [],
28
+ collision: []
29
+ };
30
+ constructor() {
31
+ super();
32
+ }
33
+ create() {
34
+ const { position: e } = this.options, { x: t, y: i, z: l } = e || { x: 0, y: 0, z: 0 };
35
+ return this.behaviors = [
36
+ { component: a, values: { x: t, y: i, z: l } },
37
+ { component: o, values: { x: 0, y: 0, z: 0 } },
38
+ { component: n, values: { x: 0, y: 0, z: 0, w: 0 } }
39
+ ], this.name = this.options.name || "", this;
40
+ }
41
+ onSetup(...e) {
42
+ const t = [...this.lifeCycleDelegate.setup ?? [], ...e];
43
+ return this.lifeCycleDelegate = {
44
+ ...this.lifeCycleDelegate,
45
+ setup: t
46
+ }, this;
47
+ }
48
+ onUpdate(...e) {
49
+ const t = [...this.lifeCycleDelegate.update ?? [], ...e];
50
+ return this.lifeCycleDelegate = {
51
+ ...this.lifeCycleDelegate,
52
+ update: t
53
+ }, this;
54
+ }
55
+ onDestroy(...e) {
56
+ return this.lifeCycleDelegate = {
57
+ ...this.lifeCycleDelegate,
58
+ destroy: e.length > 0 ? e : void 0
59
+ }, this;
60
+ }
61
+ onCollision(...e) {
62
+ return this.collisionDelegate = {
63
+ collision: e.length > 0 ? e : void 0
64
+ }, this;
65
+ }
66
+ _setup(e) {
67
+ this.behaviorCallbackMap.setup.forEach((t) => {
68
+ t({ ...e, me: this });
69
+ }), this.lifeCycleDelegate.setup?.length && this.lifeCycleDelegate.setup.forEach((i) => {
70
+ i({ ...e, me: this });
71
+ });
72
+ }
73
+ async _loaded(e) {
74
+ }
75
+ _update(e) {
76
+ this.updateMaterials(e), this.lifeCycleDelegate.update?.length && this.lifeCycleDelegate.update.forEach((i) => {
77
+ i({ ...e, me: this });
78
+ }), this.behaviorCallbackMap.update.forEach((t) => {
79
+ t({ ...e, me: this });
80
+ });
81
+ }
82
+ _destroy(e) {
83
+ this.lifeCycleDelegate.destroy?.length && this.lifeCycleDelegate.destroy.forEach((i) => {
84
+ i({ ...e, me: this });
85
+ }), this.behaviorCallbackMap.destroy.forEach((t) => {
86
+ t({ ...e, me: this });
87
+ });
88
+ }
89
+ async _cleanup(e) {
90
+ }
91
+ _collision(e, t) {
92
+ this.collisionDelegate.collision?.length && this.collisionDelegate.collision.forEach((l) => {
93
+ l({ entity: this, other: e, globals: t });
94
+ }), this.behaviorCallbackMap.collision.forEach((i) => {
95
+ i({ entity: this, other: e, globals: t });
96
+ });
97
+ }
98
+ addBehavior(e) {
99
+ const t = e.handler;
100
+ return t && this.behaviorCallbackMap[e.type].push(t), this;
101
+ }
102
+ addBehaviors(e) {
103
+ return e.forEach((t) => {
104
+ const i = t.handler;
105
+ i && this.behaviorCallbackMap[t.type].push(i);
106
+ }), this;
107
+ }
108
+ updateMaterials(e) {
109
+ if (this.materials?.length)
110
+ for (const t of this.materials)
111
+ t instanceof s && t.uniforms && t.uniforms.iTime && (t.uniforms.iTime.value += e.delta);
112
+ }
113
+ buildInfo() {
114
+ const e = {};
115
+ return e.name = this.name, e.uuid = this.uuid, e.eid = this.eid.toString(), e;
116
+ }
117
+ }
118
+ export {
119
+ f as GameEntity
120
+ };
@@ -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
+ };