@pinkaphant/create-kolmo 0.1.4 → 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.
Files changed (40) hide show
  1. package/README.md +15 -5
  2. package/bin/create-kolmo.mjs +50 -27
  3. package/package.json +2 -1
  4. package/templates/longline/README.md +14 -0
  5. package/templates/longline/index.html +15 -0
  6. package/templates/longline/package.json +38 -0
  7. package/templates/longline/src/App.tsx +48 -0
  8. package/templates/longline/src/components/Boat.tsx +97 -0
  9. package/templates/longline/src/components/CameraRig.tsx +61 -0
  10. package/templates/longline/src/components/Crates.tsx +48 -0
  11. package/templates/longline/src/components/Hud.tsx +197 -0
  12. package/templates/longline/src/components/Longline.tsx +107 -0
  13. package/templates/longline/src/components/Ocean.tsx +27 -0
  14. package/templates/longline/src/components/Radar.tsx +88 -0
  15. package/templates/longline/src/components/Referee.tsx +39 -0
  16. package/templates/longline/src/components/Scene.tsx +56 -0
  17. package/templates/longline/src/components/Sky.tsx +23 -0
  18. package/templates/longline/src/components/ocean-material.ts +169 -0
  19. package/templates/longline/src/components/round-context.ts +26 -0
  20. package/templates/longline/src/game/boat-control.ts +131 -0
  21. package/templates/longline/src/game/constants.ts +98 -0
  22. package/templates/longline/src/game/input.test.ts +83 -0
  23. package/templates/longline/src/game/input.ts +73 -0
  24. package/templates/longline/src/game/logic.test.ts +110 -0
  25. package/templates/longline/src/game/logic.ts +109 -0
  26. package/templates/longline/src/game/physics.test.ts +211 -0
  27. package/templates/longline/src/game/radar.ts +22 -0
  28. package/templates/longline/src/game/registry.ts +53 -0
  29. package/templates/longline/src/game/sea.ts +26 -0
  30. package/templates/longline/src/game/session.ts +6 -0
  31. package/templates/longline/src/game/simulation.test.ts +393 -0
  32. package/templates/longline/src/game/simulation.ts +137 -0
  33. package/templates/longline/src/game/store.test.ts +222 -0
  34. package/templates/longline/src/game/store.ts +195 -0
  35. package/templates/longline/src/game/test-world.ts +128 -0
  36. package/templates/longline/src/main.tsx +8 -0
  37. package/templates/longline/src/sfx.ts +50 -0
  38. package/templates/longline/src/styles.css +374 -0
  39. package/templates/longline/tsconfig.json +18 -0
  40. package/templates/longline/vite.config.ts +28 -0
