@adatechnology/game-destruction-three 0.26.6 → 0.27.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
@@ -2755,6 +2755,11 @@ var sameFriction = (a, b) => a === b || a !== void 0 && b !== void 0 && a.static
2755
2755
  var sameShape = (a, b) => near(a.center, b.center) && near(a.half, b.half) && near(a.rotation, b.rotation) && sameMesh(a.mesh, b.mesh);
2756
2756
  var sameBox = (a, b) => a === b || sameShape(a, b) && sameFriction(a.friction, b.friction);
2757
2757
  var sameSolids = (a, b) => a.length === b.length && a.every((s, i) => sameBox(s, b[i]));
2758
+ var NONE = /* @__PURE__ */ new Set();
2759
+ var partOfKey = (key) => {
2760
+ const colon = key.lastIndexOf(":");
2761
+ return colon < 0 || !/^\d+$/.test(key.slice(colon + 1)) ? key : key.slice(0, colon);
2762
+ };
2758
2763
  var hasSize = (s) => s.half[0] > 0 && s.half[1] > 0 && s.half[2] > 0;
2759
2764
  var shapeOf = (s) => s.mesh ? { kind: "trimesh", vertices: s.mesh.vertices, indices: s.mesh.indices } : { kind: "box", half: s.half };
2760
2765
  var EngineStatics = class {
@@ -2766,32 +2771,55 @@ var EngineStatics = class {
2766
2771
  __publicField(this, "changed", /* @__PURE__ */ new Set());
2767
2772
  /** Every body it holds now: what a piece made or put is checked against (`EnginePieces.outOfStatics`; the ground has its own rule). */
2768
2773
  __publicField(this, "ids", /* @__PURE__ */ new Set());
2774
+ /** The object each held body is a part of (what a ray that meets the body says about the object). */
2775
+ __publicField(this, "ownerOf", /* @__PURE__ */ new Map());
2776
+ /** The part of the object each held body draws (the key without its mesh number): what a blow on the body hits. */
2777
+ __publicField(this, "partOf", /* @__PURE__ */ new Map());
2778
+ /** Changes whenever an answer of `covers` may have (an object came, went, or was fitted again). */
2779
+ __publicField(this, "coverageRevision", 0);
2780
+ __publicField(this, "unsure", NONE);
2769
2781
  }
2770
2782
  /** Whether the engine has a body for the object's drawn parts (what the kit's sweep need not set a piece back from). */
2771
2783
  has(objectId) {
2772
2784
  const f = this.bodies.get(objectId);
2773
2785
  return f !== void 0 && f.held.size > 0;
2774
2786
  }
2775
- lay(now) {
2787
+ /**
2788
+ * Whether every part a ray can hit has its body: only then is a ray's miss of its bodies a miss of the object. An object whose list is only partly measured, or leaves out a part the
2789
+ * world still tests (a loose or hidden one), is not covered: `lay`'s `unsure`.
2790
+ */
2791
+ covers(objectId) {
2792
+ const f = this.bodies.get(objectId);
2793
+ return f !== void 0 && f.held.size > 0 && f.waiting === 0 && !this.unsure.has(objectId);
2794
+ }
2795
+ /** `now` is the host's list per object; `unsure` names the objects whose list is not all of what a ray can hit (see `covers`). */
2796
+ lay(now, unsure = NONE) {
2776
2797
  this.changed.clear();
2798
+ if (unsure.size !== this.unsure.size || [...unsure].some((id) => !this.unsure.has(id))) {
2799
+ this.unsure = new Set(unsure);
2800
+ this.coverageRevision++;
2801
+ }
2777
2802
  for (const [id, f] of this.bodies) {
2778
2803
  if (now.has(id)) continue;
2779
2804
  for (const h of f.held.values()) this.release(h, f.settled);
2780
2805
  this.bodies.delete(id);
2806
+ this.coverageRevision++;
2781
2807
  }
2782
2808
  const budget = { triangles: TRIANGLES_PER_UPDATE };
2783
2809
  for (const [id, solids] of now) {
2784
2810
  const f = this.bodies.get(id);
2785
2811
  if (f?.solids === solids && f.waiting === 0) continue;
2786
- const fixed = f ?? { solids, held: /* @__PURE__ */ new Map(), waiting: 0, settled: false };
2812
+ const fixed = f ?? { objectId: id, solids, held: /* @__PURE__ */ new Map(), waiting: 0, settled: false };
2787
2813
  this.bodies.set(id, fixed);
2788
2814
  fixed.solids = solids;
2815
+ this.coverageRevision++;
2789
2816
  this.fit(fixed, budget);
2790
2817
  }
2791
2818
  }
2792
2819
  clear() {
2793
2820
  for (const f of this.bodies.values()) for (const h of f.held.values()) this.drop(h.id);
2794
2821
  this.bodies.clear();
2822
+ this.coverageRevision++;
2795
2823
  this.changed.clear();
2796
2824
  }
2797
2825
  /** The object's parts of a list by name, the ones with a size only. */
@@ -2839,6 +2867,8 @@ var EngineStatics = class {
2839
2867
  const id = ids[i];
2840
2868
  f.held.set(x.key, { solid: x.solid, id });
2841
2869
  this.ids.add(id);
2870
+ this.ownerOf.set(id, f.objectId);
2871
+ this.partOf.set(id, partOfKey(x.key));
2842
2872
  this.changed.add(id);
2843
2873
  if (f.settled) this.wakeAround(x.solid);
2844
2874
  });
@@ -2869,6 +2899,8 @@ var EngineStatics = class {
2869
2899
  }
2870
2900
  drop(id) {
2871
2901
  this.ids.delete(id);
2902
+ this.ownerOf.delete(id);
2903
+ this.partOf.delete(id);
2872
2904
  this.world.removeBody(id);
2873
2905
  }
2874
2906
  wakeAround(s) {
@@ -3197,6 +3229,36 @@ function shapeContact(a, b) {
3197
3229
  return null;
3198
3230
  }
3199
3231
 
3232
+ // src/engine-impacts.ts
3233
+ import { createImpactReader } from "@adatechnology/game-physics";
3234
+ var DEFAULT_MIN_IMPULSE = 30;
3235
+ var DEFAULT_HITS_MAX = 32;
3236
+ var DEFAULT_CONTACT_RADIUS = 0.1;
3237
+ var createEngineImpacts = (loop, statics, destruction, options = {}) => {
3238
+ const hitsMax = options.hitsMax ?? DEFAULT_HITS_MAX;
3239
+ const radius = options.contactRadius ?? DEFAULT_CONTACT_RADIUS;
3240
+ return createImpactReader(loop, {
3241
+ minImpulse: options.minImpulse ?? DEFAULT_MIN_IMPULSE,
3242
+ bodiesMax: hitsMax,
3243
+ accepts: (body) => statics.ownerOf.has(body),
3244
+ onImpact: (impact2) => {
3245
+ const object = statics.ownerOf.get(impact2.body);
3246
+ const part = statics.partOf.get(impact2.body);
3247
+ if (object === void 0 || part === void 0 || destruction.part(object, part) === null) return;
3248
+ const hit = {
3249
+ type: "blunt",
3250
+ point: [impact2.point[0], impact2.point[1], impact2.point[2]],
3251
+ direction: [-impact2.normal[0], -impact2.normal[1], -impact2.normal[2]],
3252
+ energy: 0.5 * impact2.impulse * impact2.approach,
3253
+ radius,
3254
+ part,
3255
+ source: "engine"
3256
+ };
3257
+ destruction.hitPart(object, part, hit);
3258
+ }
3259
+ });
3260
+ };
3261
+
3200
3262
  // src/engine-pieces.ts
3201
3263
  import { samePatch } from "@adatechnology/game-terrain";
3202
3264
 
@@ -3366,6 +3428,55 @@ var GroundSchedule = class {
3366
3428
  }
3367
3429
  };
3368
3430
 
3431
+ // src/engine-rays.ts
3432
+ var RAY_MARGIN = 0.25;
3433
+ var LONGEST_RAY = 2e3;
3434
+ var FIXED_ONLY = { layers: ["static"] };
3435
+ var createEngineRayBroadPhase = (world, statics) => {
3436
+ const hits = [];
3437
+ const inside = [];
3438
+ const met = /* @__PURE__ */ new Set();
3439
+ const metList = [];
3440
+ const from = [0, 0, 0];
3441
+ const toward = [0, 0, 0];
3442
+ return {
3443
+ begin(origin, direction, maxDistance) {
3444
+ if (!(maxDistance <= LONGEST_RAY)) return false;
3445
+ met.clear();
3446
+ metList.length = 0;
3447
+ from[0] = origin[0];
3448
+ from[1] = origin[1];
3449
+ from[2] = origin[2];
3450
+ toward[0] = direction[0];
3451
+ toward[1] = direction[1];
3452
+ toward[2] = direction[2];
3453
+ const count = world.sphereCastAll(from, toward, maxDistance, RAY_MARGIN, hits, FIXED_ONLY);
3454
+ for (let i = 0; i < count; i++) {
3455
+ const owner = statics.ownerOf.get(hits[i].body);
3456
+ if (owner !== void 0 && !met.has(owner)) {
3457
+ met.add(owner);
3458
+ metList.push(owner);
3459
+ }
3460
+ }
3461
+ const around = world.overlapSphere(from, RAY_MARGIN, inside, FIXED_ONLY);
3462
+ for (let i = 0; i < around; i++) {
3463
+ const owner = statics.ownerOf.get(inside[i]);
3464
+ if (owner !== void 0 && !met.has(owner)) {
3465
+ met.add(owner);
3466
+ metList.push(owner);
3467
+ }
3468
+ }
3469
+ return true;
3470
+ },
3471
+ mayMeet: (objectId) => !statics.covers(objectId) || met.has(objectId),
3472
+ candidates: {
3473
+ met: () => metList,
3474
+ covers: (objectId) => statics.covers(objectId),
3475
+ revision: () => statics.coverageRevision
3476
+ }
3477
+ };
3478
+ };
3479
+
3369
3480
  // src/merge-flush-boxes.ts
3370
3481
  var FLUSH = 1e-4;
3371
3482
  var sameTurn = (a, b) => {
@@ -3542,6 +3653,10 @@ var EnginePieces = class {
3542
3653
  this.schedule = o.groundWorker ? new GroundSchedule(o.groundWorker, Math.max(1, Math.round(o.groundWorkerFrames ?? GROUND_WORKER_FRAMES))) : null;
3543
3654
  if (this.schedule) this.warmGround(this.schedule);
3544
3655
  }
3656
+ /** The fixed bodies as a broad phase for the destruction world's rays (`engine-rays.ts`). */
3657
+ rayBroadPhase() {
3658
+ return createEngineRayBroadPhase(this.world, this.statics);
3659
+ }
3545
3660
  /** The worker's first job, a sloped tile with steps away from the host's ground (no host call): its code is parsed and hot before the first blast asks for real ones. */
3546
3661
  warmGround(schedule) {
3547
3662
  const warmGround = (x, z) => 0.3 * x - 0.2 * z + 0.15 * Math.floor(x / 0.37) + 0.05 * Math.sin(3 * z);
@@ -4632,7 +4747,7 @@ var EnginePieces = class {
4632
4747
  /** The host's fixed boxes, one static body per object (`EngineStatics`). */
4633
4748
  layStatics() {
4634
4749
  const now = this.o.staticSolids?.();
4635
- if (now) this.statics.lay(now);
4750
+ if (now) this.statics.lay(now, this.o.staticUnsure?.());
4636
4751
  }
4637
4752
  keepApart(apart2) {
4638
4753
  for (const key of this.ignored) {
@@ -5073,6 +5188,7 @@ var PieceSystem = class {
5073
5188
  ...o.blastGroundTiles !== void 0 ? { blastTiles: o.blastGroundTiles } : {},
5074
5189
  gravity: o.gravity,
5075
5190
  staticSolids: o.staticSolids,
5191
+ staticUnsure: o.staticUnsure,
5076
5192
  sweep: o.sweep,
5077
5193
  onSlam: o.onSlam,
5078
5194
  onKnock: o.onKnock,
@@ -5084,6 +5200,14 @@ var PieceSystem = class {
5084
5200
  this.groundWorker = o.groundWorker ?? null;
5085
5201
  if (resolved && !resolved.stepsItself) this.joinLoop(resolved.loop, this.engine);
5086
5202
  }
5203
+ /** Contact blows on the fixed parts become `blunt` hits of `destruction` (`engine-impacts.ts`); null when the pieces are not in the engine's loop. Returns the way to stop it. */
5204
+ contactHits(destruction, options) {
5205
+ return this.engine && this.loop ? createEngineImpacts(this.loop, this.engine.statics, destruction, options) : null;
5206
+ }
5207
+ /** The engine's fixed bodies as the broad phase of the world's rays, or null when the pieces are not in the engine. */
5208
+ rayBroadPhase() {
5209
+ return this.engine?.rayBroadPhase() ?? null;
5210
+ }
5087
5211
  /**
5088
5212
  * The loop mode: the system's work goes into the loop's phases. `prepare` (sync, kinematic targets, the kit's rules that came before the step) runs in the first fixed step of
5089
5213
  * a frame; a held piece's target again in each later one; the read-back and knock sounds after the steps (`contacts`); the budget and the measure after those (`rules`).
@@ -9190,6 +9314,8 @@ var Renderer = class {
9190
9314
  __publicField(this, "staticBoxes", /* @__PURE__ */ new Map());
9191
9315
  /** `staticSolids` per object, kept until `staticNow` measures the object again for a reason. */
9192
9316
  __publicField(this, "solidCache", /* @__PURE__ */ new Map());
9317
+ /** The objects whose last `staticSolids` list is not all of what a ray can hit: measured in part, or with a loose or hidden part left out. */
9318
+ __publicField(this, "unsureSolids", /* @__PURE__ */ new Set());
9193
9319
  /** The objects `staticNow` measured again for a reason (an event, a moved root, boxes that changed): their `staticSolids` are measured again too. */
9194
9320
  __publicField(this, "solidStale", /* @__PURE__ */ new Set());
9195
9321
  /** The world matrix of each root when `staticNow` last measured it. */
@@ -9320,6 +9446,7 @@ var Renderer = class {
9320
9446
  __publicField(this, "time", 0);
9321
9447
  __publicField(this, "scanTimer", 0);
9322
9448
  __publicField(this, "disposed", false);
9449
+ __publicField(this, "stopContactHits", null);
9323
9450
  __publicField(this, "box");
9324
9451
  __publicField(this, "tmpQ");
9325
9452
  __publicField(this, "v1");
@@ -9395,6 +9522,7 @@ var Renderer = class {
9395
9522
  ...o.blastGroundTiles !== void 0 ? { blastGroundTiles: o.blastGroundTiles } : {},
9396
9523
  staticTopAt: (x, z, y, step) => this.staticTopAt(x, z, y, step),
9397
9524
  staticSolids: () => this.staticSolids(),
9525
+ staticUnsure: () => this.unsureSolids,
9398
9526
  gravity: this.gravity,
9399
9527
  scene: o.scene,
9400
9528
  maxAwake: o.maxAwakePieces ?? Infinity,
@@ -9411,6 +9539,8 @@ var Renderer = class {
9411
9539
  this.landingSound(b.objectId, point, clamp(speed / 10, 0.2, 1));
9412
9540
  }
9413
9541
  });
9542
+ if (o.rayBroadPhase) this.world.setRayBroadPhase(this.pieceSys.rayBroadPhase());
9543
+ if (o.contactHits) this.stopContactHits = this.pieceSys.contactHits(this.world, o.contactHits === true ? void 0 : o.contactHits);
9414
9544
  this.unsubscribe = this.world.on("*", (ev) => {
9415
9545
  if (ev.type === "explode") this.coverWhenWentOff.set(ev, this.coverAround(ev));
9416
9546
  if (this.queue.length < QUEUE_CAP || isStructuralEvent(ev)) this.queue.push(ev);
@@ -10274,6 +10404,10 @@ var Renderer = class {
10274
10404
  const budget = { left: VERTICES_PER_UPDATE, incomplete: false, updated: this.measuredMeshes, workers: this.o.workers ?? null };
10275
10405
  try {
10276
10406
  this.measureEach(now, budget, out);
10407
+ this.unsureSolids.clear();
10408
+ for (const id of out.keys()) {
10409
+ if (this.solidStale.has(id) || this.solidCache.get(id)?.leftOut) this.unsureSolids.add(id);
10410
+ }
10277
10411
  } finally {
10278
10412
  this.measuredMeshes.clear();
10279
10413
  }
@@ -10296,14 +10430,26 @@ var Renderer = class {
10296
10430
  budget.incomplete = false;
10297
10431
  const solids = this.measureSolids(id, budget);
10298
10432
  if (budget.incomplete) this.solidStale.add(id);
10433
+ const leftOut = this.hasUnlistedPart(id);
10299
10434
  if (cached && sameSolids(cached.solids, solids)) {
10435
+ cached.leftOut = leftOut;
10300
10436
  out.set(id, cached.solids);
10301
10437
  continue;
10302
10438
  }
10303
- this.solidCache.set(id, { solids });
10439
+ this.solidCache.set(id, { solids, leftOut });
10304
10440
  out.set(id, solids);
10305
10441
  }
10306
10442
  }
10443
+ /** Whether a part the world still lets a ray hit is not in the object's list: a loose or hidden mesh (a lost part is no hit either). */
10444
+ hasUnlistedPart(id) {
10445
+ const b = this.bounds.get(id);
10446
+ if (!b) return false;
10447
+ for (const pv of b.parts.values()) {
10448
+ if (pv.gone !== "none") continue;
10449
+ if (pv.meshes.some((mv) => mv.loose || !this.visuals.shown(mv))) return true;
10450
+ }
10451
+ return false;
10452
+ }
10307
10453
  /**
10308
10454
  * Every fixed thing the object draws as what it collides as (`drawnSolidsOf`): a box where one fits the part, the triangles where not, a part of a merged mesh by its own
10309
10455
  * vertices, a mesh of any width. What is hidden, broken or loose is not drawn, so it is not here.
@@ -12330,6 +12476,8 @@ var Renderer = class {
12330
12476
  this.releaseWarmProbes();
12331
12477
  if (this.disposed) return;
12332
12478
  this.unsubscribe();
12479
+ if (this.o.rayBroadPhase) this.world.setRayBroadPhase(null);
12480
+ this.stopContactHits?.();
12333
12481
  this.clear();
12334
12482
  for (const id of [...this.bounds.keys()]) this.unbind(id);
12335
12483
  this.fragments.dispose();