Public: anyone can read this; the crew can change it.

Migrated from github.com/amirmushichge/pixel-plays

Add Rock Pool: the playable page and its place in the catalogue, on the rules in `rock-pool/pool.js` #253

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

vesper/pixel-plays:vesper/199-add-rock-pool-the-playable-page-and-its → main 5 commits fork

  • Merged. vesper/199-add-rock-pool-the-playable-page-and-its was merged into main as dca7485.

Review

Sign in to review this change.
8 files changed +357 −2
README.md +3 −2

Diff not shown — a line 2,525 characters wide (limit 2,000). +3 −2

View file
index.html +1
@@ -124,6 +124,7 @@ footer a::after { content: none; }
124124 <li data-kind="Catching" data-input="Tap" style="--accent:#5f86a3"><a href="rain-barrel/">Rain Barrel</a><span>Slide the barrel under each spout before the yard floods</span><span>Game forged by the crew</span><span class="tags">Catching · Tap</span></li>
125125 <li data-kind="Puzzle" data-input="Tap" style="--accent:#b5656b"><a href="quilt/">Patchwork Quilt</a><span>Turn and swap the patches until every stitch lines up</span><span>Game forged by the crew</span><span class="tags">Puzzle · Tap</span></li>
126126 <li data-kind="Puzzle" data-input="Tap" style="--accent:#3d6f8f"><a href="mill-race/">Mill Race</a><span>Turn the channels until the stream reaches the mill wheel</span><span>Game forged by the crew</span><span class="tags">Puzzle · Tap</span></li>
127 <li data-kind="Catching" data-input="Tap" style="--accent:#3a8fa3"><a href="rock-pool/">Rock Pool</a><span>Scoop the darting crabs into your bucket before the tide comes back</span><span>Game forged by the crew</span><span class="tags">Catching · Tap</span></li>
127128 </ul>
128129 <footer>Concept by <a href="https://x.com/awwbhi2">abhi</a>. Game by <a href="https://x.com/AmirMushich">AmirMushich</a>. Visual reference for the style: shawn.'s <a href="https://x.com/zzzzshawn/status/2111546123546661289">Small plays</a>; no assets or subjects were copied.<br><a href="stats/">Most played</a></footer>
129130 </main>
og/home.png
Binary file not shown.
og/rock-pool.png added
Binary file not shown.
rock-pool/index.html added +340
@@ -0,0 +1,340 @@
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,viewport-fit=cover">
6 <title>Rock Pool</title>
7 <!-- og:begin -->
8 <meta name="description" content="Scoop the darting crabs into your bucket before the tide comes back">
9 <link rel="canonical" href="https://pixel-plays--forge-trevato-apps.open-platform.sh/rock-pool/">
10 <meta property="og:type" content="website">
11 <meta property="og:site_name" content="Pixel Plays">
12 <meta property="og:title" content="Rock Pool">
13 <meta property="og:description" content="Scoop the darting crabs into your bucket before the tide comes back">
14 <meta property="og:url" content="https://pixel-plays--forge-trevato-apps.open-platform.sh/rock-pool/">
15 <meta property="og:image" content="https://pixel-plays--forge-trevato-apps.open-platform.sh/og/rock-pool.png">
16 <meta property="og:image:type" content="image/png">
17 <meta property="og:image:width" content="1200">
18 <meta property="og:image:height" content="630">
19 <meta property="og:image:alt" content="Rock Pool: the opening scene of the game">
20 <meta name="twitter:card" content="summary_large_image">
21 <!-- og:end -->
22 <style>
23 :root { --ink: #1c1b19; --muted: #6b675f; --paper: #fff; --accent: #3a8fa3; }
24 * { box-sizing: border-box; }
25 html, body { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent; }
26 html, body { height: 100%; margin: 0; background: var(--paper); color: var(--ink); }
27 body { font: 13px/1.35 ui-monospace, "Cascadia Mono", Consolas, Menlo, monospace; overflow: hidden; }
28 #stage { position: fixed; inset: 0; display: grid; place-items: center; }
29 canvas { display: block; image-rendering: pixelated; touch-action: none; cursor: pointer; outline: none; }
30 .hud { position: fixed; left: max(16px, env(safe-area-inset-left)); right: max(16px, env(safe-area-inset-right)); display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; pointer-events: none; }
31 #scores { position: fixed; top: max(14px, env(safe-area-inset-top)); left: max(16px, env(safe-area-inset-left)); display: grid; gap: 2px; pointer-events: none; }
32 #bottom { bottom: max(20px, env(safe-area-inset-bottom)); }
33 .row { display: flex; align-items: center; gap: 8px; color: var(--accent); }
34 .row b { font-size: 16px; font-weight: 600; min-width: 1ch; font-variant-numeric: tabular-nums; }
35 .row span { color: var(--muted); }
36 #status { color: var(--muted); min-height: 1.35em; }
37 button { pointer-events: auto; font: inherit; color: var(--muted); background: none; border: 0; padding: 13px 10px; margin: -13px -10px; min-height: 44px; cursor: pointer; }
38 button:hover, button:focus-visible { color: var(--ink); }
39 button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
40 noscript p { position: fixed; inset: 40% 16px auto; margin: 0; text-align: center; color: var(--muted); }
41 </style>
42 </head>
43 <body>
44 <div id="stage"><canvas id="c" tabindex="0" aria-label="A tide pool seen from above and to one side: sixteen rocks in four rows, crabs that dart from rock to rock, and a bucket on the sand. Tap a crab while it is in the open, marked by a raised claw, to scoop it into the bucket; a miss makes you wait a second. Arrow keys move, Space scoops. Catch crabs before the tide returns in 30 seconds, the bucket holds six."></canvas></div>
45 <noscript><p>Rock Pool needs JavaScript to play.</p></noscript>
46 <div id="scores">
47 <div class="row"><b id="tide">30</b><span>seconds of tide left</span></div>
48 <div class="row"><b id="caught">0</b><span id="of">of 6 crabs caught</span></div>
49 <div class="row"><b id="score">0</b><span>points</span></div>
50 </div>
51 <div class="hud" id="bottom">
52 <div id="status" role="status" aria-live="polite"></div>
53 <button id="mute" type="button" aria-pressed="false">sound on</button>
54 </div>
55 <script src="../shell.js"></script>
56 <script>
57 PixelPlays.mount({
58 id: 'rock-pool',
59 rules: [
60 'Crabs dart between the rocks; one in the open shows a raised claw.',
61 'Tap it to scoop it into the bucket: 10 points, 15 in the first third of its dash.',
62 'A miss makes you wait a second; the bucket holds six. Score 80 before the tide returns.',
63 ],
64 });
65 </script>
66 <script>window.PP_SCENE = { originY: 4, shadowX: 1, shadowY: -.12 };</script>
67 <script src="../engine.js"></script>
68 <script src="pool.js"></script>
69 <script>
70 'use strict';
71
72 /* ---------- the pool: sand, sixteen rocks, a bucket, water at the edge ---------- */
73 const CELL = 3.2, X0 = -6.4, COLS = RockPool.COLS, ROWS = RockPool.ROWS, REACH = 10.8;
74 const BX = 8.4, BY = 0, BUCKET_TOP = 1.9, ROCK_Z = .55, ROCK_R = 1.15, SCOOP_Z = .8;
75 const SAND = [[222, 204, 160], [212, 194, 150]], WET = [[170, 152, 118], [162, 146, 112]];
76 const WATER = [[58, 143, 163], [50, 130, 152]];
77 const ROCKS = [[128, 132, 136], [112, 118, 124], [142, 136, 128]];
78 const PAIL = [226, 184, 70], SHELL = [214, 84, 60], CLAW = [236, 112, 70], EYE = [250, 246, 236];
79 const INK_PIX = rgb(28, 27, 25), WHITE_PIX = rgb(255, 255, 255), SIGNAL_PIX = rgb(255, 214, 10), GLINT_PIX = rgb(214, 240, 246);
80 const waterAt = [];
81
82 function floorColor(x, y, ix, iy, i) {
83 const rough = Math.pow(Math.pow(Math.abs(x) / REACH, 6) + Math.pow(Math.abs(y) / REACH, 6), 1 / 6) + (noise(x * 1.1, y * 1.1) - .5) * .05;
84 if (rough > 1) return 0;
85 if (rough > .9) { waterAt.push(i); const c = WATER[(ix + iy >> 1) & 1]; return rgb(c[0], c[1], c[2]); }
86 const t = .97 + (hash(ix, iy) - .5) * .08;
87 const c = rough > .8 ? WET[(ix + iy) & 1] : SAND[(ix + (iy >> 1)) & 1];
88 return rgb(c[0] * t, c[1] * t, c[2] * t);
89 }
90 const shade = (x, y, z, r) => castShadow(x, y, z, r, 1, 0);
91 const centre = cell => [X0 + (cell % COLS + .5) * CELL, X0 + (Math.floor(cell / COLS) + .5) * CELL];
92 function bakeRocks() {
93 for (let cell = 0; cell < RockPool.CELLS; cell++) {
94 const [x, y] = centre(cell), h = hash(cell, 3);
95 sphere(x, y, ROCK_Z, ROCK_R, ROCKS[cell % 3], 'rough');
96 sphere(x + .55, y - .35, ROCK_Z + .05, .75 + h * .15, ROCKS[(cell + 1) % 3], 'rough');
97 shade(x, y, 1.4, 1.1); shade(x + .55, y - .35, 1.1, .75);
98 }
99 }
100 function bakeBucket() {
101 box(BX - .8, BY - .8, 0, BX + .8, BY + .8, BUCKET_TOP, PAIL, 5);
102 for (let y = BY - .8; y <= BY + .8; y += .4) { shade(BX + .8, y, BUCKET_TOP, .5); shade(BX + .8, y, 1, .5); }
103 }
104 function bake() {
105 ground.fill(0); shadowMask.fill(0); staticShadow.fill(0); kinds.fill(0);
106 tufts.length = 0; leaves.length = 0; waterAt.length = 0;
107 zb.fill(-1e9);
108 pix.fill(WHITE_PIX);
109 for (let iy = 0; iy < H; iy++) for (let ix = 0; ix < W; ix++) {
110 const g = unproject(ix + .5, iy + .5), i = iy * W + ix, c = floorColor(g.x, g.y, ix, iy, i);
111 if (!c) continue;
112 pix[i] = c;
113 zb[i] = (g.x + g.y) / R2 * COS;
114 if (waterAt[waterAt.length - 1] !== i) ground[i] = 1; // water takes no shadow
115 }
116 groundZ.set(zb);
117 bakeRocks(); bakeBucket();
118 bakeOcclusion();
119 staticShadow.set(shadowMask);
120 applyShadows(groundZ);
121 bakedPix.set(pix);
122 bakedZ.set(zb);
123 }
124
125 /* ---------- the water: the only thing that moves by itself ---------- */
126 function drawWater(t) {
127 if (STILL) return;
128 const f = Math.floor(t * WIND_FPS);
129 for (const i of waterAt) if (hash((i % W) * .7 + f, (i / W | 0) + f * .3) > .965) pix[i] = GLINT_PIX;
130 }
131
132 /* ---------- state ---------- */
133 const nodes = {};
134 for (const k of ['tide', 'caught', 'of', 'score', 'status']) nodes[k] = document.getElementById(k);
135 const shown = {};
136 function setText(key, text) {
137 if (shown[key] === text) return;
138 shown[key] = text;
139 nodes[key].textContent = text;
140 }
141 const fresh = () => RockPool.start(Math.floor(Math.random() * 4294967296));
142 let P = fresh();
143 const G = { time: 0, cursor: 5, keyed: false };
144 const MIDDLE = 'Tap a crab in the open to scoop it';
145 const playing = () => P.status === 'playing';
146
147 function renderHud() {
148 setText('tide', String(Math.ceil(Math.max(0, P.tide - P.time))));
149 setText('caught', String(P.caught));
150 setText('of', `of ${P.bucket} crabs caught`);
151 setText('score', String(P.score));
152 }
153 function say(text) { setText('status', P.status === 'over' ? `${P.line} · tap or press a key to play again` : text); }
154
155 /* ---------- geometry ---------- */
156 // the canvas pixel at the top of a rock
157 function cellPoint(cell) {
158 const [x, y] = centre(cell), p = project(x, y, SCOOP_Z);
159 return [p.sx, p.sy];
160 }
161 // the rock under a canvas point, or null off the rocks
162 function cellAt(px, py) {
163 const g = unproject(px, py + SCOOP_Z * COS * S);
164 const c = Math.floor((g.x - X0) / CELL), r = Math.floor((g.y - X0) / CELL);
165 return c < 0 || r < 0 || c >= COLS || r >= ROWS ? null : r * COLS + c;
166 }
167
168 /* ---------- drawing the crabs ---------- */
169 const world = (ux, uy) => [X0 + ux * CELL, X0 + uy * CELL];
170 function drawCrab(c) {
171 const [wx, wy] = world(c.x, c.y), [fx, fy] = centre(c.from), [tx, ty] = centre(c.to);
172 const len = Math.hypot(tx - fx, ty - fy) || 1, dx = (tx - fx) / len, dy = (ty - fy) / len, px = -dy, py = dx;
173 const p = (P.time - c.start) / c.duration, edge = Math.min(p, 1 - p);
174 const z = .3 + Math.max(0, 1 - edge / .15) * 1.3;
175 const at = (side, ahead, up) => [wx + px * side + dx * ahead, wy + py * side + dy * ahead, z + up];
176 dynamicAdd(at(-.38, 0, .3), at(.38, 0, .3), .36, .36, () => SHELL);
177 for (const s of [-1, 1]) {
178 for (const k of [-.25, 0, .25]) {
179 const wig = Math.sin(p * 40 + k * 9 + s) * .1;
180 dynamicAdd(at(s * .4, k + wig, .25), at(s * .85, k + wig, 0), .08, .06, () => CLAW);
181 }
182 const lead = s === 1 ? 1 : .35;
183 dynamicAdd(at(s * .42, .25, .4), at(s * .62, .75, .4 + lead * .5), .1, .1, () => CLAW);
184 dynamicAdd(at(s * .62, .75, .4 + lead * .5), at(s * .62, .75, .4 + lead * .5), .24, .24, () => CLAW);
185 dynamicAdd(at(s * .2, .3, .72), at(s * .2, .3, .72), .1, .1, () => EYE);
186 }
187 }
188 function drawBucketCrabs() {
189 for (let k = 0; k < P.caught; k++) {
190 const c = [BX - .35 + (k % 2) * .7, BY - .5 + Math.floor(k / 2) * .5, BUCKET_TOP + .2];
191 dynamicAdd(c, c, .3, .3, () => SHELL);
192 }
193 }
194 // a raised claw over a crab in the open, so it is marked by shape as well as colour
195 const CLAW_MARK = [
196 'oo.....oo',
197 'oXo...oXo',
198 'oXXo.oXXo',
199 '.oXXoXXo.',
200 '..oXXXo..',
201 '...ooo...',
202 ];
203 function drawClaw(c) {
204 const [wx, wy] = world(c.x, c.y), p = (P.time - c.start) / c.duration, edge = Math.min(p, 1 - p);
205 const top = project(wx, wy, .3 + Math.max(0, 1 - edge / .15) * 1.3 + 1.7);
206 const left = Math.round(top.sx) - 4, up = Math.round(top.sy) - CLAW_MARK.length;
207 const ink = [], fill = [];
208 CLAW_MARK.forEach((row, y) => [...row].forEach((cell, x) => {
209 if (cell === 'o') ink.push([left + x, up + y]);
210 if (cell === 'X') fill.push([left + x, up + y]);
211 }));
212 haloDots(ink);
213 drawDots(ink, INK_PIX);
214 drawDots(fill, SIGNAL_PIX);
215 }
216 function put(ix, iy, color) {
217 if (ix >= 0 && iy >= 0 && ix < W && iy < H) pix[iy * W + ix] = color;
218 }
219 function line(ax, ay, bx, by, color) {
220 const n = Math.max(1, Math.ceil(Math.hypot(bx - ax, by - ay) / .05));
221 for (let k = 0; k <= n; k++) { const p = project(ax + (bx - ax) * k / n, ay + (by - ay) * k / n, 0); put(p.sx | 0, p.sy | 0, color); }
222 }
223 function outline(cell, inset, color) {
224 const [x, y] = centre(cell), h = CELL / 2 - inset;
225 for (const [a, b, c, d] of [[-h, -h, h, -h], [h, -h, h, h], [h, h, -h, h], [-h, h, -h, -h]]) line(x + a, y + b, x + c, y + d, color);
226 }
227 function draw() {
228 pix.set(bakedPix);
229 zb.set(bakedZ);
230 pendingShadows.length = 0;
231 const open = P.crabs.filter(c => c.phase === 'open');
232 for (const c of open) drawCrab(c);
233 drawBucketCrabs();
234 flushShadows();
235 applyShadows(bakedZ);
236 shadeEdges();
237 drawWater(G.time);
238 if (G.keyed && playing()) { outline(G.cursor, .1, WHITE_PIX); outline(G.cursor, .25, INK_PIX); }
239 for (const c of open) drawClaw(c);
240 ctx.putImageData(image, 0, 0);
241 }
242
243 /* ---------- game: every rule is the rules script's ---------- */
244 function play(r) {
245 P = r.state;
246 renderHud();
247 if (!r.ok) {
248 say(r.why === 'cooldown' ? 'Wait a moment, you missed' : r.why === 'bucket-full' ? 'The bucket is full' : 'That scoop is not allowed');
249 sound(120, .08, 'square');
250 return;
251 }
252 say(P.line);
253 if (r.event === 'won') { sound(523, .12, 'triangle'); sound(659, .12, 'triangle', .12); sound(784, .3, 'triangle', .24); }
254 else if (r.event === 'lost') sound(110, .5, 'sawtooth');
255 else if (r.event === 'early') { sound(660, .08, 'sine'); sound(880, .1, 'sine', .08); }
256 else if (r.event === 'caught') sound(560, .1, 'sine');
257 else if (r.event === 'missed') sound(150, .12, 'triangle');
258 }
259 function scoop(cell) {
260 ensureAudio();
261 if (!playing()) { restart(); return; }
262 play(RockPool.scoop(P, cell));
263 }
264 function restart() {
265 P = fresh(); G.cursor = 5;
266 renderHud();
267 say(MIDDLE);
268 }
269
270 /* ---------- input ---------- */
271 const point = e => { const r = canvas.getBoundingClientRect(); return [(e.clientX - r.left) / r.width * W, (e.clientY - r.top) / r.height * H]; };
272 canvas.addEventListener('pointerdown', e => {
273 canvas.focus({ preventScroll: true });
274 const cell = cellAt(...point(e));
275 G.keyed = false;
276 if (cell === null) { ensureAudio(); if (!playing()) restart(); return; }
277 G.cursor = cell;
278 scoop(cell);
279 });
280 const STEP = { ArrowRight: [1, 0], ArrowLeft: [-1, 0], ArrowDown: [0, 1], ArrowUp: [0, -1] };
281 canvas.addEventListener('keydown', e => {
282 const step = STEP[e.key], pick = e.key === ' ' || e.key === 'Enter';
283 if (!step && !pick) return;
284 e.preventDefault();
285 if (!playing()) { if (!e.repeat) restart(); return; }
286 G.keyed = true;
287 if (step) {
288 const c = G.cursor % COLS + step[0], r = Math.floor(G.cursor / COLS) + step[1];
289 if (c >= 0 && r >= 0 && c < COLS && r < ROWS) G.cursor = r * COLS + c;
290 } else if (!e.repeat) scoop(G.cursor);
291 });
292
293 /* ---------- sound ---------- */
294 let audio = null, muted = false;
295 function ensureAudio() {
296 if (audio) { if (audio.state === 'suspended') audio.resume(); return; }
297 const AC = window.AudioContext || window.webkitAudioContext;
298 if (AC) audio = new AC();
299 }
300 function sound(freq, life, type, delay = 0) {
301 if (!audio || muted) return;
302 const now = audio.currentTime + delay, gain = audio.createGain(), osc = audio.createOscillator();
303 osc.type = type;
304 osc.frequency.setValueAtTime(freq, now);
305 gain.gain.setValueAtTime(.0001, now);
306 gain.gain.exponentialRampToValueAtTime(.07, now + .01);
307 gain.gain.exponentialRampToValueAtTime(.0005, now + life);
308 osc.connect(gain).connect(audio.destination);
309 osc.start(now); osc.stop(now + life + .02);
310 }
311 const muteButton = document.getElementById('mute');
312 muteButton.addEventListener('click', () => {
313 muted = !muted;
314 muteButton.textContent = muted ? 'sound off' : 'sound on';
315 muteButton.setAttribute('aria-pressed', String(muted));
316 });
317
318 /* ---------- fit and boot ---------- */
319 addEventListener('resize', fit);
320 fit();
321 bake();
322 say(MIDDLE);
323 renderHud();
324 canvas.focus({ preventScroll: true });
325 let last = performance.now();
326 function frame(now) {
327 const dt = Math.min(.04, (now - last) / 1000);
328 last = now;
329 G.time += dt;
330 if (playing() && dt > 0) {
331 const r = RockPool.tick(P, dt);
332 if (r.ok) { P = r.state; if (r.event) play(r); else renderHud(); }
333 }
334 draw();
335 requestAnimationFrame(frame);
336 }
337 requestAnimationFrame(frame);
338 </script>
339 </body>
340 </html>
rounds.mjs +5
@@ -356,6 +356,11 @@ export const ROUNDS = {
356356 g.step(1);
357357 };
358358 })() },
359 // tap the rock under a crab in the first third of its dash: the early bonus is what makes the 80 points
360 'rock-pool': { over: /play again/, outcome: /Tide in, you win: \d+ crabs/, beat: g => {
361 const cell = g.read("(() => { const c = P.crabs.find(c => c.phase === 'open' && (P.time - c.start) / c.duration < .3); return c && P.caught < P.bucket && P.cooldown <= 0 ? c.cell : -1; })()");
362 if (cell >= 0) g.tap(...g.read(`cellPoint(${cell})`)); else g.step(1);
363 } },
359364 // the rules' own solution, one move a beat: pick a patch and tap it again to turn it, or pick one and tap another to swap
360365 'quilt': { over: /play again/, outcome: /Quilt finished: \d+ points/, beat: g => {
361366 const at = i => g.read(`cellPoint(${i})`);
serve.test.mjs +7
@@ -593,6 +593,13 @@ test('Patchwork Quilt and its rules script are served without a track', async ()
593593 expect((await fetch(base + '/quilt/patch.js')).status).toBe(200);
594594 });
595595
596 test('Rock Pool and its rules script are served without a track', async () => {
597 const page = await fetch(base + '/rock-pool/');
598 expect(page.status).toBe(200);
599 expect(page.headers.get('content-type')).toBe('text/html; charset=utf-8');
600 expect((await fetch(base + '/rock-pool/pool.js')).status).toBe(200);
601 });
602
596603 test('Hot Air Balloon and its rules script are served without a track', async () => {
597604 const page = await fetch(base + '/hot-air-balloon/');
598605 expect(page.status).toBe(200);
shell.js +1
@@ -64,6 +64,7 @@ window.PixelPlays = (() => {
6464 { id: 'rain-barrel', title: 'Rain Barrel', blurb: 'Slide the barrel under each spout before the yard floods', kind: 'Catching', input: 'Tap', accent: '#5f86a3', music: false, forged: true },
6565 { id: 'quilt', title: 'Patchwork Quilt', blurb: 'Turn and swap the patches until every stitch lines up', kind: 'Puzzle', input: 'Tap', accent: '#b5656b', music: false, forged: true },
6666 { id: 'mill-race', title: 'Mill Race', blurb: 'Turn the channels until the stream reaches the mill wheel', kind: 'Puzzle', input: 'Tap', accent: '#3d6f8f', music: false, forged: true },
67 { id: 'rock-pool', title: 'Rock Pool', blurb: 'Scoop the darting crabs into your bucket before the tide comes back', kind: 'Catching', input: 'Tap', accent: '#3a8fa3', music: false, forged: true },
6768 ];
6869 const MUSIC_VOLUME = .4;
6970 const CREDITS = [

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs