@korial29/tiny-fps 0.13.0 → 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.
@@ -17,6 +17,7 @@ export interface TinyFps {
17
17
  }
18
18
  /**
19
19
  * Mounts the game in a Shadow DOM inside container: one WebGL2 canvas filling it, a click locks the
20
- * pointer, the mouse looks, WASD (ZQSD on AZERTY) moves, Space jumps.
20
+ * pointer, the mouse looks, WASD (ZQSD on AZERTY) moves, Shift sprints, Space jumps (again in the
21
+ * air for a second jump), E or the right button dashes, C crouches and slides.
21
22
  */
22
23
  export declare const tinyFps: (container: HTMLElement, opts: TinyFpsOptions) => TinyFps;
@@ -14,7 +14,7 @@ export interface Controls {
14
14
  destroy(): void;
15
15
  }
16
16
  /**
17
- * Click the canvas to lock the pointer; while locked the mouse looks, the keys move and M calls
18
- * `onMute`.
17
+ * Click the canvas to lock the pointer; while locked the mouse looks, the keys move (WASD, Shift
18
+ * sprints, Space jumps, E or the right button dashes, C crouches) and M calls `onMute`.
19
19
  */
20
20
  export declare const createInput: (canvas: HTMLCanvasElement, onMute?: () => void) => Controls;
