@adatechnology/game-player 0.9.1 → 0.10.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.
@@ -26,27 +26,48 @@ var GamePlayer = (() => {
26
26
  DOOR_USE_DEFAULTS: () => DOOR_USE_DEFAULTS,
27
27
  FURNITURE_PUSH_DEFAULTS: () => FURNITURE_PUSH_DEFAULTS,
28
28
  HANDS_DEFAULTS: () => HANDS_DEFAULTS,
29
+ HANDS_TIMING: () => HANDS_TIMING,
30
+ ITEM_GRIP: () => ITEM_GRIP,
29
31
  PLAYER_DEFAULTS: () => PLAYER_DEFAULTS,
32
+ PLAYER_MASS_KG: () => PLAYER_MASS_KG,
33
+ PLAYER_PUSH_STRENGTH_NEWTONS: () => PLAYER_PUSH_STRENGTH_NEWTONS,
30
34
  PLAYER_TEXT: () => PLAYER_TEXT,
35
+ SLING_SLOT: () => SLING_SLOT,
36
+ SLOT_FALLBACKS: () => SLOT_FALLBACKS,
31
37
  STEP_POSE_DEFAULTS: () => STEP_POSE_DEFAULTS,
38
+ blendPoses: () => blendPoses,
32
39
  canPush: () => canPush,
40
+ composePose: () => composePose,
41
+ conjugateQuaternion: () => conjugateQuaternion,
33
42
  createDoorUse: () => createDoorUse,
34
43
  createFurniturePush: () => createFurniturePush,
44
+ createPhysicsController: () => createPhysicsController,
35
45
  createPlayer: () => createPlayer,
36
46
  createPlayerTestModule: () => createPlayerTestModule,
37
47
  createRng: () => createRng,
38
48
  createStepPose: () => createStepPose,
49
+ createTakeTimeline: () => createTakeTimeline,
39
50
  deriveVitals: () => deriveVitals,
40
51
  dragMarkFor: () => dragMarkFor,
41
52
  drawBackOf: () => drawBackOf,
42
53
  drawWound: () => drawWound,
54
+ easeInOut: () => easeInOut,
43
55
  frictionFor: () => frictionFor,
56
+ gripOfItem: () => gripOfItem,
44
57
  gripSecondsFor: () => gripSecondsFor,
58
+ handPoseOnItem: () => handPoseOnItem,
45
59
  interactionHandsFor: () => interactionHandsFor,
60
+ itemPoseInHand: () => itemPoseInHand,
61
+ multiplyQuaternions: () => multiplyQuaternions,
62
+ planDrawSequence: () => planDrawSequence,
46
63
  planHands: () => planHands,
64
+ planTakeSequence: () => planTakeSequence,
47
65
  playerTextOf: () => playerTextOf,
66
+ poseHolding: () => poseHolding,
48
67
  pushAccelerationFor: () => pushAccelerationFor,
49
68
  pushSpeedFor: () => pushSpeedFor,
69
+ quaternionOfBasis: () => quaternionOfBasis,
70
+ rotateVector: () => rotateVector,
50
71
  slipsOnTop: () => slipsOnTop,
51
72
  swingSecondsFor: () => swingSecondsFor
52
73
  });
