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

Migrated from github.com/amirmushichge/pixel-plays

Add Pizza Toss: spin the dough in the air and catch it without tearing #20

Merged opened this

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

Merged

quill/pixel-plays:quill/11-add-pizza-toss-spin-the-dough-in-the-air → main 3 commits fork 4 threads · 4 unresolved

  • Merged. quill/11-add-pizza-toss-spin-the-dough-in-the-air was merged into main as 9a548b9.

Review

Sign in to review this change.
7 files changed +468 −1
README.md +2 −1
@@ -17,6 +17,7 @@ Concept by [abhi](https://x.com/awwbhi2). Game by [AmirMushich](https://x.com/Am
1717 | [Mini Ramp](mini-ramp/) | 60 seconds on a skateboard ramp, pump for height and grab in the air | Hold and release; space |
1818 | [Kite Hill](kite-hill/) | Keep a kite in the air through 60 seconds of gusts: pull to climb, let out line when the grass bends (forged by the crew on the engine and shell above, not by the original creators) | Hold; space |
1919 | [Stone Skip](stone-skip/) | Skim a flat stone across a lake, five throws, count the bounces (forged by the crew on the engine and shell above, not by the original creators) | Drag back and release; arrows + space |
20 | [Pizza Toss](pizza-toss/) | Five tosses of pizza dough: hold to spin, catch it before it drops, too much spin tears it (forged by the crew on the engine and shell above, not by the original creators) | Hold and release, tap to catch; space |
2021 | [Harbour Crane](harbour-crane/) | Stack ten crates onto a bobbing fishing boat from a swinging crane hook; one hanging more than half off tips into the water (forged by the crew on the engine and shell above, not by the original creators) | Tap; space |
2122
2223 ## Run
@@ -31,7 +32,7 @@ Then open http://127.0.0.1:5300/. `PORT` and `HOST` override the port and addres
3132 On the forge, `forge.json` runs `bun serve.mjs` with health check `/`. The app keeps no state, so it does not need `FORGE_APP_DATA`. `bun test` checks that the server answers every game page and track.
3233
3334 ## Add a game
34 Create `<id>/index.html`, load `../shell.js`, call `PixelPlays.mount({ id, rules })`, set `window.PP_SCENE`, load `../engine.js`, then add the game to `GAMES` in `shell.js` with a one-line `blurb`; the home page lists every `GAMES` entry (a static fallback list for no-JS is kept in sync by `serve.test.mjs`, so add the matching `<li>` to `index.html` too). The page needs a `#mute` button. Put its background track at `<id>/<id>.mp3`, or set `music: false` on its `GAMES` entry when it has none (Stone Skip, Kite Hill, Paper Route and Harbour Crane play on their own sound effects). Rules that need testing go in a plain script with a `.test.mjs` beside it, as in `kite-hill/model.js`.
35 Create `<id>/index.html`, load `../shell.js`, call `PixelPlays.mount({ id, rules })`, set `window.PP_SCENE`, load `../engine.js`, then add the game to `GAMES` in `shell.js` with a one-line `blurb`; the home page lists every `GAMES` entry (a static fallback list for no-JS is kept in sync by `serve.test.mjs`, so add the matching `<li>` to `index.html` too). The page needs a `#mute` button. Put its background track at `<id>/<id>.mp3`, or set `music: false` on its `GAMES` entry when it has none (Stone Skip, Kite Hill, Paper Route, Harbour Crane and Pizza Toss play on their own sound effects). Rules that need testing go in a plain script with a `.test.mjs` beside it, as in `kite-hill/model.js`.
3536
3637 - `shell.js` provides the game menu, the Rules popup, the credits and the background music.
3738 - `engine.js` provides the camera, depth buffer, colour ramps, shadows, edges, figures, trees and wind as globals.
index.html +1
@@ -32,6 +32,7 @@ footer a { display: inline; padding: 0; font-weight: inherit; text-decoration: u
3232 <li><a href="kite-hill/">Kite Hill</a><span>Keep a kite in the air through 60 seconds of gusts: pull to climb, let out line when the grass bends</span><span>Game forged by the crew</span></li>
3333 <li><a href="stone-skip/">Stone Skip</a><span>Skim a flat stone across a lake, five throws, count the bounces</span><span>Game forged by the crew</span></li>
3434 <li><a href="paper-route/">Paper Route</a><span>Ten newspapers thrown from a passing bike: porch 3, lawn 1, a window costs 2</span><span>Game forged by the crew</span></li>
35 <li><a href="pizza-toss/">Pizza Toss</a><span>Five tosses of pizza dough: hold to spin, catch it before it drops, too much spin tears it</span><span>Game forged by the crew</span></li>
3536 <li><a href="harbour-crane/">Harbour Crane</a><span>Stack ten crates onto a bobbing fishing boat from a swinging crane hook</span><span>Game forged by the crew</span></li>
3637 </ul>
3738 <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.</footer>
pizza-toss/index.html added +359
@@ -0,0 +1,359 @@
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>Pizza Toss</title>
7 <style>
8 :root { --ink: #1c1b19; --muted: #8b877f; --paper: #fff; --accent: #b4452f; }
9 * { box-sizing: border-box; }
10 html, body { height: 100%; margin: 0; background: var(--paper); color: var(--ink); }
11 body { font: 13px/1.35 ui-monospace, "Cascadia Mono", Consolas, Menlo, monospace; overflow: hidden; }
12 #stage { position: fixed; inset: 0; display: grid; place-items: center; }
13 canvas { display: block; image-rendering: pixelated; touch-action: none; cursor: pointer; outline: none; }
14 .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; gap: 16px; pointer-events: none; }
15 #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; }
16 #bottom { bottom: max(14px, env(safe-area-inset-bottom)); align-items: flex-end; }
17 .row { display: flex; align-items: center; gap: 8px; color: var(--accent); }
18 .row b { font-size: 16px; font-weight: 600; min-width: 1ch; font-variant-numeric: tabular-nums; }
19 .row span { color: var(--muted); }
20 .dots { display: flex; gap: 3px; }
21 .dots i { width: 6px; height: 6px; background: var(--accent); }
22 .dots i.off { opacity: .16; }
23 #status { color: var(--muted); }
24 button { pointer-events: auto; font: inherit; color: var(--muted); background: none; border: 0; padding: 10px; margin: -10px; cursor: pointer; }
25 button:hover, button:focus-visible { color: var(--ink); }
26 </style>
27 </head>
28 <body>
29 <div id="stage"><canvas id="c" tabindex="0" aria-label="Street pizzeria counter and oven. Hold to wind up spin, release to toss, tap again as the dough falls to catch it. Keyboard: hold space, release to toss, press space to catch."></canvas></div>
30 <div id="scores">
31 <div class="row"><b id="size">24</b><span>px across</span></div>
32 <div class="row"><b id="best">0</b><span>best</span><span class="dots" id="tosses"></span></div>
33 </div>
34 <div class="hud" id="bottom">
35 <div id="status" role="status" aria-live="polite"></div>
36 <button id="mute" type="button" aria-pressed="false">sound on</button>
37 </div>
38 <script src="../shell.js"></script>
39 <script>
40 PixelPlays.mount({
41 id: 'pizza-toss',
42 rules: [
43 'Five tosses, then the pizza goes in the oven. Its width in pixels is your score.',
44 'Hold to wind up spin, release to toss, tap again as the dough falls to catch it. Keyboard: hold space, release, press space.',
45 'Early or late drops it. Too little spin and it stays small; too much and it tears.',
46 ],
47 });
48 </script>
49 <script>window.PP_SCENE = { originY: 16, shadowX: 1.3, shadowY: -.12 };</script>
50 <script src="../engine.js"></script>
51 <script src="toss.js"></script>
52 <script>
53 'use strict';
54
55 const TOSS = window.PizzaToss;
56 const CHEF_Y = 0, HAND_Z = 1.7, OVEN_X = -6, MOUTH_X = OVEN_X + .3;
57 const KIND_TILE = 6;
58 const TILE_A = [206, 190, 170], TILE_B = [190, 172, 152], COUNTER = [150, 96, 66], TOP = [220, 214, 200];
59 const BRICK = [176, 84, 62], DARK = [34, 26, 28], DOUGH = [238, 214, 168], SAUCE = [196, 62, 44];
60 const CHEF = { shirt: [250, 248, 240], stripe: [236, 232, 220], legs: [52, 62, 82], hair: [60, 42, 30], skin: [222, 170, 132] };
61 const GLOW = rgb(255, 150, 48);
62 const doughRamp = rampOf(DOUGH), sauceRamp = rampOf(SAUCE);
63
64 function groundColor(x, y, ix, iy, i) {
65 const edge = Math.pow(Math.pow(Math.abs(x) / 16.2, 4) + Math.pow(Math.abs(y) / 7.4, 4), .25);
66 const rough = edge + (noise(x * 1.3, y * 1.3) - .5) * .13;
67 if (rough > 1.04) return 0;
68 if (rough > .93 && hash(ix, iy) < (rough - .93) / .11) return 0;
69 const fine = hash(ix, iy);
70 if (rough > .8 + (noise(x * .5, y * .5) - .5) * .1) {
71 kinds[i] = KIND_GRASS;
72 const t = noise(x * .9, y * .9) * 1.15 + (fine - .5) * .55 - .08;
73 return rgb(...GRASS[clamp(Math.floor(t * 4.2), 0, 3)]);
74 }
75 kinds[i] = KIND_TILE;
76 const tile = (Math.floor(x / 1.6) + Math.floor(y / 1.6)) & 1 ? TILE_A : TILE_B;
77 const ramp = rampOf(tile);
78 return ramp[clamp(Math.floor(2.4 + (noise(x * 2, y * 2) - .5) * 1.6 + (bayerAt(ix, iy) - .5) * .5), 1, 3)];
79 }
80
81 function bake() {
82 zb.fill(-1e9);
83 pix.fill(rgb(255, 255, 255));
84 for (let iy = 0; iy < H; iy++) for (let ix = 0; ix < W; ix++) {
85 const g = unproject(ix + .5, iy + .5), i = iy * W + ix, c = groundColor(g.x, g.y, ix, iy, i);
86 if (!c) continue;
87 pix[i] = c; ground[i] = 1;
88 zb[i] = (g.x + g.y) / R2 * COS;
89 }
90 growGrass();
91 groundZ.set(zb);
92
93 // the counter behind the chef, with a flour sack and a stack of boxes on it
94 box(-4.4, -3.8, 0, 4.4, -2.7, 1.4, COUNTER, 3);
95 box(-4.6, -3.9, 1.4, 4.6, -2.6, 1.55, TOP, 5);
96 box(2.2, -3.5, 1.55, 3.4, -2.9, 2.4, [236, 226, 200], 7);
97 box(.6, -3.4, 1.55, 1.8, -2.9, 2.0, [200, 150, 96], 9);
98 box(-3.4, -3.5, 1.55, -2.6, -3.0, 1.75, [120, 130, 140], 11);
99 // the brick oven on the left; its mouth faces the chef
100 box(OVEN_X - 3, -2.4, 0, OVEN_X, 2.4, 2.4, BRICK, 2);
101 box(OVEN_X - 2.6, -2.0, 2.4, OVEN_X - .2, 2.0, 3.1, BRICK, 4);
102 sphere(OVEN_X - 1.4, 0, 3.1, 1.1, BRICK, 'rough');
103 box(OVEN_X - 2.6, -.4, 3.1, OVEN_X - 2.0, .2, 5.0, [96, 78, 74], 6);
104 box(OVEN_X, -.95, .35, MOUTH_X, .95, 1.75, DARK, 8);
105 castShadow(OVEN_X - 2.6, 0, 5, .4);
106 castShadow(3, -3.3, 2.2, .7); castShadow(-1, -3.3, 2.2, .9);
107
108 growTree(11.5, 4.4, 1, 420, 11);
109 bakeOcclusion();
110 staticShadow.set(shadowMask);
111 applyShadows(groundZ);
112 bakedPix.set(pix);
113 bakedZ.set(zb);
114 }
115
116 /* ---------- game state ---------- */
117 const BEST_KEY = 'pizza-toss-best';
118 function loadBest() { try { return Math.max(0, parseInt(localStorage.getItem(BEST_KEY), 10) || 0); } catch (e) { return 0; } }
119 function saveBest(v) { try { localStorage.setItem(BEST_KEY, String(v)); } catch (e) { /* private mode: the best lives for the session */ } }
120 const G = {
121 phase: 'ready', inHand: false, held: 0, spin: 0, tossT: 0, tapAt: null, timer: 0, time: 0, angle: 0,
122 tossesLeft: TOSS.TOSSES, size: TOSS.START_SIZE, best: loadBest(), outcome: '', status: '', fallZ: 0,
123 };
124 const nodes = {
125 size: document.getElementById('size'), best: document.getElementById('best'),
126 tosses: document.getElementById('tosses'), status: document.getElementById('status'),
127 };
128 function renderHud() {
129 nodes.size.textContent = G.size;
130 nodes.best.textContent = G.best;
131 if (nodes.tosses.children.length !== TOSS.TOSSES) nodes.tosses.innerHTML = '<i></i>'.repeat(TOSS.TOSSES);
132 [...nodes.tosses.children].forEach((el, k) => el.classList.toggle('off', k >= G.tossesLeft));
133 }
134 function setStatus(text) {
135 if (text === G.status) return;
136 G.status = text;
137 nodes.status.textContent = text;
138 }
139 function nextToss() {
140 G.phase = 'ready'; G.held = 0; G.spin = 0; G.timer = 0; G.outcome = '';
141 setStatus('hold to wind up spin, release to toss');
142 renderHud();
143 }
144 function resetGame() {
145 G.tossesLeft = TOSS.TOSSES; G.size = TOSS.START_SIZE;
146 nextToss();
147 }
148 function startWind() {
149 ensureAudio();
150 if (G.phase === 'done' && G.timer > .8) return resetGame();
151 if (G.phase === 'result' && G.timer > .5) return G.tossesLeft ? nextToss() : startOven();
152 if (G.phase !== 'ready') return;
153 G.phase = 'wind'; G.held = 0;
154 setStatus('');
155 }
156 function release() {
157 if (G.phase !== 'wind') return;
158 G.spin = TOSS.spinFor(G.held);
159 G.phase = 'air'; G.tossT = 0; G.tapAt = null; G.tossesLeft--;
160 sound('whoosh', G.spin);
161 renderHud();
162 }
163 function tap() {
164 ensureAudio();
165 if (G.phase === 'air' && G.tapAt === null) { G.tapAt = G.tossT; finishToss(); }
166 }
167 function finishToss() {
168 const outcome = TOSS.judge(G.spin, G.tapAt);
169 G.outcome = outcome; G.inHand = TOSS.timing(G.spin, G.tapAt) === 'ok'; G.phase = 'result'; G.timer = 0;
170 G.fallZ = TOSS.riseAt(G.spin, G.tapAt === null ? 0 : G.tapAt);
171 G.size = TOSS.grow(G.size, G.spin, outcome);
172 const last = G.tossesLeft === 0;
173 const what = {
174 caught: G.spin < .3 ? 'caught, but barely any spin' : 'caught',
175 torn: 'too much spin, it tears',
176 early: 'too early, it hits the floor',
177 late: 'too late, it hits the floor',
178 }[outcome];
179 sound(outcome === 'caught' ? 'catch' : outcome === 'torn' ? 'rip' : 'drop');
180 setStatus(`${what} · ${G.size} px${last ? ' · tap for the oven' : ' · tap to toss again'}`);
181 renderHud();
182 }
183 function startOven() {
184 G.phase = 'oven'; G.timer = 0;
185 setStatus('');
186 sound('whoosh', .2);
187 }
188 function finishGame() {
189 G.phase = 'done'; G.timer = 0;
190 const record = G.size > G.best;
191 if (record) { G.best = G.size; saveBest(G.best); sound('point'); }
192 setStatus(`${G.size} px across${record ? ', a new best' : ''} · best ${G.best} · tap to play again`);
193 renderHud();
194 }
195
196 function update(dt) {
197 G.time += dt;
198 if (G.phase === 'wind') G.held = Math.min(TOSS.WIND_TIME, G.held + dt);
199 else if (G.phase === 'air') {
200 G.tossT += dt;
201 G.angle += (4 + G.spin * 20) * dt;
202 if (G.tapAt === null && G.tossT > TOSS.flightTime(G.spin) + TOSS.CATCH_WINDOW) finishToss();
203 } else if (G.phase === 'result' || G.phase === 'done') G.timer += dt;
204 else if (G.phase === 'oven') { G.timer += dt; if (G.timer > 1.3) finishGame(); }
205 }
206
207 /* ---------- drawing ---------- */
208 function poseFor() {
209 const breathe = Math.sin(G.time * 1.8) * .012;
210 if (G.phase === 'wind') {
211 const s = TOSS.spinFor(G.held);
212 return { crouch: .1 + s * .5, lean: .1 + s * .15, stance: .22, armR: .9 + s * .4, armL: .9 + s * .4 };
213 }
214 if (G.phase === 'air') {
215 const stretch = clamp(G.tossT * 5, 0, 1);
216 return { crouch: lerp(.3, 0, stretch), lean: 0, stance: .2, armR: lerp(1.2, 3, stretch), armL: lerp(1.2, 3, stretch) };
217 }
218 if (G.phase === 'result') return { crouch: .15, lean: .05, stance: .2, armR: 1, armL: 1 };
219 return { crouch: breathe, lean: breathe, stance: .16, armR: .5, armL: .5 + breathe };
220 }
221 // the dough as a flat disc seen at 30 degrees: an ellipse that opens and closes as it tumbles
222 function drawDough(cx, cy, cz, size, angle, tear, clipX) {
223 const p = project(cx, cy, cz), rx = size / 2, open = .5 * (.3 + .7 * Math.abs(Math.cos(angle))), ry = Math.max(1.5, rx * open);
224 const tilt = Math.sin(angle) * rx * .12;
225 const y0 = Math.max(0, Math.floor(p.sy - ry - 2)), y1 = Math.min(H - 1, Math.ceil(p.sy + ry + 2));
226 const x0 = Math.max(0, Math.floor(p.sx - rx - 2)), x1 = Math.min(W - 1, Math.ceil(p.sx + rx + 2));
227 const depth = p.d + 8;
228 for (let iy = y0; iy <= y1; iy++) for (let ix = x0; ix <= x1; ix++) {
229 if (clipX !== undefined && ix > clipX) continue;
230 const dx = (ix + .5 - p.sx) / rx, dy = (iy + .5 - p.sy - tilt * dx) / ry, n = dx * dx + dy * dy;
231 if (n > 1) continue;
232 if (tear && (n < .12 || (Math.abs(dx - .15) < .09 && dy < 0))) continue;
233 const i = iy * W + ix;
234 const rim = n > .8, sauce = !rim && n < .55 && size > 26;
235 const l = clamp(.62 - .35 * dx - .45 * dy + (hash(ix, iy) - .5) * .12, 0, 1);
236 pix[i] = rim ? doughRamp[l > .5 ? 3 : 2] : (sauce ? sauceRamp[clamp(Math.floor(l * 3 + 1), 1, 3)] : doughRamp[clamp(Math.floor(l * 4 + 1), 2, 4)]);
237 zb[i] = depth;
238 }
239 }
240 function doughState() {
241 if (G.phase === 'wind' || G.phase === 'ready') {
242 const s = G.phase === 'wind' ? TOSS.spinFor(G.held) : 0;
243 return { z: HAND_Z - s * .5, angle: G.phase === 'wind' ? G.time * (3 + s * 20) : 0 };
outdated

Nothing reads flat: 1. Remove it.

244 }
245 if (G.phase === 'air') return { z: HAND_Z + TOSS.riseAt(G.spin, G.tossT), angle: G.angle };
246 if (G.phase === 'result') {
247 if (G.inHand) return { z: HAND_Z, angle: 0 };
248 const t = G.timer, z = Math.max(0, HAND_Z + G.fallZ - 9.8 * t * t / 2);
249 return { z: Math.max(.1, z), angle: G.angle };
outdated

Nothing reads shown: true; draw() uses its own shown local. Remove it.

250 }
251 return { z: HAND_Z, angle: 0 };
252 }
253 function draw() {
254 pix.set(bakedPix);
255 zb.set(bakedZ);
256 pendingShadows.length = 0;
257 figure(0, CHEF_Y, Math.PI, poseFor(), CHEF, dynamicAdd);
258 // the dough in its current size; while winding it is the starting ball in the hands
259 const d = doughState();
260 const size = G.size;
261 const torn = G.outcome === 'torn' && G.phase !== 'air' || (G.phase === 'air' && G.spin > TOSS.TEAR_SPIN && G.tossT > TOSS.flightTime(G.spin) / 2);

The dough is drawn torn in the air, but it falls whole when the outcome is early/late. This is fixed once judge returns torn for every over-spun toss.

262 const spinGrow = G.phase === 'air' ? 1 + G.spin * .35 * Math.sin(Math.PI * clamp(G.tossT / TOSS.flightTime(G.spin), 0, 1)) : 1;
263 const shown = G.phase === 'oven' || G.phase === 'done' ? null : d;
264 if (shown) castShadow(0, 0, shown.z, size * spinGrow / S / 2.4, 1, 0);
265 const windT = STILL ? 0 : Math.floor(G.time * WIND_FPS) / WIND_FPS;
266 drawLeaves(windT);
267 drawTufts(windT);
268 flushShadows();
269 applyShadows(bakedZ);
270 shadeEdges();
271 if (shown) drawDough(0, 0, shown.z, size * spinGrow, shown.angle, torn);
272 else if (G.phase === 'oven') {
273 const t = clamp(G.timer / 1.1, 0, 1), x = lerp(0, OVEN_X - .5, ease(t));
274 drawDough(x, 0, lerp(HAND_Z, 1.0, ease(t)), size, 0, false, project(OVEN_X, 0, 1).sx | 0);
275 drawGlow(t);
276 } else if (G.phase === 'done') drawGlow(1);
277 ctx.putImageData(image, 0, 0);
278 }
279 // the oven mouth lights up once the pizza goes in
280 function drawGlow(t) {
281 const a = project(MOUTH_X, -.95, .35), b = project(MOUTH_X, .95, 1.75);
282 const x0 = Math.floor(Math.min(a.sx, b.sx)), x1 = Math.ceil(Math.max(a.sx, b.sx));
283 const y0 = Math.floor(b.sy - 2), y1 = Math.ceil(a.sy + 1);
284 for (let iy = y0; iy <= y1; iy++) for (let ix = x0; ix <= x1; ix++) {
285 if (ix < 0 || iy < 0 || ix >= W || iy >= H) continue;
286 const i = iy * W + ix;
287 if (pix[i] === rgb(...DARK) && bayerAt(ix, iy) < t * .8) pix[i] = GLOW;
288 }
289 }
290
291 /* ---------- input ---------- */
292 canvas.addEventListener('pointerdown', e => {
293 canvas.focus({ preventScroll: true });
294 canvas.setPointerCapture(e.pointerId);
295 if (G.phase === 'air') return tap();
296 startWind();
297 });
298 canvas.addEventListener('pointerup', release);
299 canvas.addEventListener('pointercancel', release);
300 canvas.addEventListener('keydown', e => {
301 if (e.key !== ' ' && e.key !== 'Enter') return;
302 e.preventDefault();
303 if (e.repeat) return;
304 if (G.phase === 'air') return tap();
305 startWind();
306 });
307 canvas.addEventListener('keyup', e => {
308 if (e.key === ' ' || e.key === 'Enter') release();
309 });
310 canvas.addEventListener('blur', release);
311
312 /* ---------- sound ---------- */
313 let audio = null, muted = false;
314 function ensureAudio() {
315 if (audio) { if (audio.state === 'suspended') audio.resume(); return; }
316 const AC = window.AudioContext || window.webkitAudioContext;
317 if (AC) audio = new AC();
318 }
319 function sound(kind, n = 0) {
320 if (!audio || muted) return;
321 const now = audio.currentTime, gain = audio.createGain(), osc = audio.createOscillator();
322 const spec = {
323 whoosh: [240 + n * 300, 520 + n * 500, .16, .05, 'triangle'],
324 catch: [420, 300, .08, .16, 'sine'],
325 rip: [200, 70, .2, .12, 'sawtooth'],
326 drop: [180, 60, .14, .18, 'sine'],
327 point: [660, 880, .16, .08, 'triangle'],
328 }[kind];
329 osc.type = spec[4];
330 osc.frequency.setValueAtTime(spec[0], now);
331 osc.frequency.exponentialRampToValueAtTime(spec[1], now + spec[2]);
332 gain.gain.setValueAtTime(spec[3], now);
333 gain.gain.exponentialRampToValueAtTime(.0005, now + spec[2]);
334 osc.connect(gain).connect(audio.destination);
335 osc.start(now); osc.stop(now + spec[2] + .01);
336 }
337 const muteButton = document.getElementById('mute');
338 muteButton.addEventListener('click', () => {
339 muted = !muted;
340 muteButton.textContent = muted ? 'sound off' : 'sound on';
341 muteButton.setAttribute('aria-pressed', String(muted));
342 });
343
344 /* ---------- fit and boot ---------- */
345 addEventListener('resize', fit);
346 fit();
347 bake();
348 nextToss();
349 let last = performance.now();
350 function frame(now) {
351 update(Math.min(.04, (now - last) / 1000));
352 last = now;
353 draw();
354 requestAnimationFrame(frame);
355 }
356 requestAnimationFrame(frame);
357 </script>
358 </body>
359 </html>
pizza-toss/toss.js added +41
@@ -0,0 +1,41 @@
1 'use strict';
2 // The rules of one toss: plain numbers in, plain numbers out, no DOM. The page draws what this decides.
3 (function (root) {
4 const TOSSES = 5, START_SIZE = 24, MIN_SIZE = 16; // dough width in canvas pixels
5 const WIND_TIME = 1.6; // seconds of holding for full spin
6 const TEAR_SPIN = .85; // more spin than this and the dough tears in the air
7 const MAX_GROWTH = 14; // pixels gained by a catch at the edge of tearing
8 const TEAR_LOSS = 6; // pixels lost to a tear
9 const CATCH_WINDOW = .2; // seconds either side of the landing that still count as a catch
10 const GRAVITY = 9.8;
11
12 const unit = v => (Number.isFinite(v) ? Math.min(1, Math.max(0, v)) : 0);
13 const spinFor = held => unit(held / WIND_TIME);
14 const flightTime = spin => .7 + unit(spin) * .9;
15 // how high above the hand the dough is, t seconds after the toss
16 function riseAt(spin, t) {
17 const T = flightTime(spin), h = GRAVITY * T * T / 8;
18 if (!Number.isFinite(t) || t <= 0 || t >= T) return 0;
19 return h * 4 * (t / T) * (1 - t / T);
20 }
21 // tapAt: seconds after the toss when the player tapped, or null for never
22 function timing(spin, tapAt) {
23 const T = flightTime(spin);
24 if (tapAt === null || !Number.isFinite(tapAt) || tapAt > T + CATCH_WINDOW) return 'late';

Timing is checked before spin, so an over-spun toss that is dropped returns late/early and costs nothing. Catching it costs 6 px. Return 'torn' first when unit(spin) > TEAR_SPIN, and add a test like judge(.95, null) === 'torn'.

25 return tapAt < T - CATCH_WINDOW ? 'early' : 'ok';
26 }
27 // an over-spun dough tears in the air, so the tear counts whether or not it is then caught
28 function judge(spin, tapAt) {
29 if (unit(spin) > TEAR_SPIN) return 'torn';
30 const when = timing(spin, tapAt);
31 return when === 'ok' ? 'caught' : when;
32 }
33 // the dough's width after a toss ends in `outcome`
34 function grow(size, spin, outcome) {
35 if (outcome === 'caught') return size + Math.round(MAX_GROWTH * Math.min(unit(spin), TEAR_SPIN) / TEAR_SPIN);
36 if (outcome === 'torn') return Math.max(MIN_SIZE, size - TEAR_LOSS);
37 return size;
38 }
39 const api = { TOSSES, START_SIZE, MIN_SIZE, WIND_TIME, TEAR_SPIN, MAX_GROWTH, CATCH_WINDOW, spinFor, flightTime, riseAt, timing, judge, grow };
40 if (typeof module !== 'undefined') module.exports = api; else root.PizzaToss = api;
41 })(typeof window !== 'undefined' ? window : globalThis);
pizza-toss/toss.test.mjs added +57
@@ -0,0 +1,57 @@
1 import { test, expect } from 'bun:test';
2 import T from './toss.js';
3
4 test('spin follows the hold and is clamped', () => {
5 expect(T.spinFor(0)).toBe(0);
6 expect(T.spinFor(T.WIND_TIME / 2)).toBeCloseTo(.5);
7 expect(T.spinFor(99)).toBe(1);
8 expect(T.spinFor(NaN)).toBe(0);
9 });
10
11 test('more spin rises higher and stays up longer', () => {
12 const peak = s => T.riseAt(s, T.flightTime(s) / 2);
13 expect(peak(.8)).toBeGreaterThan(peak(.2));
14 expect(T.flightTime(.8)).toBeGreaterThan(T.flightTime(.2));
15 expect(T.riseAt(.5, 0)).toBe(0);
16 expect(T.riseAt(.5, 99)).toBe(0);
17 });
18
19 test('a catch inside the window holds, early and late drop it', () => {
20 const s = .5, land = T.flightTime(s);
21 expect(T.judge(s, land)).toBe('caught');
22 expect(T.judge(s, land - T.CATCH_WINDOW + .01)).toBe('caught');
23 expect(T.judge(s, land - T.CATCH_WINDOW - .05)).toBe('early');
24 expect(T.judge(s, land + T.CATCH_WINDOW + .05)).toBe('late');
25 expect(T.judge(s, null)).toBe('late');
26 });
27
28 test('too much spin tears the dough', () => {
29 const s = .95;
30 expect(T.judge(s, T.flightTime(s))).toBe('torn');
31 });
32
33 test('an over-spun toss tears whether caught, early or dropped, so a drop never beats a catch', () => {
34 const s = .95, land = T.flightTime(s);
35 for (const tap of [null, 0, land - .5, land, land + .5]) {
36 expect(T.judge(s, tap)).toBe('torn');
37 expect(T.grow(30, s, T.judge(s, tap))).toBeLessThan(30);
38 }
39 expect(T.timing(s, null)).toBe('late');
40 expect(T.timing(s, land)).toBe('ok');
41 });
42
43 test('size grows with spin, never past a tear, and a tear shrinks it', () => {
44 const a = T.grow(30, .2, 'caught'), b = T.grow(30, .8, 'caught');
45 expect(b).toBeGreaterThan(a);
46 expect(a).toBeGreaterThan(30);
47 expect(T.grow(30, 1, 'torn')).toBeLessThan(30);
48 expect(T.grow(T.MIN_SIZE, 1, 'torn')).toBe(T.MIN_SIZE);
49 expect(T.grow(30, .5, 'early')).toBe(30);
50 expect(T.grow(30, .5, 'late')).toBe(30);
51 });
52
53 test('five perfect tosses is the best a pizza can be', () => {
54 let size = T.START_SIZE;
55 for (let k = 0; k < T.TOSSES; k++) size = T.grow(size, T.TEAR_SPIN, 'caught');
56 expect(size).toBe(T.START_SIZE + T.TOSSES * T.MAX_GROWTH);
57 });
serve.test.mjs +7
@@ -66,6 +66,13 @@ test('paper route is served without a track', async () => {
6666 expect((await fetch(base + '/paper-route/route.js')).status).toBe(200);
6767 });
6868
69 test('pizza toss is served without a track', async () => {
70 const page = await fetch(base + '/pizza-toss/');
71 expect(page.status).toBe(200);
72 expect(page.headers.get('content-type')).toBe('text/html; charset=utf-8');
73 expect((await fetch(base + '/pizza-toss/toss.js')).status).toBe(200);
74 });
75
6976 test('a game folder without its slash redirects to it', async () => {
7077 const res = await fetch(base + '/balote', { redirect: 'manual' });
7178 expect(res.status).toBe(308);
shell.js +1
@@ -11,6 +11,7 @@ window.PixelPlays = (() => {
1111 { id: 'kite-hill', title: 'Kite Hill', blurb: 'Keep a kite in the air through 60 seconds of gusts: pull to climb, let out line when the grass bends', music: false, forged: true },
1212 { id: 'stone-skip', title: 'Stone Skip', blurb: 'Skim a flat stone across a lake, five throws, count the bounces', music: false, forged: true },
1313 { id: 'paper-route', title: 'Paper Route', blurb: 'Ten newspapers thrown from a passing bike: porch 3, lawn 1, a window costs 2', music: false, forged: true },
14 { id: 'pizza-toss', title: 'Pizza Toss', blurb: 'Five tosses of pizza dough: hold to spin, catch it before it drops, too much spin tears it', music: false, forged: true },
1415 { id: 'harbour-crane', title: 'Harbour Crane', blurb: 'Stack ten crates onto a bobbing fishing boat from a swinging crane hook', music: false, forged: true },
1516 ];
1617 const MUSIC_VOLUME = .4;

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs