@adatechnology/game-player 0.5.7 → 0.6.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
@@ -59,6 +59,7 @@ carries it too.
59
59
  ## Pushing furniture
60
60
 
61
61
  `createFurniturePush(rules?)` is the logic of pushing a heavy thing on foot: `begin({ massKg, material })`, then `update(dt, { walk, isFree })` every frame answers the step and says the `phase` (`grip` while the hold timer fills, `push`, `strain`), the `progress` of the timer and the `block` (`too_heavy`, `obstacle`). The host answers `isFree` by sweeping the volume of the piece and draws the rest. `gripSecondsFor`, `pushSpeedFor` and `canPush` give the numbers; the force of the legs, the friction by material, the times and the speeds are all in `FurniturePushRules` (`FURNITURE_PUSH_DEFAULTS`).
62
+ The walk with the piece speeds up and brakes like a person's (`accel`: the force left over the friction, over the mass, at most `accel.max`; braking at `accel.brake`), and each step reports its `velocity`, its `acceleration` and, when it hits something and stops at once, the velocity it had (`stoppedFrom`). `slipsOnTop(acceleration, frictionFor(material))` says whether a thing resting on the piece slides on it: a cup on a table rides while the push speeds up and slides on when the table stops dead.
62
63
 
63
64
  ## Opening doors by hand
64
65
 
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { P as PlayerMessageId, K as KnownDeathCause } from './testing-CD4p4NR5.js';
2
2
  export { A as AttributeId, a as Attributes, D as DeathCause, b as Derived, c as DerivedFormulas, d as DerivedId, I as InfectionRule, L as LinearFormula, M as MeleeItem, e as PLAYER_DEFAULTS, f as Player, g as PlayerAction, h as PlayerCrowd, i as PlayerEvent, j as PlayerEventMap, k as PlayerEventName, l as PlayerInput, m as PlayerNumbers, n as PlayerOptions, o as PlayerSave, p as PlayerSoundId, q as PlayerState, r as PlayerTestModule, s as PlayerTestModuleOptions, t as PlayerWorld, W as WoundRule, u as WoundTable, v as createPlayer, w as createPlayerTestModule, x as deriveVitals, y as drawWound } from './testing-CD4p4NR5.js';
3
- import '@adatechnology/game-contracts';
3
+ import { SurfaceId } from '@adatechnology/game-contracts';
4
4
 
5
5
  /** Deterministic mulberry32 generator; returns floats in [0, 1). */
6
6
  declare function createRng(seed: number): () => number;
@@ -9,6 +9,12 @@ declare function createRng(seed: number): () => number;
9
9
  * Pushing furniture on foot: hold the interaction button against a piece, a timer that grows with its mass fills, and the player then walks with it.
10
10
  * Engine-agnostic: the host says where the player wants to go and answers whether the volume of the piece can go there; this says how fast, and when it locks.
11
11
  */
12
+
13
+ /** What dragging a piece leaves on a floor: a strip of this colour and width (metres) under each side of its base. */
14
+ type DragMark = {
15
+ readonly color: number;
16
+ readonly width: number;
17
+ };
12
18
  type FurniturePushPhase = 'idle' | 'grip' | 'push' | 'strain';
13
19
  type FurniturePushBlock = 'too_heavy' | 'obstacle' | null;
14
20
  /** Every number of the push; a game overrides what it wants (`createFurniturePush(rules)`). */
@@ -30,21 +36,45 @@ interface FurniturePushRules {
30
36
  readonly max: number;
31
37
  readonly min: number;
32
38
  };
39
+ /**
40
+ * How fast the walk with the piece changes, m/s²: `max` the most a person speeds it up (the force left over the friction, over the mass, may give less), `brake` the
41
+ * most it slows it (stopping, turning back). Below what holds a thing on top (friction × g, about 3.4 m/s² for the softest), so a cup on a pushed table rides along.
42
+ */
43
+ readonly accel: {
44
+ readonly max: number;
45
+ readonly brake: number;
46
+ readonly min: number;
47
+ };
33
48
  /** A press shorter than this is not a push (the host keeps it for a tap, such as grabbing). */
34
49
  readonly tapSeconds: number;
35
50
  /** How long the effort stays after the last step that was refused, so that it does not flicker. */
36
51
  readonly strainHoldSeconds: number;
52
+ /**
53
+ * The scratch a dragged piece leaves, by the floor it is dragged on (`SurfaceId` of game-contracts): wood and tile scratch lighter, concrete scuffs darker,
54
+ * dirt and sand take a furrow; a floor left out (carpet, grass, gravel, asphalt, water) keeps no mark.
55
+ */
56
+ readonly dragMarks: Readonly<Partial<Record<SurfaceId, DragMark>>>;
37
57
  }
