@n3rdw1z4rd/core 0.18.4 → 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.
- package/README.md +72 -10
- package/dist/astar.d.ts +44 -0
- package/dist/astar.js +118 -0
- package/dist/canvas-renderer.d.ts +154 -0
- package/dist/canvas-renderer.js +200 -0
- package/dist/clock.d.ts +69 -0
- package/dist/clock.js +108 -0
- package/dist/color-palette.d.ts +25 -0
- package/dist/color-palette.js +112 -0
- package/dist/color.d.ts +90 -0
- package/dist/color.js +224 -0
- package/dist/css/main.css +1 -3
- package/dist/disposable.d.ts +27 -0
- package/dist/disposable.js +32 -0
- package/dist/dungeon-generator.d.ts +142 -0
- package/dist/dungeon-generator.js +288 -0
- package/dist/ecs.d.ts +87 -0
- package/dist/ecs.js +101 -0
- package/dist/emitter.d.ts +40 -0
- package/dist/emitter.js +54 -0
- package/dist/event-queue.d.ts +29 -0
- package/dist/event-queue.js +40 -0
- package/dist/index.cjs +3813 -0
- package/dist/index.d.cts +2655 -0
- package/dist/index.d.ts +35 -873
- package/dist/index.js +34 -3986
- package/dist/input/action-map.d.ts +79 -0
- package/dist/input/action-map.js +131 -0
- package/dist/input/bindings.d.ts +273 -0
- package/dist/input/bindings.js +250 -0
- package/dist/input/gestures.d.ts +108 -0
- package/dist/input/gestures.js +159 -0
- package/dist/input/index.d.ts +5 -0
- package/dist/input/index.js +5 -0
- package/dist/input/input.d.ts +297 -0
- package/dist/input/input.js +695 -0
- package/dist/log.d.ts +8 -0
- package/dist/log.js +9 -0
- package/dist/map2d.d.ts +83 -0
- package/dist/map2d.js +120 -0
- package/dist/map3d-CHDJiq5T.d.cts +236 -0
- package/dist/map3d.d.ts +94 -0
- package/dist/map3d.js +148 -0
- package/dist/math.d.ts +176 -0
- package/dist/math.js +206 -0
- package/dist/misc.d.ts +46 -0
- package/dist/misc.js +71 -0
- package/dist/observable.d.ts +43 -0
- package/dist/observable.js +53 -0
- package/dist/particle-renderer.d.ts +47 -0
- package/dist/particle-renderer.js +82 -0
- package/dist/particle-system-2d.d.ts +60 -0
- package/dist/particle-system-2d.js +147 -0
- package/dist/random.d.ts +46 -0
- package/dist/random.js +74 -0
- package/dist/rectangle.d.ts +44 -0
- package/dist/rectangle.js +69 -0
- package/dist/rng.d.ts +72 -0
- package/dist/rng.js +152 -0
- package/dist/signal.d.ts +34 -0
- package/dist/signal.js +41 -0
- package/dist/spatial-partition-2d.d.ts +42 -0
- package/dist/spatial-partition-2d.js +52 -0
- package/dist/spatial-partition-3d-Ba-VBveM.cjs +1324 -0
- package/dist/spatial-partition-3d.d.ts +44 -0
- package/dist/spatial-partition-3d.js +57 -0
- package/dist/three/asteroid-mesh.d.ts +41 -0
- package/dist/three/asteroid-mesh.js +44 -0
- package/dist/three/camera-rig.d.ts +140 -0
- package/dist/three/camera-rig.js +197 -0
- package/dist/three/cube-sphere-geometry.d.ts +55 -0
- package/dist/three/cube-sphere-geometry.js +239 -0
- package/dist/three/ecs.d.ts +75 -0
- package/dist/three/ecs.js +64 -0
- package/dist/three/index.d.ts +12 -0
- package/dist/three/index.js +12 -0
- package/dist/three/particle-system-3d.d.ts +53 -0
- package/dist/three/particle-system-3d.js +224 -0
- package/dist/three/renderer.d.ts +38 -0
- package/dist/three/renderer.js +57 -0
- package/dist/three/starfield.d.ts +23 -0
- package/dist/three/starfield.js +64 -0
- package/dist/three/texture-atlas.d.ts +76 -0
- package/dist/three/texture-atlas.js +84 -0
- package/dist/three/texture-utils.d.ts +31 -0
- package/dist/three/texture-utils.js +44 -0
- package/dist/three/voxel-geometry.d.ts +72 -0
- package/dist/three/voxel-geometry.js +231 -0
- package/dist/three/voxel-mesh.d.ts +67 -0
- package/dist/three/voxel-mesh.js +71 -0
- package/dist/three.cjs +1307 -0
- package/dist/three.d.cts +655 -0
- package/dist/tilemap.d.ts +17 -0
- package/dist/tilemap.js +26 -0
- package/dist/types.d.ts +34 -0
- package/dist/types.js +0 -0
- package/dist/vector.d.ts +78 -0
- package/dist/vector.js +169 -0
- package/dist/webgl-renderer.d.ts +89 -0
- package/dist/webgl-renderer.js +140 -0
- package/dist/webgl.d.ts +357 -0
- package/dist/webgl.js +628 -0
- package/dist/worker-interface.d.ts +30 -0
- package/dist/worker-interface.js +39 -0
- package/package.json +55 -13
- package/dist/css/index.ts +0 -1
- package/dist/index.css +0 -104
- package/dist/index.d.mts +0 -873
- package/dist/index.mjs +0 -3982
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
//#region src/particle-renderer.ts
|
|
2
|
+
/**
|
|
3
|
+
* Double-buffered square-pixel renderer for particle effects: draw with `setPixel()` into an
|
|
4
|
+
* offscreen buffer, then `render()` copies the buffer to the visible canvas and clears it.
|
|
5
|
+
* Requires `OffscreenCanvas`. Used by {@link ParticleSystem2d}.
|
|
6
|
+
*
|
|
7
|
+
* ```ts
|
|
8
|
+
* const renderer = new ParticleRenderer();
|
|
9
|
+
* renderer.appendTo(document.body);
|
|
10
|
+
*
|
|
11
|
+
* renderer.setPixel(10, 10, Color.RED);
|
|
12
|
+
* renderer.render();
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* @category Canvas 2D
|
|
16
|
+
*/
|
|
17
|
+
var ParticleRenderer = class {
|
|
18
|
+
_screenCanvas;
|
|
19
|
+
_bufferCanvas;
|
|
20
|
+
/** context of the visible canvas */
|
|
21
|
+
screenContext;
|
|
22
|
+
/** context of the offscreen buffer that `setPixel()` draws into */
|
|
23
|
+
bufferContext;
|
|
24
|
+
/** canvas width in pixels */
|
|
25
|
+
get width() {
|
|
26
|
+
return this._screenCanvas.width;
|
|
27
|
+
}
|
|
28
|
+
/** canvas height in pixels */
|
|
29
|
+
get height() {
|
|
30
|
+
return this._screenCanvas.height;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* @param canvas the visible canvas; defaults to a new canvas element, not attached to the page
|
|
34
|
+
* @throws an `Error` if a 2D context can't be created
|
|
35
|
+
*/
|
|
36
|
+
constructor(canvas) {
|
|
37
|
+
this._screenCanvas = canvas ?? document.createElement("canvas");
|
|
38
|
+
this._bufferCanvas = new OffscreenCanvas(this._screenCanvas.width, this._screenCanvas.height);
|
|
39
|
+
const screenContext = this._screenCanvas.getContext("2d");
|
|
40
|
+
const bufferContext = this._bufferCanvas.getContext("2d");
|
|
41
|
+
if (!screenContext || !bufferContext) throw new Error("ParticleRenderer: failed to create CanvasRenderingContext2D");
|
|
42
|
+
this.screenContext = screenContext;
|
|
43
|
+
this.bufferContext = bufferContext;
|
|
44
|
+
}
|
|
45
|
+
/** Moves the canvas into `target` and, with `autoResize` (default true), resizes it to fit. */
|
|
46
|
+
appendTo(target, autoResize = true) {
|
|
47
|
+
if (this._screenCanvas.parentNode) this._screenCanvas.parentNode.removeChild(this._screenCanvas);
|
|
48
|
+
target.appendChild(this._screenCanvas);
|
|
49
|
+
if (autoResize) this.resize();
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Sets the canvas and buffer size in pixels (truncated to integers). A missing dimension
|
|
53
|
+
* defaults to the parent element's size (or the canvas's own size on the page). Resizing
|
|
54
|
+
* clears the buffer. Returns whether the size changed.
|
|
55
|
+
*/
|
|
56
|
+
resize(displayWidth, displayHeight) {
|
|
57
|
+
const { width, height } = this._screenCanvas.parentElement?.getBoundingClientRect() ?? this._screenCanvas.getBoundingClientRect();
|
|
58
|
+
displayWidth = 0 | (displayWidth ?? width);
|
|
59
|
+
displayHeight = 0 | (displayHeight ?? height);
|
|
60
|
+
if (this._screenCanvas.width !== displayWidth || this._screenCanvas.height !== displayHeight) {
|
|
61
|
+
this._screenCanvas.width = displayWidth;
|
|
62
|
+
this._screenCanvas.height = displayHeight;
|
|
63
|
+
this._bufferCanvas.width = displayWidth;
|
|
64
|
+
this._bufferCanvas.height = displayHeight;
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
/** Draws a filled `size` x `size` square (pixels, default 2) centered on (`x`, `y`) into the buffer. */
|
|
70
|
+
setPixel(x, y, color, size = 2) {
|
|
71
|
+
this.bufferContext.fillStyle = color.hexStr;
|
|
72
|
+
this.bufferContext.fillRect(x - size / 2, y - size / 2, size, size);
|
|
73
|
+
}
|
|
74
|
+
/** Replaces the visible canvas with the buffer's contents, then clears the buffer for the next frame. */
|
|
75
|
+
render() {
|
|
76
|
+
this.screenContext.clearRect(0, 0, this._screenCanvas.width, this._screenCanvas.height);
|
|
77
|
+
this.screenContext.drawImage(this._bufferCanvas, 0, 0);
|
|
78
|
+
this.bufferContext.clearRect(0, 0, this._screenCanvas.width, this._screenCanvas.height);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
//#endregion
|
|
82
|
+
export { ParticleRenderer };
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { ParticleRenderer } from "./particle-renderer.js";
|
|
2
|
+
//#region src/particle-system-2d.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* A `size` x `size` attraction matrix with values in [-1, 1). Deterministic: the same `size` and
|
|
5
|
+
* `seed` always give the same matrix.
|
|
6
|
+
*
|
|
7
|
+
* @category Particles
|
|
8
|
+
*/
|
|
9
|
+
export declare function randomMatrix(size: number, seed?: number): number[][];
|
|
10
|
+
/**
|
|
11
|
+
* A "particle life" simulation: 2000 particles of 5 colors attract or repel each other according
|
|
12
|
+
* to a seeded {@link randomMatrix} and are drawn with a {@link ParticleRenderer}. The world is the
|
|
13
|
+
* unit square [0, 1) and wraps at the edges (forces act across them too).
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* const renderer = new ParticleRenderer();
|
|
17
|
+
* const particles = new ParticleSystem2d(42);
|
|
18
|
+
* const clock = new Clock();
|
|
19
|
+
*
|
|
20
|
+
* renderer.appendTo(document.body);
|
|
21
|
+
*
|
|
22
|
+
* clock.start((dt) => {
|
|
23
|
+
* particles.update(renderer, dt);
|
|
24
|
+
* renderer.render();
|
|
25
|
+
* });
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @category Particles
|
|
29
|
+
*/
|
|
30
|
+
export declare class ParticleSystem2d {
|
|
31
|
+
/** number of particles (fixed) */
|
|
32
|
+
readonly particleCount = 2000;
|
|
33
|
+
/** the seed the starting state was generated from */
|
|
34
|
+
readonly seed: number;
|
|
35
|
+
/** seconds; larger `dt` values passed to `update()` are capped to this so a stall can't blow the simulation up */
|
|
36
|
+
maxDeltaTime: number;
|
|
37
|
+
private _particleSize;
|
|
38
|
+
private _frictionHalfLife;
|
|
39
|
+
private _range;
|
|
40
|
+
private _forceFactor;
|
|
41
|
+
private _attractionMatrix;
|
|
42
|
+
private _particles;
|
|
43
|
+
private _spatialPartition;
|
|
44
|
+
/**
|
|
45
|
+
* The same seed always produces the same starting state (positions, colors, attraction matrix).
|
|
46
|
+
*
|
|
47
|
+
* @param seed defaults to a random seed from the shared `rng`, so each instance differs
|
|
48
|
+
*/
|
|
49
|
+
constructor(seed?: number);
|
|
50
|
+
private _calcForce;
|
|
51
|
+
private _updateVelocities;
|
|
52
|
+
private _updatePositions;
|
|
53
|
+
/**
|
|
54
|
+
* Advances the simulation by `dt` seconds and draws every particle into the renderer's buffer
|
|
55
|
+
* with {@link ParticleRenderer.setPixel}. Does not call {@link ParticleRenderer.render}.
|
|
56
|
+
* `dt` is capped to {@link ParticleSystem2d.maxDeltaTime}.
|
|
57
|
+
*/
|
|
58
|
+
update(renderer: ParticleRenderer, dt: number): void;
|
|
59
|
+
}
|
|
60
|
+
//#endregion
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { fract } from "./math.js";
|
|
2
|
+
import { Color } from "./color.js";
|
|
3
|
+
import { Random } from "./random.js";
|
|
4
|
+
import { rng } from "./rng.js";
|
|
5
|
+
import { SpatialPartition2d } from "./spatial-partition-2d.js";
|
|
6
|
+
//#region src/particle-system-2d.ts
|
|
7
|
+
/**
|
|
8
|
+
* A `size` x `size` attraction matrix with values in [-1, 1). Deterministic: the same `size` and
|
|
9
|
+
* `seed` always give the same matrix.
|
|
10
|
+
*
|
|
11
|
+
* @category Particles
|
|
12
|
+
*/
|
|
13
|
+
function randomMatrix(size, seed = 0) {
|
|
14
|
+
const rows = [];
|
|
15
|
+
for (let i = 0; i < size; i++) {
|
|
16
|
+
const row = [];
|
|
17
|
+
for (let j = 0; j < size; j++) row.push(Random.float(seed, i, j) * 2 - 1);
|
|
18
|
+
rows.push(row);
|
|
19
|
+
}
|
|
20
|
+
return rows;
|
|
21
|
+
}
|
|
22
|
+
const colors = [
|
|
23
|
+
Color.RED,
|
|
24
|
+
Color.GREEN,
|
|
25
|
+
Color.BLUE,
|
|
26
|
+
Color.YELLOW,
|
|
27
|
+
Color.MAGENTA
|
|
28
|
+
];
|
|
29
|
+
/**
|
|
30
|
+
* A "particle life" simulation: 2000 particles of 5 colors attract or repel each other according
|
|
31
|
+
* to a seeded {@link randomMatrix} and are drawn with a {@link ParticleRenderer}. The world is the
|
|
32
|
+
* unit square [0, 1) and wraps at the edges (forces act across them too).
|
|
33
|
+
*
|
|
34
|
+
* ```ts
|
|
35
|
+
* const renderer = new ParticleRenderer();
|
|
36
|
+
* const particles = new ParticleSystem2d(42);
|
|
37
|
+
* const clock = new Clock();
|
|
38
|
+
*
|
|
39
|
+
* renderer.appendTo(document.body);
|
|
40
|
+
*
|
|
41
|
+
* clock.start((dt) => {
|
|
42
|
+
* particles.update(renderer, dt);
|
|
43
|
+
* renderer.render();
|
|
44
|
+
* });
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
47
|
+
* @category Particles
|
|
48
|
+
*/
|
|
49
|
+
var ParticleSystem2d = class {
|
|
50
|
+
/** number of particles (fixed) */
|
|
51
|
+
particleCount = 2e3;
|
|
52
|
+
/** the seed the starting state was generated from */
|
|
53
|
+
seed;
|
|
54
|
+
/** seconds; larger `dt` values passed to `update()` are capped to this so a stall can't blow the simulation up */
|
|
55
|
+
maxDeltaTime = .1;
|
|
56
|
+
_particleSize = 2;
|
|
57
|
+
_frictionHalfLife = .04;
|
|
58
|
+
_range = .1;
|
|
59
|
+
_forceFactor = 6;
|
|
60
|
+
_attractionMatrix;
|
|
61
|
+
_particles = [];
|
|
62
|
+
_spatialPartition;
|
|
63
|
+
/**
|
|
64
|
+
* The same seed always produces the same starting state (positions, colors, attraction matrix).
|
|
65
|
+
*
|
|
66
|
+
* @param seed defaults to a random seed from the shared `rng`, so each instance differs
|
|
67
|
+
*/
|
|
68
|
+
constructor(seed = rng.nextUint()) {
|
|
69
|
+
this.seed = seed;
|
|
70
|
+
this._attractionMatrix = randomMatrix(colors.length, seed);
|
|
71
|
+
for (let i = 0; i < this.particleCount; i++) this._particles.push({
|
|
72
|
+
x: Random.float(seed, i, 0),
|
|
73
|
+
y: Random.float(seed, i, 1),
|
|
74
|
+
vx: 0,
|
|
75
|
+
vy: 0,
|
|
76
|
+
color: Math.floor(Random.float(seed, i, 2) * colors.length)
|
|
77
|
+
});
|
|
78
|
+
this._spatialPartition = new SpatialPartition2d(this._range, this._particles);
|
|
79
|
+
}
|
|
80
|
+
_calcForce(r, a, beta = .3) {
|
|
81
|
+
let f = 0;
|
|
82
|
+
if (r < beta) f = r / beta - 1;
|
|
83
|
+
else if (beta < r && r < 1) f = a * (1 - Math.abs(2 * r - 1 - beta) / (1 - beta));
|
|
84
|
+
return f;
|
|
85
|
+
}
|
|
86
|
+
_updateVelocities(deltaTimeSeconds) {
|
|
87
|
+
const frictionFactor = Math.pow(.5, deltaTimeSeconds / this._frictionHalfLife);
|
|
88
|
+
for (let y = 0; y < this._spatialPartition.cells.length; y++) for (let x = 0; x < this._spatialPartition.cells[y].length; x++) {
|
|
89
|
+
const cell = this._spatialPartition.cells[y][x];
|
|
90
|
+
const neighborCells = this._spatialPartition.getCellNeighbors(x, y);
|
|
91
|
+
for (let i = 0; i < cell.length; i++) {
|
|
92
|
+
let fx = 0;
|
|
93
|
+
let fy = 0;
|
|
94
|
+
const p1 = cell[i];
|
|
95
|
+
for (const neighborCell of neighborCells) for (let j = 0; j < neighborCell.length; j++) {
|
|
96
|
+
if (cell === neighborCell && i === j) continue;
|
|
97
|
+
const p2 = neighborCell[j];
|
|
98
|
+
let rx = p2.x - p1.x;
|
|
99
|
+
if (Math.abs(rx) > .5) rx = rx > 0 ? rx - 1 : rx + 1;
|
|
100
|
+
let ry = p2.y - p1.y;
|
|
101
|
+
if (Math.abs(ry) > .5) ry = ry > 0 ? ry - 1 : ry + 1;
|
|
102
|
+
const d = Math.hypot(rx, ry);
|
|
103
|
+
if (d > 0 && d < this._range) {
|
|
104
|
+
const f = this._calcForce(d / this._range, this._attractionMatrix[p1.color][p2.color]);
|
|
105
|
+
fx += rx / d * f;
|
|
106
|
+
fy += ry / d * f;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
fx *= this._range * this._forceFactor * deltaTimeSeconds;
|
|
110
|
+
fy *= this._range * this._forceFactor * deltaTimeSeconds;
|
|
111
|
+
p1.vx *= frictionFactor;
|
|
112
|
+
p1.vy *= frictionFactor;
|
|
113
|
+
p1.vx += fx;
|
|
114
|
+
p1.vy += fy;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
_updatePositions(deltaTimeSeconds, renderer) {
|
|
119
|
+
for (let i = 0; i < this.particleCount; i++) {
|
|
120
|
+
const particle = this._particles[i];
|
|
121
|
+
const oldCell = this._spatialPartition.getCell(particle.x, particle.y);
|
|
122
|
+
particle.x += particle.vx * deltaTimeSeconds;
|
|
123
|
+
particle.y += particle.vy * deltaTimeSeconds;
|
|
124
|
+
particle.x = fract(particle.x);
|
|
125
|
+
particle.y = fract(particle.y);
|
|
126
|
+
renderer.setPixel(particle.x * renderer.width, particle.y * renderer.height, colors[particle.color], this._particleSize);
|
|
127
|
+
const newCell = this._spatialPartition.getCell(particle.x, particle.y);
|
|
128
|
+
if (newCell !== oldCell) {
|
|
129
|
+
const index = oldCell.indexOf(particle);
|
|
130
|
+
if (index !== -1) oldCell.splice(index, 1);
|
|
131
|
+
newCell.push(particle);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Advances the simulation by `dt` seconds and draws every particle into the renderer's buffer
|
|
137
|
+
* with {@link ParticleRenderer.setPixel}. Does not call {@link ParticleRenderer.render}.
|
|
138
|
+
* `dt` is capped to {@link ParticleSystem2d.maxDeltaTime}.
|
|
139
|
+
*/
|
|
140
|
+
update(renderer, dt) {
|
|
141
|
+
dt = Math.min(dt, this.maxDeltaTime);
|
|
142
|
+
this._updateVelocities(dt);
|
|
143
|
+
this._updatePositions(dt, renderer);
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
//#endregion
|
|
147
|
+
export { ParticleSystem2d, randomMatrix };
|
package/dist/random.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
//#region src/random.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Hash-based random numbers. The static methods are pure functions of their arguments (the same
|
|
4
|
+
* inputs always give the same result), which makes them handy for coordinate-based noise; an
|
|
5
|
+
* instance is a seeded sequence that hashes its seed and then increments it on every call.
|
|
6
|
+
*
|
|
7
|
+
* ```ts
|
|
8
|
+
* Random.float(x, y); // same value for the same (x, y), in [0, 1)
|
|
9
|
+
*
|
|
10
|
+
* const random = new Random(42);
|
|
11
|
+
* random.float(); // deterministic sequence for seed 42
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* See {@link Rng} for ranges, picking, shuffling and other helpers built on top of it.
|
|
15
|
+
* @category Math & Random
|
|
16
|
+
*/
|
|
17
|
+
export declare class Random {
|
|
18
|
+
private _seed;
|
|
19
|
+
/** @param seed truncated to a 32-bit integer, default `Date.now()` */
|
|
20
|
+
constructor(seed?: number);
|
|
21
|
+
/** the next value to be hashed; truncated to a 32-bit integer when set */
|
|
22
|
+
get seed(): number;
|
|
23
|
+
/** truncated to a 32-bit integer */
|
|
24
|
+
set seed(value: number);
|
|
25
|
+
/** Unsigned 32-bit integer in [0, 2³²) derived from `values` (a {@link hash32mix} hash). */
|
|
26
|
+
static uint(...values: number[]): number;
|
|
27
|
+
/** Float in [0, 1) derived from `values`. */
|
|
28
|
+
static float(...values: number[]): number;
|
|
29
|
+
/** Boolean derived from `values`. */
|
|
30
|
+
static bool(...values: number[]): boolean;
|
|
31
|
+
/** -1 or 1 derived from `values`. */
|
|
32
|
+
static sign(...values: number[]): -1 | 1;
|
|
33
|
+
/** Angle in radians in [0, 2π) derived from `values`. */
|
|
34
|
+
static angle(...values: number[]): number;
|
|
35
|
+
/** Next unsigned 32-bit integer in [0, 2³²); advances the seed. */
|
|
36
|
+
uint(): number;
|
|
37
|
+
/** Next float in [0, 1); advances the seed. */
|
|
38
|
+
float(): number;
|
|
39
|
+
/** Next boolean; advances the seed. */
|
|
40
|
+
bool(): boolean;
|
|
41
|
+
/** Next -1 or 1; advances the seed. */
|
|
42
|
+
sign(): -1 | 1;
|
|
43
|
+
/** Next angle in radians in [0, 2π); advances the seed. */
|
|
44
|
+
angle(): number;
|
|
45
|
+
}
|
|
46
|
+
//#endregion
|
package/dist/random.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { TAU, hash32mix } from "./math.js";
|
|
2
|
+
//#region src/random.ts
|
|
3
|
+
/**
|
|
4
|
+
* Hash-based random numbers. The static methods are pure functions of their arguments (the same
|
|
5
|
+
* inputs always give the same result), which makes them handy for coordinate-based noise; an
|
|
6
|
+
* instance is a seeded sequence that hashes its seed and then increments it on every call.
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* Random.float(x, y); // same value for the same (x, y), in [0, 1)
|
|
10
|
+
*
|
|
11
|
+
* const random = new Random(42);
|
|
12
|
+
* random.float(); // deterministic sequence for seed 42
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* See {@link Rng} for ranges, picking, shuffling and other helpers built on top of it.
|
|
16
|
+
* @category Math & Random
|
|
17
|
+
*/
|
|
18
|
+
var Random = class Random {
|
|
19
|
+
_seed;
|
|
20
|
+
/** @param seed truncated to a 32-bit integer, default `Date.now()` */
|
|
21
|
+
constructor(seed = Date.now()) {
|
|
22
|
+
this._seed = seed | 0;
|
|
23
|
+
}
|
|
24
|
+
/** the next value to be hashed; truncated to a 32-bit integer when set */
|
|
25
|
+
get seed() {
|
|
26
|
+
return this._seed;
|
|
27
|
+
}
|
|
28
|
+
/** truncated to a 32-bit integer */
|
|
29
|
+
set seed(value) {
|
|
30
|
+
this._seed = value | 0;
|
|
31
|
+
}
|
|
32
|
+
/** Unsigned 32-bit integer in [0, 2³²) derived from `values` (a {@link hash32mix} hash). */
|
|
33
|
+
static uint(...values) {
|
|
34
|
+
return hash32mix(...values);
|
|
35
|
+
}
|
|
36
|
+
/** Float in [0, 1) derived from `values`. */
|
|
37
|
+
static float(...values) {
|
|
38
|
+
return hash32mix(...values) / 4294967296;
|
|
39
|
+
}
|
|
40
|
+
/** Boolean derived from `values`. */
|
|
41
|
+
static bool(...values) {
|
|
42
|
+
return (hash32mix(...values) & 1) !== 0;
|
|
43
|
+
}
|
|
44
|
+
/** -1 or 1 derived from `values`. */
|
|
45
|
+
static sign(...values) {
|
|
46
|
+
return this.bool(...values) ? 1 : -1;
|
|
47
|
+
}
|
|
48
|
+
/** Angle in radians in [0, 2π) derived from `values`. */
|
|
49
|
+
static angle(...values) {
|
|
50
|
+
return this.float(...values) * TAU;
|
|
51
|
+
}
|
|
52
|
+
/** Next unsigned 32-bit integer in [0, 2³²); advances the seed. */
|
|
53
|
+
uint() {
|
|
54
|
+
return Random.uint(this._seed++);
|
|
55
|
+
}
|
|
56
|
+
/** Next float in [0, 1); advances the seed. */
|
|
57
|
+
float() {
|
|
58
|
+
return Random.float(this._seed++);
|
|
59
|
+
}
|
|
60
|
+
/** Next boolean; advances the seed. */
|
|
61
|
+
bool() {
|
|
62
|
+
return Random.bool(this._seed++);
|
|
63
|
+
}
|
|
64
|
+
/** Next -1 or 1; advances the seed. */
|
|
65
|
+
sign() {
|
|
66
|
+
return Random.sign(this._seed++);
|
|
67
|
+
}
|
|
68
|
+
/** Next angle in radians in [0, 2π); advances the seed. */
|
|
69
|
+
angle() {
|
|
70
|
+
return Random.angle(this._seed++);
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
//#endregion
|
|
74
|
+
export { Random };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Vector } from "./vector.js";
|
|
2
|
+
//#region src/rectangle.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* Axis-aligned rectangle given by its top-left corner (`x`, `y`) and size. Edges are inclusive
|
|
5
|
+
* in the containment and intersection tests.
|
|
6
|
+
* @category Math & Random
|
|
7
|
+
*/
|
|
8
|
+
export declare class Rectangle {
|
|
9
|
+
/** left edge */
|
|
10
|
+
x: number;
|
|
11
|
+
/** top edge (minimum y; y points down) */
|
|
12
|
+
y: number;
|
|
13
|
+
/** extent along x from `x` */
|
|
14
|
+
width: number;
|
|
15
|
+
/** extent along y from `y` */
|
|
16
|
+
height: number;
|
|
17
|
+
/** Defaults to a 1 x 1 rectangle at the origin. */
|
|
18
|
+
constructor(x?: number, y?: number, width?: number, height?: number);
|
|
19
|
+
/** `width * height`. */
|
|
20
|
+
area(): number;
|
|
21
|
+
/**
|
|
22
|
+
* True if `target` (a point, or a whole rectangle) is inside or on the edge, with this rectangle
|
|
23
|
+
* grown by `padding` on every side (negative shrinks).
|
|
24
|
+
*/
|
|
25
|
+
contains(target: Vector | Rectangle, padding?: number): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* True if the rectangles overlap or touch, or are at most `padding` apart (negative requires
|
|
28
|
+
* that much overlap).
|
|
29
|
+
*/
|
|
30
|
+
intersects(other: Rectangle, padding?: number): boolean;
|
|
31
|
+
/** A new vector at the rectangle's centre. */
|
|
32
|
+
center(): Vector;
|
|
33
|
+
/** Distance between the two rectangles' centres. */
|
|
34
|
+
distanceTo(other: Rectangle): number;
|
|
35
|
+
/** A new rectangle with the same position and size. */
|
|
36
|
+
clone(): Rectangle;
|
|
37
|
+
/** Exact (`===`) equality of position and size. */
|
|
38
|
+
equals(other: Rectangle): boolean;
|
|
39
|
+
/** A new rectangle expanded by `padding` on every side (negative shrinks); this one is unchanged. */
|
|
40
|
+
grow(padding: number): Rectangle;
|
|
41
|
+
/** Same as {@link Rectangle.contains} with a point. */
|
|
42
|
+
vectorInside(vec: Vector, padding?: number): boolean;
|
|
43
|
+
}
|
|
44
|
+
//#endregion
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { Vector } from "./vector.js";
|
|
2
|
+
//#region src/rectangle.ts
|
|
3
|
+
/**
|
|
4
|
+
* Axis-aligned rectangle given by its top-left corner (`x`, `y`) and size. Edges are inclusive
|
|
5
|
+
* in the containment and intersection tests.
|
|
6
|
+
* @category Math & Random
|
|
7
|
+
*/
|
|
8
|
+
var Rectangle = class Rectangle {
|
|
9
|
+
/** left edge */
|
|
10
|
+
x;
|
|
11
|
+
/** top edge (minimum y; y points down) */
|
|
12
|
+
y;
|
|
13
|
+
/** extent along x from `x` */
|
|
14
|
+
width;
|
|
15
|
+
/** extent along y from `y` */
|
|
16
|
+
height;
|
|
17
|
+
/** Defaults to a 1 x 1 rectangle at the origin. */
|
|
18
|
+
constructor(x = 0, y = 0, width = 1, height = 1) {
|
|
19
|
+
this.x = x;
|
|
20
|
+
this.y = y;
|
|
21
|
+
this.width = width;
|
|
22
|
+
this.height = height;
|
|
23
|
+
}
|
|
24
|
+
/** `width * height`. */
|
|
25
|
+
area() {
|
|
26
|
+
return this.width * this.height;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* True if `target` (a point, or a whole rectangle) is inside or on the edge, with this rectangle
|
|
30
|
+
* grown by `padding` on every side (negative shrinks).
|
|
31
|
+
*/
|
|
32
|
+
contains(target, padding = 0) {
|
|
33
|
+
const [width, height] = target instanceof Vector ? [0, 0] : [target.width, target.height];
|
|
34
|
+
return target.x >= this.x - padding && target.y >= this.y - padding && target.x + width <= this.x + this.width + padding && target.y + height <= this.y + this.height + padding;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* True if the rectangles overlap or touch, or are at most `padding` apart (negative requires
|
|
38
|
+
* that much overlap).
|
|
39
|
+
*/
|
|
40
|
+
intersects(other, padding = 0) {
|
|
41
|
+
return !(other.x > this.x + this.width + padding || other.x + other.width < this.x - padding || other.y > this.y + this.height + padding || other.y + other.height < this.y - padding);
|
|
42
|
+
}
|
|
43
|
+
/** A new vector at the rectangle's centre. */
|
|
44
|
+
center() {
|
|
45
|
+
return new Vector(this.x + this.width / 2, this.y + this.height / 2);
|
|
46
|
+
}
|
|
47
|
+
/** Distance between the two rectangles' centres. */
|
|
48
|
+
distanceTo(other) {
|
|
49
|
+
return this.center().distanceTo(other.center());
|
|
50
|
+
}
|
|
51
|
+
/** A new rectangle with the same position and size. */
|
|
52
|
+
clone() {
|
|
53
|
+
return new Rectangle(this.x, this.y, this.width, this.height);
|
|
54
|
+
}
|
|
55
|
+
/** Exact (`===`) equality of position and size. */
|
|
56
|
+
equals(other) {
|
|
57
|
+
return this.x === other.x && this.y === other.y && this.width === other.width && this.height === other.height;
|
|
58
|
+
}
|
|
59
|
+
/** A new rectangle expanded by `padding` on every side (negative shrinks); this one is unchanged. */
|
|
60
|
+
grow(padding) {
|
|
61
|
+
return new Rectangle(this.x - padding, this.y - padding, this.width + padding * 2, this.height + padding * 2);
|
|
62
|
+
}
|
|
63
|
+
/** Same as {@link Rectangle.contains} with a point. */
|
|
64
|
+
vectorInside(vec, padding = 0) {
|
|
65
|
+
return this.contains(vec, padding);
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
//#endregion
|
|
69
|
+
export { Rectangle };
|
package/dist/rng.d.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { Random } from "./random.js";
|
|
2
|
+
//#region src/rng.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* Seeded random number generator with helpers for ranges, picking, shuffling, geometric points,
|
|
5
|
+
* Gaussian samples and matrices. Every call advances the underlying {@link Random}, so a given
|
|
6
|
+
* seed always reproduces the same sequence of calls.
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* const r = new Rng(1234);
|
|
10
|
+
* r.range(1, 7); // integer in [1, 7), like a die roll
|
|
11
|
+
* r.pick(['a', 'b', 'c']);
|
|
12
|
+
* r.shuffle([1, 2, 3, 4]); // new array, input untouched
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* A shared, time-seeded instance is exported as {@link rng}.
|
|
16
|
+
* @category Math & Random
|
|
17
|
+
*/
|
|
18
|
+
export declare class Rng {
|
|
19
|
+
/** the underlying generator; shared if one was passed to the constructor */
|
|
20
|
+
readonly random: Random;
|
|
21
|
+
/** the underlying {@link Random}'s current seed; setting it restarts the sequence */
|
|
22
|
+
get seed(): number;
|
|
23
|
+
/** sets the underlying {@link Random}'s seed */
|
|
24
|
+
set seed(seed: number);
|
|
25
|
+
/** @param seed a seed (default `Date.now()`), or an existing {@link Random} to wrap */
|
|
26
|
+
constructor(seed?: number | Random);
|
|
27
|
+
/** Float in [0, 1). */
|
|
28
|
+
nextFloat(): number;
|
|
29
|
+
/** Unsigned 32-bit integer in [0, 2³²). */
|
|
30
|
+
nextUint(): number;
|
|
31
|
+
/**
|
|
32
|
+
* random integer in [min, max); range(max) is [0, max). Non-integer bounds are rounded up,
|
|
33
|
+
* so the result is always an integer inside the requested range.
|
|
34
|
+
*/
|
|
35
|
+
range(min: number, max?: number): number;
|
|
36
|
+
/** A uniformly chosen element of `array`; `undefined` if the array is empty. */
|
|
37
|
+
pick<T>(array: readonly [T, ...T[]]): T;
|
|
38
|
+
pick<T>(array: readonly T[]): T | undefined;
|
|
39
|
+
/** Up to `count` distinct elements of `array` in random order (all of them if `count` exceeds its length). */
|
|
40
|
+
sample<T>(array: readonly T[], count: number): T[];
|
|
41
|
+
/** A shuffled copy of `array` (Fisher-Yates); the input is not modified. */
|
|
42
|
+
shuffle<T>(array: readonly T[]): T[];
|
|
43
|
+
/** True with the given probability (0 = never, 1 = always). */
|
|
44
|
+
chance(probability: number): boolean;
|
|
45
|
+
/** Random unit-length 2D direction as `[x, y]`. */
|
|
46
|
+
vector2(): [number, number];
|
|
47
|
+
/** Uniform point on a circle of `radius` (default 1) centred on the origin, as `[x, y]`. */
|
|
48
|
+
pointOnCircle(radius?: number): [number, number];
|
|
49
|
+
/** Uniform point inside a disc of `radius` (default 1) centred on the origin, as `[x, y]`. */
|
|
50
|
+
pointInCircle(radius?: number): [number, number];
|
|
51
|
+
/** Uniform point on the surface of a sphere of `radius` (default 1) centred on the origin, as `[x, y, z]`. */
|
|
52
|
+
pointOnSphere(radius?: number): [number, number, number];
|
|
53
|
+
/** Uniform point inside a ball of `radius` (default 1) centred on the origin, as `[x, y, z]`. */
|
|
54
|
+
pointInSphere(radius?: number): [number, number, number];
|
|
55
|
+
/** Normally distributed sample (Box-Muller), default mean 0 and standard deviation 1. */
|
|
56
|
+
gaussian(mean?: number, standardDeviation?: number): number;
|
|
57
|
+
/**
|
|
58
|
+
* `rows` x `columns` array of uniform floats in [`min`, `max`) (default [0, 1)); `columns`
|
|
59
|
+
* defaults to `rows`.
|
|
60
|
+
*/
|
|
61
|
+
matrix(rows: number, columns?: number, min?: number, max?: number): number[][];
|
|
62
|
+
/** Same as {@link Rng.matrix} with values in [-1, 1). */
|
|
63
|
+
transformMatrix(rows: number, columns?: number): number[][];
|
|
64
|
+
/** Random color as `[r, g, b]`, each an integer in [0, 255]. */
|
|
65
|
+
rgb(): [number, number, number];
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Shared {@link Rng} seeded from `Date.now()` at module load; set `rng.seed` for reproducible results.
|
|
69
|
+
* @category Math & Random
|
|
70
|
+
*/
|
|
71
|
+
export declare const rng: Rng;
|
|
72
|
+
//#endregion
|