@pinkaphant/create-kolmo 0.4.1 → 0.5.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 (78) hide show
  1. package/README.md +56 -17
  2. package/bin/create-kolmo.mjs +20 -12
  3. package/package.json +1 -1
  4. package/templates/demolition/README.md +17 -0
  5. package/templates/demolition/index.html +29 -0
  6. package/templates/demolition/package.json +39 -0
  7. package/templates/demolition/src/App.tsx +599 -0
  8. package/templates/{lonedrive → demolition}/tsconfig.json +4 -2
  9. package/templates/{lonedrive → demolition}/vite.config.ts +3 -1
  10. package/templates/dust/README.md +9 -1
  11. package/templates/dust/index.html +15 -1
  12. package/templates/dust/package.json +8 -6
  13. package/templates/dust/src/App.tsx +106 -122
  14. package/templates/dust/tsconfig.json +4 -2
  15. package/templates/evolution/README.md +15 -0
  16. package/templates/evolution/index.html +29 -0
  17. package/templates/{lonedrive → evolution}/package.json +8 -7
  18. package/templates/evolution/src/App.tsx +619 -0
  19. package/templates/evolution/src/main.tsx +10 -0
  20. package/templates/evolution/src/styles.css +237 -0
  21. package/templates/evolution/tsconfig.json +16 -0
  22. package/templates/evolution/vite.config.ts +10 -0
  23. package/templates/longline/README.md +16 -9
  24. package/templates/longline/index.html +26 -12
  25. package/templates/longline/package.json +11 -9
  26. package/templates/longline/src/App.tsx +191 -42
  27. package/templates/longline/src/highway.ts +222 -0
  28. package/templates/longline/src/main.tsx +7 -6
  29. package/templates/longline/tsconfig.json +14 -16
  30. package/templates/longline/vite.config.ts +5 -23
  31. package/templates/road/README.md +8 -4
  32. package/templates/road/index.html +15 -1
  33. package/templates/road/package.json +5 -5
  34. package/templates/road/src/App.tsx +21 -6
  35. package/templates/road/tsconfig.json +1 -0
  36. package/templates/tugline/README.md +16 -0
  37. package/templates/tugline/index.html +31 -0
  38. package/templates/tugline/package.json +41 -0
  39. package/templates/tugline/src/App.tsx +48 -0
  40. package/templates/{longline → tugline}/src/components/Boat.tsx +1 -2
  41. package/templates/{longline → tugline}/src/components/Crates.tsx +2 -2
  42. package/templates/tugline/src/components/Hud.tsx +281 -0
  43. package/templates/{longline → tugline}/src/components/Longline.tsx +2 -2
  44. package/templates/{longline → tugline}/src/components/Ocean.tsx +14 -3
  45. package/templates/{longline → tugline}/src/components/Radar.tsx +15 -14
  46. package/templates/{longline → tugline}/src/components/Referee.tsx +6 -4
  47. package/templates/{longline → tugline}/src/components/Scene.tsx +10 -9
  48. package/templates/{longline → tugline}/src/components/Sky.tsx +14 -8
  49. package/templates/{longline → tugline}/src/game/store.test.ts +2 -2
  50. package/templates/{longline → tugline}/src/game/store.ts +38 -19
  51. package/templates/tugline/src/main.tsx +13 -0
  52. package/templates/tugline/src/styles.css +495 -0
  53. package/templates/tugline/tsconfig.json +18 -0
  54. package/templates/tugline/vite.config.ts +16 -0
  55. package/templates/lonedrive/README.md +0 -11
  56. package/templates/lonedrive/index.html +0 -15
  57. package/templates/lonedrive/src/App.tsx +0 -149
  58. package/templates/longline/src/components/Hud.tsx +0 -197
  59. package/templates/longline/src/styles.css +0 -367
  60. /package/templates/{lonedrive → demolition}/src/main.tsx +0 -0
  61. /package/templates/{longline → tugline}/src/components/CameraRig.tsx +0 -0
  62. /package/templates/{longline → tugline}/src/components/ocean-material.ts +0 -0
  63. /package/templates/{longline → tugline}/src/components/round-context.ts +0 -0
  64. /package/templates/{longline → tugline}/src/game/boat-control.ts +0 -0
  65. /package/templates/{longline → tugline}/src/game/constants.ts +0 -0
  66. /package/templates/{longline → tugline}/src/game/input.test.ts +0 -0
  67. /package/templates/{longline → tugline}/src/game/input.ts +0 -0
  68. /package/templates/{longline → tugline}/src/game/logic.test.ts +0 -0
  69. /package/templates/{longline → tugline}/src/game/logic.ts +0 -0
  70. /package/templates/{longline → tugline}/src/game/physics.test.ts +0 -0
  71. /package/templates/{longline → tugline}/src/game/radar.ts +0 -0
  72. /package/templates/{longline → tugline}/src/game/registry.ts +0 -0
  73. /package/templates/{longline → tugline}/src/game/sea.ts +0 -0
  74. /package/templates/{longline → tugline}/src/game/session.ts +0 -0
  75. /package/templates/{longline → tugline}/src/game/simulation.test.ts +0 -0
  76. /package/templates/{longline → tugline}/src/game/simulation.ts +0 -0
  77. /package/templates/{longline → tugline}/src/game/test-world.ts +0 -0
  78. /package/templates/{longline → tugline}/src/sfx.ts +0 -0
