@pinkaphant/create-kolmo 0.1.3 → 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 +51 -28
  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
package/README.md CHANGED
@@ -1,15 +1,25 @@
1
1
  # `@pinkaphant/create-kolmo`
2
2
 
3
- Scaffold a new `@pinkaphant/kolmo` + `@khr3/core` + `@khr3/roads` + `@khr3/c12` 3D physics game in one command — pre-wired with `KOLMO_SINGLETON_DEDUPE`, exact Rapier 0.19 + Ecctrl peer versions, and `<React.StrictMode>`.
3
+ Scaffold a playable Kolmo game with React, TypeScript, Vite, and Rapier.
4
+
5
+ ## Nautical towing game
4
6
 
5
7
  ```bash
6
- npm create @pinkaphant/kolmo@latest my-game
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
- Or via `npx`:
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.
@@ -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 (const a of args) {
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 = {
@@ -49,28 +90,25 @@ const pkgJson = {
49
90
  },
50
91
  dependencies: {
51
92
  "@dimforge/rapier3d-compat": "0.19.2",
52
- "@khr3/c12": "^0.3.12",
53
- "@khr3/core": "^0.2.15",
93
+ "@khr3/c12": "^0.4.0",
94
+ "@khr3/core": "^0.2.16",
54
95
  "@khr3/roads": "^0.1.1",
55
- "@pinkaphant/ecctrl": "2.0.4-beta.5",
56
- "@pinkaphant/kolmo": "^0.5.6",
57
- "@react-three/drei": "^10.7.7",
96
+ "@pinkaphant/ecctrl": "2.0.4-beta.6",
97
+ "@pinkaphant/kolmo": "^0.10.0",
58
98
  "@react-three/fiber": "^9.6.1",
59
99
  "@react-three/rapier": "^2.2.0",
60
100
  koota: "^0.6.6",
61
- maath: "^0.10.8",
62
101
  react: "^19.2.7",
63
102
  "react-dom": "^19.2.7",
64
103
  three: "^0.184.0",
65
- zustand: "^5.1.0",
66
104
  },
67
105
  devDependencies: {
68
106
  "@types/react": "^19.2.16",
69
107
  "@types/react-dom": "^19.2.3",
70
108
  "@types/three": "^0.184.0",
71
- "@vitejs/plugin-react": "^4.5.2",
109
+ "@vitejs/plugin-react": "^6.0.0",
72
110
  typescript: "^5.8.3",
73
- vite: "^6.3.5",
111
+ vite: "^8.0.0",
74
112
  },
75
113
  };
76
114
 
@@ -227,19 +265,4 @@ createRoot(document.getElementById("root")!).render(
227
265
  `,
228
266
  );
229
267
 
230
- console.log(`\nScaffolded Kolmo project in ${root}`);
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
- `);
268
+ finishProject();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pinkaphant/create-kolmo",
3
- "version": "0.1.3",
3
+ "version": "0.3.0",
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
+ }