@adatechnology/game-player 0.13.5 → 0.14.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.d.ts +166 -5
- package/dist/index.global.js +204 -2
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +204 -2
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -467,11 +467,13 @@ function createDoorUse(rules = {}) {
|
|
|
467
467
|
}
|
|
468
468
|
|
|
469
469
|
// src/hands.ts
|
|
470
|
-
var
|
|
470
|
+
var HANDS_BY_HANDLE = { maxKg: 25 };
|
|
471
|
+
var HANDS_DEFAULTS = { oneHand: { maxKg: 4, maxLength: 0.7 }, byHandle: HANDS_BY_HANDLE };
|
|
471
472
|
var SLOT_FALLBACKS = { sling: ["back"], hip: ["belt"] };
|
|
472
473
|
var BELT_SLOT = "belt";
|
|
473
474
|
var SLING_SLOT = "sling";
|
|
474
475
|
function interactionHandsFor(thing, rules = HANDS_DEFAULTS) {
|
|
476
|
+
if (thing.handle) return thing.massKg > (rules.byHandle ?? HANDS_BY_HANDLE).maxKg ? 2 : 1;
|
|
475
477
|
const longest = Math.max(0, ...thing.size ?? []);
|
|
476
478
|
return thing.massKg > rules.oneHand.maxKg || longest > rules.oneHand.maxLength ? 2 : 1;
|
|
477
479
|
}
|
|
@@ -593,7 +595,16 @@ var AXES = [
|
|
|
593
595
|
];
|
|
594
596
|
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]];
|
|
595
597
|
var scaled = (v, k) => [v[0] * k, v[1] * k, v[2] * k];
|
|
596
|
-
function
|
|
598
|
+
function gripOnHandle(handle, hand) {
|
|
599
|
+
const up = hand === "R" ? [0, 0, -1] : [0, 0, 1];
|
|
600
|
+
const along = [1, 0, 0];
|
|
601
|
+
const quaternion = quaternionOfBasis(along, up, cross(along, up));
|
|
602
|
+
const local = { position: [0, 0, 0], quaternion };
|
|
603
|
+
const handleInHand = composePose(local, { position: [handle[0] ?? 0, handle[1] ?? 0, handle[2] ?? 0], quaternion: [0, 0, 0, 1] }).position;
|
|
604
|
+
return { local: { position: [-handleInHand[0], -handleInHand[1] - ITEM_GRIP.palm, -handleInHand[2]], quaternion }, grip: ITEM_GRIP.closed, hanging: true };
|
|
605
|
+
}
|
|
606
|
+
function gripOfItem(size, hand, handle) {
|
|
607
|
+
if (handle) return gripOnHandle(handle, hand);
|
|
597
608
|
const sides = [0, 1, 2].map((axis) => ({ axis, length: Math.max(0, size[axis] ?? 0) })).sort((a, b) => b.length - a.length);
|
|
598
609
|
const [long, mid, thin] = sides;
|
|
599
610
|
const thumb = hand === "R" ? [0, 0, -1] : [0, 0, 1];
|
|
@@ -689,12 +700,197 @@ function createStepPose(start, options = {}) {
|
|
|
689
700
|
}
|
|
690
701
|
};
|
|
691
702
|
}
|
|
703
|
+
|
|
704
|
+
// src/gesture.ts
|
|
705
|
+
var GESTURE_TIMING = {
|
|
706
|
+
reach: 0.32,
|
|
707
|
+
back: 0.3,
|
|
708
|
+
act: { press: 0.22, turn: 0.4, flip: 0.2, strike: 0.35, pour: 0.7, put_in: 0.3, tip: 0.7, stow: 0.25, read: 1.2, pull: 0.45 }
|
|
709
|
+
};
|
|
710
|
+
var ACTS_AT_START = /* @__PURE__ */ new Set(["read"]);
|
|
711
|
+
var GESTURE_ARM_REACH = 0.62;
|
|
712
|
+
function createGestureTimeline(gesture, timing = GESTURE_TIMING) {
|
|
713
|
+
const reach = timing.reach;
|
|
714
|
+
const act = timing.act[gesture];
|
|
715
|
+
const duration = reach + act + timing.back;
|
|
716
|
+
const actsAt = ACTS_AT_START.has(gesture) ? reach : reach + act;
|
|
717
|
+
let time = 0;
|
|
718
|
+
let fired = false;
|
|
719
|
+
return {
|
|
720
|
+
gesture,
|
|
721
|
+
duration,
|
|
722
|
+
advance(dt) {
|
|
723
|
+
time = Math.min(duration, time + Math.max(0, dt));
|
|
724
|
+
const acted = !fired && time >= actsAt;
|
|
725
|
+
if (acted) fired = true;
|
|
726
|
+
const phase = time < reach ? "reach" : time < reach + act ? "act" : "back";
|
|
727
|
+
const start = phase === "reach" ? 0 : phase === "act" ? reach : reach + act;
|
|
728
|
+
const length = phase === "reach" ? reach : phase === "act" ? act : timing.back;
|
|
729
|
+
return { phase, progress: easeInOut((time - start) / Math.max(1e-6, length)), acted, done: time >= duration };
|
|
730
|
+
}
|
|
731
|
+
};
|
|
732
|
+
}
|
|
733
|
+
var WITH_THE_HELD_THING = /* @__PURE__ */ new Set(["put_in", "pour"]);
|
|
734
|
+
function freeHandOf(busy) {
|
|
735
|
+
if (!busy.R) return "R";
|
|
736
|
+
return busy.L ? null : "L";
|
|
737
|
+
}
|
|
738
|
+
function gestureHandOf(choice) {
|
|
739
|
+
if (WITH_THE_HELD_THING.has(choice.gesture)) return choice.carrying ?? null;
|
|
740
|
+
return freeHandOf(choice.busy);
|
|
741
|
+
}
|
|
742
|
+
var add = (a, b, k = 1) => [a[0] + b[0] * k, a[1] + b[1] * k, a[2] + b[2] * k];
|
|
743
|
+
var cross2 = (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]];
|
|
744
|
+
var axisTurn = (axis, angle) => {
|
|
745
|
+
const s = Math.sin(angle / 2);
|
|
746
|
+
return [axis[0] * s, axis[1] * s, axis[2] * s, Math.cos(angle / 2)];
|
|
747
|
+
};
|
|
748
|
+
var UP = [0, 1, 0];
|
|
749
|
+
var actPose = (gesture, frame, k) => {
|
|
750
|
+
const { at, way, side, q } = frame;
|
|
751
|
+
switch (gesture) {
|
|
752
|
+
case "press":
|
|
753
|
+
return { position: add(at, way, -0.06 + 0.05 * Math.sin(Math.PI * k)), quaternion: q };
|
|
754
|
+
case "turn":
|
|
755
|
+
return { position: add(at, way, -0.02), quaternion: multiplyQuaternions(axisTurn(way, -k * Math.PI * 0.5), q) };
|
|
756
|
+
case "flip":
|
|
757
|
+
return { position: add(add(at, way, -0.02), UP, -0.03 * k), quaternion: q };
|
|
758
|
+
case "strike":
|
|
759
|
+
return { position: add(add(at, way, -0.04), side, 0.08 * (k - 0.5)), quaternion: q };
|
|
760
|
+
case "pour":
|
|
761
|
+
return { position: add(at, UP, 0.15), quaternion: multiplyQuaternions(axisTurn(side, -k * Math.PI * 0.4), q) };
|
|
762
|
+
case "tip":
|
|
763
|
+
return { position: add(at, UP, 0.05 * k), quaternion: multiplyQuaternions(axisTurn(side, -k * Math.PI * 0.55), q) };
|
|
764
|
+
case "pull":
|
|
765
|
+
return { position: add(at, way, -0.02 - 0.15 * k), quaternion: q };
|
|
766
|
+
case "put_in":
|
|
767
|
+
case "stow":
|
|
768
|
+
return { position: [at[0], at[1], at[2]], quaternion: q };
|
|
769
|
+
case "read":
|
|
770
|
+
return { position: [at[0], at[1], at[2]], quaternion: multiplyQuaternions(axisTurn(way, Math.PI * 0.5), q) };
|
|
771
|
+
}
|
|
772
|
+
};
|
|
773
|
+
var GRIP = {
|
|
774
|
+
press: [0.85, 0.85],
|
|
775
|
+
turn: [0.9, 0.9],
|
|
776
|
+
flip: [0.55, 0.55],
|
|
777
|
+
strike: [0.95, 0.95],
|
|
778
|
+
pour: [1, 1],
|
|
779
|
+
tip: [1, 1],
|
|
780
|
+
put_in: [1, 0.15],
|
|
781
|
+
stow: [0.2, 0.95],
|
|
782
|
+
read: [0.7, 0.7],
|
|
783
|
+
pull: [0.9, 0.9]
|
|
784
|
+
};
|
|
785
|
+
var OPEN_HAND = 0.2;
|
|
786
|
+
function frameOf(params) {
|
|
787
|
+
const { shoulder } = params;
|
|
788
|
+
const flatX = params.at[0] - shoulder[0];
|
|
789
|
+
const flatZ = params.at[2] - shoulder[2];
|
|
790
|
+
const flat = Math.hypot(flatX, flatZ);
|
|
791
|
+
const way = flat < 1e-4 ? [-Math.sin(params.yaw), 0, -Math.cos(params.yaw)] : [flatX / flat, 0, flatZ / flat];
|
|
792
|
+
const little = params.hand === "R" ? [0, -1, 0] : [0, 1, 0];
|
|
793
|
+
const side = cross2(little, way);
|
|
794
|
+
const toward = [params.at[0] - shoulder[0], params.at[1] - shoulder[1], params.at[2] - shoulder[2]];
|
|
795
|
+
const reach = Math.hypot(toward[0], toward[1], toward[2]);
|
|
796
|
+
const at = reach > GESTURE_ARM_REACH ? add(shoulder, toward, GESTURE_ARM_REACH / reach) : params.at;
|
|
797
|
+
return { at, way, side, q: quaternionOfBasis(way, side, little) };
|
|
798
|
+
}
|
|
799
|
+
function gestureHandPose(params) {
|
|
800
|
+
const { rest, phase, progress, gesture } = params;
|
|
801
|
+
const frame = frameOf(params);
|
|
802
|
+
const [gripIn, gripOut] = GRIP[gesture];
|
|
803
|
+
const index = gesture === "press" ? 0 : gesture === "flip" ? 0.2 : 1;
|
|
804
|
+
if (phase === "reach") return { pose: blendPoses(rest, actPose(gesture, frame, 0), progress), grip: OPEN_HAND + (gripIn - OPEN_HAND) * progress, index: 1 + (index - 1) * progress };
|
|
805
|
+
if (phase === "act") return { pose: actPose(gesture, frame, progress), grip: gripIn + (gripOut - gripIn) * progress, index };
|
|
806
|
+
const stowed = gesture === "stow";
|
|
807
|
+
const home = stowed ? { position: add(rest.position, UP, -0.3), quaternion: rest.quaternion } : rest;
|
|
808
|
+
const grip = stowed ? gripOut : gripOut + (OPEN_HAND - gripOut) * progress;
|
|
809
|
+
return { pose: blendPoses(actPose(gesture, frame, 1), home, progress), grip, index: index + (1 - index) * progress };
|
|
810
|
+
}
|
|
811
|
+
|
|
812
|
+
// src/gesture-hands.ts
|
|
813
|
+
var STAND_OFF = 0.5;
|
|
814
|
+
var STEP_SPEED = 2.5;
|
|
815
|
+
var MAX_WALK_SECONDS = 1.5;
|
|
816
|
+
var TURN_RATE = 8;
|
|
817
|
+
var STOOP_BELOW = 0.5;
|
|
818
|
+
function stepUp(body, now, dt) {
|
|
819
|
+
if (!body) return true;
|
|
820
|
+
const point = now.start.at();
|
|
821
|
+
body.face(point, Math.min(1, dt * TURN_RATE));
|
|
822
|
+
const [x, z] = body.at();
|
|
823
|
+
const dx = point[0] - x;
|
|
824
|
+
const dz = point[2] - z;
|
|
825
|
+
const d = Math.hypot(dx, dz);
|
|
826
|
+
if (d <= STAND_OFF + 0.03 || now.walked > MAX_WALK_SECONDS) return true;
|
|
827
|
+
const go = Math.min(d - STAND_OFF, STEP_SPEED * dt);
|
|
828
|
+
if (go < 1e-4) return false;
|
|
829
|
+
body.moveBy(dx / d * go, dz / d * go);
|
|
830
|
+
const [x1, z1] = body.at();
|
|
831
|
+
now.walked += dt;
|
|
832
|
+
return Math.hypot(x1 - x, z1 - z) < go * 0.2;
|
|
833
|
+
}
|
|
834
|
+
function createGestureHands(deps) {
|
|
835
|
+
let going = null;
|
|
836
|
+
const stop = () => {
|
|
837
|
+
going = null;
|
|
838
|
+
deps.body?.stoop?.(false);
|
|
839
|
+
};
|
|
840
|
+
return {
|
|
841
|
+
get busy() {
|
|
842
|
+
return going !== null;
|
|
843
|
+
},
|
|
844
|
+
get hand() {
|
|
845
|
+
return going?.hand ?? null;
|
|
846
|
+
},
|
|
847
|
+
start(start) {
|
|
848
|
+
if (going) return { ok: false, reason: "busy" };
|
|
849
|
+
const hand = gestureHandOf({ busy: deps.holding(), gesture: start.gesture, carrying: deps.carrying?.() ?? null });
|
|
850
|
+
if (!hand) return { ok: false, reason: "hands_full" };
|
|
851
|
+
const shoulder = deps.shoulder(hand);
|
|
852
|
+
if (!shoulder || deps.body?.able?.() === false) return { ok: false, reason: "no_body" };
|
|
853
|
+
const timeline = createGestureTimeline(start.gesture);
|
|
854
|
+
going = { start, hand, timeline, progress: timeline.advance(0), walked: 0, there: false };
|
|
855
|
+
if (shoulder[1] - start.at()[1] > STOOP_BELOW) deps.body?.stoop?.(true);
|
|
856
|
+
return { ok: true, hand };
|
|
857
|
+
},
|
|
858
|
+
step(dt) {
|
|
859
|
+
if (!going) return;
|
|
860
|
+
const now = going;
|
|
861
|
+
if (deps.body?.able?.() === false) return stop();
|
|
862
|
+
if (!now.there) {
|
|
863
|
+
now.there = stepUp(deps.body, now, dt);
|
|
864
|
+
if (!now.there) return;
|
|
865
|
+
}
|
|
866
|
+
now.progress = now.timeline.advance(dt);
|
|
867
|
+
if (now.progress.acted) now.start.onAct();
|
|
868
|
+
if (now.progress.done && going === now) stop();
|
|
869
|
+
},
|
|
870
|
+
targets(base) {
|
|
871
|
+
if (!going) return base;
|
|
872
|
+
const { hand, start, progress } = going;
|
|
873
|
+
const shoulder = deps.shoulder(hand);
|
|
874
|
+
if (!shoulder) return base;
|
|
875
|
+
const rest = base?.[hand] ?? deps.restOf(hand);
|
|
876
|
+
const worked = gestureHandPose({ gesture: start.gesture, hand, phase: progress.phase, progress: progress.progress, shoulder, rest, at: start.at(), yaw: deps.yaw() });
|
|
877
|
+
const [x, y, z] = worked.pose.position;
|
|
878
|
+
const [qx, qy, qz, qw] = worked.pose.quaternion;
|
|
879
|
+
const pose = { position: [x, y, z], quaternion: [qx, qy, qz, qw] };
|
|
880
|
+
return hand === "R" ? { ...base ?? {}, R: pose, grip: worked.grip, fingers: { index: worked.index } } : { ...base ?? {}, L: pose, gripL: worked.grip, fingersL: { index: worked.index }, slideL: null };
|
|
881
|
+
},
|
|
882
|
+
cancel: stop
|
|
883
|
+
};
|
|
884
|
+
}
|
|
692
885
|
export {
|
|
693
886
|
BELT_SLOT,
|
|
694
887
|
DOOR_ARM_REACH,
|
|
695
888
|
DOOR_SHOULDER_HEIGHT,
|
|
696
889
|
DOOR_USE_DEFAULTS,
|
|
697
890
|
FURNITURE_PUSH_DEFAULTS,
|
|
891
|
+
GESTURE_ARM_REACH,
|
|
892
|
+
GESTURE_TIMING,
|
|
893
|
+
HANDS_BY_HANDLE,
|
|
698
894
|
HANDS_DEFAULTS,
|
|
699
895
|
HANDS_TIMING,
|
|
700
896
|
ITEM_GRIP,
|
|
@@ -718,6 +914,8 @@ export {
|
|
|
718
914
|
createBodyVitals,
|
|
719
915
|
createDoorUse,
|
|
720
916
|
createFurniturePush,
|
|
917
|
+
createGestureHands,
|
|
918
|
+
createGestureTimeline,
|
|
721
919
|
createNeedsState,
|
|
722
920
|
createPhysicsController,
|
|
723
921
|
createPlayer,
|
|
@@ -732,8 +930,12 @@ export {
|
|
|
732
930
|
drawWound,
|
|
733
931
|
easeInOut,
|
|
734
932
|
footRestHeight,
|
|
933
|
+
freeHandOf,
|
|
735
934
|
frictionFor,
|
|
935
|
+
gestureHandOf,
|
|
936
|
+
gestureHandPose,
|
|
736
937
|
gripOfItem,
|
|
938
|
+
gripOnHandle,
|
|
737
939
|
gripSecondsFor,
|
|
738
940
|
handPoseOnItem,
|
|
739
941
|
interactionHandsFor,
|