@n3rdw1z4rd/core 0.18.5 → 0.19.0

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 (109) hide show
  1. package/README.md +72 -10
  2. package/dist/astar.d.ts +44 -0
  3. package/dist/astar.js +118 -0
  4. package/dist/canvas-renderer.d.ts +154 -0
  5. package/dist/canvas-renderer.js +200 -0
  6. package/dist/clock.d.ts +69 -0
  7. package/dist/clock.js +108 -0
  8. package/dist/color-palette.d.ts +25 -0
  9. package/dist/color-palette.js +112 -0
  10. package/dist/color.d.ts +90 -0
  11. package/dist/color.js +224 -0
  12. package/dist/css/main.css +1 -3
  13. package/dist/disposable.d.ts +27 -0
  14. package/dist/disposable.js +32 -0
  15. package/dist/dungeon-generator.d.ts +142 -0
  16. package/dist/dungeon-generator.js +288 -0
  17. package/dist/ecs.d.ts +87 -0
  18. package/dist/ecs.js +101 -0
  19. package/dist/emitter.d.ts +40 -0
  20. package/dist/emitter.js +54 -0
  21. package/dist/event-queue.d.ts +29 -0
  22. package/dist/event-queue.js +40 -0
  23. package/dist/index.cjs +3813 -0
  24. package/dist/index.d.cts +2655 -0
  25. package/dist/index.d.ts +35 -873
  26. package/dist/index.js +34 -3981
  27. package/dist/input/action-map.d.ts +79 -0
  28. package/dist/input/action-map.js +131 -0
  29. package/dist/input/bindings.d.ts +273 -0
  30. package/dist/input/bindings.js +250 -0
  31. package/dist/input/gestures.d.ts +108 -0
  32. package/dist/input/gestures.js +159 -0
  33. package/dist/input/index.d.ts +5 -0
  34. package/dist/input/index.js +5 -0
  35. package/dist/input/input.d.ts +297 -0
  36. package/dist/input/input.js +695 -0
  37. package/dist/log.d.ts +8 -0
  38. package/dist/log.js +9 -0
  39. package/dist/map2d.d.ts +83 -0
  40. package/dist/map2d.js +120 -0
  41. package/dist/map3d-CHDJiq5T.d.cts +236 -0
  42. package/dist/map3d.d.ts +94 -0
  43. package/dist/map3d.js +148 -0
  44. package/dist/math.d.ts +176 -0
  45. package/dist/math.js +206 -0
  46. package/dist/misc.d.ts +46 -0
  47. package/dist/misc.js +71 -0
  48. package/dist/observable.d.ts +43 -0
  49. package/dist/observable.js +53 -0
  50. package/dist/particle-renderer.d.ts +47 -0
  51. package/dist/particle-renderer.js +82 -0
  52. package/dist/particle-system-2d.d.ts +60 -0
  53. package/dist/particle-system-2d.js +147 -0
  54. package/dist/random.d.ts +46 -0
  55. package/dist/random.js +74 -0
  56. package/dist/rectangle.d.ts +44 -0
  57. package/dist/rectangle.js +69 -0
  58. package/dist/rng.d.ts +72 -0
  59. package/dist/rng.js +152 -0
  60. package/dist/signal.d.ts +34 -0
  61. package/dist/signal.js +41 -0
  62. package/dist/spatial-partition-2d.d.ts +42 -0
  63. package/dist/spatial-partition-2d.js +52 -0
  64. package/dist/spatial-partition-3d-Ba-VBveM.cjs +1324 -0
  65. package/dist/spatial-partition-3d.d.ts +44 -0
  66. package/dist/spatial-partition-3d.js +57 -0
  67. package/dist/three/asteroid-mesh.d.ts +41 -0
  68. package/dist/three/asteroid-mesh.js +44 -0
  69. package/dist/three/camera-rig.d.ts +140 -0
  70. package/dist/three/camera-rig.js +197 -0
  71. package/dist/three/cube-sphere-geometry.d.ts +55 -0
  72. package/dist/three/cube-sphere-geometry.js +239 -0
  73. package/dist/three/ecs.d.ts +75 -0
  74. package/dist/three/ecs.js +64 -0
  75. package/dist/three/index.d.ts +12 -0
  76. package/dist/three/index.js +12 -0
  77. package/dist/three/particle-system-3d.d.ts +53 -0
  78. package/dist/three/particle-system-3d.js +224 -0
  79. package/dist/three/renderer.d.ts +38 -0
  80. package/dist/three/renderer.js +57 -0
  81. package/dist/three/starfield.d.ts +23 -0
  82. package/dist/three/starfield.js +64 -0
  83. package/dist/three/texture-atlas.d.ts +76 -0
  84. package/dist/three/texture-atlas.js +84 -0
  85. package/dist/three/texture-utils.d.ts +31 -0
  86. package/dist/three/texture-utils.js +44 -0
  87. package/dist/three/voxel-geometry.d.ts +72 -0
  88. package/dist/three/voxel-geometry.js +231 -0
  89. package/dist/three/voxel-mesh.d.ts +67 -0
  90. package/dist/three/voxel-mesh.js +71 -0
  91. package/dist/three.cjs +1307 -0
  92. package/dist/three.d.cts +655 -0
  93. package/dist/tilemap.d.ts +17 -0
  94. package/dist/tilemap.js +26 -0
  95. package/dist/types.d.ts +34 -0
  96. package/dist/types.js +0 -0
  97. package/dist/vector.d.ts +78 -0
  98. package/dist/vector.js +169 -0
  99. package/dist/webgl-renderer.d.ts +89 -0
  100. package/dist/webgl-renderer.js +140 -0
  101. package/dist/webgl.d.ts +357 -0
  102. package/dist/webgl.js +628 -0
  103. package/dist/worker-interface.d.ts +30 -0
  104. package/dist/worker-interface.js +39 -0
  105. package/package.json +55 -13
  106. package/dist/css/index.ts +0 -1
  107. package/dist/index.css +0 -104
  108. package/dist/index.d.mts +0 -873
  109. package/dist/index.mjs +0 -3977