@@ -438,6 +459,7 @@ var GamePlayer = (() => {
438
459
  const fist = options.fist ?? PLAYER_DEFAULTS.fist;
439
460
  const listeners = /* @__PURE__ */ new Map();
440
461
  let P = freshState(derived, options.position);
462
+ const feet = new Float64Array(3);
441
463
  let defaultWorld = options.world ?? {};
442
464
  let world = defaultWorld;
443
465
  const emit = (event, payload) => {
@@ -671,9 +693,10 @@ var GamePlayer = (() => {
671
693
  const k = 1 - P.vaultTimer / numbers.vaultSeconds;
672
694
  P.x = lerp(P.vault[0], P.vault[2], k);
673
695
  P.z = lerp(P.vault[1], P.vault[3], k);
674
- P.y = Math.sin(k * Math.PI) * numbers.vaultHeight;
696
+ P.y = feet[1] + Math.sin(k * Math.PI) * numbers.vaultHeight;
675
697
  if (P.vaultTimer <= 0) {
676
- P.y = 0;
698
+ P.y = feet[1];
699
+ world.controller?.teleport(P.x, P.y, P.z);
677
700
  noise(P.x, P.z, numbers.vaultNoise);
678
701
  const surface = surfaceHere();
679
702
  sound("step", numbers.vaultLandVolume, [landSoundId(surface, 0)], surface);
@@ -725,11 +748,20 @@ var GamePlayer = (() => {
725
748
  const tvz = want ? mz / ml * sp : 0;
726
749
  P.vx = lerp(P.vx, tvx, Math.min(1, dt * numbers.acceleration));
727
750
  P.vz = lerp(P.vz, tvz, Math.min(1, dt * numbers.acceleration));
728
- const r = numbers.radius * heightScale;
729
- const nx = P.x + P.vx * dt;
730
- const nz = P.z + P.vz * dt;
731
- if (!blocked(nx + Math.sign(P.vx) * r, P.z)) P.x = nx;
732
- if (!blocked(P.x, nz + Math.sign(P.vz) * r)) P.z = nz;
751
+ const controller = world.controller;
752
+ if (controller) {
753
+ controller.move(P.vx, P.vz, dt);
754
+ controller.read(feet);
755
+ P.x = feet[0] ?? P.x;
756
+ P.y = feet[1] ?? P.y;
757
+ P.z = feet[2] ?? P.z;
758
+ } else {
759
+ const r = numbers.radius * heightScale;
760
+ const nx = P.x + P.vx * dt;
761
+ const nz = P.z + P.vz * dt;
762
+ if (!blocked(nx + Math.sign(P.vx) * r, P.z)) P.x = nx;
763
+ if (!blocked(P.x, nz + Math.sign(P.vz) * r)) P.z = nz;
764
+ }
733
765
  world.afterMove?.(P);
734
766
  if (crowd) {
735
767
  const n = crowd.count();
@@ -838,6 +870,7 @@ var GamePlayer = (() => {
838
870
  save: () => ({ state: structuredClone(P), attributes }),
839
871
  load(save) {
840
872
  P = structuredClone(save.state);
873
+ world.controller?.teleport(P.x, P.y, P.z);
841
874
  },
842
875
  setWorld(next) {
843
876
  defaultWorld = next;
@@ -845,6 +878,29 @@ var GamePlayer = (() => {
845
878
  };
846
879
  }
847
880
 
881
+ // src/physics-controller.ts
882
+ var PLAYER_MASS_KG = 75;
883
+ var PLAYER_PUSH_STRENGTH_NEWTONS = 400;
884
+ var createPhysicsController = (world, options) => {
885
+ const { radiusMetres, heightMetres } = options.collider;
886
+ const id = world.createCharacter({
887
+ position: options.position,
888
+ radius: radiusMetres,
889
+ halfHeight: Math.max(0, (heightMetres - 2 * radiusMetres) / 2),
890
+ mass: options.mass ?? PLAYER_MASS_KG,
891
+ strength: options.strength ?? PLAYER_PUSH_STRENGTH_NEWTONS
892
+ });
893
+ return {
894
+ id,
895
+ move: (velocityX, velocityZ, dt) => world.moveCharacter(id, velocityX, velocityZ, dt),
896
+ read: (out) => world.readCharacter(id, out, 0),
897
+ teleport: (x, y, z) => world.teleportCharacter(id, [x, y, z]),
898
+ push: (impulse) => world.pushCharacter(id, impulse),
899
+ supported: () => world.characterSupported(id),
900
+ dispose: () => world.removeCharacter(id)
901
+ };
902
+ };
903
+
848
904
  // src/furniture-push.ts
849
905
  var FURNITURE_PUSH_DEFAULTS = {
850
906
  forceN: 500,
@@ -1566,6 +1622,8 @@ var GamePlayer = (() => {
1566
1622
 
1567
1623
  // src/hands.ts
1568
1624
  var HANDS_DEFAULTS = { oneHand: { maxKg: 4, maxLength: 0.7 } };
1625
+ var SLOT_FALLBACKS = { sling: ["back"] };
1626
+ var SLING_SLOT = "sling";
1569
1627
  function interactionHandsFor(thing, rules = HANDS_DEFAULTS) {
1570
1628
  const longest = Math.max(0, ...thing.size ?? []);
1571
1629
  return thing.massKg > rules.oneHand.maxKg || longest > rules.oneHand.maxLength ? 2 : 1;
@@ -1574,11 +1632,13 @@ var GamePlayer = (() => {
1574
1632
  function planHands(params) {
1575
1633
  const { R, L } = params.weapons;
1576
1634
  const free = [...params.slots];
1635
+ const fallbacks = params.fallbacks ?? SLOT_FALLBACKS;
1577
1636
  const stow = (hand, weapon) => {
1578
- const at = weapon.slot === null ? -1 : free.indexOf(weapon.slot);
1579
- if (at < 0) return { hand, weapon: weapon.id, move: "drop" };
1580
- free.splice(at, 1);
1581
- return { hand, weapon: weapon.id, move: "holster", slot: weapon.slot };
1637
+ const tried = weapon.slot === null ? [] : [weapon.slot, ...fallbacks[weapon.slot] ?? []];
1638
+ const slot = tried.find((name) => free.includes(name));
1639
+ if (slot === void 0) return { hand, weapon: weapon.id, move: "drop" };
1640
+ free.splice(free.indexOf(slot), 1);
1641
+ return { hand, weapon: weapon.id, move: slot === SLING_SLOT ? "sling" : "holster", slot };
1582
1642
  };
1583
1643
  if (params.hands === 2) {
1584
1644
  const moves = [...holds(R) ? [stow("R", R)] : [], ...holds(L) ? [stow("L", L)] : []];
@@ -1592,6 +1652,161 @@ var GamePlayer = (() => {
1592
1652
  return plan.moves.filter((move) => move.move === "holster").map((move) => ({ hand: move.hand, weapon: move.weapon }));
1593
1653
  }
1594
1654
 
1655
+ // src/hand-math.ts
1656
+ function quaternionOfBasis(x, y, z) {
1657
+ const [m00, m10, m20] = [x[0] ?? 0, x[1] ?? 0, x[2] ?? 0];
1658
+ const [m01, m11, m21] = [y[0] ?? 0, y[1] ?? 0, y[2] ?? 0];
1659
+ const [m02, m12, m22] = [z[0] ?? 0, z[1] ?? 0, z[2] ?? 0];
1660
+ const trace = m00 + m11 + m22;
1661
+ if (trace > 0) {
1662
+ const s2 = 0.5 / Math.sqrt(trace + 1);
1663
+ return [(m21 - m12) * s2, (m02 - m20) * s2, (m10 - m01) * s2, 0.25 / s2];
1664
+ }
1665
+ if (m00 > m11 && m00 > m22) {
1666
+ const s2 = 2 * Math.sqrt(1 + m00 - m11 - m22);
1667
+ return [0.25 * s2, (m01 + m10) / s2, (m02 + m20) / s2, (m21 - m12) / s2];
1668
+ }
1669
+ if (m11 > m22) {
1670
+ const s2 = 2 * Math.sqrt(1 + m11 - m00 - m22);
1671
+ return [(m01 + m10) / s2, 0.25 * s2, (m12 + m21) / s2, (m02 - m20) / s2];
1672
+ }
1673
+ const s = 2 * Math.sqrt(1 + m22 - m00 - m11);
1674
+ return [(m02 + m20) / s, (m12 + m21) / s, 0.25 * s, (m10 - m01) / s];
1675
+ }
1676
+ function multiplyQuaternions(a, b) {
1677
+ const [ax, ay, az, aw] = a;
1678
+ const [bx, by, bz, bw] = b;
1679
+ return [ax * bw + aw * bx + ay * bz - az * by, ay * bw + aw * by + az * bx - ax * bz, az * bw + aw * bz + ax * by - ay * bx, aw * bw - ax * bx - ay * by - az * bz];
1680
+ }
1681
+ function conjugateQuaternion(q) {
1682
+ return [-q[0], -q[1], -q[2], q[3]];
1683
+ }
1684
+ function rotateVector(q, v) {
1685
+ const [x, y, z, w] = q;
1686
+ const tx = 2 * (y * v[2] - z * v[1]);
1687
+ const ty = 2 * (z * v[0] - x * v[2]);
1688
+ const tz = 2 * (x * v[1] - y * v[0]);
1689
+ return [v[0] + w * tx + (y * tz - z * ty), v[1] + w * ty + (z * tx - x * tz), v[2] + w * tz + (x * ty - y * tx)];
1690
+ }
1691
+ function composePose(parent, local) {
1692
+ const offset = rotateVector(parent.quaternion, local.position);
1693
+ return {
1694
+ position: [parent.position[0] + offset[0], parent.position[1] + offset[1], parent.position[2] + offset[2]],
1695
+ quaternion: multiplyQuaternions(parent.quaternion, local.quaternion)
1696
+ };
1697
+ }
1698
+ function poseHolding(world, local) {
1699
+ const quaternion = multiplyQuaternions(world.quaternion, conjugateQuaternion(local.quaternion));
1700
+ const offset = rotateVector(quaternion, local.position);
1701
+ return { position: [world.position[0] - offset[0], world.position[1] - offset[1], world.position[2] - offset[2]], quaternion };
1702
+ }
1703
+ function blendPoses(a, b, k) {
1704
+ const lerp2 = (i) => a.position[i] + (b.position[i] - a.position[i]) * k;
1705
+ const dot = a.quaternion[0] * b.quaternion[0] + a.quaternion[1] * b.quaternion[1] + a.quaternion[2] * b.quaternion[2] + a.quaternion[3] * b.quaternion[3];
1706
+ const sign = dot < 0 ? -1 : 1;
1707
+ const mixed = [0, 1, 2, 3].map((i) => (a.quaternion[i] ?? 0) * (1 - k) + (b.quaternion[i] ?? 0) * sign * k);
1708
+ const length = Math.hypot(...mixed) || 1;
1709
+ return { position: [lerp2(0), lerp2(1), lerp2(2)], quaternion: [(mixed[0] ?? 0) / length, (mixed[1] ?? 0) / length, (mixed[2] ?? 0) / length, (mixed[3] ?? 1) / length] };
1710
+ }
1711
+ function easeInOut(t) {
1712
+ const k = Math.min(1, Math.max(0, t));
1713
+ return k * k * (3 - 2 * k);
1714
+ }
1715
+
1716
+ // src/item-grip.ts
1717
+ var ITEM_GRIP = {
1718
+ /** Half the thickness of a palm (m): what lies between the middle of the palm and the thing. */
1719
+ palm: 0.011,
1720
+ /** The closure of the fingers (0 open, 1 a fist) on a thing this thick (m) or thinner. */
1721
+ fistAt: 0.03,
1722
+ closed: 0.95,
1723
+ /** How much the closure opens per metre the thing is thicker than `fistAt`, and the most it opens to. */
1724
+ openPerMetre: 4,
1725
+ open: 0.35,
1726
+ /** The fingers before they close on it, as the hand comes to the thing. */
1727
+ reaching: 0.15
1728
+ };
1729
+ var AXES = [
1730
+ [1, 0, 0],
1731
+ [0, 1, 0],
1732
+ [0, 0, 1]
1733
+ ];
1734
+ var cross = (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];
1735
+ var scaled = (v, k) => [v[0] * k, v[1] * k, v[2] * k];
1736
+ function gripOfItem(size, hand) {
1737
+ const sides = [0, 1, 2].map((axis) => ({ axis, length: Math.max(0, size[axis] ?? 0) })).sort((a, b) => b.length - a.length);
1738
+ const [long, mid, thin] = sides;
1739
+ const thumb = hand === "R" ? [0, 0, -1] : [0, 0, 1];
1740
+ const columns = [AXES[0], AXES[1], AXES[2]];
1741
+ columns[long.axis] = thumb;
1742
+ columns[thin.axis] = [0, 1, 0];
1743
+ const next = (axis) => (axis + 1) % 3;
1744
+ const before = columns[next(next(mid.axis))];
1745
+ const after = columns[next(mid.axis)];
1746
+ columns[mid.axis] = cross(after, before);
1747
+ const quaternion = quaternionOfBasis(columns[0], columns[1], columns[2]);
1748
+ const depth = ITEM_GRIP.palm + thin.length / 2;
1749
+ const across = (mid.length + thin.length) / 2;
1750
+ const grip = Math.max(ITEM_GRIP.open, Math.min(ITEM_GRIP.closed, ITEM_GRIP.closed - ITEM_GRIP.openPerMetre * Math.max(0, across - ITEM_GRIP.fistAt)));
1751
+ return { local: { position: scaled([0, -1, 0], depth), quaternion }, grip };
1752
+ }
1753
+ function itemPoseInHand(hand, grip) {
1754
+ return composePose(hand, grip.local);
1755
+ }
1756
+ function handPoseOnItem(item, grip) {
1757
+ return poseHolding(item, grip.local);
1758
+ }
1759
+
1760
+ // src/take-sequence.ts
1761
+ var HANDS_TIMING = { stow: 0.5, sling: 0.45, drop: 0.4, reach: 0.38, close: 0.16, lift: 0.3, draw: 0.5 };
1762
+ var KIND_OF_MOVE = { holster: "stow", sling: "sling", drop: "drop" };
1763
+ var lay = (parts) => {
1764
+ let at = 0;
1765
+ const steps = parts.map((part) => {
1766
+ const step = { kind: part.kind, start: at, end: at + part.seconds, ...part.move ? { move: part.move } : {} };
1767
+ at = step.end;
1768
+ return step;
1769
+ });
1770
+ return { steps, duration: at };
1771
+ };
1772
+ function planTakeSequence(plan, options = {}) {
1773
+ const timing = options.timing ?? HANDS_TIMING;
1774
+ const moves = [...plan.moves].reverse().map((move) => ({ kind: KIND_OF_MOVE[move.move], seconds: timing[KIND_OF_MOVE[move.move]], move }));
1775
+ const take = options.take === false ? [] : [
1776
+ { kind: "reach", seconds: timing.reach },
1777
+ { kind: "close", seconds: timing.close },
1778
+ { kind: "lift", seconds: timing.lift }
1779
+ ];
1780
+ return lay([...moves, ...take]);
1781
+ }
1782
+ function planDrawSequence(moves, timing = HANDS_TIMING) {
1783
+ return lay(moves.map((move) => ({ kind: "draw", seconds: timing.draw, move })));
1784
+ }
1785
+ function createTakeTimeline(sequence) {
1786
+ let time = 0;
1787
+ let begun = 0;
1788
+ let over = 0;
1789
+ const steps = sequence.steps;
1790
+ return {
1791
+ sequence,
1792
+ get time() {
1793
+ return time;
1794
+ },
1795
+ advance(dt) {
1796
+ time = Math.min(sequence.duration, time + Math.max(0, dt));
1797
+ const startedFrom = begun;
1798
+ while (begun < steps.length && steps[begun].start <= time) begun++;
1799
+ const endedFrom = over;
1800
+ while (over < begun && steps[over].end <= time) over++;
1801
+ const started = steps.slice(startedFrom, begun);
1802
+ const ended = steps.slice(endedFrom, over);
1803
+ const step = steps.find((entry) => time < entry.end) ?? null;
1804
+ const progress = step ? easeInOut((time - step.start) / Math.max(1e-6, step.end - step.start)) : 1;
1805
+ return { step, progress, started, ended, done: time >= sequence.duration };
1806
+ }
1807
+ };
1808
+ }
1809
+
1595
1810
  // src/step-pose.ts
1596
1811
  var STEP_POSE_DEFAULTS = { teleportDistance: 3 };
1597
1812
  function createStepPose(start, options = {}) {