@adatechnology/game-weapons-three 0.24.3 → 0.25.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.
@@ -9442,9 +9442,9 @@ var targetsOf = (THREE, hold, rootQuaternion, magazine, weights2) => {
9442
9442
  hold.works?.forEach((work, index) => {
9443
9443
  const k = weights2?.[index] ?? 0;
9444
9444
  if (k <= 0) return;
9445
- const pose = poseOf(work.anchor, rootQuaternion);
9446
- if (work.hand === "R") right = right ? blendPose(THREE, right, pose, k) : pose;
9447
- else left = left ? blendPose(THREE, left, pose, k) : pose;
9445
+ const pose2 = poseOf(work.anchor, rootQuaternion);
9446
+ if (work.hand === "R") right = right ? blendPose(THREE, right, pose2, k) : pose2;
9447
+ else left = left ? blendPose(THREE, left, pose2, k) : pose2;
9448
9448
  });
9449
9449
  const targets = { R: right, L: left, grip: hold.grip, gripL: hold.gripLeft, scale: hold.scale };
9450
9450
  if (hold.slideLeft && left) {
@@ -9757,11 +9757,17 @@ function drawItemWeapon({ THREE, def, items, itemId, turn: turn2, roll, flip, gr
9757
9757
 
9758
9758
  // src/models/explosives.ts
9759
9759
  import { markKitOrigin as markKitOrigin3 } from "@adatechnology/game-contracts";
9760
+ var M67_PARTS = {
9761
+ bodyRadius: 0.032,
9762
+ fuze: { radius: 0.011, height: 0.022, y: 0.04 },
9763
+ spoon: { size: [0.012, 0.07, 6e-3], x: 0.016, y: 0.018, tilt: 0.2 }
9764
+ };
9760
9765
  var grenade = (kit, group) => {
9761
9766
  const { M, sphere, cylinderY, box: box2, torus, THREE } = kit;
9762
- sphere(group, 0.032, M.olive, 0, 0.032, 0, 1);
9763
- cylinderY(group, 0.011, 0.022, M.metal, 0, 0.072, 0);
9764
- box2(group, 0.012, 0.07, 6e-3, M.metal, 0.016, 0.05, 0, 0.2);
9767
+ const { bodyRadius, fuze, spoon } = M67_PARTS;
9768
+ sphere(group, bodyRadius, M.olive, 0, bodyRadius, 0, 1);
9769
+ cylinderY(group, fuze.radius, fuze.height, M.metal, 0, bodyRadius + fuze.y, 0);
9770
+ box2(group, ...spoon.size, M.metal, spoon.x, bodyRadius + spoon.y, 0, spoon.tilt);
9765
9771
  torus(group, new THREE.TorusGeometry(85e-4, 16e-4, 6, 12), M.steel, -0.012, 0.082, 0);
9766
9772
  kit.mark(group, 0, 0.032, 0).name = "handle";
9767
9773
  };
@@ -10001,7 +10007,7 @@ function createBoxFx(options) {
10001
10007
  model.position.lerpVectors(holderAt, model.position, k);
10002
10008
  hand.updateMatrixWorld(true);
10003
10009
  };
10004
- const throwBody = (kind, mesh, from, velocity, radius) => {
10010
+ const throwBody = (kind, mesh, from, velocity2, radius) => {
10005
10011
  if (!(kind === "casing" ? layers.casings : layers.magazines)) {
10006
10012
  mesh.geometry.dispose();
10007
10013
  return;
@@ -10012,7 +10018,7 @@ function createBoxFx(options) {
10012
10018
  mesh.position.copy(from);
10013
10019
  mesh.castShadow = true;
10014
10020
  scene.add(mesh);
10015
- bodies.push({ mesh, velocity, spin: new THREE.Vector3(random() * 20 - 10, random() * 30 - 15, random() * 20 - 10), radius, age: 0, resting: false, bounces: 0, kind });
10021
+ bodies.push({ mesh, velocity: velocity2, spin: new THREE.Vector3(random() * 20 - 10, random() * 30 - 15, random() * 20 - 10), radius, age: 0, resting: false, bounces: 0, kind });
10016
10022
  };
10017
10023
  const ejectCasing = (caliberMm, isShell, downward) => {
10018
10024
  const radius = isShell ? 0.0105 : Math.max(3e-3, caliberMm / 1e3 * 0.6);
@@ -10022,8 +10028,8 @@ function createBoxFx(options) {
10022
10028
  const from = handPoint(0.03, 0.03, 0.05, new THREE.Vector3());
10023
10029
  const right = new THREE.Vector3(1, 0, 0).applyQuaternion(camera.quaternion);
10024
10030
  const up = new THREE.Vector3(0, 1, 0);
10025
- const velocity = downward ? up.clone().multiplyScalar(-0.5).addScaledVector(right, (random() - 0.5) * 0.4) : right.multiplyScalar(1.6 + random() * 0.8).addScaledVector(up, 1.4 + random() * 0.8);
10026
- throwBody("casing", mesh, from, velocity, radius);
10031
+ const velocity2 = downward ? up.clone().multiplyScalar(-0.5).addScaledVector(right, (random() - 0.5) * 0.4) : right.multiplyScalar(1.6 + random() * 0.8).addScaledVector(up, 1.4 + random() * 0.8);
10032
+ throwBody("casing", mesh, from, velocity2, radius);
10027
10033
  };
10028
10034
  const dropMagazine = (category) => {
10029
10035
  const long = category === "rifle" || category === "sniper" || category === "carbine";
@@ -10230,14 +10236,14 @@ function composeClip(id, tweens, events = [], minDuration = 0) {
10230
10236
  const poses = /* @__PURE__ */ new Map();
10231
10237
  for (const [key, list] of channels) {
10232
10238
  const [part, channel, axis] = key.split("|");
10233
- const pose = poses.get(part) ?? { position: [0, 0, 0], rotation: [0, 0, 0] };
10234
- pose[channel][AXIS_INDEX[axis]] = round(valueAt(list, t, includeInstants));
10235
- poses.set(part, pose);
10239
+ const pose2 = poses.get(part) ?? { position: [0, 0, 0], rotation: [0, 0, 0] };
10240
+ pose2[channel][AXIS_INDEX[axis]] = round(valueAt(list, t, includeInstants));
10241
+ poses.set(part, pose2);
10236
10242
  }
10237
10243
  const root = poses.get(ROOT);
10238
10244
  poses.delete(ROOT);
10239
10245
  const parts = {};
10240
- for (const [part, pose] of poses) parts[part] = pose;
10246
+ for (const [part, pose2] of poses) parts[part] = pose2;
10241
10247
  return { t, ...root ? { position: root.position, rotation: root.rotation } : {}, ...poses.size ? { parts } : {} };
10242
10248
  };
10243
10249
  const keys = [];
@@ -11336,13 +11342,13 @@ function createEffects(THREE, sink, random) {
11336
11342
  muzzleFlash(position, orientation, size, profile = MUZZLE_SMOKE.smokeless, track) {
11337
11343
  if (profile.puffs <= 0 && profile.flash <= 0) return;
11338
11344
  const follow = track ? () => {
11339
- const pose = track();
11340
- return pose ? { point: vec(pose.position), orientation: quaternion(pose.orientation) } : null;
11345
+ const pose2 = track();
11346
+ return pose2 ? { point: vec(pose2.position), orientation: quaternion(pose2.orientation) } : null;
11341
11347
  } : void 0;
11342
11348
  sink?.play({ effect: VIEWMODEL_EFFECT_IDS.flash, point: vec(position), direction: barrel(orientation), orientation: quaternion(orientation), seed: seed(), values: { size, ...profile }, ...follow ? { track: follow } : {} });
11343
11349
  },
11344
- smoke(position, velocity, size, life, opacity) {
11345
- sink?.play({ effect: VIEWMODEL_EFFECT_IDS.smoke, point: vec(position), seed: seed(), values: { vx: velocity.x, vy: velocity.y, vz: velocity.z, radius: size, life, opacity } });
11350
+ smoke(position, velocity2, size, life, opacity) {
11351
+ sink?.play({ effect: VIEWMODEL_EFFECT_IDS.smoke, point: vec(position), seed: seed(), values: { vx: velocity2.x, vy: velocity2.y, vz: velocity2.z, radius: size, life, opacity } });
11346
11352
  },
11347
11353
  backblast(rear, orientation) {
11348
11354
  sink?.play({ effect: VIEWMODEL_EFFECT_IDS.backblast, point: vec(rear), direction: barrel(orientation), orientation: quaternion(orientation), seed: seed() });
@@ -11508,6 +11514,61 @@ function pooledFlame(kit, pools, index) {
11508
11514
  return { geometry, material };
11509
11515
  }
11510
11516
 
11517
+ // src/engine-loose.ts
11518
+ var LOOSE_ROLLING_RESISTANCE = 0.015;
11519
+ var CASING_FILL = 0.25;
11520
+ var BRASS_DENSITY = 8500;
11521
+ var LOOSE_USER = { casing: 100 };
11522
+ var BOUNCE_FALL_SPEED = 0.5;
11523
+ var BOUNCE_RISE_SPEED = 0.2;
11524
+ var QUARTER_TURN_Z = [0, 0, Math.SQRT1_2, Math.SQRT1_2];
11525
+ var CASING_MATERIAL = { frictionStatic: 0.4, frictionDynamic: 0.3, restitution: 0.4 };
11526
+ function casingCollider(radius, length) {
11527
+ return {
11528
+ shape: { kind: "compound", children: [{ shape: { kind: "cylinder", halfHeight: length / 2, radius }, position: [0, 0, 0], rotation: QUARTER_TURN_Z }] },
11529
+ mass: Math.PI * radius * radius * length * BRASS_DENSITY * CASING_FILL,
11530
+ material: CASING_MATERIAL,
11531
+ rolling: { coefficient: LOOSE_ROLLING_RESISTANCE, radius },
11532
+ user: LOOSE_USER.casing
11533
+ };
11534
+ }
11535
+ function spawnLoose(world, collider, launch) {
11536
+ const desc = {
11537
+ shape: collider.shape,
11538
+ motion: "dynamic",
11539
+ mass: collider.mass,
11540
+ position: [launch.position.x, launch.position.y, launch.position.z],
11541
+ rotation: [launch.rotation.x, launch.rotation.y, launch.rotation.z, launch.rotation.w],
11542
+ material: collider.material,
11543
+ layer: "debris",
11544
+ continuous: true,
11545
+ rolling: collider.rolling,
11546
+ linearVelocity: [launch.velocity.x, launch.velocity.y, launch.velocity.z],
11547
+ angularVelocity: [launch.spin.x, launch.spin.y, launch.spin.z],
11548
+ user: collider.user
11549
+ };
11550
+ return { id: world.createBody(desc), bounces: 0, fallSpeed: 0 };
11551
+ }
11552
+ var scratchReading = { asleep: false, bounceSpeed: void 0 };
11553
+ var pose = new Float64Array(7);
11554
+ var velocity = new Float64Array(6);
11555
+ function syncLoose(world, body, object) {
11556
+ world.readPose(body.id, pose, 0);
11557
+ world.readVelocity(body.id, velocity, 0);
11558
+ object.position.set(pose[0] ?? 0, pose[1] ?? 0, pose[2] ?? 0);
11559
+ object.quaternion.set(pose[3] ?? 0, pose[4] ?? 0, pose[5] ?? 0, pose[6] ?? 1);
11560
+ const vertical = velocity[1] ?? 0;
11561
+ const bounced = body.fallSpeed > BOUNCE_FALL_SPEED && vertical > BOUNCE_RISE_SPEED;
11562
+ scratchReading.bounceSpeed = bounced ? body.fallSpeed : void 0;
11563
+ body.fallSpeed = vertical < 0 ? -vertical : 0;
11564
+ if (bounced) body.bounces++;
11565
+ scratchReading.asleep = world.isSleeping(body.id);
11566
+ return scratchReading;
11567
+ }
11568
+ function dropLoose(world, body) {
11569
+ world.removeBody(body.id);
11570
+ }
11571
+
11511
11572
  // src/fx/particles.ts
11512
11573
  var MAX_PARTICLES = 900;
11513
11574
  var FLOOR_FRICTION = 0.45;
@@ -11522,7 +11583,7 @@ var disposeOwned = (object) => {
11522
11583
  if (mesh.isMesh) mesh.geometry.dispose();
11523
11584
  });
11524
11585
  };
11525
- function createParticles(THREE, scene, groundAt, limit, surface) {
11586
+ function createParticles(THREE, scene, groundAt, limit, surface, physics) {
11526
11587
  const from = new THREE.Vector3();
11527
11588
  const step = new THREE.Vector3();
11528
11589
  let list = [];
@@ -11532,6 +11593,7 @@ function createParticles(THREE, scene, groundAt, limit, surface) {
11532
11593
  });
11533
11594
  particle.object.removeFromParent();
11534
11595
  if (particle.ownsGeometry) disposeOwned(particle.object);
11596
+ if (particle.body && physics) dropLoose(physics, particle.body);
11535
11597
  particle.onRemove?.();
11536
11598
  if (particle.fadeFrom !== void 0) particle.object.material.dispose();
11537
11599
  list = list.filter((other) => other !== particle);
@@ -11547,24 +11609,24 @@ function createParticles(THREE, scene, groundAt, limit, surface) {
11547
11609
  if (!hit) return;
11548
11610
  const at2 = origin.y + travel.y * hit.distance;
11549
11611
  if (at2 <= groundAt(origin.x + travel.x * hit.distance, origin.z + travel.z * hit.distance) + radius + FLOOR_EPSILON) return;
11550
- const { velocity } = particle;
11551
- const impact = Math.abs(velocity.dot(hit.normal));
11612
+ const { velocity: velocity2 } = particle;
11613
+ const impact = Math.abs(velocity2.dot(hit.normal));
11552
11614
  object.position.copy(origin).addScaledVector(travel, Math.max(0, hit.distance - radius));
11553
- velocity.addScaledVector(hit.normal, -(1 + particle.bounce) * velocity.dot(hit.normal));
11554
- velocity.multiplyScalar(FLOOR_FRICTION);
11615
+ velocity2.addScaledVector(hit.normal, -(1 + particle.bounce) * velocity2.dot(hit.normal));
11616
+ velocity2.multiplyScalar(FLOOR_FRICTION);
11555
11617
  particle.spin.multiplyScalar(FLOOR_FRICTION);
11556
11618
  if (particle.bounces < 4) api.onBounce?.(particle.kind, { object, speed: impact, bounce: particle.bounces });
11557
11619
  particle.bounces++;
11558
11620
  };
11559
11621
  const api = {
11560
- spawn(object, velocity, options = {}) {
11622
+ spawn(object, velocity2, options = {}) {
11561
11623
  object.traverse((node) => {
11562
11624
  node.userData[PARTICLE_USERDATA_KEY] = true;
11563
11625
  });
11564
11626
  scene.add(object);
11565
11627
  list.push({
11566
11628
  object,
11567
- velocity,
11629
+ velocity: velocity2,
11568
11630
  spin: options.spin ?? new THREE.Vector3(),
11569
11631
  age: 0,
11570
11632
  resting: false,
@@ -11580,7 +11642,8 @@ function createParticles(THREE, scene, groundAt, limit, surface) {
11580
11642
  onGround: options.onGround,
11581
11643
  ownsGeometry: options.ownsGeometry ?? false,
11582
11644
  onRemove: options.onRemove,
11583
- bounces: 0
11645
+ bounces: 0,
11646
+ body: options.collider && physics ? spawnLoose(physics, options.collider, { position: object.position, rotation: object.quaternion, velocity: velocity2, spin: options.spin ?? new THREE.Vector3() }) : void 0
11584
11647
  });
11585
11648
  const hostLimit = limit?.();
11586
11649
  const oldest = list[0];
@@ -11597,6 +11660,13 @@ function createParticles(THREE, scene, groundAt, limit, surface) {
11597
11660
  const { object } = particle;
11598
11661
  if (particle.fadeFrom !== void 0) object.material.opacity = Math.max(0, particle.fadeFrom * (1 - particle.age / particle.life));
11599
11662
  if (particle.resting) continue;
11663
+ if (particle.body && physics) {
11664
+ const reading = syncLoose(physics, particle.body, object);
11665
+ if (reading.bounceSpeed !== void 0 && particle.bounces < 4) api.onBounce?.(particle.kind, { object, speed: reading.bounceSpeed, bounce: particle.bounces });
11666
+ if (reading.bounceSpeed !== void 0) particle.bounces++;
11667
+ if (reading.asleep) particle.resting = true;
11668
+ continue;
11669
+ }
11600
11670
  particle.velocity.y -= particle.gravity * dt;
11601
11671
  if (particle.drag) particle.velocity.multiplyScalar(Math.max(0, 1 - particle.drag * dt));
11602
11672
  const probed = surface !== void 0 && SOLID_KINDS.has(particle.kind) && probes < MAX_PROBES_PER_FRAME && particle.velocity.lengthSq() > PROBE_MIN_SPEED * PROBE_MIN_SPEED;
@@ -11645,20 +11715,20 @@ function createModelRig(model) {
11645
11715
  const registry = partRegistry(model.parts);
11646
11716
  const rest = /* @__PURE__ */ new Map();
11647
11717
  for (const part of [...registry.values(), model.root]) rest.set(part, { position: part.position.clone(), rotation: part.rotation.clone() });
11648
- const apply = (object, pose) => {
11718
+ const apply = (object, pose2) => {
11649
11719
  const base = rest.get(object);
11650
11720
  if (!base) return;
11651
- const [dx, dy, dz] = pose.position ?? [0, 0, 0];
11652
- const [rx, ry, rz] = pose.rotation ?? [0, 0, 0];
11721
+ const [dx, dy, dz] = pose2.position ?? [0, 0, 0];
11722
+ const [rx, ry, rz] = pose2.rotation ?? [0, 0, 0];
11653
11723
  object.position.set(base.position.x + dx, base.position.y + dy, base.position.z + dz);
11654
11724
  object.rotation.set(base.rotation.x + rx, base.rotation.y + ry, base.rotation.z + rz);
11655
11725
  };
11656
11726
  return {
11657
- setPartPose(partId, pose) {
11727
+ setPartPose(partId, pose2) {
11658
11728
  const part = registry.get(partId);
11659
- if (part) apply(part, pose);
11729
+ if (part) apply(part, pose2);
11660
11730
  },
11661
- setRootPose: (pose) => apply(model.root, pose)
11731
+ setRootPose: (pose2) => apply(model.root, pose2)
11662
11732
  };
11663
11733
  }
11664
11734
 
@@ -11689,13 +11759,13 @@ function createClipEventHandler(world, session) {
11689
11759
  const vector = (x, y, z) => new THREE.Vector3(x, y, z);
11690
11760
  const positionOf = (part) => worldPositionOf(session, part, new THREE.Vector3());
11691
11761
  const reach = Math.max(0.55, Math.min(1.2, session.lengthMetres));
11692
- const throwCase = (mark, velocity, spin, tilt = 0) => {
11762
+ const throwCase = (mark, velocity2, spin, tilt = 0) => {
11693
11763
  if (!session.cartridge || !world.layers.casings) return;
11694
11764
  const casing = buildCasing(kit, session.cartridge.dims, session.cartridge.shape);
11695
11765
  casing.group.position.copy(positionOf(mark));
11696
11766
  casing.group.rotation.set(0, random() * 0.6 - 0.3, tilt);
11697
11767
  if (session.cartridge.caseType) casing.group.userData["caseType"] = session.cartridge.caseType;
11698
- particles.spawn(casing.group, velocity, { kind: "casing", life: 25, radius: casing.radius, bounce: 0.35, spin, onRest: restFlat, ownsGeometry: true });
11768
+ particles.spawn(casing.group, velocity2, { kind: "casing", life: 25, radius: casing.radius, bounce: 0.35, spin, onRest: restFlat, ownsGeometry: true, collider: casingCollider(casing.radius, casing.length) });
11699
11769
  };
11700
11770
  const ejectFromPort = (down) => {
11701
11771
  if (!parts.eject) return;
@@ -11704,8 +11774,8 @@ function createClipEventHandler(world, session) {
11704
11774
  };
11705
11775
  const ejectAll = (marks, spent) => {
11706
11776
  marks?.forEach((mark, index) => {
11707
- const { velocity, spin, tilt } = spent(index);
11708
- throwCase(mark, toWorld(session, velocity), spin, tilt);
11777
+ const { velocity: velocity2, spin, tilt } = spent(index);
11778
+ throwCase(mark, toWorld(session, velocity2), spin, tilt);
11709
11779
  });
11710
11780
  };
11711
11781
  const moveIn = (from, to, tilt) => {
@@ -12357,11 +12427,11 @@ function createViewmodel(options) {
12357
12427
  const random = createRng2(options.seed ?? 1);
12358
12428
  const layers = options.layers ?? createLayers();
12359
12429
  const distance = () => options.soundDistance?.() ?? 0;
12360
- const particles = createParticles(THREE, scene, groundAt, options.particleLimit, createSurfaceRaycast(THREE, scene));
12430
+ const particles = createParticles(THREE, scene, groundAt, options.particleLimit, createSurfaceRaycast(THREE, scene), options.physics);
12361
12431
  const spawnParticle = particles.spawn.bind(particles);
12362
- particles.spawn = (object, velocity, particleOptions) => {
12432
+ particles.spawn = (object, velocity2, particleOptions) => {
12363
12433
  if (lens) toWorldLens(camera, object.position, worldFovOf(), lens.fov);
12364
- spawnParticle(object, velocity, particleOptions);
12434
+ spawnParticle(object, velocity2, particleOptions);
12365
12435
  };
12366
12436
  particles.onBounce = (kind, hit) => {
12367
12437
  if (!audio || kind !== "casing" && kind !== "magazine") return;
@@ -12386,10 +12456,10 @@ function createViewmodel(options) {
12386
12456
  stop: (handle) => worldSink.stop(handle)
12387
12457
  } : void 0;
12388
12458
  const effects = createEffects(THREE, lensSink, random);
12389
- function seenThroughLens(pose) {
12390
- if (!pose || !lens) return pose;
12391
- const at2 = toWorldLens(camera, trackPoint.set(...pose.point), worldFovOf(), lens.fov);
12392
- return { ...pose, point: [at2.x, at2.y, at2.z] };
12459
+ function seenThroughLens(pose2) {
12460
+ if (!pose2 || !lens) return pose2;
12461
+ const at2 = toWorldLens(camera, trackPoint.set(...pose2.point), worldFovOf(), lens.fov);
12462
+ return { ...pose2, point: [at2.x, at2.y, at2.z] };
12393
12463
  }
12394
12464
  const hand = new THREE.Group();
12395
12465
  hand.name = VIEWMODEL_NODE_NAME;
@@ -12615,9 +12685,9 @@ function createViewmodel(options) {
12615
12685
  eyeTurn.copy(camera.quaternion).invert();
12616
12686
  const rest = targetsOf(THREE, held, session.model.root.getWorldQuaternion(rootQuaternion), 0);
12617
12687
  let needed = 0;
12618
- for (const [anchor, pose] of [[held.right, rest.R], [held.left, rest.L]]) {
12619
- if (!anchor || !pose || anchor.part !== session.model.root) continue;
12620
- seen.set(...pose.position).sub(camera.position).applyQuaternion(eyeTurn);
12688
+ for (const [anchor, pose2] of [[held.right, rest.R], [held.left, rest.L]]) {
12689
+ if (!anchor || !pose2 || anchor.part !== session.model.root) continue;
12690
+ seen.set(...pose2.position).sub(camera.position).applyQuaternion(eyeTurn);
12621
12691
  if (-seen.z > 0.05) needed = Math.max(needed, HAND_IN_VIEW.floor * -seen.z * tanHalf - seen.y);
12622
12692
  }
12623
12693
  return Math.max(0, Math.min(HAND_IN_VIEW.max, needed));
@@ -12868,6 +12938,7 @@ var createHandFx = (options, layers, hand, oneHand) => {
12868
12938
  ...options.condition ? { condition: options.condition } : {},
12869
12939
  ...options.attachmentModels ? { attachmentModels: options.attachmentModels } : {},
12870
12940
  ...options.raycast ? { raycast: options.raycast } : {},
12941
+ ...options.physics ? { physics: options.physics } : {},
12871
12942
  ...options.contact ? { contact: options.contact } : {},
12872
12943
  ...options.bodyHold ? { bodyHold: options.bodyHold } : {},
12873
12944
  ...options.firstPerson ? { firstPerson: options.firstPerson } : {},
@@ -12989,8 +13060,12 @@ function createWeaponFx(options) {
12989
13060
  }
12990
13061
 
12991
13062
  // src/fx/holstered.ts
13063
+ import { createSlingSwing } from "@adatechnology/game-weapons";
12992
13064
  import { discardObject3D as discardObject3D5 } from "@adatechnology/game-shapes-three";
12993
13065
 
13066
+ // src/fx/holster-rig.ts
13067
+ import { catenary as catenary3, createCord as createCord3 } from "@adatechnology/game-shapes-three";
13068
+
12994
13069
  // src/modeled-prop.ts
12995
13070
  import { markKitOrigin as markKitOrigin5 } from "@adatechnology/game-contracts";
12996
13071
  import { itemMakeup, makeupSpec, tagMakeupParts } from "@adatechnology/game-items-three";
@@ -13042,67 +13117,224 @@ function modeledPropSpec(root, def, name) {
13042
13117
  return { name, parts: [{ id: MODEL_BODY, material: "steel", shape: { kind: "box", size }, position: [0, size[1] / 2, 0], mass: def.mass }], joints: [], tags: ["weapon"] };
13043
13118
  }
13044
13119
 
13045
- // src/fx/holstered.ts
13120
+ // src/fx/holster-rig.ts
13046
13121
  var HOLSTER_PLACEMENT = {
13047
13122
  hip: { muzzle: [0, -1, 0], out: [-1, 0, 0] },
13048
- back: { muzzle: [0.55, 0.835, 0], out: [0, 0, -1] }
13123
+ back: { muzzle: [0.55, 0.835, 0], out: [0, 0, -1] },
13124
+ sling: { muzzle: [0.32, -0.947, 0], out: [0, 0, 1] }
13125
+ };
13126
+ var SLING_HANG = { pivotBehind: 0.3, frontSwivel: 0.3, shoulder: [0.19, 0.2, -0.04], strapRadius: 8e-3, sag: 0.03, color: 4146217 };
13127
+ var gripOfProp = (THREE, weapon, footprint) => {
13128
+ const model = buildWeaponModelById(THREE, weapon);
13129
+ const right = model ? holdOfModel(THREE, model, 1).right : null;
13130
+ if (!model || !right) return { position: new THREE.Vector3(-footprint[0] * 0.15, footprint[1] * 0.3, 0), quaternion: new THREE.Quaternion() };
13131
+ const box2 = new THREE.Box3().setFromObject(model.root);
13132
+ const center = box2.getCenter(new THREE.Vector3());
13133
+ const frame = new THREE.Group();
13134
+ frame.add(model.root);
13135
+ model.root.position.set(-center.x, -box2.min.y, -center.z);
13136
+ frame.updateMatrixWorld(true);
13137
+ return { position: right.part.localToWorld(right.offset.clone()), quaternion: right.quaternion.clone() };
13049
13138
  };
13050
- var holsteredModel = (THREE, weapon, slot, items) => {
13051
- const place = HOLSTER_PLACEMENT[slot];
13052
- const model = place ? buildModeledProp(THREE, { id: weapon }, weapon, void 0, items) : void 0;
13053
- if (!place || !model) return void 0;
13139
+ var placeModel = (THREE, model, place, middle) => {
13054
13140
  const [, height, thickness] = model.userData.footprint ?? [0, 0, 0];
13055
13141
  const muzzle = new THREE.Vector3(...place.muzzle).normalize();
13056
13142
  const out = new THREE.Vector3(...place.out).normalize();
13057
13143
  const top = new THREE.Vector3().crossVectors(out, muzzle).normalize();
13058
13144
  model.quaternion.setFromRotationMatrix(new THREE.Matrix4().makeBasis(muzzle, top, out));
13059
- model.position.copy(top.multiplyScalar(-height / 2)).addScaledVector(out, thickness / 2);
13145
+ model.position.copy(middle).addScaledVector(top, -height / 2).addScaledVector(out, thickness / 2);
13146
+ };
13147
+ var buildHolsterRig = (THREE, weapon, slot, options = {}) => {
13148
+ const place = HOLSTER_PLACEMENT[slot];
13149
+ const model = place ? buildModeledProp(THREE, { id: weapon }, weapon, void 0, options.items) : void 0;
13150
+ if (!place || !model) return void 0;
13151
+ const footprint = model.userData.footprint ?? [0, 0, 0];
13060
13152
  const holder = new THREE.Group();
13061
13153
  holder.name = `holstered_${weapon}`;
13062
- holder.add(model);
13063
- return holder;
13154
+ const slung = slot === "sling" && options.strapMaterial !== void 0;
13155
+ const swing = slung ? new THREE.Group() : holder;
13156
+ if (slung) holder.add(swing);
13157
+ const muzzle = new THREE.Vector3(...place.muzzle).normalize();
13158
+ const middle = slung ? muzzle.clone().multiplyScalar(footprint[0] * SLING_HANG.pivotBehind) : new THREE.Vector3();
13159
+ placeModel(THREE, model, place, middle);
13160
+ swing.add(model);
13161
+ const strap = [];
13162
+ if (slung && options.strapMaterial) {
13163
+ const out = new THREE.Vector3(...place.out).normalize();
13164
+ const front = middle.clone().addScaledVector(muzzle, footprint[0] * SLING_HANG.frontSwivel).addScaledVector(out, footprint[2] / 2);
13165
+ strap.push(createCord3(THREE, catenary3([0, 0, 0], SLING_HANG.shoulder, SLING_HANG.sag, 8), SLING_HANG.strapRadius, options.strapMaterial));
13166
+ strap.push(createCord3(THREE, catenary3([front.x, front.y, front.z], [0, 0, 0], SLING_HANG.sag, 8), SLING_HANG.strapRadius, options.strapMaterial));
13167
+ holder.add(strap[0]);
13168
+ swing.add(strap[1]);
13169
+ }
13170
+ holder.updateMatrixWorld(true);
13171
+ const inProp = gripOfProp(THREE, weapon, footprint);
13172
+ const position = model.localToWorld(inProp.position.clone());
13173
+ const quaternion2 = model.quaternion.clone().multiply(inProp.quaternion);
13174
+ return { holder, swing, grip: { position, quaternion: quaternion2 }, strap };
13175
+ };
13176
+
13177
+ // src/fx/holstered.ts
13178
+ var GROUND_SLOT = "ground";
13179
+ var HOLSTER_TOSS = { drop: 0.8, ahead: 0.3, turn: 1.3, release: 0.4 };
13180
+ var MOVE_GRIP = 0.9;
13181
+ var MAX_SWING_ACCELERATION = 40;
13182
+ var holsteredModel = (THREE, weapon, slot, items) => buildHolsterRig(THREE, weapon, slot, items ? { items } : {})?.holder;
13183
+ var ease4 = (t) => {
13184
+ const k = Math.min(1, Math.max(0, t));
13185
+ return k * k * (3 - 2 * k);
13064
13186
  };
13065
13187
  var createHolsteredWeapons = (options) => {
13066
13188
  const { THREE } = options;
13067
- const models = /* @__PURE__ */ new Map();
13189
+ const rigs = /* @__PURE__ */ new Map();
13068
13190
  const away = /* @__PURE__ */ new Map();
13069
- const place = (weapon, slot) => {
13070
- const holder = models.get(`${weapon}@${slot}`);
13071
- const pose = options.bodySlot(slot);
13072
- if (!holder) return;
13073
- holder.visible = pose !== null;
13074
- if (!pose) return;
13075
- holder.position.set(pose.position[0], pose.position[1], pose.position[2]);
13076
- holder.quaternion.set(pose.quaternion[0], pose.quaternion[1], pose.quaternion[2], pose.quaternion[3]);
13191
+ const strapMaterial = new THREE.MeshStandardMaterial({ color: SLING_HANG.color, roughness: 0.85, metalness: 0 });
13192
+ const frameOf = (pose2) => ({ position: new THREE.Vector3(...pose2.position), quaternion: new THREE.Quaternion(...pose2.quaternion) });
13193
+ const scratch3 = { position: new THREE.Vector3(), quaternion: new THREE.Quaternion(), inverse: new THREE.Quaternion(), acceleration: new THREE.Vector3(), velocity: new THREE.Vector3() };
13194
+ const holderOnHand = (rig, hand) => {
13195
+ const quaternion2 = hand.quaternion.clone().multiply(rig.grip.quaternion.clone().invert());
13196
+ return { position: hand.position.clone().sub(rig.grip.position.clone().applyQuaternion(quaternion2)), quaternion: quaternion2 };
13197
+ };
13198
+ const relativeTo = (frame, base) => {
13199
+ const inverse = base.quaternion.clone().invert();
13200
+ return { position: frame.position.clone().sub(base.position).applyQuaternion(inverse), quaternion: inverse.multiply(frame.quaternion) };
13201
+ };
13202
+ const inFrame = (local, base) => ({ position: local.position.clone().applyQuaternion(base.quaternion).add(base.position), quaternion: base.quaternion.clone().multiply(local.quaternion) });
13203
+ const holdFrame = () => {
13204
+ const pose2 = options.holdFrame?.();
13205
+ return pose2 ? frameOf(pose2) : null;
13206
+ };
13207
+ const goal = (entry) => {
13208
+ if (entry.motion?.kind === "toss") {
13209
+ const start = entry.motion.start;
13210
+ const ahead = new THREE.Vector3(0, 0, -1).applyQuaternion(holdFrame()?.quaternion ?? start.quaternion).setY(0).normalize();
13211
+ const turn2 = new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3().crossVectors(ahead, new THREE.Vector3(0, 1, 0)).normalize(), HOLSTER_TOSS.turn);
13212
+ return { position: start.position.clone().addScaledVector(ahead, HOLSTER_TOSS.ahead).add(new THREE.Vector3(0, -HOLSTER_TOSS.drop, 0)), quaternion: turn2.multiply(start.quaternion.clone()) };
13213
+ }
13214
+ if (entry.motion?.kind === "back") {
13215
+ const base = holdFrame();
13216
+ return base && entry.taken ? inFrame(entry.taken, base) : null;
13217
+ }
13218
+ const pose2 = options.bodySlot(entry.slot);
13219
+ if (!pose2) return null;
13220
+ return entry.swing ? hanging(entry, frameOf(pose2)) : frameOf(pose2);
13221
+ };
13222
+ const hanging = (entry, frame) => {
13223
+ const forward = new THREE.Vector3(0, 0, 1).applyQuaternion(frame.quaternion);
13224
+ const up = new THREE.Vector3(0, 1, 0).applyQuaternion(frame.quaternion);
13225
+ forward.y = 0;
13226
+ if (forward.lengthSq() < 1e-8) return frame;
13227
+ forward.normalize();
13228
+ entry.lean = Math.atan2(up.dot(forward), up.y);
13229
+ return { position: frame.position, quaternion: new THREE.Quaternion().setFromUnitVectors(new THREE.Vector3(0, 0, 1), forward) };
13230
+ };
13231
+ const place = (entry, frame) => {
13232
+ entry.rig.holder.position.copy(frame.position);
13233
+ entry.rig.holder.quaternion.copy(frame.quaternion);
13234
+ entry.rig.holder.updateMatrixWorld(true);
13235
+ };
13236
+ const swingOf = (entry, frame, dt) => {
13237
+ if (!entry.swing || dt <= 0) return;
13238
+ const { velocity: velocity2, acceleration, inverse } = scratch3;
13239
+ velocity2.copy(frame.position).sub(entry.last ?? frame.position).divideScalar(dt);
13240
+ acceleration.copy(velocity2).sub(entry.last ? entry.speed : velocity2).divideScalar(dt).clampLength(0, MAX_SWING_ACCELERATION);
13241
+ entry.last = (entry.last ?? new THREE.Vector3()).copy(frame.position);
13242
+ entry.speed.copy(velocity2);
13243
+ acceleration.applyQuaternion(inverse.copy(frame.quaternion).invert());
13244
+ const state = entry.swing.step(dt, [-acceleration.x, acceleration.y, acceleration.z]);
13245
+ entry.rig.swing.rotation.set(-(state.forward + Math.max(0, -entry.lean)), 0, -state.side);
13246
+ entry.rig.holder.updateMatrixWorld(true);
13247
+ };
13248
+ const hide = (weapon) => {
13249
+ const entry = away.get(weapon);
13250
+ if (!entry) return;
13251
+ entry.rig.holder.visible = false;
13252
+ away.delete(weapon);
13253
+ };
13254
+ const step = (entry, dt) => {
13255
+ const motion = entry.motion;
13256
+ const target = goal(entry);
13257
+ entry.rig.holder.visible = target !== null;
13258
+ const body = entry.rig.strap[0];
13259
+ if (body) body.visible = motion === null;
13260
+ if (!target) return;
13261
+ if (!motion) {
13262
+ place(entry, target);
13263
+ swingOf(entry, target, dt);
13264
+ return;
13265
+ }
13266
+ motion.time = Math.min(motion.seconds, motion.time + dt);
13267
+ const k = ease4(motion.time / motion.seconds);
13268
+ place(entry, { position: motion.start.position.clone().lerp(target.position, k), quaternion: motion.start.quaternion.clone().slerp(target.quaternion, k) });
13269
+ if (motion.time < motion.seconds) return;
13270
+ if (motion.kind === "away") {
13271
+ entry.motion = null;
13272
+ if (body) body.visible = true;
13273
+ }
13274
+ if (motion.kind === "toss") hide(entry.weapon);
13275
+ };
13276
+ const rigOf = (weapon, slot) => {
13277
+ const key = `${weapon}@${slot}`;
13278
+ const known = rigs.get(key);
13279
+ if (known) return known;
13280
+ const rig = buildHolsterRig(THREE, weapon, slot === GROUND_SLOT ? "hip" : slot, { ...options.items ? { items: options.items } : {}, strapMaterial });
13281
+ if (!rig) return void 0;
13282
+ options.scene.add(rig.holder);
13283
+ rig.holder.visible = false;
13284
+ rigs.set(key, rig);
13285
+ return rig;
13286
+ };
13287
+ const putAway = (event, seconds) => {
13288
+ if (!options.arsenal.weapons.has(event.weapon)) return;
13289
+ hide(event.weapon);
13290
+ const rig = rigOf(event.weapon, event.slot);
13291
+ if (!rig) return;
13292
+ rig.swing.rotation.set(0, 0, 0);
13293
+ const entry = { weapon: event.weapon, slot: event.slot, rig, motion: null, taken: null, swing: event.slot === "sling" ? createSlingSwing() : null, last: null, speed: new THREE.Vector3(), lean: 0 };
13294
+ if (event.from && seconds > 0) {
13295
+ const start = holderOnHand(rig, frameOf(event.from));
13296
+ const base = holdFrame();
13297
+ entry.taken = base ? relativeTo(start, base) : null;
13298
+ entry.motion = { kind: event.slot === GROUND_SLOT ? "toss" : "away", time: 0, seconds, start };
13299
+ } else if (event.slot === GROUND_SLOT) return;
13300
+ away.set(event.weapon, entry);
13301
+ step(entry, 0);
13077
13302
  };
13078
13303
  return {
13079
13304
  handle(event) {
13080
- const key = `${event.weapon}@${event.slot}`;
13081
- if (!event.on) {
13082
- away.delete(event.weapon);
13083
- const holder = models.get(key);
13084
- if (holder) holder.visible = false;
13085
- return;
13086
- }
13087
- if (!options.arsenal.weapons.has(event.weapon)) return;
13088
- if (!models.has(key)) {
13089
- const holder = holsteredModel(THREE, event.weapon, event.slot, options.items);
13090
- if (!holder) return;
13091
- options.scene.add(holder);
13092
- models.set(key, holder);
13093
- }
13094
- away.set(event.weapon, event.slot);
13095
- place(event.weapon, event.slot);
13305
+ const seconds = event.seconds ?? 0;
13306
+ if (event.on) return putAway(event, seconds);
13307
+ const entry = away.get(event.weapon);
13308
+ if (!entry || seconds <= 0 || !entry.taken || !holdFrame()) return hide(event.weapon);
13309
+ entry.motion = { kind: "back", time: 0, seconds, start: { position: entry.rig.holder.position.clone(), quaternion: entry.rig.holder.quaternion.clone() } };
13310
+ entry.swing?.reset();
13311
+ entry.rig.swing.rotation.set(0, 0, 0);
13096
13312
  },
13097
- update() {
13098
- for (const [weapon, slot] of away) place(weapon, slot);
13313
+ inHand(weapon) {
13314
+ if (away.get(weapon)?.motion?.kind !== "toss") hide(weapon);
13315
+ },
13316
+ update(dt = 0) {
13317
+ for (const entry of [...away.values()]) step(entry, dt);
13318
+ },
13319
+ handTargets() {
13320
+ for (const entry of away.values()) {
13321
+ const motion = entry.motion;
13322
+ if (!motion || !entry.rig.holder.visible) continue;
13323
+ if (motion.kind === "toss" && motion.time / motion.seconds > HOLSTER_TOSS.release) continue;
13324
+ const { position, quaternion: quaternion2 } = scratch3;
13325
+ position.copy(entry.rig.grip.position).applyMatrix4(entry.rig.holder.matrixWorld);
13326
+ quaternion2.copy(entry.rig.holder.quaternion).multiply(entry.rig.grip.quaternion);
13327
+ return { R: { position: [position.x, position.y, position.z], quaternion: [quaternion2.x, quaternion2.y, quaternion2.z, quaternion2.w] }, grip: MOVE_GRIP };
13328
+ }
13329
+ return null;
13099
13330
  },
13100
13331
  get drawn() {
13101
- return [...away.keys()];
13332
+ return [...away.values()].filter((entry) => entry.motion?.kind !== "toss").map((entry) => entry.weapon);
13102
13333
  },
13103
13334
  dispose() {
13104
- discardObject3D5([...models.values()]);
13105
- models.clear();
13335
+ discardObject3D5([...rigs.values()].map((rig) => rig.holder));
13336
+ strapMaterial.dispose();
13337
+ rigs.clear();
13106
13338
  away.clear();
13107
13339
  }
13108
13340
  };
@@ -14569,7 +14801,7 @@ function createWeaponsTestModule(options = {}) {
14569
14801
  });
14570
14802
  const worldEffects = createWorldEffects({ arsenal });
14571
14803
  const bodySlot = lent.bodySlot?.bind(lent);
14572
- const holstered = bodySlot ? createHolsteredWeapons({ THREE: lent.THREE, scene: lent.scene, arsenal, ...options.items ? { items: options.items } : {}, bodySlot }) : null;
14804
+ const holstered = bodySlot ? createHolsteredWeapons({ THREE: lent.THREE, scene: lent.scene, arsenal, ...options.items ? { items: options.items } : {}, bodySlot, holdFrame: () => lent.holdFrame?.("R") ?? null }) : null;
14573
14805
  const offEvents = lent.on((event) => {
14574
14806
  worldEffects.handle(event, lent.effects);
14575
14807
  if (event.type === "holster") {
@@ -14583,6 +14815,7 @@ function createWeaponsTestModule(options = {}) {
14583
14815
  return;
14584
14816
  }
14585
14817
  const tool = lent.tool();
14818
+ if (event.type === "tool") holstered?.inHand(event.weapon);
14586
14819
  created.handle(event, tool);
14587
14820
  if (event.type === "reload" && event.phase === "start") {
14588
14821
  const clip = created.clips.find((entry) => entry.name.startsWith("reload"));
@@ -14595,9 +14828,9 @@ function createWeaponsTestModule(options = {}) {
14595
14828
  });
14596
14829
  const offFrame = lent.onFrame((dt) => {
14597
14830
  created.update(dt);
14598
- holstered?.update();
14831
+ holstered?.update(dt);
14599
14832
  });
14600
- const offHands = lent.setHandTargets?.(() => created.handTargets());
14833
+ const offHands = lent.setHandTargets?.(() => holstered?.handTargets() ?? created.handTargets());
14601
14834
  const offMuzzle = lent.setMuzzleSource?.((hand) => created.muzzle(hand));
14602
14835
  created.handle({ type: "tool", weapon: lent.tool().weapon }, lent.tool());
14603
14836
  return () => {
@@ -15741,12 +15974,21 @@ export {
15741
15974
  buildCartridge,
15742
15975
  buildCasing,
15743
15976
  buildRound,
15977
+ LOOSE_ROLLING_RESISTANCE,
15978
+ casingCollider,
15979
+ spawnLoose,
15980
+ syncLoose,
15981
+ dropLoose,
15744
15982
  VIEW_MOTION,
15745
15983
  createWeaponFx,
15746
15984
  buildModeledProp,
15747
15985
  buildTripodMountedProp,
15748
15986
  modeledPropSpec,
15749
15987
  HOLSTER_PLACEMENT,
15988
+ SLING_HANG,
15989
+ buildHolsterRig,
15990
+ GROUND_SLOT,
15991
+ HOLSTER_TOSS,
15750
15992
  holsteredModel,
15751
15993
  createHolsteredWeapons,
15752
15994
  WEAPON_THUMBNAILS,
@@ -15773,4 +16015,4 @@ export {
15773
16015
  DEFAULT_EXPLOSIVES_SCENE,
15774
16016
  createExplosivesTestModule
15775
16017
  };
15776
- //# sourceMappingURL=chunk-UUBO6H5O.js.map
16018
+ //# sourceMappingURL=chunk-DCMD54HC.js.map