package/dist/rng.js ADDED
@@ -0,0 +1,152 @@
1
+ import { TAU } from "./math.js";
2
+ import { Random } from "./random.js";
3
+ //#region src/rng.ts
4
+ /**
5
+ * Seeded random number generator with helpers for ranges, picking, shuffling, geometric points,
6
+ * Gaussian samples and matrices. Every call advances the underlying {@link Random}, so a given
7
+ * seed always reproduces the same sequence of calls.
8
+ *
9
+ * ```ts
10
+ * const r = new Rng(1234);
11
+ * r.range(1, 7); // integer in [1, 7), like a die roll
12
+ * r.pick(['a', 'b', 'c']);
13
+ * r.shuffle([1, 2, 3, 4]); // new array, input untouched
14
+ * ```
15
+ *
16
+ * A shared, time-seeded instance is exported as {@link rng}.
17
+ * @category Math & Random
18
+ */
19
+ var Rng = class {
20
+ /** the underlying generator; shared if one was passed to the constructor */
21
+ random;
22
+ /** the underlying {@link Random}'s current seed; setting it restarts the sequence */
23
+ get seed() {
24
+ return this.random.seed;
25
+ }
26
+ /** sets the underlying {@link Random}'s seed */
27
+ set seed(seed) {
28
+ this.random.seed = seed;
29
+ }
30
+ /** @param seed a seed (default `Date.now()`), or an existing {@link Random} to wrap */
31
+ constructor(seed) {
32
+ this.random = seed instanceof Random ? seed : new Random(seed);
33
+ }
34
+ /** Float in [0, 1). */
35
+ nextFloat() {
36
+ return this.random.float();
37
+ }
38
+ /** Unsigned 32-bit integer in [0, 2³²). */
39
+ nextUint() {
40
+ return this.random.uint();
41
+ }
42
+ /**
43
+ * random integer in [min, max); range(max) is [0, max). Non-integer bounds are rounded up,
44
+ * so the result is always an integer inside the requested range.
45
+ */
46
+ range(min, max) {
47
+ if (typeof max !== "number") {
48
+ max = min;
49
+ min = 0;
50
+ }
51
+ min = Math.ceil(min);
52
+ max = Math.ceil(max);
53
+ return min + Math.floor(this.random.float() * (max - min));
54
+ }
55
+ pick(array) {
56
+ return array[this.range(0, array.length)];
57
+ }
58
+ /** Up to `count` distinct elements of `array` in random order (all of them if `count` exceeds its length). */
59
+ sample(array, count) {
60
+ return this.shuffle(array).slice(0, count);
61
+ }
62
+ /** A shuffled copy of `array` (Fisher-Yates); the input is not modified. */
63
+ shuffle(array) {
64
+ const result = [...array];
65
+ for (let i = result.length - 1; i > 0; --i) {
66
+ const j = this.range(0, i + 1);
67
+ [result[i], result[j]] = [result[j], result[i]];
68
+ }
69
+ return result;
70
+ }
71
+ /** True with the given probability (0 = never, 1 = always). */
72
+ chance(probability) {
73
+ return this.random.float() < probability;
74
+ }
75
+ /** Random unit-length 2D direction as `[x, y]`. */
76
+ vector2() {
77
+ const θ = this.random.angle();
78
+ return [Math.cos(θ), Math.sin(θ)];
79
+ }
80
+ /** Uniform point on a circle of `radius` (default 1) centred on the origin, as `[x, y]`. */
81
+ pointOnCircle(radius = 1) {
82
+ const θ = this.random.angle();
83
+ return [radius * Math.cos(θ), radius * Math.sin(θ)];
84
+ }
85
+ /** Uniform point inside a disc of `radius` (default 1) centred on the origin, as `[x, y]`. */
86
+ pointInCircle(radius = 1) {
87
+ const θ = this.random.angle();
88
+ const r = Math.sqrt(this.random.float()) * radius;
89
+ return [r * Math.cos(θ), r * Math.sin(θ)];
90
+ }
91
+ /** Uniform point on the surface of a sphere of `radius` (default 1) centred on the origin, as `[x, y, z]`. */
92
+ pointOnSphere(radius = 1) {
93
+ const u = this.random.float();
94
+ const v = this.random.float();
95
+ const θ = TAU * u;
96
+ const φ = Math.acos(2 * v - 1);
97
+ const s = Math.sin(φ);
98
+ return [
99
+ radius * s * Math.cos(θ),
100
+ radius * s * Math.sin(θ),
101
+ radius * Math.cos(φ)
102
+ ];
103
+ }
104
+ /** Uniform point inside a ball of `radius` (default 1) centred on the origin, as `[x, y, z]`. */
105
+ pointInSphere(radius = 1) {
106
+ const u = this.random.float();
107
+ const v = this.random.float();
108
+ const θ = TAU * u;
109
+ const φ = Math.acos(2 * v - 1);
110
+ const r = radius * Math.cbrt(this.random.float());
111
+ const s = Math.sin(φ);
112
+ return [
113
+ r * s * Math.cos(θ),
114
+ r * s * Math.sin(θ),
115
+ r * Math.cos(φ)
116
+ ];
117
+ }
118
+ /** Normally distributed sample (Box-Muller), default mean 0 and standard deviation 1. */
119
+ gaussian(mean = 0, standardDeviation = 1) {
120
+ let u = 0;
121
+ let v = 0;
122
+ while (u === 0) u = this.random.float();
123
+ while (v === 0) v = this.random.float();
124
+ return mean + Math.sqrt(-2 * Math.log(u)) * Math.cos(TAU * v) * standardDeviation;
125
+ }
126
+ /**
127
+ * `rows` x `columns` array of uniform floats in [`min`, `max`) (default [0, 1)); `columns`
128
+ * defaults to `rows`.
129
+ */
130
+ matrix(rows, columns = rows, min = 0, max = 1) {
131
+ return Array.from({ length: rows }, () => Array.from({ length: columns }, () => min + this.random.float() * (max - min)));
132
+ }
133
+ /** Same as {@link Rng.matrix} with values in [-1, 1). */
134
+ transformMatrix(rows, columns = rows) {
135
+ return this.matrix(rows, columns, -1, 1);
136
+ }
137
+ /** Random color as `[r, g, b]`, each an integer in [0, 255]. */
138
+ rgb() {
139
+ return [
140
+ this.range(256),
141
+ this.range(256),
142
+ this.range(256)
143
+ ];
144
+ }
145
+ };
146
+ /**
147
+ * Shared {@link Rng} seeded from `Date.now()` at module load; set `rng.seed` for reproducible results.
148
+ * @category Math & Random
149
+ */
150
+ const rng = new Rng();
151
+ //#endregion
152
+ export { Rng, rng };
@@ -0,0 +1,34 @@
1
+ //#region src/signal.d.ts
2
+ /**
3
+ * A {@link Signal} subscriber: called with the new value.
4
+ *
5
+ * @category Events & Timing
6
+ */
7
+ export type Subscriber<T> = (value: T) => void;
8
+ /**
9
+ * A value holder that notifies subscribers when the value changes (compared with `Object.is`).
10
+ * Subscribers are called immediately with the current value when they subscribe.
11
+ *
12
+ * ```ts
13
+ * const health = new Signal(100);
14
+ * const unsubscribe = health.subscribe(value => console.log(value)); // logs 100
15
+ *
16
+ * health.set(80); // logs 80
17
+ * health.set(80); // unchanged: nothing logged
18
+ * unsubscribe();
19
+ * ```
20
+ *
21
+ * @category Events & Timing
22
+ */
23
+ export declare class Signal<T> {
24
+ private value;
25
+ private readonly subscribers;
26
+ constructor(initialValue: T);
27
+ /** Returns the current value. */
28
+ get(): T;
29
+ /** Sets the value and notifies subscribers; does nothing if it is the same (`Object.is`). */
30
+ set(value: T): void;
31
+ /** Adds a subscriber and calls it right away with the current value; returns a function that removes it. */
32
+ subscribe(subscriber: Subscriber<T>): () => void;
33
+ }
34
+ //#endregion
package/dist/signal.js ADDED
@@ -0,0 +1,41 @@
1
+ //#region src/signal.ts
2
+ /**
3
+ * A value holder that notifies subscribers when the value changes (compared with `Object.is`).
4
+ * Subscribers are called immediately with the current value when they subscribe.
5
+ *
6
+ * ```ts
7
+ * const health = new Signal(100);
8
+ * const unsubscribe = health.subscribe(value => console.log(value)); // logs 100
9
+ *
10
+ * health.set(80); // logs 80
11
+ * health.set(80); // unchanged: nothing logged
12
+ * unsubscribe();
13
+ * ```
14
+ *
15
+ * @category Events & Timing
16
+ */
17
+ var Signal = class {
18
+ value;
19
+ subscribers = /* @__PURE__ */ new Set();
20
+ constructor(initialValue) {
21
+ this.value = initialValue;
22
+ }
23
+ /** Returns the current value. */
24
+ get() {
25
+ return this.value;
26
+ }
27
+ /** Sets the value and notifies subscribers; does nothing if it is the same (`Object.is`). */
28
+ set(value) {
29
+ if (Object.is(this.value, value)) return;
30
+ this.value = value;
31
+ for (const subscriber of [...this.subscribers]) subscriber(value);
32
+ }
33
+ /** Adds a subscriber and calls it right away with the current value; returns a function that removes it. */
34
+ subscribe(subscriber) {
35
+ this.subscribers.add(subscriber);
36
+ subscriber(this.value);
37
+ return () => this.subscribers.delete(subscriber);
38
+ }
39
+ };
40
+ //#endregion
41
+ export { Signal };
@@ -0,0 +1,42 @@
1
+ //#region src/spatial-partition-2d.d.ts
2
+ /**
3
+ * Anything with a position that a {@link SpatialPartition2d} can bucket.
4
+ *
5
+ * @category Maps & Spatial
6
+ */
7
+ export interface SpatialPartitionEntity2d {
8
+ /** in [0, 1) */
9
+ x: number;
10
+ /** in [0, 1) */
11
+ y: number;
12
+ }
13
+ /**
14
+ * Uniform grid over the wrapping unit square [0, 1)², for finding nearby entities without
15
+ * comparing every pair. The grid has `ceil(1 / cellSize)` cells per axis.
16
+ *
17
+ * Entities are bucketed by position when added; the partition doesn't track movement, so move an
18
+ * entity between cells yourself (see {@link SpatialPartition2d.getCell}) when it changes cell.
19
+ *
20
+ * @category Maps & Spatial
21
+ */
22
+ export declare class SpatialPartition2d {
23
+ /** buckets, indexed `cells[cellY][cellX]` */
24
+ cells: SpatialPartitionEntity2d[][][];
25
+ /** cell edge length in unit-square units, e.g. 0.1 for a 10 x 10 grid */
26
+ cellSize: number;
27
+ /** the array passed to the constructor; `addEntity()` doesn't add to it */
28
+ entities: SpatialPartitionEntity2d[];
29
+ /** Builds the grid and adds every entity in `entities`. */
30
+ constructor(cellSize: number, entities: SpatialPartitionEntity2d[]);
31
+ /** Puts `entity` in the cell its current position falls in. */
32
+ addEntity(entity: SpatialPartitionEntity2d): void;
33
+ /** positions are expected in [0, 1); anything outside wraps around, matching getCellNeighbors */
34
+ getCell(x: number, y: number): SpatialPartitionEntity2d[];
35
+ /**
36
+ * The 3 x 3 block of cells around cell (`x`, `y`), including that cell, wrapping at the edges.
37
+ * Takes cell indices, not positions. Each cell appears once, so with fewer than 3 cells per
38
+ * axis the block has fewer than 9 cells.
39
+ */
40
+ getCellNeighbors(x: number, y: number): SpatialPartitionEntity2d[][];
41
+ }
42
+ //#endregion
@@ -0,0 +1,52 @@
1
+ import { mod } from "./math.js";
2
+ //#region src/spatial-partition-2d.ts
3
+ /**
4
+ * Uniform grid over the wrapping unit square [0, 1)², for finding nearby entities without
5
+ * comparing every pair. The grid has `ceil(1 / cellSize)` cells per axis.
6
+ *
7
+ * Entities are bucketed by position when added; the partition doesn't track movement, so move an
8
+ * entity between cells yourself (see {@link SpatialPartition2d.getCell}) when it changes cell.
9
+ *
10
+ * @category Maps & Spatial
11
+ */
12
+ var SpatialPartition2d = class {
13
+ /** buckets, indexed `cells[cellY][cellX]` */
14
+ cells;
15
+ /** cell edge length in unit-square units, e.g. 0.1 for a 10 x 10 grid */
16
+ cellSize;
17
+ /** the array passed to the constructor; `addEntity()` doesn't add to it */
18
+ entities;
19
+ /** Builds the grid and adds every entity in `entities`. */
20
+ constructor(cellSize, entities) {
21
+ this.cellSize = cellSize;
22
+ this.entities = entities;
23
+ this.cells = Array.from({ length: Math.ceil(1 / cellSize) }, () => Array.from({ length: Math.ceil(1 / cellSize) }, () => []));
24
+ for (const entity of this.entities) this.addEntity(entity);
25
+ }
26
+ /** Puts `entity` in the cell its current position falls in. */
27
+ addEntity(entity) {
28
+ this.getCell(entity.x, entity.y).push(entity);
29
+ }
30
+ /** positions are expected in [0, 1); anything outside wraps around, matching getCellNeighbors */
31
+ getCell(x, y) {
32
+ const cellX = mod(Math.floor(x / this.cellSize), this.cells[0].length);
33
+ const cellY = mod(Math.floor(y / this.cellSize), this.cells.length);
34
+ return this.cells[cellY][cellX];
35
+ }
36
+ /**
37
+ * The 3 x 3 block of cells around cell (`x`, `y`), including that cell, wrapping at the edges.
38
+ * Takes cell indices, not positions. Each cell appears once, so with fewer than 3 cells per
39
+ * axis the block has fewer than 9 cells.
40
+ */
41
+ getCellNeighbors(x, y) {
42
+ const neighbors = /* @__PURE__ */ new Set();
43
+ for (let dy = -1; dy <= 1; dy++) for (let dx = -1; dx <= 1; dx++) {
44
+ const nx = mod(x + dx, this.cells[0].length);
45
+ const ny = mod(y + dy, this.cells.length);
46
+ neighbors.add(this.cells[ny][nx]);
47
+ }
48
+ return [...neighbors];
49
+ }
50
+ };
51
+ //#endregion
52
+ export { SpatialPartition2d };