@@ -0,0 +1,222 @@
1
+ import assert from "node:assert/strict";
2
+ import test from "node:test";
3
+ import { CRATE, FIXED_DT, ROUND } from "./constants.js";
4
+ import { Game, browserStore, type BestStore } from "./store.js";
5
+
6
+ function memoryStore(initial: Record<string, string> = {}): BestStore & { data: Record<string, string> } {
7
+ const data = { ...initial };
8
+ return {
9
+ data,
10
+ getItem: (k) => data[k] ?? null,
11
+ setItem: (k, v) => {
12
+ data[k] = v;
13
+ },
14
+ };
15
+ }
16
+
17
+ /** `seconds` of round, one physics step at a time. */
18
+ function run(game: Game, seconds: number): void {
19
+ const n = Math.round(seconds / FIXED_DT);
20
+ for (let i = 0; i < n; i++) game.tick(FIXED_DT);
21
+ }
22
+
23
+ test("a new game is on the title screen with a full clock and no score", () => {
24
+ const g = new Game();
25
+ const s = g.getSnapshot();
26
+ assert.equal(s.phase, "menu");
27
+ assert.equal(s.secondsLeft, ROUND.seconds);
28
+ assert.equal(s.score, 0);
29
+ assert.equal(s.best, 0);
30
+ });
31
+
32
+ test("the clock counts physics steps, shows whole seconds and notifies only when something changed", () => {
33
+ const g = new Game();
34
+ g.start("calm");
35
+ let notified = 0;
36
+ g.subscribe(() => notified++);
37
+ run(g, 0.5);
38
+ assert.equal(notified, 0, "half a second inside the first second changes nothing on screen");
39
+ assert.equal(g.getSnapshot().secondsLeft, ROUND.seconds);
40
+ run(g, 0.6);
41
+ assert.equal(g.getSnapshot().secondsLeft, ROUND.seconds - 1);
42
+ assert.equal(notified, 1);
43
+ });
44
+
45
+ test("the round ends when the clock runs out, once, and stops counting", () => {
46
+ const g = new Game();
47
+ g.start("choppy");
48
+ run(g, ROUND.seconds + 1);
49
+ const s = g.getSnapshot();
50
+ assert.equal(s.phase, "over");
51
+ assert.equal(s.secondsLeft, 0);
52
+ let notified = 0;
53
+ g.subscribe(() => notified++);
54
+ run(g, 5);
55
+ assert.equal(notified, 0);
56
+ });
57
+
58
+ test("hauls score the crate's points times the combo, and the combo lapses after the window", () => {
59
+ const g = new Game();
60
+ g.start("calm");
61
+ g.haul(1);
62
+ assert.equal(g.getSnapshot().score, CRATE.points);
63
+ assert.equal(g.getSnapshot().combo, 1);
64
+ run(g, 1);
65
+ g.haul(1);
66
+ assert.equal(g.getSnapshot().score, CRATE.points + 2 * CRATE.points);
67
+ assert.equal(g.getSnapshot().combo, 2);
68
+ run(g, 1);
69
+ g.haul(1);
70
+ assert.equal(g.getSnapshot().score, CRATE.points * (1 + 2 + 3));
71
+ assert.equal(g.getSnapshot().lastHaul?.points, 3 * CRATE.points);
72
+ // a gap longer than the window: the badge goes away and the next haul is worth the base again
73
+ run(g, ROUND.comboWindow + 0.5);
74
+ assert.equal(g.getSnapshot().combo, 0);
75
+ g.haul(1);
76
+ assert.equal(g.getSnapshot().combo, 1);
77
+ assert.equal(g.getSnapshot().lastHaul?.points, CRATE.points);
78
+ assert.equal(g.getSnapshot().hauled, 4);
79
+ });
80
+
81
+ test("several crates in one step chain the combo among themselves", () => {
82
+ const g = new Game();
83
+ g.start("calm");
84
+ g.haul(3);
85
+ assert.equal(g.getSnapshot().score, CRATE.points * (1 + 2 + 3));
86
+ assert.equal(g.getSnapshot().hauled, 3);
87
+ assert.equal(g.getSnapshot().combo, 3);
88
+ });
89
+
90
+ test("the combo stops at the cap", () => {
91
+ const g = new Game();
92
+ g.start("calm");
93
+ g.haul(ROUND.maxCombo + 3);
94
+ assert.equal(g.getSnapshot().combo, ROUND.maxCombo);
95
+ });
96
+
97
+ test("each haul has its own id, so the HUD can tell a new one from the last", () => {
98
+ const g = new Game();
99
+ g.start("calm");
100
+ g.haul(1);
101
+ const a = g.getSnapshot().lastHaul!.id;
102
+ g.haul(1);
103
+ assert.equal(g.getSnapshot().lastHaul!.id, a + 1);
104
+ });
105
+
106
+ test("hauls, losses and the clock are ignored outside a round", () => {
107
+ const g = new Game();
108
+ g.haul(2);
109
+ g.lose(1);
110
+ g.tick(1);
111
+ const s = g.getSnapshot();
112
+ assert.deepEqual([s.score, s.hauled, s.lost, s.secondsLeft], [0, 0, 0, ROUND.seconds]);
113
+ g.start("calm");
114
+ run(g, ROUND.seconds + 1);
115
+ const over = g.getSnapshot();
116
+ g.haul(5);
117
+ g.lose(5);
118
+ assert.equal(g.getSnapshot(), over, "the snapshot did not change: no notification either");
119
+ });
120
+
121
+ test("start while a round is on does nothing, so a double click does not restart it", () => {
122
+ const g = new Game();
123
+ g.start("rough");
124
+ g.haul(1);
125
+ const id = g.getSnapshot().runId;
126
+ g.start("calm");
127
+ assert.equal(g.getSnapshot().runId, id);
128
+ assert.equal(g.getSnapshot().sea, "rough");
129
+ assert.equal(g.getSnapshot().score, CRATE.points);
130
+ });
131
+
132
+ test("a new round gets a new run id, a fresh clock and a clean score", () => {
133
+ const g = new Game();
134
+ g.start("calm");
135
+ g.haul(2);
136
+ run(g, ROUND.seconds + 1);
137
+ const before = g.getSnapshot().runId;
138
+ g.start("calm");
139
+ const s = g.getSnapshot();
140
+ assert.equal(s.runId, before + 1);
141
+ assert.deepEqual([s.phase, s.score, s.hauled, s.lost, s.secondsLeft, s.combo, s.newBest], ["playing", 0, 0, 0, ROUND.seconds, 0, false]);
142
+ });
143
+
144
+ test("the best score is kept per sea state, and a round that beats it says so", () => {
145
+ const store = memoryStore();
146
+ const g = new Game(store);
147
+ g.start("calm");
148
+ g.haul(2); // 100 + 200
149
+ run(g, ROUND.seconds + 1);
150
+ let s = g.getSnapshot();
151
+ assert.equal(s.score, 300);
152
+ assert.equal(s.newBest, true);
153
+ assert.equal(s.best, 300);
154
+ assert.equal(store.data["kolmo-longline:best:calm"], "300");
155
+
156
+ // a worse round keeps the best
157
+ g.start("calm");
158
+ g.haul(1);
159
+ run(g, ROUND.seconds + 1);
160
+ s = g.getSnapshot();
161
+ assert.equal(s.newBest, false);
162
+ assert.equal(s.best, 300);
163
+
164
+ // another sea has its own best
165
+ g.start("rough");
166
+ assert.equal(g.getSnapshot().best, 0);
167
+ run(g, ROUND.seconds + 1);
168
+ assert.equal(g.getSnapshot().newBest, false, "a score of 0 is no record");
169
+ });
170
+
171
+ test("a best score saved by an earlier visit is read back; junk in storage is ignored", () => {
172
+ const g = new Game(memoryStore({ "kolmo-longline:best:rough": "1250", "kolmo-longline:best:choppy": "banana" }));
173
+ g.selectSea("rough");
174
+ assert.equal(g.getSnapshot().best, 1250);
175
+ g.selectSea("choppy");
176
+ assert.equal(g.getSnapshot().best, 0);
177
+ });
178
+
179
+ test("a storage that throws does not take the game down", () => {
180
+ const broken: BestStore = {
181
+ getItem() {
182
+ throw new Error("blocked");
183
+ },
184
+ setItem() {
185
+ throw new Error("full");
186
+ },
187
+ };
188
+ const g = new Game(broken);
189
+ g.start("calm");
190
+ g.haul(1);
191
+ run(g, ROUND.seconds + 1);
192
+ assert.equal(g.getSnapshot().phase, "over");
193
+ assert.equal(g.getSnapshot().score, CRATE.points);
194
+ });
195
+
196
+ test("selectSea changes the title screen's sea but not a round that is on", () => {
197
+ const g = new Game();
198
+ g.selectSea("rough");
199
+ assert.equal(g.getSnapshot().sea, "rough");
200
+ g.start("rough");
201
+ g.selectSea("calm");
202
+ assert.equal(g.getSnapshot().sea, "rough");
203
+ run(g, ROUND.seconds + 1);
204
+ g.toMenu();
205
+ assert.equal(g.getSnapshot().phase, "menu");
206
+ assert.equal(g.getSnapshot().sea, "rough", "the menu offers the sea that was last played");
207
+ });
208
+
209
+ test("subscribers hear about changes until they unsubscribe", () => {
210
+ const g = new Game();
211
+ let n = 0;
212
+ const off = g.subscribe(() => n++);
213
+ g.start("calm");
214
+ assert.equal(n, 1);
215
+ off();
216
+ g.haul(1);
217
+ assert.equal(n, 1);
218
+ });
219
+
220
+ test("browserStore is null where there is no localStorage, as under Node", () => {
221
+ assert.equal(browserStore(), null);
222
+ });
@@ -0,0 +1,195 @@
1
+ import { CRATE, ROUND, type SeaStateId } from "./constants.js";
2
+ import { nextCombo } from "./logic.js";
3
+
4
+ export type Phase = "menu" | "playing" | "over";
5
+
6
+ export interface Haul {
7
+ /** Counts up with every haul, so the HUD can tell a new one from the last. */
8
+ id: number;
9
+ points: number;
10
+ combo: number;
11
+ }
12
+
13
+ /** What the HUD draws. A new object whenever any of it changes, which is what `useSyncExternalStore` needs. */
14
+ export interface Snapshot {
15
+ phase: Phase;
16
+ sea: SeaStateId;
17
+ /** Counts up with every round, so the scene can start afresh. */
18
+ runId: number;
19
+ secondsLeft: number;
20
+ score: number;
21
+ combo: number;
22
+ hauled: number;
23
+ lost: number;
24
+ /** Best score at this sea state, before this round's if the round is not over. */
25
+ best: number;
26
+ newBest: boolean;
27
+ lastHaul: Haul | null;
28
+ }
29
+
30
+ /** The part of `Storage` the game uses. */
31
+ export interface BestStore {
32
+ getItem(key: string): string | null;
33
+ setItem(key: string, value: string): void;
34
+ }
35
+
36
+ /** localStorage when the browser has one and lets us at it, otherwise nothing: a best score is a convenience. */
37
+ export function browserStore(): BestStore | null {
38
+ try {
39
+ const s = globalThis.localStorage;
40
+ return s === undefined ? null : s;
41
+ } catch {
42
+ return null;
43
+ }
44
+ }
45
+
46
+ const keyFor = (sea: SeaStateId): string => `kolmo-longline:best:${sea}`;
47
+
48
+ /**
49
+ * The round: a score, a clock and a combo. The clock counts physics steps (`tick` is called once per step), not wall
50
+ * time, so a paused world pauses the round and a run replays the same way.
51
+ */
52
+ export class Game {
53
+ private snap: Snapshot;
54
+ private timeLeft: number = ROUND.seconds;
55
+ private sinceHaul = Infinity;
56
+ private readonly listeners = new Set<() => void>();
57
+
58
+ constructor(private readonly store: BestStore | null = null) {
59
+ this.snap = {
60
+ phase: "menu",
61
+ sea: "calm",
62
+ runId: 0,
63
+ secondsLeft: ROUND.seconds,
64
+ score: 0,
65
+ combo: 0,
66
+ hauled: 0,
67
+ lost: 0,
68
+ best: this.readBest("calm"),
69
+ newBest: false,
70
+ lastHaul: null,
71
+ };
72
+ }
73
+
74
+ readonly subscribe = (listener: () => void): (() => void) => {
75
+ this.listeners.add(listener);
76
+ return () => {
77
+ this.listeners.delete(listener);
78
+ };
79
+ };
80
+
81
+ readonly getSnapshot = (): Snapshot => this.snap;
82
+
83
+ get phase(): Phase {
84
+ return this.snap.phase;
85
+ }
86
+
87
+ /** Starts a round at the given sea state. A round already on is not restarted, so a double click or a held key does no harm. */
88
+ start(sea: SeaStateId): void {
89
+ if (this.snap.phase === "playing") return;
90
+ this.timeLeft = ROUND.seconds;
91
+ this.sinceHaul = Infinity;
92
+ this.set({
93
+ phase: "playing",
94
+ sea,
95
+ runId: this.snap.runId + 1,
96
+ secondsLeft: ROUND.seconds,
97
+ score: 0,
98
+ combo: 0,
99
+ hauled: 0,
100
+ lost: 0,
101
+ best: this.readBest(sea),
102
+ newBest: false,
103
+ lastHaul: null,
104
+ });
105
+ }
106
+
107
+ /** Back to the title screen. The sea stays the one that was last played. */
108
+ toMenu(): void {
109
+ this.set({ phase: "menu", runId: this.snap.runId + 1, best: this.readBest(this.snap.sea) });
110
+ }
111
+
112
+ /** Selects a sea state on the title screen. */
113
+ selectSea(sea: SeaStateId): void {
114
+ if (this.snap.phase === "playing") return;
115
+ this.set({ sea, best: this.readBest(sea), runId: this.snap.runId + 1 });
116
+ }
117
+
118
+ /** One physics step of the round. */
119
+ tick(dt: number): void {
120
+ if (this.snap.phase !== "playing") return;
121
+ this.timeLeft -= dt;
122
+ this.sinceHaul += dt;
123
+ if (this.timeLeft <= 0) {
124
+ this.finish();
125
+ return;
126
+ }
127
+ const secondsLeft = Math.ceil(this.timeLeft);
128
+ const combo = this.sinceHaul > ROUND.comboWindow ? 0 : this.snap.combo;
129
+ if (secondsLeft !== this.snap.secondsLeft || combo !== this.snap.combo) this.set({ secondsLeft, combo });
130
+ }
131
+
132
+ /** `count` crates were hauled in this step. Each raises the combo and is worth `CRATE.points` times it. */
133
+ haul(count: number): void {
134
+ if (this.snap.phase !== "playing" || count <= 0) return;
135
+ let combo = this.snap.combo;
136
+ let score = this.snap.score;
137
+ let points = 0;
138
+ let sinceHaul = this.sinceHaul;
139
+ for (let i = 0; i < count; i++) {
140
+ combo = nextCombo(combo, sinceHaul);
141
+ sinceHaul = 0;
142
+ points = CRATE.points * combo;
143
+ score += points;
144
+ }
145
+ this.sinceHaul = 0;
146
+ this.set({
147
+ score,
148
+ combo,
149
+ hauled: this.snap.hauled + count,
150
+ lastHaul: { id: (this.snap.lastHaul?.id ?? 0) + 1, points, combo },
151
+ });
152
+ }
153
+
154
+ /** `count` crates drifted out of the arena. */
155
+ lose(count: number): void {
156
+ if (this.snap.phase !== "playing" || count <= 0) return;
157
+ this.set({ lost: this.snap.lost + count });
158
+ }
159
+
160
+ private finish(): void {
161
+ this.timeLeft = 0;
162
+ const newBest = this.snap.score > this.snap.best;
163
+ if (newBest) this.writeBest(this.snap.sea, this.snap.score);
164
+ this.set({
165
+ phase: "over",
166
+ secondsLeft: 0,
167
+ combo: 0,
168
+ best: newBest ? this.snap.score : this.snap.best,
169
+ newBest,
170
+ });
171
+ }
172
+
173
+ private readBest(sea: SeaStateId): number {
174
+ try {
175
+ const raw = this.store?.getItem(keyFor(sea));
176
+ const n = raw === null || raw === undefined ? 0 : Number(raw);
177
+ return Number.isFinite(n) && n > 0 ? Math.floor(n) : 0;
178
+ } catch {
179
+ return 0;
180
+ }
181
+ }
182
+
183
+ private writeBest(sea: SeaStateId, score: number): void {
184
+ try {
185
+ this.store?.setItem(keyFor(sea), String(score));
186
+ } catch {
187
+ // storage full or blocked: the score still counts for this visit
188
+ }
189
+ }
190
+
191
+ private set(patch: Partial<Snapshot>): void {
192
+ this.snap = { ...this.snap, ...patch };
193
+ for (const listener of [...this.listeners]) listener();
194
+ }
195
+ }
@@ -0,0 +1,128 @@
1
+ import RAPIER from "@dimforge/rapier3d-compat";
2
+ import { BuoyancyController, createFlatSurface, type WaterSurface } from "@pinkaphant/kolmo/water";
3
+ import { driveBoat, wetGrip, type DriveInput } from "./boat-control.js";
4
+ import { BOAT, CRATE, FIXED_DT, GRAVITY, LINE } from "./constants.js";
5
+ import type { Sea } from "./sea.js";
6
+ import { Simulation, type Fleet } from "./simulation.js";
7
+ import type { Game } from "./store.js";
8
+
9
+ // A round's world made straight on Rapier, with the numbers of constants.ts and the same hooks' controllers the scene
10
+ // uses, so the tests can run minutes of play in a blink with no React and no renderer. The scene test checks that the
11
+ // real components make the same world.
12
+
13
+ let ready: Promise<void> | null = null;
14
+
15
+ export function initRapier(): Promise<void> {
16
+ return (ready ??= RAPIER.init());
17
+ }
18
+
19
+ const NO_CONTACT = 0;
20
+
21
+ export interface TestWorld {
22
+ world: RAPIER.World;
23
+ boat: RAPIER.RigidBody;
24
+ floats: RAPIER.RigidBody[];
25
+ crates: RAPIER.RigidBody[];
26
+ fleet: Fleet;
27
+ /** Water time at the start of the next step. */
28
+ readonly time: number;
29
+ /** One physics step: the helm if a simulation was given, the buoyancy of every body, the world, then the rules. */
30
+ step(input?: DriveInput): void;
31
+ /** `seconds` of steps with the same input. */
32
+ run(seconds: number, input?: DriveInput): void;
33
+ boatController: BuoyancyController;
34
+ }
35
+
36
+ export interface TestWorldOptions {
37
+ /** Still water when absent. */
38
+ sea?: Sea;
39
+ simulation?: Simulation;
40
+ game?: Game;
41
+ withLine?: boolean;
42
+ withCrates?: boolean;
43
+ }
44
+
45
+ export function createTestWorld(options: TestWorldOptions = {}): TestWorld {
46
+ const surface: WaterSurface = options.sea?.surface ?? createFlatSurface();
47
+ const world = new RAPIER.World({ x: 0, y: -GRAVITY, z: 0 });
48
+ world.timestep = FIXED_DT;
49
+ let time = 0;
50
+ const clock = () => time;
51
+
52
+ const boat = world.createRigidBody(RAPIER.RigidBodyDesc.dynamic().setCanSleep(false));
53
+ world.createCollider(RAPIER.ColliderDesc.cuboid(...BOAT.halfExtents).setDensity(BOAT.density).setCollisionGroups(NO_CONTACT), boat);
54
+ const boatController = new BuoyancyController({ surface, time: clock, timeStep: FIXED_DT, cells: { slices: BOAT.slices }, drag: BOAT.drag });
55
+ const controllers: Array<[BuoyancyController, RAPIER.RigidBody]> = [[boatController, boat]];
56
+
57
+ const floats: RAPIER.RigidBody[] = [];
58
+ if (options.withLine ?? true) {
59
+ let prev = boat;
60
+ let anchor: { x: number; y: number; z: number } = { x: BOAT.tow[0], y: BOAT.tow[1], z: BOAT.tow[2] };
61
+ for (let i = 0; i < LINE.floats; i++) {
62
+ const body = world.createRigidBody(RAPIER.RigidBodyDesc.dynamic().setCanSleep(false).setTranslation(0, 0, BOAT.tow[2] + (i + 1) * LINE.spacing));
63
+ world.createCollider(RAPIER.ColliderDesc.ball(LINE.floatRadius).setDensity(LINE.floatDensity).setCollisionGroups(NO_CONTACT), body);
64
+ world.createImpulseJoint(RAPIER.JointData.rope(LINE.spacing, anchor, { x: 0, y: 0, z: 0 }), prev, body, true);
65
+ controllers.push([new BuoyancyController({ surface, time: clock, timeStep: FIXED_DT }), body]);
66
+ floats.push(body);
67
+ prev = body;
68
+ anchor = { x: 0, y: 0, z: 0 };
69
+ }
70
+ }
71
+
72
+ const crates: RAPIER.RigidBody[] = [];
73
+ if (options.withCrates ?? false) {
74
+ const [hx, hy, hz] = CRATE.halfExtents;
75
+ for (let i = 0; i < CRATE.count; i++) {
76
+ const start = options.simulation?.initialCrates[i] ?? { x: 20 + 8 * i, z: 30 };
77
+ const body = world.createRigidBody(RAPIER.RigidBodyDesc.dynamic().setCanSleep(false).setTranslation(start.x, 0, start.z));
78
+ world.createCollider(RAPIER.ColliderDesc.cuboid(hx, hy, hz).setDensity(CRATE.density).setCollisionGroups(NO_CONTACT), body);
79
+ controllers.push([new BuoyancyController({ surface, time: clock, timeStep: FIXED_DT }), body]);
80
+ crates.push(body);
81
+ }
82
+ }
83
+
84
+ const fleet: Fleet = { boat, floats, crates };
85
+ const idle: DriveInput = { throttle: 0, steer: 0 };
86
+
87
+ const step = (input: DriveInput = idle): void => {
88
+ if (options.simulation !== undefined) {
89
+ options.simulation.beforeStep(fleet, input, boatController.stats.submergedFraction, FIXED_DT);
90
+ } else {
91
+ driveBoat(boat, input, wetGrip(boatController.stats.submergedFraction), FIXED_DT);
92
+ }
93
+ for (const [controller, body] of controllers) controller.beforeStep(world, body);
94
+ world.step();
95
+ for (const [controller, body] of controllers) controller.afterStep(world, body);
96
+ time += FIXED_DT;
97
+ options.simulation?.afterStep(fleet, time, FIXED_DT);
98
+ };
99
+
100
+ return {
101
+ world,
102
+ boat,
103
+ floats,
104
+ crates,
105
+ fleet,
106
+ get time() {
107
+ return time;
108
+ },
109
+ step,
110
+ run(seconds, input) {
111
+ const n = Math.round(seconds / FIXED_DT);
112
+ for (let i = 0; i < n; i++) step(input);
113
+ },
114
+ boatController,
115
+ };
116
+ }
117
+
118
+ /** Speed of a body on the water plane. */
119
+ export function planarSpeed(body: RAPIER.RigidBody): number {
120
+ const v = body.linvel();
121
+ return Math.hypot(v.x, v.z);
122
+ }
123
+
124
+ /** The tilt of a body's up axis from the vertical, radians. */
125
+ export function tilt(body: RAPIER.RigidBody): number {
126
+ const q = body.rotation();
127
+ return Math.acos(Math.max(-1, Math.min(1, 1 - 2 * (q.x * q.x + q.z * q.z))));
128
+ }
@@ -0,0 +1,8 @@
1
+ import { createRoot } from "react-dom/client";
2
+ import { App } from "./App.js";
3
+ import { game, helm } from "./game/session.js";
4
+ import "./styles.css";
5
+
6
+ const root = document.getElementById("root");
7
+ if (root === null) throw new Error("index.html has no #root");
8
+ createRoot(root).render(<App game={game} helm={helm} />);
@@ -0,0 +1,50 @@
1
+ // A few synthesised blips, no audio files. Browsers only let a page make sound after a gesture, so `unlockAudio` is
2
+ // called from the Start button, and every function here does nothing until then (and nothing at all without WebAudio).
3
+
4
+ let ctx: AudioContext | null = null;
5
+ let muted = false;
6
+
7
+ export function unlockAudio(): void {
8
+ try {
9
+ ctx ??= new AudioContext();
10
+ if (ctx.state === "suspended") void ctx.resume();
11
+ } catch {
12
+ ctx = null;
13
+ }
14
+ }
15
+
16
+ export function setMuted(value: boolean): void {
17
+ muted = value;
18
+ }
19
+
20
+ export function isMuted(): boolean {
21
+ return muted;
22
+ }
23
+
24
+ function tone(frequency: number, delay: number, length: number, type: OscillatorType, peak: number): void {
25
+ if (muted || ctx === null) return;
26
+ const t0 = ctx.currentTime + delay;
27
+ const osc = ctx.createOscillator();
28
+ const gain = ctx.createGain();
29
+ osc.type = type;
30
+ osc.frequency.setValueAtTime(frequency, t0);
31
+ gain.gain.setValueAtTime(0.0001, t0);
32
+ gain.gain.exponentialRampToValueAtTime(peak, t0 + 0.012);
33
+ gain.gain.exponentialRampToValueAtTime(0.0001, t0 + length);
34
+ osc.connect(gain).connect(ctx.destination);
35
+ osc.start(t0);
36
+ osc.stop(t0 + length + 0.02);
37
+ }
38
+
39
+ /** A rising two-note chime; each step of the combo is a whole tone higher. */
40
+ export function playHaul(combo: number): void {
41
+ const up = 2 ** (Math.max(combo - 1, 0) / 6);
42
+ tone(523.25 * up, 0, 0.14, "triangle", 0.16);
43
+ tone(783.99 * up, 0.08, 0.22, "triangle", 0.14);
44
+ }
45
+
46
+ /** The round is over. */
47
+ export function playEnd(): void {
48
+ tone(392, 0, 0.22, "sine", 0.16);
49
+ tone(293.66, 0.2, 0.4, "sine", 0.16);
50
+ }