@daneren2005/shared-memory-physics 1.2.0 → 1.2.2

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,8 +1,9 @@
1
1
  import e from "flatbush";
2
- import { addAtomicFloat32 as t, loadFloat32 as n, storeFloat32 as r } from "@daneren2005/shared-memory-objects";
3
- import { ComponentSystem as i } from "@daneren2005/shared-memory-ecs";
2
+ import { ComponentSystem as t, DEAD_INDEX as n } from "@daneren2005/shared-memory-ecs";
3
+ import { addAtomicFloat32 as r } from "@daneren2005/shared-memory-objects/utils/atomic-math";
4
+ import { loadFloat32 as i, storeFloat32 as a } from "@daneren2005/shared-memory-objects/utils/float32-atomics";
4
5
  //#region src/components/transform-component.ts
5
- var a = 0, o = 1, s = 2, c = 3, l = 4, u = 5, d = {
6
+ var o = 0, s = 1, c = 2, l = 3, u = 4, d = 5, f = {
6
7
  type: Float32Array,
7
8
  size: 5,
8
9
  loadProperties: [
@@ -63,7 +64,7 @@ var a = 0, o = 1, s = 2, c = 3, l = 4, u = 5, d = {
63
64
  y: e.y
64
65
  };
65
66
  }
66
- }, f = 0, p = 1, m = 2, h = {
67
+ }, p = 0, m = 1, h = 2, g = {
67
68
  type: Float32Array,
68
69
  size: 2,
69
70
  loadProperties: ["velocityX", "velocityY"],
@@ -91,19 +92,19 @@ var a = 0, o = 1, s = 2, c = 3, l = 4, u = 5, d = {
91
92
  velocityY: e.velocityY
92
93
  };
93
94
  }
94
- }, g = 1, _ = 2, v = 3, y = [
95
+ }, _ = 1, v = 2, y = 3, b = [
95
96
  1,
96
97
  2,
97
98
  3
98
- ], b = 1, x = 4294967295;
99
- function ee(e, t, n, r) {
99
+ ], x = 1, S = 4294967295;
100
+ function C(e, t, n, r) {
100
101
  return (t & n) !== 0 && (r & e) !== 0;
101
102
  }