@@ -13,6 +13,15 @@ export interface Predictor {
13
13
  step(i: Omit<Input, 'seq' | 'view'>, now?: number): Input;
14
14
  /** A decoded snapshot, received at `now` (ms, performance.now()). */
15
15
  snapshot(s: Snapshot, now: number): void;
16
+ /**
17
+ * What happened in the own player's live steps since the last call: the EV bits of every one of
18
+ * them or-ed together, and the fastest landing. Replays after a snapshot do not count: a move is
19
+ * reported once, when it is first predicted. For the camera, the body and the sounds.
20
+ */
21
+ events(): {
22
+ ev: number;
23
+ fall: number;
24
+ };
16
25
  /**
17
26
  * The own player, eased between the state before and after the last step by alpha in [0, 1]
18
27
  * (the share of a step elapsed since it), so the camera moves smoothly at any frame rate.
@@ -22,6 +22,97 @@ export declare const GRAVITY = 20;
22
22
  export declare const MAX_PITCH = 1.55;
23
23
  /** A floor at most this much higher is walked onto; a higher one needs a jump. */
24
24
  export declare const STEP_H = 0.5;
25
+ /** Ground acceleration toward the wished direction: from standing to RUN_SPEED in 0.12 s. */
26
+ export declare const ACCEL = 50;
27
+ /** Ground deceleration with no direction held: strong, a stop takes about 0.15 s. */
28
+ export declare const FRICTION = 45;
29
+ /**
30
+ * How fast the speed across the wished direction is killed on the ground. A gentle turn keeps
31
+ * nearly all the speed, a turn of 90 degrees loses it all and starts again from RUN_SPEED.
32
+ */
33
+ export declare const GRIP = 45;
34
+ /** How fast a speed above the current top speed falls back to it (sprint released, dash over). */
35
+ export declare const DECAY = 12;
36
+ /** Acceleration in the air toward the wished direction. The speed never grows from it, see AIR_SPEED. */
37
+ export declare const AIR_ACCEL = 14;
38
+ /** The speed air control may build up to; a faster player keeps his speed and only turns. */
39
+ export declare const AIR_SPEED = 6;
40
+ /** Top speed after about a second of sprinting forward (Shift held, forward held). */
41
+ export declare const SPRINT_SPEED = 9;
42
+ /** Acceleration from RUN_SPEED to SPRINT_SPEED: 3 m/s over 1 s. */
43
+ export declare const SPRINT_ACCEL = 3;
44
+ /** Dash: a burst of this speed in the wished direction (forward without a key), no gravity. */
45
+ export declare const DASH_SPEED = 16;
46
+ /** Ticks the burst lasts (0.17 s, about 2.7 m). */
47
+ export declare const DASH_TICKS = 10;
48
+ /** The speed left after the burst; it then falls to the top speed at DECAY. */
49
+ export declare const DASH_EXIT_SPEED = 10;
50
+ /** Ticks from one dash to the next dash, burst included (1 s). */
51
+ export declare const DASH_COOLDOWN = 60;
52
+ /** The extra jump in the air: weaker than the first one, so a walk and a double jump stay short. */
53
+ export declare const DOUBLE_JUMP_SPEED = 6;
54
+ /** Air jumps that landing, a wall-run and a ledge grab give back. */
55
+ export declare const AIR_JUMPS = 1;
56
+ /** Ticks after walking off an edge in which a jump still works (0.1 s). */
57
+ export declare const COYOTE_TICKS = 6;
58
+ /**
59
+ * Ticks a jump press is remembered after its own (0.1 s): pressed this close to the ground, it
60
+ * goes off on landing. The state counts it as JUMP_BUFFER_TICKS + 1, which must fit 3 bits.
61
+ */
62
+ export declare const JUMP_BUFFER_TICKS = 6;
63
+ /** Height of the cylinder when sliding or crouching, and the eye in it. */
64
+ export declare const LOW_H = 0.9;
65
+ export declare const LOW_EYE = 0.7;
66
+ /** Speed from which a crouch is a slide (above RUN_SPEED: only a sprint slides). */
67
+ export declare const SLIDE_MIN_SPEED = 7.5;
68
+ /** A slide bleeds speed at this rate (a sprint of 9 m/s is down to 5 after a second)... */
69
+ export declare const SLIDE_DECEL = 4;
70
+ /** ...and ends under this speed, or after this many ticks (1 s), into a crouch or a stand. */
71
+ export declare const SLIDE_END_SPEED = 4.5;
72
+ export declare const SLIDE_TICKS = 60;
73
+ /** Top speed of a crouch walk. */
74
+ export declare const CROUCH_SPEED = 3;
75
+ /** Wall-run: moving at least this fast along a wall (m/s) starts it, and under WALLRUN_KEEP it ends. */
76
+ export declare const WALLRUN_MIN_SPEED = 7;
77
+ export declare const WALLRUN_KEEP_SPEED = 5.5;
78
+ /** Speed lost along the wall, m/s². */
79
+ export declare const WALLRUN_DRAG = 2.5;
80
+ /** Longest run, in ticks (1 s). */
81
+ export declare const WALLRUN_TICKS = 60;
82
+ /** Gravity during the run, as a share of GRAVITY, and the fastest rise it starts with. */
83
+ export declare const WALLRUN_GRAVITY = 0.15;
84
+ export declare const WALLRUN_VY_MAX = 3;
85
+ /** How far from the cylinder a wall still counts as next to it (m). */
86
+ export declare const WALL_REACH = 0.15;
87
+ /** The share of the speed that may point into or away from the wall for it to count as along it. */
88
+ export declare const WALL_PARALLEL = 0.4;
89
+ /** A kick-off from the wall: speed away from it and upward (m/s). */
90
+ export declare const WALL_KICK_SPEED = 5;
91
+ export declare const WALL_KICK_UP = 6.5;
92
+ /** Ledge grab: a top this far above the feet (m) can be caught, if the player faces it (cosine). */
93
+ export declare const GRAB_MIN = 1.2;
94
+ export declare const GRAB_MAX = 2.4;
95
+ export declare const GRAB_FACING = 0.5;
96
+ /** A hand's reach beyond the cylinder (m), and the gap left between the hanging body and the wall. */
97
+ export declare const GRAB_REACH = 0.2;
98
+ export declare const GRAB_GAP = 0.03;
99
+ /** Feet this far under the ledge (m) when hanging, and how fast the body settles there (m/s). */
100
+ export declare const HANG_H = 2;
101
+ export declare const HANG_SNAP = 8;
102
+ /** A forward press climbs from this tick of the hang (0.1 s); with nothing held the hand slips at HANG_SLIDE. */
103
+ export declare const HANG_MIN_TICKS = 6;
104
+ export declare const HANG_SLIDE_TICKS = 30;
105
+ /** Climb: the body rises to the top at this speed (m/s), then moves CLIMB_INSET in from the edge. */
106
+ export declare const CLIMB_RISE = 8;
107
+ export declare const CLIMB_INSET = 0.2;
108
+ export declare const CLIMB_FWD_SPEED = 5.5;
109
+ /** Safety: a climb that has not ended after this many ticks ends where it is. */
110
+ export declare const CLIMB_MAX_TICKS = 40;
111
+ /** Speed forward when the climb ends (m/s). */
112
+ export declare const CLIMB_EXIT_SPEED = 3;
113
+ /** Jumping off a ledge facing away from it: speed away and upward (m/s). */
114
+ export declare const VAULT_SPEED = 4;
115
+ export declare const VAULT_UP = 6;
25
116
  /**
26
117
  * Host ticks the history of the players goes back (0.5 s): the host rewinds the others to the
27
118
  * view tick of an input, clamped to this.
@@ -1,16 +1,19 @@
1
1
  import type { WorldDef } from './world';
2
2
  /**
3
- * A parkour test course, laid out for the moves of step 3 and walkable now with run, jump and the
4
- * 0.5 m step-up. Metres, y up, the course runs north (toward -z). Jumps reach about 4 m on the
5
- * flat, and a box 1.7 m high.
3
+ * A parkour test course, laid out for the moves of step 3 and flown with them by test/lines.ts.
4
+ * Metres, y up, the course runs north (toward -z). A walking jump flies about 4.2 m on the flat, a
5
+ * sprint jump 6.3 m, and reaches a box 1.7 m high; the hands catch a top 1.2 to 2.4 m above the
6
+ * feet, and a fall that reaches a face within about 3.5 m is caught by them.
6
7
  *
7
- * - A: the start roof (10 m), with the spawns. B: a roof at 10 m, across a 2.5 m gap from A.
8
+ * - A: the start roof (10 m), with the spawns. B: a roof at 10 m, across a 4.2 m gap from A (a
9
+ * run and a jump: walking off the edge falls).
8
10
  * - On B, a platform floating at 12.2 to 12.6 m (an overhang to walk under, a bump for a jump) and
9
11
  * a stair of six 0.4 m steps up to it. 3 m north of it, C (12.4 m): the south face of C above B
10
12
  * is a 2.4 m ledge to grab. Two poles stand by the platform.
11
13
  * - E (8 m), 3 m east of C and 4.4 m lower: a long wall (6.5 m high, 26 m long) to run along, ledges
12
14
  * 2.0 and 2.2 m high, and a 1.2 m low gap under a lintel to slide under, with a way round it.
13
15
  * - G (8 m) is across a 10 m drop from E, reached by a 0.5 m wide beam; it has a 1.6 m ledge.
14
- * - F (12.4 m) is 8 m north of C: out of reach until the dash and the double jump.
16
+ * - F (12.4 m) is 13 m north of C: a sprint and a jump fall short of it, and so does a walk with
17
+ * the dash or the double jump; it takes the sprint and the jump and the dash or the double jump.
15
18
  */
16
19
  export declare const ROOFTOPS: WorldDef;
@@ -5,19 +5,36 @@ export declare const TAU: number;
5
5
  export declare const norm: (a: number) => number;
6
6
  /** Unit vector the player faces at yaw, on the floor plane: yaw 0 looks toward -z. */
7
7
  export declare const forward: (yaw: number) => [x: number, z: number];
8
+ /** The movement fields of a player at rest on a floor, for a state built by hand. */
9
+ export declare const STILL: {
10
+ vx: number;
11
+ vz: number;
12
+ mode: number;
13
+ t: number;
14
+ wall: number;
15
+ edge: number;
16
+ jumps: number;
17
+ dashes: number;
18
+ dash: number;
19
+ coyote: number;
20
+ buffer: number;
21
+ held: number;
22
+ ev: number;
23
+ fall: number;
24
+ };
8
25
  /** A player standing on spawn point n of the world (modulo the count). */
9
26
  export declare const spawn: (id: number, w: World, n: number) => PlayerState;
10
- /**
11
- * Moves the feet by (dx, dz) for 1 / TICK_HZ s, then a jump from the ground, GRAVITY and the
12
- * collisions of the cylinder (PLAYER_R, PLAYER_H) with the brushes of the world: it slides along
13
- * their sides (see push), stands on the highest top under the circle (a top up to STEP_H above the
14
- * feet is climbed at once, a higher one needs a jump, an edge is a fall), and bumps its head on the
15
- * lowest bottom, in place. Feet under the world's kill floor put the player back at a spawn.
16
- * Sides and tops are judged from the feet at the start of the step.
17
- */
18
- export declare const walk: (p: PlayerState, dx: number, dz: number, jump: boolean, w: World) => void;
27
+ /** The height of the cylinder in this mode. */
28
+ export declare const heightOf: (p: Pick<PlayerState, "mode">) => number;
29
+ /** The camera height above the feet in this mode. */
30
+ export declare const eyeHeight: (p: Pick<PlayerState, "mode">) => number;
31
+ /** Horizontal speed, m/s. */
32
+ export declare const speedOf: (p: Pick<PlayerState, "vx" | "vz">) => number;
33
+ /** Running faster than a walk on the ground: the sprint, for the animation to pump the arms. */
34
+ export declare const sprinting: (p: Pick<PlayerState, "mode" | "vx" | "vz">) => boolean;
19
35
  /**
20
36
  * Applies one input for 1 / TICK_HZ s, in place: yaw and pitch from the input (pitch clamped to
21
- * MAX_PITCH), RUN_SPEED along the held keys (diagonals not faster), then walk().
37
+ * MAX_PITCH), then the step of the current mode (see the top of the file). Feet under the world's
38
+ * kill floor put the player back at a spawn.
22
39
  */
23
40
  export declare const move: (p: PlayerState, i: Input, w: World) => void;
@@ -14,7 +14,7 @@ export declare const MSG_INPUT = 1;
14
14
  export declare const MSG_SNAPSHOT = 2;
15
15
  export declare const INPUT_BYTES = 11;
16
16
  export declare const SNAP_HEAD = 4;
17
- export declare const SNAP_PLAYER = 24;
17
+ export declare const SNAP_PLAYER = 39;
18
18
  /**
19
19
  * The input as the host will read it after encodeInput and decodeInput: yaw, pitch and view
20
20
  * rounded to the wire (an absent view stays absent here, and reads 0 on the host). The client
@@ -11,6 +11,56 @@ export declare const KEY: {
11
11
  readonly LEFT: 4;
12
12
  readonly RIGHT: 8;
13
13
  readonly JUMP: 16;
14
+ /** Hold to sprint (with forward). */
15
+ readonly SPRINT: 32;
16
+ /** Press to dash. */
17
+ readonly DASH: 64;
18
+ /** Hold to crouch, or to slide when fast. */
19
+ readonly CROUCH: 128;
20
+ };
21
+ /** The movement state of a player: what the body is doing, one of these. */
22
+ export declare const MODE: {
23
+ /** Standing or running on a floor. */
24
+ readonly GROUND: 0;
25
+ /** Off the floor: a jump, a fall, a dash. */
26
+ readonly AIR: 1;
27
+ /** Running along a wall. */
28
+ readonly WALLRUN: 2;
29
+ /** Hanging from a ledge. */
30
+ readonly HANG: 3;
31
+ /** Pulling up onto a ledge. */
32
+ readonly CLIMB: 4;
33
+ /** Sliding on the ground, low. */
34
+ readonly SLIDE: 5;
35
+ /** Walking low. */
36
+ readonly CROUCH: 6;
37
+ };
38
+ /**
39
+ * Which side of the player a wall or a ledge is on (PlayerState.wall). In the air it is the side
40
+ * of the last wall-run or hang, which does not catch the player again until he lands.
41
+ */
42
+ export declare const SIDE: {
43
+ readonly NONE: 0;
44
+ readonly EAST: 1;
45
+ readonly WEST: 2;
46
+ readonly SOUTH: 3;
47
+ readonly NORTH: 4;
48
+ };
49
+ /** Bits of PlayerState.ev: what happened in the last step. */
50
+ export declare const EV: {
51
+ readonly JUMP: 1;
52
+ /** The extra jump in the air. */
53
+ readonly DOUBLE: 2;
54
+ readonly DASH: 4;
55
+ /** Back on a floor: PlayerState.fall is the speed it came down at. */
56
+ readonly LAND: 8;
57
+ readonly GRAB: 16;
58
+ /** The climb ended standing on the top. */
59
+ readonly CLIMB: 32;
60
+ readonly WALLRUN_START: 64;
61
+ readonly WALLRUN_END: 128;
62
+ readonly SLIDE_START: 256;
63
+ readonly SLIDE_END: 512;
14
64
  };
