@korial29/tiny-fps 0.5.0 → 0.5.1
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 +239 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,9 +1,245 @@
|
|
|
1
1
|
# tiny-fps
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/@korial29/tiny-fps)
|
|
4
|
+
[](https://www.npmjs.com/package/@korial29/tiny-fps)
|
|
5
|
+
[](https://bundlephobia.com/package/@korial29/tiny-fps)
|
|
6
|
+
[](https://github.com/korial29/tiny-fps/actions/workflows/ci.yml)
|
|
7
|
+
[](package.json)
|
|
8
|
+
[](LICENSE)
|
|
9
|
+
[](https://korial29.github.io/tiny-fps/)
|
|
4
10
|
|
|
5
|
-
|
|
11
|
+
**▶ Play it now: https://korial29.github.io/tiny-fps/**
|
|
12
|
+
|
|
13
|
+
A tiny co-op first-person shooter for the browser, written in TypeScript with raw WebGL2, no game library and no runtime dependency. Players team up against walkers and flyers through five dark levels: find the keys, open the barred gates, reach the goal, then get everyone back to the start. Everything is generated in code: geometry, textures, lights. There are no images, fonts or assets to serve.
|
|
14
|
+
|
|
15
|
+
It is inspired by carlini's [Yet Another Doom Clone](https://github.com/carlini/js13k2019-yet-another-doom-clone) (js13k 2019), and adds multiplayer. Only the ideas are reused, not the code.
|
|
16
|
+
|
|
17
|
+

|
|
18
|
+
|
|
19
|
+

|
|
20
|
+
|
|
21
|
+

|
|
22
|
+
|
|
23
|
+
## Framework-agnostic, light to add
|
|
24
|
+
|
|
25
|
+
`tinyFps()` mounts the game into any element, inside its own Shadow DOM: React, Vue, Angular, Svelte or a plain page, nothing leaks in or out. The game draws everything on one WebGL2 canvas that fills the container.
|
|
26
|
+
|
|
27
|
+
| Entry | What it is | Size (gzip) |
|
|
28
|
+
| --------------------------- | ---------------------------------------------------------------------- | ----------- |
|
|
29
|
+
| `@korial29/tiny-fps` | The game client: rendering, lights and shadows, input, prediction, HUD | ~18 KB |
|
|
30
|
+
| `@korial29/tiny-fps/local` | Same-browser multiplayer, no server: the host runs in a tab | ~9.5 KB |
|
|
31
|
+
| `@korial29/tiny-fps/server` | Node WebSocket server (needs `ws`) | Node only |
|
|
32
|
+
|
|
33
|
+
CI checks the budgets (30 KB and 12 KB) on every pull request.
|
|
34
|
+
|
|
35
|
+
## Live demo
|
|
36
|
+
|
|
37
|
+
https://korial29.github.io/tiny-fps/
|
|
38
|
+
|
|
39
|
+
1. Open the page, pick a name and play: you are alone against the monsters.
|
|
40
|
+
2. For co-op on one machine, open it in a second tab of the same browser and join with another name: the tabs play together, with no server.
|
|
41
|
+
3. To play across devices, pick the server mode and give it the URL of a running `tiny-fps-server`.
|
|
42
|
+
|
|
43
|
+
---
|
|
44
|
+
|
|
45
|
+
## Installation
|
|
46
|
+
|
|
47
|
+
```sh
|
|
6
48
|
npm install @korial29/tiny-fps
|
|
7
49
|
```
|
|
8
50
|
|
|
9
|
-
The unscoped name `tiny-fps` belongs to another package.
|
|
51
|
+
The unscoped name `tiny-fps` belongs to another package. The server entry also needs [`ws`](https://github.com/websockets/ws), an optional peer dependency:
|
|
52
|
+
|
|
53
|
+
```sh
|
|
54
|
+
npm install ws
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
---
|
|
58
|
+
|
|
59
|
+
## Basic usage
|
|
60
|
+
|
|
61
|
+
### Tabs of the same browser, no server
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
import { tinyFps } from '@korial29/tiny-fps';
|
|
65
|
+
import { localSocket } from '@korial29/tiny-fps/local';
|
|
66
|
+
|
|
67
|
+
const game = tinyFps(document.getElementById('game')!, {
|
|
68
|
+
name: 'Ana',
|
|
69
|
+
socket: localSocket('my-room'),
|
|
70
|
+
});
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Every tab that opens `localSocket('my-room')` joins the same game over a [`BroadcastChannel`](https://developer.mozilla.org/en-US/docs/Web/API/BroadcastChannel). One tab hosts the game, elected with [`navigator.locks`](https://developer.mozilla.org/en-US/docs/Web/API/Web_Locks_API); when it closes, another tab takes over with a fresh game. `navigator.locks` needs a secure context: on `http://` from a LAN address, each tab plays alone.
|
|
74
|
+
|
|
75
|
+
### Across devices, with a WebSocket server
|
|
76
|
+
|
|
77
|
+
```ts
|
|
78
|
+
// server.ts (Node)
|
|
79
|
+
import { serve } from '@korial29/tiny-fps/server';
|
|
80
|
+
|
|
81
|
+
const stop = serve({ port: 8787 }); // or serve({ server: existingHttpServer })
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
// in the browser
|
|
86
|
+
import { tinyFps } from '@korial29/tiny-fps';
|
|
87
|
+
|
|
88
|
+
tinyFps(document.getElementById('game')!, {
|
|
89
|
+
name: 'Ana',
|
|
90
|
+
url: 'wss://your-host.example/my-room',
|
|
91
|
+
});
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Each URL path is its own room: `/my-room` and `/other` are two separate games. A ready-made server ships as a command:
|
|
95
|
+
|
|
96
|
+
```sh
|
|
97
|
+
PORT=8787 npx tiny-fps-server
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Sizing
|
|
101
|
+
|
|
102
|
+
The canvas fills its container, at any size and ratio. Give the container a size:
|
|
103
|
+
|
|
104
|
+
```css
|
|
105
|
+
#game {
|
|
106
|
+
width: 100vw;
|
|
107
|
+
height: 100dvh;
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
---
|
|
112
|
+
|
|
113
|
+
## API
|
|
114
|
+
|
|
115
|
+
### `tinyFps(container, options)`
|
|
116
|
+
|
|
117
|
+
Mounts the game and joins. Returns `{ on, players, destroy }`.
|
|
118
|
+
|
|
119
|
+
| Option | Type | Description |
|
|
120
|
+
| -------- | ------------ | -------------------------------------------------------------------------------------------- |
|
|
121
|
+
| `name` | `string` | Player name shown above your head (16 characters max). A taken name gets a suffix (`Bob 2`). |
|
|
122
|
+
| `url` | `string` | WebSocket URL, room in the path. Ignored when `socket` is given. |
|
|
123
|
+
| `socket` | `SocketLike` | Any WebSocket-like object, e.g. `localSocket('room')`. |
|
|
124
|
+
|
|
125
|
+
Without WebGL2 the game stays connected and says so on screen.
|
|
126
|
+
|
|
127
|
+
### `on(event, callback)`
|
|
128
|
+
|
|
129
|
+
| Event | When |
|
|
130
|
+
| -------- | -------------------------------------------------------- |
|
|
131
|
+
| `open` | Connected. |
|
|
132
|
+
| `close` | Disconnected. |
|
|
133
|
+
| `roster` | A player joined or left; read the list with `players()`. |
|
|
134
|
+
|
|
135
|
+
```ts
|
|
136
|
+
game.on('roster', () => console.log(game.players().map(p => p.name)));
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### `players()`
|
|
140
|
+
|
|
141
|
+
Everyone in the room, you included: `{ id, name, color }[]`. Each player has a colour of their own.
|
|
142
|
+
|
|
143
|
+
### `destroy()`
|
|
144
|
+
|
|
145
|
+
Leaves the game, stops rendering, releases the pointer and removes the game's DOM. Safe to call twice.
|
|
146
|
+
|
|
147
|
+
### `localSocket(room, rules?)`: `@korial29/tiny-fps/local`
|
|
148
|
+
|
|
149
|
+
A same-browser socket for `tinyFps`. `rules` apply to the game the tab hosts, so pass the same ones from every tab.
|
|
150
|
+
|
|
151
|
+
### `serve({ port | server, rules? })`: `@korial29/tiny-fps/server`
|
|
152
|
+
|
|
153
|
+
Starts the WebSocket server, one room per URL path, and returns an async `stop()`. `rules` apply to every room.
|
|
154
|
+
|
|
155
|
+
### `SocketLike`
|
|
156
|
+
|
|
157
|
+
The subset of `WebSocket` the client uses: `binaryType`, `onopen`, `onclose`, `onmessage`, `send(data)` and `close()`. A real `WebSocket` and `localSocket()` both fit, and so does a relay over a transport you already have. Text frames are JSON, and binary frames (inputs, snapshots) must arrive as an `ArrayBuffer` of exactly their bytes.
|
|
158
|
+
|
|
159
|
+
### Rules
|
|
160
|
+
|
|
161
|
+
| Rule | Default | Description |
|
|
162
|
+
| ------------ | ------- | ----------------------------------------------------------- |
|
|
163
|
+
| `maxPlayers` | `16` | Players in a room; the next ones are told the room is full. |
|
|
164
|
+
|
|
165
|
+
---
|
|
166
|
+
|
|
167
|
+
## Controls
|
|
168
|
+
|
|
169
|
+
| Action | Keys |
|
|
170
|
+
| ------ | ------------------------------------------------------------- |
|
|
171
|
+
| Look | Mouse (click the game to lock the pointer, Esc to release it) |
|
|
172
|
+
| Move | WASD, or the arrow keys |
|
|
173
|
+
| Jump | Space |
|
|
174
|
+
| Fire | Left mouse button, held |
|
|
175
|
+
|
|
176
|
+
Keys are read by their position (`KeyboardEvent.code`), so on an AZERTY keyboard WASD is ZQSD with nothing to set.
|
|
177
|
+
|
|
178
|
+
---
|
|
179
|
+
|
|
180
|
+
## How a game plays
|
|
181
|
+
|
|
182
|
+
- **Co-op**: everyone fights the monsters together; there is no friendly fire. Up to 16 players.
|
|
183
|
+
- **Chaingun**: 12 shots a second, at half speed while you fire. The host decides every hit, and moves the targets back to where you saw them (lag compensation).
|
|
184
|
+
- **Monsters**:
|
|
185
|
+
- Walkers patrol the floors, fire bullets and strike up close.
|
|
186
|
+
- Flyers float over steps and pits.
|
|
187
|
+
- They spot you within 60° of where they face and never through a wall, then alert their neighbours. One shot kills.
|
|
188
|
+
- **Health**: 9 points. A hit takes one, then you can't be hurt for a second. A dead player comes back at the start 3 s later. Every 4th kill leaves a health pack.
|
|
189
|
+
- **Keys and gates**: red, blue and yellow keys open the barred gates of their colour, for the whole team.
|
|
190
|
+
- **The way out**: the start room locks behind you. Touch the goal to open it, then the level is won once every living player is back inside.
|
|
191
|
+
- **Five levels**: Warm-up, Foundry, Cistern, Loop and Citadel. Then the run starts over in hard mode, with faster monsters that strike and fire more often. The end screen shows the time.
|
|
192
|
+
- **Dark levels**: your flashlight casts shadows, and so do muzzle flashes. Explosions, hits, bullets, the goal and the open exit light up their surroundings.
|
|
193
|
+
|
|
194
|
+
---
|
|
195
|
+
|
|
196
|
+
## Features
|
|
197
|
+
|
|
198
|
+
Parity with the js13k game is tracked in [`docs/PARITY.md`](docs/PARITY.md).
|
|
199
|
+
|
|
200
|
+
- [x] Multiplayer over WebSocket, or between tabs of one browser with no server
|
|
201
|
+
- [x] Doom-style sector maps: steps, pits, low ceilings, balconies, procedural brick, tile, lava and stone textures
|
|
202
|
+
- [x] Client-side prediction, exact to the bit, and the other players interpolated
|
|
203
|
+
- [x] Chaingun with lag-compensated hitscan, spinning barrels, recoil, muzzle flash, head bob, sparks
|
|
204
|
+
- [x] Walkers and flyers: patrol, sight cone, alert, chase, bullets, melee, gibs and explosions
|
|
205
|
+
- [x] Health, hit cooldown, death and respawn, health drops, team keys and barred gates
|
|
206
|
+
- [x] Five levels, the goal and the way back, hard mode on the next run, end screen with the time
|
|
207
|
+
- [x] Flashlights with shadow maps, lit muzzle flashes, explosion, hit, bullet, goal and exit lights
|
|
208
|
+
- [ ] Sound: a small synthesiser, stereo by position, looped music
|
|
209
|
+
- [ ] HUD and screens: health bar, damage and pickup fades, low-health cue, title screen
|
|
210
|
+
- [x] Shadow DOM, zero runtime dependencies, size-gated in CI
|
|
211
|
+
|
|
212
|
+
---
|
|
213
|
+
|
|
214
|
+
## How it works
|
|
215
|
+
|
|
216
|
+
- **The host is authoritative**: the Node server, or the host tab in local mode. It runs the players, the monsters, the bullets, the pickups and the levels.
|
|
217
|
+
- **Inputs, not positions**: each client sends one 11-byte input per step at 60 Hz, holding its keys, view angles and the moment it sees the others at. It predicts its own player at once and replays what the host has not acknowledged yet.
|
|
218
|
+
- **Snapshots** go out at 30 Hz: players, shots, monsters, bullets, events and pickups, in a compact binary format. The other players and the monsters are drawn 100 ms in the past, interpolated between snapshots.
|
|
219
|
+
- **Rendering**: one WebGL2 shader program and one vertex buffer, up to 16 lights, with up to 4 shadow maps drawn by the same program.
|
|
220
|
+
|
|
221
|
+
---
|
|
222
|
+
|
|
223
|
+
## Development
|
|
224
|
+
|
|
225
|
+
Node 24 (see `.nvmrc`).
|
|
226
|
+
|
|
227
|
+
```sh
|
|
228
|
+
npm install
|
|
229
|
+
npm start # demo with hot reload (Vite)
|
|
230
|
+
npm run dev:server # WebSocket server on :8787
|
|
231
|
+
npm test # Vitest
|
|
232
|
+
npm run lint # Prettier, ESLint, tsc
|
|
233
|
+
npm run build # dist/: client, local, server, types
|
|
234
|
+
npm run size # gzip budgets
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
The demo reads `?name=`, `?mode=local|server` and `?url=` from its URL, which makes testing several players in several tabs quick.
|
|
238
|
+
|
|
239
|
+
Pull requests need one release label (`release:major`, `release:minor`, `release:patch` or `release:skip`). Merging one into `master` publishes the matching npm release. See [`.github/CI.md`](.github/CI.md).
|
|
240
|
+
|
|
241
|
+
---
|
|
242
|
+
|
|
243
|
+
## License
|
|
244
|
+
|
|
245
|
+
[Apache-2.0](LICENSE)
|
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.
|
|
5
|
+
"version": "0.5.1",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"files": [
|
|
8
8
|
"dist"
|