102
- var te = 0, ne = 1, re = 2, ie = 3, ae = 4;
103
- function oe(e) {
103
+ var w = 0, T = 1, E = 2, D = 3, O = 4;
104
+ function k(e) {
104
105
  return e[3] !== 0;
105
106
  }
106
- var S = {
107
+ var A = {
107
108
  type: Uint32Array,
108
109
  size: 4,
109
110
  loadProperties: [
@@ -117,7 +118,7 @@ var S = {
117
118
  ],
118
119
  load(e, t, n) {
119
120
  let r = t.create([
120
- se(n),
121
+ ee(n),
121
122
  n.collideCategory ?? 1,
122
123
  n.collideMask ?? 4294967295,
123
124
  +!!n.sensor
@@ -151,15 +152,15 @@ var S = {
151
152
  };
152
153
  }
153
154
  };
154
- function se(e) {
155
+ function ee(e) {
155
156
  let t = e.shape;
156
157
  if (t === void 0) return e.radius === void 0 ? 1 : 2;
157
- if (!y.includes(t)) throw Error(`Unknown body shape: ${t}`);
158
+ if (!b.includes(t)) throw Error(`Unknown body shape: ${t}`);
158
159
  return t;
159
160
  }
160
161
  //#endregion
161
162
  //#region src/components/bounciness-component.ts
162
- var ce = 0, le = 1, C = {
163
+ var te = 0, ne = 1, re = {
163
164
  type: Float32Array,
164
165
  size: 1,
165
166
  loadProperties: ["bounciness"],
@@ -175,7 +176,7 @@ var ce = 0, le = 1, C = {
175
176
  }
176
177
  };
177
178
  }
178
- }, ue = 0, de = 1, fe = 2, pe = 3, me = 4, he = 5, ge = 6, _e = 7, ve = 8, w = {
179
+ }, ie = 0, ae = 1, oe = 2, se = 3, ce = 4, le = 5, ue = 6, de = 7, fe = 8, pe = {
179
180
  type: Float32Array,
180
181
  size: 8,
181
182
  loadProperties: ["interpolate"],
@@ -219,106 +220,153 @@ var ce = 0, le = 1, C = {
219
220
  };
220
221
  }
221
222
  };
222
- function ye(e) {
223
+ function me(e) {
223
224
  let { transform: t, interpolation: n } = e.components;
224
225
  !t || !n || (n.prevX = n.x = t.x, n.prevY = n.y = t.y);
225
226
  }
226
227
  //#endregion
227
228
  //#region src/components/registry.ts
228
- var be = {
229
- transform: d,
230
- velocity: h,
231
- body: S,
232
- bounciness: C,
233
- interpolation: w
229
+ var he = {
230
+ transform: f,
231
+ velocity: g,
232
+ body: A,
233
+ bounciness: re,
234
+ interpolation: pe
235
+ }, j = {
236
+ x: 0,
237
+ y: 0
238
+ }, M = {
239
+ x: 0,
240
+ y: 0
234
241
  };
235
- //#endregion
236
- //#region src/math/shapes.ts
237
- function T(e, t) {
242
+ function N(e, t) {
238
243
  return Math.max(0, (e - t) / 2);
239
244
  }
240
- function E(e, t, n) {
245
+ function P(e, t, n) {
241
246
  return e === 2 ? t / 2 : e === 3 ? n / 2 : 0;
242
247
  }
243
- function D(e, t, n) {
248
+ function F(e, t, n) {
244
249
  return e === 2 ? t <= 0 : e === 3 ? n <= 0 : t <= 0 || n <= 0;
245
250
  }
246
- function O(e, t, n, r) {
247
- return e === 2 ? t / 2 : e === 3 ? Math.abs(Math.cos(r)) * T(t, n) + n / 2 : M(t, n, r);
251
+ function I(e, t, n, r) {
252
+ return e === 2 ? t / 2 : e === 3 ? Math.abs(Math.cos(r)) * N(t, n) + n / 2 : Se(t, n, r);
248
253
  }
249
- function k(e, t, n, r) {
250
- return e === 2 ? t / 2 : e === 3 ? Math.abs(Math.sin(r)) * T(t, n) + n / 2 : N(t, n, r);
254
+ function L(e, t, n, r) {
255
+ return e === 2 ? t / 2 : e === 3 ? Math.abs(Math.sin(r)) * N(t, n) + n / 2 : Ce(t, n, r);
251
256
  }
252
- function A(e, t, n, r, i, a, o, s, c, l, u, d) {
253
- if (D(e, r, i) || D(o, l, u)) return !1;
257
+ function R(e, t, n, r, i, a, o, s, c, l, u, d) {
258
+ if (F(e, r, i) || F(o, l, u)) return !1;
254
259
  let f = e !== 1, p = o !== 1;
255
- if (!f && !p) return P(t, n, r, i, a, s, c, l, u, d);
256
- if (f && !p) return A(o, s, c, l, u, d, e, t, n, r, i, a);
257
- let m = j(o, l, u), h = Math.cos(d) * m, g = Math.sin(d) * m, _ = E(o, l, u);
258
- if (!f) return L(s - h, c - g, s + h, c + g, t, n, r, i, a) < _ * _;
259
- let v = j(e, r, i), y = Math.cos(a) * v, b = Math.sin(a) * v, x = E(e, r, i) + _;
260
- return I(t - y, n - b, t + y, n + b, s - h, c - g, s + h, c + g) < x * x;
261
- }
262
- function j(e, t, n) {
263
- return e === 3 ? T(t, n) : 0;
264
- }
265
- function M(e, t, n) {
260
+ if (!f && !p) return we(t, n, r, i, a, s, c, l, u, d);
261
+ if (f && !p) return R(o, s, c, l, u, d, e, t, n, r, i, a);
262
+ let m = z(o, l, u), h = Math.cos(d) * m, g = Math.sin(d) * m, _ = P(o, l, u);
263
+ if (!f) return U(s - h, c - g, s + h, c + g, t, n, r, i, a) < _ * _;
264
+ let v = z(e, r, i), y = Math.cos(a) * v, b = Math.sin(a) * v, x = P(e, r, i) + _;
265
+ return H(t - y, n - b, t + y, n + b, s - h, c - g, s + h, c + g) < x * x;
266
+ }
267
+ function z(e, t, n) {
268
+ return e === 3 ? N(t, n) : 0;
269
+ }
270
+ function ge(e, t, n, r, i, a, o, s, c, l, u, d, f) {
271
+ let p = e !== 1, m = o !== 1;
272
+ return !p && !m ? _e(t, n, r, i, a, s, c, l, u, d, f) : p && m ? ve(e, t, n, r, i, a, o, s, c, l, u, d, f) : p ? ye(s, c, l, u, d, e, t, n, r, i, a, f) : ye(t, n, r, i, a, o, s, c, l, u, d, f) ? (f.x = -f.x, f.y = -f.y, !0) : !1;
273
+ }
274
+ function _e(e, t, n, r, i, a, o, s, c, l, u) {
275
+ 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);
276
+ u.x = d, u.y = f;
277
+ let S = V(-f, d, y, b, h, g, d, f, _, v, p, m);
278
+ 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), be(y, b, u), !0;
279
+ }
280
+ function ve(e, t, n, r, i, a, o, s, c, l, u, d, f) {
281
+ let p = z(e, r, i), m = Math.cos(a) * p, h = Math.sin(a) * p, g = z(o, l, u), _ = Math.cos(d) * g, v = Math.sin(d) * g, y = Math.sqrt(H(t - m, n - h, t + m, n + h, s - _, c - v, s + _, c + v, j));
282
+ return y > 0 ? (f.x = j.x / y, f.y = j.y / y, !0) : xe(t - s, n - c, f);
283
+ }
284
+ function ye(e, t, n, r, i, a, o, s, c, l, u, d) {
285
+ let f = z(a, c, l), p = Math.cos(u) * f, m = Math.sin(u) * f, h = Math.sqrt(U(o - p, s - m, o + p, s + m, e, t, n, r, i, j));
286
+ if (h > 0) return d.x = j.x / h, d.y = j.y / h, !0;
287
+ 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 = P(a, c, l), T = n / 2, E = r / 2, D = B(1, 0, b, x, S, C, w, T, E);
288
+ d.x = 1, d.y = 0;
289
+ let O = B(0, 1, b, x, S, C, w, T, E);
290
+ O < D && (D = O, d.x = 0, d.y = 1), xe(-C, S, M) && (O = B(M.x, M.y, b, x, S, C, w, T, E), O < D && (d.x = M.x, d.y = M.y)), be(b, x, d);
291
+ let k = d.x, A = d.y;
292
+ return d.x = k * g - A * _, d.y = k * _ + A * g, !0;
293
+ }
294
+ function B(e, t, n, r, i, a, o, s, c) {
295
+ return s * Math.abs(e) + c * Math.abs(t) + (Math.abs(i * e + a * t) + o) - Math.abs(n * e + r * t);
296
+ }
297
+ function be(e, t, n) {
298
+ e * n.x + t * n.y < 0 && (n.x = -n.x, n.y = -n.y);
299
+ }
300
+ function xe(e, t, n) {
301
+ let r = Math.sqrt(e * e + t * t);
302
+ return r === 0 ? !1 : (n.x = e / r, n.y = t / r, !0);
303
+ }
304
+ function Se(e, t, n) {
266
305
  return (Math.abs(Math.cos(n)) * e + Math.abs(Math.sin(n)) * t) / 2;
267
306
  }
268
- function N(e, t, n) {
307
+ function Ce(e, t, n) {
269
308
  return (Math.abs(Math.sin(n)) * e + Math.abs(Math.cos(n)) * t) / 2;
270
309
  }
271
- function P(e, t, n, r, i, a, o, s, c, l) {
310
+ function we(e, t, n, r, i, a, o, s, c, l) {
272
311
  if (n <= 0 || r <= 0 || s <= 0 || c <= 0) return !1;
273
312
  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;
274
- return !F(u, d, v, y, m, h, u, d, g, _, f, p) && !F(-d, u, v, y, m, h, u, d, g, _, f, p) && !F(f, p, v, y, m, h, u, d, g, _, f, p) && !F(-p, f, v, y, m, h, u, d, g, _, f, p);
313
+ 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;
275
314
  }
276
- function F(e, t, n, r, i, a, o, s, c, l, u, d) {
277
- let f = i * Math.abs(o * e + s * t) + a * Math.abs(-s * e + o * t), p = c * Math.abs(u * e + d * t) + l * Math.abs(-d * e + u * t);
278
- return Math.abs(n * e + r * t) >= f + p;
315
+ function V(e, t, n, r, i, a, o, s, c, l, u, d) {
316
+ 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);
279
317
  }
280
- function I(e, t, n, r, i, a, o, s) {
281
- let c = n - e, l = r - t, u = o - i, d = s - a, f = e - i, p = t - a, m = c * c + l * l, h = u * u + d * d, g = u * f + d * p, _ = 0, v = 0;
282
- if (m <= 0 && h <= 0) return f * f + p * p;
283
- if (m <= 0) v = B(g / h);
318
+ function H(e, t, n, r, i, a, o, s, c) {
319
+ 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;
320
+ if (h <= 0 && g <= 0) return c && (c.x = p, c.y = m), p * p + m * m;
321
+ if (h <= 0) y = q(_ / g);
284
322
  else {
285
- let e = c * f + l * p;
286
- if (h <= 0) _ = B(-e / m);
323
+ let e = l * p + u * m;
324
+ if (g <= 0) v = q(-e / h);
287
325
  else {
288
- let t = c * u + l * d, n = m * h - t * t;
289
- n !== 0 && (_ = B((t * g - e * h) / n)), v = (t * _ + g) / h, v < 0 ? (v = 0, _ = B(-e / m)) : v > 1 && (v = 1, _ = B((t - e) / m));
326
+ let t = l * d + u * f, n = h * g - t * t;
327
+ n !== 0 && (v = q((t * _ - e * g) / n)), y = (t * v + _) / g, y < 0 ? (y = 0, v = q(-e / h)) : y > 1 && (y = 1, v = q((t - e) / h));
290
328
  }
291
329
  }
292
- let y = e + c * _ - (i + u * v), b = t + l * _ - (a + d * v);
293
- return y * y + b * b;
294
- }
295
- function L(e, t, n, r, i, a, o, s, c) {
296
- let l = Math.cos(c), u = Math.sin(c), d = o / 2, f = s / 2, p = e - i, m = t - a, h = n - i, g = r - a, _ = p * l + m * u, v = -p * u + m * l, y = h * l + g * u, b = -h * u + g * l;
297
- return xe(_, v, y, b, d, f) ? Math.min(z(_, v, d, f), z(y, b, d, f), R(-d, -f, _, v, y, b), R(d, -f, _, v, y, b), R(d, f, _, v, y, b), R(-d, f, _, v, y, b)) : 0;
298
- }
299
- function R(e, t, n, r, i, a) {
300
- let o = i - n, s = a - r, c = o * o + s * s, l = c > 0 ? B(((e - n) * o + (t - r) * s) / c) : 0, u = e - (n + o * l), d = t - (r + s * l);
301
- return u * u + d * d;
302
- }
303
- function xe(e, t, n, r, i, a) {
330
+ let b = e + l * v - (i + d * y), x = t + u * v - (a + f * y);
331
+ return c && (c.x = b, c.y = x), b * b + x * x;
332
+ }
333
+ function U(e, t, n, r, i, a, o, s, c, l) {
334
+ 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;
335
+ if (!Te(v, y, b, x, f, p)) return l && (l.x = 0, l.y = 0), 0;
336
+ if (!l) return Math.min(K(v, y, f, p), K(b, x, f, p), G(-f, -p, v, y, b, x), G(f, -p, v, y, b, x), G(f, p, v, y, b, x), G(-f, p, v, y, b, x));
337
+ let S = K(v, y, f, p, l);
338
+ S = W(S, K(b, x, f, p, M), M, l, !1), S = W(S, G(-f, -p, v, y, b, x, M), M, l, !0), S = W(S, G(f, -p, v, y, b, x, M), M, l, !0), S = W(S, G(f, p, v, y, b, x, M), M, l, !0), S = W(S, G(-f, p, v, y, b, x, M), M, l, !0);
339
+ let C = l.x, w = l.y;
340
+ return l.x = C * u - w * d, l.y = C * d + w * u, S;
341
+ }
342
+ function W(e, t, n, r, i) {
343
+ return t >= e ? e : (r.x = i ? -n.x : n.x, r.y = i ? -n.y : n.y, t);
344
+ }
345
+ function G(e, t, n, r, i, a, o) {
346
+ let s = i - n, c = a - r, l = s * s + c * c, u = l > 0 ? q(((e - n) * s + (t - r) * c) / l) : 0, d = e - (n + s * u), f = t - (r + c * u);
347
+ return o && (o.x = d, o.y = f), d * d + f * f;
348
+ }
349
+ function Te(e, t, n, r, i, a) {
304
350
  if (Math.max(e, n) < -i || Math.min(e, n) > i || Math.max(t, r) < -a || Math.min(t, r) > a) return !0;
305
351
  let o = -(r - t), s = n - e;
306
352
  return Math.abs(o * e + s * t) > i * Math.abs(o) + a * Math.abs(s);
307
353
  }
308
- function z(e, t, n, r) {
309
- let i = Math.abs(e) - n, a = Math.abs(t) - r, o = i > 0 ? i : 0, s = a > 0 ? a : 0;
310
- return o * o + s * s;
354
+ function K(e, t, n, r, i) {
355
+ let a = Math.abs(e) - n, o = Math.abs(t) - r, s = a > 0 ? a : 0, c = o > 0 ? o : 0;
356
+ return i && (i.x = e < 0 ? -s : s, i.y = t < 0 ? -c : c), s * s + c * c;
311
357
  }
312
- function B(e) {
358
+ function q(e) {
313
359
  return e < 0 ? 0 : e > 1 ? 1 : e;
314
360
  }
315
361
  //#endregion
316
362
  //#region src/systems/collision.ts
317
- var V = 1e-4, Se = 24, H = "collidable", U = [], W = {
363
+ var Ee = 1e-4, De = 24, Oe = "collidable", J = [], ke = 2 ** 26, Ae = {
318
364
  fraction: 1,
319
- blocking: U
320
- }, G = class {
365
+ blocking: J
366
+ }, je = class {
321
367
  buckets = [];
368
+ hasBounciness = !1;
369
+ resolved = /* @__PURE__ */ new Set();
322
370
  constructor(t, n) {
323
371
  let r = /* @__PURE__ */ new Map();
324
372
  for (let e of t) {
@@ -327,8 +375,10 @@ var V = 1e-4, Se = 24, H = "collidable", U = [], W = {
327
375
  let o = a[1];
328
376
  if (o === 0) continue;
329
377
  let s = a[0], c = i[2], l = i[3];
330
- if (D(s, c, l)) continue;
331
- let u = i[4], d = O(s, c, l, u), f = k(s, c, l, u), p = t.velocity, m = p ? Math.abs(p[0]) * n : 0, h = p ? Math.abs(p[1]) * n : 0, g = i[0], _ = i[1], v = r.get(o);
378
+ if (F(s, c, l)) continue;
379
+ let u = i[4], d = I(s, c, l, u), f = L(s, c, l, u);
380
+ t.bounciness && (this.hasBounciness = !0);
381
+ let p = t.velocity, m = p ? Math.abs(p[0]) * n : 0, h = p ? Math.abs(p[1]) * n : 0, g = i[0], _ = i[1], v = r.get(o);
332
382
  v || (v = {
333
383
  entries: [],
334
384
  bounds: []
@@ -347,55 +397,59 @@ var V = 1e-4, Se = 24, H = "collidable", U = [], W = {
347
397
  });
348
398
  }
349
399
  }
400
+ claimContact(e, t) {
401
+ let n = e < t ? e * ke + t : t * ke + e;
402
+ return this.resolved.has(n) ? !1 : (this.resolved.add(n), !0);
403
+ }
350
404
  forEachOverlapping(e, t) {
351
- let n = K(e);
405
+ let n = Y(e);
352
406
  if (!n) return;
353
407
  let { x: r, y: i, halfWidth: a, halfHeight: o } = n;
354
408
  this.forEachCandidate(n, r - a, i - o, r + a, i + o, (e) => {
355
- q(n, r, i, e) && t(e);
409
+ X(n, r, i, e) && t(e);
356
410
  });
357
411
  }
358
412
  sweep(e, t, n) {
359
- let r = Math.sqrt(t * t + n * n), i = K(e);
413
+ let r = Math.sqrt(t * t + n * n), i = Y(e);
360
414
  if (!i || r === 0) return {
361
415
  fraction: 1,
362
- blocking: U
416
+ blocking: J
363
417
  };
364
418
  let a = this.gatherCandidates(i, t, n);
365
419
  return a.length === 0 ? {
366
420
  fraction: 1,
367
- blocking: U
421
+ blocking: J
368
422
  } : this.refine(i, a, t, n, r);
369
423
  }
370
424
  resolveMove(e, t, n, r) {
371
- let i = Math.sqrt(t * t + n * n), a = K(e);
425
+ let i = Math.sqrt(t * t + n * n), a = Y(e);
372
426
  if (!a || i === 0) return {
373
427
  moveX: t,
374
428
  moveY: n,
375
- blocking: U
429
+ blocking: J
376
430
  };
377
431
  let o = this.gatherCandidates(a, t, n);
378
432
  if (o.length === 0) return {
379
433
  moveX: t,
380
434
  moveY: n,
381
- blocking: U
435
+ blocking: J
382
436
  };
383
437
  if (r && t !== 0 && n !== 0) {
384
438
  let e = this.gatherCandidates(a, t, 0), r = this.gatherCandidates(a, 0, n);
385
439
  if (e.length === 0 && r.length === 0) return Math.abs(t) >= Math.abs(n) ? {
386
440
  moveX: t,
387
441
  moveY: 0,
388
- blocking: U
442
+ blocking: J
389
443
  } : {
390
444
  moveX: 0,
391
445
  moveY: n,
392
- blocking: U
446
+ blocking: J
393
447
  };
394
- let i = e.length === 0 ? W : this.refine(a, e, t, 0, Math.abs(t)), o = r.length === 0 ? W : this.refine(a, r, 0, n, Math.abs(n));
448
+ let i = e.length === 0 ? Ae : this.refine(a, e, t, 0, Math.abs(t)), o = r.length === 0 ? Ae : this.refine(a, r, 0, n, Math.abs(n));
395
449
  return {
396
450
  moveX: t * i.fraction,
397
451
  moveY: n * o.fraction,
398
- blocking: Ce(i.blocking, o.blocking)
452
+ blocking: Me(i.blocking, o.blocking)
399
453
  };
400
454
  }
401
455
  let s = this.refine(a, o, t, n, i);
@@ -410,48 +464,48 @@ var V = 1e-4, Se = 24, H = "collidable", U = [], W = {
410
464
  if (e.sensor) return [];
411
465
  let s = r + t, c = i + n, l = [];
412
466
  return this.forEachCandidate(e, s - a, c - o, s + a, c + o, (t) => {
413
- q(e, s, c, t) && t.components.body[3] === 0 && (q(e, r, i, t) || l.push(t));
467
+ X(e, s, c, t) && t.components.body[3] === 0 && (X(e, r, i, t) || l.push(t));
414
468
  }), l;
415
469
  }
416
470
  refine(e, t, n, r, i) {
417
- let { x: a, y: o } = e, s = 0, c = 1, l = V / i;
418
- for (let i = 0; i < Se && c - s > l; i++) {
471
+ let { x: a, y: o } = e, s = 0, c = 1, l = Ee / i;
472
+ for (let i = 0; i < De && c - s > l; i++) {
419
473
  let i = (s + c) / 2;
420
474
  this.blockedAt(e, t, n, r, i) ? c = i : s = i;
421
475
  }
422
476
  let u = [], d = a + n * c, f = o + r * c;
423
- for (let n of t) q(e, d, f, n) && u.push(n);
477
+ for (let n of t) X(e, d, f, n) && u.push(n);
424
478
  return {
425
- fraction: s * i > V ? s : 0,
479
+ fraction: s * i > Ee ? s : 0,
426
480
  blocking: u
427
481
  };
428
482
  }
429
483
  blockedAt(e, t, n, r, i) {
430
484
  let a = e.x + n * i, o = e.y + r * i;
431
- for (let n of t) if (q(e, a, o, n)) return !0;
485
+ for (let n of t) if (X(e, a, o, n)) return !0;
432
486
  return !1;
433
487
  }
434
- forEachCandidate(e, t, n, r, i, a) {
435
- for (let o of this.buckets) if ((e.mask & o.category) !== 0) for (let s of o.index.search(t, n, r, i)) {
436
- let t = o.entries[s];
437
- t.entityId !== e.entityId && (t.components.body[2] & e.category) !== 0 && a(t);
488
+ forEachCandidate(e, t, r, i, a, o) {
489
+ for (let s of this.buckets) if ((e.mask & s.category) !== 0) for (let c of s.index.search(t, r, i, a)) {
490
+ let t = s.entries[c];
491
+ t.entityId !== e.entityId && (t.components.body[2] & e.category) !== 0 && t.components.entity?.[n] !== 1 && o(t);
438
492
  }
439
493
  }
440
494
  };
441
- function Ce(e, t) {
495
+ function Me(e, t) {
442
496
  if (e.length === 0) return t;
443
497
  if (t.length === 0) return e;
444
498
  let n = [...e];
445
499
  for (let e of t) n.some((t) => t.entityId === e.entityId) || n.push(e);
446
500
  return n;
447
501
  }
448
- function K(e) {
502
+ function Y(e) {
449
503
  let t = e.components.body;
450
504
  if (!t) return;
451
505
  let n = t[2];
452
506
  if (n === 0) return;
453
507
  let r = e.components.transform, i = t[0], a = r[2], o = r[3];
454
- if (D(i, a, o)) return;
508
+ if (F(i, a, o)) return;
455
509
  let s = r[4];
456
510
  return {
457
511
  entityId: e.entityId,
@@ -461,20 +515,20 @@ function K(e) {
461
515
  width: a,
462
516
  height: o,
463
517
  angle: s,
464
- halfWidth: O(i, a, o, s),
465
- halfHeight: k(i, a, o, s),
518
+ halfWidth: I(i, a, o, s),
519
+ halfHeight: L(i, a, o, s),
466
520
  category: t[1],
467
521
  mask: n,
468
522
  sensor: t[3] !== 0
469
523
  };
470
524
  }
471
- function q(e, t, n, r) {
525
+ function X(e, t, n, r) {
472
526
  let i = r.components.transform;
473
- return A(e.shape, t, n, e.width, e.height, e.angle, r.components.body[0], i[0], i[1], i[2], i[3], i[4]);
527
+ return R(e.shape, t, n, e.width, e.height, e.angle, r.components.body[0], i[0], i[1], i[2], i[3], i[4]);
474
528
  }
475
529
  //#endregion
476
530
  //#region src/systems/spatial-index.ts
477
- var we = class {
531
+ var Ne = class {
478
532
  entries = [];
479
533
  index;
480
534
  constructor(t) {
@@ -482,7 +536,7 @@ var we = class {
482
536
  for (let e of t) {
483
537
  let t = e.components, r = t.transform;
484
538
  if (!r) continue;
485
- let i = t.body, a = i ? i[0] : 1, o = r[2], s = r[3], c = r[4], l = r[0], u = r[1], d = O(a, o, s, c), f = k(a, o, s, c);
539
+ let i = t.body, a = i ? i[0] : 1, o = r[2], s = r[3], c = r[4], l = r[0], u = r[1], d = I(a, o, s, c), f = L(a, o, s, c);
486
540
  this.entries.push({
487
541
  entityId: e.entityId,
488
542
  components: t
@@ -514,96 +568,101 @@ var we = class {
514
568
  let o = this.entries;
515
569
  return (i ? a.neighbors(e, t, n, r, (e) => i(o[e])) : a.neighbors(e, t, n, r)).map((e) => o[e]);
516
570
  }
517
- }, J = {
518
- x: 0,
519
- y: 0
520
- }, Y = {
521
- x: 0,
522
- y: 0
523
- }, X = {
571
+ }, Z = {
524
572
  x: 0,
525
573
  y: 0
526
574
  };
527
- function Z(e, t) {
528
- let n = e.components.bounciness;
529
- if (!n || De(e.components.body) || De(t.components.body) || !Te(e, t, J)) return;
530
- let r = e.components.velocity, i = r[0], a = r[1], o = i * J.x + a * J.y;
531
- if (o >= 0) return;
532
- let s = (1 + n[0]) * o;
533
- r[0] = i - s * J.x, r[1] = a - s * J.y;
534
- }
535
- function Te(e, t, n) {
536
- let r = e.components.transform, i = t.components.transform, a = r[0] - i[0], o = r[1] - i[1], s = Ee(e.components.body), c = Ee(t.components.body);
537
- if (s === 2 && c === 2) {
538
- let e = Math.sqrt(a * a + o * o);
539
- return e === 0 ? !1 : (n.x = a / e, n.y = o / e, !0);
540
- }
541
- return Oe(r, s, Y), Oe(i, c, X), Y.x + X.x - Math.abs(a) < Y.y + X.y - Math.abs(o) ? (n.x = a < 0 ? -1 : 1, n.y = 0) : (n.x = 0, n.y = o < 0 ? -1 : 1), !0;
575
+ function Pe(e, t) {
576
+ !Ie(e) && !Ie(t) || Le(e, t) && Re(e, t, Z) && (Fe(e, Z.x, Z.y), Fe(t, -Z.x, -Z.y));
577
+ }
578
+ function Fe(e, t, n) {
579
+ let r = e.components.bounciness, i = e.components.velocity;
580
+ if (!r || !i) return;
581
+ let a = i[0], o = i[1], s = a * t + o * n;
582
+ if (s >= 0) return;
583
+ let c = (1 + r[0]) * s;
584
+ i[0] = a - c * t, i[1] = o - c * n;
585
+ }
586
+ function Ie(e) {
587
+ return e.components.bounciness !== void 0 && e.components.velocity !== void 0;
542
588
  }
543
- function Ee(e) {
589
+ function Le(e, t) {
590
+ return !Be(e.components.body) && !Be(t.components.body);
591
+ }
592
+ function Re(e, t, n) {
593
+ let r = e.components.transform, i = t.components.transform;
594
+ return ge(ze(e.components.body), r[0], r[1], r[2], r[3], r[4], ze(t.components.body), i[0], i[1], i[2], i[3], i[4], n);
595
+ }
596
+ function ze(e) {
544
597
  return e ? e[0] : 1;
545
598
  }
546
- function De(e) {
599
+ function Be(e) {
547
600
  return e !== void 0 && e[3] !== 0;
548
601
  }
549
- function Oe(e, t, n) {
550
- let r = e[2], i = e[3], a = e[4];
551
- n.x = O(t, r, i, a), n.y = k(t, r, i, a);
552
- }
553
602
  //#endregion
554
603
  //#region src/systems/physics-update.ts
555
- var ke = "position-updated";
556
- function Ae(e = {}) {
557
- let { onCollision: t, optional: n = [] } = e, r, i = Object.assign((e, n, i, a, o) => {
558
- let s = {
559
- entityId: n,
560
- components: i
561
- }, c = i.interpolation, l = e.elapsedTime / 1e3, u = i.velocity[0] * l, d = i.velocity[1] * l;
562
- if (Me(c, i.transform, e.elapsedTime), !r) {
563
- $(n, i.transform, u, d, o, e.reportMoves), Q(c, e.tick);
604
+ var Ve = "position-updated";
605
+ function He(e = {}) {
606
+ let { onCollision: t, optional: r = [] } = e, i, a = Object.assign((e, r, a, o, s) => {
607
+ if (a.entity?.[n] === 1) return;
608
+ let c = {
609
+ entityId: r,
610
+ components: a
611
+ }, l = a.interpolation, u = e.elapsedTime / 1e3, d = a.velocity[0] * u, f = a.velocity[1] * u;
612
+ Ge(l, a.transform, e.elapsedTime);
613
+ let p = i;
614
+ if (!p) {
615
+ $(r, a.transform, d, f, s, e.reportMoves), Q(l, e.tick);
564
616
  return;
565
617
  }
566
- let f = i.bounciness !== void 0, p = r.resolveMove(s, u, d, !f);
567
- if ($(n, i.transform, p.moveX, p.moveY, o, e.reportMoves), Q(c, e.tick), !(!f && !t)) {
568
- r.forEachOverlapping(s, (n) => {
569
- f && Z(s, n), t && t(e, s, n, a, o);
618
+ let m = a.bounciness !== void 0, h = p.resolveMove(c, d, f, !m);
619
+ if ($(r, a.transform, h.moveX, h.moveY, s, e.reportMoves), Q(l, e.tick), !(!p.hasBounciness && !t)) {
620
+ p.forEachOverlapping(c, (n) => {
621
+ p.claimContact(r, n.entityId) && Ue(e, c, n, o, s, t);
570
622
  });
571
- for (let n of p.blocking) f && Z(s, n), t && t(e, s, n, a, o);
623
+ for (let n of h.blocking) p.claimContact(r, n.entityId) && Ue(e, c, n, o, s, t);
572
624
  }
573
625
  }, { physics: {
574
626
  collision: !0,
575
- optional: n
627
+ optional: r
576
628
  } });
577
- return i.preRun = (e, t, n) => {
578
- r = new G(n.collidable ?? [], e.elapsedTime / 1e3);
579
- }, i;
629
+ return a.preRun = (e, t, n) => {
630
+ i = new je(n.collidable ?? [], e.elapsedTime / 1e3);
631
+ }, a;
580
632
  }
581
- var je = (e, t, n, r, i) => {
633
+ function Ue(e, t, n, r, i, a) {
634
+ Pe(t, n), a && a(e, t, n, r, i);
635
+ }
636
+ function We(e, t, n, r, i) {
582
637
  let a = e.elapsedTime / 1e3, o = n.interpolation;
583
- Me(o, n.transform, e.elapsedTime), $(t, n.transform, n.velocity[0] * a, n.velocity[1] * a, i, e.reportMoves), Q(o, e.tick);
584
- };
585
- function Me(e, t, n) {
638
+ Ge(o, n.transform, e.elapsedTime), $(t, n.transform, n.velocity[0] * a, n.velocity[1] * a, i, e.reportMoves), Q(o, e.tick);
639
+ }
640
+ function Ge(e, t, n) {
586
641
  e && (e[2] = t[0], e[3] = t[1], e[6] = n);
587
642
  }
588
643
  function Q(e, t) {
589
- e && r(e, 7, t);
644
+ e && a(e, 7, t);
590
645
  }
591
- function $(e, n, r, i, a, o) {
592
- r === 0 && i === 0 || (r !== 0 && t(n, 0, r, Infinity), i !== 0 && t(n, 1, i, Infinity), o !== !1 && a.emitSystemEvent(ke, e));
646
+ function $(e, t, n, i, a, o) {
647
+ n === 0 && i === 0 || (n !== 0 && r(t, 0, n, Infinity), i !== 0 && r(t, 1, i, Infinity), o !== !1 && a.emitSystemEvent(Ve, e));
593
648
  }
594
649
  //#endregion
595
650
  //#region src/systems/physics-system.ts
596
- var Ne = 50, Pe = class extends i {
651
+ var Ke = 50, qe = class extends t {
597
652
  tick = 0;
598
653
  reportMoves;
599
654
  constructor(e, t = {}) {
600
- let n = t.updateFunction ?? je, r = t.optional ?? n.physics?.optional ?? [], i = t.collision ?? n.physics?.collision ?? !1, a = [];
601
- i && !r.includes("body") && a.push("body"), i && !r.includes("bounciness") && a.push("bounciness"), r.includes("interpolation") || a.push("interpolation");
602
- let o = a.length ? [...a, ...r] : r, s = i ? { [H]: {
655
+ let n = t.updateFunction ?? We, r = t.optional ?? n.physics?.optional ?? [], i = t.collision ?? n.physics?.collision ?? !1, a = [];
656
+ i && !r.includes("body") && a.push("body"), i && !r.includes("bounciness") && a.push("bounciness"), r.includes("interpolation") || a.push("interpolation"), i && a.push("entity");
657
+ let o = a.length ? [...a, ...r] : r, s = r.includes("bounciness") ? r : ["bounciness", ...r], c = i ? { [Oe]: {
603
658
  required: ["transform", "body"],
604
- optional: ["velocity", ...r]
605
- } } : void 0, c = s || t.queries ? {
606
- ...s,
659
+ optional: [
660
+ "velocity",
661
+ "entity",
662
+ ...s
663
+ ]
664
+ } } : void 0, l = c || t.queries ? {
665
+ ...c,
607
666
  ...t.queries
608
667
  } : void 0;
609
668
  super(e, {
@@ -614,7 +673,7 @@ var Ne = 50, Pe = class extends i {
614
673
  optional: o,
615
674
  filter: t.filter,
616
675
  updateFunction: n,
617
- queries: c,
676
+ queries: l,
618
677
  forceMainThread: t.forceMainThread ?? !t.getWorker,
619
678
  getWorker: t.getWorker ?? (() => {
620
679
  throw Error("PhysicsSystem cannot start a worker: no getWorker was supplied");
@@ -624,23 +683,23 @@ var Ne = 50, Pe = class extends i {
624
683
  addDataToWorld(e) {
625
684
  e.tick = ++this.tick, e.reportMoves = this.reportMoves ?? this.listenerCount("position-updated") > 0;
626
685
  }
627
- }, Fe = (e, t, r) => {
628
- let i = r.interpolation, a = r.transform, o = n(i, 7), s = i[2], c = i[3], l = i[6], u = a[0], d = a[1], f = n(i, 7);
686
+ }, Je = (e, t, n) => {
687
+ 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);
629
688
  if (o !== f) return;
630
689
  if (l <= 0) {
631
- i[4] = 1, i[5] = f, i[0] = u, i[1] = d;
690
+ r[4] = 1, r[5] = f, r[0] = u, r[1] = d;
632
691
  return;
633
692
  }
634
- let p = i[4];
635
- 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;
636
- }, Ie = class extends i {
693
+ let p = r[4];
694
+ 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;
695
+ }, Ye = class extends t {
637
696
  constructor(e, t = {}) {
638
697
  super(e, {
639
698
  name: t.name ?? "InterpolationSystem",
640
699
  deltaBetweenRuns: 0,
641
700
  firstRun: t.firstRun,
642
701
  required: ["transform", "interpolation"],
643
- updateFunction: Fe,
702
+ updateFunction: Je,
644
703
  forceMainThread: t.forceMainThread ?? !t.getWorker,
645
704
  getWorker: t.getWorker ?? (() => {
646
705
  throw Error("InterpolationSystem cannot start a worker: no getWorker was supplied");
@@ -649,6 +708,6 @@ var Ne = 50, Pe = class extends i {
649
708
  }
650
709
  };
651
710
  //#endregion
652
- export { ne as BODY_CATEGORY_INDEX, re as BODY_MASK_INDEX, ie as BODY_SENSOR_INDEX, te as BODY_SHAPE_INDEX, ae as BODY_SIZE, ce as BOUNCINESS_INDEX, le as BOUNCINESS_SIZE, H as COLLIDABLE_QUERY, G as CollisionBroadphase, b as DEFAULT_COLLIDE_CATEGORY, x as DEFAULT_COLLIDE_MASK, Ne as DEFAULT_PHYSICS_STEP_MS, ge as INTERPOLATION_DURATION_INDEX, fe as INTERPOLATION_PREV_X_INDEX, pe as INTERPOLATION_PREV_Y_INDEX, me as INTERPOLATION_PROGRESS_INDEX, ve as INTERPOLATION_SIZE, he as INTERPOLATION_SYNCED_TICK_INDEX, _e as INTERPOLATION_TICK_INDEX, ue as INTERPOLATION_X_INDEX, de as INTERPOLATION_Y_INDEX, Ie as InterpolationSystem, ke as POSITION_UPDATED_EVENT, Pe as PhysicsSystem, v as SHAPE_CAPSULE, _ as SHAPE_CIRCLE, g as SHAPE_RECTANGLE, we as SpatialIndex, l as TRANSFORM_ANGLE_INDEX, c as TRANSFORM_HEIGHT_INDEX, u as TRANSFORM_SIZE, s as TRANSFORM_WIDTH_INDEX, a as TRANSFORM_X_INDEX, o as TRANSFORM_Y_INDEX, m as VELOCITY_SIZE, f as VELOCITY_X_INDEX, p as VELOCITY_Y_INDEX, S as bodyDefinition, C as bouncinessDefinition, N as boundsHalfHeight, M as boundsHalfWidth, ee as canCollide, T as capsuleHalfLength, Ae as createPhysicsUpdate, w as interpolationDefinition, Fe as interpolationUpdate, oe as isSensor, P as orientedBoxesOverlap, be as physicsRegistry, je as physicsUpdate, R as pointSegmentDistanceSquared, L as segmentBoxDistanceSquared, I as segmentSegmentDistanceSquared, k as shapeHalfHeight, O as shapeHalfWidth, D as shapeIsEmpty, E as shapeRadius, A as shapesOverlap, ye as snapEntity, d as transformDefinition, h as velocityDefinition };
711
+ export { T as BODY_CATEGORY_INDEX, E as BODY_MASK_INDEX, D as BODY_SENSOR_INDEX, w as BODY_SHAPE_INDEX, O as BODY_SIZE, te as BOUNCINESS_INDEX, ne as BOUNCINESS_SIZE, Oe as COLLIDABLE_QUERY, je as CollisionBroadphase, x as DEFAULT_COLLIDE_CATEGORY, S as DEFAULT_COLLIDE_MASK, Ke as DEFAULT_PHYSICS_STEP_MS, ue as INTERPOLATION_DURATION_INDEX, oe as INTERPOLATION_PREV_X_INDEX, se as INTERPOLATION_PREV_Y_INDEX, ce as INTERPOLATION_PROGRESS_INDEX, fe as INTERPOLATION_SIZE, le as INTERPOLATION_SYNCED_TICK_INDEX, de as INTERPOLATION_TICK_INDEX, ie as INTERPOLATION_X_INDEX, ae as INTERPOLATION_Y_INDEX, Ye as InterpolationSystem, Ve as POSITION_UPDATED_EVENT, qe as PhysicsSystem, y as SHAPE_CAPSULE, v as SHAPE_CIRCLE, _ as SHAPE_RECTANGLE, Ne as SpatialIndex, u as TRANSFORM_ANGLE_INDEX, l as TRANSFORM_HEIGHT_INDEX, d as TRANSFORM_SIZE, c as TRANSFORM_WIDTH_INDEX, o as TRANSFORM_X_INDEX, s as TRANSFORM_Y_INDEX, h as VELOCITY_SIZE, p as VELOCITY_X_INDEX, m as VELOCITY_Y_INDEX, A as bodyDefinition, re as bouncinessDefinition, Ce as boundsHalfHeight, Se as boundsHalfWidth, C as canCollide, N as capsuleHalfLength, ge as contactNormal, He as createPhysicsUpdate, pe as interpolationDefinition, Je as interpolationUpdate, k as isSensor, we as orientedBoxesOverlap, he as physicsRegistry, We as physicsUpdate, G as pointSegmentDistanceSquared, U as segmentBoxDistanceSquared, H as segmentSegmentDistanceSquared, L as shapeHalfHeight, I as shapeHalfWidth, F as shapeIsEmpty, P as shapeRadius, R as shapesOverlap, me as snapEntity, f as transformDefinition, g as velocityDefinition };
653
712
 
654
713
  //# sourceMappingURL=index.js.map