@adatechnology/game-player 0.15.1 → 0.16.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/README.md CHANGED
@@ -119,7 +119,10 @@ vehicle without a door is sat on and got off. `takeSeat()` puts the body at the
119
119
 
120
120
  `createDrivingHands()` is the driver's hands: both on the rim, going round with the wheel; the hand that goes down the rim lets go first and takes it again
121
121
  ahead of its place, the other after it, never both off the wheel; `shift(hand)` sends the hand on the lever's side to the knob, moves the lever
122
- (`leverThrow`) and brings it back. Angles on the rim and shares of the way to the lever: the host turns them into places in its world.
122
+ (`leverThrow`) and brings it back; `work(hand, control, held?, place?)` sends a hand to a control (a stalk, the horn, the radio): `update` answers the
123
+ control the hand got to in that step, which is when it is worked, switches asked for meanwhile wait their turn (the hand stays at the same place to click
124
+ again) and a held control keeps the hand on it. Angles on the rim and shares of the way to the lever or the control: the host turns them into places in
125
+ its world.
123
126
 
124
127
  ## The hands and the weapon
125
128
 
package/dist/index.d.ts CHANGED
@@ -635,7 +635,8 @@ declare function createVehicleBoarding(ports: VehicleBoardingPorts, rules?: Part
635
635
 
636
636
  /**
637
637
  * The hands of a driver: both on the rim of the steering wheel, turning with it; a hand carried too far round lets go, comes back to its own side and holds
638
- * again (one hand at a time, the other keeps the wheel); to change gear the hand on the lever's side leaves the rim, takes the knob, moves it and comes back.
638
+ * again (one hand at a time, the other keeps the wheel); to change gear the hand on the lever's side leaves the rim, takes the knob, moves it and comes back;
639
+ * to work a control (a stalk, the horn, the radio, a switch) a hand leaves the rim, goes to it, works it when it gets there and comes back (CLAUDE.md §18).
639
640
  * Engine-agnostic: the host gives the wheel's turn and says when a gear changes; this says where each hand is, as angles on the rim and shares of the way to
640
641
  * the lever, and the host turns that into places in its world.
641
642
  */
@@ -658,6 +659,9 @@ interface DrivingHandsRules {
658
659
  readonly leverReachSeconds: number;
659
660
  /** The hand on the knob while the lever moves, seconds. */
660
661
  readonly leverHoldSeconds: number;
662
+ /** The hand from the rim to a control, and back, seconds each way; and on the control, seconds. */
663
+ readonly controlReachSeconds: number;
664
+ readonly controlHoldSeconds: number;
661
665
  }
662
666
  declare const DRIVING_HANDS_DEFAULTS: DrivingHandsRules;
663
667
  interface DrivingHand {
@@ -667,6 +671,9 @@ interface DrivingHand {
667
671
  readonly hold: number;
668
672
  /** Share of the way from the rim to the gear lever's knob, 0 on the rim to 1 on the knob. */
669
673
  readonly toLever: number;
674
+ /** Share of the way from the rim to the control it works, 0 on the rim to 1 on the control; and which control (the host knows where it is). */
675
+ readonly toControl: number;
676
+ readonly control: string | null;
670
677
  }
671
678
  interface DrivingHands {
672
679
  readonly left: DrivingHand;
@@ -675,8 +682,15 @@ interface DrivingHands {
675
682
  readonly leverThrow: number;
676
683
  /** A gear change starts: `hand` the hand on the lever's side. Ignored while one is going on. */
677
684
  shift(hand: 'left' | 'right'): void;
678
- /** `wheel`: the steering wheel's turn now, radians, positive to the left. */
679
- update(dt: number, wheel: number): void;
685
+ /**
686
+ * A hand goes to work a control: one hand always keeps the wheel. While a gear change or another control is going on, a switch (no `held`) waits its
687
+ * turn (up to `MAX_WAITING`; the same hand at the same `place`, a stalk clicked again, stays there for it instead of coming back to the rim first) and a
688
+ * held control answers false, nothing started. `held` (a horn pressed, a washer squirting): the hand stays on the control while it answers true.
689
+ * `place`: where the control is, for the hand to stay (default: the control itself).
690
+ */
691
+ work(hand: 'left' | 'right', control: string, held?: () => boolean, place?: string): boolean;
692
+ /** `wheel`: the steering wheel's turn now, radians, positive to the left. Answers the control a hand got to in this step (it is worked now), or null. */
693
+ update(dt: number, wheel: number): string | null;
680
694
  /** Both hands back on the rim at rest (the driver got in). */
681
695
  reset(wheel: number): void;
682
696
  }
@@ -2287,15 +2287,20 @@ var GamePlayer = (() => {
2287
2287
  }
2288
2288
 
2289
2289
  // src/driving-hands.ts
2290
- var DRIVING_HANDS_DEFAULTS = { regripAt: 1.75, firstAt: 0.7, lead: 0.9, stillRate: 0.3, settleAt: 0.25, reachLimit: 2.3, regripSeconds: 0.24, leverReachSeconds: 0.22, leverHoldSeconds: 0.26 };
2290
+ var DRIVING_HANDS_DEFAULTS = { regripAt: 1.75, firstAt: 0.7, lead: 0.9, stillRate: 0.3, settleAt: 0.25, reachLimit: 2.3, regripSeconds: 0.24, leverReachSeconds: 0.22, leverHoldSeconds: 0.26, controlReachSeconds: 0.2, controlHoldSeconds: 0.14 };
2291
2291
  var smooth = (k) => k * k * (3 - 2 * k);
2292
+ var MAX_WAITING = 4;
2293
+ var AT_REST = { at: 0, hold: 1, toLever: 0, toControl: 0, control: null, regrip: -1, from: 0, to: 0 };
2292
2294
  function createDrivingHands(rules = {}) {
2293
2295
  const r = { ...DRIVING_HANDS_DEFAULTS, ...rules };
2294
- const hands = { left: { at: 0, hold: 1, toLever: 0, regrip: -1, from: 0, to: 0 }, right: { at: 0, hold: 1, toLever: 0, regrip: -1, from: 0, to: 0 } };
2296
+ const hands = { left: { ...AT_REST }, right: { ...AT_REST } };
2295
2297
  let last = 0;
2296
2298
  let shifting = null;
2297
- const busy = (h) => h.regrip >= 0 || h.toLever > 0;
2299
+ let working = null;
2300
+ const waiting = [];
2301
+ const busy = (h) => h.regrip >= 0 || h.toLever > 0 || h.toControl > 0;
2298
2302
  const total = r.leverReachSeconds * 2 + r.leverHoldSeconds;
2303
+ const workTotal = r.controlReachSeconds * 2 + r.controlHoldSeconds;
2299
2304
  return {
2300
2305
  left: hands.left,
2301
2306
  right: hands.right,
@@ -2303,15 +2308,29 @@ var GamePlayer = (() => {
2303
2308
  if (!shifting) return 1;
2304
2309
  return smooth(Math.max(0, Math.min(1, (shifting.clock - r.leverReachSeconds) / r.leverHoldSeconds)));
2305
2310
  },
2311
+ work(hand, control, held, place = control) {
2312
+ const other = hands[hand === "left" ? "right" : "left"];
2313
+ if (shifting || working || busy(other)) {
2314
+ if (held || waiting.length >= MAX_WAITING) return false;
2315
+ waiting.push({ hand, control, place });
2316
+ return true;
2317
+ }
2318
+ working = { hand, clock: 0, acted: false, held, place };
2319
+ hands[hand].regrip = -1;
2320
+ hands[hand].control = control;
2321
+ return true;
2322
+ },
2306
2323
  shift(hand) {
2307
- if (shifting) return;
2324
+ if (shifting || working) return;
2308
2325
  shifting = { hand, clock: 0 };
2309
2326
  hands[hand].regrip = -1;
2310
2327
  },
2311
2328
  reset(wheel) {
2312
2329
  last = wheel;
2313
2330
  shifting = null;
2314
- for (const h of [hands.left, hands.right]) Object.assign(h, { at: 0, hold: 1, toLever: 0, regrip: -1, from: 0, to: 0 });
2331
+ working = null;
2332
+ waiting.length = 0;
2333
+ for (const h of [hands.left, hands.right]) Object.assign(h, AT_REST);
2315
2334
  },
2316
2335
  update(dt, wheel) {
2317
2336
  const turned = wheel - last;
@@ -2331,10 +2350,45 @@ var GamePlayer = (() => {
2331
2350
  shifting = null;
2332
2351
  }
2333
2352
  }
2353
+ let acted = null;
2354
+ if (working) {
2355
+ working.clock += dt;
2356
+ if (working.acted && working.held?.()) working.clock = Math.min(working.clock, r.controlReachSeconds);
2357
+ const h = hands[working.hand];
2358
+ const c = working.clock;
2359
+ const out = smooth(Math.min(1, c / r.controlReachSeconds));
2360
+ const back = smooth(Math.max(0, Math.min(1, (c - r.controlReachSeconds - r.controlHoldSeconds) / r.controlReachSeconds)));
2361
+ h.toControl = Math.max(out * (1 - back), 1e-6);
2362
+ h.hold = 1 - h.toControl;
2363
+ h.at = 0;
2364
+ if (!working.acted && c >= r.controlReachSeconds) {
2365
+ working.acted = true;
2366
+ acted = h.control;
2367
+ }
2368
+ const next = waiting[0];
2369
+ if (working.acted && acted === null && !working.held?.() && c >= r.controlReachSeconds + r.controlHoldSeconds && next && next.hand === working.hand && next.place === working.place) {
2370
+ waiting.shift();
2371
+ h.control = next.control;
2372
+ working.clock = r.controlReachSeconds;
2373
+ working.acted = false;
2374
+ working.held = void 0;
2375
+ } else if (c >= workTotal) {
2376
+ h.toControl = 0;
2377
+ h.control = null;
2378
+ h.hold = 1;
2379
+ working = null;
2380
+ }
2381
+ }
2382
+ if (!working && !shifting && waiting.length > 0 && !busy(hands.left) && !busy(hands.right)) {
2383
+ const next = waiting.shift();
2384
+ working = { hand: next.hand, clock: 0, acted: false, held: void 0, place: next.place };
2385
+ hands[next.hand].regrip = -1;
2386
+ hands[next.hand].control = next.control;
2387
+ }
2334
2388
  for (const side of ["left", "right"]) {
2335
2389
  const h = hands[side];
2336
2390
  const other = hands[side === "left" ? "right" : "left"];
2337
- if (h.toLever > 0) continue;
2391
+ if (h.toLever > 0 || h.toControl > 0) continue;
2338
2392
  if (h.regrip >= 0) {
2339
2393
  h.regrip += dt;
2340
2394
  const k = Math.min(1, h.regrip / r.regripSeconds);
@@ -2359,6 +2413,7 @@ var GamePlayer = (() => {
2359
2413
  h.to = turning ? -Math.sign(rate) * r.lead : 0;
2360
2414
  }
2361
2415
  }
2416
+ return acted;
2362
2417
  }
2363
2418
  };
2364
2419
  }