38
58
  declare const FURNITURE_PUSH_DEFAULTS: FurniturePushRules;
39
59
  type FurniturePushTarget = {
40
60
  readonly massKg: number;
41
61
  readonly material?: string;
42
62
  };
63
+ /** Static friction of a thing of `material` on what it stands on (the floor, a table top). */
64
+ declare const frictionFor: (material: string | undefined, rules?: FurniturePushRules) => number;
65
+ /** A piece, or a row of pieces pushed one against the next (a sofa shoving a chair): their frictions on the floor add up against the legs. */
66
+ type FurniturePushLoad = FurniturePushTarget | readonly FurniturePushTarget[];
43
67
  declare const gripSecondsFor: (massKg: number, rules?: FurniturePushRules) => number;
44
- /** Whether the legs overcome the friction of the piece on the floor. */
45
- declare const canPush: (target: FurniturePushTarget, rules?: FurniturePushRules) => boolean;
68
+ /** Whether the legs overcome the friction of the piece (or of the row of pieces) on the floor. */
69
+ declare const canPush: (target: FurniturePushLoad, rules?: FurniturePushRules) => boolean;
46
70
  /** Metres per second with the piece on the hands: slower the closer it is to the limit of the force, 0 past it. */
47
- declare const pushSpeedFor: (target: FurniturePushTarget, rules?: FurniturePushRules) => number;
71
+ declare const pushSpeedFor: (target: FurniturePushLoad, rules?: FurniturePushRules) => number;
72
+ /** How fast the walk with the piece speeds up, m/s²: the force left over its friction, over its mass, at most what a person does (`accel.max`). */
73
+ declare const pushAccelerationFor: (target: FurniturePushLoad, rules?: FurniturePushRules) => number;
74
+ /** The scratch dragging leaves on `surface`, or null where the floor keeps none (carpet, grass). */
75
+ declare const dragMarkFor: (surface: SurfaceId, rules?: FurniturePushRules) => DragMark | null;
76
+ /** Whether a thing resting on a moving piece slides on it: the piece changes speed faster than the friction under the thing can follow (`accelMps2` > friction × g). */
77
+ declare const slipsOnTop: (accelMps2: number, friction: number, gravity?: number) => boolean;
48
78
  type FurniturePushInput = {
49
79
  /** Where the player wants to go on the floor: a unit vector, or `[0, 0]` standing. */
50
80
  readonly walk: readonly [number, number];
@@ -58,6 +88,12 @@ type FurniturePushStep = {
58
88
  readonly engaged: boolean;
59
89
  /** A step was refused now and the one before was not: the moment the piece hits something. */
60
90
  readonly hitObstacle: boolean;
91
+ /** The velocity of the piece after this update, m/s on the floor. */
92
+ readonly velocity: readonly [number, number];
93
+ /** How fast its velocity changed in this update, m/s²: what a thing on top feels. */
94
+ readonly acceleration: number;
95
+ /** The velocity it had when it hit something and stopped at once (what rests on it keeps it and slides on), or null. */
96
+ readonly stoppedFrom: readonly [number, number] | null;
61
97
  };
62
98
  interface FurniturePush {
63
99
  readonly phase: FurniturePushPhase;
@@ -67,7 +103,11 @@ interface FurniturePush {
67
103
  readonly heldSeconds: number;
68
104
  /** Whether the hold is still short enough to be a tap. */
69
105
  readonly isTap: boolean;
106
+ /** The velocity of the piece now, m/s on the floor. */
107
+ readonly velocity: readonly [number, number];
70
108
  begin(target: FurniturePushTarget): void;
109
+ /** The load changed while pushing (the piece met another and shoves it along): the speed and the speeding up follow the whole row. */
110
+ setLoad(load: FurniturePushLoad): void;
71
111
  release(): void;
72
112
  update(dt: number, input: FurniturePushInput): FurniturePushStep;
73
113
  }
@@ -145,4 +185,4 @@ interface DoorUse {
145
185
  }
146
186
  declare function createDoorUse(rules?: Partial<DoorUseRules>): DoorUse;
147
187
 
148
- export { DOOR_USE_DEFAULTS, type DoorUse, type DoorUseBlock, type DoorUsePhase, type DoorUseRules, type DoorUseStep, type DoorUseTarget, FURNITURE_PUSH_DEFAULTS, type FurniturePush, type FurniturePushBlock, type FurniturePushInput, type FurniturePushPhase, type FurniturePushRules, type FurniturePushStep, type FurniturePushTarget, KnownDeathCause, PLAYER_TEXT, PlayerMessageId, type PlayerText, canPush, createDoorUse, createFurniturePush, createRng, gripSecondsFor, playerTextOf, pushSpeedFor, swingSecondsFor };
188
+ export { DOOR_USE_DEFAULTS, type DoorUse, type DoorUseBlock, type DoorUsePhase, type DoorUseRules, type DoorUseStep, type DoorUseTarget, type DragMark, FURNITURE_PUSH_DEFAULTS, type FurniturePush, type FurniturePushBlock, type FurniturePushInput, type FurniturePushLoad, type FurniturePushPhase, type FurniturePushRules, type FurniturePushStep, type FurniturePushTarget, KnownDeathCause, PLAYER_TEXT, PlayerMessageId, type PlayerText, canPush, createDoorUse, createFurniturePush, createRng, dragMarkFor, frictionFor, gripSecondsFor, playerTextOf, pushAccelerationFor, pushSpeedFor, slipsOnTop, swingSecondsFor };
@@ -32,10 +32,14 @@ var GamePlayer = (() => {
32
32
  createPlayerTestModule: () => createPlayerTestModule,
33
33
  createRng: () => createRng,
34
34
  deriveVitals: () => deriveVitals,
35
+ dragMarkFor: () => dragMarkFor,
35
36
  drawWound: () => drawWound,
37
+ frictionFor: () => frictionFor,
36
38
  gripSecondsFor: () => gripSecondsFor,
37
39
  playerTextOf: () => playerTextOf,
40
+ pushAccelerationFor: () => pushAccelerationFor,
38
41
  pushSpeedFor: () => pushSpeedFor,
42
+ slipsOnTop: () => slipsOnTop,
39
43
  swingSecondsFor: () => swingSecondsFor
40
44
  });
41
45
 
@@ -790,18 +794,38 @@ var GamePlayer = (() => {
790
794
  defaultFriction: 0.45,
791
795
  gripSeconds: { base: 0.4, perKg: 0.015, max: 2.5 },
792
796
  speed: { max: 1.8, min: 0.35 },
797
+ accel: { max: 1.5, brake: 3, min: 0.2 },
793
798
  tapSeconds: 0.2,
794
- strainHoldSeconds: 0.25
799
+ strainHoldSeconds: 0.25,
800
+ dragMarks: {
801
+ wood: { color: 14206112, width: 0.012 },
802
+ tile: { color: 10132118, width: 0.01 },
803
+ concrete: { color: 4539199, width: 0.02 },
804
+ metal: { color: 13685976, width: 8e-3 },
805
+ glass: { color: 15921906, width: 6e-3 },
806
+ dirt: { color: 4863268, width: 0.04 },
807
+ sand: { color: 9271900, width: 0.05 }
808
+ }
795
809
  };
796
- var frictionOf = (target, rules) => (target.material !== void 0 ? rules.frictionByMaterial[target.material] : void 0) ?? rules.defaultFriction;
797
- var loadShare = (target, rules) => frictionOf(target, rules) * target.massKg * rules.gravity / rules.forceN;
810
+ var frictionFor = (material, rules = FURNITURE_PUSH_DEFAULTS) => (material !== void 0 ? rules.frictionByMaterial[material] : void 0) ?? rules.defaultFriction;
811
+ var frictionOf = (target, rules) => frictionFor(target.material, rules);
812
+ var piecesOf = (load) => Array.isArray(load) ? load : [load];
813
+ var frictionForceOf = (load, rules) => piecesOf(load).reduce((sum, t) => sum + frictionOf(t, rules) * t.massKg * rules.gravity, 0);
814
+ var massOf = (load) => piecesOf(load).reduce((sum, t) => sum + t.massKg, 0);
815
+ var loadShare = (load, rules) => frictionForceOf(load, rules) / rules.forceN;
798
816
  var gripSecondsFor = (massKg, rules = FURNITURE_PUSH_DEFAULTS) => Math.min(rules.gripSeconds.max, rules.gripSeconds.base + rules.gripSeconds.perKg * Math.max(0, massKg));
799
817
  var canPush = (target, rules = FURNITURE_PUSH_DEFAULTS) => loadShare(target, rules) <= 1;
800
818
  var pushSpeedFor = (target, rules = FURNITURE_PUSH_DEFAULTS) => {
801
819
  const share = loadShare(target, rules);
802
820
  return share > 1 ? 0 : rules.speed.max + (rules.speed.min - rules.speed.max) * share;
803
821
  };
804
- var STILL = { dx: 0, dz: 0, engaged: false, hitObstacle: false };
822
+ var pushAccelerationFor = (target, rules = FURNITURE_PUSH_DEFAULTS) => {
823
+ const spare = (rules.forceN - frictionForceOf(target, rules)) / Math.max(1e-3, massOf(target));
824
+ return Math.max(rules.accel.min, Math.min(rules.accel.max, spare));
825
+ };
826
+ var dragMarkFor = (surface, rules = FURNITURE_PUSH_DEFAULTS) => rules.dragMarks[surface] ?? null;
827
+ var slipsOnTop = (accelMps2, friction, gravity = FURNITURE_PUSH_DEFAULTS.gravity) => accelMps2 > friction * gravity;
828
+ var STILL = { dx: 0, dz: 0, engaged: false, hitObstacle: false, velocity: [0, 0], acceleration: 0, stoppedFrom: null };
805
829
  function createFurniturePush(overrides = {}) {
806
830
  const rules = { ...FURNITURE_PUSH_DEFAULTS, ...overrides };
807
831
  let phase = "idle";
@@ -809,37 +833,59 @@ var GamePlayer = (() => {
809
833
  let held = 0;
810
834
  let gripSeconds = 1;
811
835
  let speed = 0;
836
+ let accel = rules.accel.max;
837
+ let vx = 0;
838
+ let vz = 0;
812
839
  let block = null;
813
840
  let refused = 0;
814
841
  let wasRefused = false;
842
+ const steer = (dt, walk) => {
843
+ const wantX = walk[0] * speed;
844
+ const wantZ = walk[1] * speed;
845
+ const braking = wantX * vx + wantZ * vz < vx * vx + vz * vz;
846
+ let dvx = wantX - vx;
847
+ let dvz = wantZ - vz;
848
+ const change = Math.hypot(dvx, dvz);
849
+ const most = (braking ? rules.accel.brake : accel) * dt;
850
+ if (change > most) {
851
+ dvx *= most / change;
852
+ dvz *= most / change;
853
+ }
854
+ vx += dvx;
855
+ vz += dvz;
856
+ return Math.min(change, most) / dt;
857
+ };
815
858
  const walkWith = (dt, input) => {
816
859
  refused = Math.max(0, refused - dt);
817
- if (block === "too_heavy") return STILL;
818
- const [x, z] = input.walk;
819
- if (x === 0 && z === 0 || dt <= 0) {
860
+ if (block === "too_heavy" || dt <= 0) return STILL;
861
+ const acceleration = steer(dt, input.walk);
862
+ if (Math.hypot(vx, vz) < 1e-4) {
863
+ vx = vz = 0;
820
864
  if (refused === 0) {
821
865
  phase = "push";
822
866
  block = null;
823
867
  wasRefused = false;
824
868
  }
825
- return STILL;
869
+ return { ...STILL, acceleration };
826
870
  }
827
- const dx = x * speed * dt;
828
- const dz = z * speed * dt;
871
+ const dx = vx * dt;
872
+ const dz = vz * dt;
829
873
  if (input.isFree(dx, dz)) {
830
874
  if (refused === 0) {
831
875
  phase = "push";
832
876
  block = null;
833
877
  }
834
878
  wasRefused = false;
835
- return { dx, dz, engaged: false, hitObstacle: false };
879
+ return { dx, dz, engaged: false, hitObstacle: false, velocity: [vx, vz], acceleration, stoppedFrom: null };
836
880
  }
881
+ const from = [vx, vz];
882
+ vx = vz = 0;
837
883
  const hitObstacle = !wasRefused;
838
884
  wasRefused = true;
839
885
  refused = rules.strainHoldSeconds;
840
886
  phase = "strain";
841
887
  block = "obstacle";
842
- return { dx: 0, dz: 0, engaged: false, hitObstacle };
888
+ return { ...STILL, hitObstacle, acceleration: Math.hypot(from[0], from[1]) / dt, stoppedFrom: hitObstacle ? from : null };
843
889
  };
844
890
  return {
845
891
  get phase() {
@@ -854,6 +900,9 @@ var GamePlayer = (() => {
854
900
  get heldSeconds() {
855
901
  return held;
856
902
  },
903
+ get velocity() {
904
+ return [vx, vz];
905
+ },
857
906
  get isTap() {
858
907
  return phase === "grip" && held < rules.tapSeconds;
859
908
  },
@@ -866,9 +915,16 @@ var GamePlayer = (() => {
866
915
  wasRefused = false;
867
916
  gripSeconds = gripSecondsFor(next.massKg, rules);
868
917
  speed = pushSpeedFor(next, rules);
918
+ accel = pushAccelerationFor(next, rules);
919
+ vx = vz = 0;
920
+ },
921
+ setLoad(load) {
922
+ speed = pushSpeedFor(load, rules);
923
+ accel = pushAccelerationFor(load, rules);
869
924
  },
870
925
  release() {
871
926
  phase = "idle";
927
+ vx = vz = 0;
872
928
  held = 0;
873
929
  block = null;
874
930
  },