@@ -0,0 +1,281 @@
1
+ import { useCallback, useEffect, useRef, useState, useSyncExternalStore } from "react";
2
+ import { SEA_STATES, SEA_STATE_IDS, type SeaStateId } from "../game/constants.js";
3
+ import type { Helm } from "../game/input.js";
4
+ import type { Game, Snapshot } from "../game/store.js";
5
+ import { isMuted, playEnd, playHaul, setMuted, unlockAudio } from "../sfx.js";
6
+ import { Radar } from "./Radar.js";
7
+
8
+ export function formatClock(seconds: number): string {
9
+ const s = Math.max(0, Math.ceil(seconds));
10
+ return `${Math.floor(s / 60)}:${String(s % 60).padStart(2, "0")}`;
11
+ }
12
+
13
+ function useSnapshot(game: Game): Snapshot {
14
+ return useSyncExternalStore(game.subscribe, game.getSnapshot);
15
+ }
16
+
17
+ /** Sounds for the round's events: a chime on each haul, a tone when time runs out. */
18
+ function useSounds(snap: Snapshot): void {
19
+ const haul = useRef(snap.lastHaul?.id ?? 0);
20
+ const phase = useRef(snap.phase);
21
+ useEffect(() => {
22
+ const id = snap.lastHaul?.id ?? 0;
23
+ if (snap.lastHaul !== null && id !== haul.current) playHaul(snap.lastHaul.combo);
24
+ haul.current = id;
25
+ if (phase.current === "playing" && snap.phase === "over") playEnd();
26
+ phase.current = snap.phase;
27
+ }, [snap]);
28
+ }
29
+
30
+ function TouchControls({ helm }: { helm: Helm }) {
31
+ const axes = useRef({ throttle: 0, steer: 0 });
32
+ const set = (axis: "throttle" | "steer", value: number) => ({
33
+ onPointerDown: (e: React.PointerEvent) => {
34
+ e.currentTarget.setPointerCapture(e.pointerId);
35
+ axes.current[axis] = value;
36
+ helm.setTouch(axes.current.throttle, axes.current.steer);
37
+ },
38
+ onPointerUp: () => {
39
+ axes.current[axis] = 0;
40
+ helm.setTouch(axes.current.throttle, axes.current.steer);
41
+ },
42
+ onPointerCancel: () => {
43
+ axes.current[axis] = 0;
44
+ helm.setTouch(axes.current.throttle, axes.current.steer);
45
+ },
46
+ });
47
+ return (
48
+ <div className="touch">
49
+ <div className="touch-side">
50
+ <button type="button" aria-label="Steer to port" {...set("steer", -1)}>
51
+ PORT
52
+ </button>
53
+ <button type="button" aria-label="Steer to starboard" {...set("steer", 1)}>
54
+ STBD
55
+ </button>
56
+ </div>
57
+ <div className="touch-side">
58
+ <button type="button" aria-label="Astern" {...set("throttle", -1)}>
59
+ ASTERN
60
+ </button>
61
+ <button type="button" aria-label="Ahead" {...set("throttle", 1)}>
62
+ AHEAD
63
+ </button>
64
+ </div>
65
+ </div>
66
+ );
67
+ }
68
+
69
+ function SeaPicker({ game, snap }: { game: Game; snap: Snapshot }) {
70
+ return (
71
+ <div className="seas" role="radiogroup" aria-label="Sea state">
72
+ {SEA_STATE_IDS.map((id: SeaStateId, idx: number) => (
73
+ <button
74
+ key={id}
75
+ type="button"
76
+ role="radio"
77
+ aria-checked={snap.sea === id}
78
+ className={snap.sea === id ? "sea selected" : "sea"}
79
+ onClick={() => game.selectSea(id)}
80
+ >
81
+ <span className="sea-head">
82
+ <span className="sea-idx">0{idx + 1}</span>
83
+ <strong>{SEA_STATES[id].label}</strong>
84
+ </span>
85
+ <span className="sea-meta">DRIFT {SEA_STATES[id].current.toFixed(2)} m/s</span>
86
+ </button>
87
+ ))}
88
+ </div>
89
+ );
90
+ }
91
+
92
+ function Title({ game, snap }: { game: Game; snap: Snapshot }) {
93
+ const start = useCallback((): void => {
94
+ unlockAudio();
95
+ game.start(snap.sea);
96
+ }, [game, snap.sea]);
97
+
98
+ return (
99
+ <div className="panel title">
100
+ <div className="panel-header">
101
+ <span className="spec-tag">01 // GERSTNER TOW LINE</span>
102
+ <span className="spec-tag">{SEA_STATES[snap.sea].label.toUpperCase()}</span>
103
+ </div>
104
+ <h1>TUGLINE</h1>
105
+ <p className="tag">Sweep the 8-float tow line across drifting cargo crates before the 90 s clock expires.</p>
106
+ <SeaPicker game={game} snap={snap} />
107
+ <div className="best-row">
108
+ <span className="best-label">BEST ({SEA_STATES[snap.sea].label.toUpperCase()})</span>
109
+ <span className="best-val">{snap.best > 0 ? snap.best : "—"}</span>
110
+ </div>
111
+ <button type="button" className="go" onClick={start}>
112
+ LAUNCH ROUND [ENTER]
113
+ </button>
114
+ <p className="keys">
115
+ <kbd>WASD</kbd> / <kbd>ARROWS</kbd> helm · <kbd>1–3</kbd> sea · <kbd>SPACE</kbd> pause · <kbd>R</kbd> restart
116
+ </p>
117
+ </div>
118
+ );
119
+ }
120
+
121
+ function Result({ game, snap }: { game: Game; snap: Snapshot }) {
122
+ const again = useCallback((): void => {
123
+ unlockAudio();
124
+ game.restart(snap.sea);
125
+ }, [game, snap.sea]);
126
+
127
+ return (
128
+ <div className="panel result">
129
+ <div className="panel-header">
130
+ <span className="spec-tag">ROUND COMPLETE</span>
131
+ <span className="spec-tag">{SEA_STATES[snap.sea].label.toUpperCase()}</span>
132
+ </div>
133
+ <h2>FINAL TALLY</h2>
134
+ <div className="big">{snap.score}</div>
135
+ {snap.newBest ? <p className="newbest">NEW SEA RECORD</p> : <p className="best">BEST: {snap.best}</p>}
136
+ <p className="stats">
137
+ {snap.hauled} HAULED · {snap.lost} DRIFTED OUT
138
+ </p>
139
+ <div className="row">
140
+ <button type="button" className="go" onClick={again}>
141
+ SAIL AGAIN [ENTER]
142
+ </button>
143
+ <button type="button" className="secondary" onClick={() => game.toMenu()}>
144
+ SEA SELECT
145
+ </button>
146
+ </div>
147
+ </div>
148
+ );
149
+ }
150
+
151
+ export function Hud({ game, helm }: { game: Game; helm: Helm }) {
152
+ const snap = useSnapshot(game);
153
+ useSounds(snap);
154
+ const [muted, setMutedState] = useState(isMuted());
155
+ const [coarse] = useState(() => typeof window !== "undefined" && window.matchMedia?.("(pointer: coarse)").matches === true);
156
+
157
+ useEffect(() => {
158
+ const onKey = (e: KeyboardEvent): void => {
159
+ if (e.repeat) return;
160
+ const active = document.activeElement?.tagName;
161
+ const onButton = active === "BUTTON" || active === "INPUT";
162
+ if (snap.phase === "menu") {
163
+ if (e.code === "Digit1") game.selectSea("calm");
164
+ else if (e.code === "Digit2") game.selectSea("choppy");
165
+ else if (e.code === "Digit3") game.selectSea("rough");
166
+ else if ((e.code === "Enter" || e.code === "Space") && !onButton) {
167
+ e.preventDefault();
168
+ unlockAudio();
169
+ game.start(snap.sea);
170
+ }
171
+ } else if (snap.phase === "playing") {
172
+ if (e.code === "Space" && !onButton) {
173
+ e.preventDefault();
174
+ game.togglePause();
175
+ } else if (e.code === "KeyR") {
176
+ e.preventDefault();
177
+ helm.releaseAll();
178
+ unlockAudio();
179
+ game.restart(snap.sea);
180
+ } else if (e.code === "Digit1") {
181
+ helm.releaseAll();
182
+ game.restart("calm");
183
+ } else if (e.code === "Digit2") {
184
+ helm.releaseAll();
185
+ game.restart("choppy");
186
+ } else if (e.code === "Digit3") {
187
+ helm.releaseAll();
188
+ game.restart("rough");
189
+ }
190
+ } else if (snap.phase === "over") {
191
+ if ((e.code === "Enter" || e.code === "KeyR") && !onButton) {
192
+ e.preventDefault();
193
+ unlockAudio();
194
+ game.restart(snap.sea);
195
+ } else if (e.code === "Digit1") {
196
+ game.selectSea("calm");
197
+ game.toMenu();
198
+ } else if (e.code === "Digit2") {
199
+ game.selectSea("choppy");
200
+ game.toMenu();
201
+ } else if (e.code === "Digit3") {
202
+ game.selectSea("rough");
203
+ game.toMenu();
204
+ }
205
+ }
206
+ };
207
+ window.addEventListener("keydown", onKey);
208
+ return () => window.removeEventListener("keydown", onKey);
209
+ }, [game, helm, snap.phase, snap.sea]);
210
+
211
+ const playing = snap.phase === "playing";
212
+ return (
213
+ <div className="hud">
214
+ <div className="topbar">
215
+ <div className="topbar-group">
216
+ <span className="cell-label">TUGLINE</span>
217
+ <span className="divider" />
218
+ <span className="cell-meta">{SEA_STATES[snap.sea].label.toUpperCase()}</span>
219
+ {playing ? (
220
+ <>
221
+ <span className="divider" />
222
+ <span className="cell-label">SCORE</span>
223
+ <span className="cell-value">{String(snap.score).padStart(4, "0")}</span>
224
+ {snap.combo > 1 ? <span className="combo">×{snap.combo}</span> : null}
225
+ <span className="divider" />
226
+ <span className={snap.secondsLeft <= 10 ? "clock low" : "clock"}>{formatClock(snap.secondsLeft)}</span>
227
+ <span className="divider" />
228
+ <span className="cell-meta">
229
+ HAULED {snap.hauled} · LOST {snap.lost}
230
+ </span>
231
+ </>
232
+ ) : null}
233
+ </div>
234
+ <div className="topbar-actions">
235
+ {playing ? (
236
+ <>
237
+ <button type="button" className="dock-btn" onClick={() => game.togglePause()}>
238
+ {snap.paused ? "RESUME [SPACE]" : "PAUSE [SPACE]"}
239
+ </button>
240
+ <button
241
+ type="button"
242
+ className="dock-btn"
243
+ onClick={() => {
244
+ helm.releaseAll();
245
+ game.restart(snap.sea);
246
+ }}
247
+ >
248
+ RESTART [R]
249
+ </button>
250
+ </>
251
+ ) : null}
252
+ <button
253
+ type="button"
254
+ className="dock-btn"
255
+ aria-pressed={!muted}
256
+ onClick={() => {
257
+ setMuted(!muted);
258
+ setMutedState(!muted);
259
+ }}
260
+ >
261
+ {muted ? "SFX MUTED" : "SFX ON"}
262
+ </button>
263
+ </div>
264
+ </div>
265
+
266
+ {playing && snap.paused ? <div className="paused-banner">PAUSED · PRESS SPACE TO RESUME</div> : null}
267
+
268
+ {playing && snap.lastHaul !== null ? (
269
+ <div className="toast" key={snap.lastHaul.id}>
270
+ +{snap.lastHaul.points}
271
+ {snap.lastHaul.combo > 1 ? ` ×${snap.lastHaul.combo}` : ""}
272
+ </div>
273
+ ) : null}
274
+
275
+ {playing && coarse ? <TouchControls helm={helm} /> : null}
276
+ <Radar />
277
+ {snap.phase === "menu" ? <Title game={game} snap={snap} /> : null}
278
+ {snap.phase === "over" ? <Result game={game} snap={snap} /> : null}
279
+ </div>
280
+ );
281
+ }
@@ -3,7 +3,7 @@ import { useFrame } from "@react-three/fiber";
3
3
  import { BallCollider, RigidBody, useRopeJoint, type RapierRigidBody } from "@react-three/rapier";