15
65
  /** One client step: what the player held during 1 / TICK_HZ s, and where they looked. */
16
66
  export interface Input {
@@ -30,18 +80,44 @@ export interface Input {
30
80
  */
31
81
  view?: number;
32
82
  }
83
+ /**
84
+ * One player, float32 where it is a number with a fraction. All of it but the last two fields goes
85
+ * on the wire (see protocol.ts) and is what move() needs to continue from.
86
+ */
33
87
  export interface PlayerState {
34
88
  id: number;
35
89
  /** Feet position. */
36
90
  x: number;
37
91
  y: number;
38
92
  z: number;
39
- /** Vertical speed, m/s. */
93
+ /** Speed, m/s. */
94
+ vx: number;
40
95
  vy: number;
96
+ vz: number;
41
97
  yaw: number;
42
98
  pitch: number;
43
- /** Standing on a floor: a jump is allowed. */
99
+ /** On a floor, derived from mode (ground, slide, crouch): not sent. */
44
100
  ground: boolean;
101
+ /** MODE, and the ticks spent in it (saturates at 255). */
102
+ mode: number;
103
+ t: number;
104
+ /** SIDE of the wall being run on, the ledge hung from, or the last one (see SIDE). */
105
+ wall: number;
106
+ /** Height of the ledge top being hung from or climbed, else 0. */
107
+ edge: number;
108
+ /** Extra jumps and air dashes left: refilled on landing, a wall-run and a ledge grab. */
109
+ jumps: number;
110
+ dashes: number;
111
+ /** Ticks to the next dash; the first DASH_TICKS of DASH_COOLDOWN are the burst. */
112
+ dash: number;
113
+ /** Ticks left in which a jump still works after walking off an edge, and in which a press is kept. */
114
+ coyote: number;
115
+ buffer: number;
116
+ /** KEY.JUMP and KEY.DASH as held in the last step, to tell a press from a hold. */
117
+ held: number;
118
+ /** For the animation, not sent: the EV bits of the last step, and the speed of a landing. */
119
+ ev: number;
120
+ fall: number;
45
121
  }
46
122
  /** Procedural textures, generated by the client. A brush names one for its top and one for its sides. */
47
123
  export declare const TEX: {
@@ -58,16 +134,8 @@ export declare const TEX: {
58
134
  /** Metal floor grating over darkness. */
59
135
  readonly GRATE: 7;
60
136
  };
61
- /** One player in a snapshot. */
62
- export interface SnapPlayer {
63
- id: number;
64
- x: number;
65
- y: number;
66
- z: number;
67
- vy: number;
68
- yaw: number;
69
- pitch: number;
70
- ground: boolean;
137
+ /** One player in a snapshot: the state (its ev and fall read 0) and the ack. */
138
+ export interface SnapPlayer extends PlayerState {
71
139
  /** Seq of the last input of this player the host applied: the client replays the ones after. */
72
140
  ack: number;
73
141
  }
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@korial29/tiny-fps",
3
3
  "description": "Tiny multiplayer first-person rooftop game for the browser: raw WebGL, zero runtime dependencies, WebSocket or same-browser multi-tab play.",
4
4
  "license": "Apache-2.0",
5
- "version": "0.13.0",
5
+ "version": "0.14.0",
6
6
  "type": "module",
7
7
  "files": [
8
8
  "dist"