@n3rdw1z4rd/core 0.16.3 → 0.17.1

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/dist/index.js CHANGED
@@ -5,6 +5,12 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
6
  var __getProtoOf = Object.getPrototypeOf;
7
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __esm = (fn, res) => function __init() {
9
+ return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
10
+ };
11
+ var __commonJS = (cb, mod2) => function __require() {
12
+ return mod2 || (0, cb[__getOwnPropNames(cb)[0]])((mod2 = { exports: {} }).exports, mod2), mod2.exports;
13
+ };
8
14
  var __export = (target, all) => {
9
15
  for (var name in all)
10
16
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -17,15 +23,30 @@ var __copyProps = (to, from, except, desc) => {
17
23
  }
18
24
  return to;
19
25
  };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
26
+ var __reExport = (target, mod2, secondTarget) => (__copyProps(target, mod2, "default"), secondTarget && __copyProps(secondTarget, mod2, "default"));
27
+ var __toESM = (mod2, isNodeMode, target) => (target = mod2 != null ? __create(__getProtoOf(mod2)) : {}, __copyProps(
21
28
  // If the importer is in node compatibility mode or this is not an ESM
22
29
  // file that has been converted to a CommonJS file using a Babel-
23
30
  // compatible transform (i.e. "__esModule" has not been set), then set
24
31
  // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
32
+ isNodeMode || !mod2 || !mod2.__esModule ? __defProp(target, "default", { value: mod2, enumerable: true }) : target,
33
+ mod2
27
34
  ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
35
+ var __toCommonJS = (mod2) => __copyProps(__defProp({}, "__esModule", { value: true }), mod2);
36
+
37
+ // src/css/main.css
38
+ var init_main = __esm({
39
+ "src/css/main.css"() {
40
+ }
41
+ });
42
+
43
+ // src/css/index.ts
44
+ var require_css = __commonJS({
45
+ "src/css/index.ts"() {
46
+ "use strict";
47
+ init_main();
48
+ }
49
+ });
29
50
 
30
51
  // src/index.ts
31
52
  var index_exports = {};
@@ -49,9 +70,18 @@ __export(index_exports, {
49
70
  CreateThreeEntity: () => CreateThreeEntity,
50
71
  CreateWebGlContext: () => CreateWebGlContext,
51
72
  CubeSphereGeometry: () => CubeSphereGeometry,
52
- DOUBLE_PI: () => DOUBLE_PI,
73
+ DEFAULT_COLOR: () => DEFAULT_COLOR,
74
+ DEFAULT_FONT: () => DEFAULT_FONT,
75
+ DEFAULT_PIXEL_SIZE: () => DEFAULT_PIXEL_SIZE,
76
+ DEFAULT_TEXTALIGN: () => DEFAULT_TEXTALIGN,
77
+ DEFAULT_TEXTBASELINE: () => DEFAULT_TEXTBASELINE,
78
+ DEG2RAD: () => DEG2RAD,
79
+ DOWN: () => DOWN,
80
+ Disposable: () => Disposable,
81
+ E: () => E,
53
82
  ECS: () => ECS,
54
- Emitter: () => Emitter,
83
+ EPSILON: () => EPSILON,
84
+ EventQueue: () => EventQueue,
55
85
  GenerateDoors: () => GenerateDoors,
56
86
  GenerateEntityId: () => GenerateEntityId,
57
87
  GenerateRooms: () => GenerateRooms,
@@ -62,24 +92,35 @@ __export(index_exports, {
62
92
  HALF_PI: () => HALF_PI,
63
93
  Input: () => Input,
64
94
  IsThreeEntity: () => IsThreeEntity,
95
+ LN10: () => LN10,
96
+ LN2: () => LN2,
97
+ LOG10E: () => LOG10E,
98
+ LOG2E: () => LOG2E,
65
99
  LoadTexture: () => LoadTexture,
66
100
  Map2D: () => Map2D,
67
101
  Map3D: () => Map3D,
68
102
  Noise: () => Noise,
103
+ Observable: () => Observable,
69
104
  PI: () => PI,
70
105
  ParticleRenderer: () => ParticleRenderer,
71
106
  ParticleSystem2d: () => ParticleSystem2d,
72
107
  ParticleSystem3d: () => ParticleSystem3d,
73
108
  PoissonDiskSampler: () => PoissonDiskSampler,
74
- RADIAN_FACTOR: () => RADIAN_FACTOR,
75
- RandomNumberGenerator: () => RandomNumberGenerator,
109
+ QUARTER_PI: () => QUARTER_PI,
110
+ RAD2DEG: () => RAD2DEG,
111
+ Random: () => Random,
112
+ RandomTools: () => RandomTools,
76
113
  Rectangle: () => Rectangle,
77
114
  RemoveThreeComponent: () => RemoveThreeComponent,
78
115
  ResizeWebGlContext: () => ResizeWebGlContext,
116
+ SQRT1_2: () => SQRT1_2,
117
+ SQRT2: () => SQRT2,
79
118
  ShaderType: () => ShaderType,
119
+ Signal: () => Signal,
80
120
  SimplexNoise: () => SimplexNoise,
81
121
  SpatialPartition2d: () => SpatialPartition2d,
82
122
  SpatialPartition3d: () => SpatialPartition3d,
123
+ TAU: () => TAU,
83
124
  TILE_EMPTY: () => TILE_EMPTY,
84
125
  TILE_FLOOR: () => TILE_FLOOR,
85
126
  TILE_PATH: () => TILE_PATH,
@@ -88,44 +129,161 @@ __export(index_exports, {
88
129
  ThreeJsCameraRig: () => ThreeJsCameraRig,
89
130
  ThreeJsPlayerController: () => ThreeJsPlayerController,
90
131
  Tilemap: () => Tilemap,
132
+ UP: () => UP,
91
133
  Vector: () => Vector,
92
134
  VoxelFaces: () => VoxelFaces,
93
135
  VoxelMesh: () => VoxelMesh,
94
136
  WebGlRenderer: () => WebGlRenderer,
95
137
  WorkerInterface: () => WorkerInterface,
96
138
  abs: () => abs,
139
+ acos: () => acos,
140
+ asin: () => asin,
141
+ atan: () => atan,
142
+ atan2: () => atan2,
143
+ cbrt: () => cbrt,
97
144
  ceil: () => ceil,
98
145
  clamp: () => clamp,
99
146
  cos: () => cos,
100
- deg2rad: () => deg2rad,
147
+ cosh: () => cosh,
148
+ degrees: () => degrees,
101
149
  distance2d: () => distance2d,
102
150
  distance3d: () => distance3d,
151
+ exp: () => exp,
152
+ expm1: () => expm1,
103
153
  floor: () => floor,
154
+ fract: () => fract,
104
155
  getRedYellowGreenGradientHex: () => getRedYellowGreenGradientHex,
156
+ hypot: () => hypot,
105
157
  hypot2: () => hypot2,
106
158
  hypot3: () => hypot3,
107
159
  imul: () => imul,
160
+ inverseLerp: () => inverseLerp,
161
+ isFinite: () => isFinite,
162
+ isInteger: () => isInteger,
163
+ isNaN: () => isNaN,
108
164
  isNullOrUndefined: () => isNullOrUndefined,
109
165
  isPositiveString: () => isPositiveString,
110
166
  k2xy: () => k2xy,
111
167
  k2xyz: () => k2xyz,
112
168
  lerp: () => lerp,
113
169
  log: () => log,
170
+ log10: () => log10,
171
+ log1p: () => log1p,
172
+ log2: () => log2,
114
173
  manhattanDistance: () => manhattanDistance,
174
+ map: () => map,
115
175
  max: () => max,
116
176
  min: () => min,
177
+ mod: () => mod,
178
+ nearlyEqual: () => nearlyEqual,
179
+ normalizeRadians: () => normalizeRadians,
117
180
  pow: () => pow,
181
+ radians: () => radians,
182
+ randomMatrix: () => randomMatrix,
118
183
  rng: () => rng,
119
184
  round: () => round,
120
185
  roundTo: () => roundTo,
186
+ saturate: () => saturate,
187
+ sign: () => sign,
121
188
  sin: () => sin,
189
+ sinh: () => sinh,
190
+ smoothstep: () => smoothstep,
122
191
  sqrt: () => sqrt,
123
192
  squaredDistance: () => squaredDistance,
193
+ tan: () => tan,
194
+ tanh: () => tanh,
195
+ trunc: () => trunc,
196
+ wrap: () => wrap,
124
197
  xy2k: () => xy2k,
125
198
  xyz2k: () => xyz2k
126
199
  });
127
200
  module.exports = __toCommonJS(index_exports);
128
201
 
202
+ // src/math.ts
203
+ var PI = Math.PI;
204
+ var TAU = Math.PI * 2;
205
+ var HALF_PI = Math.PI / 2;
206
+ var QUARTER_PI = Math.PI / 4;
207
+ var E = Math.E;
208
+ var DEG2RAD = PI / 180;
209
+ var RAD2DEG = 180 / PI;
210
+ var LN2 = Math.LN2;
211
+ var LN10 = Math.LN10;
212
+ var LOG2E = Math.LOG2E;
213
+ var LOG10E = Math.LOG10E;
214
+ var SQRT2 = Math.SQRT2;
215
+ var SQRT1_2 = Math.SQRT1_2;
216
+ var EPSILON = Number.EPSILON;
217
+ var abs = Math.abs;
218
+ var min = Math.min;
219
+ var max = Math.max;
220
+ var floor = Math.floor;
221
+ var ceil = Math.ceil;
222
+ var round = Math.round;
223
+ var trunc = Math.trunc;
224
+ var sign = Math.sign;
225
+ var sqrt = Math.sqrt;
226
+ var cbrt = Math.cbrt;
227
+ var pow = Math.pow;
228
+ var exp = Math.exp;
229
+ var expm1 = Math.expm1;
230
+ var log = Math.log;
231
+ var log1p = Math.log1p;
232
+ var log2 = Math.log2;
233
+ var log10 = Math.log10;
234
+ var hypot = Math.hypot;
235
+ var imul = Math.imul;
236
+ var sin = Math.sin;
237
+ var cos = Math.cos;
238
+ var tan = Math.tan;
239
+ var asin = Math.asin;
240
+ var acos = Math.acos;
241
+ var atan = Math.atan;
242
+ var atan2 = Math.atan2;
243
+ var sinh = Math.sinh;
244
+ var cosh = Math.cosh;
245
+ var tanh = Math.tanh;
246
+ var clamp = (value, minValue, maxValue) => min(max(value, minValue), maxValue);
247
+ var saturate = (value) => clamp(value, 0, 1);
248
+ var lerp = (start, end, t) => start + (end - start) * t;
249
+ var inverseLerp = (start, end, value) => (value - start) / (end - start);
250
+ var map = (value, inMin, inMax, outMin, outMax) => outMin + (value - inMin) * (outMax - outMin) / (inMax - inMin);
251
+ var smoothstep = (edge0, edge1, value) => {
252
+ const t = saturate((value - edge0) / (edge1 - edge0));
253
+ return t * t * (3 - 2 * t);
254
+ };
255
+ var fract = (value) => value - floor(value);
256
+ var mod = (value, divisor) => (value % divisor + divisor) % divisor;
257
+ var wrap = (value, minValue, maxValue) => {
258
+ const range = maxValue - minValue;
259
+ return minValue + mod(value - minValue, range);
260
+ };
261
+ var radians = (degrees2) => degrees2 * DEG2RAD;
262
+ var degrees = (radians2) => radians2 * RAD2DEG;
263
+ var normalizeRadians = (radians2) => wrap(radians2, -PI, PI);
264
+ var hypot2 = (x, y) => hypot(x, y);
265
+ var hypot3 = (x, y, z) => hypot(x, y, z);
266
+ var distance2d = (x1, y1, x2, y2) => hypot(x2 - x1, y2 - y1);
267
+ var distance3d = (x1, y1, z1, x2, y2, z2) => hypot(
268
+ x2 - x1,
269
+ y2 - y1,
270
+ z2 - z1
271
+ );
272
+ var squaredDistance = (x1, y1, x2, y2) => {
273
+ const dx = x2 - x1;
274
+ const dy = y2 - y1;
275
+ return dx * dx + dy * dy;
276
+ };
277
+ var manhattanDistance = (x1, y1, x2, y2) => abs(x2 - x1) + abs(y2 - y1);
278
+ var nearlyEqual = (a, b, epsilon = EPSILON) => abs(a - b) <= epsilon;
279
+ var roundTo = (value, digits = 0) => {
280
+ const factor = 10 ** digits;
281
+ return round(value * factor) / factor;
282
+ };
283
+ var isFinite = Number.isFinite;
284
+ var isInteger = Number.isInteger;
285
+ var isNaN = Number.isNaN;
286
+
129
287
  // src/astar.ts
130
288
  var DIRECTIONS_4_WAY = [
131
289
  { x: 0, y: -1 },
@@ -135,11 +293,11 @@ var DIRECTIONS_4_WAY = [
135
293
  ];
136
294
  var AStar = class {
137
295
  map;
138
- constructor(map) {
139
- this.map = map;
296
+ constructor(map2) {
297
+ this.map = map2;
140
298
  }
141
299
  _manhattan(a, b) {
142
- return Math.abs(a.x - b.x) + Math.abs(a.y - b.y);
300
+ return abs(a.x - b.x) + abs(a.y - b.y);
143
301
  }
144
302
  _isWalkable(point, walkable = [0]) {
145
303
  const value = this.map.get(point.x, point.y);
@@ -218,38 +376,35 @@ var AStar = class {
218
376
  }
219
377
  };
220
378
 
221
- // src/emitter.ts
222
- var Emitter = class {
223
- listeners = /* @__PURE__ */ new Map();
224
- constructor() {
225
- }
226
- on(...eventName) {
227
- const listener = eventName.pop();
228
- const events = eventName.filter((ev) => typeof ev === "string");
229
- events.forEach((event) => {
230
- if (!this.listeners.has(event)) {
231
- this.listeners.set(event, new Array());
232
- }
233
- this.listeners.get(event)?.push(listener);
379
+ // src/observable.ts
380
+ var Observable = class {
381
+ observers = /* @__PURE__ */ new Set();
382
+ subscribe(observer) {
383
+ this.observers.add(observer);
384
+ return () => this.observers.delete(observer);
385
+ }
386
+ once(observer, ...args) {
387
+ const unsubscribe = this.subscribe((value) => {
388
+ unsubscribe();
389
+ observer(value, ...args);
234
390
  });
235
- return this;
236
- }
237
- emit(eventName, ...args) {
238
- this.listeners.get(eventName)?.forEach((listener) => listener(...args, eventName));
239
- return this;
391
+ return unsubscribe;
240
392
  }
241
- clear(eventName) {
242
- if (eventName) {
243
- this.listeners.delete(eventName);
244
- } else {
245
- this.listeners.clear();
393
+ notify(value, ...args) {
394
+ for (const observer of [...this.observers]) {
395
+ observer(value, ...args);
246
396
  }
247
- return this;
397
+ }
398
+ clear() {
399
+ this.observers.clear();
400
+ }
401
+ get size() {
402
+ return this.observers.size;
248
403
  }
249
404
  };
250
405
 
251
406
  // src/clock.ts
252
- var Clock = class extends Emitter {
407
+ var Clock = class {
253
408
  _startTime = 0;
254
409
  _time = 0;
255
410
  _deltaTime = 0;
@@ -272,6 +427,7 @@ var Clock = class extends Emitter {
272
427
  get isRunning() {
273
428
  return this._isRunning;
274
429
  }
430
+ onFrame = new Observable();
275
431
  start() {
276
432
  if (!this._isRunning) {
277
433
  this._startTime = performance.now();
@@ -286,7 +442,7 @@ var Clock = class extends Emitter {
286
442
  this._fps = this._frameCount;
287
443
  this._frameCount = 0;
288
444
  }
289
- this.emit("frame", this._deltaTime);
445
+ this.onFrame.notify(this);
290
446
  if (this._isRunning) {
291
447
  requestAnimationFrame(update);
292
448
  }
@@ -300,135 +456,294 @@ var Clock = class extends Emitter {
300
456
  }
301
457
  };
302
458
 
303
- // src/math/math.ts
304
- var PI = 3.141592653589793;
305
- var DOUBLE_PI = PI * 2;
306
- var HALF_PI = PI / 2;
307
- var RADIAN_FACTOR = PI / 180;
308
- var abs = (n) => n < 0 ? -n : n;
309
- var min = (a, b) => a < b ? a : b;
310
- var max = (a, b) => a > b ? a : b;
311
- var floor = (n) => {
312
- const t = n | 0;
313
- return n < 0 && t !== n ? t - 1 : t;
314
- };
315
- var ceil = (n) => {
316
- const t = n | 0;
317
- return n > 0 && t !== n ? t + 1 : t;
318
- };
319
- var round = (n) => floor(n + 0.5);
320
- var sqrt = (n) => n ** 0.5;
321
- var pow = (base, exp) => base ** exp;
322
- var hypot2 = (x, y) => (x * x + y * y) ** 0.5;
323
- var hypot3 = (x, y, z) => (x * x + y * y + z * z) ** 0.5;
324
- var imul = (a, b) => {
325
- b |= 0;
326
- let result = (a & 4194303) * b;
327
- if (a & 4290772992) result += (a & 4290772992) * b | 0;
328
- return result | 0;
329
- };
330
- var _wrapAngle = (n) => {
331
- n = n % DOUBLE_PI;
332
- if (n > PI) n -= DOUBLE_PI;
333
- else if (n < -PI) n += DOUBLE_PI;
334
- return n;
335
- };
336
- var _sinSeries = (n) => {
337
- const n2 = n * n;
338
- return n * (1 + n2 * (-1 / 6 + n2 * (1 / 120 + n2 * (-1 / 5040 + n2 * (1 / 362880 + n2 * (-1 / 39916800))))));
339
- };
340
- var _cosSeries = (n) => {
341
- const n2 = n * n;
342
- return 1 + n2 * (-1 / 2 + n2 * (1 / 24 + n2 * (-1 / 720 + n2 * (1 / 40320 + n2 * (-1 / 3628800)))));
343
- };
344
- var _reduceAngle = (n) => {
345
- let x = _wrapAngle(n);
346
- let sinSign = 1;
347
- if (x < 0) {
348
- x = -x;
349
- sinSign = -1;
350
- }
351
- let cosSign = 1;
352
- if (x > HALF_PI) {
353
- x = PI - x;
354
- cosSign = -1;
355
- }
356
- return [x, sinSign, cosSign];
357
- };
358
- var sin = (n) => {
359
- const [x, sinSign] = _reduceAngle(n);
360
- return sinSign * _sinSeries(x);
459
+ // src/index.ts
460
+ __reExport(index_exports, __toESM(require_css()), module.exports);
461
+
462
+ // src/disposable.ts
463
+ var Disposable = class {
464
+ disposers = [];
465
+ add(disposer) {
466
+ this.disposers.push(disposer);
467
+ }
468
+ dispose() {
469
+ while (this.disposers.length) {
470
+ this.disposers.pop()?.();
471
+ }
472
+ }
361
473
  };
362
- var cos = (n) => {
363
- const [x, , cosSign] = _reduceAngle(n);
364
- return cosSign * _cosSeries(x);
474
+
475
+ // src/dungeon-generator.ts
476
+ var import_heap_js = require("heap-js");
477
+
478
+ // src/map2d.ts
479
+ var DEFAULT_VALUE = -1;
480
+ var BASE_2D = 2097152;
481
+ var OFFSET_2D = 1048576;
482
+ var Map2D = class {
483
+ defaultValue;
484
+ _data;
485
+ get data() {
486
+ return this._data;
487
+ }
488
+ get size() {
489
+ return this._data.size;
490
+ }
491
+ constructor(defaultValue = DEFAULT_VALUE) {
492
+ this.defaultValue = defaultValue;
493
+ this._data = /* @__PURE__ */ new Map();
494
+ }
495
+ _floor(x, y) {
496
+ return [floor(x), floor(y)];
497
+ }
498
+ _key(x, y) {
499
+ const X = x + OFFSET_2D & BASE_2D - 1;
500
+ const Y = y + OFFSET_2D & BASE_2D - 1;
501
+ return X * BASE_2D + Y;
502
+ }
503
+ _pos(key) {
504
+ const Y = key % BASE_2D;
505
+ const X = floor(key / BASE_2D);
506
+ return [X - OFFSET_2D, Y - OFFSET_2D];
507
+ }
508
+ clear() {
509
+ this._data.clear();
510
+ }
511
+ get(x, y, defaultValue = this.defaultValue) {
512
+ [x, y] = this._floor(x, y);
513
+ return this._data.get(this._key(x, y)) ?? defaultValue;
514
+ }
515
+ set(x, y, value = this.defaultValue) {
516
+ [x, y] = this._floor(x, y);
517
+ const key = this._key(x, y);
518
+ if (value === this.defaultValue) {
519
+ this._data.delete(key);
520
+ } else {
521
+ this._data.set(key, value);
522
+ }
523
+ }
524
+ forEach(callback) {
525
+ this._data.forEach((v, key) => {
526
+ const [x, y] = this._pos(key);
527
+ callback(x, y, v);
528
+ });
529
+ }
365
530
  };
366
- var clamp = (value, lo, hi) => max(lo, min(hi, value));
367
- var deg2rad = (degree) => degree * RADIAN_FACTOR;
368
- var lerp = (start, end, scale) => start * (1 - scale) + end * scale;
369
- var distance2d = (x1, y1, x2, y2) => hypot2(x2 - x1, y2 - y1);
370
- var distance3d = (x1, y1, z1, x2, y2, z2) => hypot3(x2 - x1, y2 - y1, z2 - z1);
371
- var squaredDistance = (x1, y1, x2, y2) => {
372
- const dx = x1 - x2;
373
- const dy = y1 - y2;
374
- return dx * dx + dy * dy;
531
+
532
+ // src/random.ts
533
+ var hash32 = (seed) => {
534
+ seed |= 0;
535
+ seed ^= seed >>> 16;
536
+ seed = imul(seed, 2246822507);
537
+ seed ^= seed >>> 13;
538
+ seed = imul(seed, 3266489909);
539
+ seed ^= seed >>> 16;
540
+ return seed >>> 0;
375
541
  };
376
- var manhattanDistance = (x1, y1, x2, y2) => abs(x1 - x2) + abs(y1 - y2);
377
- var roundTo = (value, digits, base = 10) => {
378
- const p = pow(base, digits);
379
- return round(value * p) / p;
542
+ var Random = class _Random {
543
+ _seed;
544
+ constructor(seed = Date.now()) {
545
+ this._seed = seed | 0;
546
+ }
547
+ get seed() {
548
+ return this._seed;
549
+ }
550
+ set seed(value) {
551
+ this._seed = value | 0;
552
+ }
553
+ //--------------------------------------------------------------------------
554
+ // Internal
555
+ //--------------------------------------------------------------------------
556
+ static _mix(...values) {
557
+ let seed = 0;
558
+ for (const value of values) {
559
+ seed ^= value | 0;
560
+ seed = hash32(seed);
561
+ }
562
+ return seed;
563
+ }
564
+ //--------------------------------------------------------------------------
565
+ // Stateless
566
+ //--------------------------------------------------------------------------
567
+ static hash(...values) {
568
+ return this._mix(...values);
569
+ }
570
+ static uint(...values) {
571
+ return this._mix(...values);
572
+ }
573
+ static float(...values) {
574
+ return this._mix(...values) / 4294967296;
575
+ }
576
+ static bool(...values) {
577
+ return (this._mix(...values) & 1) !== 0;
578
+ }
579
+ static sign(...values) {
580
+ return this.bool(...values) ? 1 : -1;
581
+ }
582
+ static angle(...values) {
583
+ return this.float(...values) * TAU;
584
+ }
585
+ //--------------------------------------------------------------------------
586
+ // Stateful
587
+ //--------------------------------------------------------------------------
588
+ uint() {
589
+ return _Random.uint(this._seed++);
590
+ }
591
+ float() {
592
+ return _Random.float(this._seed++);
593
+ }
594
+ bool() {
595
+ return _Random.bool(this._seed++);
596
+ }
597
+ sign() {
598
+ return _Random.sign(this._seed++);
599
+ }
600
+ angle() {
601
+ return _Random.angle(this._seed++);
602
+ }
380
603
  };
381
604
 
382
- // src/math/rectangle.ts
383
- var Rectangle = class _Rectangle {
384
- x;
385
- y;
386
- width;
387
- height;
388
- constructor(x = 0, y = 0, width = 1, height = 1) {
389
- this.x = x;
390
- this.y = y;
391
- this.width = width;
392
- this.height = height;
605
+ // src/rng.ts
606
+ var RandomTools = class {
607
+ rng;
608
+ get seed() {
609
+ return this.rng.seed;
393
610
  }
394
- area() {
395
- return this.width * this.height;
611
+ set seed(seed) {
612
+ this.rng.seed = seed;
396
613
  }
397
- contains(target, padding = 0) {
398
- return target instanceof Vector ? target.x >= this.x - padding && target.x <= this.x + this.width + padding && target.y >= this.y - padding && target.y <= this.y + this.height + padding : target.x >= this.x + padding && target.y >= this.y + padding && target.x + target.width <= this.x + this.width - padding && target.y + target.height <= this.y + this.height - padding;
614
+ constructor(seed) {
615
+ this.rng = seed instanceof Random ? seed : new Random(seed);
399
616
  }
400
- intersects(other, padding = 0) {
401
- return !(other.x - padding > this.x + this.width + padding || other.x + other.width + padding < this.x - padding || other.y - padding > this.y + this.height + padding || other.y + other.height + padding < this.y - padding);
617
+ //----------------------------------------------------------------------
618
+ // Numbers
619
+ //----------------------------------------------------------------------
620
+ nextFloat() {
621
+ return this.rng.float();
402
622
  }
403
- center() {
404
- return new Vector(
405
- this.x + this.width / 2,
406
- this.y + this.height / 2
407
- );
623
+ nextUint() {
624
+ return this.rng.uint();
408
625
  }
409
- distanceTo(other) {
410
- return this.center().distanceTo(other.center());
626
+ floatRange(min2, max2) {
627
+ if (typeof max2 !== "number") {
628
+ max2 = min2;
629
+ min2 = 0;
630
+ }
631
+ return min2 + this.rng.float() * (max2 - min2);
411
632
  }
412
- clone() {
413
- return new _Rectangle(this.x, this.y, this.width, this.height);
633
+ range(min2, max2) {
634
+ if (typeof max2 !== "number") {
635
+ max2 = min2;
636
+ min2 = 0;
637
+ }
638
+ return floor(this.floatRange(min2, max2 + 1));
414
639
  }
415
- equals(other) {
416
- return this.x === other.x && this.y === other.y && this.width === other.width && this.height === other.height;
640
+ //----------------------------------------------------------------------
641
+ // Collections
642
+ //----------------------------------------------------------------------
643
+ pick(array) {
644
+ return array[this.range(0, array.length - 1)];
417
645
  }
418
- grow(padding) {
419
- return new _Rectangle(
420
- this.x - padding,
421
- this.y - padding,
422
- this.width + padding * 2,
423
- this.height + padding * 2
646
+ sample(array, count) {
647
+ return this.shuffle(array).slice(0, count);
648
+ }
649
+ shuffle(array) {
650
+ const result = [...array];
651
+ for (let i = result.length - 1; i > 0; --i) {
652
+ const j = this.range(0, i);
653
+ [result[i], result[j]] = [result[j], result[i]];
654
+ }
655
+ return result;
656
+ }
657
+ chance(probability) {
658
+ return this.rng.float() < probability;
659
+ }
660
+ //----------------------------------------------------------------------
661
+ // Geometry
662
+ //----------------------------------------------------------------------
663
+ vector2() {
664
+ const \u03B8 = this.rng.angle();
665
+ return [
666
+ cos(\u03B8),
667
+ sin(\u03B8)
668
+ ];
669
+ }
670
+ pointOnCircle(radius = 1) {
671
+ const \u03B8 = this.rng.angle();
672
+ return [
673
+ radius * cos(\u03B8),
674
+ radius * sin(\u03B8)
675
+ ];
676
+ }
677
+ pointInCircle(radius = 1) {
678
+ const \u03B8 = this.rng.angle();
679
+ const r = sqrt(this.rng.float()) * radius;
680
+ return [
681
+ r * cos(\u03B8),
682
+ r * sin(\u03B8)
683
+ ];
684
+ }
685
+ pointOnSphere(radius = 1) {
686
+ const u = this.rng.float();
687
+ const v = this.rng.float();
688
+ const \u03B8 = TAU * u;
689
+ const \u03C6 = acos(2 * v - 1);
690
+ const s = sin(\u03C6);
691
+ return [
692
+ radius * s * cos(\u03B8),
693
+ radius * s * sin(\u03B8),
694
+ radius * cos(\u03C6)
695
+ ];
696
+ }
697
+ pointInSphere(radius = 1) {
698
+ const u = this.rng.float();
699
+ const v = this.rng.float();
700
+ const \u03B8 = TAU * u;
701
+ const \u03C6 = acos(2 * v - 1);
702
+ const r = radius * cbrt(this.rng.float());
703
+ const s = sin(\u03C6);
704
+ return [
705
+ r * s * cos(\u03B8),
706
+ r * s * sin(\u03B8),
707
+ r * cos(\u03C6)
708
+ ];
709
+ }
710
+ //----------------------------------------------------------------------
711
+ // Distributions
712
+ //----------------------------------------------------------------------
713
+ gaussian(mean = 0, standardDeviation = 1) {
714
+ let u = 0;
715
+ let v = 0;
716
+ while (u === 0) u = this.rng.float();
717
+ while (v === 0) v = this.rng.float();
718
+ const z = sqrt(-2 * Math.log(u)) * cos(TAU * v);
719
+ return mean + z * standardDeviation;
720
+ }
721
+ //----------------------------------------------------------------------
722
+ // Matrices
723
+ //----------------------------------------------------------------------
724
+ matrix(rows, columns = rows, min2 = 0, max2 = 1) {
725
+ return Array.from(
726
+ { length: rows },
727
+ () => Array.from(
728
+ { length: columns },
729
+ () => this.floatRange(min2, max2)
730
+ )
424
731
  );
425
732
  }
426
- vectorInside(vec, padding = 0) {
427
- return vec.x >= this.x - padding && vec.x <= this.x + this.width + padding && vec.y >= this.y - padding && vec.y <= this.y + this.height + padding;
733
+ transformMatrix(rows, columns = rows) {
734
+ return this.matrix(rows, columns, -1, 1);
428
735
  }
429
736
  };
737
+ var rng = new RandomTools();
738
+
739
+ // src/logger.ts
740
+ var logger_default = console.debug.bind(
741
+ console,
742
+ "%c[dev]",
743
+ "font-weight: bold; color: #888;"
744
+ );
430
745
 
431
- // src/math/vector.ts
746
+ // src/vector.ts
432
747
  var Vector = class _Vector {
433
748
  x;
434
749
  y;
@@ -439,6 +754,24 @@ var Vector = class _Vector {
439
754
  clone() {
440
755
  return new _Vector(this.x, this.y);
441
756
  }
757
+ copy(that) {
758
+ this.x = that.x;
759
+ this.y = that.y;
760
+ return this;
761
+ }
762
+ copyTo(target) {
763
+ target.x = this.x;
764
+ target.y = this.y;
765
+ return this;
766
+ }
767
+ set(x, y) {
768
+ this.x = x;
769
+ this.y = y;
770
+ return this;
771
+ }
772
+ equals(that) {
773
+ return this.x === that.x && this.y === that.y;
774
+ }
442
775
  distanceTo(that) {
443
776
  return distance2d(
444
777
  this.x,
@@ -447,6 +780,44 @@ var Vector = class _Vector {
447
780
  that.y
448
781
  );
449
782
  }
783
+ length() {
784
+ return Math.hypot(this.x, this.y);
785
+ }
786
+ normalize() {
787
+ const length = this.length();
788
+ if (length > 0) {
789
+ this.dividedBy(length);
790
+ }
791
+ return this;
792
+ }
793
+ setLength(length) {
794
+ return this.normalize().times(length);
795
+ }
796
+ dot(that) {
797
+ return this.x * that.x + this.y * that.y;
798
+ }
799
+ angle() {
800
+ return Math.atan2(this.y, this.x);
801
+ }
802
+ angleTo(that) {
803
+ const denominator = this.length() * that.length();
804
+ if (denominator === 0) {
805
+ return 0;
806
+ }
807
+ const theta = this.dot(that) / denominator;
808
+ return Math.acos(Math.min(1, Math.max(-1, theta)));
809
+ }
810
+ rotate(radians2) {
811
+ const cos2 = Math.cos(radians2);
812
+ const sin2 = Math.sin(radians2);
813
+ const { x, y } = this;
814
+ this.x = x * cos2 - y * sin2;
815
+ this.y = x * sin2 + y * cos2;
816
+ return this;
817
+ }
818
+ rotateAround(origin, radians2) {
819
+ return this.minus(origin).rotate(radians2).plus(origin);
820
+ }
450
821
  floored() {
451
822
  return new _Vector(
452
823
  Math.floor(this.x),
@@ -497,6 +868,12 @@ var Vector = class _Vector {
497
868
  this.y /= y || 1;
498
869
  return this;
499
870
  }
871
+ static from(value) {
872
+ return new _Vector(value.x, value.y);
873
+ }
874
+ static get ZERO() {
875
+ return new _Vector(0, 0);
876
+ }
500
877
  static get NORTH() {
501
878
  return new _Vector(0, -1);
502
879
  }
@@ -511,175 +888,54 @@ var Vector = class _Vector {
511
888
  }
512
889
  };
513
890
 
514
- // src/dungeon-generator.ts
515
- var import_heap_js = require("heap-js");
516
-
517
- // src/log.ts
518
- var log = console.debug.bind(
519
- console,
520
- "%c[dev]",
521
- "font-weight: bold; color: #888;"
522
- );
523
-
524
- // src/map2d.ts
525
- var DEFAULT_VALUE = -1;
526
- var Map2D = class {
527
- defaultValue;
528
- _data;
529
- get size() {
530
- return this._data.size;
531
- }
532
- constructor(defaultValue = DEFAULT_VALUE) {
533
- this.defaultValue = defaultValue;
534
- this._data = /* @__PURE__ */ new Map();
535
- }
536
- _floor(x, y) {
537
- return [floor(x), floor(y)];
538
- }
539
- _key(x, y) {
540
- return `${x},${y}`;
541
- }
542
- _pos(key) {
543
- return key.split(",").map((v) => parseInt(v));
544
- }
545
- clear() {
546
- this._data.clear();
547
- }
548
- get(x, y, defaultValue = this.defaultValue) {
549
- [x, y] = this._floor(x, y);
550
- return this._data.get(this._key(x, y)) ?? defaultValue;
551
- }
552
- set(x, y, value = this.defaultValue) {
553
- [x, y] = this._floor(x, y);
554
- const key = this._key(x, y);
555
- if (value === this.defaultValue) {
556
- this._data.delete(key);
557
- } else {
558
- this._data.set(key, value);
559
- }
560
- }
561
- forEach(callback) {
562
- this._data.forEach((v, key) => {
563
- const [x, y] = this._pos(key);
564
- callback(x, y, v);
565
- });
566
- }
567
- };
568
-
569
- // src/rng.ts
570
- var RandomNumberGenerator = class {
571
- __seed;
572
- _seed;
573
- _uid_characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
574
- constructor() {
575
- this._seed = Date.now();
576
- this.__seed = this._seed;
577
- this._uid_characters = this.shuffle(this._uid_characters);
578
- this.seed = this.nexti;
891
+ // src/rectangle.ts
892
+ var Rectangle = class _Rectangle {
893
+ x;
894
+ y;
895
+ width;
896
+ height;
897
+ constructor(x = 0, y = 0, width = 1, height = 1) {
898
+ this.x = x;
899
+ this.y = y;
900
+ this.width = width;
901
+ this.height = height;
579
902
  }
580
- get seed() {
581
- return this._seed;
903
+ area() {
904
+ return this.width * this.height;
582
905
  }
583
- set seed(value) {
584
- this._seed = value;
585
- this.__seed = this._seed;
906
+ contains(target, padding = 0) {
907
+ return target instanceof Vector ? target.x >= this.x - padding && target.x <= this.x + this.width + padding && target.y >= this.y - padding && target.y <= this.y + this.height + padding : target.x >= this.x + padding && target.y >= this.y + padding && target.x + target.width <= this.x + this.width - padding && target.y + target.height <= this.y + this.height - padding;
586
908
  }
587
- get startingSeed() {
588
- return this.__seed;
909
+ intersects(other, padding = 0) {
910
+ return !(other.x - padding > this.x + this.width + padding || other.x + other.width + padding < this.x - padding || other.y - padding > this.y + this.height + padding || other.y + other.height + padding < this.y - padding);
589
911
  }
590
- get nextf() {
591
- this._seed |= 0;
592
- this._seed = this._seed + 2654435769 | 0;
593
- let t = this._seed ^ this._seed >>> 16;
594
- t = imul(t, 569420461);
595
- t = t ^ t >>> 15;
596
- t = imul(t, 1935289751);
597
- return ((t = t ^ t >>> 15) >>> 0) / 4294967296;
912
+ center() {
913
+ return new Vector(
914
+ this.x + this.width / 2,
915
+ this.y + this.height / 2
916
+ );
598
917
  }
599
- get nexti() {
600
- return this.nextf * Number.MAX_SAFE_INTEGER | 0;
918
+ distanceTo(other) {
919
+ return this.center().distanceTo(other.center());
601
920
  }
602
- range(min2, max2) {
603
- if (max2 === void 0) {
604
- max2 = min2;
605
- min2 = 0;
606
- }
607
- return min2 + this.nextf * (max2 - min2) | 0;
608
- }
609
- randomUnitVector() {
610
- const theta = this.nextf * 2 * PI;
611
- return [cos(theta), sin(theta)];
612
- }
613
- pointInUnitCircle(radius = 1, floored = false) {
614
- const theta = this.nextf * 2 * PI;
615
- const r = sqrt(this.nextf) * radius;
616
- const x = r * cos(theta);
617
- const y = r * sin(theta);
618
- return !floored ? [x, y] : [floor(x), floor(y)];
619
- }
620
- pointInUnitSphere(radius = 1, floored = false) {
621
- var u = this.nextf;
622
- var v = this.nextf;
623
- var theta = u * 2 * PI;
624
- var phi = Math.acos(2 * v - 1);
625
- var r = radius * Math.cbrt(rng.nextf);
626
- var sinTheta = sin(theta);
627
- var cosTheta = cos(theta);
628
- var sinPhi = sin(phi);
629
- var cosPhi = cos(phi);
630
- var x = r * sinPhi * cosTheta;
631
- var y = r * sinPhi * sinTheta;
632
- var z = r * cosPhi;
633
- return !floored ? [x, y, z] : [floor(x), floor(y), floor(z)];
634
- }
635
- parkMillerNormal() {
636
- const mean = 1 / 2;
637
- const stddev = 1 / 6;
638
- let u = 0;
639
- let v = 0;
640
- while (u === 0) u = this.nextf;
641
- while (v === 0) v = this.nextf;
642
- const n = sqrt(-2 * Math.log(u)) * cos(2 * PI * v);
643
- return n * stddev + mean;
644
- }
645
- choose(...args) {
646
- if (args.length === 1) {
647
- if (Array.isArray(args[0])) {
648
- return args[0][this.range(args[0].length - 1)];
649
- } else if (typeof args[0] === "string") {
650
- return args[0].charAt(this.range(args[0].length - 1));
651
- } else {
652
- return args[0];
653
- }
654
- } else {
655
- return args[this.range(args.length - 1)];
656
- }
921
+ clone() {
922
+ return new _Rectangle(this.x, this.y, this.width, this.height);
657
923
  }
658
- shuffle(value) {
659
- if (Array.isArray(value)) {
660
- return value.sort(() => 0.5 - this.nextf);
661
- } else {
662
- return value.split("").sort(() => 0.5 - this.nextf).join("");
663
- }
924
+ equals(other) {
925
+ return this.x === other.x && this.y === other.y && this.width === other.width && this.height === other.height;
664
926
  }
665
- uid(length = 16) {
666
- const uid = [];
667
- for (let i = 0; i < length; i++) uid.push(this.choose(this._uid_characters));
668
- return uid.join("");
927
+ grow(padding) {
928
+ return new _Rectangle(
929
+ this.x - padding,
930
+ this.y - padding,
931
+ this.width + padding * 2,
932
+ this.height + padding * 2
933
+ );
669
934
  }
670
- randomMatrix(size) {
671
- const rows = [];
672
- for (let i = 0; i < size; i++) {
673
- const row = [];
674
- for (let j = 0; j < size; j++) {
675
- row.push(this.nextf * 2 - 1);
676
- }
677
- rows.push(row);
678
- }
679
- return rows;
935
+ vectorInside(vec, padding = 0) {
936
+ return vec.x >= this.x - padding && vec.x <= this.x + this.width + padding && vec.y >= this.y - padding && vec.y <= this.y + this.height + padding;
680
937
  }
681
938
  };
682
- var rng = new RandomNumberGenerator();
683
939
 
684
940
  // src/dungeon-generator.ts
685
941
  var TILE_EMPTY = 0;
@@ -712,20 +968,20 @@ var DIRECTIONS_8_WAY = [
712
968
  { x: -1, y: 1 }
713
969
  // down-left
714
970
  ];
715
- function _plot(map, data, tile) {
971
+ function _plot(map2, data, tile) {
716
972
  data.forEach((element) => {
717
973
  if (element instanceof Rectangle) {
718
974
  for (let y = 0; y < element.height; y++) {
719
975
  for (let x = 0; x < element.width; x++) {
720
- map.set(element.x + x, element.y + y, tile);
976
+ map2.set(element.x + x, element.y + y, tile);
721
977
  }
722
978
  }
723
979
  } else if (Array.isArray(element)) {
724
980
  element.forEach((point) => {
725
- map.set(point.x, point.y, tile);
981
+ map2.set(point.x, point.y, tile);
726
982
  });
727
983
  } else {
728
- map.set(element.x, element.y, tile);
984
+ map2.set(element.x, element.y, tile);
729
985
  }
730
986
  });
731
987
  }
@@ -739,11 +995,11 @@ function _generateRooms(params = {}) {
739
995
  let iterations = 0;
740
996
  while (iterations < maxIterations) {
741
997
  const baseSize = rng.range(minRoomSize, maxRoomSize);
742
- const aspectRatio = 0.75 + rng.parkMillerNormal() * 0.5;
743
- const width = Math.min(maxRoomSize, Math.max(minRoomSize, Math.round(baseSize * aspectRatio)));
744
- const height = Math.min(maxRoomSize, Math.max(minRoomSize, Math.round(baseSize / aspectRatio)));
745
- const x = Math.floor(rng.range(1 + padding, size - width - padding));
746
- const y = Math.floor(rng.range(1 + padding, size - height - padding));
998
+ const aspectRatio = 0.75 + rng.gaussian() * 0.5;
999
+ const width = min(maxRoomSize, max(minRoomSize, round(baseSize * aspectRatio)));
1000
+ const height = min(maxRoomSize, max(minRoomSize, round(baseSize / aspectRatio)));
1001
+ const x = floor(rng.range(1 + padding, size - width - padding));
1002
+ const y = floor(rng.range(1 + padding, size - height - padding));
747
1003
  const room = new Rectangle(x, y, width, height);
748
1004
  if (!rooms.some((existing) => room.intersects(existing, padding))) {
749
1005
  rooms.push(room);
@@ -807,7 +1063,7 @@ function _generateMST(rooms, params = {}) {
807
1063
  const remainingEdges = heap.toArray().sort((a, b) => a.cost - b.cost);
808
1064
  for (let i = 0; i < extraPathLevels && i < remainingEdges.length; i++) {
809
1065
  const e = remainingEdges[i];
810
- if (rng.nextf < extraPathDensity) {
1066
+ if (rng.nextFloat() < extraPathDensity) {
811
1067
  extraPaths.push({
812
1068
  a: rooms[e.from],
813
1069
  b: rooms[e.to]
@@ -844,8 +1100,8 @@ function _getShortestPathBetweenRects(rectA, rectB) {
844
1100
  }
845
1101
  return { a: paths.a, b: paths.b };
846
1102
  }
847
- function _generatePaths(map, rooms, params = {}) {
848
- const aStar = new AStar(map);
1103
+ function _generatePaths(map2, rooms, params = {}) {
1104
+ const aStar = new AStar(map2);
849
1105
  const paths = [];
850
1106
  _generateMST(rooms, params).forEach((rects) => {
851
1107
  const { a, b } = _getShortestPathBetweenRects(rects.a, rects.b);
@@ -855,15 +1111,15 @@ function _generatePaths(map, rooms, params = {}) {
855
1111
  }
856
1112
  function GenerateRooms(params = {}) {
857
1113
  const startTime = Date.now();
858
- const map = params.map ?? new Map2D(TILE_EMPTY);
1114
+ const map2 = params.map ?? new Map2D(TILE_EMPTY);
859
1115
  const rooms = _generateRooms(params.rooms);
860
- _plot(map, rooms, TILE_FLOOR);
861
- const paths = _generatePaths(map, rooms, params.paths);
862
- _plot(map, paths, TILE_PATH);
863
- log("GenerateRooms: done:", Date.now() - startTime, "ms");
864
- return { map, rooms, paths };
1116
+ _plot(map2, rooms, TILE_FLOOR);
1117
+ const paths = _generatePaths(map2, rooms, params.paths);
1118
+ _plot(map2, paths, TILE_PATH);
1119
+ logger_default("GenerateRooms: done:", Date.now() - startTime, "ms");
1120
+ return { map: map2, rooms, paths };
865
1121
  }
866
- function GenerateDoors(map, paths, params = {}) {
1122
+ function GenerateDoors(map2, paths, params = {}) {
867
1123
  const chanceExists = params.chanceExists ?? DEFAULT_DOOR_CHANCE_EXISTS;
868
1124
  const possibleDoorPoints = [];
869
1125
  paths.forEach((points) => {
@@ -874,19 +1130,19 @@ function GenerateDoors(map, paths, params = {}) {
874
1130
  });
875
1131
  const doors = [];
876
1132
  possibleDoorPoints.forEach((doorPoint) => {
877
- if (rng.nextf < chanceExists && (map.get(doorPoint.x, doorPoint.y - 1) === map.defaultValue && map.get(doorPoint.x, doorPoint.y + 1) === map.defaultValue || map.get(doorPoint.x - 1, doorPoint.y) === map.defaultValue && map.get(doorPoint.x + 1, doorPoint.y) === map.defaultValue)) {
1133
+ if (rng.nextFloat() < chanceExists && (map2.get(doorPoint.x, doorPoint.y - 1) === map2.defaultValue && map2.get(doorPoint.x, doorPoint.y + 1) === map2.defaultValue || map2.get(doorPoint.x - 1, doorPoint.y) === map2.defaultValue && map2.get(doorPoint.x + 1, doorPoint.y) === map2.defaultValue)) {
878
1134
  doors.push(doorPoint);
879
1135
  }
880
1136
  });
881
1137
  return doors;
882
1138
  }
883
- function GenerateWalls(map) {
1139
+ function GenerateWalls(map2) {
884
1140
  const walls = [];
885
- map.forEach((x, y, _v) => {
1141
+ map2.forEach((x, y, _v) => {
886
1142
  DIRECTIONS_8_WAY.forEach((dir) => {
887
1143
  const nx = dir.x + x;
888
1144
  const ny = dir.y + y;
889
- if (map.get(nx, ny) === map.defaultValue) {
1145
+ if (map2.get(nx, ny) === map2.defaultValue) {
890
1146
  walls.push({ x: nx, y: ny });
891
1147
  }
892
1148
  });
@@ -927,79 +1183,118 @@ var ECS = class {
927
1183
  }
928
1184
  };
929
1185
 
930
- // src/input/index.ts
931
- var Input = class extends Emitter {
932
- _down = /* @__PURE__ */ new Map();
1186
+ // src/event-queue.ts
1187
+ var EventQueue = class {
1188
+ events = [];
1189
+ emit(event) {
1190
+ this.events.push(event);
1191
+ }
1192
+ consume() {
1193
+ return this.events;
1194
+ }
1195
+ clear() {
1196
+ this.events.length = 0;
1197
+ }
1198
+ drain() {
1199
+ return this.events.splice(0);
1200
+ }
1201
+ get size() {
1202
+ return this.events.length;
1203
+ }
1204
+ };
1205
+
1206
+ // src/input.ts
1207
+ var DOWN = 1;
1208
+ var UP = 0;
1209
+ var Input = class _Input {
1210
+ static _instance;
1211
+ static get instance() {
1212
+ if (!_Input._instance) {
1213
+ _Input._instance = new _Input();
1214
+ }
1215
+ return _Input._instance;
1216
+ }
1217
+ _keyStates;
1218
+ _buttonStates;
933
1219
  _pointerPosition = new Vector();
934
1220
  inputThreshold = 200;
935
- get down() {
936
- return this._down;
937
- }
938
1221
  get pointerPosition() {
939
- return this._pointerPosition;
940
- }
1222
+ return this._pointerPosition.clone();
1223
+ }
1224
+ // get mouseWorldPosition(): Vector {
1225
+ // return new Vector(
1226
+ // (this._mousePosition.x / this.canvasWidth) * 2 - 1,
1227
+ // -(this._mousePosition.y / this.canvasHeight) * 2 + 1,
1228
+ // )
1229
+ // }
1230
+ onKeyDown = new Observable();
1231
+ onKeyUp = new Observable();
1232
+ onKeyPressed = new Observable();
1233
+ onPointerDown = new Observable();
1234
+ onPointerUp = new Observable();
1235
+ onPointerPressed = new Observable();
1236
+ onPointerMove = new Observable();
1237
+ onWheel = new Observable();
941
1238
  constructor() {
942
- super();
943
- window.addEventListener("contextmenu", (ev) => {
944
- log("contextmenu:", ev);
945
- });
1239
+ this._keyStates = /* @__PURE__ */ new Map();
1240
+ this._buttonStates = /* @__PURE__ */ new Map();
946
1241
  window.addEventListener("keydown", (ev) => {
947
- const { code, repeat, timeStamp } = ev;
948
- if (!repeat) {
949
- this._down.set(code, { state: 1, time: timeStamp });
950
- this.emit("key_down", code, ev);
1242
+ if (!ev.repeat) {
1243
+ this._keyStates.set(ev.code, { state: DOWN, time: ev.timeStamp });
1244
+ this.onKeyDown.notify(this, ev);
951
1245
  }
952
1246
  });
953
1247
  window.addEventListener("keyup", (ev) => {
954
- const { code, timeStamp } = ev;
955
- const delta = timeStamp - (this._down.get(code)?.time ?? 0);
956
- this._down.set(code, { state: 0, time: timeStamp });
957
- this.emit("key_up", code, ev);
1248
+ this._keyStates.set(ev.code, { state: UP, time: ev.timeStamp });
1249
+ this.onKeyUp.notify(this, ev);
1250
+ const delta = ev.timeStamp - (this._keyStates.get(ev.code)?.time ?? 0);
958
1251
  if (delta < this.inputThreshold) {
959
- this.emit("key_pressed", code, ev);
1252
+ this.onKeyPressed.notify(this, ev);
960
1253
  }
961
1254
  });
962
1255
  window.addEventListener("pointerdown", (ev) => {
963
- const { button, timeStamp } = ev;
964
- const code = `Button${button}`;
965
- if (!this._down.get(code)) {
966
- this._down.set(code, { state: 1, time: timeStamp });
967
- this.emit("pointer_down", code, ev);
1256
+ const code = `Button${ev.button}`;
1257
+ if (!this._buttonStates.get(code)) {
1258
+ this._buttonStates.set(code, { state: DOWN, time: ev.timeStamp });
1259
+ this.onPointerDown.notify(this, ev);
968
1260
  }
969
1261
  });
970
1262
  window.addEventListener("pointerup", (ev) => {
971
- const { button, timeStamp } = ev;
972
- const code = `Button${button}`;
973
- const delta = timeStamp - (this._down.get(code)?.time ?? 0);
974
- this._down.set(code, { state: 0, time: timeStamp });
1263
+ const code = `Button${ev.button}`;
1264
+ this._buttonStates.set(code, { state: UP, time: ev.timeStamp });
1265
+ this.onPointerUp.notify(this, ev);
1266
+ const delta = ev.timeStamp - (this._buttonStates.get(code)?.time ?? 0);
975
1267
  if (delta < this.inputThreshold) {
976
- this.emit("button_clicked", code, ev);
1268
+ this.onPointerPressed.notify(this, ev);
977
1269
  }
978
1270
  });
979
1271
  window.addEventListener("pointermove", (ev) => {
980
- const { offsetX, offsetY, movementX, movementY } = ev;
981
- this._pointerPosition.x = offsetX;
982
- this._pointerPosition.y = offsetY;
983
- this.emit("pointer_move", movementX, movementY, ev);
1272
+ this._pointerPosition.x = ev.offsetX;
1273
+ this._pointerPosition.y = ev.offsetY;
1274
+ this.onPointerMove.notify(this, ev);
984
1275
  });
985
1276
  window.addEventListener("wheel", (ev) => {
986
- const { deltaY } = ev;
987
- this.emit("wheel", deltaY, ev);
1277
+ this.onWheel.notify(this, ev);
988
1278
  });
989
1279
  }
990
- isDown(name) {
991
- return this._down.get(name) ? true : false;
1280
+ isDown(key) {
1281
+ return (this._keyStates.get(key) ?? this._buttonStates.get(key))?.state === 1;
992
1282
  }
993
1283
  };
994
1284
 
995
1285
  // src/map3d.ts
996
1286
  var DEFAULT_VALUE2 = -1;
1287
+ var BASE_3D = 131072;
1288
+ var OFFSET_3D = 65536;
997
1289
  var Map3D = class {
998
1290
  defaultValue;
999
1291
  _data;
1000
1292
  get data() {
1001
1293
  return this._data;
1002
1294
  }
1295
+ get size() {
1296
+ return this._data.size;
1297
+ }
1003
1298
  constructor(defaultValue = DEFAULT_VALUE2) {
1004
1299
  this.defaultValue = defaultValue;
1005
1300
  this._data = /* @__PURE__ */ new Map();
@@ -1008,10 +1303,16 @@ var Map3D = class {
1008
1303
  return [floor(x), floor(y), floor(z)];
1009
1304
  }
1010
1305
  _key(x, y, z) {
1011
- return `${x},${y},${z}`;
1306
+ const X = x + OFFSET_3D & BASE_3D - 1;
1307
+ const Y = y + OFFSET_3D & BASE_3D - 1;
1308
+ const Z = z + OFFSET_3D & BASE_3D - 1;
1309
+ return X * BASE_3D * BASE_3D + Y * BASE_3D + Z;
1012
1310
  }
1013
1311
  _pos(key) {
1014
- return key.split(",").map((v) => parseInt(v));
1312
+ const Z = key % BASE_3D;
1313
+ const Y = floor(key / BASE_3D) % BASE_3D;
1314
+ const X = floor(key / (BASE_3D * BASE_3D));
1315
+ return [X - OFFSET_3D, Y - OFFSET_3D, Z - OFFSET_3D];
1015
1316
  }
1016
1317
  clear() {
1017
1318
  this._data.clear();
@@ -1053,7 +1354,7 @@ var getRedYellowGreenGradientHex = (min2, max2) => {
1053
1354
  max2 = 1;
1054
1355
  }
1055
1356
  const percent = min2 / max2;
1056
- const index = clamp(Math.floor(percent * 10), 0, 10);
1357
+ const index = clamp(floor(percent * 10), 0, 10);
1057
1358
  return [
1058
1359
  "#ff0000",
1059
1360
  "#ff3300",
@@ -1089,19 +1390,19 @@ var Noise = class _Noise {
1089
1390
  static _n4d;
1090
1391
  static noise2d(x, y) {
1091
1392
  if (!_Noise._n2d) {
1092
- _Noise._n2d = (0, import_simplex_noise.createNoise2D)(() => rng.nextf);
1393
+ _Noise._n2d = (0, import_simplex_noise.createNoise2D)(() => rng.nextFloat());
1093
1394
  }
1094
1395
  return _Noise._n2d(x, y);
1095
1396
  }
1096
1397
  static noise3d(x, y, z) {
1097
1398
  if (!_Noise._n3d) {
1098
- _Noise._n3d = (0, import_simplex_noise.createNoise3D)(() => rng.nextf);
1399
+ _Noise._n3d = (0, import_simplex_noise.createNoise3D)(() => rng.nextFloat());
1099
1400
  }
1100
1401
  return _Noise._n3d(x, y, z);
1101
1402
  }
1102
1403
  static noise4d(x, y, z, w) {
1103
1404
  if (!_Noise._n4d) {
1104
- _Noise._n4d = (0, import_simplex_noise.createNoise4D)(() => rng.nextf);
1405
+ _Noise._n4d = (0, import_simplex_noise.createNoise4D)(() => rng.nextFloat());
1105
1406
  }
1106
1407
  return _Noise._n4d(x, y, z, w);
1107
1408
  }
@@ -1185,7 +1486,7 @@ var Color = class _Color {
1185
1486
  [this._r, this._g, this._b, this._a] = this._parse_hex_color_string(value);
1186
1487
  this._hex = value;
1187
1488
  } else {
1188
- log(`Color: invalid color string: ${value}. Should be a hexadecimal color string (i.e.: '#ffffff' or '#ffffffff'). Keeping the current color.`);
1489
+ logger_default(`Color: invalid color string: ${value}. Should be a hexadecimal color string (i.e.: '#ffffff' or '#ffffffff'). Keeping the current color.`);
1189
1490
  }
1190
1491
  }
1191
1492
  get rgba() {
@@ -1196,7 +1497,7 @@ var Color = class _Color {
1196
1497
  if (r.startsWith("#")) {
1197
1498
  [r, g, b, a] = this._parse_hex_color_string(r);
1198
1499
  } else {
1199
- log(`Color: invalid color string: ${r}. Should be a hexadecimal color string (i.e.: '#ffffff' or '#ffffffff'). Defaulting to opaque white.`);
1500
+ logger_default(`Color: invalid color string: ${r}. Should be a hexadecimal color string (i.e.: '#ffffff' or '#ffffffff'). Defaulting to opaque white.`);
1200
1501
  r = g = b = a = 255;
1201
1502
  }
1202
1503
  } else {
@@ -1297,7 +1598,7 @@ var Color = class _Color {
1297
1598
  const [r, g, b, a] = rgba.map(Number);
1298
1599
  return new _Color(r, g, b, a);
1299
1600
  } else {
1300
- log(`Color: invalid CSS color name: ${name}. Defaulting to transparent.`);
1601
+ logger_default(`Color: invalid CSS color name: ${name}. Defaulting to transparent.`);
1301
1602
  return _Color.TRANSPARENT;
1302
1603
  }
1303
1604
  }
@@ -1349,6 +1650,17 @@ var SpatialPartition2d = class {
1349
1650
  };
1350
1651
 
1351
1652
  // src/particle-system-2d.ts
1653
+ function randomMatrix(size) {
1654
+ const rows = [];
1655
+ for (let i = 0; i < size; i++) {
1656
+ const row = [];
1657
+ for (let j = 0; j < size; j++) {
1658
+ row.push(Random.float(i, j) * 2 - 1);
1659
+ }
1660
+ rows.push(row);
1661
+ }
1662
+ return rows;
1663
+ }
1352
1664
  var colors = [
1353
1665
  Color.RED,
1354
1666
  Color.GREEN,
@@ -1362,14 +1674,14 @@ var ParticleSystem2d = class {
1362
1674
  _frictionHalfLife = 0.04;
1363
1675
  _range = 0.1;
1364
1676
  _rangeFactor = 0.1;
1365
- _attractionMatrix = rng.randomMatrix(colors.length);
1677
+ _attractionMatrix = randomMatrix(colors.length);
1366
1678
  _particles = [];
1367
1679
  _spatialPartition;
1368
1680
  constructor() {
1369
1681
  for (let i = 0; i < this.particleCount; i++) {
1370
1682
  this._particles.push({
1371
- x: rng.nextf,
1372
- y: rng.nextf,
1683
+ x: Random.float(i),
1684
+ y: Random.float(i + 1),
1373
1685
  vx: 0,
1374
1686
  vy: 0,
1375
1687
  color: rng.range(colors.length)
@@ -1382,12 +1694,12 @@ var ParticleSystem2d = class {
1382
1694
  if (r < beta) {
1383
1695
  f = r / beta - 1;
1384
1696
  } else if (beta < r && r < 1) {
1385
- f = a * (1 - Math.abs(2 * r - 1 - beta) / (1 - beta));
1697
+ f = a * (1 - abs(2 * r - 1 - beta) / (1 - beta));
1386
1698
  }
1387
1699
  return f;
1388
1700
  }
1389
1701
  _updateVelocities(deltaTimeSeconds) {
1390
- const frictionFactor = Math.pow(
1702
+ const frictionFactor = pow(
1391
1703
  0.5,
1392
1704
  deltaTimeSeconds / this._frictionHalfLife
1393
1705
  );
@@ -1404,10 +1716,10 @@ var ParticleSystem2d = class {
1404
1716
  if (cell === neighborCell && i === j) continue;
1405
1717
  const p2 = neighborCell[j];
1406
1718
  let rx = p2.x - p1.x;
1407
- if (Math.abs(rx) > 0.5) rx = rx > 0 ? rx - 1 : rx + 1;
1719
+ if (abs(rx) > 0.5) rx = rx > 0 ? rx - 1 : rx + 1;
1408
1720
  let ry = p2.y - p1.y;
1409
- if (Math.abs(ry) > 0.5) ry = ry > 0 ? ry - 1 : ry + 1;
1410
- const d = Math.hypot(rx, ry);
1721
+ if (abs(ry) > 0.5) ry = ry > 0 ? ry - 1 : ry + 1;
1722
+ const d = hypot2(rx, ry);
1411
1723
  if (d > 0 && d < this._range) {
1412
1724
  const f = this._calcForce(
1413
1725
  d / this._range,
@@ -1463,9 +1775,9 @@ var ParticleSystem2d = class {
1463
1775
  // src/poisson-disk-sampler.ts
1464
1776
  function PoissonDiskSampler(width, height, minDist, maxPoints = 30) {
1465
1777
  const k = maxPoints;
1466
- const cellSize = minDist / Math.SQRT2;
1467
- const gridWidth = Math.ceil(width / cellSize);
1468
- const gridHeight = Math.ceil(height / cellSize);
1778
+ const cellSize = minDist / SQRT2;
1779
+ const gridWidth = ceil(width / cellSize);
1780
+ const gridHeight = ceil(height / cellSize);
1469
1781
  const grid = Array.from(
1470
1782
  { length: gridWidth },
1471
1783
  () => Array(gridHeight).fill(null)
@@ -1474,8 +1786,8 @@ function PoissonDiskSampler(width, height, minDist, maxPoints = 30) {
1474
1786
  const active = [];
1475
1787
  function gridIndex(p) {
1476
1788
  return {
1477
- i: Math.floor(p.x / cellSize),
1478
- j: Math.floor(p.y / cellSize)
1789
+ i: floor(p.x / cellSize),
1790
+ j: floor(p.y / cellSize)
1479
1791
  };
1480
1792
  }
1481
1793
  function isFarEnough(p) {
@@ -1503,15 +1815,15 @@ function PoissonDiskSampler(width, height, minDist, maxPoints = 30) {
1503
1815
  samples.push(initial);
1504
1816
  active.push(initial);
1505
1817
  while (active.length) {
1506
- const index = Math.floor(rng.range(0, active.length));
1818
+ const index = floor(rng.range(0, active.length));
1507
1819
  const point = active[index];
1508
1820
  let found = false;
1509
1821
  for (let n = 0; n < k; n++) {
1510
- const angle = rng.range(0, Math.PI * 2);
1822
+ const angle = rng.range(0, PI * 2);
1511
1823
  const dist = rng.range(minDist, 2 * minDist);
1512
1824
  const newPoint = {
1513
- x: point.x + Math.cos(angle) * dist,
1514
- y: point.y + Math.sin(angle) * dist
1825
+ x: point.x + cos(angle) * dist,
1826
+ y: point.y + sin(angle) * dist
1515
1827
  };
1516
1828
  if (newPoint.x >= 0 && newPoint.y >= 0 && newPoint.x < width && newPoint.y < height && isFarEnough(newPoint)) {
1517
1829
  const { i, j } = gridIndex(newPoint);
@@ -1530,71 +1842,96 @@ function PoissonDiskSampler(width, height, minDist, maxPoints = 30) {
1530
1842
  }
1531
1843
 
1532
1844
  // src/rendering/canvas-renderer.ts
1845
+ var DEFAULT_COLOR = "white";
1846
+ var DEFAULT_PIXEL_SIZE = 1;
1847
+ var DEFAULT_FONT = "18px monospace";
1848
+ var DEFAULT_TEXTALIGN = "left";
1849
+ var DEFAULT_TEXTBASELINE = "alphabetic";
1533
1850
  var CanvasRenderer = class {
1534
- canvas;
1535
1851
  context;
1536
- drawCentered = true;
1852
+ get canvas() {
1853
+ return this.context.canvas;
1854
+ }
1537
1855
  get width() {
1538
- return this.context.canvas.width;
1856
+ return this.canvas.width;
1539
1857
  }
1540
1858
  get height() {
1541
- return this.context.canvas.height;
1859
+ return this.canvas.height;
1542
1860
  }
1543
1861
  constructor(canvas) {
1544
- this.canvas = canvas ?? document.createElement("canvas");
1545
- this.context = this.canvas.getContext("2d");
1546
- }
1547
- appendTo(target) {
1548
- if (this.canvas.parentNode) {
1549
- this.canvas.parentNode.removeChild(this.canvas);
1550
- }
1551
- target.appendChild(this.canvas);
1862
+ this.context = (canvas ?? document.createElement("canvas")).getContext("2d");
1863
+ this.context.font = DEFAULT_FONT;
1864
+ this.context.textAlign = DEFAULT_TEXTALIGN;
1865
+ this.context.textBaseline = DEFAULT_TEXTBASELINE;
1552
1866
  }
1553
- resize(displayWidth, displayHeight) {
1554
- const { width, height } = this.canvas.parentElement?.getBoundingClientRect() ?? this.canvas.getBoundingClientRect();
1555
- displayWidth = 0 | (displayWidth ?? width);
1556
- displayHeight = 0 | (displayHeight ?? height);
1557
- if (this.canvas.width !== displayWidth || this.canvas.height !== displayHeight) {
1558
- this.canvas.width = displayWidth;
1559
- this.canvas.height = displayHeight;
1560
- return true;
1867
+ resize(width, height) {
1868
+ let resized = false;
1869
+ width = width ?? this.canvas.parentElement?.clientWidth ?? this.width;
1870
+ height = height ?? this.canvas.parentElement?.clientHeight ?? this.height;
1871
+ if (this.canvas.width != width || this.canvas.height != height) {
1872
+ this.canvas.width = width;
1873
+ this.canvas.height = height;
1874
+ resized = true;
1561
1875
  }
1562
- return false;
1876
+ return resized;
1563
1877
  }
1564
1878
  clear() {
1565
1879
  this.context.clearRect(0, 0, this.width, this.height);
1566
1880
  }
1567
- drawRect(x, y, width, height, params = {}) {
1568
- if (this.drawCentered) {
1569
- x -= width / 2;
1570
- y -= height / 2;
1881
+ render(callback, camera) {
1882
+ camera = camera ?? { x: 0, y: 0, z: 1 };
1883
+ this.clear();
1884
+ this.context.save();
1885
+ this.context.translate(this.width / 2, this.height / 2);
1886
+ this.context.scale(camera.z, camera.z);
1887
+ this.context.translate(-camera.x, -camera.y);
1888
+ callback();
1889
+ this.context.restore();
1890
+ }
1891
+ drawPath(buildPath, params = {}) {
1892
+ this.context.fillStyle = params.color ?? DEFAULT_COLOR;
1893
+ this.context.strokeStyle = params.color ?? DEFAULT_COLOR;
1894
+ this.context.lineWidth = params.size ?? 1;
1895
+ if (params.lineDash) {
1896
+ this.context.setLineDash(params.lineDash);
1897
+ }
1898
+ this.context.beginPath();
1899
+ buildPath();
1900
+ this.context.stroke();
1901
+ if (params.filled) {
1902
+ this.context.fill();
1903
+ }
1904
+ if (params.lineDash) {
1905
+ this.context.setLineDash([]);
1571
1906
  }
1572
- this.context.strokeStyle = params.strokeColor ?? params.color ?? "white";
1573
- this.context.fillStyle = params.fillColor ?? params.color ?? "white";
1907
+ }
1908
+ drawRect(x, y, width, height, params = {}) {
1909
+ x -= width / 2;
1910
+ y -= height / 2;
1911
+ this.context.fillStyle = params.color ?? DEFAULT_COLOR;
1912
+ this.context.strokeStyle = params.color ?? DEFAULT_COLOR;
1574
1913
  this.context.lineWidth = params.size ?? 1;
1575
1914
  if (params.lineDash) {
1576
1915
  this.context.setLineDash(params.lineDash);
1577
1916
  }
1578
1917
  this.context.beginPath();
1579
- if (params.filled === true) {
1918
+ this.context.strokeRect(x, y, width, height);
1919
+ if (params.filled) {
1580
1920
  this.context.fillRect(x, y, width, height);
1581
- if (params.strokeColor !== void 0) this.context.strokeRect(x, y, width, height);
1582
- } else {
1583
- this.context.strokeRect(x, y, width, height);
1584
1921
  }
1585
1922
  if (params.lineDash) {
1586
1923
  this.context.setLineDash([]);
1587
1924
  }
1588
1925
  }
1589
1926
  drawPixel(x, y, params = {}) {
1590
- const size = abs(params.size || 1);
1927
+ const size = abs(params.size || DEFAULT_PIXEL_SIZE);
1591
1928
  this.drawRect(x, y, size, size, {
1592
1929
  ...params,
1593
1930
  filled: true
1594
1931
  });
1595
1932
  }
1596
1933
  drawLine(x1, y1, x2, y2, params = {}) {
1597
- this.context.strokeStyle = params.color ?? "white";
1934
+ this.context.strokeStyle = params.color ?? DEFAULT_COLOR;
1598
1935
  this.context.lineWidth = params.size ?? 1;
1599
1936
  if (params.lineDash) {
1600
1937
  this.context.setLineDash(params.lineDash);
@@ -1608,27 +1945,49 @@ var CanvasRenderer = class {
1608
1945
  }
1609
1946
  }
1610
1947
  drawCircle(x, y, radius, params = {}) {
1611
- this.context.strokeStyle = params.strokeColor ?? params.color ?? "white";
1612
- this.context.fillStyle = params.fillColor ?? params.color ?? "white";
1613
- this.context.lineWidth = params.size ?? 1;
1614
- if (params.lineDash) {
1615
- this.context.setLineDash(params.lineDash);
1616
- }
1617
- this.context.beginPath();
1618
- this.context.arc(x, y, radius, 0, 2 * PI);
1619
- if (params.filled === true) this.context.fill();
1620
- this.context.stroke();
1621
- if (params.lineDash) {
1622
- this.context.setLineDash([]);
1623
- }
1948
+ this.drawPath(() => {
1949
+ this.context.arc(x, y, radius, 0, TAU);
1950
+ }, params);
1951
+ }
1952
+ drawPolygon(x, y, radius, sides, params = {}) {
1953
+ sides = floor(clamp(sides, 3, 36));
1954
+ this.drawPath(() => {
1955
+ for (let i = 0; i < sides; i++) {
1956
+ const angle = i / sides * TAU - PI / 2;
1957
+ const px = x + cos(angle) * radius;
1958
+ const py = y + sin(angle) * radius;
1959
+ if (i === 0) {
1960
+ this.context.moveTo(px, py);
1961
+ } else {
1962
+ this.context.lineTo(px, py);
1963
+ }
1964
+ }
1965
+ this.context.closePath();
1966
+ }, params);
1624
1967
  }
1625
1968
  drawText(x, y, text, params = {}) {
1626
- this.context.font = `${params.size ?? 14}px ${params.fontName ?? "monospace"}`;
1627
- this.context.textAlign = params.textAlign ?? "left";
1628
- this.context.textBaseline = params.textBaseline ?? "alphabetic";
1629
- this.context.fillStyle = params.color ?? "white";
1969
+ this.context.fillStyle = params.color ?? DEFAULT_COLOR;
1630
1970
  this.context.fillText(text, x, y);
1631
1971
  }
1972
+ drawSprite(x, y, image, width, height, params = {}) {
1973
+ x -= width / 2;
1974
+ y -= height / 2;
1975
+ if (params.sourceWidth !== void 0 && params.sourceHeight !== void 0) {
1976
+ this.context.drawImage(
1977
+ image,
1978
+ params.sourceX ?? 0,
1979
+ params.sourceY ?? 0,
1980
+ params.sourceWidth,
1981
+ params.sourceHeight,
1982
+ x,
1983
+ y,
1984
+ width,
1985
+ height
1986
+ );
1987
+ } else {
1988
+ this.context.drawImage(image, x, y, width, height);
1989
+ }
1990
+ }
1632
1991
  };
1633
1992
 
1634
1993
  // src/rendering/particle-renderer.ts
@@ -1860,6 +2219,7 @@ var AtlasTextureMaterial = class _AtlasTextureMaterial extends import_three2.Mes
1860
2219
  };
1861
2220
 
1862
2221
  // src/rendering/three/threejs-boiler-plate.ts
2222
+ init_main();
1863
2223
  var import_three4 = require("three");
1864
2224
 
1865
2225
  // src/rendering/three/threejs-camera-rig.ts
@@ -1872,8 +2232,8 @@ var ThreeJsCameraRig = class extends import_three3.Group {
1872
2232
  wheelSensitivity = 0.02;
1873
2233
  minCameraDistance = 2;
1874
2234
  maxCameraDistance = 100;
1875
- minTiltAngle = deg2rad(-90);
1876
- maxTiltAngle = deg2rad(0);
2235
+ minTiltAngle = degrees(-90);
2236
+ maxTiltAngle = degrees(0);
1877
2237
  clampTiltAngle = false;
1878
2238
  constructor(params) {
1879
2239
  super();
@@ -1908,7 +2268,7 @@ var ThreeJsCameraRig = class extends import_three3.Group {
1908
2268
  var ThreeJsBoilerPlate = class {
1909
2269
  clock = new Clock();
1910
2270
  scene = new import_three4.Scene();
1911
- input = new Input();
2271
+ input = Input.instance;
1912
2272
  raycaster = new import_three4.Raycaster();
1913
2273
  renderer;
1914
2274
  cameraRig;
@@ -1932,11 +2292,12 @@ var ThreeJsBoilerPlate = class {
1932
2292
  enableCameraRigControls() {
1933
2293
  this.orbitControls?.dispose();
1934
2294
  this.orbitControls = void 0;
1935
- this.input.on("mouse_move", ({ deltaX, deltaY }) => {
2295
+ this.input.onPointerMove.subscribe((_, ev) => {
1936
2296
  if (this.input.isDown("Button0")) {
1937
- this.cameraRig.orbit(deltaX, deltaY);
2297
+ this.cameraRig.orbit(ev.movementX, ev.movementY);
1938
2298
  }
1939
- }).on("mouse_wheel", ({ deltaY }) => this.cameraRig.dolly(deltaY));
2299
+ });
2300
+ this.input.onWheel.subscribe((_, ev) => this.cameraRig.dolly(ev.deltaY));
1940
2301
  }
1941
2302
  async enableOrbitControls() {
1942
2303
  const { OrbitControls } = await import("three/examples/jsm/Addons.js");
@@ -2485,16 +2846,16 @@ var ParticleSystem3d = class extends import_three7.Group {
2485
2846
  this.add(pointsMesh);
2486
2847
  for (let i = 0; i < this.particleCount; i++) {
2487
2848
  this._particles.push({
2488
- x: rng.nextf,
2489
- y: rng.nextf,
2490
- z: rng.nextf,
2849
+ x: rng.nextFloat(),
2850
+ y: rng.nextFloat(),
2851
+ z: rng.nextFloat(),
2491
2852
  vx: 0,
2492
2853
  vy: 0,
2493
2854
  vz: 0,
2494
2855
  color: rng.range(Colors2.length)
2495
2856
  });
2496
2857
  }
2497
- this._attractionMatrix = rng.randomMatrix(Colors2.length);
2858
+ this._attractionMatrix = randomMatrix(Colors2.length);
2498
2859
  this._spatialPartition = new SpatialPartition3d(this._range, this._particles);
2499
2860
  }
2500
2861
  _calcForce(r, a, beta = 0.3) {
@@ -2624,8 +2985,8 @@ var AsteroidMesh = class extends import_three8.Mesh {
2624
2985
  const geometry = new import_three8.IcosahedronGeometry(radius, detail);
2625
2986
  const material = new import_three8.MeshStandardMaterial({ color });
2626
2987
  super(geometry, material);
2627
- const noise3d = (0, import_simplex_noise2.createNoise3D)(() => rng.nextf);
2628
- const noise3d2 = secondaryNoise ? (0, import_simplex_noise2.createNoise3D)(() => rng.nextf) : null;
2988
+ const noise3d = (0, import_simplex_noise2.createNoise3D)(rng.rng.float);
2989
+ const noise3d2 = secondaryNoise ? (0, import_simplex_noise2.createNoise3D)(rng.rng.float) : null;
2629
2990
  const position = this.geometry.attributes.position;
2630
2991
  const vertices = position.array;
2631
2992
  for (let i = 0; i < vertices.length; i += 3) {
@@ -2651,8 +3012,8 @@ function CreateStarfield(radius = 1e3, count = 5e3, color = 16777215) {
2651
3012
  const geometry = new import_three9.BufferGeometry();
2652
3013
  const positions = new Float32Array(count * 3);
2653
3014
  for (let i = 0; i < count; i++) {
2654
- const theta = rng.nextf * Math.PI * 2;
2655
- const phi = Math.acos(rng.nextf * 2 - 1);
3015
+ const theta = rng.nextFloat() * Math.PI * 2;
3016
+ const phi = Math.acos(rng.nextFloat() * 2 - 1);
2656
3017
  const x = radius * Math.sin(phi) * Math.cos(theta);
2657
3018
  const y = radius * Math.sin(phi) * Math.sin(theta);
2658
3019
  const z = radius * Math.cos(phi);
@@ -2930,6 +3291,30 @@ var WebGlRenderer = class {
2930
3291
  }
2931
3292
  };
2932
3293
 
3294
+ // src/signal.ts
3295
+ var Signal = class {
3296
+ value;
3297
+ subscribers = /* @__PURE__ */ new Set();
3298
+ constructor(initialValue) {
3299
+ this.value = initialValue;
3300
+ }
3301
+ get() {
3302
+ return this.value;
3303
+ }
3304
+ set(value) {
3305
+ if (Object.is(this.value, value)) return;
3306
+ this.value = value;
3307
+ for (const subscriber of [...this.subscribers]) {
3308
+ subscriber(value);
3309
+ }
3310
+ }
3311
+ subscribe(subscriber) {
3312
+ this.subscribers.add(subscriber);
3313
+ subscriber(this.value);
3314
+ return () => this.subscribers.delete(subscriber);
3315
+ }
3316
+ };
3317
+
2933
3318
  // src/simplex-noise.ts
2934
3319
  var SimplexNoise = class {
2935
3320
  grad3;
@@ -2952,7 +3337,7 @@ var SimplexNoise = class {
2952
3337
  ];
2953
3338
  this.p = [];
2954
3339
  for (let i = 0; i < 256; i++) {
2955
- this.p.push(Math.floor(rng.nextf * 256));
3340
+ this.p.push(Math.floor(rng.nextFloat() * 256));
2956
3341
  }
2957
3342
  this.perm = [];
2958
3343
  for (let i = 0; i < 512; i++) {
@@ -3143,13 +3528,14 @@ var Tilemap = class {
3143
3528
  };
3144
3529
 
3145
3530
  // src/worker-interface.ts
3146
- var WorkerInterface = class extends Emitter {
3531
+ var WorkerInterface = class {
3147
3532
  _worker;
3533
+ onError = new Observable();
3534
+ onMessage = new Observable();
3148
3535
  constructor(url) {
3149
- super();
3150
3536
  this._worker = new Worker(url, { type: "module" });
3151
- this._worker.onerror = (error) => this.emit("error", error);
3152
- this._worker.onmessage = (message) => this.emit("message", message);
3537
+ this._worker.onerror = (error) => this.onError.notify(this, error);
3538
+ this._worker.onmessage = (message) => this.onMessage.notify(this, message);
3153
3539
  }
3154
3540
  postMessage(message, options) {
3155
3541
  this._worker.postMessage(message, options);
@@ -3179,9 +3565,18 @@ var WorkerInterface = class extends Emitter {
3179
3565
  CreateThreeEntity,
3180
3566
  CreateWebGlContext,
3181
3567
  CubeSphereGeometry,
3182
- DOUBLE_PI,
3568
+ DEFAULT_COLOR,
3569
+ DEFAULT_FONT,
3570
+ DEFAULT_PIXEL_SIZE,
3571
+ DEFAULT_TEXTALIGN,
3572
+ DEFAULT_TEXTBASELINE,
3573
+ DEG2RAD,
3574
+ DOWN,
3575
+ Disposable,
3576
+ E,
3183
3577
  ECS,
3184
- Emitter,
3578
+ EPSILON,
3579
+ EventQueue,
3185
3580
  GenerateDoors,
3186
3581
  GenerateEntityId,
3187
3582
  GenerateRooms,
@@ -3192,24 +3587,35 @@ var WorkerInterface = class extends Emitter {
3192
3587
  HALF_PI,
3193
3588
  Input,
3194
3589
  IsThreeEntity,
3590
+ LN10,
3591
+ LN2,
3592
+ LOG10E,
3593
+ LOG2E,
3195
3594
  LoadTexture,
3196
3595
  Map2D,
3197
3596
  Map3D,
3198
3597
  Noise,
3598
+ Observable,
3199
3599
  PI,
3200
3600
  ParticleRenderer,
3201
3601
  ParticleSystem2d,
3202
3602
  ParticleSystem3d,
3203
3603
  PoissonDiskSampler,
3204
- RADIAN_FACTOR,
3205
- RandomNumberGenerator,
3604
+ QUARTER_PI,
3605
+ RAD2DEG,
3606
+ Random,
3607
+ RandomTools,
3206
3608
  Rectangle,
3207
3609
  RemoveThreeComponent,
3208
3610
  ResizeWebGlContext,
3611
+ SQRT1_2,
3612
+ SQRT2,
3209
3613
  ShaderType,
3614
+ Signal,
3210
3615
  SimplexNoise,
3211
3616
  SpatialPartition2d,
3212
3617
  SpatialPartition3d,
3618
+ TAU,
3213
3619
  TILE_EMPTY,
3214
3620
  TILE_FLOOR,
3215
3621
  TILE_PATH,
@@ -3218,39 +3624,71 @@ var WorkerInterface = class extends Emitter {
3218
3624
  ThreeJsCameraRig,
3219
3625
  ThreeJsPlayerController,
3220
3626
  Tilemap,
3627
+ UP,
3221
3628
  Vector,
3222
3629
  VoxelFaces,
3223
3630
  VoxelMesh,
3224
3631
  WebGlRenderer,
3225
3632
  WorkerInterface,
3226
3633
  abs,
3634
+ acos,
3635
+ asin,
3636
+ atan,
3637
+ atan2,
3638
+ cbrt,
3227
3639
  ceil,
3228
3640
  clamp,
3229
3641
  cos,
3230
- deg2rad,
3642
+ cosh,
3643
+ degrees,
3231
3644
  distance2d,
3232
3645
  distance3d,
3646
+ exp,
3647
+ expm1,
3233
3648
  floor,
3649
+ fract,
3234
3650
  getRedYellowGreenGradientHex,
3651
+ hypot,
3235
3652
  hypot2,
3236
3653
  hypot3,
3237
3654
  imul,
3655
+ inverseLerp,
3656
+ isFinite,
3657
+ isInteger,
3658
+ isNaN,
3238
3659
  isNullOrUndefined,
3239
3660
  isPositiveString,
3240
3661
  k2xy,
3241
3662
  k2xyz,
3242
3663
  lerp,
3243
3664
  log,
3665
+ log10,
3666
+ log1p,
3667
+ log2,
3244
3668
  manhattanDistance,
3669
+ map,
3245
3670
  max,
3246
3671
  min,
3672
+ mod,
3673
+ nearlyEqual,
3674
+ normalizeRadians,
3247
3675
  pow,
3676
+ radians,
3677
+ randomMatrix,
3248
3678
  rng,
3249
3679
  round,
3250
3680
  roundTo,
3681
+ saturate,
3682
+ sign,
3251
3683
  sin,
3684
+ sinh,
3685
+ smoothstep,
3252
3686
  sqrt,
3253
3687
  squaredDistance,
3688
+ tan,
3689
+ tanh,
3690
+ trunc,
3691
+ wrap,
3254
3692
  xy2k,
3255
3693
  xyz2k
3256
3694
  });