4
4
  import { useLayoutEffect, useRef, type RefObject } from "react";
5
5
  import * as THREE from "three";
6
- import { BOAT, FIXED_DT, LINE } from "../game/constants.js";
6
+ import { BOAT, LINE } from "../game/constants.js";
7
7
  import { NO_CONTACT } from "./Boat.js";
8
8
  import { useRound } from "./round-context.js";
9
9
 
@@ -12,7 +12,7 @@ function Float({ index }: { index: number }) {
12
12
  const self = registry.floats[index]!;
13
13
  const prev = index === 0 ? registry.boat : registry.floats[index - 1]!;
14
14
  const object = useRef<THREE.Object3D>(null);
15
- useBuoyancy(self, { surface: sea.surface, clock, timeStep: FIXED_DT });
15
+ useBuoyancy(self, { surface: sea.surface, clock });
16
16
  // the first rope is made fast to the stern, the others to the float before
17
17
  useRopeJoint(prev as RefObject<RapierRigidBody>, self as RefObject<RapierRigidBody>, [
18
18
  index === 0 ? [BOAT.tow[0], BOAT.tow[1], BOAT.tow[2]] : [0, 0, 0],
@@ -1,5 +1,5 @@
1
1
  import { useFrame } from "@react-three/fiber";
2
- import { useEffect, useMemo } from "react";
2
+ import { useEffect, useMemo, useRef } from "react";
3
3
  import { OCEAN, createOceanGeometry, createOceanMaterial } from "./ocean-material.js";
4
4
  import { useRound } from "./round-context.js";
5
5
 
@@ -12,8 +12,19 @@ export function Ocean() {
12
12
  const { sea, clock, registry } = useRound();
13
13
  const material = useMemo(() => createOceanMaterial(sea.field, sea.state.foam), [sea]);
14
14
  const geometry = useMemo(() => createOceanGeometry(), []);
15
- useEffect(() => () => material.dispose(), [material]);
16
- useEffect(() => () => geometry.dispose(), [geometry]);
15
+ const mountsRef = useRef(0);
16
+ useEffect(() => {
17
+ mountsRef.current += 1;
18
+ return () => {
19
+ mountsRef.current -= 1;
20
+ queueMicrotask(() => {
21
+ if (mountsRef.current === 0) {
22
+ material.dispose();
23
+ geometry.dispose();
24
+ }
25
+ });
26
+ };
27
+ }, [material, geometry]);
17
28
  useFrame(() => {
18
29
  const boat = registry.boat.current;
19
30
  const cell = OCEAN.size / OCEAN.segments;
@@ -2,12 +2,12 @@ import { useEffect, useRef } from "react";
2
2
  import { ARENA, CRATE } from "../game/constants.js";
3
3
  import { radar } from "../game/radar.js";
4
4
 
5
- const SIZE = 156;
5
+ const SIZE = 148;
6
6
  const RANGE = ARENA.radius + 12;
7
7
 
8
8
  /**
9
- * A heading-up radar of the arena: the boat in the middle pointing up, the crates around it, the edge as a ring. It
10
- * reads `radar`, which the referee writes every physics step, in its own animation frame loop, so it costs React nothing.
9
+ * A heading-up radar of the arena: the boat in the middle pointing up, the crates around it, the edge as a ring.
10
+ * Reads `radar` in its own animation frame loop so it costs React zero re-renders.
11
11
  */
12
12
  export function Radar() {
13
13
  const canvas = useRef<HTMLCanvasElement>(null);
@@ -32,21 +32,23 @@ export function Radar() {
32
32
  ctx.save();
33
33
  ctx.beginPath();
34
34
  ctx.arc(half, half, half - 1, 0, Math.PI * 2);
35
- ctx.fillStyle = "rgba(6, 36, 56, 0.62)";
35
+ ctx.fillStyle = "rgba(18, 20, 26, 0.9)";
36
36
  ctx.fill();
37
37
  ctx.clip();
38
38
  if (radar.ready) {
39
39
  const sin = Math.sin(radar.heading);
40
40
  const cos = Math.cos(radar.heading);
41
- // world offset -> screen: right is (cos, sin), forward is (sin, -cos), forward is up
42
- const toScreen = (dx: number, dz: number): [number, number] => [half + (dx * cos + dz * sin) * scale, half - (dx * sin - dz * cos) * scale];
41
+ const toScreen = (dx: number, dz: number): [number, number] => [
42
+ half + (dx * cos + dz * sin) * scale,
43
+ half - (dx * sin - dz * cos) * scale,
44
+ ];
43
45
  const [ox, oy] = toScreen(-radar.boatX, -radar.boatZ);
44
46
  ctx.beginPath();
45
47
  ctx.arc(ox, oy, ARENA.radius * scale, 0, Math.PI * 2);
46
- ctx.strokeStyle = "rgba(255, 210, 61, 0.75)";
47
- ctx.lineWidth = 1.5;
48
+ ctx.strokeStyle = "rgba(245, 158, 11, 0.72)";
49
+ ctx.lineWidth = 1;
48
50
  ctx.stroke();
49
- ctx.fillStyle = "#ff3d6e";
51
+ ctx.fillStyle = "#f59e0b";
50
52
  for (let i = 0; i < CRATE.count; i++) {
51
53
  const [cx, cy] = toScreen(radar.crates[i * 2]! - radar.boatX, radar.crates[i * 2 + 1]! - radar.boatZ);
52
54
  ctx.fillRect(cx - 2.5, cy - 2.5, 5, 5);
@@ -58,19 +60,18 @@ export function Radar() {
58
60
  }
59
61
  }
60
62
  ctx.restore();
61
- // the boat: a small arrow pointing up
62
63
  ctx.beginPath();
63
64
  ctx.moveTo(half, half - 6);
64
65
  ctx.lineTo(half + 4.5, half + 5);
65
66
  ctx.lineTo(half, half + 2.5);
66
67
  ctx.lineTo(half - 4.5, half + 5);
67
68
  ctx.closePath();
68
- ctx.fillStyle = "#ffffff";
69
+ ctx.fillStyle = "#38bdf8";
69
70
  ctx.fill();
70
71
  ctx.beginPath();
71
72
  ctx.arc(half, half, half - 1, 0, Math.PI * 2);
72
- ctx.strokeStyle = "rgba(255, 255, 255, 0.55)";
73
- ctx.lineWidth = 1.5;
73
+ ctx.strokeStyle = "rgba(148, 163, 184, 0.28)";
74
+ ctx.lineWidth = 1;
74
75
  ctx.stroke();
75
76
  };
76
77
  frame = requestAnimationFrame(draw);
@@ -80,7 +81,7 @@ export function Radar() {
80
81
  return (
81
82
  <>
82
83
  <div className="warning" ref={warning} aria-live="polite">
83
- Out of bounds: turn back
84
+ BOUNDARY ALERT · TURN ASTERN
84
85
  </div>
85
86
  <canvas className="radar" ref={canvas} style={{ width: SIZE, height: SIZE }} aria-label="Radar of the crates around the boat" />
86
87
  </>
@@ -15,18 +15,20 @@ export function Referee() {
15
15
  const { registry, simulation, clock, helm } = useRound();
16
16
  const input = useRef<DriveInput>({ throttle: 0, steer: 0 });
17
17
 
18
- useBeforePhysicsStep(() => {
18
+ useBeforePhysicsStep((world) => {
19
+ const dt = world.timestep > 0 ? world.timestep : FIXED_DT;
19
20
  const fleet = registry.fleet();
20
21
  if (fleet === null) return;
21
22
  helm.read(input.current);
22
23
  const wet = registry.boatController?.stats.submergedFraction ?? 0;
23
- simulation.beforeStep(fleet, input.current, wet, FIXED_DT);
24
+ simulation.beforeStep(fleet, input.current, wet, dt);
24
25
  });
25
26
 
26
- useAfterPhysicsStep(() => {
27
+ useAfterPhysicsStep((world) => {
28
+ const dt = world.timestep > 0 ? world.timestep : FIXED_DT;
27
29
  const fleet = registry.fleet();
28
30
  if (fleet === null) return;
29
- simulation.afterStep(fleet, clock.time, FIXED_DT);
31
+ simulation.afterStep(fleet, clock.time, dt);
30
32
  const p = fleet.boat.translation();
31
33
  planeHeading(fleet.boat.rotation(), heading);
32
34
  radar.boatX = p.x;
@@ -1,7 +1,7 @@
1
+ import { PhysicsWorld } from "@khr3/core";
1
2
  import { usePhysicsClock } from "@pinkaphant/kolmo/water/react";
2
- import { Physics } from "@react-three/rapier";
3
3
  import { useMemo } from "react";
4
- import { FIXED_DT, GRAVITY, type SeaStateId } from "../game/constants.js";
4
+ import { GRAVITY, type SeaStateId } from "../game/constants.js";
5
5
  import type { Helm } from "../game/input.js";
6
6
  import { Registry } from "../game/registry.js";
7
7
  import { createSea } from "../game/sea.js";
@@ -20,11 +20,12 @@ export interface SceneProps {
20
20
  sea: SeaStateId;
21
21
  /** Counts up with every round; it is the seed of the crates, so a round replays the same way. */
22
22
  runId: number;
23
+ paused: boolean;
23
24
  game: Game;
24
25
  helm: Helm;
25
26
  }
26
27
 
27
- function RoundScene({ sea: seaId, runId, game, helm }: SceneProps) {
28
+ function RoundScene({ sea: seaId, runId, game, helm }: Omit<SceneProps, "paused">) {
28
29
  const clock = usePhysicsClock();
29
30
  const round = useMemo<Round>(() => {
30
31
  const sea = createSea(seaId);
@@ -44,13 +45,13 @@ function RoundScene({ sea: seaId, runId, game, helm }: SceneProps) {
44
45
  }
45
46
 
46
47
  /**
47
- * One round's world: the sea, the boat, the line, the crates and the rules, in a <Physics> with a fixed step. Mount it
48
- * with a new `key` for a new round.
48
+ * One round's world: the sea, the boat, the line, the crates and the rules inside `@khr3/core`'s `<PhysicsWorld>`.
49
+ * Mount it with a new `key` for a new round.
49
50
  */
50
- export function Scene(props: SceneProps) {
51
+ export function Scene({ paused, ...rest }: SceneProps) {
51
52
  return (
52
- <Physics gravity={[0, -GRAVITY, 0]} timeStep={FIXED_DT}>
53
- <RoundScene {...props} />
54
- </Physics>
53
+ <PhysicsWorld paused={paused} gravity={[0, -GRAVITY, 0]}>
54
+ <RoundScene {...rest} />
55
+ </PhysicsWorld>
55
56
  );
56
57
  }
@@ -1,4 +1,4 @@
1
- import { useEffect, useMemo } from "react";
1
+ import { useEffect, useMemo, useRef } from "react";
2
2
  import * as THREE from "three";
3
3
  import { PALETTE, SUN, createSkyMaterial } from "./ocean-material.js";
4
4
 
@@ -6,13 +6,19 @@ import { PALETTE, SUN, createSkyMaterial } from "./ocean-material.js";
6
6
  export function Sky() {
7
7
  const material = useMemo(() => createSkyMaterial(), []);
8
8
  const geometry = useMemo(() => new THREE.SphereGeometry(1, 24, 16), []);
9
- useEffect(
10
- () => () => {
11
- material.dispose();
12
- geometry.dispose();
13
- },
14
- [material, geometry],
15
- );
9
+ const mountsRef = useRef(0);
10
+ useEffect(() => {
11
+ mountsRef.current += 1;
12
+ return () => {
13
+ mountsRef.current -= 1;
14
+ queueMicrotask(() => {
15
+ if (mountsRef.current === 0) {
16
+ material.dispose();
17
+ geometry.dispose();
18
+ }
19
+ });
20
+ };
21
+ }, [material, geometry]);
16
22
  return (
17
23
  <>
18
24
  <mesh geometry={geometry} material={material} frustumCulled={false} renderOrder={-1} />
@@ -151,7 +151,7 @@ test("the best score is kept per sea state, and a round that beats it says so",
151
151
  assert.equal(s.score, 300);
152
152
  assert.equal(s.newBest, true);
153
153
  assert.equal(s.best, 300);
154
- assert.equal(store.data["kolmo-longline:best:calm"], "300");
154
+ assert.equal(store.data["kolmo-tugline:best:calm"], "300");
155
155
 
156
156
  // a worse round keeps the best
157
157
  g.start("calm");
@@ -169,7 +169,7 @@ test("the best score is kept per sea state, and a round that beats it says so",
169
169
  });
170
170
 
171
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" }));
172
+ const g = new Game(memoryStore({ "kolmo-tugline:best:rough": "1250", "kolmo-tugline:best:choppy": "banana" }));
173
173
  g.selectSea("rough");
174
174
  assert.equal(g.getSnapshot().best, 1250);
175
175
  g.selectSea("choppy");
@@ -13,6 +13,7 @@ export interface Haul {
13
13
  /** What the HUD draws. A new object whenever any of it changes, which is what `useSyncExternalStore` needs. */
14
14
  export interface Snapshot {
15
15
  phase: Phase;
16
+ paused: boolean;
16
17
  sea: SeaStateId;
17
18
  /** Counts up with every round, so the scene can start afresh. */
18
19
  runId: number;
@@ -43,7 +44,7 @@ export function browserStore(): BestStore | null {
43
44
  }
44
45
  }
45
46
 
46
- const keyFor = (sea: SeaStateId): string => `kolmo-longline:best:${sea}`;
47
+ const keyFor = (sea: SeaStateId): string => `kolmo-tugline:best:${sea}`;
47
48
 
48
49
  /**
49
50
  * The round: a score, a clock and a combo. The clock counts physics steps (`tick` is called once per step), not wall
@@ -58,6 +59,7 @@ export class Game {
58
59
  constructor(private readonly store: BestStore | null = null) {
59
60
  this.snap = {
60
61
  phase: "menu",
62
+ paused: false,
61
63
  sea: "calm",
62
64
  runId: 0,
63
65
  secondsLeft: ROUND.seconds,
@@ -87,37 +89,34 @@ export class Game {
87
89
  /** 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
90
  start(sea: SeaStateId): void {
89
91
  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
- });
92
+ this.launch(sea);
93
+ }
94
+
95
+ /** Starts a fresh round at the current (or given) sea state even if a round is already running. */
96
+ restart(sea: SeaStateId = this.snap.sea): void {
97
+ this.launch(sea);
98
+ }
99
+
100
+ /** Toggles pause while a round is active. */
101
+ togglePause(): void {
102
+ if (this.snap.phase !== "playing") return;
103
+ this.set({ paused: !this.snap.paused });
105
104
  }
106
105
 
107
106
  /** Back to the title screen. The sea stays the one that was last played. */
108
107
  toMenu(): void {
109
- this.set({ phase: "menu", runId: this.snap.runId + 1, best: this.readBest(this.snap.sea) });
108
+ this.set({ phase: "menu", paused: false, runId: this.snap.runId + 1, best: this.readBest(this.snap.sea) });
110
109
  }
111
110
 
112
111
  /** Selects a sea state on the title screen. */
113
112
  selectSea(sea: SeaStateId): void {
114
113
  if (this.snap.phase === "playing") return;
115
- this.set({ sea, best: this.readBest(sea), runId: this.snap.runId + 1 });
114
+ this.set({ sea, paused: false, best: this.readBest(sea), runId: this.snap.runId + 1 });
116
115
  }
117
116
 
118
117
  /** One physics step of the round. */
119
118
  tick(dt: number): void {
120
- if (this.snap.phase !== "playing") return;
119
+ if (this.snap.phase !== "playing" || this.snap.paused) return;
121
120
  this.timeLeft -= dt;
122
121
  this.sinceHaul += dt;
123
122
  if (this.timeLeft <= 0) {
@@ -157,12 +156,32 @@ export class Game {
157
156
  this.set({ lost: this.snap.lost + count });
158
157
  }
159
158
 
159
+ private launch(sea: SeaStateId): void {
160
+ this.timeLeft = ROUND.seconds;
161
+ this.sinceHaul = Infinity;
162
+ this.set({
163
+ phase: "playing",
164
+ paused: false,
165
+ sea,
166
+ runId: this.snap.runId + 1,
167
+ secondsLeft: ROUND.seconds,
168
+ score: 0,
169
+ combo: 0,
170
+ hauled: 0,
171
+ lost: 0,
172
+ best: this.readBest(sea),
173
+ newBest: false,
174
+ lastHaul: null,
175
+ });
176
+ }
177
+
160
178
  private finish(): void {
161
179
  this.timeLeft = 0;
162
180
  const newBest = this.snap.score > this.snap.best;
163
181
  if (newBest) this.writeBest(this.snap.sea, this.snap.score);
164
182
  this.set({
165
183
  phase: "over",
184
+ paused: false,
166
185
  secondsLeft: 0,
167
186
  combo: 0,
168
187
  best: newBest ? this.snap.score : this.snap.best,
@@ -0,0 +1,13 @@
1
+ import React from "react";
2
+ import { createRoot } from "react-dom/client";
3
+ import { App } from "./App.js";
4
+ import { game, helm } from "./game/session.js";
5
+ import "./styles.css";
6
+
7
+ const root = document.getElementById("root");
8
+ if (root === null) throw new Error("index.html has no #root");
9
+ createRoot(root).render(
10
+ <React.StrictMode>
11
+ <App game={game} helm={helm} />
12
+ </React.StrictMode>,
13
+ );