@daneren2005/shared-memory-physics 1.4.0 → 1.5.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
@@ -1,9 +1,10 @@
1
- import { Component as e, ComponentSystem as t, DEAD_INDEX as n } from "@daneren2005/shared-memory-ecs";
2
- import { loadFloat32 as r, storeFloat32 as i } from "@daneren2005/shared-memory-objects/utils/float32-atomics";
3
- import a from "flatbush";
4
- import { addAtomicFloat32 as o } from "@daneren2005/shared-memory-objects/utils/atomic-math";
1
+ import { BaseWorld as e, Component as t, ComponentSystem as n, DEAD_INDEX as r } from "@daneren2005/shared-memory-ecs";
2
+ import { loadFloat32 as i, storeFloat32 as a } from "@daneren2005/shared-memory-objects/utils/float32-atomics";
3
+ import o from "@daneren2005/shared-memory-objects/spatial/shared-spatial-map";
4
+ import s from "flatbush";
5
+ import { addAtomicFloat32 as c } from "@daneren2005/shared-memory-objects/utils/atomic-math";
5
6
  //#region src/components/transform-component.ts
6
- var s = 0, c = 1, l = 2, u = 3, d = 4, f = 5, p = class extends e {
7
+ var l = 0, u = 1, d = 2, f = 3, p = 4, m = 5, h = class extends t {
7
8
  get x() {
8
9
  return this.block[0];
9
10
  }
@@ -34,7 +35,7 @@ var s = 0, c = 1, l = 2, u = 3, d = 4, f = 5, p = class extends e {
34
35
  set angle(e) {
35
36
  this.block[4] = e;
36
37
  }
37
- }, m = {
38
+ }, g = {
38
39
  type: Float32Array,
39
40
  size: 5,
40
41
  loadProperties: [
@@ -57,7 +58,7 @@ var s = 0, c = 1, l = 2, u = 3, d = 4, f = 5, p = class extends e {
57
58
  ];
58
59
  },
59
60
  attach(e, t, n) {
60
- return new p(t.getBlock(n), n);
61
+ return new h(t.getBlock(n), n);
61
62
  },
62
63
  save(e) {
63
64
  return {
@@ -66,7 +67,7 @@ var s = 0, c = 1, l = 2, u = 3, d = 4, f = 5, p = class extends e {
66
67
  angle: e.angle
67
68
  };
68
69
  }
69
- }, h = 0, g = 1, _ = 2, v = class extends e {
70
+ }, _ = 0, v = 1, y = 2, b = class extends t {
70
71
  get velocityX() {
71
72
  return this.block[0];
72
73
  }
@@ -79,7 +80,7 @@ var s = 0, c = 1, l = 2, u = 3, d = 4, f = 5, p = class extends e {
79
80
  set velocityY(e) {
80
81
  this.block[1] = e;
81
82
  }
82
- }, y = {
83
+ }, x = {
83
84
  type: Float32Array,
84
85
  size: 2,
85
86
  loadProperties: ["velocityX", "velocityY"],
@@ -87,7 +88,7 @@ var s = 0, c = 1, l = 2, u = 3, d = 4, f = 5, p = class extends e {
87
88
  return [e.velocityX ?? 0, e.velocityY ?? 0];
88
89
  },
89
90
  attach(e, t, n) {
90
- return new v(t.getBlock(n), n);
91
+ return new b(t.getBlock(n), n);
91
92
  },
92
93
  save(e) {
93
94
  return {
@@ -95,25 +96,31 @@ var s = 0, c = 1, l = 2, u = 3, d = 4, f = 5, p = class extends e {
95
96
  velocityY: e.velocityY
96
97
  };
97
98
  }
98
- }, b = 1, x = 2, S = 3, C = [
99
+ }, S = 1, C = 2, w = 3, T = [
99
100
  1,
100
101
  2,
101
102
  3
102
- ], w = 1, T = 4294967295;
103
- function E(e, t, n, r) {
103
+ ], E = 1, D = 4294967295;
104
+ function O(e, t, n, r) {
104
105
  return (t & n) !== 0 && (r & e) !== 0;
105
106
  }
106
- var D = 0, O = 1, k = 2, A = 3, ee = 3, te = 4, ne = 8;
107
- function j(e) {
107
+ var ee = 0, te = 1, ne = 2, re = 3, ie = 3, ae = 4, oe = 8, se = 16;
108
+ function k(e) {
108
109
  return e[0] & 3;
109
110
  }
110
- function M(e) {
111
+ function A(e) {
111
112
  return (e[0] & 4) != 0;
112
113
  }
113
- function re(e) {
114
+ function ce(e) {
114
115
  return (e[0] & 8) != 0;
115
116
  }
116
- var ie = class extends e {
117
+ function j(e) {
118
+ return (e[0] & 16) != 0;
119
+ }
120
+ function le(e) {
121
+ e[0] |= 16;
122
+ }
123
+ var ue = class extends t {
117
124
  get shape() {
118
125
  return this.block[0] & 3;
119
126
  }
@@ -144,7 +151,7 @@ var ie = class extends e {
144
151
  set continuousCollisionDetection(e) {
145
152
  this.block[0] = e ? this.block[0] | 8 : this.block[0] & -9;
146
153
  }
147
- }, ae = {
154
+ }, de = {
148
155
  type: Uint32Array,
149
156
  size: 3,
150
157
  loadProperties: [
@@ -159,35 +166,35 @@ var ie = class extends e {
159
166
  ],
160
167
  toBlock(e) {
161
168
  return [
162
- oe(e),
169
+ fe(e),
163
170
  e.collideCategory ?? 1,
164
171
  e.collideMask ?? 4294967295
165
172
  ];
166
173
  },
167
174
  attach(e, t, n) {
168
- return new ie(t.getBlock(n), n);
175
+ return new ue(t.getBlock(n), n);
169
176
  }
170
177
  };
171
- function oe(e) {
172
- let t = se(e);
178
+ function fe(e) {
179
+ let t = pe(e);
173
180
  return e.sensor && (t |= 4), e.continuousCollisionDetection && (t |= 8), t;
174
181
  }
175
- function se(e) {
182
+ function pe(e) {
176
183
  let t = e.shape;
177
184
  if (t === void 0) return e.radius === void 0 ? 1 : 2;
178
- if (!C.includes(t)) throw Error(`Unknown body shape: ${t}`);
185
+ if (!T.includes(t)) throw Error(`Unknown body shape: ${t}`);
179
186
  return t;
180
187
  }
181
188
  //#endregion
182
189
  //#region src/components/bounciness-component.ts
183
- var ce = 0, le = 1, ue = class extends e {
190
+ var me = 0, he = 1, ge = class extends t {
184
191
  get bounciness() {
185
192
  return this.block[0];
186
193
  }
187
194
  set bounciness(e) {
188
195
  this.block[0] = e;
189
196
  }
190
- }, de = {
197
+ }, _e = {
191
198
  type: Float32Array,
192
199
  size: 1,
193
200
  loadProperties: ["bounciness"],
@@ -195,9 +202,9 @@ var ce = 0, le = 1, ue = class extends e {
195
202
  return [e.bounciness ?? 0];
196
203
  },
197
204
  attach(e, t, n) {
198
- return new ue(t.getBlock(n), n);
205
+ return new ge(t.getBlock(n), n);
199
206
  }
200
- }, fe = 0, pe = 1, me = 2, he = 3, ge = 4, _e = 5, ve = 6, ye = 7, be = 8, xe = class extends e {
207
+ }, ve = 0, ye = 1, be = 2, xe = 3, Se = 4, Ce = 5, we = 6, Te = 7, Ee = 8, De = class extends t {
201
208
  get x() {
202
209
  return this.block[0];
203
210
  }
@@ -241,12 +248,12 @@ var ce = 0, le = 1, ue = class extends e {
241
248
  this.block[6] = e;
242
249
  }
243
250
  get tick() {
244
- return r(this.block, 7);
251
+ return i(this.block, 7);
245
252
  }
246
253
  set tick(e) {
247
- i(this.block, 7, e);
254
+ a(this.block, 7, e);
248
255
  }
249
- }, Se = {
256
+ }, Oe = {
250
257
  type: Float32Array,
251
258
  size: 8,
252
259
  loadProperties: ["interpolate"],
@@ -264,14 +271,14 @@ var ce = 0, le = 1, ue = class extends e {
264
271
  ];
265
272
  },
266
273
  attach(e, t, n) {
267
- return new xe(t.getBlock(n), n);
274
+ return new De(t.getBlock(n), n);
268
275
  }
269
276
  };
270
- function Ce(e) {
277
+ function ke(e) {
271
278
  let { transform: t, interpolation: n } = e.components;
272
279
  !t || !n || (n.prevX = n.x = t.x, n.prevY = n.y = t.y);
273
280
  }
274
- function we(e, t) {
281
+ function Ae(e, t) {
275
282
  let { transform: n, velocity: r, interpolation: i } = e.components;
276
283
  if (!n || !r || !i) return;
277
284
  let a = 1 - (t.stepFraction < 0 ? 0 : t.stepFraction > 1 ? 1 : t.stepFraction), o = t.stepMs / 1e3, s = n.x, c = n.y, l = r.velocityX * o * a, u = r.velocityY * o * a;
@@ -279,144 +286,227 @@ function we(e, t) {
279
286
  }
280
287
  //#endregion
281
288
  //#region src/components/registry.ts
282
- var Te = {
283
- transform: m,
284
- velocity: y,
285
- body: ae,
286
- bounciness: de,
287
- interpolation: Se
288
- }, N = {
289
+ var je = {
290
+ transform: g,
291
+ velocity: x,
292
+ body: de,
293
+ bounciness: _e,
294
+ interpolation: Oe
295
+ }, M = {
289
296
  x: 0,
290
297
  y: 0
291
- }, P = {
298
+ }, N = {
292
299
  x: 0,
293
300
  y: 0
294
301
  };
295
- function F(e, t) {
302
+ function P(e, t) {
296
303
  return Math.max(0, (e - t) / 2);
297
304
  }
298
- function I(e, t, n) {
305
+ function F(e, t, n) {
299
306
  return e === 2 ? t / 2 : e === 3 ? n / 2 : 0;
300
307
  }
301
- function L(e, t, n) {
308
+ function I(e, t, n) {
302
309
  return e === 2 ? t <= 0 : e === 3 ? n <= 0 : t <= 0 || n <= 0;
303
310
  }
304
- function R(e, t, n, r) {
305
- return e === 2 ? t / 2 : e === 3 ? Math.abs(Math.cos(r)) * F(t, n) + n / 2 : Me(t, n, r);
311
+ function L(e, t, n, r) {
312
+ return e === 2 ? t / 2 : e === 3 ? Math.abs(Math.cos(r)) * P(t, n) + n / 2 : ze(t, n, r);
306
313
  }
307
- function z(e, t, n, r) {
308
- return e === 2 ? t / 2 : e === 3 ? Math.abs(Math.sin(r)) * F(t, n) + n / 2 : Ne(t, n, r);
314
+ function R(e, t, n, r) {
315
+ return e === 2 ? t / 2 : e === 3 ? Math.abs(Math.sin(r)) * P(t, n) + n / 2 : Be(t, n, r);
309
316
  }
310
- function B(e, t, n, r, i, a, o, s, c, l, u, d) {
311
- if (L(e, r, i) || L(o, l, u)) return !1;
317
+ function z(e, t, n, r, i, a, o, s, c, l, u, d) {
318
+ if (I(e, r, i) || I(o, l, u)) return !1;
312
319
  let f = e !== 1, p = o !== 1;
313
- if (!f && !p) return Pe(t, n, r, i, a, s, c, l, u, d);
314
- if (f && !p) return B(o, s, c, l, u, d, e, t, n, r, i, a);
315
- let m = V(o, l, u), h = Math.cos(d) * m, g = Math.sin(d) * m, _ = I(o, l, u);
316
- if (!f) return Fe(s - h, c - g, s + h, c + g, t, n, r, i, a) < _ * _;
317
- let v = V(e, r, i), y = Math.cos(a) * v, b = Math.sin(a) * v, x = I(e, r, i) + _;
318
- return W(t - y, n - b, t + y, n + b, s - h, c - g, s + h, c + g) < x * x;
320
+ if (!f && !p) return Ve(t, n, r, i, a, s, c, l, u, d);
321
+ if (f && !p) return z(o, s, c, l, u, d, e, t, n, r, i, a);
322
+ let m = B(o, l, u), h = Math.cos(d) * m, g = Math.sin(d) * m, _ = F(o, l, u);
323
+ if (!f) return Ue(s - h, c - g, s + h, c + g, t, n, r, i, a) < _ * _;
324
+ let v = B(e, r, i), y = Math.cos(a) * v, b = Math.sin(a) * v, x = F(e, r, i) + _;
325
+ return He(t - y, n - b, t + y, n + b, s - h, c - g, s + h, c + g) < x * x;
319
326
  }
320
- function V(e, t, n) {
321
- return e === 3 ? F(t, n) : 0;
327
+ function B(e, t, n) {
328
+ return e === 3 ? P(t, n) : 0;
322
329
  }
323
- function Ee(e, t, n, r, i, a, o, s, c, l, u, d, f) {
330
+ function Me(e, t, n, r, i, a, o, s, c, l, u, d, f) {
324
331
  let p = e !== 1, m = o !== 1;
325
- return !p && !m ? De(t, n, r, i, a, s, c, l, u, d, f) : p && m ? Oe(e, t, n, r, i, a, o, s, c, l, u, d, f) : p ? ke(s, c, l, u, d, e, t, n, r, i, a, f) : ke(t, n, r, i, a, o, s, c, l, u, d, f) ? (f.x = -f.x, f.y = -f.y, !0) : !1;
332
+ return !p && !m ? Ne(t, n, r, i, a, s, c, l, u, d, f) : p && m ? Pe(e, t, n, r, i, a, o, s, c, l, u, d, f) : p ? Fe(s, c, l, u, d, e, t, n, r, i, a, f) : Fe(t, n, r, i, a, o, s, c, l, u, d, f) ? (f.x = -f.x, f.y = -f.y, !0) : !1;
326
333
  }
327
- function De(e, t, n, r, i, a, o, s, c, l, u) {
328
- let d = Math.cos(i), f = Math.sin(i), p = Math.cos(l), m = Math.sin(l), h = n / 2, g = r / 2, _ = s / 2, v = c / 2, y = e - a, b = t - o, x = U(d, f, y, b, h, g, d, f, _, v, p, m);
334
+ function Ne(e, t, n, r, i, a, o, s, c, l, u) {
335
+ let d = Math.cos(i), f = Math.sin(i), p = Math.cos(l), m = Math.sin(l), h = n / 2, g = r / 2, _ = s / 2, v = c / 2, y = e - a, b = t - o, x = V(d, f, y, b, h, g, d, f, _, v, p, m);
329
336
  u.x = d, u.y = f;
330
- let S = U(-f, d, y, b, h, g, d, f, _, v, p, m);
331
- return S < x && (x = S, u.x = -f, u.y = d), S = U(p, m, y, b, h, g, d, f, _, v, p, m), S < x && (x = S, u.x = p, u.y = m), S = U(-m, p, y, b, h, g, d, f, _, v, p, m), S < x && (u.x = -m, u.y = p), Ae(y, b, u), !0;
337
+ let S = V(-f, d, y, b, h, g, d, f, _, v, p, m);
338
+ return S < x && (x = S, u.x = -f, u.y = d), S = V(p, m, y, b, h, g, d, f, _, v, p, m), S < x && (x = S, u.x = p, u.y = m), S = V(-m, p, y, b, h, g, d, f, _, v, p, m), S < x && (u.x = -m, u.y = p), Le(y, b, u), !0;
332
339
  }
333
- function Oe(e, t, n, r, i, a, o, s, c, l, u, d, f) {
334
- let p = V(e, r, i), m = Math.cos(a) * p, h = Math.sin(a) * p, g = V(o, l, u), _ = Math.cos(d) * g, v = Math.sin(d) * g, y = Math.sqrt(W(t - m, n - h, t + m, n + h, s - _, c - v, s + _, c + v, N));
335
- return y > 0 ? (f.x = N.x / y, f.y = N.y / y, !0) : je(t - s, n - c, f);
340
+ function Pe(e, t, n, r, i, a, o, s, c, l, u, d, f) {
341
+ let p = B(e, r, i), m = Math.cos(a) * p, h = Math.sin(a) * p, g = B(o, l, u), _ = Math.cos(d) * g, v = Math.sin(d) * g, y = Math.sqrt(He(t - m, n - h, t + m, n + h, s - _, c - v, s + _, c + v, M));
342
+ return y > 0 ? (f.x = M.x / y, f.y = M.y / y, !0) : Re(t - s, n - c, f);
336
343
  }
337
- function ke(e, t, n, r, i, a, o, s, c, l, u, d) {
338
- let f = V(a, c, l), p = Math.cos(u) * f, m = Math.sin(u) * f, h = Math.sqrt(Fe(o - p, s - m, o + p, s + m, e, t, n, r, i, N));
339
- if (h > 0) return d.x = N.x / h, d.y = N.y / h, !0;
340
- let g = Math.cos(i), _ = Math.sin(i), v = o - e, y = s - t, b = v * g + y * _, x = -v * _ + y * g, S = p * g + m * _, C = -p * _ + m * g, w = I(a, c, l), T = n / 2, E = r / 2, D = H(1, 0, b, x, S, C, w, T, E);
344
+ function Fe(e, t, n, r, i, a, o, s, c, l, u, d) {
345
+ let f = B(a, c, l), p = Math.cos(u) * f, m = Math.sin(u) * f, h = Math.sqrt(Ue(o - p, s - m, o + p, s + m, e, t, n, r, i, M));
346
+ if (h > 0) return d.x = M.x / h, d.y = M.y / h, !0;
347
+ let g = Math.cos(i), _ = Math.sin(i), v = o - e, y = s - t, b = v * g + y * _, x = -v * _ + y * g, S = p * g + m * _, C = -p * _ + m * g, w = F(a, c, l), T = n / 2, E = r / 2, D = Ie(1, 0, b, x, S, C, w, T, E);
341
348
  d.x = 1, d.y = 0;
342
- let O = H(0, 1, b, x, S, C, w, T, E);
343
- O < D && (D = O, d.x = 0, d.y = 1), je(-C, S, P) && (O = H(P.x, P.y, b, x, S, C, w, T, E), O < D && (d.x = P.x, d.y = P.y)), Ae(b, x, d);
344
- let k = d.x, A = d.y;
345
- return d.x = k * g - A * _, d.y = k * _ + A * g, !0;
349
+ let O = Ie(0, 1, b, x, S, C, w, T, E);
350
+ O < D && (D = O, d.x = 0, d.y = 1), Re(-C, S, N) && (O = Ie(N.x, N.y, b, x, S, C, w, T, E), O < D && (d.x = N.x, d.y = N.y)), Le(b, x, d);
351
+ let ee = d.x, te = d.y;
352
+ return d.x = ee * g - te * _, d.y = ee * _ + te * g, !0;
346
353
  }
347
- function H(e, t, n, r, i, a, o, s, c) {
354
+ function Ie(e, t, n, r, i, a, o, s, c) {
348
355
  return s * Math.abs(e) + c * Math.abs(t) + (Math.abs(i * e + a * t) + o) - Math.abs(n * e + r * t);
349
356
  }
350
- function Ae(e, t, n) {
357
+ function Le(e, t, n) {
351
358
  e * n.x + t * n.y < 0 && (n.x = -n.x, n.y = -n.y);
352
359
  }
353
- function je(e, t, n) {
360
+ function Re(e, t, n) {
354
361
  let r = Math.sqrt(e * e + t * t);
355
362
  return r === 0 ? !1 : (n.x = e / r, n.y = t / r, !0);
356
363
  }
357
- function Me(e, t, n) {
364
+ function ze(e, t, n) {
358
365
  return (Math.abs(Math.cos(n)) * e + Math.abs(Math.sin(n)) * t) / 2;
359
366
  }
360
- function Ne(e, t, n) {
367
+ function Be(e, t, n) {
361
368
  return (Math.abs(Math.sin(n)) * e + Math.abs(Math.cos(n)) * t) / 2;
362
369
  }
363
- function Pe(e, t, n, r, i, a, o, s, c, l) {
370
+ function Ve(e, t, n, r, i, a, o, s, c, l) {
364
371
  if (n <= 0 || r <= 0 || s <= 0 || c <= 0) return !1;
365
372
  let u = Math.cos(i), d = Math.sin(i), f = Math.cos(l), p = Math.sin(l), m = n / 2, h = r / 2, g = s / 2, _ = c / 2, v = a - e, y = o - t;
366
- return U(u, d, v, y, m, h, u, d, g, _, f, p) > 0 && U(-d, u, v, y, m, h, u, d, g, _, f, p) > 0 && U(f, p, v, y, m, h, u, d, g, _, f, p) > 0 && U(-p, f, v, y, m, h, u, d, g, _, f, p) > 0;
373
+ return V(u, d, v, y, m, h, u, d, g, _, f, p) > 0 && V(-d, u, v, y, m, h, u, d, g, _, f, p) > 0 && V(f, p, v, y, m, h, u, d, g, _, f, p) > 0 && V(-p, f, v, y, m, h, u, d, g, _, f, p) > 0;
367
374
  }
368
- function U(e, t, n, r, i, a, o, s, c, l, u, d) {
375
+ function V(e, t, n, r, i, a, o, s, c, l, u, d) {
369
376
  return i * Math.abs(o * e + s * t) + a * Math.abs(-s * e + o * t) + (c * Math.abs(u * e + d * t) + l * Math.abs(-d * e + u * t)) - Math.abs(n * e + r * t);
370
377
  }
371
- function W(e, t, n, r, i, a, o, s, c) {
378
+ function He(e, t, n, r, i, a, o, s, c) {
372
379
  let l = n - e, u = r - t, d = o - i, f = s - a, p = e - i, m = t - a, h = l * l + u * u, g = d * d + f * f, _ = d * p + f * m, v = 0, y = 0;
373
380
  if (h <= 0 && g <= 0) return c && (c.x = p, c.y = m), p * p + m * m;
374
- if (h <= 0) y = J(_ / g);
381
+ if (h <= 0) y = G(_ / g);
375
382
  else {
376
383
  let e = l * p + u * m;
377
- if (g <= 0) v = J(-e / h);
384
+ if (g <= 0) v = G(-e / h);
378
385
  else {
379
386
  let t = l * d + u * f, n = h * g - t * t;
380
- n !== 0 && (v = J((t * _ - e * g) / n)), y = (t * v + _) / g, y < 0 ? (y = 0, v = J(-e / h)) : y > 1 && (y = 1, v = J((t - e) / h));
387
+ n !== 0 && (v = G((t * _ - e * g) / n)), y = (t * v + _) / g, y < 0 ? (y = 0, v = G(-e / h)) : y > 1 && (y = 1, v = G((t - e) / h));
381
388
  }
382
389
  }
383
390
  let b = e + l * v - (i + d * y), x = t + u * v - (a + f * y);
384
391
  return c && (c.x = b, c.y = x), b * b + x * x;
385
392
  }
386
- function Fe(e, t, n, r, i, a, o, s, c, l) {
393
+ function Ue(e, t, n, r, i, a, o, s, c, l) {
387
394
  let u = Math.cos(c), d = Math.sin(c), f = o / 2, p = s / 2, m = e - i, h = t - a, g = n - i, _ = r - a, v = m * u + h * d, y = -m * d + h * u, b = g * u + _ * d, x = -g * d + _ * u;
388
- if (!Ie(v, y, b, x, f, p)) return l && (l.x = 0, l.y = 0), 0;
389
- if (!l) return Math.min(q(v, y, f, p), q(b, x, f, p), K(-f, -p, v, y, b, x), K(f, -p, v, y, b, x), K(f, p, v, y, b, x), K(-f, p, v, y, b, x));
390
- let S = q(v, y, f, p, l);
391
- S = G(S, q(b, x, f, p, P), P, l, !1), S = G(S, K(-f, -p, v, y, b, x, P), P, l, !0), S = G(S, K(f, -p, v, y, b, x, P), P, l, !0), S = G(S, K(f, p, v, y, b, x, P), P, l, !0), S = G(S, K(-f, p, v, y, b, x, P), P, l, !0);
395
+ if (!We(v, y, b, x, f, p)) return l && (l.x = 0, l.y = 0), 0;
396
+ if (!l) return Math.min(W(v, y, f, p), W(b, x, f, p), U(-f, -p, v, y, b, x), U(f, -p, v, y, b, x), U(f, p, v, y, b, x), U(-f, p, v, y, b, x));
397
+ let S = W(v, y, f, p, l);
398
+ S = H(S, W(b, x, f, p, N), N, l, !1), S = H(S, U(-f, -p, v, y, b, x, N), N, l, !0), S = H(S, U(f, -p, v, y, b, x, N), N, l, !0), S = H(S, U(f, p, v, y, b, x, N), N, l, !0), S = H(S, U(-f, p, v, y, b, x, N), N, l, !0);
392
399
  let C = l.x, w = l.y;
393
400
  return l.x = C * u - w * d, l.y = C * d + w * u, S;
394
401
  }
395
- function G(e, t, n, r, i) {
402
+ function H(e, t, n, r, i) {
396
403
  return t >= e ? e : (r.x = i ? -n.x : n.x, r.y = i ? -n.y : n.y, t);
397
404
  }
398
- function K(e, t, n, r, i, a, o) {
399
- let s = i - n, c = a - r, l = s * s + c * c, u = l > 0 ? J(((e - n) * s + (t - r) * c) / l) : 0, d = e - (n + s * u), f = t - (r + c * u);
405
+ function U(e, t, n, r, i, a, o) {
406
+ let s = i - n, c = a - r, l = s * s + c * c, u = l > 0 ? G(((e - n) * s + (t - r) * c) / l) : 0, d = e - (n + s * u), f = t - (r + c * u);
400
407
  return o && (o.x = d, o.y = f), d * d + f * f;
401
408
  }
402
- function Ie(e, t, n, r, i, a) {
409
+ function We(e, t, n, r, i, a) {
403
410
  if (Math.max(e, n) < -i || Math.min(e, n) > i || Math.max(t, r) < -a || Math.min(t, r) > a) return !0;
404
411
  let o = -(r - t), s = n - e;
405
412
  return Math.abs(o * e + s * t) > i * Math.abs(o) + a * Math.abs(s);
406
413
  }
407
- function q(e, t, n, r, i) {
414
+ function W(e, t, n, r, i) {
408
415
  let a = Math.abs(e) - n, o = Math.abs(t) - r, s = a > 0 ? a : 0, c = o > 0 ? o : 0;
409
416
  return i && (i.x = e < 0 ? -s : s, i.y = t < 0 ? -c : c), s * s + c * c;
410
417
  }
411
- function J(e) {
418
+ function G(e) {
412
419
  return e < 0 ? 0 : e > 1 ? 1 : e;
413
420
  }
414
421
  //#endregion
415
- //#region src/systems/collision.ts
416
- var Y = 1e-4, Le = 24, Re = "collidable", X = [], ze = 2 ** 26, Be = {
422
+ //#region src/systems/spatial-bounds.ts
423
+ function K(e) {
424
+ let t = e.transform, n = e.body ? k(e.body) : 1, r = t[2], i = t[3], a = t[4], o = L(n, r, i, a), s = R(n, r, i, a), c = t[0], l = t[1];
425
+ return {
426
+ minX: c - o,
427
+ minY: l - s,
428
+ maxX: c + o,
429
+ maxY: l + s
430
+ };
431
+ }
432
+ function Ge(e, t, n, r, i) {
433
+ return e.minX <= r && e.maxX >= t && e.minY <= i && e.maxY >= n;
434
+ }
435
+ function Ke(e, t) {
436
+ return t > e ? t - e : Math.max(1, Math.abs(e)) * 2 ** -52;
437
+ }
438
+ //#endregion
439
+ //#region src/world.ts
440
+ function qe(e, t) {
441
+ t.spatialMapMemory = e.spatialMap.getSharedMemory();
442
+ }
443
+ function Je(e) {
444
+ if (e.spatialMap) return e.spatialMap;
445
+ if (!e.heap || !e.spatialMapMemory) throw Error("Spatial system world is missing its shared MemoryHeap or spatial map");
446
+ return e.spatialMap = new o(e.heap, e.spatialMapMemory), e.spatialMap;
447
+ }
448
+ var Ye = class extends e {
449
+ spatialMap;
450
+ spatialIds = /* @__PURE__ */ new Set();
451
+ constructor(e, t = {}) {
452
+ let { spatial: n, ...r } = t;
453
+ super(e, r), this.spatialMap = new o(this.heap, n), this.on("entity-added", (e) => {
454
+ this.updateSpatialEntity(e), e.on("component-added", (t) => {
455
+ (t === "transform" || t === "body") && this.updateSpatialEntity(e);
456
+ }), e.on("component-removed", (t) => {
457
+ (t === "transform" || t === "body") && this.updateSpatialEntity(e);
458
+ });
459
+ }), this.on("entity-removed", (e) => {
460
+ this.spatialIds.delete(e.eid), this.spatialMap.remove(e.eid);
461
+ });
462
+ }
463
+ updateSpatialEntity(e) {
464
+ let t = typeof e == "number" ? this.getEntityByEid(e) : e;
465
+ if (!t) return;
466
+ let n = t.components, r = n.transform?.block;
467
+ if (!r) {
468
+ this.spatialIds.delete(t.eid), this.spatialMap.remove(t.eid);
469
+ return;
470
+ }
471
+ let i = K({
472
+ transform: r,
473
+ body: n.body?.block
474
+ });
475
+ this.spatialIds.add(t.eid), this.spatialMap.update(t.eid, i.minX, i.minY, i.maxX - i.minX, i.maxY - i.minY);
476
+ }
477
+ searchSpatial(e, t, n, r, i) {
478
+ if (n < e || r < t) return [];
479
+ let a = [], o = Number.isFinite(e) && Number.isFinite(t) && Number.isFinite(n) && Number.isFinite(r) ? this.spatialMap.search(e, t, Ke(e, n), Ke(t, r)) : this.spatialIds;
480
+ for (let s of o) {
481
+ let o = this.getEntityByEid(s);
482
+ if (!o || i && !i(o)) continue;
483
+ let c = o.components, l = c.transform?.block;
484
+ l && Ge(K({
485
+ transform: l,
486
+ body: c.body?.block
487
+ }), e, t, n, r) && a.push(o);
488
+ }
489
+ return a;
490
+ }
491
+ searchSpatialAround(e, t, n, r, i) {
492
+ return this.searchSpatial(e - n, t - r, e + n, t + r, i);
493
+ }
494
+ findNearestSpatial(e, t, n = Infinity, r) {
495
+ return this.findNearbySpatial(e, t, 1, n, r)[0];
496
+ }
497
+ findNearbySpatial(e, t, n, r = Infinity, i) {
498
+ return this.spatialMap.neighbors(e, t, n, r, (e) => {
499
+ let t = this.getEntityByEid(e);
500
+ return !!t && (!i || i(t));
501
+ }).flatMap((e) => {
502
+ let t = this.getEntityByEid(e);
503
+ return t ? [t] : [];
504
+ });
505
+ }
506
+ }, q = 1e-4, Xe = 24, Ze = "collidable", J = [], Qe = 2 ** 26, $e = {
417
507
  fraction: 1,
418
- blocking: X
419
- }, Ve = class {
508
+ blocking: J
509
+ }, et = class {
420
510
  buckets = [];
421
511
  hasBounciness = !1;
422
512
  resolved = /* @__PURE__ */ new Set();
@@ -427,9 +517,9 @@ var Y = 1e-4, Le = 24, Re = "collidable", X = [], ze = 2 ** 26, Be = {
427
517
  if (!i || !a) continue;
428
518
  let o = a[1];
429
519
  if (o === 0) continue;
430
- let s = j(a), c = i[2], l = i[3];
431
- if (L(s, c, l)) continue;
432
- let u = i[4], d = R(s, c, l, u), f = z(s, c, l, u);
520
+ let s = k(a), c = i[2], l = i[3];
521
+ if (I(s, c, l)) continue;
522
+ let u = i[4], d = L(s, c, l, u), f = R(s, c, l, u);
433
523
  e.bounciness && (this.hasBounciness = !0);
434
524
  let p = e.velocity, m = p ? Math.abs(p[0]) * t : 0, h = p ? Math.abs(p[1]) * t : 0, g = i[0], _ = i[1], v = n.get(o);
435
525
  v || (v = {
@@ -441,7 +531,7 @@ var Y = 1e-4, Le = 24, Re = "collidable", X = [], ze = 2 ** 26, Be = {
441
531
  }), v.bounds.push(g - d - m, _ - f - h, g + d + m, _ + f + h);
442
532
  }
443
533
  for (let [e, { entries: t, bounds: r }] of n) {
444
- let n = new a(t.length);
534
+ let n = new s(t.length);
445
535
  for (let e = 0; e < t.length; e++) n.add(r[e * 4], r[e * 4 + 1], r[e * 4 + 2], r[e * 4 + 3]);
446
536
  n.finish(), this.buckets.push({
447
537
  category: e,
@@ -451,43 +541,56 @@ var Y = 1e-4, Le = 24, Re = "collidable", X = [], ze = 2 ** 26, Be = {
451
541
  }
452
542
  }
453
543
  claimContact(e, t) {
454
- let n = e < t ? e * ze + t : t * ze + e;
544
+ let n = e < t ? e * Qe + t : t * Qe + e;
455
545
  return this.resolved.has(n) ? !1 : (this.resolved.add(n), !0);
456
546
  }
457
547
  forEachOverlapping(e, t, n = 0, r = 0) {
458
- let i = Ue(e);
548
+ let i = Y(e);
459
549
  if (!i) return;
460
550
  let { x: a, y: o, halfWidth: s, halfHeight: c } = i;
461
551
  if (i.ccd && (n !== 0 || r !== 0)) {
462
552
  let e = a - n, l = o - r, u = Math.min(e, a) - s, d = Math.min(l, o) - c, f = Math.max(e, a) + s, p = Math.max(l, o) + c;
463
553
  this.forEachCandidate(i, u, d, f, p, (a) => {
464
- We(i, e, l, n, r, a) && t(a);
554
+ nt(i, e, l, n, r, a) && t(a);
465
555
  });
466
556
  return;
467
557
  }
468
558
  this.forEachCandidate(i, a - s, o - c, a + s, o + c, (e) => {
469
- Z(i, a, o, e) && t(e);
559
+ X(i, a, o, e) && t(e);
470
560
  });
471
561
  }
562
+ contactPoint(e, t, n, r) {
563
+ let i = e.components.transform, a = i[0], o = i[1], s = Math.sqrt(n * n + r * r), c = Y(e);
564
+ if (!c || s === 0) return {
565
+ x: a,
566
+ y: o
567
+ };
568
+ c.x = a - n, c.y = o - r;
569
+ let l = this.entryFraction(c, t, n, r, s);
570
+ return {
571
+ x: c.x + n * l,
572
+ y: c.y + r * l
573
+ };
574
+ }
472
575
  sweep(e, t, n) {
473
- let r = Math.sqrt(t * t + n * n), i = Ue(e);
576
+ let r = Math.sqrt(t * t + n * n), i = Y(e);
474
577
  if (!i || r === 0) return {
475
578
  fraction: 1,
476
- blocking: X
579
+ blocking: J
477
580
  };
478
581
  if (i.ccd) return this.sweepContinuous(i, t, n, r);
479
582
  let a = this.gatherCandidates(i, t, n);
480
583
  return a.length === 0 ? {
481
584
  fraction: 1,
482
- blocking: X
585
+ blocking: J
483
586
  } : this.refine(i, a, t, n, r);
484
587
  }
485
588
  resolveMove(e, t, n, r) {
486
- let i = Math.sqrt(t * t + n * n), a = Ue(e);
589
+ let i = Math.sqrt(t * t + n * n), a = Y(e);
487
590
  if (!a || i === 0) return {
488
591
  moveX: t,
489
592
  moveY: n,
490
- blocking: X
593
+ blocking: J
491
594
  };
492
595
  if (a.ccd) {
493
596
  let e = this.sweepContinuous(a, t, n, i);
@@ -501,20 +604,20 @@ var Y = 1e-4, Le = 24, Re = "collidable", X = [], ze = 2 ** 26, Be = {
501
604
  if (o.length === 0) return {
502
605
  moveX: t,
503
606
  moveY: n,
504
- blocking: X
607
+ blocking: J
505
608
  };
506
609
  if (r && t !== 0 && n !== 0) {
507
610
  let e = this.gatherCandidates(a, t, 0), r = this.gatherCandidates(a, 0, n);
508
611
  if (e.length === 0 && r.length === 0) return Math.abs(t) >= Math.abs(n) ? {
509
612
  moveX: t,
510
613
  moveY: 0,
511
- blocking: X
614
+ blocking: J
512
615
  } : {
513
616
  moveX: 0,
514
617
  moveY: n,
515
- blocking: X
618
+ blocking: J
516
619
  };
517
- let i = e.length === 0 ? Be : this.refine(a, e, t, 0, Math.abs(t)), o = r.length === 0 ? Be : this.refine(a, r, 0, n, Math.abs(n)), s = t * i.fraction, c = n * o.fraction;
620
+ let i = e.length === 0 ? $e : this.refine(a, e, t, 0, Math.abs(t)), o = r.length === 0 ? $e : this.refine(a, r, 0, n, Math.abs(n)), s = t * i.fraction, c = n * o.fraction;
518
621
  if (s !== 0 && c !== 0) {
519
622
  let e = this.gatherCandidates(a, s, c);
520
623
  if (e.length > 0) {
@@ -529,7 +632,7 @@ var Y = 1e-4, Le = 24, Re = "collidable", X = [], ze = 2 ** 26, Be = {
529
632
  return {
530
633
  moveX: s,
531
634
  moveY: c,
532
- blocking: He(i.blocking, o.blocking)
635
+ blocking: tt(i.blocking, o.blocking)
533
636
  };
534
637
  }
535
638
  let s = this.refine(a, o, t, n, i);
@@ -544,19 +647,19 @@ var Y = 1e-4, Le = 24, Re = "collidable", X = [], ze = 2 ** 26, Be = {
544
647
  if (e.sensor) return [];
545
648
  let s = r + t, c = i + n, l = [];
546
649
  return this.forEachCandidate(e, s - a, c - o, s + a, c + o, (t) => {
547
- Z(e, s, c, t) && (M(t.components.body) || Z(e, r, i, t) || l.push(t));
650
+ X(e, s, c, t) && (A(t.components.body) || X(e, r, i, t) || l.push(t));
548
651
  }), l;
549
652
  }
550
653
  refine(e, t, n, r, i) {
551
- let { x: a, y: o } = e, s = 0, c = 1, l = Y / i;
552
- for (let i = 0; i < Le && c - s > l; i++) {
654
+ let { x: a, y: o } = e, s = 0, c = 1, l = q / i;
655
+ for (let i = 0; i < Xe && c - s > l; i++) {
553
656
  let i = (s + c) / 2;
554
657
  this.blockedAt(e, t, n, r, i) ? c = i : s = i;
555
658
  }
556
659
  let u = [], d = a + n * c, f = o + r * c;
557
- for (let n of t) Z(e, d, f, n) && u.push(n);
660
+ for (let n of t) X(e, d, f, n) && u.push(n);
558
661
  return {
559
- fraction: s * i > Y ? s : 0,
662
+ fraction: s * i > q ? s : 0,
560
663
  blocking: u
561
664
  };
562
665
  }
@@ -564,22 +667,22 @@ var Y = 1e-4, Le = 24, Re = "collidable", X = [], ze = 2 ** 26, Be = {
564
667
  if (e.sensor) return [];
565
668
  let { x: r, y: i, halfWidth: a, halfHeight: o } = e, s = r + t, c = i + n, l = [];
566
669
  return this.forEachCandidate(e, Math.min(r, s) - a, Math.min(i, c) - o, Math.max(r, s) + a, Math.max(i, c) + o, (a) => {
567
- M(a.components.body) || Z(e, r, i, a) || We(e, r, i, t, n, a) && l.push(a);
670
+ A(a.components.body) || X(e, r, i, a) || nt(e, r, i, t, n, a) && l.push(a);
568
671
  }), l;
569
672
  }
570
673
  sweepContinuous(e, t, n, r) {
571
674
  let i = this.gatherContinuousCandidates(e, t, n);
572
675
  if (i.length === 0) return {
573
676
  fraction: 1,
574
- blocking: X
677
+ blocking: J
575
678
  };
576
- let a = 1, o = [], s = Y / r;
679
+ let a = 1, o = [], s = q / r;
577
680
  for (let c of i) {
578
681
  let i = this.entryFraction(e, c, t, n, r);
579
682
  i < a - s ? (a = i, o = [c]) : i <= a + s && o.push(c);
580
683
  }
581
684
  return {
582
- fraction: a * r > Y ? a : 0,
685
+ fraction: a * r > q ? a : 0,
583
686
  blocking: o
584
687
  };
585
688
  }
@@ -587,46 +690,46 @@ var Y = 1e-4, Le = 24, Re = "collidable", X = [], ze = 2 ** 26, Be = {
587
690
  let a;
588
691
  if (this.blockedAtOne(e, t, n, r, 1)) a = 1;
589
692
  else {
590
- let o = t.components.transform, s = o[0] - e.x, c = o[1] - e.y, l = Ge((s * n + c * r) / (i * i));
693
+ let o = t.components.transform, s = o[0] - e.x, c = o[1] - e.y, l = rt((s * n + c * r) / (i * i));
591
694
  if (!this.blockedAtOne(e, t, n, r, l)) return l;
592
695
  a = l;
593
696
  }
594
- let o = 0, s = Y / i;
595
- for (let i = 0; i < Le && a - o > s; i++) {
697
+ let o = 0, s = q / i;
698
+ for (let i = 0; i < Xe && a - o > s; i++) {
596
699
  let i = (o + a) / 2;
597
700
  this.blockedAtOne(e, t, n, r, i) ? a = i : o = i;
598
701
  }
599
702
  return o;
600
703
  }
601
704
  blockedAtOne(e, t, n, r, i) {
602
- return Z(e, Math.fround(e.x + n * i), Math.fround(e.y + r * i), t);
705
+ return X(e, Math.fround(e.x + n * i), Math.fround(e.y + r * i), t);
603
706
  }
604
707
  blockedAt(e, t, n, r, i) {
605
708
  let a = Math.fround(e.x + n * i), o = Math.fround(e.y + r * i);
606
- for (let n of t) if (Z(e, a, o, n)) return !0;
709
+ for (let n of t) if (X(e, a, o, n)) return !0;
607
710
  return !1;
608
711
  }
609
- forEachCandidate(e, t, r, i, a, o) {
610
- for (let s of this.buckets) if ((e.mask & s.category) !== 0) for (let c of s.index.search(t, r, i, a)) {
712
+ forEachCandidate(e, t, n, i, a, o) {
713
+ for (let s of this.buckets) if ((e.mask & s.category) !== 0) for (let c of s.index.search(t, n, i, a)) {
611
714
  let t = s.entries[c];
612
- t.entityId !== e.entityId && (t.components.body[2] & e.category) !== 0 && t.components.entity?.[n] !== 1 && o(t);
715
+ t.entityId !== e.entityId && (t.components.body[2] & e.category) !== 0 && t.components.entity?.[r] !== 1 && (j(t.components.body) || o(t));
613
716
  }
614
717
  }
615
718
  };
616
- function He(e, t) {
719
+ function tt(e, t) {
617
720
  if (e.length === 0) return t;
618
721
  if (t.length === 0) return e;
619
722
  let n = [...e];
620
723
  for (let e of t) n.some((t) => t.entityId === e.entityId) || n.push(e);
621
724
  return n;
622
725
  }
623
- function Ue(e) {
726
+ function Y(e) {
624
727
  let t = e.components.body;
625
728
  if (!t) return;
626
729
  let n = t[2];
627
730
  if (n === 0) return;
628
- let r = e.components.transform, i = j(t), a = r[2], o = r[3];
629
- if (L(i, a, o)) return;
731
+ let r = e.components.transform, i = k(t), a = r[2], o = r[3];
732
+ if (I(i, a, o)) return;
630
733
  let s = r[4];
631
734
  return {
632
735
  entityId: e.entityId,
@@ -636,33 +739,33 @@ function Ue(e) {
636
739
  width: a,
637
740
  height: o,
638
741
  angle: s,
639
- halfWidth: R(i, a, o, s),
640
- halfHeight: z(i, a, o, s),
742
+ halfWidth: L(i, a, o, s),
743
+ halfHeight: R(i, a, o, s),
641
744
  category: t[1],
642
745
  mask: n,
643
- sensor: M(t),
644
- ccd: re(t)
746
+ sensor: A(t),
747
+ ccd: ce(t)
645
748
  };
646
749
  }
647
- function We(e, t, n, r, i, a) {
648
- let o = t + r, s = n + i, c = a.components.transform, l = j(a.components.body), u = c[0], d = c[1], f = c[2], p = c[3], m = c[4];
750
+ function nt(e, t, n, r, i, a) {
751
+ let o = t + r, s = n + i, c = a.components.transform, l = k(a.components.body), u = c[0], d = c[1], f = c[2], p = c[3], m = c[4];
649
752
  if (e.shape === 1) {
650
753
  let a = Math.cos(e.angle), c = Math.sin(e.angle), h = r * a + i * c, g = -r * c + i * a;
651
- return B(1, (t + o) / 2, (n + s) / 2, e.width + Math.abs(h), e.height + Math.abs(g), e.angle, l, u, d, f, p, m);
754
+ return z(1, (t + o) / 2, (n + s) / 2, e.width + Math.abs(h), e.height + Math.abs(g), e.angle, l, u, d, f, p, m);
652
755
  }
653
- let h = I(e.shape, e.width, e.height), g = Math.sqrt(r * r + i * i), _ = g > 0 ? Math.atan2(i, r) : e.angle;
654
- return B(3, (t + o) / 2, (n + s) / 2, g + h * 2, h * 2, _, l, u, d, f, p, m) ? !0 : B(e.shape, o, s, e.width, e.height, e.angle, l, u, d, f, p, m);
756
+ let h = F(e.shape, e.width, e.height), g = Math.sqrt(r * r + i * i), _ = g > 0 ? Math.atan2(i, r) : e.angle;
757
+ return z(3, (t + o) / 2, (n + s) / 2, g + h * 2, h * 2, _, l, u, d, f, p, m) ? !0 : z(e.shape, o, s, e.width, e.height, e.angle, l, u, d, f, p, m);
655
758
  }
656
- function Ge(e) {
759
+ function rt(e) {
657
760
  return e < 0 ? 0 : e > 1 ? 1 : e;
658
761
  }
659
- function Z(e, t, n, r) {
762
+ function X(e, t, n, r) {
660
763
  let i = r.components.transform;
661
- return B(e.shape, t, n, e.width, e.height, e.angle, j(r.components.body), i[0], i[1], i[2], i[3], i[4]);
764
+ return z(e.shape, t, n, e.width, e.height, e.angle, k(r.components.body), i[0], i[1], i[2], i[3], i[4]);
662
765
  }
663
766
  //#endregion
664
767
  //#region src/systems/spatial-index.ts
665
- var Ke = class {
768
+ var it = class {
666
769
  entries = [];
667
770
  index;
668
771
  constructor(e) {
@@ -670,14 +773,14 @@ var Ke = class {
670
773
  for (let n of e) {
671
774
  let e = n.components, r = e.transform;
672
775
  if (!r) continue;
673
- let i = e.body, a = i ? j(i) : 1, o = r[2], s = r[3], c = r[4], l = r[0], u = r[1], d = R(a, o, s, c), f = z(a, o, s, c);
776
+ let i = e.body, a = i ? k(i) : 1, o = r[2], s = r[3], c = r[4], l = r[0], u = r[1], d = L(a, o, s, c), f = R(a, o, s, c);
674
777
  this.entries.push({
675
778
  entityId: n.entityId,
676
779
  components: e
677
780
  }), t.push(l - d, u - f, l + d, u + f);
678
781
  }
679
782
  if (this.entries.length === 0) return;
680
- let n = new a(this.entries.length);
783
+ let n = new s(this.entries.length);
681
784
  for (let e = 0; e < this.entries.length; e++) n.add(t[e * 4], t[e * 4 + 1], t[e * 4 + 2], t[e * 4 + 3]);
682
785
  n.finish(), this.index = n;
683
786
  }
@@ -702,14 +805,14 @@ var Ke = class {
702
805
  let o = this.entries;
703
806
  return (i ? a.neighbors(e, t, n, r, (e) => i(o[e])) : a.neighbors(e, t, n, r)).map((e) => o[e]);
704
807
  }
705
- }, Q = {
808
+ }, Z = {
706
809
  x: 0,
707
810
  y: 0
708
811
  };
709
- function qe(e, t) {
710
- !Ye(e) && !Ye(t) || Xe(e, t) && Ze(e, t, Q) && (Je(e, Q.x, Q.y), Je(t, -Q.x, -Q.y));
812
+ function at(e, t) {
813
+ !st(e) && !st(t) || ct(e, t) && lt(e, t, Z) && (ot(e, Z.x, Z.y), ot(t, -Z.x, -Z.y));
711
814
  }
712
- function Je(e, t, n) {
815
+ function ot(e, t, n) {
713
816
  let r = e.components.bounciness, i = e.components.velocity;
714
817
  if (!r || !i) return;
715
818
  let a = i[0], o = i[1], s = a * t + o * n;
@@ -717,101 +820,140 @@ function Je(e, t, n) {
717
820
  let c = (1 + r[0]) * s;
718
821
  i[0] = a - c * t, i[1] = o - c * n;
719
822
  }
720
- function Ye(e) {
823
+ function st(e) {
721
824
  return e.components.bounciness !== void 0 && e.components.velocity !== void 0;
722
825
  }
723
- function Xe(e, t) {
724
- return !$e(e.components.body) && !$e(t.components.body);
826
+ function ct(e, t) {
827
+ return !dt(e.components.body) && !dt(t.components.body);
725
828
  }
726
- function Ze(e, t, n) {
829
+ function lt(e, t, n) {
727
830
  let r = e.components.transform, i = t.components.transform;
728
- return Ee(Qe(e.components.body), r[0], r[1], r[2], r[3], r[4], Qe(t.components.body), i[0], i[1], i[2], i[3], i[4], n);
831
+ return Me(ut(e.components.body), r[0], r[1], r[2], r[3], r[4], ut(t.components.body), i[0], i[1], i[2], i[3], i[4], n);
729
832
  }
730
- function Qe(e) {
731
- return e ? j(e) : 1;
833
+ function ut(e) {
834
+ return e ? k(e) : 1;
732
835
  }
733
- function $e(e) {
734
- return e !== void 0 && M(e);
836
+ function dt(e) {
837
+ return e !== void 0 && A(e);
735
838
  }
736
839
  //#endregion
737
840
  //#region src/systems/physics-update.ts
738
- var et = "position-updated";
739
- function tt(e = {}) {
740
- let { onCollision: t, optional: r = [], group: i } = e, a, o, s = Object.assign((e, r, s, c, l) => {
741
- if (s.entity?.[n] === 1) return;
742
- let u = i ? nt(s, i) : 0;
841
+ function Q(e, t, n) {
842
+ if (!e.spatialMap && (!e.heap || !e.spatialMapMemory)) return;
843
+ let r = Je(e), i = K(n);
844
+ r.update(t, i.minX, i.minY, i.maxX - i.minX, i.maxY - i.minY);
845
+ }
846
+ var ft = "position-updated";
847
+ function pt(e = {}) {
848
+ let { onCollision: t, optional: n = [], group: i } = e, a, o, s = Object.assign((e, n, s, c, l) => {
849
+ if (s.entity?.[r] === 1) return;
850
+ if (s.body !== void 0 && j(s.body)) {
851
+ l.entityDied(n);
852
+ return;
853
+ }
854
+ let u = i ? $(s, i) : 0;
743
855
  if (i && e.skipGroup === u) return;
744
856
  let d = {
745
- entityId: r,
857
+ entityId: n,
746
858
  components: s
747
859
  }, f = s.interpolation, p = e.elapsedTime / 1e3, m = s.velocity[0] * p, h = s.velocity[1] * p;
748
- at(f, s.transform, e.elapsedTime);
860
+ vt(f, s.transform, e.elapsedTime);
749
861
  let g = i ? o?.get(u) : a;
750
862
  if (!g) {
751
- $(r, s.transform, m, h, l, e.reportMoves), ot(f, e.tick);
863
+ bt(n, s.transform, m, h, l, e.reportMoves), Q(e, n, s), yt(f, e.tick);
752
864
  return;
753
865
  }
754
866
  let _ = s.bounciness !== void 0, v = g.resolveMove(d, m, h, !_);
755
- if ($(r, s.transform, v.moveX, v.moveY, l, e.reportMoves), ot(f, e.tick), !(!g.hasBounciness && !t)) {
756
- g.forEachOverlapping(d, (n) => {
757
- g.claimContact(r, n.entityId) && rt(e, d, n, c, l, t);
867
+ if (bt(n, s.transform, v.moveX, v.moveY, l, e.reportMoves), Q(e, n, s), yt(f, e.tick), !(!g.hasBounciness && !t)) {
868
+ g.forEachOverlapping(d, (r) => {
869
+ g.claimContact(n, r.entityId) && mt(e, d, r, c, l, t);
758
870
  }, v.moveX, v.moveY);
759
- for (let n of v.blocking) g.claimContact(r, n.entityId) && rt(e, d, n, c, l, t);
871
+ for (let r of v.blocking) g.claimContact(n, r.entityId) && mt(e, d, r, c, l, t);
760
872
  }
761
873
  }, { physics: {
762
874
  collision: !0,
763
- optional: r,
875
+ optional: n,
764
876
  group: i
765
877
  } });
766
878
  return s.preRun = (e, t, n) => {
879
+ e.dieAtImpact = (t, n) => {
880
+ gt(e, i ? o?.get($(t.components, i)) : a, t, n);
881
+ };
767
882
  let r = e.elapsedTime / 1e3, s = n.collidable ?? [];
768
883
  if (!i) {
769
- a = new Ve(s, r);
884
+ a = new et(s, r);
770
885
  return;
771
886
  }
772
887
  let c = /* @__PURE__ */ new Map();
773
888
  for (let t of s) {
774
- let n = nt(t.components, i);
889
+ let n = $(t.components, i);
775
890
  if (e.skipGroup === n) continue;
776
891
  let r = c.get(n);
777
892
  r || (r = [], c.set(n, r)), r.push(t);
778
893
  }
779
894
  o = /* @__PURE__ */ new Map();
780
- for (let [e, t] of c) o.set(e, new Ve(t, r));
895
+ for (let [e, t] of c) o.set(e, new et(t, r));
781
896
  }, s;
782
897
  }
783
- function nt(e, t) {
898
+ function $(e, t) {
784
899
  let n = e[t.component];
785
900
  return n ? n[t.index] : 0;
786
901
  }
787
- function rt(e, t, n, r, i, a) {
788
- qe(t, n), a && a(e, t, n, r, i);
902
+ function mt(e, t, n, r, i, a) {
903
+ if (at(t, n), !a) return;
904
+ let o = K(t.components), s = K(n.components);
905
+ a(e, t, n, r, i), ht(e, t.entityId, t.components, o), ht(e, n.entityId, n.components, s);
906
+ }
907
+ function ht(e, t, n, r) {
908
+ let i = K(n);
909
+ (i.minX !== r.minX || i.minY !== r.minY || i.maxX !== r.maxX || i.maxY !== r.maxY) && Q(e, t, n);
910
+ }
911
+ function gt(e, t, n, r) {
912
+ let i = n.components.body;
913
+ if (!i || j(i)) return;
914
+ le(i);
915
+ let a = n.components.transform, o = n.components.velocity, s = r.components.transform, c = r.components.body;
916
+ if (!t || !a || !o || !s || !c) return;
917
+ let l = e.elapsedTime / 1e3, u = o[0] * l, d = o[1] * l;
918
+ if (u === 0 && d === 0) return;
919
+ let f = t.contactPoint({
920
+ entityId: n.entityId,
921
+ components: {
922
+ transform: a,
923
+ body: i
924
+ }
925
+ }, { components: {
926
+ transform: s,
927
+ body: c
928
+ } }, u, d);
929
+ a[0] = f.x, a[1] = f.y;
789
930
  }
790
- function it(e, t, n, r, i) {
931
+ function _t(e, t, n, r, i) {
791
932
  let a = e.elapsedTime / 1e3, o = n.interpolation;
792
- at(o, n.transform, e.elapsedTime), $(t, n.transform, n.velocity[0] * a, n.velocity[1] * a, i, e.reportMoves), ot(o, e.tick);
933
+ vt(o, n.transform, e.elapsedTime), bt(t, n.transform, n.velocity[0] * a, n.velocity[1] * a, i, e.reportMoves), Q(e, t, n), yt(o, e.tick);
793
934
  }
794
- function at(e, t, n) {
935
+ function vt(e, t, n) {
795
936
  e && (e[2] = t[0], e[3] = t[1], e[6] = n);
796
937
  }
797
- function ot(e, t) {
798
- e && i(e, 7, t);
938
+ function yt(e, t) {
939
+ e && a(e, 7, t);
799
940
  }
800
- function $(e, t, n, r, i, a) {
801
- n === 0 && r === 0 || (n !== 0 && o(t, 0, n, Infinity), r !== 0 && o(t, 1, r, Infinity), a !== !1 && i.emitSystemEvent(et, e));
941
+ function bt(e, t, n, r, i, a) {
942
+ n === 0 && r === 0 || (n !== 0 && c(t, 0, n, Infinity), r !== 0 && c(t, 1, r, Infinity), a !== !1 && i.emitSystemEvent(ft, e));
802
943
  }
803
944
  //#endregion
804
945
  //#region src/systems/physics-system.ts
805
- var st = 50, ct = class extends t {
946
+ var xt = 50, St = class extends n {
947
+ spatialWorld;
806
948
  tick = 0;
807
949
  reportMoves;
808
950
  skipGroup;
809
951
  constructor(e, t = {}) {
810
- let n = t.updateFunction ?? it, r = t.optional ?? n.physics?.optional ?? [], i = t.collision ?? n.physics?.collision ?? !1, a = n.physics?.group, o = [];
952
+ let n = t.updateFunction ?? _t, r = t.optional ?? n.physics?.optional ?? [], i = t.collision ?? n.physics?.collision ?? !1, a = n.physics?.group, o = [];
811
953
  i && !r.includes("body") && o.push("body"), i && !r.includes("bounciness") && o.push("bounciness"), r.includes("interpolation") || o.push("interpolation"), i && o.push("entity"), a && !r.includes(a.component) && o.push(a.component);
812
954
  let s = o.length ? [...o, ...r] : r, c = r.includes("bounciness") ? [...r] : ["bounciness", ...r];
813
955
  a && !c.includes(a.component) && c.push(a.component);
814
- let l = i ? { [Re]: {
956
+ let l = i ? { [Ze]: {
815
957
  required: ["transform", "body"],
816
958
  optional: [
817
959
  "velocity",
@@ -822,7 +964,7 @@ var st = 50, ct = class extends t {
822
964
  } } : void 0, u = l || t.queries ? {
823
965
  ...l,
824
966
  ...t.queries
825
- } : void 0, d = lt(t.scope, t.filter);
967
+ } : void 0, d = Ct(t.scope, t.filter);
826
968
  super(e, {
827
969
  name: t.name ?? "PhysicsSystem",
828
970
  deltaBetweenRuns: t.deltaBetweenRuns ?? 50,
@@ -836,42 +978,42 @@ var st = 50, ct = class extends t {
836
978
  getWorker: t.getWorker ?? (() => {
837
979
  throw Error("PhysicsSystem cannot start a worker: no getWorker was supplied");
838
980
  })
839
- }), this.reportMoves = t.reportMoves, this.skipGroup = t.skipGroup;
981
+ }), this.spatialWorld = e, this.reportMoves = t.reportMoves, this.skipGroup = t.skipGroup;
840
982
  }
841
983
  startInterpolation(e) {
842
984
  let t = this.deltaBetweenRuns;
843
- t <= 0 || we(e, {
985
+ t <= 0 || Ae(e, {
844
986
  stepMs: t,
845
987
  stepFraction: this.currentDelta / t,
846
988
  tick: this.tick
847
989
  });
848
990
  }
849
991
  addDataToWorld(e) {
850
- e.tick = ++this.tick, e.reportMoves = this.reportMoves ?? this.listenerCount("position-updated") > 0, e.skipGroup = this.skipGroup;
992
+ e.tick = ++this.tick, qe(this.spatialWorld, e), e.reportMoves = this.reportMoves ?? this.listenerCount("position-updated") > 0, e.skipGroup = this.skipGroup;
851
993
  }
852
994
  };
853
- function lt(e, t) {
995
+ function Ct(e, t) {
854
996
  return e ? t ? (n) => e(n) && t(n) : e : t;
855
997
  }
856
998
  //#endregion
857
999
  //#region src/systems/interpolation-update.ts
858
- var ut = (e, t, n) => {
859
- let i = n.interpolation, a = n.transform, o = r(i, 7), s = i[2], c = i[3], l = i[6], u = a[0], d = a[1], f = r(i, 7);
1000
+ var wt = (e, t, n) => {
1001
+ let r = n.interpolation, a = n.transform, o = i(r, 7), s = r[2], c = r[3], l = r[6], u = a[0], d = a[1], f = i(r, 7);
860
1002
  if (o !== f) return;
861
1003
  if (l <= 0) {
862
- i[4] = 1, i[5] = f, i[0] = u, i[1] = d;
1004
+ r[4] = 1, r[5] = f, r[0] = u, r[1] = d;
863
1005
  return;
864
1006
  }
865
- let p = i[4];
866
- f !== i[5] && (p = 0, i[5] = f), p += e.elapsedTime / l, p > 1 && (p = 1), i[4] = p, i[0] = s + (u - s) * p, i[1] = c + (d - c) * p;
867
- }, dt = class extends t {
1007
+ let p = r[4];
1008
+ f !== r[5] && (p = 0, r[5] = f), p += e.elapsedTime / l, p > 1 && (p = 1), r[4] = p, r[0] = s + (u - s) * p, r[1] = c + (d - c) * p;
1009
+ }, Tt = class extends n {
868
1010
  constructor(e, t = {}) {
869
1011
  super(e, {
870
1012
  name: t.name ?? "InterpolationSystem",
871
1013
  deltaBetweenRuns: 0,
872
1014
  firstRun: t.firstRun,
873
1015
  required: ["transform", "interpolation"],
874
- updateFunction: ut,
1016
+ updateFunction: wt,
875
1017
  forceMainThread: t.forceMainThread ?? !t.getWorker,
876
1018
  getWorker: t.getWorker ?? (() => {
877
1019
  throw Error("InterpolationSystem cannot start a worker: no getWorker was supplied");
@@ -880,6 +1022,6 @@ var ut = (e, t, n) => {
880
1022
  }
881
1023
  };
882
1024
  //#endregion
883
- export { O as BODY_CATEGORY_INDEX, ne as BODY_CCD_FLAG, D as BODY_FLAGS_INDEX, k as BODY_MASK_INDEX, te as BODY_SENSOR_FLAG, ee as BODY_SHAPE_MASK, A as BODY_SIZE, ce as BOUNCINESS_INDEX, le as BOUNCINESS_SIZE, Re as COLLIDABLE_QUERY, Ve as CollisionBroadphase, w as DEFAULT_COLLIDE_CATEGORY, T as DEFAULT_COLLIDE_MASK, st as DEFAULT_PHYSICS_STEP_MS, ve as INTERPOLATION_DURATION_INDEX, me as INTERPOLATION_PREV_X_INDEX, he as INTERPOLATION_PREV_Y_INDEX, ge as INTERPOLATION_PROGRESS_INDEX, be as INTERPOLATION_SIZE, _e as INTERPOLATION_SYNCED_TICK_INDEX, ye as INTERPOLATION_TICK_INDEX, fe as INTERPOLATION_X_INDEX, pe as INTERPOLATION_Y_INDEX, dt as InterpolationSystem, et as POSITION_UPDATED_EVENT, ct as PhysicsSystem, S as SHAPE_CAPSULE, x as SHAPE_CIRCLE, b as SHAPE_RECTANGLE, Ke as SpatialIndex, d as TRANSFORM_ANGLE_INDEX, u as TRANSFORM_HEIGHT_INDEX, f as TRANSFORM_SIZE, l as TRANSFORM_WIDTH_INDEX, s as TRANSFORM_X_INDEX, c as TRANSFORM_Y_INDEX, _ as VELOCITY_SIZE, h as VELOCITY_X_INDEX, g as VELOCITY_Y_INDEX, ae as bodyDefinition, j as bodyShape, de as bouncinessDefinition, Ne as boundsHalfHeight, Me as boundsHalfWidth, E as canCollide, F as capsuleHalfLength, Ee as contactNormal, tt as createPhysicsUpdate, Se as interpolationDefinition, ut as interpolationUpdate, re as isContinuous, M as isSensor, Pe as orientedBoxesOverlap, Te as physicsRegistry, it as physicsUpdate, K as pointSegmentDistanceSquared, Fe as segmentBoxDistanceSquared, W as segmentSegmentDistanceSquared, z as shapeHalfHeight, R as shapeHalfWidth, L as shapeIsEmpty, I as shapeRadius, B as shapesOverlap, Ce as snapEntity, we as startSpawnInterpolation, m as transformDefinition, y as velocityDefinition };
1025
+ export { te as BODY_CATEGORY_INDEX, oe as BODY_CCD_FLAG, se as BODY_DYING_FLAG, ee as BODY_FLAGS_INDEX, ne as BODY_MASK_INDEX, ae as BODY_SENSOR_FLAG, ie as BODY_SHAPE_MASK, re as BODY_SIZE, me as BOUNCINESS_INDEX, he as BOUNCINESS_SIZE, Ze as COLLIDABLE_QUERY, et as CollisionBroadphase, E as DEFAULT_COLLIDE_CATEGORY, D as DEFAULT_COLLIDE_MASK, xt as DEFAULT_PHYSICS_STEP_MS, we as INTERPOLATION_DURATION_INDEX, be as INTERPOLATION_PREV_X_INDEX, xe as INTERPOLATION_PREV_Y_INDEX, Se as INTERPOLATION_PROGRESS_INDEX, Ee as INTERPOLATION_SIZE, Ce as INTERPOLATION_SYNCED_TICK_INDEX, Te as INTERPOLATION_TICK_INDEX, ve as INTERPOLATION_X_INDEX, ye as INTERPOLATION_Y_INDEX, Tt as InterpolationSystem, ft as POSITION_UPDATED_EVENT, Ye as PhysicalWorld, St as PhysicsSystem, w as SHAPE_CAPSULE, C as SHAPE_CIRCLE, S as SHAPE_RECTANGLE, it as SpatialIndex, p as TRANSFORM_ANGLE_INDEX, f as TRANSFORM_HEIGHT_INDEX, m as TRANSFORM_SIZE, d as TRANSFORM_WIDTH_INDEX, l as TRANSFORM_X_INDEX, u as TRANSFORM_Y_INDEX, y as VELOCITY_SIZE, _ as VELOCITY_X_INDEX, v as VELOCITY_Y_INDEX, qe as addPhysicalWorldData, de as bodyDefinition, k as bodyShape, _e as bouncinessDefinition, Be as boundsHalfHeight, ze as boundsHalfWidth, O as canCollide, P as capsuleHalfLength, Me as contactNormal, pt as createPhysicsUpdate, Je as getSpatialMap, Oe as interpolationDefinition, wt as interpolationUpdate, ce as isContinuous, j as isDying, A as isSensor, le as markDying, Ve as orientedBoxesOverlap, je as physicsRegistry, _t as physicsUpdate, U as pointSegmentDistanceSquared, Ue as segmentBoxDistanceSquared, He as segmentSegmentDistanceSquared, R as shapeHalfHeight, L as shapeHalfWidth, I as shapeIsEmpty, F as shapeRadius, z as shapesOverlap, ke as snapEntity, Ae as startSpawnInterpolation, g as transformDefinition, Q as updateSpatialMap, x as velocityDefinition };
884
1026
 
885
1027
  //# sourceMappingURL=index.js.map