@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.
- package/README.md +15 -5
- package/bin/create-kolmo.mjs +50 -27
- 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 +169 -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
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { useBuoyancy } from "@pinkaphant/kolmo/water/react";
|
|
2
|
+
import { useFrame } from "@react-three/fiber";
|
|
3
|
+
import { BallCollider, RigidBody, useRopeJoint, type RapierRigidBody } from "@react-three/rapier";
|
|
4
|
+
import { useLayoutEffect, useRef, type RefObject } from "react";
|
|
5
|
+
import * as THREE from "three";
|
|
6
|
+
import { BOAT, FIXED_DT, LINE } from "../game/constants.js";
|
|
7
|
+
import { NO_CONTACT } from "./Boat.js";
|
|
8
|
+
import { useRound } from "./round-context.js";
|
|
9
|
+
|
|
10
|
+
function Float({ index }: { index: number }) {
|
|
11
|
+
const { sea, clock, registry } = useRound();
|
|
12
|
+
const self = registry.floats[index]!;
|
|
13
|
+
const prev = index === 0 ? registry.boat : registry.floats[index - 1]!;
|
|
14
|
+
const object = useRef<THREE.Object3D>(null);
|
|
15
|
+
useBuoyancy(self, { surface: sea.surface, clock, timeStep: FIXED_DT });
|
|
16
|
+
// the first rope is made fast to the stern, the others to the float before
|
|
17
|
+
useRopeJoint(prev as RefObject<RapierRigidBody>, self as RefObject<RapierRigidBody>, [
|
|
18
|
+
index === 0 ? [BOAT.tow[0], BOAT.tow[1], BOAT.tow[2]] : [0, 0, 0],
|
|
19
|
+
[0, 0, 0],
|
|
20
|
+
LINE.spacing,
|
|
21
|
+
]);
|
|
22
|
+
useLayoutEffect(() => {
|
|
23
|
+
registry.floatObjects[index] = object.current;
|
|
24
|
+
return () => {
|
|
25
|
+
registry.floatObjects[index] = null;
|
|
26
|
+
};
|
|
27
|
+
}, [registry, index]);
|
|
28
|
+
const last = index === LINE.floats - 1;
|
|
29
|
+
return (
|
|
30
|
+
<RigidBody ref={self} colliders={false} position={[0, 0, BOAT.tow[2] + (index + 1) * LINE.spacing]} canSleep={false}>
|
|
31
|
+
<BallCollider args={[LINE.floatRadius]} density={LINE.floatDensity} collisionGroups={NO_CONTACT} />
|
|
32
|
+
<object3D ref={object} />
|
|
33
|
+
<mesh>
|
|
34
|
+
<sphereGeometry args={[LINE.floatRadius, 16, 12]} />
|
|
35
|
+
<meshStandardMaterial color="#ff7a1a" roughness={0.5} />
|
|
36
|
+
</mesh>
|
|
37
|
+
{last ? (
|
|
38
|
+
<group>
|
|
39
|
+
<mesh position={[0, 0.7, 0]}>
|
|
40
|
+
<cylinderGeometry args={[0.025, 0.025, 1.2, 6]} />
|
|
41
|
+
<meshStandardMaterial color="#d9d9d9" />
|
|
42
|
+
</mesh>
|
|
43
|
+
<mesh position={[0.18, 1.15, 0]}>
|
|
44
|
+
<boxGeometry args={[0.36, 0.22, 0.02]} />
|
|
45
|
+
<meshStandardMaterial color="#ffd23d" />
|
|
46
|
+
</mesh>
|
|
47
|
+
</group>
|
|
48
|
+
) : null}
|
|
49
|
+
</RigidBody>
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const UP = new THREE.Vector3(0, 1, 0);
|
|
54
|
+
|
|
55
|
+
/** The rope itself: one thin cylinder per span, from the stern through every float, laid out each frame. */
|
|
56
|
+
function Rope() {
|
|
57
|
+
const { registry } = useRound();
|
|
58
|
+
const mesh = useRef<THREE.InstancedMesh>(null);
|
|
59
|
+
const points = useRef(Array.from({ length: LINE.floats + 1 }, () => new THREE.Vector3()));
|
|
60
|
+
const scratch = useRef({ mid: new THREE.Vector3(), dir: new THREE.Vector3(), quat: new THREE.Quaternion(), scale: new THREE.Vector3(), m: new THREE.Matrix4() });
|
|
61
|
+
useFrame(() => {
|
|
62
|
+
const m = mesh.current;
|
|
63
|
+
const boat = registry.boatObject;
|
|
64
|
+
if (m === null || boat === null) return;
|
|
65
|
+
const pts = points.current;
|
|
66
|
+
const { mid, dir, quat, scale, m: matrix } = scratch.current;
|
|
67
|
+
boat.updateWorldMatrix(true, false);
|
|
68
|
+
pts[0]!.set(BOAT.tow[0], BOAT.tow[1], BOAT.tow[2]).applyMatrix4(boat.matrixWorld);
|
|
69
|
+
for (let i = 0; i < LINE.floats; i++) {
|
|
70
|
+
const o = registry.floatObjects[i];
|
|
71
|
+
if (o === null || o === undefined) return;
|
|
72
|
+
o.updateWorldMatrix(true, false);
|
|
73
|
+
o.getWorldPosition(pts[i + 1]!);
|
|
74
|
+
}
|
|
75
|
+
for (let i = 0; i < LINE.floats; i++) {
|
|
76
|
+
const a = pts[i]!;
|
|
77
|
+
const b = pts[i + 1]!;
|
|
78
|
+
dir.subVectors(b, a);
|
|
79
|
+
const len = dir.length();
|
|
80
|
+
if (len < 1e-6) continue;
|
|
81
|
+
quat.setFromUnitVectors(UP, dir.divideScalar(len));
|
|
82
|
+
mid.addVectors(a, b).multiplyScalar(0.5);
|
|
83
|
+
scale.set(1, len, 1);
|
|
84
|
+
matrix.compose(mid, quat, scale);
|
|
85
|
+
m.setMatrixAt(i, matrix);
|
|
86
|
+
}
|
|
87
|
+
m.instanceMatrix.needsUpdate = true;
|
|
88
|
+
});
|
|
89
|
+
return (
|
|
90
|
+
<instancedMesh ref={mesh} args={[undefined, undefined, LINE.floats]} frustumCulled={false}>
|
|
91
|
+
<cylinderGeometry args={[0.03, 0.03, 1, 6]} />
|
|
92
|
+
<meshStandardMaterial color="#2b2b2b" roughness={0.9} />
|
|
93
|
+
</instancedMesh>
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** The longline: floats on rope joints behind the stern, and the rope drawn through them. */
|
|
98
|
+
export function Longline() {
|
|
99
|
+
return (
|
|
100
|
+
<>
|
|
101
|
+
{Array.from({ length: LINE.floats }, (_, i) => (
|
|
102
|
+
<Float key={i} index={i} />
|
|
103
|
+
))}
|
|
104
|
+
<Rope />
|
|
105
|
+
</>
|
|
106
|
+
);
|
|
107
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { useFrame } from "@react-three/fiber";
|
|
2
|
+
import { useEffect, useMemo } from "react";
|
|
3
|
+
import { OCEAN, createOceanGeometry, createOceanMaterial } from "./ocean-material.js";
|
|
4
|
+
import { useRound } from "./round-context.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The sea. The vertex shader runs the same Gerstner sum as the wave field the hulls float on, and reads the same
|
|
8
|
+
* clock, so a hull sits in the water it is drawn in. The grid follows the boat in whole cells, so its vertices stay on
|
|
9
|
+
* a lattice of rest positions and the waves do not shimmer as it moves.
|
|
10
|
+
*/
|
|
11
|
+
export function Ocean() {
|
|
12
|
+
const { sea, clock, registry } = useRound();
|
|
13
|
+
const material = useMemo(() => createOceanMaterial(sea.field, sea.state.foam), [sea]);
|
|
14
|
+
const geometry = useMemo(() => createOceanGeometry(), []);
|
|
15
|
+
useEffect(() => () => material.dispose(), [material]);
|
|
16
|
+
useEffect(() => () => geometry.dispose(), [geometry]);
|
|
17
|
+
useFrame(() => {
|
|
18
|
+
const boat = registry.boat.current;
|
|
19
|
+
const cell = OCEAN.size / OCEAN.segments;
|
|
20
|
+
if (boat !== null) {
|
|
21
|
+
const p = boat.translation();
|
|
22
|
+
material.uniforms.uOrigin.value.set(Math.round(p.x / cell) * cell, Math.round(p.z / cell) * cell);
|
|
23
|
+
}
|
|
24
|
+
material.uniforms.uTime.value = clock.time;
|
|
25
|
+
});
|
|
26
|
+
return <mesh geometry={geometry} material={material} frustumCulled={false} />;
|
|
27
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { useEffect, useRef } from "react";
|
|
2
|
+
import { ARENA, CRATE } from "../game/constants.js";
|
|
3
|
+
import { radar } from "../game/radar.js";
|
|
4
|
+
|
|
5
|
+
const SIZE = 156;
|
|
6
|
+
const RANGE = ARENA.radius + 12;
|
|
7
|
+
|
|
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.
|
|
11
|
+
*/
|
|
12
|
+
export function Radar() {
|
|
13
|
+
const canvas = useRef<HTMLCanvasElement>(null);
|
|
14
|
+
const warning = useRef<HTMLDivElement>(null);
|
|
15
|
+
|
|
16
|
+
useEffect(() => {
|
|
17
|
+
const el = canvas.current;
|
|
18
|
+
const ctx = el?.getContext("2d") ?? null;
|
|
19
|
+
if (el === null || ctx === null) return;
|
|
20
|
+
const dpr = Math.min(window.devicePixelRatio || 1, 2);
|
|
21
|
+
el.width = SIZE * dpr;
|
|
22
|
+
el.height = SIZE * dpr;
|
|
23
|
+
ctx.scale(dpr, dpr);
|
|
24
|
+
const half = SIZE / 2;
|
|
25
|
+
const scale = (half - 4) / RANGE;
|
|
26
|
+
let frame = 0;
|
|
27
|
+
let outside = false;
|
|
28
|
+
|
|
29
|
+
const draw = (): void => {
|
|
30
|
+
frame = requestAnimationFrame(draw);
|
|
31
|
+
ctx.clearRect(0, 0, SIZE, SIZE);
|
|
32
|
+
ctx.save();
|
|
33
|
+
ctx.beginPath();
|
|
34
|
+
ctx.arc(half, half, half - 1, 0, Math.PI * 2);
|
|
35
|
+
ctx.fillStyle = "rgba(6, 36, 56, 0.62)";
|
|
36
|
+
ctx.fill();
|
|
37
|
+
ctx.clip();
|
|
38
|
+
if (radar.ready) {
|
|
39
|
+
const sin = Math.sin(radar.heading);
|
|
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];
|
|
43
|
+
const [ox, oy] = toScreen(-radar.boatX, -radar.boatZ);
|
|
44
|
+
ctx.beginPath();
|
|
45
|
+
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.stroke();
|
|
49
|
+
ctx.fillStyle = "#ff3d6e";
|
|
50
|
+
for (let i = 0; i < CRATE.count; i++) {
|
|
51
|
+
const [cx, cy] = toScreen(radar.crates[i * 2]! - radar.boatX, radar.crates[i * 2 + 1]! - radar.boatZ);
|
|
52
|
+
ctx.fillRect(cx - 2.5, cy - 2.5, 5, 5);
|
|
53
|
+
}
|
|
54
|
+
const isOut = Math.hypot(radar.boatX, radar.boatZ) > ARENA.radius;
|
|
55
|
+
if (isOut !== outside) {
|
|
56
|
+
outside = isOut;
|
|
57
|
+
if (warning.current !== null) warning.current.style.opacity = isOut ? "1" : "0";
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
ctx.restore();
|
|
61
|
+
// the boat: a small arrow pointing up
|
|
62
|
+
ctx.beginPath();
|
|
63
|
+
ctx.moveTo(half, half - 6);
|
|
64
|
+
ctx.lineTo(half + 4.5, half + 5);
|
|
65
|
+
ctx.lineTo(half, half + 2.5);
|
|
66
|
+
ctx.lineTo(half - 4.5, half + 5);
|
|
67
|
+
ctx.closePath();
|
|
68
|
+
ctx.fillStyle = "#ffffff";
|
|
69
|
+
ctx.fill();
|
|
70
|
+
ctx.beginPath();
|
|
71
|
+
ctx.arc(half, half, half - 1, 0, Math.PI * 2);
|
|
72
|
+
ctx.strokeStyle = "rgba(255, 255, 255, 0.55)";
|
|
73
|
+
ctx.lineWidth = 1.5;
|
|
74
|
+
ctx.stroke();
|
|
75
|
+
};
|
|
76
|
+
frame = requestAnimationFrame(draw);
|
|
77
|
+
return () => cancelAnimationFrame(frame);
|
|
78
|
+
}, []);
|
|
79
|
+
|
|
80
|
+
return (
|
|
81
|
+
<>
|
|
82
|
+
<div className="warning" ref={warning} aria-live="polite">
|
|
83
|
+
Out of bounds: turn back
|
|
84
|
+
</div>
|
|
85
|
+
<canvas className="radar" ref={canvas} style={{ width: SIZE, height: SIZE }} aria-label="Radar of the crates around the boat" />
|
|
86
|
+
</>
|
|
87
|
+
);
|
|
88
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { useAfterPhysicsStep, useBeforePhysicsStep } from "@react-three/rapier";
|
|
2
|
+
import { useRef } from "react";
|
|
3
|
+
import { planeHeading, type DriveInput } from "../game/boat-control.js";
|
|
4
|
+
import { CRATE, FIXED_DT } from "../game/constants.js";
|
|
5
|
+
import { radar } from "../game/radar.js";
|
|
6
|
+
import { useRound } from "./round-context.js";
|
|
7
|
+
|
|
8
|
+
const heading = { fx: 0, fz: -1, rx: 1, rz: 0 };
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Runs the rules of the round in the physics world's own step: the helm and the arena edge before it, the hauls, the
|
|
12
|
+
* lost crates and the clock after it. It renders nothing.
|
|
13
|
+
*/
|
|
14
|
+
export function Referee() {
|
|
15
|
+
const { registry, simulation, clock, helm } = useRound();
|
|
16
|
+
const input = useRef<DriveInput>({ throttle: 0, steer: 0 });
|
|
17
|
+
|
|
18
|
+
useBeforePhysicsStep(() => {
|
|
19
|
+
const fleet = registry.fleet();
|
|
20
|
+
if (fleet === null) return;
|
|
21
|
+
helm.read(input.current);
|
|
22
|
+
const wet = registry.boatController?.stats.submergedFraction ?? 0;
|
|
23
|
+
simulation.beforeStep(fleet, input.current, wet, FIXED_DT);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
useAfterPhysicsStep(() => {
|
|
27
|
+
const fleet = registry.fleet();
|
|
28
|
+
if (fleet === null) return;
|
|
29
|
+
simulation.afterStep(fleet, clock.time, FIXED_DT);
|
|
30
|
+
const p = fleet.boat.translation();
|
|
31
|
+
planeHeading(fleet.boat.rotation(), heading);
|
|
32
|
+
radar.boatX = p.x;
|
|
33
|
+
radar.boatZ = p.z;
|
|
34
|
+
radar.heading = Math.atan2(heading.fx, -heading.fz);
|
|
35
|
+
radar.crates.set(simulation.crateXZ.subarray(0, CRATE.count * 2));
|
|
36
|
+
radar.ready = true;
|
|
37
|
+
});
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { usePhysicsClock } from "@pinkaphant/kolmo/water/react";
|
|
2
|
+
import { Physics } from "@react-three/rapier";
|
|
3
|
+
import { useMemo } from "react";
|
|
4
|
+
import { FIXED_DT, GRAVITY, type SeaStateId } from "../game/constants.js";
|
|
5
|
+
import type { Helm } from "../game/input.js";
|
|
6
|
+
import { Registry } from "../game/registry.js";
|
|
7
|
+
import { createSea } from "../game/sea.js";
|
|
8
|
+
import { Simulation } from "../game/simulation.js";
|
|
9
|
+
import type { Game } from "../game/store.js";
|
|
10
|
+
import { Boat } from "./Boat.js";
|
|
11
|
+
import { CameraRig } from "./CameraRig.js";
|
|
12
|
+
import { Crates } from "./Crates.js";
|
|
13
|
+
import { Longline } from "./Longline.js";
|
|
14
|
+
import { Ocean } from "./Ocean.js";
|
|
15
|
+
import { Referee } from "./Referee.js";
|
|
16
|
+
import { Sky } from "./Sky.js";
|
|
17
|
+
import { RoundContext, type Round } from "./round-context.js";
|
|
18
|
+
|
|
19
|
+
export interface SceneProps {
|
|
20
|
+
sea: SeaStateId;
|
|
21
|
+
/** Counts up with every round; it is the seed of the crates, so a round replays the same way. */
|
|
22
|
+
runId: number;
|
|
23
|
+
game: Game;
|
|
24
|
+
helm: Helm;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function RoundScene({ sea: seaId, runId, game, helm }: SceneProps) {
|
|
28
|
+
const clock = usePhysicsClock();
|
|
29
|
+
const round = useMemo<Round>(() => {
|
|
30
|
+
const sea = createSea(seaId);
|
|
31
|
+
return { sea, clock, registry: new Registry(), simulation: new Simulation(game, sea.field, 1000 + runId), game, helm };
|
|
32
|
+
}, [seaId, runId, clock, game, helm]);
|
|
33
|
+
return (
|
|
34
|
+
<RoundContext.Provider value={round}>
|
|
35
|
+
<Sky />
|
|
36
|
+
<Ocean />
|
|
37
|
+
<Boat />
|
|
38
|
+
<Longline />
|
|
39
|
+
<Crates />
|
|
40
|
+
<Referee />
|
|
41
|
+
<CameraRig />
|
|
42
|
+
</RoundContext.Provider>
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
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.
|
|
49
|
+
*/
|
|
50
|
+
export function Scene(props: SceneProps) {
|
|
51
|
+
return (
|
|
52
|
+
<Physics gravity={[0, -GRAVITY, 0]} timeStep={FIXED_DT}>
|
|
53
|
+
<RoundScene {...props} />
|
|
54
|
+
</Physics>
|
|
55
|
+
);
|
|
56
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { useEffect, useMemo } from "react";
|
|
2
|
+
import * as THREE from "three";
|
|
3
|
+
import { PALETTE, SUN, createSkyMaterial } from "./ocean-material.js";
|
|
4
|
+
|
|
5
|
+
/** A gradient dome that never moves relative to the camera, and the lights of the scene. */
|
|
6
|
+
export function Sky() {
|
|
7
|
+
const material = useMemo(() => createSkyMaterial(), []);
|
|
8
|
+
const geometry = useMemo(() => new THREE.SphereGeometry(1, 24, 16), []);
|
|
9
|
+
useEffect(
|
|
10
|
+
() => () => {
|
|
11
|
+
material.dispose();
|
|
12
|
+
geometry.dispose();
|
|
13
|
+
},
|
|
14
|
+
[material, geometry],
|
|
15
|
+
);
|
|
16
|
+
return (
|
|
17
|
+
<>
|
|
18
|
+
<mesh geometry={geometry} material={material} frustumCulled={false} renderOrder={-1} />
|
|
19
|
+
<hemisphereLight args={[PALETTE.zenith, PALETTE.deep, 1.1]} />
|
|
20
|
+
<directionalLight position={[SUN.x * 60, SUN.y * 60, SUN.z * 60]} intensity={2.2} color="#fff1d6" />
|
|
21
|
+
</>
|
|
22
|
+
);
|
|
23
|
+
}
|