@korial29/tiny-fps 0.1.0 → 0.3.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.js +5 -5
- package/dist/local.js +1 -1
- package/dist/server.js +703 -44
- package/dist/types/client/effects.d.ts +93 -0
- package/dist/types/client/gun.d.ts +19 -0
- package/dist/types/client/input.d.ts +1 -1
- package/dist/types/client/mesh.d.ts +24 -0
- package/dist/types/client/monsters.d.ts +36 -0
- package/dist/types/client/predict.d.ts +18 -2
- package/dist/types/client/render.d.ts +10 -15
- package/dist/types/shared/constants.d.ts +40 -0
- package/dist/types/shared/monsters.d.ts +17 -0
- package/dist/types/shared/physics.d.ts +26 -6
- package/dist/types/shared/protocol.d.ts +8 -3
- package/dist/types/shared/room.d.ts +20 -3
- package/dist/types/shared/shoot.d.ts +33 -0
- package/dist/types/shared/types.d.ts +92 -0
- package/package.json +1 -1
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import type { Bullet, GameEvent, Level, Monster, PlayerState, Shot } from '../shared/types';
|
|
2
|
+
import type { Vec3 } from './mesh';
|
|
3
|
+
export interface Spark {
|
|
4
|
+
p: Vec3;
|
|
5
|
+
v: Vec3;
|
|
6
|
+
/** Seconds left. */
|
|
7
|
+
life: number;
|
|
8
|
+
/** Flat colour, orange when absent. */
|
|
9
|
+
c?: number[];
|
|
10
|
+
}
|
|
11
|
+
/** A chunk of a dead monster: it flies, lands on the floor below, then shrinks away. */
|
|
12
|
+
export interface Gib {
|
|
13
|
+
p: Vec3;
|
|
14
|
+
v: Vec3;
|
|
15
|
+
yaw: number;
|
|
16
|
+
/** Half the side of its cube, metres. */
|
|
17
|
+
size: number;
|
|
18
|
+
c: number[];
|
|
19
|
+
/** Seconds left. */
|
|
20
|
+
life: number;
|
|
21
|
+
/** Floor height under it, as of the last sector it was in. */
|
|
22
|
+
floor: number;
|
|
23
|
+
/** Landed for good. */
|
|
24
|
+
rest: boolean;
|
|
25
|
+
}
|
|
26
|
+
/** An explosion: a glow of radius r at its biggest, t seconds old. */
|
|
27
|
+
export interface Blast {
|
|
28
|
+
p: Vec3;
|
|
29
|
+
r: number;
|
|
30
|
+
t: number;
|
|
31
|
+
}
|
|
32
|
+
/** What the client keeps of each monster it has drawn. */
|
|
33
|
+
export interface Walker {
|
|
34
|
+
x: number;
|
|
35
|
+
z: number;
|
|
36
|
+
/** Distance covered, metres: the limbs swing with it. */
|
|
37
|
+
walked: number;
|
|
38
|
+
kind: number;
|
|
39
|
+
}
|
|
40
|
+
/** What the renderer and the camera read; update and shot move it. */
|
|
41
|
+
export interface Effects {
|
|
42
|
+
/** Vertical eye offset, metres. */
|
|
43
|
+
bob: number;
|
|
44
|
+
/** Radians, positive leans right (strafing right). */
|
|
45
|
+
roll: number;
|
|
46
|
+
/** Camera yaw and pitch jitter, radians. */
|
|
47
|
+
shakeYaw: number;
|
|
48
|
+
shakePitch: number;
|
|
49
|
+
/** Recoil, 1 at a shot down to 0. */
|
|
50
|
+
kick: number;
|
|
51
|
+
/** Barrel angle, radians. */
|
|
52
|
+
spin: number;
|
|
53
|
+
/** Seconds of own muzzle flash left. */
|
|
54
|
+
flash: number;
|
|
55
|
+
/** Seconds of flash left at each other shooter, by player id. */
|
|
56
|
+
flashes: Map<number, number>;
|
|
57
|
+
sparks: Spark[];
|
|
58
|
+
gibs: Gib[];
|
|
59
|
+
blasts: Blast[];
|
|
60
|
+
/** Seconds of the red hurt tint left. */
|
|
61
|
+
hurt: number;
|
|
62
|
+
/** Seconds of blow left at each monster, by id. */
|
|
63
|
+
swings: Map<number, number>;
|
|
64
|
+
/** Every monster seen so far, by id. Ids are bounded, so it is too. */
|
|
65
|
+
herd: Map<number, Walker>;
|
|
66
|
+
/** Unit heading of each bullet in flight, by id, once it has moved. */
|
|
67
|
+
aim: Map<number, Vec3>;
|
|
68
|
+
/** Advances dt seconds: the own player (eased, undefined before the first snapshot) and the held keys. */
|
|
69
|
+
update(me: PlayerState | undefined, keys: number, dt: number): void;
|
|
70
|
+
/**
|
|
71
|
+
* A shot, from the shooter's eye; own ones kick the camera and the gun. No sparks with
|
|
72
|
+
* `sparks` false: a shot that hit the viewer would burst them inside the camera.
|
|
73
|
+
*/
|
|
74
|
+
shot(s: Shot, from: Vec3, own: boolean, sparks?: boolean): void;
|
|
75
|
+
/** The monsters and bullets drawn this frame: how far each walked, which way each bullet flies. */
|
|
76
|
+
track(monsters: Monster[], bullets: Bullet[]): void;
|
|
77
|
+
/** A host event, once delayed to line up with the drawn monsters; `you` is the own player id. */
|
|
78
|
+
event(e: GameEvent, you: number): void;
|
|
79
|
+
}
|
|
80
|
+
export declare const BLAST = 0.3;
|
|
81
|
+
/** Half the side of an explosion's glow: it swells fast, then shrinks to nothing as it ends. */
|
|
82
|
+
export declare const glow: ({ r, t }: Blast) => number;
|
|
83
|
+
/**
|
|
84
|
+
* Holds what the host reported at `now` for INTERP_DELAY ms, so it fires in step with the others,
|
|
85
|
+
* who are drawn that far in the past. Keeps at most `max`: a hidden tab does not grow it.
|
|
86
|
+
*/
|
|
87
|
+
export declare const createDelay: <T>(max?: number) => {
|
|
88
|
+
push(items: T[], now: number): void;
|
|
89
|
+
/** What is due at `now`, oldest first. */
|
|
90
|
+
due(now: number): T[];
|
|
91
|
+
};
|
|
92
|
+
/** rand in [0, 1): injectable so the tests are exact. The level lets gibs land and bounce off walls. */
|
|
93
|
+
export declare const createEffects: (rand?: () => number, lvl?: Level) => Effects;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
type Vec3 = [number, number, number];
|
|
2
|
+
/** Where the barrels end, on -z: the muzzle flash sits just past it. */
|
|
3
|
+
export declare const MUZZLE = 0.55;
|
|
4
|
+
/**
|
|
5
|
+
* Revolves a profile of [radius, length] points around the axis through `at` along -z: `length`
|
|
6
|
+
* runs forward from `at`. Returns triangles as x, y, z, nx, ny, nz per vertex, `sides` quads of 2
|
|
7
|
+
* triangles per profile segment. The profile goes from back to front and closes on the axis, so
|
|
8
|
+
* the normals (smooth around the ring, flat along the profile) point outward.
|
|
9
|
+
*/
|
|
10
|
+
export declare const lathe: (profile: [number, number][], sides: number, at?: Vec3) => number[];
|
|
11
|
+
/**
|
|
12
|
+
* The chaingun, built once per level of detail: `barrels` turn around the central axis (the caller
|
|
13
|
+
* spins them), the `body` stays. Vertices as in lathe.
|
|
14
|
+
*/
|
|
15
|
+
export declare const chaingun: (sides: number) => {
|
|
16
|
+
barrels: number[];
|
|
17
|
+
body: number[];
|
|
18
|
+
};
|
|
19
|
+
export {};
|
|
@@ -13,5 +13,5 @@ export interface Controls {
|
|
|
13
13
|
/** Removes every listener. */
|
|
14
14
|
destroy(): void;
|
|
15
15
|
}
|
|
16
|
-
/** Click the canvas to lock the pointer; while locked the mouse looks
|
|
16
|
+
/** Click the canvas to lock the pointer; while locked the mouse looks, the keys move, the left button fires. */
|
|
17
17
|
export declare const createInput: (canvas: HTMLCanvasElement) => Controls;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export type Vec3 = [number, number, number];
|
|
2
|
+
/**
|
|
3
|
+
* Floats per vertex: position, normal, then c. A map vertex has c = (u, v, texture layer, light),
|
|
4
|
+
* u and v in metres. A player vertex has layer -1 and its flat colour in c.x, c.y, c.w; layer -2 is
|
|
5
|
+
* the same colour, bright and unlit (flashes, sparks, eyes).
|
|
6
|
+
*/
|
|
7
|
+
export declare const STRIDE = 10;
|
|
8
|
+
/**
|
|
9
|
+
* Camera axes [right, up, forward]: forward is (-sin yaw cos pitch, sin pitch, -cos yaw cos pitch),
|
|
10
|
+
* y up at roll 0. A positive roll leans the camera right: up tilts toward right.
|
|
11
|
+
*/
|
|
12
|
+
export declare const basis: (yaw: number, pitch: number, roll?: number) => [Vec3, Vec3, Vec3];
|
|
13
|
+
/**
|
|
14
|
+
* Appends the 36 vertices of a box centred on (cx, cy, cz) with half sizes (hx, hy, hz), turned
|
|
15
|
+
* by yaw about its own vertical axis (local forward, -z, ends up along forward(yaw)).
|
|
16
|
+
*/
|
|
17
|
+
export declare const pushBox: (out: number[], [cx, cy, cz]: Vec3, [hx, hy, hz]: Vec3, yaw: number, color: number[], layer?: number) => void;
|
|
18
|
+
/** gun.ts vertices (position, normal) given a flat colour, in the vertex format above. */
|
|
19
|
+
export declare const dress: (v: number[], [r, g, b]: number[]) => number[];
|
|
20
|
+
/**
|
|
21
|
+
* Appends gun-space vertices (x right, y up, -z forward) scaled by k, turned by spin about z, then
|
|
22
|
+
* carried to the world: axes `[r, u, f]` at eye, moved by off = (right, up, forward) along them.
|
|
23
|
+
*/
|
|
24
|
+
export declare const place: (out: number[], src: number[], eye: Vec3, [r, u, f]: Vec3[], off: Vec3, k: number, spin?: number) => void;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { Bullet, Monster } from '../shared/types';
|
|
2
|
+
import type { Vec3 } from './mesh';
|
|
3
|
+
/** Seconds a walker's blow takes. */
|
|
4
|
+
export declare const SWING = 0.3;
|
|
5
|
+
/** Limb phase per metre walked: a full stride every 1.75 m. */
|
|
6
|
+
export declare const GAIT = 3.6;
|
|
7
|
+
/** Flat colours, shared with the gibs. Eyes are the unlit layer: they glow in the dark. */
|
|
8
|
+
export declare const COLOR: {
|
|
9
|
+
body: number[];
|
|
10
|
+
arm: number[];
|
|
11
|
+
leg: number[];
|
|
12
|
+
head: number[];
|
|
13
|
+
shell: number[];
|
|
14
|
+
fin: number[];
|
|
15
|
+
gore: number[];
|
|
16
|
+
calm: number[];
|
|
17
|
+
chase: number[];
|
|
18
|
+
blob: number[];
|
|
19
|
+
core: number[];
|
|
20
|
+
};
|
|
21
|
+
/** What a monster of each MONSTER kind bursts into. */
|
|
22
|
+
export declare const CHUNKS: number[][][];
|
|
23
|
+
/** Arm angle during a blow, `s` in (0, 1] the share of SWING elapsed: wind up, then strike. */
|
|
24
|
+
export declare const strike: (s: number) => number;
|
|
25
|
+
/**
|
|
26
|
+
* Appends a walker: `walked` is the distance it has covered (the limbs swing with it, so a
|
|
27
|
+
* standing one is still), `blow` the share of SWING elapsed, 0 when not swinging.
|
|
28
|
+
*/
|
|
29
|
+
export declare const walker: (out: number[], m: Monster, walked: number, blow: number) => void;
|
|
30
|
+
/** Appends a flyer at time t (seconds): it bobs on a slow sine and its fins spin. */
|
|
31
|
+
export declare const flyer: (out: number[], m: Monster, t: number) => void;
|
|
32
|
+
/**
|
|
33
|
+
* Appends a bullet: a glowing blob with a short tail behind it when `dir` (unit, its heading) is
|
|
34
|
+
* known.
|
|
35
|
+
*/
|
|
36
|
+
export declare const bullet: (out: number[], b: Bullet, dir?: Vec3) => void;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Input, Level, PlayerState, Snapshot } from '../shared/types';
|
|
1
|
+
import type { Bullet, Input, Level, Monster, PlayerState, Shot, Snapshot } from '../shared/types';
|
|
2
2
|
export interface Predictor {
|
|
3
3
|
/** The own player id, from the roster. Snapshots before it are only used for the others. */
|
|
4
4
|
you(id: number): void;
|
|
@@ -7,8 +7,15 @@ export interface Predictor {
|
|
|
7
7
|
* own player with it, keeps it for replay, and returns it for the caller to send (the very
|
|
8
8
|
* values predicted with, which the host reads back). Until the own player is known (no snapshot
|
|
9
9
|
* shows it yet) it only numbers and keeps the input: the first snapshot replays it.
|
|
10
|
+
* With `now` (ms, performance.now()) the input carries the view tick the others are drawn at.
|
|
10
11
|
*/
|
|
11
|
-
step(i: Omit<Input, 'seq'
|
|
12
|
+
step(i: Omit<Input, 'seq' | 'view'>, now?: number): Input;
|
|
13
|
+
/**
|
|
14
|
+
* The shot the last step fired, resolved with fire() against others(now) and monsters(now) of
|
|
15
|
+
* that step (a monster this predictor already shot dead, and the host has not acked yet, is not
|
|
16
|
+
* a target); undefined when it did not fire. Replays on a snapshot never fire again.
|
|
17
|
+
*/
|
|
18
|
+
shot(): Shot | undefined;
|
|
12
19
|
/** A decoded snapshot, received at `now` (ms, performance.now()). */
|
|
13
20
|
snapshot(s: Snapshot, now: number): void;
|
|
14
21
|
/**
|
|
@@ -19,5 +26,14 @@ export interface Predictor {
|
|
|
19
26
|
me(alpha: number): PlayerState | undefined;
|
|
20
27
|
/** The other players at now - INTERP_DELAY; a player missing from the latest snapshot is gone. */
|
|
21
28
|
others(now: number): PlayerState[];
|
|
29
|
+
/**
|
|
30
|
+
* The living monsters at now - INTERP_DELAY, interpolated like others() (yaw along the shortest
|
|
31
|
+
* arc). Unlike others(), presence follows the newer snapshot of the interpolation pair, not the
|
|
32
|
+
* latest: a monster is drawn until the pair reaches the first snapshot that lacks it, as late
|
|
33
|
+
* as its DIE event is shown.
|
|
34
|
+
*/
|
|
35
|
+
monsters(now: number): Monster[];
|
|
36
|
+
/** The bullets in flight at now - INTERP_DELAY, interpolated and kept the same way (until their BOOM). */
|
|
37
|
+
bullets(now: number): Bullet[];
|
|
22
38
|
}
|
|
23
39
|
export declare const createPredictor: (lvl: Level) => Predictor;
|
|
@@ -1,28 +1,23 @@
|
|
|
1
|
-
import type { Edge, Level, PlayerState } from '../shared/types';
|
|
2
|
-
type
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
*/
|
|
7
|
-
export declare const viewProj: (eye: Vec3, yaw: number, pitch: number, aspect: number) => Float32Array<ArrayBuffer>;
|
|
1
|
+
import type { Bullet, Edge, Level, Monster, PlayerState } from '../shared/types';
|
|
2
|
+
import type { Effects } from './effects';
|
|
3
|
+
import type { Vec3 } from './mesh';
|
|
4
|
+
/** Column-major view-projection matrix of basis(yaw, pitch, roll) at eye, 70° vertical field of view. */
|
|
5
|
+
export declare const viewProj: (eye: Vec3, yaw: number, pitch: number, aspect: number, roll?: number) => Float32Array<ArrayBuffer>;
|
|
8
6
|
/** A world point on screen, in [0, 1] from the top left; undefined behind the camera. */
|
|
9
7
|
export declare const project: (m: Float32Array, x: number, y: number, z: number) => [number, number] | undefined;
|
|
10
|
-
/**
|
|
11
|
-
* Appends the 36 vertices of a box centred on (cx, cy, cz) with half sizes (hx, hy, hz), turned
|
|
12
|
-
* by yaw about its own vertical axis (local forward, -z, ends up along forward(yaw)).
|
|
13
|
-
*/
|
|
14
|
-
export declare const pushBox: (out: number[], [cx, cy, cz]: Vec3, [hx, hy, hz]: Vec3, yaw: number, color: number[]) => void;
|
|
15
8
|
/** Vertical spans [y0, y1] an edge draws on its own side: the wall, or the steps of a portal. */
|
|
16
9
|
export declare const spans: ({ map }: Level, { front, back }: Edge) => [number, number][];
|
|
17
10
|
/** Floors and ceilings as fans, then every edge's walls as quads, in the vertex format above. */
|
|
18
11
|
export declare const levelVerts: (lvl: Level) => number[];
|
|
19
12
|
export interface Renderer {
|
|
20
|
-
/**
|
|
21
|
-
|
|
13
|
+
/**
|
|
14
|
+
* Draws one frame: the map, the others, the monsters, their bullets and the effects as seen from
|
|
15
|
+
* eye, with the view model on top. colors maps player id to CSS.
|
|
16
|
+
*/
|
|
17
|
+
frame(eye: Vec3, yaw: number, pitch: number, others: PlayerState[], colors: ReadonlyMap<number, string>, fx: Effects, monsters: Monster[], bullets: Bullet[]): void;
|
|
22
18
|
/** A world point on screen as of the last frame, in [0, 1] from the top left. */
|
|
23
19
|
project(x: number, y: number, z: number): [number, number] | undefined;
|
|
24
20
|
destroy(): void;
|
|
25
21
|
}
|
|
26
22
|
/** Throws when the browser has no WebGL2. */
|
|
27
23
|
export declare const createRenderer: (canvas: HTMLCanvasElement, lvl: Level) => Renderer;
|
|
28
|
-
export {};
|
|
@@ -22,12 +22,52 @@ 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
|
+
/** Steps between two chaingun shots while the trigger is held: 60 / 5 = 12 shots per second. */
|
|
26
|
+
export declare const FIRE_EVERY = 5;
|
|
27
|
+
/** Speed factor while the trigger is held. */
|
|
28
|
+
export declare const FIRE_SLOW = 0.5;
|
|
29
|
+
/** Metres a shot reaches. */
|
|
30
|
+
export declare const RANGE = 100;
|
|
31
|
+
/** Largest angle, in radians, between a shot and the view direction. */
|
|
32
|
+
export declare const SPREAD = 0.02;
|
|
33
|
+
/**
|
|
34
|
+
* Host ticks a shot may look back (0.5 s): the host rewinds the targets to the view tick of the
|
|
35
|
+
* input that fired, clamped to this.
|
|
36
|
+
*/
|
|
37
|
+
export declare const MAX_REWIND = 30;
|
|
25
38
|
/**
|
|
26
39
|
* Host-side speed-hack guard: each player earns one input per host tick and may bank up to
|
|
27
40
|
* INPUT_BURST of them; inputs beyond the bank are dropped (and not acked).
|
|
28
41
|
*/
|
|
29
42
|
export declare const INPUT_BURST = 10;
|
|
30
43
|
export declare const MAX_PLAYERS = 16;
|
|
44
|
+
/** Monsters alive at once at most; their ids run from MAX_PLAYERS to MAX_PLAYERS + MAX_MONSTERS - 1. */
|
|
45
|
+
export declare const MAX_MONSTERS = 64;
|
|
46
|
+
export declare const WALKER_R = 0.4;
|
|
47
|
+
export declare const WALKER_H = 1.8;
|
|
48
|
+
export declare const WALKER_SPEED = 2.5;
|
|
49
|
+
export declare const FLYER_R = 0.45;
|
|
50
|
+
export declare const FLYER_H = 0.9;
|
|
51
|
+
/** A flyer's underside floats this high above the floor below it, ceilings allowing. */
|
|
52
|
+
export declare const FLYER_HOVER = 1.2;
|
|
53
|
+
export declare const FLYER_SPEED = 3.5;
|
|
54
|
+
/** Each monster looks for players once per SIGHT_EVERY ticks (staggered), in a cone this wide. */
|
|
55
|
+
export declare const SIGHT_EVERY = 60;
|
|
56
|
+
export declare const SIGHT_COS: number;
|
|
57
|
+
export declare const SIGHT_RANGE = 30;
|
|
58
|
+
/** A monster that sees a player or dies alerts the others within ALERT_R that can see it. */
|
|
59
|
+
export declare const ALERT_R = 12;
|
|
60
|
+
/** A chasing monster this close (centre to centre, on the floor plane) swings, once per MELEE_EVERY. */
|
|
61
|
+
export declare const MELEE_R = 1.4;
|
|
62
|
+
export declare const MELEE_EVERY = 45;
|
|
63
|
+
/** A chasing walker that sees its target fires once per SHOOT_EVERY ticks. */
|
|
64
|
+
export declare const SHOOT_EVERY = 90;
|
|
65
|
+
export declare const BULLET_SPEED = 12;
|
|
66
|
+
export declare const BULLET_R = 0.15;
|
|
67
|
+
/** Ticks a bullet lives at most. */
|
|
68
|
+
export declare const BULLET_LIFE = 300;
|
|
69
|
+
/** Ticks after the last monster dies before the map's monsters come back (until step 4). */
|
|
70
|
+
export declare const RESPAWN_AFTER = 300;
|
|
31
71
|
/** Longest player name, in characters (code points). */
|
|
32
72
|
export declare const MAX_NAME = 16;
|
|
33
73
|
export declare const RULES: Rules;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Bullet, GameEvent, Level, Monster, PlayerState } from './types';
|
|
2
|
+
/** Mulberry32: a small seeded generator, uniform in [0, 1). */
|
|
3
|
+
export declare const mulberry32: (seed: number) => (() => number);
|
|
4
|
+
export interface Horde {
|
|
5
|
+
/** The living monsters, copied. */
|
|
6
|
+
monsters(): Monster[];
|
|
7
|
+
/** The bullets in flight, copied. */
|
|
8
|
+
bullets(): Bullet[];
|
|
9
|
+
/** What happened since the owner last emptied it: the owner takes events off the front. */
|
|
10
|
+
events: GameEvent[];
|
|
11
|
+
/** One host tick, with the players' states. */
|
|
12
|
+
step(players: PlayerState[]): void;
|
|
13
|
+
/** A player's shot killed monster `id`: it dies at once, and the monsters that see it hunt `by`. */
|
|
14
|
+
kill(id: number, by: number): void;
|
|
15
|
+
}
|
|
16
|
+
/** The monsters of the level's map, from the first spawn. */
|
|
17
|
+
export declare const createHorde: (lvl: Level, rand: () => number) => Horde;
|
|
@@ -1,17 +1,37 @@
|
|
|
1
1
|
import type { Input, Level, PlayerState } from './types';
|
|
2
|
+
type Pos = Pick<PlayerState, 'x' | 'y' | 'z'>;
|
|
3
|
+
/** What walk() moves: a player, or a walker with the same feet, speed and floor contact. */
|
|
4
|
+
type Walker = Pos & Pick<PlayerState, 'vy' | 'ground'>;
|
|
2
5
|
export declare const TAU: number;
|
|
3
6
|
/** An angle in [0, 2π). */
|
|
4
7
|
export declare const norm: (a: number) => number;
|
|
5
8
|
/** Unit vector the player faces at yaw, on the floor plane: yaw 0 looks toward -z. */
|
|
6
9
|
export declare const forward: (yaw: number) => [x: number, z: number];
|
|
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 sectors of the level: it slides along
|
|
13
|
+
* shut edges (see shut and push), stands on the highest floor under the circle (a step up to
|
|
14
|
+
* STEP_H is climbed at once, a higher one needs a jump, a ledge is a fall), and bumps its head on
|
|
15
|
+
* the lowest ceiling. Players and walkers share it, in place.
|
|
16
|
+
*/
|
|
17
|
+
export declare const walk: (p: Walker, dx: number, dz: number, jump: boolean, lvl: Level) => void;
|
|
18
|
+
/**
|
|
19
|
+
* Moves a flyer (underside at p.y, radius FLYER_R, height FLYER_H) by (dx, dy, dz): sideways it is
|
|
20
|
+
* stopped by solid walls and by openings too small for it, and it never steps (it flies over
|
|
21
|
+
* steps and pits); then y is held between FLYER_HOVER above the highest floor under it and the
|
|
22
|
+
* lowest ceiling minus FLYER_H, the ceiling winning in a low room. Below the hover height it
|
|
23
|
+
* rises at FLYER_SPEED, so crossing from a pit to a ledge is not a jump; the floor itself is
|
|
24
|
+
* never entered.
|
|
25
|
+
*/
|
|
26
|
+
export declare const fly: (p: Pos, dx: number, dy: number, dz: number, lvl: Level) => void;
|
|
7
27
|
/**
|
|
8
28
|
* Applies one input for 1 / TICK_HZ s, in place: yaw and pitch from the input (pitch clamped to
|
|
9
|
-
* MAX_PITCH), RUN_SPEED along the held keys (diagonals not faster),
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* bumps its head on the lowest ceiling.
|
|
29
|
+
* MAX_PITCH), RUN_SPEED along the held keys (diagonals not faster), then walk().
|
|
30
|
+
* The chaingun: `cool` counts down one per step, RUN_SPEED is scaled by FIRE_SLOW while KEY.FIRE
|
|
31
|
+
* is held, and the step that finds it held with `cool` at 0 re-arms it to FIRE_EVERY and returns
|
|
32
|
+
* true: the caller resolves a shot (one every FIRE_EVERY steps, the first at once).
|
|
14
33
|
*/
|
|
15
|
-
export declare const move: (p: PlayerState, i: Input, lvl: Level) =>
|
|
34
|
+
export declare const move: (p: PlayerState, i: Input, lvl: Level) => boolean;
|
|
16
35
|
/** A player standing on the floor at spawn point n of the map (modulo the count). */
|
|
17
36
|
export declare const spawn: (id: number, lvl: Level, n: number) => PlayerState;
|
|
37
|
+
export {};
|
|
@@ -12,12 +12,17 @@ export type ServerText = {
|
|
|
12
12
|
};
|
|
13
13
|
export declare const MSG_INPUT = 1;
|
|
14
14
|
export declare const MSG_SNAPSHOT = 2;
|
|
15
|
-
export declare const INPUT_BYTES =
|
|
15
|
+
export declare const INPUT_BYTES = 11;
|
|
16
16
|
export declare const SNAP_HEAD = 4;
|
|
17
17
|
export declare const SNAP_PLAYER = 24;
|
|
18
|
+
export declare const SNAP_SHOT = 14;
|
|
19
|
+
export declare const SNAP_MONSTER = 16;
|
|
20
|
+
export declare const SNAP_BULLET = 13;
|
|
21
|
+
export declare const SNAP_EVENT = 14;
|
|
18
22
|
/**
|
|
19
|
-
* The input as the host will read it after encodeInput and decodeInput: yaw
|
|
20
|
-
* the wire
|
|
23
|
+
* The input as the host will read it after encodeInput and decodeInput: yaw, pitch and view
|
|
24
|
+
* rounded to the wire (an absent view stays absent here, and reads 0 on the host). The client
|
|
25
|
+
* predicts with this, so both sides move with the same values.
|
|
21
26
|
*/
|
|
22
27
|
export declare const onWire: (i: Input) => Input;
|
|
23
28
|
export declare const encodeInput: (i: Input) => ArrayBuffer;
|
|
@@ -7,18 +7,28 @@ import type { Conn, Every, Rules } from './types';
|
|
|
7
7
|
* taken by another player; 'full' beyond rules.maxPlayers. Everyone gets a fresh 'roster' on
|
|
8
8
|
* every join and leave.
|
|
9
9
|
* - Each input frame moves its player at once with move(); the INPUT_BURST bank drops the excess.
|
|
10
|
-
*
|
|
10
|
+
* A step that fires resolves its shot at once with fire(), the other players rewound to the
|
|
11
|
+
* view tick of the input (lag compensation, at most MAX_REWIND ticks back).
|
|
12
|
+
* - The room owns the horde (monsters.ts), stepped every tick while a player is present. A shot
|
|
13
|
+
* rewinds the monsters with the players, and one that hits a monster kills it.
|
|
14
|
+
* - Every SNAP_EVERY ticks of TICK_HZ, one snapshot with every player, the shots resolved since
|
|
15
|
+
* the last one, the monsters, the bullets and the events since the last one goes to every peer.
|
|
11
16
|
*/
|
|
12
17
|
export declare class Room {
|
|
13
18
|
/** Every connected peer; null until it joins. */
|
|
14
19
|
private peers;
|
|
15
20
|
private ticks;
|
|
21
|
+
/** The players and monsters as each snapshot of the last MAX_REWIND ticks sent them. */
|
|
22
|
+
private history;
|
|
23
|
+
private horde;
|
|
24
|
+
/** Shots resolved since the last snapshot. */
|
|
25
|
+
private shots;
|
|
16
26
|
private halt;
|
|
17
27
|
private rules;
|
|
18
28
|
/** Round-robin cursor over the spawn points, used once they are all taken. */
|
|
19
29
|
private rr;
|
|
20
|
-
/** Missing rules fall back to RULES. */
|
|
21
|
-
constructor(rules?: Partial<Rules
|
|
30
|
+
/** Missing rules fall back to RULES. The seed drives the monsters. */
|
|
31
|
+
constructor(rules?: Partial<Rules>, seed?: number);
|
|
22
32
|
/** Number of joined players. */
|
|
23
33
|
get size(): number;
|
|
24
34
|
/** A peer opened. Nothing is sent until it joins. */
|
|
@@ -38,6 +48,13 @@ export declare class Room {
|
|
|
38
48
|
*/
|
|
39
49
|
start(every?: Every): void;
|
|
40
50
|
stop(): void;
|
|
51
|
+
/**
|
|
52
|
+
* The bodies a shot of `shooter` can hit as the shooter drew them at `view` (a u16 tick,
|
|
53
|
+
* fractional, read against the current tick and clamped to the kept history): the other players
|
|
54
|
+
* and the monsters, x, y, z interpolated between the two snapshots around it like the client
|
|
55
|
+
* does. One the history does not hold stands where it is.
|
|
56
|
+
*/
|
|
57
|
+
private rewind;
|
|
41
58
|
private players;
|
|
42
59
|
private join;
|
|
43
60
|
/** Each joined peer gets the full roster with its own id. */
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { Body, Level, Monster, PlayerState, Shot } from './types';
|
|
2
|
+
type Vec3 = [number, number, number];
|
|
3
|
+
/**
|
|
4
|
+
* Unit direction of a shot: the view direction at yaw and pitch (yaw 0, pitch 0 is -z; pitch > 0
|
|
5
|
+
* looks up), turned by at most SPREAD in a direction hashed from seq, so the host and the
|
|
6
|
+
* shooter's client agree on it.
|
|
7
|
+
*/
|
|
8
|
+
export declare const aim: (yaw: number, pitch: number, seq: number) => Vec3;
|
|
9
|
+
/**
|
|
10
|
+
* Distance along the ray from o (inside the map) in the unit direction d to the first wall, step
|
|
11
|
+
* face, floor or ceiling, at most max.
|
|
12
|
+
*
|
|
13
|
+
* Walks the sectors: in the current convex one the ray leaves through the edge with the least
|
|
14
|
+
* crossing distance among the edges it goes outward through, or ends earlier on the floor or the
|
|
15
|
+
* ceiling. A solid edge, or a portal whose opening does not hold the crossing height, ends it.
|
|
16
|
+
*/
|
|
17
|
+
export declare const trace: (lvl: Level, o: Vec3, d: Vec3, max: number) => number;
|
|
18
|
+
/**
|
|
19
|
+
* Distance at which the ray enters the upright cylinder of t (sides and caps), Infinity on a miss:
|
|
20
|
+
* the span of the ray inside the infinite cylinder, cut to the span between the cap planes.
|
|
21
|
+
*/
|
|
22
|
+
export declare const hit: (o: Vec3, d: Vec3, t: Body) => number;
|
|
23
|
+
/** The body a shot sees of a player. */
|
|
24
|
+
export declare const body: ({ id, x, y, z }: PlayerState) => Body;
|
|
25
|
+
/** The body a shot sees of a monster. */
|
|
26
|
+
export declare const monsterBody: ({ id, kind, x, y, z }: Monster) => Body;
|
|
27
|
+
/**
|
|
28
|
+
* Resolves one shot of `shooter` (state after the step that fired) from its eye (feet + EYE)
|
|
29
|
+
* along aim(yaw, pitch, seq): the nearest of the level (trace) and the upright cylinders
|
|
30
|
+
* of `targets` (bodies: players, monsters) other than the shooter, within RANGE.
|
|
31
|
+
*/
|
|
32
|
+
export declare const fire: (lvl: Level, shooter: PlayerState, seq: number, targets: Body[]) => Shot;
|
|
33
|
+
export {};
|
|
@@ -11,6 +11,8 @@ export declare const KEY: {
|
|
|
11
11
|
readonly LEFT: 4;
|
|
12
12
|
readonly RIGHT: 8;
|
|
13
13
|
readonly JUMP: 16;
|
|
14
|
+
/** Left mouse button: the chaingun fires every FIRE_EVERY steps while held. */
|
|
15
|
+
readonly FIRE: 32;
|
|
14
16
|
};
|
|
15
17
|
/** One client step: what the player held during 1 / TICK_HZ s, and where they looked. */
|
|
16
18
|
export interface Input {
|
|
@@ -22,6 +24,13 @@ export interface Input {
|
|
|
22
24
|
yaw: number;
|
|
23
25
|
/** Radians, [-MAX_PITCH, MAX_PITCH]. */
|
|
24
26
|
pitch: number;
|
|
27
|
+
/**
|
|
28
|
+
* Host tick the client drew the others at when it sent this input, fractional (interpolation
|
|
29
|
+
* between two snapshots), in [0, 65536): the host rewinds the targets of a shot to it (lag
|
|
30
|
+
* compensation). The wire keeps 1/256 tick. Absent (no snapshot seen yet) goes out as 0: the host
|
|
31
|
+
* clamps it to the oldest tick it keeps.
|
|
32
|
+
*/
|
|
33
|
+
view?: number;
|
|
25
34
|
}
|
|
26
35
|
export interface PlayerState {
|
|
27
36
|
id: number;
|
|
@@ -35,6 +44,78 @@ export interface PlayerState {
|
|
|
35
44
|
pitch: number;
|
|
36
45
|
/** Standing on a floor: a jump is allowed. */
|
|
37
46
|
ground: boolean;
|
|
47
|
+
/** Steps until the chaingun may fire again, 0 to FIRE_EVERY. */
|
|
48
|
+
cool: number;
|
|
49
|
+
}
|
|
50
|
+
/** One shot, as the host resolved it. */
|
|
51
|
+
export interface Shot {
|
|
52
|
+
/** The shooter. */
|
|
53
|
+
id: number;
|
|
54
|
+
/** Where the shot stopped: a wall, a floor, a ceiling, a target, or RANGE away. */
|
|
55
|
+
x: number;
|
|
56
|
+
y: number;
|
|
57
|
+
z: number;
|
|
58
|
+
/** Id of the player or monster hit, -1 for the level or nothing. */
|
|
59
|
+
hit: number;
|
|
60
|
+
}
|
|
61
|
+
/** Monster kinds. */
|
|
62
|
+
export declare const MONSTER: {
|
|
63
|
+
/** Walks the floors like a player, shoots bullets from afar and hits up close. */
|
|
64
|
+
readonly WALKER: 0;
|
|
65
|
+
/** Hovers above the floor, flies over steps and pits, hits up close. */
|
|
66
|
+
readonly FLYER: 1;
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* A monster as the host runs it and the snapshot carries it. Ids run from MAX_PLAYERS up, so a
|
|
70
|
+
* Shot.hit or an event names a player or a monster with one u8.
|
|
71
|
+
*/
|
|
72
|
+
export interface Monster {
|
|
73
|
+
id: number;
|
|
74
|
+
/** MONSTER kind. */
|
|
75
|
+
kind: number;
|
|
76
|
+
/** Bottom of the body: the feet of a walker, the underside of a flyer. */
|
|
77
|
+
x: number;
|
|
78
|
+
y: number;
|
|
79
|
+
z: number;
|
|
80
|
+
/** Facing, as a player's yaw. */
|
|
81
|
+
yaw: number;
|
|
82
|
+
/** Hunting a player (it saw one, or was alerted), not patrolling. */
|
|
83
|
+
chase: boolean;
|
|
84
|
+
}
|
|
85
|
+
/** A monster's bullet in flight, host-simulated. Ids are a u8 counter that wraps. */
|
|
86
|
+
export interface Bullet {
|
|
87
|
+
id: number;
|
|
88
|
+
x: number;
|
|
89
|
+
y: number;
|
|
90
|
+
z: number;
|
|
91
|
+
}
|
|
92
|
+
/** What a GameEvent reports. */
|
|
93
|
+
export declare const EVENT: {
|
|
94
|
+
/** Monster `id` died, (x, y, z) the middle of its body: gibs and an explosion. */
|
|
95
|
+
readonly DIE: 0;
|
|
96
|
+
/** Player `id` was hit by a bullet or a blow at (x, y, z). Harmless until health (step 4). */
|
|
97
|
+
readonly HURT: 1;
|
|
98
|
+
/** A bullet burst on the level at (x, y, z); `id` is the bullet. */
|
|
99
|
+
readonly BOOM: 2;
|
|
100
|
+
/** Monster `id` swung at its target from (x, y, z), hit or miss. */
|
|
101
|
+
readonly SWING: 3;
|
|
102
|
+
};
|
|
103
|
+
export interface GameEvent {
|
|
104
|
+
/** EVENT type. */
|
|
105
|
+
type: number;
|
|
106
|
+
id: number;
|
|
107
|
+
x: number;
|
|
108
|
+
y: number;
|
|
109
|
+
z: number;
|
|
110
|
+
}
|
|
111
|
+
/** Something a shot can hit: an upright cylinder of radius r from y up to y + h. */
|
|
112
|
+
export interface Body {
|
|
113
|
+
id: number;
|
|
114
|
+
x: number;
|
|
115
|
+
y: number;
|
|
116
|
+
z: number;
|
|
117
|
+
r: number;
|
|
118
|
+
h: number;
|
|
38
119
|
}
|
|
39
120
|
/** Procedural textures, generated by the client. A sector names one per surface. */
|
|
40
121
|
export declare const TEX: {
|
|
@@ -69,6 +150,8 @@ export interface MapDef {
|
|
|
69
150
|
sectors: Sector[];
|
|
70
151
|
/** Spawn points: feet x, z (inside a sector, standing on its floor) and the yaw to face. */
|
|
71
152
|
spawns: [x: number, z: number, yaw: number][];
|
|
153
|
+
/** Monsters at the start: x, z inside a sector, the yaw they face, their MONSTER kind. Absent: none. */
|
|
154
|
+
monsters?: [x: number, z: number, yaw: number, kind: number][];
|
|
72
155
|
}
|
|
73
156
|
/** One directed edge of a sector, see compile in level.ts. */
|
|
74
157
|
export interface Edge {
|
|
@@ -97,6 +180,7 @@ export interface SnapPlayer {
|
|
|
97
180
|
yaw: number;
|
|
98
181
|
pitch: number;
|
|
99
182
|
ground: boolean;
|
|
183
|
+
cool: number;
|
|
100
184
|
/** Seq of the last input of this player the host applied: the client replays the ones after. */
|
|
101
185
|
ack: number;
|
|
102
186
|
}
|
|
@@ -104,6 +188,14 @@ export interface Snapshot {
|
|
|
104
188
|
/** Host tick, u16, wraps. */
|
|
105
189
|
tick: number;
|
|
106
190
|
players: SnapPlayer[];
|
|
191
|
+
/** Shots fired since the previous snapshot, in order. */
|
|
192
|
+
shots: Shot[];
|
|
193
|
+
/** Living monsters. */
|
|
194
|
+
monsters: Monster[];
|
|
195
|
+
/** Bullets in flight. */
|
|
196
|
+
bullets: Bullet[];
|
|
197
|
+
/** Events since the previous snapshot, in order. */
|
|
198
|
+
events: GameEvent[];
|
|
107
199
|
}
|
|
108
200
|
/** Room rules, set by the host. Defaults in constants.ts (RULES). */
|
|
109
201
|
export interface Rules {
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@korial29/tiny-fps",
|
|
3
3
|
"description": "Tiny multiplayer first-person shooter for the browser: raw WebGL, zero runtime dependencies, WebSocket or same-browser multi-tab play.",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
|
-
"version": "0.
|
|
5
|
+
"version": "0.3.0",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"files": [
|
|
8
8
|
"dist"
|