@n3rdw1z4rd/core 0.14.1 → 0.16.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/dist/index.js CHANGED
@@ -37,7 +37,6 @@ __export(index_exports, {
37
37
  BuildDefaultPalette: () => BuildDefaultPalette,
38
38
  CanvasRenderer: () => CanvasRenderer,
39
39
  Clock: () => Clock,
40
- ClockStats: () => ClockStats,
41
40
  Color: () => Color,
42
41
  Colors: () => Colors,
43
42
  CompileShader: () => CompileShader,
@@ -50,16 +49,17 @@ __export(index_exports, {
50
49
  CreateThreeEntity: () => CreateThreeEntity,
51
50
  CreateWebGlContext: () => CreateWebGlContext,
52
51
  CubeSphereGeometry: () => CubeSphereGeometry,
52
+ DOUBLE_PI: () => DOUBLE_PI,
53
53
  ECS: () => ECS,
54
54
  Emitter: () => Emitter,
55
- EventBus: () => EventBus,
56
55
  GenerateDoors: () => GenerateDoors,
57
56
  GenerateEntityId: () => GenerateEntityId,
58
57
  GenerateRooms: () => GenerateRooms,
59
58
  GenerateWalls: () => GenerateWalls,
60
59
  GetProgramLocations: () => GetProgramLocations,
61
60
  GetThreeComponent: () => GetThreeComponent,
62
- GlobalEmitter: () => GlobalEmitter,
61
+ GetUrlParams: () => GetUrlParams,
62
+ HALF_PI: () => HALF_PI,
63
63
  Input: () => Input,
64
64
  IsThreeEntity: () => IsThreeEntity,
65
65
  LoadTexture: () => LoadTexture,
@@ -67,14 +67,15 @@ __export(index_exports, {
67
67
  Map3D: () => Map3D,
68
68
  Noise: () => Noise,
69
69
  PI: () => PI,
70
+ ParticleRenderer: () => ParticleRenderer,
70
71
  ParticleSystem2d: () => ParticleSystem2d,
71
72
  ParticleSystem3d: () => ParticleSystem3d,
72
73
  PoissonDiskSampler: () => PoissonDiskSampler,
74
+ RADIAN_FACTOR: () => RADIAN_FACTOR,
75
+ RandomNumberGenerator: () => RandomNumberGenerator,
73
76
  Rectangle: () => Rectangle,
74
77
  RemoveThreeComponent: () => RemoveThreeComponent,
75
- Renderer: () => Renderer,
76
78
  ResizeWebGlContext: () => ResizeWebGlContext,
77
- Rng: () => Rng,
78
79
  ShaderType: () => ShaderType,
79
80
  SimplexNoise: () => SimplexNoise,
80
81
  SpatialPartition2d: () => SpatialPartition2d,
@@ -97,20 +98,19 @@ __export(index_exports, {
97
98
  clamp: () => clamp,
98
99
  cos: () => cos,
99
100
  deg2rad: () => deg2rad,
100
- distance: () => distance,
101
- eventBus: () => eventBus,
101
+ distance2d: () => distance2d,
102
+ distance3d: () => distance3d,
102
103
  floor: () => floor,
103
104
  getRedYellowGreenGradientHex: () => getRedYellowGreenGradientHex,
104
105
  hypot2: () => hypot2,
105
106
  hypot3: () => hypot3,
106
107
  imul: () => imul,
107
108
  isNullOrUndefined: () => isNullOrUndefined,
109
+ isPositiveString: () => isPositiveString,
110
+ k2xy: () => k2xy,
111
+ k2xyz: () => k2xyz,
108
112
  lerp: () => lerp,
109
113
  log: () => log,
110
- logdev: () => logdev,
111
- logerr: () => logerr,
112
- loginf: () => loginf,
113
- logwrn: () => logwrn,
114
114
  manhattanDistance: () => manhattanDistance,
115
115
  max: () => max,
116
116
  min: () => min,
@@ -120,7 +120,9 @@ __export(index_exports, {
120
120
  roundTo: () => roundTo,
121
121
  sin: () => sin,
122
122
  sqrt: () => sqrt,
123
- squaredDistance: () => squaredDistance
123
+ squaredDistance: () => squaredDistance,
124
+ xy2k: () => xy2k,
125
+ xyz2k: () => xyz2k
124
126
  });
125
127
  module.exports = __toCommonJS(index_exports);
126
128
 
@@ -146,10 +148,6 @@ var AStar = class {
146
148
  _key(x, y) {
147
149
  return `${x},${y}`;
148
150
  }
149
- // NOTE: ported as-is from rogue-craft. useAdjacent is currently hard-coded
150
- // to true below - the original had a known unresolved bug where setting it
151
- // to false via params caused an infinite loop, so that path is disabled
152
- // rather than exposed broken.
153
151
  findPath(a, b, params = {}) {
154
152
  const useAdjacent = true;
155
153
  const walkable = params.walkableValues ?? [0];
@@ -220,198 +218,131 @@ var AStar = class {
220
218
  }
221
219
  };
222
220
 
223
- // src/clock-stats.ts
224
- var ClockStats = class {
225
- divElement;
226
- keyColor = "gray";
227
- valueColor = "lightgray";
228
- constructor(align = "bottom", justify = "right") {
229
- this.divElement = document.createElement("div");
230
- this.divElement.setAttribute("id", "clock-stats");
231
- this.divElement.style.setProperty("position", "absolute");
232
- this.divElement.style.setProperty(align, "4px");
233
- this.divElement.style.setProperty(justify, "4px");
221
+ // src/emitter.ts
222
+ var Emitter = class {
223
+ listeners = /* @__PURE__ */ new Map();
224
+ constructor() {
234
225
  }
235
- appendTo(target) {
236
- this.divElement.parentNode?.removeChild(this.divElement);
237
- if (target) {
238
- target.appendChild(this.divElement);
239
- }
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);
234
+ });
235
+ return this;
240
236
  }
241
- update(data) {
242
- this.divElement.innerHTML = Object.entries(data).map(
243
- ([key, value]) => `<span style="color: ${this.keyColor};">${key}:</span>&nbsp;<span style="color: ${this.valueColor};">${value}</span>`
244
- ).join("<br/>");
237
+ emit(eventName, ...args) {
238
+ this.listeners.get(eventName)?.forEach((listener) => listener(...args, eventName));
239
+ return this;
240
+ }
241
+ clear(eventName) {
242
+ if (eventName) {
243
+ this.listeners.delete(eventName);
244
+ } else {
245
+ this.listeners.clear();
246
+ }
247
+ return this;
245
248
  }
246
249
  };
247
250
 
248
251
  // src/clock.ts
249
- var Clock = class {
250
- _lastFrameTime = 0;
251
- _daltaTimeMilliseconds = 0;
252
- _deltaTimeSeconds = 0;
253
- _avgDeltaTime = 0;
254
- _frameCount = 0;
255
- _frameCountTotal = 0;
252
+ var Clock = class extends Emitter {
253
+ _startTime = 0;
254
+ _time = 0;
255
+ _deltaTime = 0;
256
256
  _frameTime = 0;
257
+ _frameCount = 0;
257
258
  _fps = 0;
258
259
  _isRunning = false;
259
- _updateCallback = () => {
260
- };
261
- _statsParent;
262
- _stats;
263
- _startTime = 0;
264
- get fps() {
265
- return this._fps;
266
- }
267
- get frames() {
268
- return this._frameCountTotal;
269
- }
270
- get deltaTimeSeconds() {
271
- return this._deltaTimeSeconds;
272
- }
273
- get deltaTimeMilliseconds() {
274
- return this._daltaTimeMilliseconds;
275
- }
276
- get avgDeltaTime() {
277
- return this._avgDeltaTime;
278
- }
279
260
  get time() {
280
- return this._lastFrameTime;
261
+ return this._time;
281
262
  }
282
- get isRunning() {
283
- return this._isRunning;
263
+ get deltaTime() {
264
+ return this._deltaTime;
284
265
  }
285
- get elapsedTimeSinceStart() {
266
+ get elapsedTime() {
286
267
  return performance.now() - this._startTime;
287
268
  }
288
- constructor(statsDivParent) {
289
- this._statsParent = statsDivParent;
290
- }
291
- _update(time) {
292
- this.update(time);
293
- this._updateCallback(this._deltaTimeSeconds);
294
- if (this._isRunning) {
295
- requestAnimationFrame(this._update.bind(this));
296
- }
269
+ get fps() {
270
+ return this._fps;
297
271
  }
298
- update(time) {
299
- this._daltaTimeMilliseconds = time - this._lastFrameTime;
300
- this._deltaTimeSeconds = this._daltaTimeMilliseconds / 1e3;
301
- this._lastFrameTime = time;
302
- if (this._frameTime + 1e3 >= time) {
303
- this._frameCount += 1;
304
- } else {
305
- this._frameTime = time;
306
- this._fps = this._frameCount;
307
- this._frameCount = 0;
308
- }
309
- this._frameCountTotal++;
310
- this._avgDeltaTime = (this._avgDeltaTime * this._frameCount + this._daltaTimeMilliseconds) / (this._frameCount + 1);
272
+ get isRunning() {
273
+ return this._isRunning;
311
274
  }
312
- run(callback) {
275
+ start() {
313
276
  if (!this._isRunning) {
314
- this._frameCountTotal = 0;
315
277
  this._startTime = performance.now();
316
278
  this._isRunning = true;
317
- this._updateCallback = callback;
318
- requestAnimationFrame(this._update.bind(this));
279
+ const update = (time) => {
280
+ this._deltaTime = (time - this._time) / 1e3;
281
+ this._time = time;
282
+ if (this._frameTime + 1e3 >= time) {
283
+ this._frameCount += 1;
284
+ } else {
285
+ this._frameTime = time;
286
+ this._fps = this._frameCount;
287
+ this._frameCount = 0;
288
+ }
289
+ this.emit("frame", this._deltaTime);
290
+ if (this._isRunning) {
291
+ requestAnimationFrame(update);
292
+ }
293
+ };
294
+ requestAnimationFrame(update);
319
295
  }
320
- }
321
- runOnce(callback) {
322
- this._isRunning = false;
323
- this._updateCallback = callback;
324
- this._update(0);
296
+ return this;
325
297
  }
326
298
  stop() {
327
299
  this._isRunning = false;
328
300
  }
329
- getExecuteTime(func) {
330
- const now = performance.now();
331
- func();
332
- return performance.now() - now;
333
- }
334
- showStats(data = {}, parent) {
335
- parent = parent ?? this._statsParent ?? document.body;
336
- if (!this._stats) {
337
- this._stats = new ClockStats();
338
- this._stats.appendTo(parent);
339
- }
340
- this._stats.update({
341
- fps: this._fps,
342
- "deltaTime(s)": `${this._deltaTimeSeconds.toFixed(3)}`,
343
- "avgDeltaTime(ms)": `${this._avgDeltaTime.toFixed(3)}`,
344
- ...data
345
- });
346
- }
347
301
  };
348
302
 
349
- // src/logger.ts
350
- var logdev = console.debug.bind(console, "%c[dbg]", "font-weight: bold; color: #888;");
351
- var log = logdev;
352
- var loginf = console.log.bind(console, "%c[inf]", "font-weight: bold;");
353
- var logwrn = console.log.bind(console, "%c[wrn]", "font-weight: bold; color: orange;");
354
- var logerr = console.log.bind(console, "%c[err]", "font-weight: bold; color: red;");
355
-
356
- // src/math.ts
357
- var DEG2RAD = 0.01745329;
303
+ // src/math/math.ts
358
304
  var PI = 3.141592653589793;
359
- var TWO_PI = PI * 2;
305
+ var DOUBLE_PI = PI * 2;
360
306
  var HALF_PI = PI / 2;
361
- function abs(n) {
362
- return n < 0 ? -n : n;
363
- }
364
- function min(a, b) {
365
- return a < b ? a : b;
366
- }
367
- function max(a, b) {
368
- return a > b ? a : b;
369
- }
370
- function floor(n) {
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) => {
371
312
  const t = n | 0;
372
313
  return n < 0 && t !== n ? t - 1 : t;
373
- }
374
- function ceil(n) {
314
+ };
315
+ var ceil = (n) => {
375
316
  const t = n | 0;
376
317
  return n > 0 && t !== n ? t + 1 : t;
377
- }
378
- function round(n) {
379
- return floor(n + 0.5);
380
- }
381
- function sqrt(n) {
382
- return n ** 0.5;
383
- }
384
- function pow(base, exp) {
385
- return base ** exp;
386
- }
387
- function hypot2(x, y) {
388
- return (x * x + y * y) ** 0.5;
389
- }
390
- function hypot3(x, y, z) {
391
- return (x * x + y * y + z * z) ** 0.5;
392
- }
393
- function imul(a, b) {
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) => {
394
325
  b |= 0;
395
326
  let result = (a & 4194303) * b;
396
327
  if (a & 4290772992) result += (a & 4290772992) * b | 0;
397
328
  return result | 0;
398
- }
399
- function wrapAngle(n) {
400
- n = n % TWO_PI;
401
- if (n > PI) n -= TWO_PI;
402
- else if (n < -PI) n += TWO_PI;
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;
403
334
  return n;
404
- }
405
- function sinSeries(n) {
335
+ };
336
+ var _sinSeries = (n) => {
406
337
  const n2 = n * n;
407
338
  return n * (1 + n2 * (-1 / 6 + n2 * (1 / 120 + n2 * (-1 / 5040 + n2 * (1 / 362880 + n2 * (-1 / 39916800))))));
408
- }
409
- function cosSeries(n) {
339
+ };
340
+ var _cosSeries = (n) => {
410
341
  const n2 = n * n;
411
342
  return 1 + n2 * (-1 / 2 + n2 * (1 / 24 + n2 * (-1 / 720 + n2 * (1 / 40320 + n2 * (-1 / 3628800)))));
412
- }
413
- function reduceAngle(n) {
414
- let x = wrapAngle(n);
343
+ };
344
+ var _reduceAngle = (n) => {
345
+ let x = _wrapAngle(n);
415
346
  let sinSign = 1;
416
347
  if (x < 0) {
417
348
  x = -x;
@@ -423,287 +354,115 @@ function reduceAngle(n) {
423
354
  cosSign = -1;
424
355
  }
425
356
  return [x, sinSign, cosSign];
426
- }
427
- function sin(n) {
428
- const [x, sinSign] = reduceAngle(n);
429
- return sinSign * sinSeries(x);
430
- }
431
- function cos(n) {
432
- const [x, , cosSign] = reduceAngle(n);
433
- return cosSign * cosSeries(x);
434
- }
435
- function clamp(value, lo, hi) {
436
- return max(lo, min(hi, value));
437
- }
438
- function deg2rad(value) {
439
- return value * DEG2RAD;
440
- }
441
- function lerp(start, end, scale) {
442
- return start * (1 - scale) + end * scale;
443
- }
444
- function distance(x1, y1, x2, y2) {
445
- return hypot2(x2 - x1, y2 - y1);
446
- }
447
- function squaredDistance(x1, y1, x2, y2) {
357
+ };
358
+ var sin = (n) => {
359
+ const [x, sinSign] = _reduceAngle(n);
360
+ return sinSign * _sinSeries(x);
361
+ };
362
+ var cos = (n) => {
363
+ const [x, , cosSign] = _reduceAngle(n);
364
+ return cosSign * _cosSeries(x);
365
+ };
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) => {
448
372
  const dx = x1 - x2;
449
373
  const dy = y1 - y2;
450
374
  return dx * dx + dy * dy;
451
- }
452
- function manhattanDistance(x1, y1, x2, y2) {
453
- return abs(x1 - x2) + abs(y1 - y2);
454
- }
455
- function roundTo(value, digits, base = 10) {
375
+ };
376
+ var manhattanDistance = (x1, y1, x2, y2) => abs(x1 - x2) + abs(y1 - y2);
377
+ var roundTo = (value, digits, base = 10) => {
456
378
  const p = pow(base, digits);
457
379
  return round(value * p) / p;
458
- }
380
+ };
459
381
 
460
- // src/color.ts
461
- var Color = class _Color {
462
- _hex = "#ffffffff";
463
- _r = 255;
464
- _g = 255;
465
- _b = 255;
466
- _a = 255;
467
- get r() {
468
- return this._r;
469
- }
470
- set r(value) {
471
- this._r = clamp(value, 0, 255);
472
- this._hex = this._to_hex();
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;
473
393
  }
474
- get g() {
475
- return this._g;
394
+ area() {
395
+ return this.width * this.height;
476
396
  }
477
- set g(value) {
478
- this._g = clamp(value, 0, 255);
479
- this._hex = this._to_hex();
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;
480
399
  }
481
- get b() {
482
- return this._b;
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);
483
402
  }
484
- set b(value) {
485
- this._b = clamp(value, 0, 255);
486
- this._hex = this._to_hex();
403
+ center() {
404
+ return new Vector(
405
+ this.x + this.width / 2,
406
+ this.y + this.height / 2
407
+ );
487
408
  }
488
- get a() {
489
- return this._a;
409
+ distanceTo(other) {
410
+ return this.center().distanceTo(other.center());
490
411
  }
491
- set a(value) {
492
- this._a = clamp(value, 0, 255);
493
- this._hex = this._to_hex();
412
+ clone() {
413
+ return new _Rectangle(this.x, this.y, this.width, this.height);
494
414
  }
495
- get hexStr() {
496
- return this._hex;
415
+ equals(other) {
416
+ return this.x === other.x && this.y === other.y && this.width === other.width && this.height === other.height;
497
417
  }
498
- set hexStr(value) {
499
- if (value.startsWith("#")) {
500
- [this._r, this._g, this._b, this._a] = this._parse_hex_color_string(value);
501
- this._hex = value;
502
- } else {
503
- logwrn(`Color: invalid color string: ${value}. Should be a hexadecimal color string (i.e.: '#ffffff' or '#ffffffff'). Keeping the current color.`);
504
- }
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
424
+ );
505
425
  }
506
- get rgba() {
507
- return [this._r, this._g, this._b, this._a];
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;
508
428
  }
509
- constructor(r = 255, g = 255, b = 255, a = 255) {
510
- if (typeof r === "string") {
511
- if (r.startsWith("#")) {
512
- [r, g, b, a] = this._parse_hex_color_string(r);
513
- } else {
514
- logwrn(`Color: invalid color string: ${r}. Should be a hexadecimal color string (i.e.: '#ffffff' or '#ffffffff'). Defaulting to opaque white.`);
515
- r = g = b = a = 255;
516
- }
517
- } else {
518
- [r, g, b, a] = [r, g, b, a].map((n) => _Color._normalize(n));
519
- }
520
- this.r = r;
521
- this.g = g;
522
- this.b = b;
523
- this.a = a;
429
+ };
430
+
431
+ // src/math/vector.ts
432
+ var Vector = class _Vector {
433
+ x;
434
+ y;
435
+ constructor(x = 0, y = 0) {
436
+ this.x = x;
437
+ this.y = y;
524
438
  }
525
- static _normalize(value) {
526
- return clamp(0 | (value > 0 && value < 1 ? value * 255 : value), 0, 255);
439
+ clone() {
440
+ return new _Vector(this.x, this.y);
527
441
  }
528
- _to_hex() {
529
- return `#${this.r.toString(16).padStart(2, "0")}${this.g.toString(16).padStart(2, "0")}${this.b.toString(16).padStart(2, "0")}${this.a.toString(16).padStart(2, "0")}`;
442
+ distanceTo(that) {
443
+ return distance2d(
444
+ this.x,
445
+ this.y,
446
+ that.x,
447
+ that.y
448
+ );
530
449
  }
531
- _parse_hex_color_string(hex) {
532
- const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})?$/i.exec(hex);
533
- return result ? [
534
- parseInt(result[1], 16),
535
- parseInt(result[2], 16),
536
- parseInt(result[3], 16),
537
- result[4] ? parseInt(result[4], 16) : 255
538
- ] : [255, 255, 255, 255];
450
+ floored() {
451
+ return new _Vector(
452
+ Math.floor(this.x),
453
+ Math.floor(this.y)
454
+ );
539
455
  }
540
- static fromHsv(h, s, v, a = 255) {
541
- let r = 0;
542
- let g = 0;
543
- let b = 0;
544
- let i = Math.floor(h * 6);
545
- let f = h * 6 - i;
546
- let p = v * (1 - s);
547
- let q = v * (1 - f * s);
548
- let t = v * (1 - (1 - f) * s);
549
- switch (i % 6) {
550
- case 0:
551
- r = v, g = t, b = p;
552
- break;
553
- case 1:
554
- r = q, g = v, b = p;
555
- break;
556
- case 2:
557
- r = p, g = v, b = t;
558
- break;
559
- case 3:
560
- r = p, g = q, b = v;
561
- break;
562
- case 4:
563
- r = t, g = p, b = v;
564
- break;
565
- case 5:
566
- r = v, g = p, b = q;
567
- break;
456
+ plus(x, y) {
457
+ if (x instanceof _Vector) {
458
+ y = x.y;
459
+ x = x.x;
460
+ } else if (y === void 0) {
461
+ y = x;
568
462
  }
569
- return new _Color(Math.round(r * 255), Math.round(g * 255), Math.round(b * 255), a);
570
- }
571
- static get BLACK() {
572
- return new _Color(0, 0, 0, 255);
573
- }
574
- static get GRAY() {
575
- return new _Color(128, 128, 128, 255);
576
- }
577
- static get WHITE() {
578
- return new _Color(255, 255, 255, 255);
579
- }
580
- static get RED() {
581
- return new _Color(255, 0, 0, 255);
582
- }
583
- static get GREEN() {
584
- return new _Color(0, 255, 0, 255);
585
- }
586
- static get BLUE() {
587
- return new _Color(0, 0, 255, 255);
588
- }
589
- static get YELLOW() {
590
- return new _Color(255, 255, 0, 255);
591
- }
592
- static get ORANGE() {
593
- return new _Color(255, 128, 0, 255);
594
- }
595
- static get PURPLE() {
596
- return new _Color(128, 0, 128, 255);
597
- }
598
- static get CYAN() {
599
- return new _Color(0, 255, 255, 255);
600
- }
601
- static get MAGENTA() {
602
- return new _Color(255, 0, 255, 255);
603
- }
604
- static fromName(name) {
605
- const element = document.createElement("div");
606
- element.style.color = name;
607
- document.body.appendChild(element);
608
- const computedColor = getComputedStyle(element).color;
609
- document.body.removeChild(element);
610
- const rgba = computedColor.match(/\d+/g);
611
- if (rgba && rgba.length === 4) {
612
- const [r, g, b, a] = rgba.map(Number);
613
- return new _Color(r, g, b, a);
614
- } else {
615
- logwrn(`Color: invalid CSS color name: ${name}. Defaulting to transparent.`);
616
- return _Color.TRANSPARENT;
617
- }
618
- }
619
- static get TRANSPARENT() {
620
- return new _Color(0, 0, 0, 0);
621
- }
622
- };
623
-
624
- // src/dungeon-generator.ts
625
- var import_heap_js = require("heap-js");
626
-
627
- // src/map2d.ts
628
- var DEFAULT_VALUE = -1;
629
- var Map2D = class {
630
- defaultValue;
631
- _data;
632
- get size() {
633
- return this._data.size;
634
- }
635
- constructor(defaultValue = DEFAULT_VALUE) {
636
- this.defaultValue = defaultValue;
637
- this._data = /* @__PURE__ */ new Map();
638
- }
639
- _floor(x, y) {
640
- return [floor(x), floor(y)];
641
- }
642
- _key(x, y) {
643
- return `${x},${y}`;
644
- }
645
- _pos(key) {
646
- return key.split(",").map((v) => parseInt(v));
647
- }
648
- clear() {
649
- this._data.clear();
650
- }
651
- get(x, y, defaultValue = this.defaultValue) {
652
- [x, y] = this._floor(x, y);
653
- return this._data.get(this._key(x, y)) ?? defaultValue;
654
- }
655
- set(x, y, value = this.defaultValue) {
656
- [x, y] = this._floor(x, y);
657
- const key = this._key(x, y);
658
- if (value === this.defaultValue) {
659
- this._data.delete(key);
660
- } else {
661
- this._data.set(key, value);
662
- }
663
- }
664
- forEach(callback) {
665
- this._data.forEach((v, key) => {
666
- const [x, y] = this._pos(key);
667
- callback(x, y, v);
668
- });
669
- }
670
- };
671
-
672
- // src/vector.ts
673
- var Vector = class _Vector {
674
- x;
675
- y;
676
- constructor(x = 0, y = 0) {
677
- this.x = x;
678
- this.y = y;
679
- }
680
- clone() {
681
- return new _Vector(this.x, this.y);
682
- }
683
- distanceTo(that) {
684
- return distance(
685
- this.x,
686
- this.y,
687
- that.x,
688
- that.y
689
- );
690
- }
691
- floored() {
692
- return new _Vector(
693
- Math.floor(this.x),
694
- Math.floor(this.y)
695
- );
696
- }
697
- plus(x, y) {
698
- if (x instanceof _Vector) {
699
- y = x.y;
700
- x = x.x;
701
- } else if (y === void 0) {
702
- y = x;
703
- }
704
- this.x += x;
705
- this.y += y;
706
- return this;
463
+ this.x += x;
464
+ this.y += y;
465
+ return this;
707
466
  }
708
467
  minus(x, y) {
709
468
  if (x instanceof _Vector) {
@@ -752,57 +511,63 @@ var Vector = class _Vector {
752
511
  }
753
512
  };
754
513
 
755
- // src/rectangle.ts
756
- var Rectangle = class _Rectangle {
757
- x;
758
- y;
759
- width;
760
- height;
761
- constructor(x = 0, y = 0, width = 1, height = 1) {
762
- this.x = x;
763
- this.y = y;
764
- this.width = width;
765
- this.height = height;
766
- }
767
- area() {
768
- return this.width * this.height;
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;
769
531
  }
770
- contains(target, padding = 0) {
771
- 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;
532
+ constructor(defaultValue = DEFAULT_VALUE) {
533
+ this.defaultValue = defaultValue;
534
+ this._data = /* @__PURE__ */ new Map();
772
535
  }
773
- intersects(other, padding = 0) {
774
- 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);
536
+ _floor(x, y) {
537
+ return [floor(x), floor(y)];
775
538
  }
776
- center() {
777
- return new Vector(
778
- this.x + this.width / 2,
779
- this.y + this.height / 2
780
- );
539
+ _key(x, y) {
540
+ return `${x},${y}`;
781
541
  }
782
- distanceTo(other) {
783
- return this.center().distanceTo(other.center());
542
+ _pos(key) {
543
+ return key.split(",").map((v) => parseInt(v));
784
544
  }
785
- clone() {
786
- return new _Rectangle(this.x, this.y, this.width, this.height);
545
+ clear() {
546
+ this._data.clear();
787
547
  }
788
- equals(other) {
789
- return this.x === other.x && this.y === other.y && this.width === other.width && this.height === other.height;
548
+ get(x, y, defaultValue = this.defaultValue) {
549
+ [x, y] = this._floor(x, y);
550
+ return this._data.get(this._key(x, y)) ?? defaultValue;
790
551
  }
791
- grow(padding) {
792
- return new _Rectangle(
793
- this.x - padding,
794
- this.y - padding,
795
- this.width + padding * 2,
796
- this.height + padding * 2
797
- );
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
+ }
798
560
  }
799
- vectorInside(vec, padding = 0) {
800
- 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;
561
+ forEach(callback) {
562
+ this._data.forEach((v, key) => {
563
+ const [x, y] = this._pos(key);
564
+ callback(x, y, v);
565
+ });
801
566
  }
802
567
  };
803
568
 
804
569
  // src/rng.ts
805
- var Rng = class {
570
+ var RandomNumberGenerator = class {
806
571
  __seed;
807
572
  _seed;
808
573
  _uid_characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
@@ -834,10 +599,6 @@ var Rng = class {
834
599
  get nexti() {
835
600
  return this.nextf * Number.MAX_SAFE_INTEGER | 0;
836
601
  }
837
- // NOTE: as of the core/apparatus consolidation, range(min, max) treats
838
- // max as EXCLUSIVE (previous published versions of this package treated
839
- // it as inclusive - if you're upgrading from an older @n3rdw1z4rd/core,
840
- // range(0, 10) now returns [0, 10) instead of [0, 10]).
841
602
  range(min2, max2) {
842
603
  if (max2 === void 0) {
843
604
  max2 = min2;
@@ -849,9 +610,6 @@ var Rng = class {
849
610
  const theta = this.nextf * 2 * PI;
850
611
  return [cos(theta), sin(theta)];
851
612
  }
852
- // Box-Muller-ish gaussian sample, mean 0.5 stddev 1/6 (so ~99.7% of
853
- // samples land in [0, 1]). Uses native Math.log since a pure natural-log
854
- // implementation isn't part of math.ts yet.
855
613
  parkMillerNormal() {
856
614
  const mean = 1 / 2;
857
615
  const stddev = 1 / 6;
@@ -899,7 +657,7 @@ var Rng = class {
899
657
  return rows;
900
658
  }
901
659
  };
902
- var rng = new Rng();
660
+ var rng = new RandomNumberGenerator();
903
661
 
904
662
  // src/dungeon-generator.ts
905
663
  var TILE_EMPTY = 0;
@@ -1056,7 +814,7 @@ function _getShortestPathBetweenRects(rectA, rectB) {
1056
814
  let paths = { a: edgeA[0], b: edgeB[0], distance: Infinity };
1057
815
  for (const a of edgeA) {
1058
816
  for (const b of edgeB) {
1059
- const d = distance(a.x, a.y, b.x, b.y);
817
+ const d = distance2d(a.x, a.y, b.x, b.y);
1060
818
  if (d < paths.distance) {
1061
819
  paths = { a, b, distance: d };
1062
820
  }
@@ -1115,384 +873,100 @@ function GenerateWalls(map) {
1115
873
  }
1116
874
 
1117
875
  // src/ecs.ts
1118
- var ECS = class _ECS {
1119
- _globals = /* @__PURE__ */ new Map();
1120
- _entities = /* @__PURE__ */ new Map();
1121
- _components = /* @__PURE__ */ new Map();
1122
- _defaultComponents = [];
1123
- _systems = /* @__PURE__ */ new Map();
1124
- _onTickStartCallbacks = [];
1125
- _onTickEndCallbacks = [];
1126
- get components() {
1127
- return [...this._components.keys()];
1128
- }
1129
- get systems() {
1130
- return [...this._systems.keys()];
1131
- }
1132
- get entities() {
1133
- return [...this._entities.values()];
1134
- }
1135
- uid(length = 8) {
1136
- const numBytes = Math.ceil(length / 2);
1137
- const buffer = new Uint8Array(numBytes);
1138
- window.crypto.getRandomValues(buffer);
1139
- let uid = "";
1140
- for (let i = 0; i < buffer.length; i++) {
1141
- uid += buffer[i].toString(16).padStart(2, "0");
1142
- }
1143
- return uid.substring(0, length);
1144
- }
1145
- createComponent(uid, data = null) {
1146
- if (!this._components.has(uid)) {
1147
- this._components.set(uid, data);
1148
- log("created component:", { uid, data });
1149
- } else {
1150
- logwrn("createComponent: a component already exists with uid:", uid);
1151
- }
1152
- return this;
1153
- }
1154
- includeAsDefaultComponents(...components) {
1155
- components.forEach((component) => {
1156
- if (!this._defaultComponents.includes(component)) {
1157
- this._defaultComponents.push(component);
1158
- log("includeAsDefaultComponents:", component);
1159
- } else {
1160
- log("includeAsDefaultComponents: already exists:", component);
1161
- }
1162
- });
1163
- return this;
1164
- }
1165
- createSystem(uid, ...components) {
1166
- const callback = components.pop();
1167
- const comps = components.filter((comp) => typeof comp === "string");
1168
- if (!this._systems.has(uid)) {
1169
- this._systems.set(uid, { components: comps, callback });
1170
- log("created system:", { uid, components, callback });
1171
- } else {
1172
- logwrn("createSystem: a system already exists with uid:", uid);
1173
- }
1174
- return this;
1175
- }
1176
- createEntityWithUid(uid, ...components) {
1177
- if (!this._entities.has(uid)) {
1178
- const componentList = [
1179
- .../* @__PURE__ */ new Set([
1180
- ...this._defaultComponents,
1181
- ...components
1182
- ])
1183
- ];
1184
- const comps = /* @__PURE__ */ new Map();
1185
- componentList.forEach((component) => {
1186
- if (this._components.has(component)) {
1187
- const componentData = this._components.get(component);
1188
- const data = {};
1189
- for (const uid2 in componentData) {
1190
- const value = componentData[uid2];
1191
- data[uid2] = typeof value === "function" ? value(uid2) : value;
1192
- }
1193
- comps.set(component, data);
1194
- } else {
1195
- logwrn("createEntityWithUid: missing component:", component);
1196
- }
1197
- });
1198
- if (comps.size === componentList.length) {
1199
- const entity = { uid, components: comps };
1200
- this._entities.set(uid, entity);
1201
- log("created entity:", entity);
1202
- } else {
1203
- logwrn("createEntityWithUid: failed to create an entity with missing components");
876
+ var ECS = class {
877
+ nextEntityId = 1;
878
+ components = /* @__PURE__ */ new Map();
879
+ createEntity() {
880
+ return this.nextEntityId++;
881
+ }
882
+ addComponent(entity, name, data) {
883
+ if (!this.components.has(name)) {
884
+ this.components.set(name, /* @__PURE__ */ new Map());
885
+ }
886
+ this.components.get(name).set(entity, data);
887
+ }
888
+ getComponent(entity, name) {
889
+ return this.components.get(name)?.get(entity);
890
+ }
891
+ removeComponent(entity, name) {
892
+ this.components.get(name)?.delete(entity);
893
+ }
894
+ getEntitiesWithComponents(...componentNames) {
895
+ if (componentNames.length === 0) return [];
896
+ const first = this.components.get(componentNames[0]);
897
+ if (!first) return [];
898
+ const result = [];
899
+ for (const entity of first.keys()) {
900
+ if (componentNames.every((name) => this.components.get(name)?.has(entity))) {
901
+ result.push(entity);
1204
902
  }
1205
- } else {
1206
- logwrn("createEntityWithUid: an entity already exists with uid:", uid);
1207
903
  }
1208
- return this;
904
+ return result;
1209
905
  }
1210
- createEntity(...components) {
1211
- const uid = this.uid();
1212
- return this.createEntityWithUid(uid, ...components);
906
+ };
907
+
908
+ // src/input/index.ts
909
+ var Input = class extends Emitter {
910
+ _down = /* @__PURE__ */ new Map();
911
+ _pointerPosition = new Vector();
912
+ inputThreshold = 200;
913
+ get down() {
914
+ return this._down;
1213
915
  }
1214
- createEntities(count, ...components) {
1215
- for (; count > 0; count--) {
1216
- this.createEntity(...components);
1217
- }
1218
- return this;
916
+ get pointerPosition() {
917
+ return this._pointerPosition;
1219
918
  }
1220
- getEntity(uid) {
1221
- return this._entities.get(uid);
1222
- }
1223
- getEntitiesWithComponents(...components) {
1224
- const filter = components.pop();
1225
- const uids = components.filter((component) => typeof component === "string");
1226
- const entities = [];
1227
- this._entities.forEach((entity) => {
1228
- if (uids.every(
1229
- (component) => [...entity.components.keys()].includes(component)
1230
- )) {
1231
- for (const filterComponent in filter) {
1232
- for (const filterKey in filter[filterComponent]) {
1233
- const entityValue = entity.components.get(filterComponent)[filterKey];
1234
- const filterValue = filter[filterComponent][filterKey];
1235
- if (entityValue === filterValue) {
1236
- entities.push(entity);
1237
- }
1238
- }
1239
- }
919
+ constructor() {
920
+ super();
921
+ window.addEventListener("contextmenu", (ev) => {
922
+ log("contextmenu:", ev);
923
+ });
924
+ window.addEventListener("keydown", (ev) => {
925
+ const { code, repeat, timeStamp } = ev;
926
+ if (!repeat) {
927
+ this._down.set(code, { state: 1, time: timeStamp });
928
+ this.emit("key_down", code, ev);
1240
929
  }
1241
930
  });
1242
- return entities;
1243
- }
1244
- addComponent(uid, component, values = {}) {
1245
- const entity = this._entities.get(uid);
1246
- if (entity) {
1247
- if (this._components.has(component)) {
1248
- const comp = this._components.get(component);
1249
- const componentData = {
1250
- ...comp,
1251
- ...values
1252
- };
1253
- entity.components.set(component, componentData);
1254
- log("addComponent: added component:", component, "to entity:", uid);
1255
- } else {
1256
- logwrn("addComponent: component not found:", component);
931
+ window.addEventListener("keyup", (ev) => {
932
+ const { code, timeStamp } = ev;
933
+ const delta = timeStamp - (this._down.get(code)?.time ?? 0);
934
+ this._down.set(code, { state: 0, time: timeStamp });
935
+ this.emit("key_up", code, ev);
936
+ if (delta < this.inputThreshold) {
937
+ this.emit("key_pressed", code, ev);
1257
938
  }
1258
- } else {
1259
- logwrn("addComponent: entity not found:", uid);
1260
- }
1261
- return this;
1262
- }
1263
- onAllEntitiesNow(callback) {
1264
- this._entities.forEach((entity) => callback(entity));
1265
- log("onAllEntitiesNow: executed on all entities:", callback);
1266
- return this;
1267
- }
1268
- duplicateEntity(uid, count = 1, deep = false) {
1269
- const entity = this._entities.get(uid);
1270
- if (entity) {
1271
- if (!deep) {
1272
- for (let i = 0; i < count; i++) this.createEntity(...entity.components.keys());
1273
- } else {
1274
- for (let i = 0; i < count; i++) {
1275
- const newEntity = { uid: this.uid(), components: /* @__PURE__ */ new Map() };
1276
- entity.components.forEach((value, key) => {
1277
- newEntity.components.set(key, value);
1278
- });
1279
- this._entities.set(newEntity.uid, newEntity);
1280
- }
1281
- ;
939
+ });
940
+ window.addEventListener("pointerdown", (ev) => {
941
+ const { button, timeStamp } = ev;
942
+ const code = `Button${button}`;
943
+ if (!this._down.get(code)) {
944
+ this._down.set(code, { state: 1, time: timeStamp });
945
+ this.emit("pointer_down", code, ev);
1282
946
  }
1283
- log("duplicateEntity: duplicated entity:", uid);
1284
- } else {
1285
- logwrn("duplicateEntity: entity not found:", uid);
1286
- }
1287
- return this;
947
+ });
948
+ window.addEventListener("pointerup", (ev) => {
949
+ const { button, timeStamp } = ev;
950
+ const code = `Button${button}`;
951
+ const delta = timeStamp - (this._down.get(code)?.time ?? 0);
952
+ this._down.set(code, { state: 0, time: timeStamp });
953
+ if (delta < this.inputThreshold) {
954
+ this.emit("button_clicked", code, ev);
955
+ }
956
+ });
957
+ window.addEventListener("mousemove", (ev) => {
958
+ const { offsetX, offsetY, movementX, movementY } = ev;
959
+ this._pointerPosition.x = offsetX;
960
+ this._pointerPosition.y = offsetY;
961
+ this.emit("pointer_move", movementX, movementY, ev);
962
+ });
963
+ window.addEventListener("wheel", (ev) => {
964
+ const { deltaY } = ev;
965
+ this.emit("wheel", deltaY, ev);
966
+ });
1288
967
  }
1289
- getGlobal(key) {
1290
- return this._globals.get(key);
1291
- }
1292
- setGlobal(key, value) {
1293
- this._globals.set(key, value);
1294
- return this;
1295
- }
1296
- beforeTick(callback) {
1297
- this._onTickStartCallbacks.push(callback);
1298
- log("beforeTick: added:", callback);
1299
- return this;
1300
- }
1301
- afterTick(callback) {
1302
- this._onTickEndCallbacks.push(callback);
1303
- log("afterTick: added:", callback);
1304
- return this;
1305
- }
1306
- runSystem(uid) {
1307
- const system = this._systems.get(uid);
1308
- if (system) {
1309
- this._entities.forEach((entity) => {
1310
- if (system.components.every(
1311
- (component) => [...entity.components.keys()].includes(component)
1312
- )) {
1313
- system.callback(entity, Object.fromEntries(entity.components));
1314
- }
1315
- });
1316
- } else {
1317
- logwrn("runSystem: system not found:", uid);
1318
- }
1319
- return this;
1320
- }
1321
- update() {
1322
- this._onTickStartCallbacks.forEach((cb) => cb());
1323
- this._systems.forEach((_, uid) => this.runSystem(uid));
1324
- this._onTickEndCallbacks.forEach((cb) => cb());
1325
- }
1326
- static get instance() {
1327
- if (!_ECS._instance) {
1328
- _ECS._instance = new _ECS();
1329
- }
1330
- return _ECS._instance;
1331
- }
1332
- static _instance;
1333
- constructor() {
1334
- }
1335
- };
1336
-
1337
- // src/emitter.ts
1338
- var Emitter = class {
1339
- listeners = /* @__PURE__ */ new Map();
1340
- constructor() {
1341
- }
1342
- on(...eventName) {
1343
- const listener = eventName.pop();
1344
- const events = eventName.filter((ev) => typeof ev === "string");
1345
- events.forEach((event) => {
1346
- if (!this.listeners.has(event)) {
1347
- this.listeners.set(event, new Array());
1348
- }
1349
- this.listeners.get(event)?.push(listener);
1350
- });
1351
- return this;
1352
- }
1353
- emit(eventName, ...args) {
1354
- this.listeners.get(eventName)?.forEach((listener) => listener(...args, eventName));
1355
- return this;
1356
- }
1357
- clear(eventName) {
1358
- if (eventName) {
1359
- this.listeners.delete(eventName);
1360
- } else {
1361
- this.listeners.clear();
1362
- }
1363
- return this;
1364
- }
1365
- };
1366
- var GlobalEmitter = new Emitter();
1367
- var EventBus = class extends EventTarget {
1368
- emit(type, detail) {
1369
- this.dispatchEvent(new CustomEvent(type, { detail }));
1370
- }
1371
- on(type, cb) {
1372
- this.addEventListener(type, cb);
1373
- }
1374
- };
1375
- var eventBus = new EventBus();
1376
-
1377
- // src/input.ts
1378
- var Input = class extends Emitter {
1379
- inputThreshold = 200;
1380
- _keyStates = {};
1381
- _mouseButtonStates = {};
1382
- _mousePosition = [0, 0];
1383
- _mousePosition2 = [0, 0];
1384
- get mousePosition() {
1385
- return this._mousePosition;
1386
- }
1387
- get mousePosition2() {
1388
- return this._mousePosition2;
1389
- }
1390
- constructor() {
1391
- super();
1392
- window.addEventListener("keydown", this._onKeyDown.bind(this));
1393
- window.addEventListener("keyup", this._onKeyUp.bind(this));
1394
- window.addEventListener("mousedown", this._onMouseButtonDown.bind(this));
1395
- window.addEventListener("mouseup", this._onMouseButtonUp.bind(this));
1396
- window.addEventListener("mousemove", this._onMouseMove.bind(this));
1397
- window.addEventListener("wheel", this._onWheel.bind(this));
1398
- }
1399
- _getCommonEventProps(ev) {
1400
- const props = {
1401
- timeStamp: ev.timeStamp,
1402
- altKey: ev.altKey,
1403
- ctrlKey: ev.ctrlKey,
1404
- metaKey: ev.metaKey,
1405
- shiftKey: ev.shiftKey
1406
- };
1407
- return props;
1408
- }
1409
- // private _onContextMenu(ev: MouseEvent) {
1410
- // ev.preventDefault();
1411
- // this.emit('contextmenu');
1412
- // return false;
1413
- // }
1414
- _onKeyDown(ev) {
1415
- const props = this._getCommonEventProps(ev);
1416
- const { code, key } = ev;
1417
- if (!ev.repeat) {
1418
- this._keyStates[code] = { state: 1, timeStamp: props.timeStamp };
1419
- this.emit("key_down", { ...props, code, key });
1420
- this.emit(`${code.toLowerCase()}_down`, props);
1421
- }
1422
- }
1423
- _onKeyUp(ev) {
1424
- const props = this._getCommonEventProps(ev);
1425
- const { code, key } = ev;
1426
- const deltaStamp = props.timeStamp - (this._keyStates[code]?.timeStamp ?? 0);
1427
- this._keyStates[code] = { state: 0, timeStamp: props.timeStamp };
1428
- this.emit("key_up", { ...props, code, key });
1429
- this.emit(`${code.toLowerCase()}_up`, props);
1430
- if (deltaStamp < this.inputThreshold) {
1431
- this.emit("key_pressed", { ...props, code, key });
1432
- this.emit(`${code.toLowerCase()}_pressed`, props);
1433
- }
1434
- }
1435
- _onMouseButtonDown(ev) {
1436
- const props = this._getCommonEventProps(ev);
1437
- const { button } = ev;
1438
- if (!this._mouseButtonStates[button]?.state) {
1439
- this._mouseButtonStates[button] = { state: 1, timeStamp: props.timeStamp };
1440
- this.emit("mouse_button_down", { ...props, button });
1441
- this.emit(`mouse_button_${button}_down`, props);
1442
- }
1443
- }
1444
- _onMouseButtonUp(ev) {
1445
- const props = this._getCommonEventProps(ev);
1446
- const { button } = ev;
1447
- const deltaStamp = props.timeStamp - (this._mouseButtonStates[button]?.timeStamp ?? 0);
1448
- this._mouseButtonStates[button] = { state: 0, timeStamp: props.timeStamp };
1449
- this.emit("mouse_button_up", { ...props, button });
1450
- this.emit(`mouse_button_${button}_up`, props);
1451
- if (deltaStamp < this.inputThreshold) {
1452
- this.emit("mouse_button_clicked", { ...props, button });
1453
- this.emit(`mouse_button_${button}_clicked`, props);
1454
- }
1455
- }
1456
- _onMouseMove(ev) {
1457
- const props = this._getCommonEventProps(ev);
1458
- const { buttons, offsetX, offsetY, movementX, movementY } = ev;
1459
- this._mousePosition = [offsetX, offsetY];
1460
- const width = window.innerWidth;
1461
- const height = window.innerHeight;
1462
- this._mousePosition2 = [
1463
- ev.clientX / width * 2 - 1,
1464
- -(ev.clientY / height) * 2 + 1
1465
- ];
1466
- this.emit("mouse_move", {
1467
- ...props,
1468
- buttons,
1469
- x: offsetX,
1470
- y: offsetY,
1471
- deltaX: movementX,
1472
- deltaY: movementY
1473
- });
1474
- }
1475
- _onWheel(ev) {
1476
- const props = this._getCommonEventProps(ev);
1477
- const { deltaX, deltaY, deltaZ } = ev;
1478
- this.emit("mouse_wheel", {
1479
- ...props,
1480
- deltaX,
1481
- deltaY,
1482
- deltaZ
1483
- });
1484
- }
1485
- isKeyDown(keyCode) {
1486
- return this._keyStates[keyCode]?.state === 1 ? true : false;
1487
- }
1488
- getKeyState(keyCode) {
1489
- return this._keyStates[keyCode]?.state ?? 0;
1490
- }
1491
- isMouseButtonDown(mouseButton) {
1492
- return this._mouseButtonStates[mouseButton]?.state === 1 ? true : false;
1493
- }
1494
- getMouseButtonState(button) {
1495
- return this._mouseButtonStates[button]?.state ?? 0;
968
+ isDown(name) {
969
+ return this._down.get(name) ? true : false;
1496
970
  }
1497
971
  };
1498
972
 
@@ -1545,6 +1019,9 @@ var Map3D = class {
1545
1019
  function isNullOrUndefined(target) {
1546
1020
  return target === null || target === void 0;
1547
1021
  }
1022
+ function isPositiveString(target) {
1023
+ return target.length > 0;
1024
+ }
1548
1025
  var getRedYellowGreenGradientHex = (min2, max2) => {
1549
1026
  if (max2 === void 0) {
1550
1027
  max2 = min2;
@@ -1569,6 +1046,18 @@ var getRedYellowGreenGradientHex = (min2, max2) => {
1569
1046
  "#00ff00"
1570
1047
  ][index];
1571
1048
  };
1049
+ function GetUrlParams() {
1050
+ const params = {};
1051
+ const url = new URLSearchParams(location.search);
1052
+ for (const [key, value] of url.entries()) {
1053
+ params[key] = value;
1054
+ }
1055
+ return params;
1056
+ }
1057
+ var xy2k = (x, y, delimiter = ",") => `${x}${delimiter}${y}`;
1058
+ var k2xy = (key, delimiter = ",") => key.split(delimiter).map(parseFloat);
1059
+ var xyz2k = (x, y, z, delimiter = ",") => `${x}${delimiter},${y},${delimiter},${z}`;
1060
+ var k2xyz = (key, delimiter = ",") => key.split(delimiter).map(parseFloat);
1572
1061
 
1573
1062
  // src/noise.ts
1574
1063
  var import_simplex_noise = require("simplex-noise");
@@ -1631,154 +1120,264 @@ var Noise = class _Noise {
1631
1120
  }
1632
1121
  };
1633
1122
 
1634
- // src/particle-renderer.ts
1635
- var Renderer = class {
1636
- _screenCanvas;
1637
- _bufferCanvas;
1638
- screenContext;
1639
- bufferContext;
1640
- get width() {
1641
- return this._screenCanvas.width;
1123
+ // src/rendering/color.ts
1124
+ var Color = class _Color {
1125
+ _hex = "#ffffffff";
1126
+ _r = 255;
1127
+ _g = 255;
1128
+ _b = 255;
1129
+ _a = 255;
1130
+ get r() {
1131
+ return this._r;
1642
1132
  }
1643
- get height() {
1644
- return this._screenCanvas.height;
1133
+ set r(value) {
1134
+ this._r = clamp(value, 0, 255);
1135
+ this._hex = this._to_hex();
1645
1136
  }
1646
- constructor(canvas) {
1647
- this._screenCanvas = canvas ?? document.createElement("canvas");
1648
- this._bufferCanvas = new OffscreenCanvas(this._screenCanvas.width, this._screenCanvas.height);
1649
- this.screenContext = this._screenCanvas.getContext("2d");
1650
- this.bufferContext = this._bufferCanvas.getContext("2d");
1651
- if (!this.screenContext || !this.bufferContext) {
1652
- throw "Failed to create CanvasRenderingContext2D";
1653
- }
1137
+ get g() {
1138
+ return this._g;
1654
1139
  }
1655
- appendTo(target, autoResize = true) {
1656
- if (this._screenCanvas.parentNode) {
1657
- this._screenCanvas.parentNode.removeChild(this._screenCanvas);
1658
- }
1659
- target.appendChild(this._screenCanvas);
1660
- if (autoResize) {
1661
- this.resize();
1662
- }
1140
+ set g(value) {
1141
+ this._g = clamp(value, 0, 255);
1142
+ this._hex = this._to_hex();
1663
1143
  }
1664
- resize(displayWidth, displayHeight) {
1665
- const { width, height } = this._screenCanvas.parentElement?.getBoundingClientRect() ?? this._screenCanvas.getBoundingClientRect();
1666
- displayWidth = 0 | (displayWidth ?? width);
1667
- displayHeight = 0 | (displayHeight ?? height);
1668
- if (this._screenCanvas.width !== displayWidth || this._screenCanvas.height !== displayHeight) {
1669
- this._screenCanvas.width = displayWidth;
1670
- this._screenCanvas.height = displayHeight;
1671
- this._bufferCanvas.width = displayWidth;
1672
- this._bufferCanvas.height = displayHeight;
1673
- return true;
1674
- }
1675
- return false;
1144
+ get b() {
1145
+ return this._b;
1676
1146
  }
1677
- setPixel(x, y, color, size = 2) {
1678
- this.bufferContext.fillStyle = color.hexStr;
1679
- this.bufferContext.fillRect(x - size / 2, y - size / 2, size, size);
1147
+ set b(value) {
1148
+ this._b = clamp(value, 0, 255);
1149
+ this._hex = this._to_hex();
1680
1150
  }
1681
- render() {
1682
- this.screenContext.clearRect(0, 0, this._screenCanvas.width, this._screenCanvas.height);
1683
- this.screenContext.drawImage(this._bufferCanvas, 0, 0);
1684
- this.bufferContext.clearRect(0, 0, this._screenCanvas.width, this._screenCanvas.height);
1151
+ get a() {
1152
+ return this._a;
1685
1153
  }
1686
- };
1687
-
1688
- // src/spatial-partition-2d.ts
1689
- var SpatialPartition2d = class {
1690
- cells;
1691
- cellSize;
1692
- entities;
1693
- constructor(cellSize, entities) {
1694
- this.cellSize = cellSize;
1695
- this.entities = entities;
1696
- this.cells = Array.from(
1697
- { length: Math.ceil(1 / cellSize) },
1698
- () => Array.from({ length: Math.ceil(1 / cellSize) }, () => [])
1699
- );
1700
- for (const entity of this.entities) {
1701
- this.addEntity(entity);
1702
- }
1154
+ set a(value) {
1155
+ this._a = clamp(value, 0, 255);
1156
+ this._hex = this._to_hex();
1703
1157
  }
1704
- addEntity(entity) {
1705
- const cell = this.getCell(entity.x, entity.y);
1706
- cell.push(entity);
1158
+ get hexStr() {
1159
+ return this._hex;
1707
1160
  }
1708
- getCell(x, y) {
1709
- const cellX = Math.floor(x / this.cellSize);
1710
- const cellY = Math.floor(y / this.cellSize);
1711
- return this.cells[cellY][cellX];
1161
+ set hexStr(value) {
1162
+ if (value.startsWith("#")) {
1163
+ [this._r, this._g, this._b, this._a] = this._parse_hex_color_string(value);
1164
+ this._hex = value;
1165
+ } else {
1166
+ log(`Color: invalid color string: ${value}. Should be a hexadecimal color string (i.e.: '#ffffff' or '#ffffffff'). Keeping the current color.`);
1167
+ }
1712
1168
  }
1713
- getCellNeighbors(x, y) {
1714
- const neighbors = [];
1715
- for (let dy = -1; dy <= 1; dy++) {
1716
- for (let dx = -1; dx <= 1; dx++) {
1717
- let nx = x + dx;
1718
- let ny = y + dy;
1719
- if (nx < 0) nx = this.cells[0].length - 1;
1720
- else if (nx >= this.cells[0].length) nx = 0;
1721
- if (ny < 0) ny = this.cells.length - 1;
1722
- else if (ny >= this.cells.length) ny = 0;
1723
- neighbors.push(this.cells[ny][nx]);
1169
+ get rgba() {
1170
+ return [this._r, this._g, this._b, this._a];
1171
+ }
1172
+ constructor(r = 255, g = 255, b = 255, a = 255) {
1173
+ if (typeof r === "string") {
1174
+ if (r.startsWith("#")) {
1175
+ [r, g, b, a] = this._parse_hex_color_string(r);
1176
+ } else {
1177
+ log(`Color: invalid color string: ${r}. Should be a hexadecimal color string (i.e.: '#ffffff' or '#ffffffff'). Defaulting to opaque white.`);
1178
+ r = g = b = a = 255;
1724
1179
  }
1180
+ } else {
1181
+ [r, g, b, a] = [r, g, b, a].map((n) => _Color._normalize(n));
1725
1182
  }
1726
- return neighbors;
1183
+ this.r = r;
1184
+ this.g = g;
1185
+ this.b = b;
1186
+ this.a = a;
1727
1187
  }
1728
- };
1729
-
1730
- // src/particle-system-2d.ts
1731
- var colors = [
1732
- Color.RED,
1733
- Color.GREEN,
1734
- Color.BLUE,
1735
- Color.YELLOW,
1736
- Color.MAGENTA
1737
- ];
1738
- var ParticleSystem2d = class {
1739
- particleCount = 2e3;
1740
- _particleSize = 2;
1741
- _frictionHalfLife = 0.04;
1742
- _range = 0.1;
1743
- _rangeFactor = 0.1;
1744
- _attractionMatrix = rng.randomMatrix(colors.length);
1745
- _particles = [];
1746
- _spatialPartition;
1747
- constructor() {
1748
- for (let i = 0; i < this.particleCount; i++) {
1749
- this._particles.push({
1750
- x: rng.nextf,
1751
- y: rng.nextf,
1752
- vx: 0,
1753
- vy: 0,
1754
- color: rng.range(colors.length)
1755
- });
1756
- }
1757
- this._spatialPartition = new SpatialPartition2d(this._range, this._particles);
1188
+ static _normalize(value) {
1189
+ return clamp(0 | (value > 0 && value < 1 ? value * 255 : value), 0, 255);
1758
1190
  }
1759
- _calcForce(r, a, beta = 0.3) {
1760
- let f = 0;
1761
- if (r < beta) {
1762
- f = r / beta - 1;
1763
- } else if (beta < r && r < 1) {
1764
- f = a * (1 - Math.abs(2 * r - 1 - beta) / (1 - beta));
1765
- }
1766
- return f;
1191
+ _to_hex() {
1192
+ return `#${this.r.toString(16).padStart(2, "0")}${this.g.toString(16).padStart(2, "0")}${this.b.toString(16).padStart(2, "0")}${this.a.toString(16).padStart(2, "0")}`;
1767
1193
  }
1768
- _updateVelocities(deltaTimeSeconds) {
1769
- const frictionFactor = Math.pow(
1770
- 0.5,
1771
- deltaTimeSeconds / this._frictionHalfLife
1772
- );
1773
- for (let y = 0; y < this._spatialPartition.cells.length; y++) {
1774
- for (let x = 0; x < this._spatialPartition.cells[y].length; x++) {
1775
- const cell = this._spatialPartition.cells[y][x];
1776
- for (let i = 0; i < cell.length; i++) {
1777
- let fx = 0;
1778
- let fy = 0;
1779
- const p1 = cell[i];
1780
- const neighborCells = this._spatialPartition.getCellNeighbors(x, y);
1781
- for (const neighborCell of neighborCells) {
1194
+ _parse_hex_color_string(hex) {
1195
+ const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})?$/i.exec(hex);
1196
+ return result ? [
1197
+ parseInt(result[1], 16),
1198
+ parseInt(result[2], 16),
1199
+ parseInt(result[3], 16),
1200
+ result[4] ? parseInt(result[4], 16) : 255
1201
+ ] : [255, 255, 255, 255];
1202
+ }
1203
+ static fromHsv(h, s, v, a = 255) {
1204
+ let r = 0;
1205
+ let g = 0;
1206
+ let b = 0;
1207
+ let i = Math.floor(h * 6);
1208
+ let f = h * 6 - i;
1209
+ let p = v * (1 - s);
1210
+ let q = v * (1 - f * s);
1211
+ let t = v * (1 - (1 - f) * s);
1212
+ switch (i % 6) {
1213
+ case 0:
1214
+ r = v, g = t, b = p;
1215
+ break;
1216
+ case 1:
1217
+ r = q, g = v, b = p;
1218
+ break;
1219
+ case 2:
1220
+ r = p, g = v, b = t;
1221
+ break;
1222
+ case 3:
1223
+ r = p, g = q, b = v;
1224
+ break;
1225
+ case 4:
1226
+ r = t, g = p, b = v;
1227
+ break;
1228
+ case 5:
1229
+ r = v, g = p, b = q;
1230
+ break;
1231
+ }
1232
+ return new _Color(Math.round(r * 255), Math.round(g * 255), Math.round(b * 255), a);
1233
+ }
1234
+ static get BLACK() {
1235
+ return new _Color(0, 0, 0, 255);
1236
+ }
1237
+ static get GRAY() {
1238
+ return new _Color(128, 128, 128, 255);
1239
+ }
1240
+ static get WHITE() {
1241
+ return new _Color(255, 255, 255, 255);
1242
+ }
1243
+ static get RED() {
1244
+ return new _Color(255, 0, 0, 255);
1245
+ }
1246
+ static get GREEN() {
1247
+ return new _Color(0, 255, 0, 255);
1248
+ }
1249
+ static get BLUE() {
1250
+ return new _Color(0, 0, 255, 255);
1251
+ }
1252
+ static get YELLOW() {
1253
+ return new _Color(255, 255, 0, 255);
1254
+ }
1255
+ static get ORANGE() {
1256
+ return new _Color(255, 128, 0, 255);
1257
+ }
1258
+ static get PURPLE() {
1259
+ return new _Color(128, 0, 128, 255);
1260
+ }
1261
+ static get CYAN() {
1262
+ return new _Color(0, 255, 255, 255);
1263
+ }
1264
+ static get MAGENTA() {
1265
+ return new _Color(255, 0, 255, 255);
1266
+ }
1267
+ static fromName(name) {
1268
+ const element = document.createElement("div");
1269
+ element.style.color = name;
1270
+ document.body.appendChild(element);
1271
+ const computedColor = getComputedStyle(element).color;
1272
+ document.body.removeChild(element);
1273
+ const rgba = computedColor.match(/\d+/g);
1274
+ if (rgba && rgba.length === 4) {
1275
+ const [r, g, b, a] = rgba.map(Number);
1276
+ return new _Color(r, g, b, a);
1277
+ } else {
1278
+ log(`Color: invalid CSS color name: ${name}. Defaulting to transparent.`);
1279
+ return _Color.TRANSPARENT;
1280
+ }
1281
+ }
1282
+ static get TRANSPARENT() {
1283
+ return new _Color(0, 0, 0, 0);
1284
+ }
1285
+ };
1286
+
1287
+ // src/spatial-partition-2d.ts
1288
+ var SpatialPartition2d = class {
1289
+ cells;
1290
+ cellSize;
1291
+ entities;
1292
+ constructor(cellSize, entities) {
1293
+ this.cellSize = cellSize;
1294
+ this.entities = entities;
1295
+ this.cells = Array.from(
1296
+ { length: Math.ceil(1 / cellSize) },
1297
+ () => Array.from({ length: Math.ceil(1 / cellSize) }, () => [])
1298
+ );
1299
+ for (const entity of this.entities) {
1300
+ this.addEntity(entity);
1301
+ }
1302
+ }
1303
+ addEntity(entity) {
1304
+ const cell = this.getCell(entity.x, entity.y);
1305
+ cell.push(entity);
1306
+ }
1307
+ getCell(x, y) {
1308
+ const cellX = Math.floor(x / this.cellSize);
1309
+ const cellY = Math.floor(y / this.cellSize);
1310
+ return this.cells[cellY][cellX];
1311
+ }
1312
+ getCellNeighbors(x, y) {
1313
+ const neighbors = [];
1314
+ for (let dy = -1; dy <= 1; dy++) {
1315
+ for (let dx = -1; dx <= 1; dx++) {
1316
+ let nx = x + dx;
1317
+ let ny = y + dy;
1318
+ if (nx < 0) nx = this.cells[0].length - 1;
1319
+ else if (nx >= this.cells[0].length) nx = 0;
1320
+ if (ny < 0) ny = this.cells.length - 1;
1321
+ else if (ny >= this.cells.length) ny = 0;
1322
+ neighbors.push(this.cells[ny][nx]);
1323
+ }
1324
+ }
1325
+ return neighbors;
1326
+ }
1327
+ };
1328
+
1329
+ // src/particle-system-2d.ts
1330
+ var colors = [
1331
+ Color.RED,
1332
+ Color.GREEN,
1333
+ Color.BLUE,
1334
+ Color.YELLOW,
1335
+ Color.MAGENTA
1336
+ ];
1337
+ var ParticleSystem2d = class {
1338
+ particleCount = 2e3;
1339
+ _particleSize = 2;
1340
+ _frictionHalfLife = 0.04;
1341
+ _range = 0.1;
1342
+ _rangeFactor = 0.1;
1343
+ _attractionMatrix = rng.randomMatrix(colors.length);
1344
+ _particles = [];
1345
+ _spatialPartition;
1346
+ constructor() {
1347
+ for (let i = 0; i < this.particleCount; i++) {
1348
+ this._particles.push({
1349
+ x: rng.nextf,
1350
+ y: rng.nextf,
1351
+ vx: 0,
1352
+ vy: 0,
1353
+ color: rng.range(colors.length)
1354
+ });
1355
+ }
1356
+ this._spatialPartition = new SpatialPartition2d(this._range, this._particles);
1357
+ }
1358
+ _calcForce(r, a, beta = 0.3) {
1359
+ let f = 0;
1360
+ if (r < beta) {
1361
+ f = r / beta - 1;
1362
+ } else if (beta < r && r < 1) {
1363
+ f = a * (1 - Math.abs(2 * r - 1 - beta) / (1 - beta));
1364
+ }
1365
+ return f;
1366
+ }
1367
+ _updateVelocities(deltaTimeSeconds) {
1368
+ const frictionFactor = Math.pow(
1369
+ 0.5,
1370
+ deltaTimeSeconds / this._frictionHalfLife
1371
+ );
1372
+ for (let y = 0; y < this._spatialPartition.cells.length; y++) {
1373
+ for (let x = 0; x < this._spatialPartition.cells[y].length; x++) {
1374
+ const cell = this._spatialPartition.cells[y][x];
1375
+ for (let i = 0; i < cell.length; i++) {
1376
+ let fx = 0;
1377
+ let fy = 0;
1378
+ const p1 = cell[i];
1379
+ const neighborCells = this._spatialPartition.getCellNeighbors(x, y);
1380
+ for (const neighborCell of neighborCells) {
1782
1381
  for (let j = 0; j < neighborCell.length; j++) {
1783
1382
  if (cell === neighborCell && i === j) continue;
1784
1383
  const p2 = neighborCell[j];
@@ -1908,547 +1507,212 @@ function PoissonDiskSampler(width, height, minDist, maxPoints = 30) {
1908
1507
  return samples;
1909
1508
  }
1910
1509
 
1911
- // src/renderer/canvas-renderer.ts
1510
+ // src/rendering/canvas-renderer.ts
1912
1511
  var CanvasRenderer = class {
1913
- ctx;
1914
- clearColor = "#000000";
1915
- fontName = "monospace";
1916
- fontSize = 32;
1917
- textAlign = "center";
1918
- textBaseline = "middle";
1512
+ canvas;
1513
+ context;
1514
+ drawCentered = true;
1919
1515
  get width() {
1920
- return this.ctx?.canvas.width ?? 0;
1516
+ return this.context.canvas.width;
1921
1517
  }
1922
1518
  get height() {
1923
- return this.ctx?.canvas.height ?? 0;
1924
- }
1925
- get center() {
1926
- return [this.width / 2, this.height / 2];
1519
+ return this.context.canvas.height;
1927
1520
  }
1928
1521
  constructor(canvas) {
1929
- this.ctx = (canvas ?? document.createElement("canvas")).getContext("2d");
1930
- this.ctx.canvas.style.backgroundColor = "#000000";
1931
- this.ctx.canvas.style.display = "block";
1932
- this.ctx.font = `${this.fontSize}px ${this.fontName}`;
1933
- this.ctx.textAlign = this.textAlign;
1934
- this.ctx.textBaseline = this.textBaseline;
1935
- this.resize();
1936
- this.clear();
1522
+ this.canvas = canvas ?? document.createElement("canvas");
1523
+ this.context = this.canvas.getContext("2d");
1937
1524
  }
1938
- appendTo(target, autoResize = true) {
1939
- if (this.ctx.canvas.parentElement) {
1940
- this.ctx.canvas.parentElement.removeChild(this.ctx.canvas);
1941
- }
1942
- if (target) {
1943
- target.appendChild(this.ctx.canvas);
1944
- if (autoResize) {
1945
- this.resize();
1946
- }
1525
+ appendTo(target) {
1526
+ if (this.canvas.parentNode) {
1527
+ this.canvas.parentNode.removeChild(this.canvas);
1947
1528
  }
1529
+ target.appendChild(this.canvas);
1948
1530
  }
1949
1531
  resize(displayWidth, displayHeight) {
1950
- let resized = false;
1951
- const { width, height } = this.ctx.canvas.parentElement?.getBoundingClientRect() ?? this.ctx.canvas.getBoundingClientRect();
1532
+ const { width, height } = this.canvas.parentElement?.getBoundingClientRect() ?? this.canvas.getBoundingClientRect();
1952
1533
  displayWidth = 0 | (displayWidth ?? width);
1953
1534
  displayHeight = 0 | (displayHeight ?? height);
1954
- if (this.width !== displayWidth || this.height !== displayHeight) {
1955
- this.ctx.canvas.width = displayWidth;
1956
- this.ctx.canvas.height = displayHeight;
1957
- resized = true;
1535
+ if (this.canvas.width !== displayWidth || this.canvas.height !== displayHeight) {
1536
+ this.canvas.width = displayWidth;
1537
+ this.canvas.height = displayHeight;
1538
+ return true;
1958
1539
  }
1959
- return resized;
1540
+ return false;
1960
1541
  }
1961
1542
  clear() {
1962
- this.ctx?.clearRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height);
1963
- }
1964
- drawPoint(xy, params = {}) {
1965
- const [x, y] = xy;
1966
- const radius = params.radius || 1;
1967
- const fill = params.fill !== false;
1968
- const color = params.color || "#000000";
1969
- this.ctx.fillStyle = color;
1970
- this.ctx.strokeStyle = color;
1971
- this.ctx.beginPath();
1972
- this.ctx.arc(x, y, radius, 0, 2 * Math.PI);
1973
- this.ctx[fill ? "fill" : "stroke"]();
1974
- }
1975
- drawBox(xy, wh, params = {}) {
1976
- const [x, y] = xy;
1977
- const [w, h] = wh;
1978
- const fill = params.fill !== false;
1979
- const color = params.color || "#000000";
1980
- this.ctx.fillStyle = color;
1981
- this.ctx.strokeStyle = color;
1982
- this.ctx.beginPath();
1983
- this.ctx.moveTo(x, y);
1984
- this.ctx.lineTo(x + w, y);
1985
- this.ctx.lineTo(x + w, y + h);
1986
- this.ctx.lineTo(x, y + h);
1987
- this.ctx.closePath();
1988
- this.ctx[fill ? "fill" : "stroke"]();
1989
- }
1990
- drawLine(x1y1x2y2, color = "#000000") {
1991
- const [x1, y1, x2, y2] = x1y1x2y2;
1992
- this.ctx.strokeStyle = color;
1993
- this.ctx.beginPath();
1994
- this.ctx.moveTo(x1, y1);
1995
- this.ctx.lineTo(x2, y2);
1996
- this.ctx.stroke();
1997
- }
1998
- drawText(text, xy, color = "#ffffff", size = this.fontSize, font = this.fontName) {
1999
- const [x, y] = xy;
2000
- this.ctx.fillStyle = color;
2001
- this.ctx.font = `${size}px ${font}`;
2002
- this.ctx.textAlign = this.textAlign;
2003
- this.ctx.textBaseline = this.textBaseline;
2004
- this.ctx.fillText(text, x, y + 0.5);
2005
- }
2006
- // drawNamePlate(name: string, title: string, color = '#bbbbbb', s: number = 12, tile_size: number = 32) {
2007
- // let x = (actor.position.x * tile_size);
2008
- // let y = (actor.position.y * tile_size) - 18;
2009
- // let healthPercent = (actor.health / actor.maxHealth);
2010
- // let healthBarWidth = (healthPercent * tile_size)
2011
- // let healthColor = getRedYellowGreenGradientHex(actor.health, actor.maxHealth);
2012
- // this.drawBox(x, y, tile_size, 3, { fill: true, color: '#666666' });
2013
- // this.drawBox(x, y, healthBarWidth, 3, { fill: true, color: healthColor });
2014
- // if (actor.title) {
2015
- // this.drawBox(x, y - 16, actor.name.length * (this.fontSize / 5), 10, { fill: true, color: '#000000' });
2016
- // this.drawText(actor.name || '', x, y - 16, color, s);
2017
- // this.drawBox(x, y - 7, actor.title.length * (this.fontSize / 5), 10, { fill: true, color: '#000000' });
2018
- // this.drawText(`<${actor.title}>`, x, y - 6, '#00FFFF', s);
2019
- // } else {
2020
- // this.drawBox(x, y - 6, actor.name.length * (this.fontSize / 5), 10, { fill: true, color: '#000000' });
2021
- // this.drawText(actor.name || '', x, y - 6, color, s);
2022
- // }
2023
- // }
2024
- };
2025
-
2026
- // src/renderer/webgl.ts
2027
- var ShaderType = ((ShaderType2) => {
2028
- ShaderType2[ShaderType2["VERTEX"] = WebGL2RenderingContext.VERTEX_SHADER] = "VERTEX";
2029
- ShaderType2[ShaderType2["FRAGMENT"] = WebGL2RenderingContext.FRAGMENT_SHADER] = "FRAGMENT";
2030
- return ShaderType2;
2031
- })(ShaderType || {});
2032
- function CreateWebGlContext(canvas) {
2033
- return (canvas ?? document.createElement("canvas")).getContext("webgl2");
2034
- }
2035
- function ResizeWebGlContext(gl, displayWidth, displayHeight) {
2036
- const canvas = gl.canvas;
2037
- const { width, height } = canvas.parentElement?.getBoundingClientRect() ?? canvas.getBoundingClientRect();
2038
- displayWidth = 0 | (displayWidth ?? width);
2039
- displayHeight = 0 | (displayHeight ?? height);
2040
- if (canvas.width !== displayWidth || canvas.height !== displayHeight) {
2041
- gl.canvas.width = displayWidth;
2042
- gl.canvas.height = displayHeight;
2043
- gl.viewport(0, 0, displayWidth, displayHeight);
2044
- return true;
1543
+ this.context.clearRect(0, 0, this.width, this.height);
1544
+ }
1545
+ drawRect(x, y, width, height, params = {}) {
1546
+ if (this.drawCentered) {
1547
+ x -= width / 2;
1548
+ y -= height / 2;
1549
+ }
1550
+ this.context.strokeStyle = params.strokeColor ?? params.color ?? "white";
1551
+ this.context.fillStyle = params.fillColor ?? params.color ?? "white";
1552
+ this.context.lineWidth = params.size ?? 1;
1553
+ if (params.lineDash) {
1554
+ this.context.setLineDash(params.lineDash);
1555
+ }
1556
+ this.context.beginPath();
1557
+ if (params.filled === true) {
1558
+ this.context.fillRect(x, y, width, height);
1559
+ if (params.strokeColor !== void 0) this.context.strokeRect(x, y, width, height);
1560
+ } else {
1561
+ this.context.strokeRect(x, y, width, height);
1562
+ }
1563
+ if (params.lineDash) {
1564
+ this.context.setLineDash([]);
1565
+ }
2045
1566
  }
2046
- return false;
2047
- }
2048
- function CompileShader(gl, type, source) {
2049
- const shader = gl.createShader(type);
2050
- gl.shaderSource(shader, source);
2051
- gl.compileShader(shader);
2052
- if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
2053
- throw `CompileShader: failed to compile shader: ${gl.getShaderInfoLog(shader)}`;
1567
+ drawPixel(x, y, params = {}) {
1568
+ const size = abs(params.size || 1);
1569
+ this.drawRect(x, y, size, size, {
1570
+ ...params,
1571
+ filled: true
1572
+ });
2054
1573
  }
2055
- return shader;
2056
- }
2057
- function CreateProgram(gl, vertexShader2, fragmentShader2) {
2058
- if (typeof vertexShader2 === "string") {
2059
- vertexShader2 = CompileShader(gl, ShaderType.VERTEX, vertexShader2);
1574
+ drawLine(x1, y1, x2, y2, params = {}) {
1575
+ this.context.strokeStyle = params.color ?? "white";
1576
+ this.context.lineWidth = params.size ?? 1;
1577
+ if (params.lineDash) {
1578
+ this.context.setLineDash(params.lineDash);
1579
+ }
1580
+ this.context.beginPath();
1581
+ this.context.moveTo(x1, y1);
1582
+ this.context.lineTo(x2, y2);
1583
+ this.context.stroke();
1584
+ if (params.lineDash) {
1585
+ this.context.setLineDash([]);
1586
+ }
1587
+ }
1588
+ drawCircle(x, y, radius, params = {}) {
1589
+ this.context.strokeStyle = params.strokeColor ?? params.color ?? "white";
1590
+ this.context.fillStyle = params.fillColor ?? params.color ?? "white";
1591
+ this.context.lineWidth = params.size ?? 1;
1592
+ if (params.lineDash) {
1593
+ this.context.setLineDash(params.lineDash);
1594
+ }
1595
+ this.context.beginPath();
1596
+ this.context.arc(x, y, radius, 0, 2 * PI);
1597
+ if (params.filled === true) this.context.fill();
1598
+ this.context.stroke();
1599
+ if (params.lineDash) {
1600
+ this.context.setLineDash([]);
1601
+ }
1602
+ }
1603
+ drawText(x, y, text, params = {}) {
1604
+ this.context.font = `${params.size ?? 14}px ${params.fontName ?? "monospace"}`;
1605
+ this.context.textAlign = params.textAlign ?? "left";
1606
+ this.context.textBaseline = params.textBaseline ?? "alphabetic";
1607
+ this.context.fillStyle = params.color ?? "white";
1608
+ this.context.fillText(text, x, y);
2060
1609
  }
2061
- if (typeof fragmentShader2 === "string") {
2062
- fragmentShader2 = CompileShader(gl, ShaderType.FRAGMENT, fragmentShader2);
1610
+ };
1611
+
1612
+ // src/rendering/particle-renderer.ts
1613
+ var ParticleRenderer = class {
1614
+ _screenCanvas;
1615
+ _bufferCanvas;
1616
+ screenContext;
1617
+ bufferContext;
1618
+ get width() {
1619
+ return this._screenCanvas.width;
2063
1620
  }
2064
- const program = gl.createProgram();
2065
- gl.attachShader(program, vertexShader2);
2066
- gl.attachShader(program, fragmentShader2);
2067
- gl.linkProgram(program);
2068
- if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
2069
- throw `CreateProgram: failed to link program: ${gl.getProgramInfoLog(program)}`;
1621
+ get height() {
1622
+ return this._screenCanvas.height;
2070
1623
  }
2071
- return program;
2072
- }
2073
- function GetProgramLocations(gl, program) {
2074
- const attributes = {};
2075
- const attributeCount = gl.getProgramParameter(program, gl.ACTIVE_ATTRIBUTES);
2076
- for (let i = 0; i < attributeCount; i++) {
2077
- const info = gl.getActiveAttrib(program, i);
2078
- attributes[info.name] = gl.getAttribLocation(program, info.name);
2079
- }
2080
- const uniforms = {};
2081
- const uniformCount = gl.getProgramParameter(program, gl.ACTIVE_UNIFORMS);
2082
- for (let i = 0; i < uniformCount; i++) {
2083
- const info = gl.getActiveUniform(program, i);
2084
- uniforms[info.name] = gl.getUniformLocation(program, info.name);
2085
- }
2086
- return { attributes, uniforms };
2087
- }
2088
- function CreateProgramInfo(gl, vertexShaderSource, fragmentShaderSource) {
2089
- const program = CreateProgram(
2090
- gl,
2091
- vertexShaderSource,
2092
- fragmentShaderSource
2093
- );
2094
- const locations = GetProgramLocations(gl, program);
2095
- return { program, ...locations };
2096
- }
2097
-
2098
- // src/renderer/webgl-renderer.ts
2099
- var WebGlRenderer = class {
2100
- gl;
2101
- programInfo;
2102
- get canvas() {
2103
- return this.gl.canvas;
2104
- }
2105
- get width() {
2106
- return this.gl.canvas.width;
2107
- }
2108
- get height() {
2109
- return this.gl.canvas.height;
2110
- }
2111
- constructor() {
2112
- this.gl = CreateWebGlContext();
2113
- this.gl.enable(this.gl.DEPTH_TEST);
1624
+ constructor(canvas) {
1625
+ this._screenCanvas = canvas ?? document.createElement("canvas");
1626
+ this._bufferCanvas = new OffscreenCanvas(this._screenCanvas.width, this._screenCanvas.height);
1627
+ this.screenContext = this._screenCanvas.getContext("2d");
1628
+ this.bufferContext = this._bufferCanvas.getContext("2d");
1629
+ if (!this.screenContext || !this.bufferContext) {
1630
+ throw "Failed to create CanvasRenderingContext2D";
1631
+ }
2114
1632
  }
2115
- appendTo(htmlElement) {
2116
- if (this.canvas.parentElement) {
2117
- this.canvas.parentElement.removeChild(this.canvas);
1633
+ appendTo(target, autoResize = true) {
1634
+ if (this._screenCanvas.parentNode) {
1635
+ this._screenCanvas.parentNode.removeChild(this._screenCanvas);
2118
1636
  }
2119
- if (htmlElement) {
2120
- htmlElement.appendChild(this.canvas);
1637
+ target.appendChild(this._screenCanvas);
1638
+ if (autoResize) {
2121
1639
  this.resize();
2122
1640
  }
2123
1641
  }
2124
1642
  resize(displayWidth, displayHeight) {
2125
- const { width, height } = this.canvas.parentElement?.getBoundingClientRect() ?? this.canvas.getBoundingClientRect();
1643
+ const { width, height } = this._screenCanvas.parentElement?.getBoundingClientRect() ?? this._screenCanvas.getBoundingClientRect();
2126
1644
  displayWidth = 0 | (displayWidth ?? width);
2127
1645
  displayHeight = 0 | (displayHeight ?? height);
2128
- if (this.width !== displayWidth || this.height !== displayHeight) {
2129
- this.gl.canvas.width = displayWidth;
2130
- this.gl.canvas.height = displayHeight;
2131
- this.gl.viewport(0, 0, displayWidth, displayHeight);
1646
+ if (this._screenCanvas.width !== displayWidth || this._screenCanvas.height !== displayHeight) {
1647
+ this._screenCanvas.width = displayWidth;
1648
+ this._screenCanvas.height = displayHeight;
1649
+ this._bufferCanvas.width = displayWidth;
1650
+ this._bufferCanvas.height = displayHeight;
2132
1651
  return true;
2133
1652
  }
2134
1653
  return false;
2135
1654
  }
2136
- render(_deltaTime) {
2137
- if (this.programInfo) {
2138
- }
1655
+ setPixel(x, y, color, size = 2) {
1656
+ this.bufferContext.fillStyle = color.hexStr;
1657
+ this.bufferContext.fillRect(x - size / 2, y - size / 2, size, size);
1658
+ }
1659
+ render() {
1660
+ this.screenContext.clearRect(0, 0, this._screenCanvas.width, this._screenCanvas.height);
1661
+ this.screenContext.drawImage(this._bufferCanvas, 0, 0);
1662
+ this.bufferContext.clearRect(0, 0, this._screenCanvas.width, this._screenCanvas.height);
2139
1663
  }
2140
1664
  };
2141
1665
 
2142
- // src/simplex-noise.ts
2143
- var SimplexNoise = class {
2144
- grad3;
2145
- p;
2146
- perm;
2147
- constructor() {
2148
- this.grad3 = [
2149
- [1, 1, 0],
2150
- [-1, 1, 0],
2151
- [1, -1, 0],
2152
- [-1, -1, 0],
2153
- [1, 0, 1],
2154
- [-1, 0, 1],
2155
- [1, 0, -1],
2156
- [-1, 0, -1],
2157
- [0, 1, 1],
2158
- [0, -1, 1],
2159
- [0, 1, -1],
2160
- [0, -1, -1]
2161
- ];
2162
- this.p = [];
2163
- for (let i = 0; i < 256; i++) {
2164
- this.p.push(Math.floor(rng.nextf * 256));
2165
- }
2166
- this.perm = [];
2167
- for (let i = 0; i < 512; i++) {
2168
- this.perm.push(this.p[i & 255]);
1666
+ // src/rendering/three/cube-sphere-geometry.ts
1667
+ var import_three = require("three");
1668
+ var CubeSphereGeometry = class extends import_three.BoxGeometry {
1669
+ constructor(size, segments) {
1670
+ size = Math.floor(size);
1671
+ segments = Math.floor(segments);
1672
+ super(size, size, size, segments, segments, segments);
1673
+ this.size = size;
1674
+ this.segments = segments;
1675
+ this._halfSize = this.size / 2;
1676
+ const attrPositions = this.attributes.position;
1677
+ const position = new import_three.Vector3();
1678
+ for (let i = 0; i < attrPositions.count; i++) {
1679
+ position.fromBufferAttribute(attrPositions, i);
1680
+ position.normalize().multiplyScalar(this._halfSize);
1681
+ attrPositions.setXYZ(i, position.x, position.y, position.z);
2169
1682
  }
1683
+ attrPositions.needsUpdate = true;
2170
1684
  }
2171
- dot(g, x, y, z) {
2172
- return g[0] * x + g[1] * y + (typeof z === "number" ? g[2] * z : 0);
2173
- }
2174
- noise2d(xin, yin) {
2175
- let n0, n1, n2;
2176
- const F2 = 0.5 * (Math.sqrt(3) - 1);
2177
- const s = (xin + yin) * F2;
2178
- let i = Math.floor(xin + s);
2179
- let j = Math.floor(yin + s);
2180
- const G2 = (3 - Math.sqrt(3)) / 6;
2181
- const t = (i + j) * G2;
2182
- const X0 = i - t;
2183
- const Y0 = j - t;
2184
- const x0 = xin - X0;
2185
- const y0 = yin - Y0;
2186
- let i1, j1;
2187
- if (x0 > y0) {
2188
- i1 = 1;
2189
- j1 = 0;
2190
- } else {
2191
- i1 = 0;
2192
- j1 = 1;
2193
- }
2194
- const x1 = x0 - i1 + G2;
2195
- const y1 = y0 - j1 + G2;
2196
- const x2 = x0 - 1 + 2 * G2;
2197
- const y2 = y0 - 1 + 2 * G2;
2198
- const ii = i & 255;
2199
- const jj = j & 255;
2200
- const gi0 = this.perm[ii + this.perm[jj]] % 12;
2201
- const gi1 = this.perm[ii + i1 + this.perm[jj + j1]] % 12;
2202
- const gi2 = this.perm[ii + 1 + this.perm[jj + 1]] % 12;
2203
- let t0 = 0.5 - x0 * x0 - y0 * y0;
2204
- if (t0 < 0) {
2205
- n0 = 0;
2206
- } else {
2207
- t0 *= t0;
2208
- n0 = t0 * t0 * this.dot(this.grad3[gi0], x0, y0);
2209
- }
2210
- let t1 = 0.5 - x1 * x1 - y1 * y1;
2211
- if (t1 < 0) {
2212
- n1 = 0;
2213
- } else {
2214
- t1 *= t1;
2215
- n1 = t1 * t1 * this.dot(this.grad3[gi1], x1, y1);
1685
+ size;
1686
+ segments;
1687
+ _halfSize;
1688
+ applyFractalBrownianMotion(octaves, persistence, scale, amplitude) {
1689
+ const attrPositions = this.attributes.position;
1690
+ const position = new import_three.Vector3();
1691
+ for (let i = 0; i < attrPositions.count; i++) {
1692
+ position.fromBufferAttribute(attrPositions, i);
1693
+ position.normalize().multiplyScalar(this._halfSize);
1694
+ const noiseValue = this.fBm(position.x * scale, position.y * scale, position.z * scale, octaves, persistence);
1695
+ position.addScaledVector(position.clone().normalize(), noiseValue * amplitude);
1696
+ attrPositions.setXYZ(i, position.x, position.y, position.z);
2216
1697
  }
2217
- let t2 = 0.5 - x2 * x2 - y2 * y2;
2218
- if (t2 < 0) {
2219
- n2 = 0;
2220
- } else {
2221
- t2 *= t2;
2222
- n2 = t2 * t2 * this.dot(this.grad3[gi2], x2, y2);
1698
+ attrPositions.needsUpdate = true;
1699
+ }
1700
+ fBm(x, y, z, octaves, persistence) {
1701
+ let total = 0;
1702
+ let frequency = 1;
1703
+ let amplitude = 1;
1704
+ let maxValue = 0;
1705
+ for (let i = 0; i < octaves; i++) {
1706
+ total += Noise.noise3d(x * frequency, y * frequency, z * frequency) * amplitude;
1707
+ maxValue += amplitude;
1708
+ amplitude *= persistence;
1709
+ frequency *= 2;
2223
1710
  }
2224
- return 70 * (n0 + n1 + n2);
2225
- }
2226
- // 3D simplex noise
2227
- noise3d(xin, yin, zin) {
2228
- let n0, n1, n2, n3;
2229
- const F3 = 1 / 3;
2230
- const s = (xin + yin + zin) * F3;
2231
- const i = Math.floor(xin + s);
2232
- const j = Math.floor(yin + s);
2233
- const k = Math.floor(zin + s);
2234
- const G3 = 1 / 6;
2235
- const t = (i + j + k) * G3;
2236
- const X0 = i - t;
2237
- const Y0 = j - t;
2238
- const Z0 = k - t;
2239
- const x0 = xin - X0;
2240
- const y0 = yin - Y0;
2241
- const z0 = zin - Z0;
2242
- let i1, j1, k1;
2243
- let i2, j2, k2;
2244
- if (x0 >= y0) {
2245
- if (y0 >= z0) {
2246
- i1 = 1;
2247
- j1 = 0;
2248
- k1 = 0;
2249
- i2 = 1;
2250
- j2 = 1;
2251
- k2 = 0;
2252
- } else if (x0 >= z0) {
2253
- i1 = 1;
2254
- j1 = 0;
2255
- k1 = 0;
2256
- i2 = 1;
2257
- j2 = 0;
2258
- k2 = 1;
2259
- } else {
2260
- i1 = 0;
2261
- j1 = 0;
2262
- k1 = 1;
2263
- i2 = 1;
2264
- j2 = 0;
2265
- k2 = 1;
2266
- }
2267
- } else {
2268
- if (y0 < z0) {
2269
- i1 = 0;
2270
- j1 = 0;
2271
- k1 = 1;
2272
- i2 = 0;
2273
- j2 = 1;
2274
- k2 = 1;
2275
- } else if (x0 < z0) {
2276
- i1 = 0;
2277
- j1 = 1;
2278
- k1 = 0;
2279
- i2 = 0;
2280
- j2 = 1;
2281
- k2 = 1;
2282
- } else {
2283
- i1 = 0;
2284
- j1 = 1;
2285
- k1 = 0;
2286
- i2 = 1;
2287
- j2 = 1;
2288
- k2 = 0;
2289
- }
2290
- }
2291
- const x1 = x0 - i1 + G3;
2292
- const y1 = y0 - j1 + G3;
2293
- const z1 = z0 - k1 + G3;
2294
- const x2 = x0 - i2 + 2 * G3;
2295
- const y2 = y0 - j2 + 2 * G3;
2296
- const z2 = z0 - k2 + 2 * G3;
2297
- const x3 = x0 - 1 + 3 * G3;
2298
- const y3 = y0 - 1 + 3 * G3;
2299
- const z3 = z0 - 1 + 3 * G3;
2300
- const ii = i & 255;
2301
- const jj = j & 255;
2302
- const kk = k & 255;
2303
- const gi0 = this.perm[ii + this.perm[jj + this.perm[kk]]] % 12;
2304
- const gi1 = this.perm[ii + i1 + this.perm[jj + j1 + this.perm[kk + k1]]] % 12;
2305
- const gi2 = this.perm[ii + i2 + this.perm[jj + j2 + this.perm[kk + k2]]] % 12;
2306
- const gi3 = this.perm[ii + 1 + this.perm[jj + 1 + this.perm[kk + 1]]] % 12;
2307
- let t0 = 0.6 - x0 * x0 - y0 * y0 - z0 * z0;
2308
- if (t0 < 0) {
2309
- n0 = 0;
2310
- } else {
2311
- t0 *= t0;
2312
- n0 = t0 * t0 * this.dot(this.grad3[gi0], x0, y0, z0);
2313
- }
2314
- let t1 = 0.6 - x1 * x1 - y1 * y1 - z1 * z1;
2315
- if (t1 < 0) {
2316
- n1 = 0;
2317
- } else {
2318
- t1 *= t1;
2319
- n1 = t1 * t1 * this.dot(this.grad3[gi1], x1, y1, z1);
2320
- }
2321
- let t2 = 0.6 - x2 * x2 - y2 * y2 - z2 * z2;
2322
- if (t2 < 0) {
2323
- n2 = 0;
2324
- } else {
2325
- t2 *= t2;
2326
- n2 = t2 * t2 * this.dot(this.grad3[gi2], x2, y2, z2);
2327
- }
2328
- let t3 = 0.6 - x3 * x3 - y3 * y3 - z3 * z3;
2329
- if (t3 < 0) {
2330
- n3 = 0;
2331
- } else {
2332
- t3 *= t3;
2333
- n3 = t3 * t3 * this.dot(this.grad3[gi3], x3, y3, z3);
2334
- }
2335
- return 32 * (n0 + n1 + n2 + n3);
2336
- }
2337
- };
2338
-
2339
- // src/spatial-partition-3d.ts
2340
- var SpatialPartition3d = class {
2341
- cells;
2342
- cellSize;
2343
- entities;
2344
- constructor(cellSize, entities) {
2345
- this.cellSize = cellSize;
2346
- this.entities = entities;
2347
- const gridSize = Math.ceil(1 / cellSize);
2348
- this.cells = Array.from(
2349
- { length: gridSize },
2350
- () => Array.from(
2351
- { length: gridSize },
2352
- () => Array.from({ length: gridSize }, () => [])
2353
- )
2354
- );
2355
- for (const entity of this.entities) {
2356
- this.addEntity(entity);
2357
- }
2358
- }
2359
- getCell(x, y, z) {
2360
- const cx = Math.floor(x / this.cellSize);
2361
- const cy = Math.floor(y / this.cellSize);
2362
- const cz = Math.floor(z / this.cellSize);
2363
- return this.cells[cz][cy][cx];
2364
- }
2365
- getCellNeighbors(cx, cy, cz) {
2366
- const neighbors = [];
2367
- const gridSize = this.cells.length;
2368
- for (let dz = -1; dz <= 1; dz++) {
2369
- for (let dy = -1; dy <= 1; dy++) {
2370
- for (let dx = -1; dx <= 1; dx++) {
2371
- let nx = (cx + dx + gridSize) % gridSize;
2372
- let ny = (cy + dy + gridSize) % gridSize;
2373
- let nz = (cz + dz + gridSize) % gridSize;
2374
- neighbors.push(this.cells[nz][ny][nx]);
2375
- }
2376
- }
2377
- }
2378
- return neighbors;
2379
- }
2380
- addEntity(entity) {
2381
- const cell = this.getCell(entity.x, entity.y, entity.z);
2382
- cell.push(entity);
2383
- }
2384
- };
2385
-
2386
- // src/tilemap.ts
2387
- var Tilemap = class {
2388
- map = /* @__PURE__ */ new Map();
2389
- _key(x, y) {
2390
- x = floor(x);
2391
- y = floor(y);
2392
- return `${x}x${y}`;
2393
- }
2394
- get(x, y) {
2395
- return this.map.get(this._key(x, y));
2396
- }
2397
- set(x, y, v) {
2398
- this.map.set(this._key(x, y), v);
2399
- }
2400
- };
2401
-
2402
- // src/three/cube-sphere-geometry.ts
2403
- var import_three = require("three");
2404
- var CubeSphereGeometry = class extends import_three.BoxGeometry {
2405
- constructor(size, segments) {
2406
- size = Math.floor(size);
2407
- segments = Math.floor(segments);
2408
- super(size, size, size, segments, segments, segments);
2409
- this.size = size;
2410
- this.segments = segments;
2411
- this._halfSize = this.size / 2;
2412
- const attrPositions = this.attributes.position;
2413
- const position = new import_three.Vector3();
2414
- for (let i = 0; i < attrPositions.count; i++) {
2415
- position.fromBufferAttribute(attrPositions, i);
2416
- position.normalize().multiplyScalar(this._halfSize);
2417
- attrPositions.setXYZ(i, position.x, position.y, position.z);
2418
- }
2419
- attrPositions.needsUpdate = true;
2420
- }
2421
- size;
2422
- segments;
2423
- _halfSize;
2424
- applyFractalBrownianMotion(octaves, persistence, scale, amplitude) {
2425
- const attrPositions = this.attributes.position;
2426
- const position = new import_three.Vector3();
2427
- for (let i = 0; i < attrPositions.count; i++) {
2428
- position.fromBufferAttribute(attrPositions, i);
2429
- position.normalize().multiplyScalar(this._halfSize);
2430
- const noiseValue = this.fBm(position.x * scale, position.y * scale, position.z * scale, octaves, persistence);
2431
- position.addScaledVector(position.clone().normalize(), noiseValue * amplitude);
2432
- attrPositions.setXYZ(i, position.x, position.y, position.z);
2433
- }
2434
- attrPositions.needsUpdate = true;
2435
- }
2436
- fBm(x, y, z, octaves, persistence) {
2437
- let total = 0;
2438
- let frequency = 1;
2439
- let amplitude = 1;
2440
- let maxValue = 0;
2441
- for (let i = 0; i < octaves; i++) {
2442
- total += Noise.noise3d(x * frequency, y * frequency, z * frequency) * amplitude;
2443
- maxValue += amplitude;
2444
- amplitude *= persistence;
2445
- frequency *= 2;
2446
- }
2447
- return total / maxValue;
1711
+ return total / maxValue;
2448
1712
  }
2449
1713
  };
2450
1714
 
2451
- // src/three/texture-atlas.ts
1715
+ // src/rendering/three/texture-atlas.ts
2452
1716
  var import_three2 = require("three");
2453
1717
  function LoadTexture(url) {
2454
1718
  return new Promise((res, rej) => {
@@ -2573,10 +1837,10 @@ var AtlasTextureMaterial = class _AtlasTextureMaterial extends import_three2.Mes
2573
1837
  }
2574
1838
  };
2575
1839
 
2576
- // src/three/threejs-boiler-plate.ts
1840
+ // src/rendering/three/threejs-boiler-plate.ts
2577
1841
  var import_three4 = require("three");
2578
1842
 
2579
- // src/three/threejs-camera-rig.ts
1843
+ // src/rendering/three/threejs-camera-rig.ts
2580
1844
  var import_three3 = require("three");
2581
1845
  var ThreeJsCameraRig = class extends import_three3.Group {
2582
1846
  gimbal;
@@ -2618,7 +1882,7 @@ var ThreeJsCameraRig = class extends import_three3.Group {
2618
1882
  }
2619
1883
  };
2620
1884
 
2621
- // src/three/threejs-boiler-plate.ts
1885
+ // src/rendering/three/threejs-boiler-plate.ts
2622
1886
  var ThreeJsBoilerPlate = class {
2623
1887
  clock = new Clock();
2624
1888
  scene = new import_three4.Scene();
@@ -2643,24 +1907,15 @@ var ThreeJsBoilerPlate = class {
2643
1907
  this.appendTo(params.parentElement);
2644
1908
  }
2645
1909
  }
2646
- // Mutually exclusive with enableOrbitControls() - pick one. Gives you
2647
- // manual orbit()/dolly() with tilt clamping and target-following, driven
2648
- // by this.input's mouse events.
2649
1910
  enableCameraRigControls() {
2650
1911
  this.orbitControls?.dispose();
2651
1912
  this.orbitControls = void 0;
2652
1913
  this.input.on("mouse_move", ({ deltaX, deltaY }) => {
2653
- if (this.input.isMouseButtonDown(0)) {
1914
+ if (this.input.isDown("Button0")) {
2654
1915
  this.cameraRig.orbit(deltaX, deltaY);
2655
1916
  }
2656
1917
  }).on("mouse_wheel", ({ deltaY }) => this.cameraRig.dolly(deltaY));
2657
1918
  }
2658
- // Mutually exclusive with enableCameraRigControls() - pick one. Uses
2659
- // Three's own OrbitControls instead of the hand-rolled rig math; call
2660
- // update() once per frame in your render loop while this is active.
2661
- // Async because OrbitControls only ships as an ESM-only addon module -
2662
- // a dynamic import avoids forcing this whole package (built as both
2663
- // CJS and ESM) to statically import an ESM-only file.
2664
1919
  async enableOrbitControls() {
2665
1920
  const { OrbitControls } = await import("three/examples/jsm/Addons.js");
2666
1921
  this.orbitControls = new OrbitControls(this.camera, this.canvas);
@@ -2668,7 +1923,6 @@ var ThreeJsBoilerPlate = class {
2668
1923
  this.orbitControls.maxDistance = 100;
2669
1924
  this.orbitControls.update();
2670
1925
  }
2671
- // Call once per frame if enableOrbitControls() is active - no-op otherwise.
2672
1926
  update() {
2673
1927
  this.orbitControls?.update();
2674
1928
  }
@@ -2704,8 +1958,8 @@ var ThreeJsBoilerPlate = class {
2704
1958
  }
2705
1959
  }
2706
1960
  pick() {
2707
- const pickX = this.input.mousePosition[0] / this.renderer.domElement.width * 2 - 1;
2708
- const pickY = -(this.input.mousePosition[1] / this.renderer.domElement.height) * 2 + 1;
1961
+ const pickX = this.input.pointerPosition.x / this.renderer.domElement.width * 2 - 1;
1962
+ const pickY = -(this.input.pointerPosition.y / this.renderer.domElement.height) * 2 + 1;
2709
1963
  this.raycaster.setFromCamera(new import_three4.Vector2(pickX, pickY), this.cameraRig.camera);
2710
1964
  const intersected = this.raycaster.intersectObjects(this.scene.children);
2711
1965
  return intersected.length ? intersected[0] : null;
@@ -2726,7 +1980,7 @@ var ThreeJsBoilerPlate = class {
2726
1980
  }
2727
1981
  };
2728
1982
 
2729
- // src/three/threejs-player-controller.ts
1983
+ // src/rendering/three/threejs-player-controller.ts
2730
1984
  var import_three5 = require("three");
2731
1985
  var ThreeJsPlayerController = class extends import_three5.Group {
2732
1986
  moveSpeed = 1;
@@ -2745,7 +1999,7 @@ var ThreeJsPlayerController = class extends import_three5.Group {
2745
1999
  }
2746
2000
  };
2747
2001
 
2748
- // src/three/voxel-mesh.ts
2002
+ // src/rendering/three/voxel-mesh.ts
2749
2003
  var import_three6 = require("three");
2750
2004
  var VoxelMesh = class extends import_three6.Mesh {
2751
2005
  type = "VoxelMesh";
@@ -3095,8 +2349,57 @@ var Colors = {
3095
2349
  ]
3096
2350
  };
3097
2351
 
3098
- // src/three/particle-system-3d.ts
2352
+ // src/rendering/three/particle-system-3d.ts
3099
2353
  var import_three7 = require("three");
2354
+
2355
+ // src/spatial-partition-3d.ts
2356
+ var SpatialPartition3d = class {
2357
+ cells;
2358
+ cellSize;
2359
+ entities;
2360
+ constructor(cellSize, entities) {
2361
+ this.cellSize = cellSize;
2362
+ this.entities = entities;
2363
+ const gridSize = Math.ceil(1 / cellSize);
2364
+ this.cells = Array.from(
2365
+ { length: gridSize },
2366
+ () => Array.from(
2367
+ { length: gridSize },
2368
+ () => Array.from({ length: gridSize }, () => [])
2369
+ )
2370
+ );
2371
+ for (const entity of this.entities) {
2372
+ this.addEntity(entity);
2373
+ }
2374
+ }
2375
+ getCell(x, y, z) {
2376
+ const cx = Math.floor(x / this.cellSize);
2377
+ const cy = Math.floor(y / this.cellSize);
2378
+ const cz = Math.floor(z / this.cellSize);
2379
+ return this.cells[cz][cy][cx];
2380
+ }
2381
+ getCellNeighbors(cx, cy, cz) {
2382
+ const neighbors = [];
2383
+ const gridSize = this.cells.length;
2384
+ for (let dz = -1; dz <= 1; dz++) {
2385
+ for (let dy = -1; dy <= 1; dy++) {
2386
+ for (let dx = -1; dx <= 1; dx++) {
2387
+ let nx = (cx + dx + gridSize) % gridSize;
2388
+ let ny = (cy + dy + gridSize) % gridSize;
2389
+ let nz = (cz + dz + gridSize) % gridSize;
2390
+ neighbors.push(this.cells[nz][ny][nx]);
2391
+ }
2392
+ }
2393
+ }
2394
+ return neighbors;
2395
+ }
2396
+ addEntity(entity) {
2397
+ const cell = this.getCell(entity.x, entity.y, entity.z);
2398
+ cell.push(entity);
2399
+ }
2400
+ };
2401
+
2402
+ // src/rendering/three/particle-system-3d.ts
3100
2403
  var vertexShader = `
3101
2404
  uniform float pointSize;
3102
2405
 
@@ -3285,7 +2588,7 @@ var ParticleSystem3d = class extends import_three7.Group {
3285
2588
  }
3286
2589
  };
3287
2590
 
3288
- // src/three/asteroid-mesh.ts
2591
+ // src/rendering/three/asteroid-mesh.ts
3289
2592
  var import_three8 = require("three");
3290
2593
  var import_simplex_noise2 = require("simplex-noise");
3291
2594
  var AsteroidMesh = class extends import_three8.Mesh {
@@ -3320,7 +2623,7 @@ var AsteroidMesh = class extends import_three8.Mesh {
3320
2623
  }
3321
2624
  };
3322
2625
 
3323
- // src/three/starfield.ts
2626
+ // src/rendering/three/starfield.ts
3324
2627
  var import_three9 = require("three");
3325
2628
  function CreateStarfield(radius = 1e3, count = 5e3, color = 16777215) {
3326
2629
  const geometry = new import_three9.BufferGeometry();
@@ -3359,7 +2662,7 @@ function CreateSystemStar(position) {
3359
2662
  return star;
3360
2663
  }
3361
2664
 
3362
- // src/three/palette-controller.ts
2665
+ // src/rendering/three/palette-controller.ts
3363
2666
  var import_three10 = require("three");
3364
2667
  async function CreatePaletteController(parentGui, colorPalette, onChange, folderName = "palette") {
3365
2668
  const { Controller } = await import("three/examples/jsm/libs/lil-gui.module.min.js");
@@ -3464,7 +2767,7 @@ async function CreatePaletteController(parentGui, colorPalette, onChange, folder
3464
2767
  return new PaletteControllerImpl();
3465
2768
  }
3466
2769
 
3467
- // src/three/ecs.ts
2770
+ // src/rendering/three/ecs.ts
3468
2771
  var import_three11 = require("three");
3469
2772
  var entityCounter = 0;
3470
2773
  function GenerateEntityId() {
@@ -3489,6 +2792,334 @@ function IsThreeEntity(obj) {
3489
2792
  return obj.userData.entityId !== void 0;
3490
2793
  }
3491
2794
 
2795
+ // src/rendering/webgl.ts
2796
+ var ShaderType = ((ShaderType2) => {
2797
+ ShaderType2[ShaderType2["VERTEX"] = WebGL2RenderingContext.VERTEX_SHADER] = "VERTEX";
2798
+ ShaderType2[ShaderType2["FRAGMENT"] = WebGL2RenderingContext.FRAGMENT_SHADER] = "FRAGMENT";
2799
+ return ShaderType2;
2800
+ })(ShaderType || {});
2801
+ function CreateWebGlContext(canvas) {
2802
+ return (canvas ?? document.createElement("canvas")).getContext("webgl2");
2803
+ }
2804
+ function ResizeWebGlContext(gl, displayWidth, displayHeight) {
2805
+ const canvas = gl.canvas;
2806
+ const { width, height } = canvas.parentElement?.getBoundingClientRect() ?? canvas.getBoundingClientRect();
2807
+ displayWidth = 0 | (displayWidth ?? width);
2808
+ displayHeight = 0 | (displayHeight ?? height);
2809
+ if (canvas.width !== displayWidth || canvas.height !== displayHeight) {
2810
+ gl.canvas.width = displayWidth;
2811
+ gl.canvas.height = displayHeight;
2812
+ gl.viewport(0, 0, displayWidth, displayHeight);
2813
+ return true;
2814
+ }
2815
+ return false;
2816
+ }
2817
+ function CompileShader(gl, type, source) {
2818
+ const shader = gl.createShader(type);
2819
+ gl.shaderSource(shader, source);
2820
+ gl.compileShader(shader);
2821
+ if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
2822
+ throw `CompileShader: failed to compile shader: ${gl.getShaderInfoLog(shader)}`;
2823
+ }
2824
+ return shader;
2825
+ }
2826
+ function CreateProgram(gl, vertexShader2, fragmentShader2) {
2827
+ if (typeof vertexShader2 === "string") {
2828
+ vertexShader2 = CompileShader(gl, ShaderType.VERTEX, vertexShader2);
2829
+ }
2830
+ if (typeof fragmentShader2 === "string") {
2831
+ fragmentShader2 = CompileShader(gl, ShaderType.FRAGMENT, fragmentShader2);
2832
+ }
2833
+ const program = gl.createProgram();
2834
+ gl.attachShader(program, vertexShader2);
2835
+ gl.attachShader(program, fragmentShader2);
2836
+ gl.linkProgram(program);
2837
+ if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
2838
+ throw `CreateProgram: failed to link program: ${gl.getProgramInfoLog(program)}`;
2839
+ }
2840
+ return program;
2841
+ }
2842
+ function GetProgramLocations(gl, program) {
2843
+ const attributes = {};
2844
+ const attributeCount = gl.getProgramParameter(program, gl.ACTIVE_ATTRIBUTES);
2845
+ for (let i = 0; i < attributeCount; i++) {
2846
+ const info = gl.getActiveAttrib(program, i);
2847
+ attributes[info.name] = gl.getAttribLocation(program, info.name);
2848
+ }
2849
+ const uniforms = {};
2850
+ const uniformCount = gl.getProgramParameter(program, gl.ACTIVE_UNIFORMS);
2851
+ for (let i = 0; i < uniformCount; i++) {
2852
+ const info = gl.getActiveUniform(program, i);
2853
+ uniforms[info.name] = gl.getUniformLocation(program, info.name);
2854
+ }
2855
+ return { attributes, uniforms };
2856
+ }
2857
+ function CreateProgramInfo(gl, vertexShaderSource, fragmentShaderSource) {
2858
+ const program = CreateProgram(
2859
+ gl,
2860
+ vertexShaderSource,
2861
+ fragmentShaderSource
2862
+ );
2863
+ const locations = GetProgramLocations(gl, program);
2864
+ return { program, ...locations };
2865
+ }
2866
+
2867
+ // src/rendering/webgl-renderer.ts
2868
+ var WebGlRenderer = class {
2869
+ gl;
2870
+ programInfo;
2871
+ get canvas() {
2872
+ return this.gl.canvas;
2873
+ }
2874
+ get width() {
2875
+ return this.gl.canvas.width;
2876
+ }
2877
+ get height() {
2878
+ return this.gl.canvas.height;
2879
+ }
2880
+ constructor() {
2881
+ this.gl = CreateWebGlContext();
2882
+ this.gl.enable(this.gl.DEPTH_TEST);
2883
+ }
2884
+ appendTo(htmlElement) {
2885
+ if (this.canvas.parentElement) {
2886
+ this.canvas.parentElement.removeChild(this.canvas);
2887
+ }
2888
+ if (htmlElement) {
2889
+ htmlElement.appendChild(this.canvas);
2890
+ this.resize();
2891
+ }
2892
+ }
2893
+ resize(displayWidth, displayHeight) {
2894
+ const { width, height } = this.canvas.parentElement?.getBoundingClientRect() ?? this.canvas.getBoundingClientRect();
2895
+ displayWidth = 0 | (displayWidth ?? width);
2896
+ displayHeight = 0 | (displayHeight ?? height);
2897
+ if (this.width !== displayWidth || this.height !== displayHeight) {
2898
+ this.gl.canvas.width = displayWidth;
2899
+ this.gl.canvas.height = displayHeight;
2900
+ this.gl.viewport(0, 0, displayWidth, displayHeight);
2901
+ return true;
2902
+ }
2903
+ return false;
2904
+ }
2905
+ render(_deltaTime) {
2906
+ if (this.programInfo) {
2907
+ }
2908
+ }
2909
+ };
2910
+
2911
+ // src/simplex-noise.ts
2912
+ var SimplexNoise = class {
2913
+ grad3;
2914
+ p;
2915
+ perm;
2916
+ constructor() {
2917
+ this.grad3 = [
2918
+ [1, 1, 0],
2919
+ [-1, 1, 0],
2920
+ [1, -1, 0],
2921
+ [-1, -1, 0],
2922
+ [1, 0, 1],
2923
+ [-1, 0, 1],
2924
+ [1, 0, -1],
2925
+ [-1, 0, -1],
2926
+ [0, 1, 1],
2927
+ [0, -1, 1],
2928
+ [0, 1, -1],
2929
+ [0, -1, -1]
2930
+ ];
2931
+ this.p = [];
2932
+ for (let i = 0; i < 256; i++) {
2933
+ this.p.push(Math.floor(rng.nextf * 256));
2934
+ }
2935
+ this.perm = [];
2936
+ for (let i = 0; i < 512; i++) {
2937
+ this.perm.push(this.p[i & 255]);
2938
+ }
2939
+ }
2940
+ dot(g, x, y, z) {
2941
+ return g[0] * x + g[1] * y + (typeof z === "number" ? g[2] * z : 0);
2942
+ }
2943
+ noise2d(xin, yin) {
2944
+ let n0, n1, n2;
2945
+ const F2 = 0.5 * (Math.sqrt(3) - 1);
2946
+ const s = (xin + yin) * F2;
2947
+ let i = Math.floor(xin + s);
2948
+ let j = Math.floor(yin + s);
2949
+ const G2 = (3 - Math.sqrt(3)) / 6;
2950
+ const t = (i + j) * G2;
2951
+ const X0 = i - t;
2952
+ const Y0 = j - t;
2953
+ const x0 = xin - X0;
2954
+ const y0 = yin - Y0;
2955
+ let i1, j1;
2956
+ if (x0 > y0) {
2957
+ i1 = 1;
2958
+ j1 = 0;
2959
+ } else {
2960
+ i1 = 0;
2961
+ j1 = 1;
2962
+ }
2963
+ const x1 = x0 - i1 + G2;
2964
+ const y1 = y0 - j1 + G2;
2965
+ const x2 = x0 - 1 + 2 * G2;
2966
+ const y2 = y0 - 1 + 2 * G2;
2967
+ const ii = i & 255;
2968
+ const jj = j & 255;
2969
+ const gi0 = this.perm[ii + this.perm[jj]] % 12;
2970
+ const gi1 = this.perm[ii + i1 + this.perm[jj + j1]] % 12;
2971
+ const gi2 = this.perm[ii + 1 + this.perm[jj + 1]] % 12;
2972
+ let t0 = 0.5 - x0 * x0 - y0 * y0;
2973
+ if (t0 < 0) {
2974
+ n0 = 0;
2975
+ } else {
2976
+ t0 *= t0;
2977
+ n0 = t0 * t0 * this.dot(this.grad3[gi0], x0, y0);
2978
+ }
2979
+ let t1 = 0.5 - x1 * x1 - y1 * y1;
2980
+ if (t1 < 0) {
2981
+ n1 = 0;
2982
+ } else {
2983
+ t1 *= t1;
2984
+ n1 = t1 * t1 * this.dot(this.grad3[gi1], x1, y1);
2985
+ }
2986
+ let t2 = 0.5 - x2 * x2 - y2 * y2;
2987
+ if (t2 < 0) {
2988
+ n2 = 0;
2989
+ } else {
2990
+ t2 *= t2;
2991
+ n2 = t2 * t2 * this.dot(this.grad3[gi2], x2, y2);
2992
+ }
2993
+ return 70 * (n0 + n1 + n2);
2994
+ }
2995
+ noise3d(xin, yin, zin) {
2996
+ let n0, n1, n2, n3;
2997
+ const F3 = 1 / 3;
2998
+ const s = (xin + yin + zin) * F3;
2999
+ const i = Math.floor(xin + s);
3000
+ const j = Math.floor(yin + s);
3001
+ const k = Math.floor(zin + s);
3002
+ const G3 = 1 / 6;
3003
+ const t = (i + j + k) * G3;
3004
+ const X0 = i - t;
3005
+ const Y0 = j - t;
3006
+ const Z0 = k - t;
3007
+ const x0 = xin - X0;
3008
+ const y0 = yin - Y0;
3009
+ const z0 = zin - Z0;
3010
+ let i1, j1, k1;
3011
+ let i2, j2, k2;
3012
+ if (x0 >= y0) {
3013
+ if (y0 >= z0) {
3014
+ i1 = 1;
3015
+ j1 = 0;
3016
+ k1 = 0;
3017
+ i2 = 1;
3018
+ j2 = 1;
3019
+ k2 = 0;
3020
+ } else if (x0 >= z0) {
3021
+ i1 = 1;
3022
+ j1 = 0;
3023
+ k1 = 0;
3024
+ i2 = 1;
3025
+ j2 = 0;
3026
+ k2 = 1;
3027
+ } else {
3028
+ i1 = 0;
3029
+ j1 = 0;
3030
+ k1 = 1;
3031
+ i2 = 1;
3032
+ j2 = 0;
3033
+ k2 = 1;
3034
+ }
3035
+ } else {
3036
+ if (y0 < z0) {
3037
+ i1 = 0;
3038
+ j1 = 0;
3039
+ k1 = 1;
3040
+ i2 = 0;
3041
+ j2 = 1;
3042
+ k2 = 1;
3043
+ } else if (x0 < z0) {
3044
+ i1 = 0;
3045
+ j1 = 1;
3046
+ k1 = 0;
3047
+ i2 = 0;
3048
+ j2 = 1;
3049
+ k2 = 1;
3050
+ } else {
3051
+ i1 = 0;
3052
+ j1 = 1;
3053
+ k1 = 0;
3054
+ i2 = 1;
3055
+ j2 = 1;
3056
+ k2 = 0;
3057
+ }
3058
+ }
3059
+ const x1 = x0 - i1 + G3;
3060
+ const y1 = y0 - j1 + G3;
3061
+ const z1 = z0 - k1 + G3;
3062
+ const x2 = x0 - i2 + 2 * G3;
3063
+ const y2 = y0 - j2 + 2 * G3;
3064
+ const z2 = z0 - k2 + 2 * G3;
3065
+ const x3 = x0 - 1 + 3 * G3;
3066
+ const y3 = y0 - 1 + 3 * G3;
3067
+ const z3 = z0 - 1 + 3 * G3;
3068
+ const ii = i & 255;
3069
+ const jj = j & 255;
3070
+ const kk = k & 255;
3071
+ const gi0 = this.perm[ii + this.perm[jj + this.perm[kk]]] % 12;
3072
+ const gi1 = this.perm[ii + i1 + this.perm[jj + j1 + this.perm[kk + k1]]] % 12;
3073
+ const gi2 = this.perm[ii + i2 + this.perm[jj + j2 + this.perm[kk + k2]]] % 12;
3074
+ const gi3 = this.perm[ii + 1 + this.perm[jj + 1 + this.perm[kk + 1]]] % 12;
3075
+ let t0 = 0.6 - x0 * x0 - y0 * y0 - z0 * z0;
3076
+ if (t0 < 0) {
3077
+ n0 = 0;
3078
+ } else {
3079
+ t0 *= t0;
3080
+ n0 = t0 * t0 * this.dot(this.grad3[gi0], x0, y0, z0);
3081
+ }
3082
+ let t1 = 0.6 - x1 * x1 - y1 * y1 - z1 * z1;
3083
+ if (t1 < 0) {
3084
+ n1 = 0;
3085
+ } else {
3086
+ t1 *= t1;
3087
+ n1 = t1 * t1 * this.dot(this.grad3[gi1], x1, y1, z1);
3088
+ }
3089
+ let t2 = 0.6 - x2 * x2 - y2 * y2 - z2 * z2;
3090
+ if (t2 < 0) {
3091
+ n2 = 0;
3092
+ } else {
3093
+ t2 *= t2;
3094
+ n2 = t2 * t2 * this.dot(this.grad3[gi2], x2, y2, z2);
3095
+ }
3096
+ let t3 = 0.6 - x3 * x3 - y3 * y3 - z3 * z3;
3097
+ if (t3 < 0) {
3098
+ n3 = 0;
3099
+ } else {
3100
+ t3 *= t3;
3101
+ n3 = t3 * t3 * this.dot(this.grad3[gi3], x3, y3, z3);
3102
+ }
3103
+ return 32 * (n0 + n1 + n2 + n3);
3104
+ }
3105
+ };
3106
+
3107
+ // src/tilemap.ts
3108
+ var Tilemap = class {
3109
+ map = /* @__PURE__ */ new Map();
3110
+ _key(x, y) {
3111
+ x = floor(x);
3112
+ y = floor(y);
3113
+ return `${x}x${y}`;
3114
+ }
3115
+ get(x, y) {
3116
+ return this.map.get(this._key(x, y));
3117
+ }
3118
+ set(x, y, v) {
3119
+ this.map.set(this._key(x, y), v);
3120
+ }
3121
+ };
3122
+
3492
3123
  // src/worker-interface.ts
3493
3124
  var WorkerInterface = class extends Emitter {
3494
3125
  _worker;
@@ -3514,7 +3145,6 @@ var WorkerInterface = class extends Emitter {
3514
3145
  BuildDefaultPalette,
3515
3146
  CanvasRenderer,
3516
3147
  Clock,
3517
- ClockStats,
3518
3148
  Color,
3519
3149
  Colors,
3520
3150
  CompileShader,
@@ -3527,16 +3157,17 @@ var WorkerInterface = class extends Emitter {
3527
3157
  CreateThreeEntity,
3528
3158
  CreateWebGlContext,
3529
3159
  CubeSphereGeometry,
3160
+ DOUBLE_PI,
3530
3161
  ECS,
3531
3162
  Emitter,
3532
- EventBus,
3533
3163
  GenerateDoors,
3534
3164
  GenerateEntityId,
3535
3165
  GenerateRooms,
3536
3166
  GenerateWalls,
3537
3167
  GetProgramLocations,
3538
3168
  GetThreeComponent,
3539
- GlobalEmitter,
3169
+ GetUrlParams,
3170
+ HALF_PI,
3540
3171
  Input,
3541
3172
  IsThreeEntity,
3542
3173
  LoadTexture,
@@ -3544,14 +3175,15 @@ var WorkerInterface = class extends Emitter {
3544
3175
  Map3D,
3545
3176
  Noise,
3546
3177
  PI,
3178
+ ParticleRenderer,
3547
3179
  ParticleSystem2d,
3548
3180
  ParticleSystem3d,
3549
3181
  PoissonDiskSampler,
3182
+ RADIAN_FACTOR,
3183
+ RandomNumberGenerator,
3550
3184
  Rectangle,
3551
3185
  RemoveThreeComponent,
3552
- Renderer,
3553
3186
  ResizeWebGlContext,
3554
- Rng,
3555
3187
  ShaderType,
3556
3188
  SimplexNoise,
3557
3189
  SpatialPartition2d,
@@ -3574,20 +3206,19 @@ var WorkerInterface = class extends Emitter {
3574
3206
  clamp,
3575
3207
  cos,
3576
3208
  deg2rad,
3577
- distance,
3578
- eventBus,
3209
+ distance2d,
3210
+ distance3d,
3579
3211
  floor,
3580
3212
  getRedYellowGreenGradientHex,
3581
3213
  hypot2,
3582
3214
  hypot3,
3583
3215
  imul,
3584
3216
  isNullOrUndefined,
3217
+ isPositiveString,
3218
+ k2xy,
3219
+ k2xyz,
3585
3220
  lerp,
3586
3221
  log,
3587
- logdev,
3588
- logerr,
3589
- loginf,
3590
- logwrn,
3591
3222
  manhattanDistance,
3592
3223
  max,
3593
3224
  min,
@@ -3597,5 +3228,7 @@ var WorkerInterface = class extends Emitter {
3597
3228
  roundTo,
3598
3229
  sin,
3599
3230
  sqrt,
3600
- squaredDistance
3231
+ squaredDistance,
3232
+ xy2k,
3233
+ xyz2k
3601
3234
  });