@adatechnology/game-vehicles 0.4.6 → 0.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
@@ -5047,8 +5047,15 @@ function installGlassDamage(kit) {
5047
5047
  x.arc(cx, cy, 26 * s, 0, PI * 2);
5048
5048
  x.fill();
5049
5049
  }
5050
+ const NO_CANVAS = new Proxy(function() {
5051
+ }, { get: () => NO_CANVAS, apply: () => NO_CANVAS, set: () => true });
5050
5052
  function crackGlass(c, msh, p, force, sc) {
5051
5053
  const G = msh.userData.glass;
5054
+ if (G.cracksFreed) {
5055
+ drawWeb(NO_CANVAS, 0, 0, sc != null ? sc : 0.6 + force * 0.5, Math.max(0.4, G.aspect || 1), c.rand);
5056
+ G.hits++;
5057
+ return;
5058
+ }
5052
5059
  if (!G.cv) {
5053
5060
  G.cv = document2.createElement("canvas");
5054
5061
  G.cv.width = G.cv.height = 256;
@@ -5131,6 +5138,14 @@ function installGlassDamage(kit) {
5131
5138
  const G = msh.userData.glass;
5132
5139
  if (G.broken) return;
5133
5140
  G.broken = true;
5141
+ const freeCracks = function() {
5142
+ if (!G.tex) return;
5143
+ if (msh.material !== MATERIALS.glassEdge && msh.material?.map === G.tex) msh.material.dispose();
5144
+ G.tex.dispose();
5145
+ G.tex = null;
5146
+ G.cv = null;
5147
+ G.cracksFreed = true;
5148
+ };
5134
5149
  const cubesAt = [];
5135
5150
  if (G.outline && G.T !== void 0) {
5136
5151
  const out = G.outline;
@@ -5164,6 +5179,7 @@ function installGlassDamage(kit) {
5164
5179
  geo.computeVertexNormals();
5165
5180
  msh.geometry.dispose();
5166
5181
  msh.geometry = geo;
5182
+ freeCracks();
5167
5183
  msh.material = MATERIALS.glassEdge;
5168
5184
  let minx = 1e9;
5169
5185
  let maxx = -1e9;
@@ -5187,8 +5203,12 @@ function installGlassDamage(kit) {
5187
5203
  if (rim) {
5188
5204
  msh.geometry.dispose();
5189
5205
  msh.geometry = rim;
5206
+ freeCracks();
5190
5207
  msh.material = MATERIALS.glassEdge;
5191
- } else msh.visible = false;
5208
+ } else {
5209
+ freeCracks();
5210
+ msh.visible = false;
5211
+ }
5192
5212
  for (let j = 0; j < 36; j++)
5193
5213
  cubesAt.push(
5194
5214
  new V3(
@@ -12885,13 +12905,100 @@ function buildVehicleItem(kit, key, options = {}) {
12885
12905
  root.userData.vehicle = vehicle;
12886
12906
  return { root, vehicle };
12887
12907
  }
12908
+
12909
+ // src/destructible/enclosure.ts
12910
+ var CABIN_LEAK = 0.02;
12911
+ var HOLE_RATE_PER_AREA = 3e3;
12912
+ var CRACK_GAP_PER_METRE = 0.5;
12913
+ var THROUGH = 0.18;
12914
+ var HOLE_PATCH = 0.12;
12915
+ var FULL_SWING = 1.15;
12916
+ var SHELL_ROLES = /* @__PURE__ */ new Set(["door", "glass", "roof", "lid", "panel", "pillar"]);
12917
+ var GONE = /* @__PURE__ */ new Set(["broken", "melted", "ashes"]);
12918
+ function cabinBoxOf(vehicle) {
12919
+ const cabin = vehicle.meta.cabin;
12920
+ if (!cabin || !(cabin.x1 > cabin.x0) || !(cabin.y1 > cabin.y0) || !(cabin.z > 0)) return null;
12921
+ return cabin;
12922
+ }
12923
+ function swingOf(vehicle, partId) {
12924
+ const pivot = vehicle.vehicle?.getObjectByName?.(partId);
12925
+ const q = pivot?.quaternion;
12926
+ if (!q) return 0;
12927
+ const angle = 2 * Math.acos(Math.min(1, Math.abs(q.w)));
12928
+ return Math.min(1, angle / FULL_SWING);
12929
+ }
12930
+ var overlaps = (b, c, margin) => Math.abs(b.center[0] - (c.x0 + c.x1) / 2) <= b.size[0] / 2 + (c.x1 - c.x0) / 2 + margin && Math.abs(b.center[1] - (c.y0 + c.y1) / 2) <= b.size[1] / 2 + (c.y1 - c.y0) / 2 + margin && Math.abs(b.center[2]) <= b.size[2] / 2 + c.z + margin;
12931
+ function cabinEnclosure(source, options = {}) {
12932
+ const c = source.vehicle;
12933
+ const cabin = cabinBoxOf(c);
12934
+ const node = c.vehicle;
12935
+ if (!cabin || !node?.localToWorld) return null;
12936
+ const state = source.state();
12937
+ if (state.destroyed) return null;
12938
+ const V3 = node.position.constructor;
12939
+ const Quat = node.quaternion.constructor;
12940
+ const mid = [(cabin.x0 + cabin.x1) / 2, (cabin.y0 + cabin.y1) / 2, 0];
12941
+ const half = [(cabin.x1 - cabin.x0) / 2, (cabin.y1 - cabin.y0) / 2, cabin.z];
12942
+ const ajar = options.ajar ?? 0.06;
12943
+ const byId = new Map(state.parts.map((p) => [p.id, p]));
12944
+ const mainGroup = state.groups[0] ? new Set(state.groups[0]) : null;
12945
+ const openings = [];
12946
+ let crackLength = 0;
12947
+ const toCabin = (b) => ({
12948
+ center: [b.center[0] - mid[0], b.center[1] - mid[1], b.center[2] - mid[2]],
12949
+ halfSize: [b.size[0] / 2 + THROUGH, b.size[1] / 2 + THROUGH, b.size[2] / 2 + THROUGH]
12950
+ });
12951
+ node.updateWorldMatrix?.(true, false);
12952
+ const frame = options.frame ?? c.root;
12953
+ frame?.updateWorldMatrix?.(true, false);
12954
+ const inverse = node.matrixWorld.clone().invert();
12955
+ for (const def of source.model.parts) {
12956
+ if (!SHELL_ROLES.has(def.role) || !overlaps(def.box, cabin, 0.12)) continue;
12957
+ const part = byId.get(def.id);
12958
+ if (!part) continue;
12959
+ const gone = GONE.has(part.status) || mainGroup !== null && !mainGroup.has(def.id);
12960
+ const swung = (def.role === "door" || def.role === "lid") && swingOf(c, def.id) > ajar;
12961
+ if (gone || swung) {
12962
+ openings.push(toCabin(def.box));
12963
+ continue;
12964
+ }
12965
+ for (const hole of part.holes) {
12966
+ if (!hole.through && def.material !== "glass") continue;
12967
+ const area = Math.PI * (hole.diameter / 2) ** 2;
12968
+ const p = new V3(hole.point[0], hole.point[1], hole.point[2]);
12969
+ if (frame?.localToWorld) frame.localToWorld(p);
12970
+ p.applyMatrix4(inverse);
12971
+ openings.push({
12972
+ center: [p.x - mid[0], p.y - mid[1], p.z - mid[2]],
12973
+ halfSize: [HOLE_PATCH, HOLE_PATCH, HOLE_PATCH],
12974
+ // the patch is far bigger than the hole: only a share of what crosses it passes, more for a bigger hole
12975
+ rate: Math.min(4, area * HOLE_RATE_PER_AREA)
12976
+ });
12977
+ }
12978
+ for (const crack of part.cracks) crackLength += crack.length;
12979
+ }
12980
+ const center = node.localToWorld(new V3(mid[0], mid[1], mid[2]));
12981
+ const q = node.getWorldQuaternion(new Quat());
12982
+ const scale = node.getWorldScale(new V3());
12983
+ const leak = (options.leak ?? CABIN_LEAK) * (1 + crackLength * CRACK_GAP_PER_METRE);
12984
+ return {
12985
+ center: [center.x, center.y, center.z],
12986
+ halfSize: [half[0] * Math.abs(scale.x), half[1] * Math.abs(scale.y), half[2] * Math.abs(scale.z)],
12987
+ orientation: [q.x, q.y, q.z, q.w],
12988
+ openings: openings.map((o) => ({ ...o, center: [o.center[0] * scale.x, o.center[1] * scale.y, o.center[2] * scale.z], halfSize: [o.halfSize[0] * Math.abs(scale.x), o.halfSize[1] * Math.abs(scale.y), o.halfSize[2] * Math.abs(scale.z)] })),
12989
+ leak
12990
+ };
12991
+ }
12888
12992
  export {
12993
+ CABIN_LEAK,
12889
12994
  JOINT_RULES,
12890
12995
  LEGACY_STATES,
12891
12996
  LEGACY_ZONE_IDS,
12892
12997
  SEED_SALTS,
12893
12998
  buildVehicleItem,
12894
12999
  buildVehicleModel,
13000
+ cabinBoxOf,
13001
+ cabinEnclosure,
12895
13002
  clamp,
12896
13003
  createLod,
12897
13004
  createTranslate,
@@ -12911,6 +13018,7 @@ export {
12911
13018
  rng,
12912
13019
  smooth,
12913
13020
  strHash,
13021
+ swingOf,
12914
13022
  wrapThreeForLod
12915
13023
  };
12916
13024
  //# sourceMappingURL=index.js.map