@pinkaphant/create-kolmo 0.1.4 → 0.3.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 +15 -5
- package/bin/create-kolmo.mjs +45 -19
- package/package.json +2 -1
- package/templates/longline/README.md +14 -0
- package/templates/longline/index.html +15 -0
- package/templates/longline/package.json +38 -0
- package/templates/longline/src/App.tsx +48 -0
- package/templates/longline/src/components/Boat.tsx +97 -0
- package/templates/longline/src/components/CameraRig.tsx +61 -0
- package/templates/longline/src/components/Crates.tsx +48 -0
- package/templates/longline/src/components/Hud.tsx +197 -0
- package/templates/longline/src/components/Longline.tsx +107 -0
- package/templates/longline/src/components/Ocean.tsx +27 -0
- package/templates/longline/src/components/Radar.tsx +88 -0
- package/templates/longline/src/components/Referee.tsx +39 -0
- package/templates/longline/src/components/Scene.tsx +56 -0
- package/templates/longline/src/components/Sky.tsx +23 -0
- package/templates/longline/src/components/ocean-material.ts +143 -0
- package/templates/longline/src/components/round-context.ts +26 -0
- package/templates/longline/src/game/boat-control.ts +131 -0
- package/templates/longline/src/game/constants.ts +98 -0
- package/templates/longline/src/game/input.test.ts +83 -0
- package/templates/longline/src/game/input.ts +73 -0
- package/templates/longline/src/game/logic.test.ts +110 -0
- package/templates/longline/src/game/logic.ts +109 -0
- package/templates/longline/src/game/physics.test.ts +211 -0
- package/templates/longline/src/game/radar.ts +22 -0
- package/templates/longline/src/game/registry.ts +53 -0
- package/templates/longline/src/game/sea.ts +26 -0
- package/templates/longline/src/game/session.ts +6 -0
- package/templates/longline/src/game/simulation.test.ts +393 -0
- package/templates/longline/src/game/simulation.ts +137 -0
- package/templates/longline/src/game/store.test.ts +222 -0
- package/templates/longline/src/game/store.ts +195 -0
- package/templates/longline/src/game/test-world.ts +128 -0
- package/templates/longline/src/main.tsx +8 -0
- package/templates/longline/src/sfx.ts +50 -0
- package/templates/longline/src/styles.css +374 -0
- package/templates/longline/tsconfig.json +18 -0
- package/templates/longline/vite.config.ts +28 -0
package/README.md
CHANGED
|
@@ -1,15 +1,25 @@
|
|
|
1
1
|
# `@pinkaphant/create-kolmo`
|
|
2
2
|
|
|
3
|
-
Scaffold a
|
|
3
|
+
Scaffold a playable Kolmo game with React, TypeScript, Vite, and Rapier.
|
|
4
|
+
|
|
5
|
+
## Nautical towing game
|
|
4
6
|
|
|
5
7
|
```bash
|
|
6
|
-
|
|
7
|
-
cd my-game
|
|
8
|
+
npx @pinkaphant/create-kolmo@latest my-longline-game --template longline
|
|
9
|
+
cd my-longline-game
|
|
8
10
|
npm run dev
|
|
9
11
|
```
|
|
10
12
|
|
|
11
|
-
|
|
13
|
+
Longline includes the complete deterministic sea simulation, boat controls, tow line, collectible crates, radar, camera modes, and HUD. **WASD** or the **arrow keys** steer the boat.
|
|
14
|
+
|
|
15
|
+
## Road and vehicle sandbox
|
|
12
16
|
|
|
13
17
|
```bash
|
|
14
|
-
npx @pinkaphant/create-kolmo@latest my-game
|
|
18
|
+
npx @pinkaphant/create-kolmo@latest my-road-game
|
|
19
|
+
cd my-road-game
|
|
20
|
+
npm run dev
|
|
15
21
|
```
|
|
22
|
+
|
|
23
|
+
The default road template includes a road heightfield, vehicle, on-foot controller, and toolgun. **WASD** moves, **Shift** runs, **F** boards, and **Space** jumps.
|
|
24
|
+
|
|
25
|
+
Add `--no-install` to either command to skip dependency installation.
|
package/bin/create-kolmo.mjs
CHANGED
|
@@ -6,21 +6,35 @@ import { spawnSync } from "node:child_process";
|
|
|
6
6
|
const args = process.argv.slice(2);
|
|
7
7
|
let targetArg = "";
|
|
8
8
|
let skipInstall = false;
|
|
9
|
+
let template = "road";
|
|
9
10
|
|
|
10
|
-
for (
|
|
11
|
+
for (let i = 0; i < args.length; i++) {
|
|
12
|
+
const a = args[i];
|
|
11
13
|
if (a === "--no-install" || a === "--skip-install") {
|
|
12
14
|
skipInstall = true;
|
|
15
|
+
} else if (a === "--template") {
|
|
16
|
+
template = args[++i] || "";
|
|
17
|
+
} else if (a.startsWith("--template=")) {
|
|
18
|
+
template = a.slice("--template=".length);
|
|
13
19
|
} else if (a === "-h" || a === "--help") {
|
|
14
20
|
console.log(
|
|
15
|
-
"Usage: npm create @pinkaphant/kolmo@latest <project-dir> [--no-install]\n" +
|
|
16
|
-
" or: npx @pinkaphant/create-kolmo@latest <project-dir> [--no-install]",
|
|
21
|
+
"Usage: npm create @pinkaphant/kolmo@latest <project-dir> [--template road|longline] [--no-install]\n" +
|
|
22
|
+
" or: npx @pinkaphant/create-kolmo@latest <project-dir> [--template road|longline] [--no-install]",
|
|
17
23
|
);
|
|
18
24
|
process.exit(0);
|
|
19
25
|
} else if (!a.startsWith("-") && !targetArg) {
|
|
20
26
|
targetArg = a;
|
|
27
|
+
} else {
|
|
28
|
+
console.error(`Error: Unknown argument "${a}".`);
|
|
29
|
+
process.exit(1);
|
|
21
30
|
}
|
|
22
31
|
}
|
|
23
32
|
|
|
33
|
+
if (template !== "road" && template !== "longline") {
|
|
34
|
+
console.error(`Error: Unknown template "${template}". Available templates: road, longline.`);
|
|
35
|
+
process.exit(1);
|
|
36
|
+
}
|
|
37
|
+
|
|
24
38
|
const dirName = targetArg || "kolmo-game";
|
|
25
39
|
const root = path.resolve(process.cwd(), dirName);
|
|
26
40
|
const pkgName = path
|
|
@@ -34,6 +48,33 @@ if (fs.existsSync(root) && fs.readdirSync(root).length > 0) {
|
|
|
34
48
|
process.exit(1);
|
|
35
49
|
}
|
|
36
50
|
|
|
51
|
+
function finishProject() {
|
|
52
|
+
console.log(`\nScaffolded Kolmo ${template} project in ${root}`);
|
|
53
|
+
|
|
54
|
+
if (!skipInstall) {
|
|
55
|
+
console.log("Installing dependencies with npm...");
|
|
56
|
+
const res = spawnSync("npm", ["install"], { cwd: root, stdio: "inherit", shell: process.platform === "win32" });
|
|
57
|
+
if (res.status !== 0) {
|
|
58
|
+
console.error("\nnpm install exited with an error. Run `npm install` inside the project directory.");
|
|
59
|
+
process.exit(res.status ?? 1);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
console.log(`\nReady! Next steps:\n cd ${dirName}\n npm run dev\n`);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (template === "longline") {
|
|
67
|
+
const templateRoot = path.resolve(import.meta.dirname, "../templates/longline");
|
|
68
|
+
fs.cpSync(templateRoot, root, { recursive: true });
|
|
69
|
+
const templatePkgPath = path.join(root, "package.json");
|
|
70
|
+
const templatePkg = JSON.parse(fs.readFileSync(templatePkgPath, "utf8"));
|
|
71
|
+
templatePkg.name = pkgName;
|
|
72
|
+
fs.writeFileSync(templatePkgPath, JSON.stringify(templatePkg, null, 2) + "\n");
|
|
73
|
+
fs.writeFileSync(path.join(root, ".gitignore"), "node_modules/\ndist/\n.DS_Store\n*.local\n");
|
|
74
|
+
finishProject();
|
|
75
|
+
process.exit(0);
|
|
76
|
+
}
|
|
77
|
+
|
|
37
78
|
fs.mkdirSync(path.join(root, "src"), { recursive: true });
|
|
38
79
|
|
|
39
80
|
const pkgJson = {
|
|
@@ -227,19 +268,4 @@ createRoot(document.getElementById("root")!).render(
|
|
|
227
268
|
`,
|
|
228
269
|
);
|
|
229
270
|
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
if (!skipInstall) {
|
|
233
|
-
console.log("Installing dependencies with npm...");
|
|
234
|
-
const res = spawnSync("npm", ["install"], { cwd: root, stdio: "inherit", shell: process.platform === "win32" });
|
|
235
|
-
if (res.status !== 0) {
|
|
236
|
-
console.error("\nnpm install exited with an error. Run `npm install` inside the project directory.");
|
|
237
|
-
process.exit(res.status ?? 1);
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
console.log(`
|
|
242
|
-
Ready! Next steps:
|
|
243
|
-
cd ${dirName}
|
|
244
|
-
npm run dev
|
|
245
|
-
`);
|
|
271
|
+
finishProject();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pinkaphant/create-kolmo",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "Scaffold a new @pinkaphant/kolmo + @khr3/core + @khr3/roads + @khr3/c12 3D physics game in one command",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
},
|
|
10
10
|
"files": [
|
|
11
11
|
"bin",
|
|
12
|
+
"templates",
|
|
12
13
|
"LICENSE",
|
|
13
14
|
"README.md"
|
|
14
15
|
],
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# Longline
|
|
2
|
+
|
|
3
|
+
A deterministic nautical towing game built with React Three Fiber, Rapier, and Kolmo's water module.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
npm run dev
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
Steer the boat with **WASD** or the **arrow keys**. Tow the line through drifting crates to collect them. Use the HUD to change sea state, pause, restart, or switch camera.
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
npm test
|
|
13
|
+
npm run build
|
|
14
|
+
```
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8" />
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover, user-scalable=no" />
|
|
6
|
+
<meta name="theme-color" content="#b9dbea" />
|
|
7
|
+
<meta name="description" content="Longline: tow a line of floats across a rolling sea and haul in the drifting crates. A game on the water module of @pinkaphant/kolmo." />
|
|
8
|
+
<title>Longline</title>
|
|
9
|
+
<link rel="icon" href="data:image/svg+xml,%3Csvg xmlns=%27http://www.w3.org/2000/svg%27 viewBox=%270 0 32 32%27%3E%3Crect width=%2732%27 height=%2732%27 rx=%277%27 fill=%27%23134a6b%27/%3E%3Cpath d=%27M4 20q6-6 12 0t12 0%27 stroke=%27%23ffd23f%27 stroke-width=%273%27 fill=%27none%27/%3E%3C/svg%3E" />
|
|
10
|
+
</head>
|
|
11
|
+
<body>
|
|
12
|
+
<div id="root"><p class="loading">Loading the sea…</p></div>
|
|
13
|
+
<script type="module" src="/src/main.tsx"></script>
|
|
14
|
+
</body>
|
|
15
|
+
</html>
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "longline-game",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"private": true,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"dev": "vite",
|
|
8
|
+
"typecheck": "tsc --noEmit",
|
|
9
|
+
"build": "tsc --noEmit && vite build",
|
|
10
|
+
"preview": "vite preview",
|
|
11
|
+
"test": "node --import tsx --test \"src/**/*.test.ts\""
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"@dimforge/rapier3d-compat": "0.19.2",
|
|
15
|
+
"@pinkaphant/ecctrl": "2.0.4-beta.6",
|
|
16
|
+
"@pinkaphant/kolmo": "^0.10.0",
|
|
17
|
+
"@react-three/fiber": "^9.6.1",
|
|
18
|
+
"@react-three/rapier": "^2.2.0",
|
|
19
|
+
"react": "^19.2.7",
|
|
20
|
+
"react-dom": "^19.2.7",
|
|
21
|
+
"three": "0.184.0"
|
|
22
|
+
},
|
|
23
|
+
"overrides": {
|
|
24
|
+
"@dimforge/rapier3d-compat": "0.19.2",
|
|
25
|
+
"@pinkaphant/ecctrl": "2.0.4-beta.6",
|
|
26
|
+
"three": "0.184.0"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@types/node": "^22.14.0",
|
|
30
|
+
"@types/react": "^19.2.16",
|
|
31
|
+
"@types/react-dom": "^19.2.3",
|
|
32
|
+
"@types/three": "0.184.0",
|
|
33
|
+
"@vitejs/plugin-react": "^6.1.1",
|
|
34
|
+
"tsx": "^4.21.0",
|
|
35
|
+
"typescript": "^7.0.2",
|
|
36
|
+
"vite": "^8.3.0"
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { Canvas } from "@react-three/fiber";
|
|
2
|
+
import { Component, Suspense, useEffect, useSyncExternalStore, type ReactNode } from "react";
|
|
3
|
+
import { Hud } from "./components/Hud.js";
|
|
4
|
+
import { PALETTE } from "./components/ocean-material.js";
|
|
5
|
+
import { Scene } from "./components/Scene.js";
|
|
6
|
+
import { attachKeyboard, type Helm } from "./game/input.js";
|
|
7
|
+
import type { Game } from "./game/store.js";
|
|
8
|
+
|
|
9
|
+
/** The canvas throws when the browser cannot make a WebGL context; say so instead of leaving a blank page. */
|
|
10
|
+
class CanvasBoundary extends Component<{ children: ReactNode }, { failed: boolean }> {
|
|
11
|
+
override state = { failed: false };
|
|
12
|
+
|
|
13
|
+
static getDerivedStateFromError(): { failed: boolean } {
|
|
14
|
+
return { failed: true };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
override render(): ReactNode {
|
|
18
|
+
if (this.state.failed) {
|
|
19
|
+
return (
|
|
20
|
+
<div className="fatal">
|
|
21
|
+
<h1>Longline needs WebGL</h1>
|
|
22
|
+
<p>This browser could not start a WebGL canvas. Try another browser, or turn on hardware acceleration.</p>
|
|
23
|
+
</div>
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
return this.props.children;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function App({ game, helm }: { game: Game; helm: Helm }) {
|
|
31
|
+
const snap = useSyncExternalStore(game.subscribe, game.getSnapshot);
|
|
32
|
+
useEffect(() => attachKeyboard(helm), [helm]);
|
|
33
|
+
return (
|
|
34
|
+
<>
|
|
35
|
+
<div className="stage">
|
|
36
|
+
<CanvasBoundary>
|
|
37
|
+
<Canvas flat dpr={[1, 2]} camera={{ fov: 58, near: 0.1, far: 600, position: [0, 8, 16] }}>
|
|
38
|
+
<color attach="background" args={[PALETTE.horizon]} />
|
|
39
|
+
<Suspense fallback={null}>
|
|
40
|
+
<Scene key={snap.runId} sea={snap.sea} runId={snap.runId} game={game} helm={helm} />
|
|
41
|
+
</Suspense>
|
|
42
|
+
</Canvas>
|
|
43
|
+
</CanvasBoundary>
|
|
44
|
+
</div>
|
|
45
|
+
<Hud game={game} helm={helm} />
|
|
46
|
+
</>
|
|
47
|
+
);
|
|
48
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { useBuoyancy } from "@pinkaphant/kolmo/water/react";
|
|
2
|
+
import { CuboidCollider, RigidBody, interactionGroups } from "@react-three/rapier";
|
|
3
|
+
import { useLayoutEffect, useRef } from "react";
|
|
4
|
+
import type { Object3D } from "three";
|
|
5
|
+
import { BOAT, FIXED_DT } from "../game/constants.js";
|
|
6
|
+
import { useRound } from "./round-context.js";
|
|
7
|
+
|
|
8
|
+
/** Nothing in this game collides with anything: the rules look at distances, and the water does the pushing. */
|
|
9
|
+
export const NO_CONTACT = interactionGroups(0, []);
|
|
10
|
+
|
|
11
|
+
const HULL = "#f4efe4";
|
|
12
|
+
const BOOT_TOP = "#b8322a";
|
|
13
|
+
const CABIN = "#e9b44c";
|
|
14
|
+
const GLASS = "#27414f";
|
|
15
|
+
|
|
16
|
+
function BoatMesh() {
|
|
17
|
+
const [hx, hy, hz] = BOAT.halfExtents;
|
|
18
|
+
// the bow is a triangular prism as wide as the hull: its circumradius is width / sqrt(3)
|
|
19
|
+
const r = (2 * hx) / Math.sqrt(3);
|
|
20
|
+
return (
|
|
21
|
+
<group>
|
|
22
|
+
<mesh>
|
|
23
|
+
<boxGeometry args={[2 * hx, 2 * hy, 2 * hz]} />
|
|
24
|
+
<meshStandardMaterial color={HULL} roughness={0.6} flatShading />
|
|
25
|
+
</mesh>
|
|
26
|
+
<mesh position={[0, -0.14, 0]}>
|
|
27
|
+
<boxGeometry args={[2 * hx + 0.02, 0.22, 2 * hz + 0.02]} />
|
|
28
|
+
<meshStandardMaterial color={BOOT_TOP} roughness={0.7} flatShading />
|
|
29
|
+
</mesh>
|
|
30
|
+
<mesh position={[0, 0, -hz - r / 2]} rotation={[0, Math.PI, 0]}>
|
|
31
|
+
<cylinderGeometry args={[r, r, 2 * hy, 3]} />
|
|
32
|
+
<meshStandardMaterial color={HULL} roughness={0.6} flatShading />
|
|
33
|
+
</mesh>
|
|
34
|
+
<mesh position={[0, -0.14, -hz - r / 2]} rotation={[0, Math.PI, 0]}>
|
|
35
|
+
<cylinderGeometry args={[r + 0.02, r + 0.02, 0.22, 3]} />
|
|
36
|
+
<meshStandardMaterial color={BOOT_TOP} roughness={0.7} flatShading />
|
|
37
|
+
</mesh>
|
|
38
|
+
<mesh position={[0, hy + 0.325, 0.5]}>
|
|
39
|
+
<boxGeometry args={[1.0, 0.65, 1.3]} />
|
|
40
|
+
<meshStandardMaterial color={CABIN} roughness={0.6} flatShading />
|
|
41
|
+
</mesh>
|
|
42
|
+
<mesh position={[0, hy + 0.37, 0.5]}>
|
|
43
|
+
<boxGeometry args={[1.02, 0.2, 0.9]} />
|
|
44
|
+
<meshStandardMaterial color={GLASS} roughness={0.2} />
|
|
45
|
+
</mesh>
|
|
46
|
+
<mesh position={[0, hy + 0.68, 0.5]}>
|
|
47
|
+
<boxGeometry args={[1.12, 0.06, 1.42]} />
|
|
48
|
+
<meshStandardMaterial color={HULL} roughness={0.6} flatShading />
|
|
49
|
+
</mesh>
|
|
50
|
+
<mesh position={[0, hy + 1.1, 0.85]}>
|
|
51
|
+
<cylinderGeometry args={[0.03, 0.03, 0.9, 6]} />
|
|
52
|
+
<meshStandardMaterial color="#d9d9d9" />
|
|
53
|
+
</mesh>
|
|
54
|
+
<mesh position={[0.17, hy + 1.45, 0.85]}>
|
|
55
|
+
<boxGeometry args={[0.34, 0.18, 0.02]} />
|
|
56
|
+
<meshStandardMaterial color="#ff3d6e" />
|
|
57
|
+
</mesh>
|
|
58
|
+
<mesh position={[BOAT.tow[0], BOAT.tow[1], BOAT.tow[2]]}>
|
|
59
|
+
<boxGeometry args={[0.16, 0.12, 0.16]} />
|
|
60
|
+
<meshStandardMaterial color="#2b2b2b" />
|
|
61
|
+
</mesh>
|
|
62
|
+
</group>
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The boat: one rigid body, a cuboid at half the density of water so it draws half its height, floated by Kolmo's
|
|
68
|
+
* `useBuoyancy` with four slices so the hull sees the wave under its bow and the one under its stern separately.
|
|
69
|
+
* The helm (thrust, rudder, keel) is applied by the referee each step.
|
|
70
|
+
*/
|
|
71
|
+
export function Boat() {
|
|
72
|
+
const { sea, clock, registry } = useRound();
|
|
73
|
+
const anchor = useRef<Object3D>(null);
|
|
74
|
+
const controller = useBuoyancy(registry.boat, {
|
|
75
|
+
surface: sea.surface,
|
|
76
|
+
clock,
|
|
77
|
+
timeStep: FIXED_DT,
|
|
78
|
+
cells: { slices: BOAT.slices },
|
|
79
|
+
drag: BOAT.drag,
|
|
80
|
+
});
|
|
81
|
+
useLayoutEffect(() => {
|
|
82
|
+
registry.boatController = controller;
|
|
83
|
+
registry.boatObject = anchor.current;
|
|
84
|
+
return () => {
|
|
85
|
+
registry.boatController = null;
|
|
86
|
+
registry.boatObject = null;
|
|
87
|
+
};
|
|
88
|
+
}, [registry, controller]);
|
|
89
|
+
const [hx, hy, hz] = BOAT.halfExtents;
|
|
90
|
+
return (
|
|
91
|
+
<RigidBody ref={registry.boat} colliders={false} position={[0, 0, 0]} canSleep={false}>
|
|
92
|
+
<CuboidCollider args={[hx, hy, hz]} density={BOAT.density} collisionGroups={NO_CONTACT} />
|
|
93
|
+
<object3D ref={anchor} />
|
|
94
|
+
<BoatMesh />
|
|
95
|
+
</RigidBody>
|
|
96
|
+
);
|
|
97
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { useFrame } from "@react-three/fiber";
|
|
2
|
+
import { useRef } from "react";
|
|
3
|
+
import * as THREE from "three";
|
|
4
|
+
import { useRound } from "./round-context.js";
|
|
5
|
+
|
|
6
|
+
const CHASE = { distance: 13, height: 6.5, lookAhead: 5, follow: 5, turn: 2.4 } as const;
|
|
7
|
+
const ORBIT = { distance: 21, height: 7, speed: 0.1 } as const;
|
|
8
|
+
|
|
9
|
+
/** Shortest signed angle from `from` to `to`, radians. */
|
|
10
|
+
function angleDelta(from: number, to: number): number {
|
|
11
|
+
return Math.atan2(Math.sin(to - from), Math.cos(to - from));
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The camera: behind the boat and turning after its heading while a round is on, circling it on the title and result
|
|
16
|
+
* screens. It reads the boat's interpolated pose, so it does not stutter between physics steps.
|
|
17
|
+
*/
|
|
18
|
+
export function CameraRig() {
|
|
19
|
+
const { registry, game } = useRound();
|
|
20
|
+
const s = useRef({ ready: false, heading: 0, orbit: 0, look: new THREE.Vector3() });
|
|
21
|
+
const tmp = useRef({ pos: new THREE.Vector3(), quat: new THREE.Quaternion(), fwd: new THREE.Vector3(), want: new THREE.Vector3(), lookAt: new THREE.Vector3() });
|
|
22
|
+
|
|
23
|
+
useFrame(({ camera }, delta) => {
|
|
24
|
+
const boat = registry.boatObject;
|
|
25
|
+
if (boat === null) return;
|
|
26
|
+
const dt = Math.min(delta, 0.1);
|
|
27
|
+
const st = s.current;
|
|
28
|
+
const { pos, quat, fwd, want, lookAt } = tmp.current;
|
|
29
|
+
boat.updateWorldMatrix(true, false);
|
|
30
|
+
boat.getWorldPosition(pos);
|
|
31
|
+
boat.getWorldQuaternion(quat);
|
|
32
|
+
fwd.set(0, 0, -1).applyQuaternion(quat);
|
|
33
|
+
const heading = Math.atan2(fwd.x, -fwd.z);
|
|
34
|
+
|
|
35
|
+
if (game.phase === "playing") {
|
|
36
|
+
if (!st.ready) st.heading = heading;
|
|
37
|
+
st.heading += angleDelta(st.heading, heading) * (1 - Math.exp(-CHASE.turn * dt));
|
|
38
|
+
const sx = Math.sin(st.heading);
|
|
39
|
+
const sz = -Math.cos(st.heading);
|
|
40
|
+
want.set(pos.x - sx * CHASE.distance, pos.y + CHASE.height, pos.z - sz * CHASE.distance);
|
|
41
|
+
lookAt.set(pos.x + sx * CHASE.lookAhead, pos.y + 0.5, pos.z + sz * CHASE.lookAhead);
|
|
42
|
+
st.orbit = Math.atan2(-sx, sz);
|
|
43
|
+
} else {
|
|
44
|
+
st.orbit += ORBIT.speed * dt;
|
|
45
|
+
want.set(pos.x + Math.sin(st.orbit) * ORBIT.distance, pos.y + ORBIT.height, pos.z + Math.cos(st.orbit) * ORBIT.distance);
|
|
46
|
+
lookAt.set(pos.x, pos.y + 1, pos.z);
|
|
47
|
+
st.heading = heading;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (!st.ready) {
|
|
51
|
+
camera.position.copy(want);
|
|
52
|
+
st.look.copy(lookAt);
|
|
53
|
+
st.ready = true;
|
|
54
|
+
} else {
|
|
55
|
+
camera.position.lerp(want, 1 - Math.exp(-CHASE.follow * dt));
|
|
56
|
+
st.look.lerp(lookAt, 1 - Math.exp(-CHASE.follow * dt));
|
|
57
|
+
}
|
|
58
|
+
camera.lookAt(st.look);
|
|
59
|
+
});
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { useBuoyancy } from "@pinkaphant/kolmo/water/react";
|
|
2
|
+
import { CuboidCollider, RigidBody } from "@react-three/rapier";
|
|
3
|
+
import { CRATE, FIXED_DT } from "../game/constants.js";
|
|
4
|
+
import { NO_CONTACT } from "./Boat.js";
|
|
5
|
+
import { useRound } from "./round-context.js";
|
|
6
|
+
|
|
7
|
+
function Crate({ index }: { index: number }) {
|
|
8
|
+
const { sea, clock, registry, simulation } = useRound();
|
|
9
|
+
const self = registry.crates[index]!;
|
|
10
|
+
useBuoyancy(self, { surface: sea.surface, clock, timeStep: FIXED_DT });
|
|
11
|
+
const start = simulation.initialCrates[index]!;
|
|
12
|
+
const y = sea.field.heightAt(start.x, start.z, 0);
|
|
13
|
+
const [hx, hy, hz] = CRATE.halfExtents;
|
|
14
|
+
return (
|
|
15
|
+
<RigidBody ref={self} colliders={false} position={[start.x, y, start.z]} rotation={[0, (index * 1.7) % (2 * Math.PI), 0]} canSleep={false}>
|
|
16
|
+
<CuboidCollider args={[hx, hy, hz]} density={CRATE.density} collisionGroups={NO_CONTACT} />
|
|
17
|
+
<mesh>
|
|
18
|
+
<boxGeometry args={[2 * hx, 2 * hy, 2 * hz]} />
|
|
19
|
+
<meshStandardMaterial color="#c98a3b" roughness={0.8} flatShading />
|
|
20
|
+
</mesh>
|
|
21
|
+
{[-0.28, 0, 0.28].map((z) => (
|
|
22
|
+
<mesh key={z} position={[0, hy + 0.01, z]}>
|
|
23
|
+
<boxGeometry args={[2 * hx + 0.02, 0.04, 0.2]} />
|
|
24
|
+
<meshStandardMaterial color="#8e5c22" roughness={0.9} flatShading />
|
|
25
|
+
</mesh>
|
|
26
|
+
))}
|
|
27
|
+
<mesh position={[0.3, 0.85, 0.3]}>
|
|
28
|
+
<cylinderGeometry args={[0.02, 0.02, 1.2, 6]} />
|
|
29
|
+
<meshStandardMaterial color="#f2f2f2" />
|
|
30
|
+
</mesh>
|
|
31
|
+
<mesh position={[0.45, 1.3, 0.3]}>
|
|
32
|
+
<boxGeometry args={[0.3, 0.2, 0.01]} />
|
|
33
|
+
<meshStandardMaterial color="#ff3d6e" />
|
|
34
|
+
</mesh>
|
|
35
|
+
</RigidBody>
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** The crates: floating cuboids at 350 kg/m3, each on its own `useBuoyancy`, carried along by the current. */
|
|
40
|
+
export function Crates() {
|
|
41
|
+
return (
|
|
42
|
+
<>
|
|
43
|
+
{Array.from({ length: CRATE.count }, (_, i) => (
|
|
44
|
+
<Crate key={i} index={i} />
|
|
45
|
+
))}
|
|
46
|
+
</>
|
|
47
|
+
);
|
|
48
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { 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 aria-label="Steer to port" {...set("steer", -1)}>
|
|
51
|
+
◀
|
|
52
|
+
</button>
|
|
53
|
+
<button aria-label="Steer to starboard" {...set("steer", 1)}>
|
|
54
|
+
▶
|
|
55
|
+
</button>
|
|
56
|
+
</div>
|
|
57
|
+
<div className="touch-side">
|
|
58
|
+
<button aria-label="Astern" {...set("throttle", -1)}>
|
|
59
|
+
▼
|
|
60
|
+
</button>
|
|
61
|
+
<button aria-label="Ahead" {...set("throttle", 1)}>
|
|
62
|
+
▲
|
|
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) => (
|
|
73
|
+
<button
|
|
74
|
+
key={id}
|
|
75
|
+
role="radio"
|
|
76
|
+
aria-checked={snap.sea === id}
|
|
77
|
+
className={snap.sea === id ? "sea selected" : "sea"}
|
|
78
|
+
onClick={() => game.selectSea(id)}
|
|
79
|
+
>
|
|
80
|
+
<strong>{SEA_STATES[id].label}</strong>
|
|
81
|
+
<span>Current {SEA_STATES[id].current.toFixed(1)} m/s</span>
|
|
82
|
+
</button>
|
|
83
|
+
))}
|
|
84
|
+
</div>
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function Title({ game, snap }: { game: Game; snap: Snapshot }) {
|
|
89
|
+
const start = (): void => {
|
|
90
|
+
unlockAudio();
|
|
91
|
+
game.start(snap.sea);
|
|
92
|
+
};
|
|
93
|
+
useEffect(() => {
|
|
94
|
+
const onKey = (e: KeyboardEvent): void => {
|
|
95
|
+
if (e.code === "Enter" || e.code === "Space") {
|
|
96
|
+
e.preventDefault();
|
|
97
|
+
start();
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
window.addEventListener("keydown", onKey);
|
|
101
|
+
return () => window.removeEventListener("keydown", onKey);
|
|
102
|
+
});
|
|
103
|
+
return (
|
|
104
|
+
<div className="panel title">
|
|
105
|
+
<h1>Longline</h1>
|
|
106
|
+
<p className="tag">Tow the line through the crates before time runs out.</p>
|
|
107
|
+
<SeaPicker game={game} snap={snap} />
|
|
108
|
+
<p className="best">{snap.best > 0 ? `Best at ${SEA_STATES[snap.sea].label.toLowerCase()} sea: ${snap.best}` : "No score at this sea yet"}</p>
|
|
109
|
+
<button className="go" onClick={start}>
|
|
110
|
+
Start
|
|
111
|
+
</button>
|
|
112
|
+
<p className="keys">
|
|
113
|
+
<kbd>W</kbd> <kbd>S</kbd> throttle · <kbd>A</kbd> <kbd>D</kbd> steer · arrow keys work too · <kbd>Enter</kbd> starts
|
|
114
|
+
</p>
|
|
115
|
+
<p className="credit">Floated by the water module of @pinkaphant/kolmo</p>
|
|
116
|
+
</div>
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function Result({ game, snap }: { game: Game; snap: Snapshot }) {
|
|
121
|
+
const again = (): void => {
|
|
122
|
+
unlockAudio();
|
|
123
|
+
game.start(snap.sea);
|
|
124
|
+
};
|
|
125
|
+
useEffect(() => {
|
|
126
|
+
const onKey = (e: KeyboardEvent): void => {
|
|
127
|
+
if (e.code === "Enter") again();
|
|
128
|
+
};
|
|
129
|
+
window.addEventListener("keydown", onKey);
|
|
130
|
+
return () => window.removeEventListener("keydown", onKey);
|
|
131
|
+
});
|
|
132
|
+
return (
|
|
133
|
+
<div className="panel result">
|
|
134
|
+
<h2>Time's up</h2>
|
|
135
|
+
<div className="big">{snap.score}</div>
|
|
136
|
+
{snap.newBest ? <p className="newbest">New best!</p> : <p className="best">Best: {snap.best}</p>}
|
|
137
|
+
<p className="stats">
|
|
138
|
+
{snap.hauled} hauled · {snap.lost} lost to the current
|
|
139
|
+
</p>
|
|
140
|
+
<div className="row">
|
|
141
|
+
<button className="go" onClick={again}>
|
|
142
|
+
Sail again
|
|
143
|
+
</button>
|
|
144
|
+
<button onClick={() => game.toMenu()}>Change sea</button>
|
|
145
|
+
</div>
|
|
146
|
+
</div>
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** The heads-up display: score, clock, combo, radar and, on the title and result screens, the panels. */
|
|
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
|
+
const playing = snap.phase === "playing";
|
|
157
|
+
return (
|
|
158
|
+
<div className="hud">
|
|
159
|
+
{playing ? (
|
|
160
|
+
<>
|
|
161
|
+
<div className="top">
|
|
162
|
+
<div className="score">
|
|
163
|
+
<span className="label">Score</span>
|
|
164
|
+
<span className="value">{snap.score}</span>
|
|
165
|
+
{snap.combo > 1 ? <span className="combo">×{snap.combo}</span> : null}
|
|
166
|
+
</div>
|
|
167
|
+
<div className={snap.secondsLeft <= 10 ? "clock low" : "clock"}>{formatClock(snap.secondsLeft)}</div>
|
|
168
|
+
<div className="tally">
|
|
169
|
+
<span>{snap.hauled} hauled</span>
|
|
170
|
+
<span>{snap.lost} lost</span>
|
|
171
|
+
</div>
|
|
172
|
+
</div>
|
|
173
|
+
{snap.lastHaul !== null ? (
|
|
174
|
+
<div className="toast" key={snap.lastHaul.id}>
|
|
175
|
+
+{snap.lastHaul.points}
|
|
176
|
+
{snap.lastHaul.combo > 1 ? ` ×${snap.lastHaul.combo}` : ""}
|
|
177
|
+
</div>
|
|
178
|
+
) : null}
|
|
179
|
+
{coarse ? <TouchControls helm={helm} /> : null}
|
|
180
|
+
</>
|
|
181
|
+
) : null}
|
|
182
|
+
<Radar />
|
|
183
|
+
<button
|
|
184
|
+
className="mute"
|
|
185
|
+
aria-label={muted ? "Unmute" : "Mute"}
|
|
186
|
+
onClick={() => {
|
|
187
|
+
setMuted(!muted);
|
|
188
|
+
setMutedState(!muted);
|
|
189
|
+
}}
|
|
190
|
+
>
|
|
191
|
+
{muted ? "🔇" : "🔊"}
|
|
192
|
+
</button>
|
|
193
|
+
{snap.phase === "menu" ? <Title game={game} snap={snap} /> : null}
|
|
194
|
+
{snap.phase === "over" ? <Result game={game} snap={snap} /> : null}
|
|
195
|
+
</div>
|
|
196
|
+
);
|
|
197
|
+
}
|