@adatechnology/game-destruction-three 0.24.2 → 0.24.3

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
@@ -2299,6 +2299,51 @@ function surfaceOf(materialId, def) {
2299
2299
 
2300
2300
  // src/renderer.ts
2301
2301
  import { kitOriginOf } from "@adatechnology/game-contracts";
2302
+ import { debrisColor as debrisColor2, debrisLook, fragmentTone } from "@adatechnology/game-destruction";
2303
+
2304
+ // src/debris-events.ts
2305
+ import { debrisColor, debrisFamily, dustColor } from "@adatechnology/game-destruction";
2306
+ import { DEBRIS_COLOR_VARIABLE, DUST_COLOR_VARIABLE } from "@adatechnology/game-vfx";
2307
+ var HIT_POINT_SECONDS = 1;
2308
+ function familyPlay(def, partColor) {
2309
+ const dust = dustColor(def, partColor);
2310
+ return {
2311
+ material: debrisFamily(def),
2312
+ values: { [DEBRIS_COLOR_VARIABLE]: debrisColor(def, partColor), ...dust !== void 0 ? { [DUST_COLOR_VARIABLE]: dust } : {} }
2313
+ };
2314
+ }
2315
+ function pointOf(ev) {
2316
+ switch (ev.type) {
2317
+ case "hole":
2318
+ case "crack":
2319
+ return ev.worldPoint;
2320
+ case "spall":
2321
+ case "debris":
2322
+ return ev.point;
2323
+ case "chip":
2324
+ return ev.center;
2325
+ default:
2326
+ return void 0;
2327
+ }
2328
+ }
2329
+ var HitPoints = class {
2330
+ constructor() {
2331
+ __publicField(this, "points", /* @__PURE__ */ new Map());
2332
+ }
2333
+ note(ev, time) {
2334
+ const point = pointOf(ev);
2335
+ if (!point || !("part" in ev)) return;
2336
+ this.points.set(`${ev.object}|${ev.part}`, { point, time });
2337
+ }
2338
+ /** The last hit on the part, if it was recent. */
2339
+ of(objectId, partId, time) {
2340
+ const hit = this.points.get(`${objectId}|${partId}`);
2341
+ return hit && time - hit.time <= HIT_POINT_SECONDS ? hit.point : void 0;
2342
+ }
2343
+ forget(objectId) {
2344
+ for (const key of this.points.keys()) if (key.startsWith(`${objectId}|`)) this.points.delete(key);
2345
+ }
2346
+ };
2302
2347
 
2303
2348
  // src/heat.ts
2304
2349
  var STOPS = [
@@ -3578,7 +3623,6 @@ var REBAR_COLOR = 7628380;
3578
3623
  var STEEL_DENSITY = 7850;
3579
3624
  var CHIP_DECAL_EDGE = 0.05;
3580
3625
  var WOOD_RIM = 1.5;
3581
- var IMPACT_TAGS = ["metal", "glass", "wood"];
3582
3626
  var GLOW_FROM_C = 430;
3583
3627
  var MIN_DRAWN_CRACK_WIDTH = 4e-3;
3584
3628
  var SETTLEMENT_CRACK_COLOR = 2827296;
@@ -3732,11 +3776,8 @@ function fractureKey(def, style) {
3732
3776
  return impactKey(def);
3733
3777
  }
3734
3778
  }
3735
- function fallbackColor(id, def) {
3736
- if (def?.tags?.includes("metal")) return 9080726;
3737
- if (def?.visual?.transparent) return 10473696;
3738
- const h = hashString(id);
3739
- return mixHex(7035464, 10130568, h % 100 / 100);
3779
+ function fallbackColor(def) {
3780
+ return debrisColor2(def);
3740
3781
  }
3741
3782
  function materialsOf2(m) {
3742
3783
  return Array.isArray(m) ? m : [m];
@@ -3810,6 +3851,7 @@ var Renderer = class {
3810
3851
  __publicField(this, "ground");
3811
3852
  __publicField(this, "pieceSys");
3812
3853
  __publicField(this, "cages");
3854
+ __publicField(this, "hitPoints", new HitPoints());
3813
3855
  __publicField(this, "active", /* @__PURE__ */ new Map());
3814
3856
  __publicField(this, "fires", /* @__PURE__ */ new Map());
3815
3857
  /** Primed parts burning in the effects world until they go off: their burn loop ends when they are put out. */
@@ -4021,6 +4063,7 @@ var Renderer = class {
4021
4063
  this.restore(objectId);
4022
4064
  this.pieceSys.remove(objectId);
4023
4065
  this.bounds.delete(objectId);
4066
+ this.hitPoints.forget(objectId);
4024
4067
  }
4025
4068
  // ----------------------------------------------------------------------------------------------------------------------------------
4026
4069
  // Pieces
@@ -4477,6 +4520,7 @@ var Renderer = class {
4477
4520
  }
4478
4521
  apply(ev) {
4479
4522
  this.staticDirty.add(ev.object);
4523
+ this.hitPoints.note(ev, this.time);
4480
4524
  if (ev.type === "fracture" || ev.type === "detach" || ev.type === "collapse" || ev.type === "burnout" || ev.type === "destroyed") this.broken.add(ev.object);
4481
4525
  switch (ev.type) {
4482
4526
  case "damage":
@@ -4524,7 +4568,7 @@ var Renderer = class {
4524
4568
  if (pv) this.settlement.finish(pv);
4525
4569
  }
4526
4570
  this.splitLoose(ev.object);
4527
- if (!furniture || ev.mass >= COLLAPSE_CLOUD_KG) this.playEvent({ kind: "collapse" }, this.rootPoint(ev.object));
4571
+ if (!furniture || ev.mass >= COLLAPSE_CLOUD_KG) this.playEvent({ kind: "collapse" }, this.rootPoint(ev.object), this.partPlay(ev.object, ev.parts[0] ?? ""));
4528
4572
  else this.vfx.burst("dust", this.rootPoint(ev.object), [0, 1, 0], clamp(Math.round(ev.mass / 4), 4, 24), 0.8);
4529
4573
  break;
4530
4574
  }
@@ -4565,7 +4609,7 @@ var Renderer = class {
4565
4609
  else this.dropCage(ev.object, ev.part);
4566
4610
  if (ev.bars.length > 0) {
4567
4611
  const [a, b] = ev.bars[0];
4568
- this.playEvent({ kind: "expose" }, [(a[0] + b[0]) / 2, (a[1] + b[1]) / 2, (a[2] + b[2]) / 2]);
4612
+ this.playEvent({ kind: "expose" }, [(a[0] + b[0]) / 2, (a[1] + b[1]) / 2, (a[2] + b[2]) / 2], this.partPlay(ev.object, ev.part));
4569
4613
  }
4570
4614
  break;
4571
4615
  case "extinguish": {
@@ -4605,7 +4649,7 @@ var Renderer = class {
4605
4649
  const b = this.bounds.get(ev.object);
4606
4650
  const pv = b?.parts.get(ev.part);
4607
4651
  const def = this.world.materials.get(ev.material);
4608
- const color = (pv && this.partColor(pv, def)) ?? fallbackColor(ev.material, def);
4652
+ const color = (pv && this.partColor(pv, def)) ?? fallbackColor(def);
4609
4653
  let tag = this.homeTags.indexOf(ev.object) + 1;
4610
4654
  if (tag === 0) tag = this.homeTags.push(ev.object);
4611
4655
  this.vfx.spawn("drips", ev.point, [(this.rng() - 0.5) * 0.1, -0.1, (this.rng() - 0.5) * 0.1], { color, tag, owner: this.owner });
@@ -4692,7 +4736,7 @@ var Renderer = class {
4692
4736
  const tint = this.visuals.vertexTint(mv);
4693
4737
  return tint ? c.clone().multiply(tint).getHex() : c.getHex();
4694
4738
  }
4695
- return def ? fallbackColor(def.id, def) : null;
4739
+ return def ? fallbackColor(def) : null;
4696
4740
  }
4697
4741
  debrisOptions(kind, objectId, partId, material) {
4698
4742
  const pv = this.partVis(objectId, partId);
@@ -4747,22 +4791,23 @@ var Renderer = class {
4747
4791
  if (!b || !pv) return;
4748
4792
  const def = this.world.materials.get(pv.material);
4749
4793
  const n = this.worldDir(b, ev.hole.normal);
4794
+ const look = debrisLook(def, "shot");
4750
4795
  const draw = (point, normal) => {
4751
4796
  const wp = new this.THREE.Vector3(point[0], point[1], point[2]);
4752
4797
  const mv = this.nearestMesh(pv, wp);
4753
4798
  if (!mv) return;
4754
- if (def?.visual?.transparent) {
4799
+ if (look.mark === "star_crack") {
4755
4800
  const seed = hashNumbers(ev.time, point[0], point[1], point[2]);
4756
4801
  this.visuals.addCrack(mv, wp, normal, 7, Math.max(0.05, ev.hole.diameter * 5), 15267071, seed);
4757
4802
  } else {
4758
- const fresh = def?.penetrationClass === "wood" ? def.visual?.innerColor : void 0;
4803
+ const fresh = look.mark === "splintered_hole" ? def?.visual?.innerColor : void 0;
4759
4804
  if (fresh !== void 0) this.visuals.addSpall(mv, wp, normal, Math.max(6e-3, ev.hole.diameter * WOOD_RIM), fresh);
4760
4805
  this.visuals.addHole(mv, wp, normal, ev.hole.diameter);
4761
4806
  }
4762
4807
  };
4763
4808
  draw(ev.worldPoint, n);
4764
4809
  if (ev.hole.through && ev.exitPoint) draw(ev.exitPoint, n.clone().negate());
4765
- if (def?.tags?.includes("metal")) this.vfx.burst("sparks", ev.worldPoint, [n.x, n.y, n.z], 6, 3.5);
4810
+ if (look.sparks) this.vfx.burst("sparks", ev.worldPoint, [n.x, n.y, n.z], 6, 3.5);
4766
4811
  if (ev.powder) {
4767
4812
  const puff = { color: ev.powder.color, size: 0.6, life: 1.6, alpha: 1.3 };
4768
4813
  this.vfx.burst("dust", ev.worldPoint, [n.x, n.y, n.z], this.low ? 5 : 10, 0.7, puff);
@@ -4804,19 +4849,21 @@ var Renderer = class {
4804
4849
  const side = point[axis] >= (box.min[axis] + box.max[axis]) / 2 ? 1 : -1;
4805
4850
  return new this.THREE.Vector3(axis === "x" ? side : 0, axis === "y" ? side : 0, axis === "z" ? side : 0);
4806
4851
  }
4807
- /** The material an impact effect is chosen by: the tags of the material (`metal`, `glass`, `wood`), else stone and the like are `concrete`. */
4808
- impactMaterial(def) {
4809
- for (const tag of IMPACT_TAGS) if (def?.tags?.includes(tag)) return tag;
4810
- return "concrete";
4811
- }
4812
4852
  /**
4813
4853
  * Plays the effect the event map names for a destruction event (`data/event-effects.json` of `game-vfx`, or `RendererOptions.eventEffects`) at a point.
4814
- * Answers whether one played, so the caller can leave out the burst it replaces.
4854
+ * `play` gives the family of the material (the map's `{material}`) and the colours of its debris and dust. Answers whether one played, so the caller can
4855
+ * leave out the burst it replaces.
4815
4856
  */
4816
- playEvent(event, point) {
4817
- const id = effectForEvent(event, this.o.eventEffects ?? EVENT_EFFECTS_MAP);
4857
+ playEvent(event, point, play) {
4858
+ const id = effectForEvent(play && event.material === void 0 ? { ...event, material: play.material } : event, this.o.eventEffects ?? EVENT_EFFECTS_MAP);
4818
4859
  if (!id) return false;
4819
- return this.vfx.play(id, point, { seed: hashNumbers(point[0], point[1], point[2], this.time) }) !== null;
4860
+ return this.vfx.play(id, point, { seed: hashNumbers(point[0], point[1], point[2], this.time), ...play ? { values: play.values } : {} }) !== null;
4861
+ }
4862
+ /** The family and colours of a part's material, for the effects of the event map. */
4863
+ partPlay(objectId, partId) {
4864
+ const pv = this.partVis(objectId, partId);
4865
+ const def = pv ? this.world.materials.get(pv.material) : void 0;
4866
+ return familyPlay(def, pv ? this.partColor(pv, def) ?? void 0 : void 0);
4820
4867
  }
4821
4868
  /** A coat came off: a patch in the colour of the core under it, below the bullet holes and cracks. */
4822
4869
  onSpall(ev) {
@@ -4828,11 +4875,12 @@ var Renderer = class {
4828
4875
  const def = this.world.materials.get(pv.material);
4829
4876
  const normal = this.faceNormal(mv, wp);
4830
4877
  const core = def?.visual?.innerColor;
4831
- if (core !== void 0) this.vfx.burst("chips", ev.point, [normal.x, normal.y, normal.z], 5, 2.5, { color: core });
4832
- else this.playEvent({ kind: "spall", material: this.impactMaterial(def) }, ev.point);
4878
+ const look = debrisLook(def, "spall", { partColor: this.partColor(pv, def) ?? void 0 });
4879
+ if (core !== void 0 || look.impact === void 0) this.vfx.burst(look.debris, ev.point, [normal.x, normal.y, normal.z], 5, 2.5, { color: look.color });
4880
+ else this.playEvent({ kind: "spall", material: look.impact }, ev.point);
4833
4881
  if (core !== void 0) this.trickleFrom(ev.point, [normal.x, normal.y, normal.z], core, mv);
4834
4882
  if (mv.mesh.userData.drawnSpalls?.includes(ev.part)) return;
4835
- this.visuals.addSpall(mv, wp, normal, Math.sqrt(ev.area / Math.PI), core ?? fallbackColor(pv.material, def));
4883
+ this.visuals.addSpall(mv, wp, normal, Math.sqrt(ev.area / Math.PI), core ?? fallbackColor(def));
4836
4884
  }
4837
4885
  /**
4838
4886
  * Grit and dust go on falling from where a blow broke a hard face (masonry, concrete, wood) for `TRICKLE_SECONDS`, a pinch at a time and less and
@@ -4875,17 +4923,18 @@ var Renderer = class {
4875
4923
  * (cutting the wall's faces) is not drawn yet: it needs the per-vertex binding of the house renderer.
4876
4924
  */
4877
4925
  onStage(ev) {
4878
- const played = this.playEvent({ kind: "stage", stage: ev.stage }, ev.opening?.center ?? this.rootPoint(ev.object));
4879
- if (!ev.fragments || !ev.opening) return;
4880
4926
  const pv = this.partVis(ev.object, ev.part);
4881
4927
  const def = pv ? this.world.materials.get(pv.material) : void 0;
4882
- const color = (pv ? this.partColor(pv, def) : null) ?? fallbackColor(pv?.material ?? "", def);
4928
+ const at = ev.opening?.center ?? this.hitPoints.of(ev.object, ev.part, this.time) ?? this.rootPoint(ev.object);
4929
+ const played = this.playEvent({ kind: "stage", stage: ev.stage }, at, this.partPlay(ev.object, ev.part));
4930
+ if (!ev.fragments || !ev.opening) return;
4931
+ const color = (pv ? this.partColor(pv, def) : null) ?? fallbackColor(def);
4883
4932
  const maxAlive = this.low ? Math.floor(this.maxFragments / 2) : this.maxFragments;
4884
4933
  const step = this.low ? 2 : 1;
4885
4934
  for (let i = 0; i < ev.fragments.length; i += step) {
4886
4935
  const fr = ev.fragments[i];
4887
4936
  const fdef = this.world.materials.get(fr.material);
4888
- this.fragments.spawn(fr, this.fragments.materialFor(fr.material, fdef === def || !fdef ? color : fallbackColor(fr.material, fdef), false), ev.object, maxAlive);
4937
+ this.fragments.spawn(fr, this.fragments.materialFor(fr.material, fdef === def || !fdef ? color : fallbackColor(fdef), false), ev.object, maxAlive);
4889
4938
  }
4890
4939
  if (!played) this.vfx.burst("dust", ev.opening.center, [0, 1, 0], 14, 1.2, { color });
4891
4940
  this.impactSound(pv?.material ?? "", ev.opening.center, 0.8, ev.blow);
@@ -4898,11 +4947,11 @@ var Renderer = class {
4898
4947
  onChip(ev) {
4899
4948
  const pv = this.partVis(ev.object, ev.part);
4900
4949
  const def = pv ? this.world.materials.get(pv.material) : void 0;
4901
- const color = (pv ? this.partColor(pv, def) : null) ?? fallbackColor(pv?.material ?? "", def);
4950
+ const color = (pv ? this.partColor(pv, def) : null) ?? fallbackColor(def);
4902
4951
  const maxAlive = this.low ? Math.floor(this.maxFragments / 2) : this.maxFragments;
4903
4952
  for (const fr of ev.fragments) {
4904
4953
  const fdef = this.world.materials.get(fr.material);
4905
- const tone3 = fr.unit && fdef?.visual?.unitColor !== void 0 ? fdef.visual.unitColor : fr.inner && fdef?.visual?.innerColor !== void 0 ? fdef.visual.innerColor : fdef === def ? color : fallbackColor(fr.material, fdef);
4954
+ const tone3 = fragmentTone(fdef, fr, fdef === def ? color : fallbackColor(fdef));
4906
4955
  this.fragments.spawn(fr, this.fragments.materialFor(fr.material, tone3, false), ev.object, maxAlive);
4907
4956
  }
4908
4957
  if (def?.visual?.innerColor !== void 0 || pv?.material === "concrete" || pv?.material === "brick") {
@@ -4926,7 +4975,7 @@ var Renderer = class {
4926
4975
  const pv = b?.parts.get(ev.part);
4927
4976
  const matId = pv?.material || (ev.fragments[0]?.material ?? "");
4928
4977
  const def = this.world.materials.get(matId);
4929
- let color = fallbackColor(matId, def);
4978
+ let color = fallbackColor(def);
4930
4979
  let opacity = 0.5;
4931
4980
  const transparent = def?.visual?.transparent === true;
4932
4981
  if (pv) {
@@ -4940,23 +4989,25 @@ var Renderer = class {
4940
4989
  this.loosenIfFootless(ev.object);
4941
4990
  const up = [0, 1, 0];
4942
4991
  const sizeK = clamp(ev.fragments.length / 10, 0.5, 2);
4992
+ const look = debrisLook(def, "fracture", { style: ev.style, partColor: color });
4993
+ const dust = look.dust !== void 0 ? { color: look.dust } : void 0;
4943
4994
  switch (ev.style) {
4944
4995
  case "shatter":
4945
4996
  this.vfx.burst("shards", ev.point, up, 18 * sizeK, 3);
4946
4997
  break;
4947
4998
  case "crumble":
4948
- this.vfx.burst("dust", ev.point, up, 16 * sizeK, 1.2, { color: pv ? this.partColor(pv, def) ?? void 0 : void 0 });
4949
- this.vfx.burst("chips", ev.point, up, 6, 3);
4999
+ this.vfx.burst("dust", ev.point, up, 16 * sizeK, 1.2, dust);
5000
+ this.vfx.burst(look.family === "glass" ? "shards" : "chips", ev.point, up, 6, 3, { color: look.color });
4950
5001
  break;
4951
5002
  case "splinter":
4952
- this.vfx.burst("splinters", ev.point, up, 14 * sizeK, 3);
4953
- this.vfx.burst("dust", ev.point, up, 5, 0.8, { color: 11901556 });
5003
+ this.vfx.burst("splinters", ev.point, up, 14 * sizeK, 3, { color: look.color });
5004
+ this.vfx.burst("dust", ev.point, up, 5, 0.8, dust);
4954
5005
  break;
4955
5006
  case "tear":
4956
- this.vfx.burst("fibers", ev.point, up, 12 * sizeK, 1.6, { color: def?.visual?.innerColor ?? color });
5007
+ this.vfx.burst("fibers", ev.point, up, 12 * sizeK, 1.6, { color: look.color });
4957
5008
  break;
4958
5009
  case "snap":
4959
- this.vfx.burst("chips", ev.point, up, 8 * sizeK, 3);
5010
+ this.vfx.burst("chips", ev.point, up, 8 * sizeK, 3, { color: look.color });
4960
5011
  break;
4961
5012
  default:
4962
5013
  this.vfx.burst("sparks", ev.point, up, 10, 4);
@@ -4974,7 +5025,7 @@ var Renderer = class {
4974
5025
  }
4975
5026
  const fr = ev.fragments[i];
4976
5027
  const fdef = this.world.materials.get(fr.material);
4977
- const tone3 = fr.inner && fdef?.visual?.innerColor !== void 0 ? fdef.visual.innerColor : fdef === look.def || !fdef ? look.color : fallbackColor(fr.material, fdef);
5028
+ const tone3 = fragmentTone(fdef, fr, fdef === look.def || !fdef ? look.color : fallbackColor(fdef));
4978
5029
  const mat = this.fragments.materialFor(fr.material, tone3, look.transparent || fdef?.visual?.transparent === true, look.opacity);
4979
5030
  this.fragments.spawn(fr, mat, ev.object, maxAlive);
4980
5031
  }
@@ -5497,7 +5548,7 @@ var Renderer = class {
5497
5548
  const s = bb.getSize(new this.THREE.Vector3());
5498
5549
  const footprint = Math.sqrt(Math.max(s.x * s.z, 1e-4));
5499
5550
  if (reason === "melted") {
5500
- this.ground.add("puddle", c.x, c.z, clamp(footprint * 0.55, 0.05, 1), this.partColor(pv, def) ?? fallbackColor(pv.material, def), objectId);
5551
+ this.ground.add("puddle", c.x, c.z, clamp(footprint * 0.55, 0.05, 1), this.partColor(pv, def) ?? fallbackColor(def), objectId);
5501
5552
  } else {
5502
5553
  this.ground.add("ash", c.x, c.z, clamp(footprint * 0.35, 0.04, 0.5), 1907224, objectId);
5503
5554
  this.vfx.burst("ash", [c.x, bb.max.y, c.z], [0, 1, 0], 10, 0.8);