Public: anyone can read this; the crew can change it.
Migrated from github.com/amirmushichge/pixel-plays
Add Chestnut Roast: the playable page and its place in the catalogue, on the rules in `chestnut-roast/pan.js` #311
Check · Review · Land · Live done — Landed and live in production.
vesper/pixel-plays:vesper/280-add-chestnut-roast-the-playable-page-and → main 5 commits fork
10 files
changed +587
−5
README.md +3 −2
Diff not shown — a line 3,091 characters wide (limit 2,000). +3 −2
View filechestnut-roast/index.html added +391
| @@ -0,0 +1,391 @@ | ||
| 1 | ||
| 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>Chestnut Roast</title> | |
| 7 | <!-- og:begin --> | |
| 8 | <meta name="description" content="Catch the roasted chestnuts in your paper cone and let the burnt ones drop"> | |
| 9 | <link rel="canonical" href="https://pixel-plays--forge-trevato-apps.open-platform.sh/chestnut-roast/"> | |
| 10 | <meta property="og:type" content="website"> | |
| 11 | <meta property="og:site_name" content="Pixel Plays"> | |
| 12 | <meta property="og:title" content="Chestnut Roast"> | |
| 13 | <meta property="og:description" content="Catch the roasted chestnuts in your paper cone and let the burnt ones drop"> | |
| 14 | <meta property="og:url" content="https://pixel-plays--forge-trevato-apps.open-platform.sh/chestnut-roast/"> | |
| 15 | <meta property="og:image" content="https://pixel-plays--forge-trevato-apps.open-platform.sh/og/chestnut-roast.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="Chestnut Roast: the opening scene of the game"> | |
| 20 | <meta name="twitter:card" content="summary_large_image"> | |
| 21 | <!-- og:end --> | |
| 22 | <style> | |
| 23 | :root { : #1c1b19; : #6b675f; : #fff; : #8b4513; } | |
| 24 | * { box-sizing: border-box; } | |
| 25 | html, body { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent; height: 100%; margin: 0; background: var(); color: var(); } | |
| 26 | body { font: 13px/1.35 ui-monospace, "Cascadia Mono", Consolas, Menlo, monospace; overflow: hidden; } | |
| 27 | #stage { position: fixed; inset: 0; display: grid; place-items: center; } | |
| 28 | canvas { display: block; image-rendering: pixelated; touch-action: none; cursor: ew-resize; outline: none; } | |
| 29 | canvas:focus-visible { outline: 2px solid var(); outline-offset: -2px; } | |
| 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; 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(14px, env(safe-area-inset-bottom)); align-items: flex-end; } | |
| 33 | .row { display: flex; align-items: center; gap: 8px; color: var(); } | |
| 34 | .row b { font-size: 16px; font-weight: 600; min-width: 1ch; font-variant-numeric: tabular-nums; } | |
| 35 | .row span { color: var(); } | |
| 36 | .cones { display: inline-flex; gap: 3px; } | |
| 37 | .cones i { position: relative; width: 11px; height: 15px; background: var(); clip-path: polygon(0 0, 100% 0, 50% 100%); } | |
| 38 | .cones i.gone { background: var(); } | |
| 39 | .cones i.gone::after { content: ''; position: absolute; inset: 2px 3px 3px; background: var(); clip-path: polygon(0 0, 100% 0, 50% 100%); } | |
| 40 | #line { color: var(); min-height: 1.35em; } | |
| 41 | button { pointer-events: auto; font: inherit; color: var(); background: none; border: 0; padding: 10px; margin: -10px; cursor: pointer; } | |
| 42 | button:hover, button:focus-visible { color: var(); } | |
| 43 | </style> | |
| 44 | </head> | |
| 45 | <body> | |
| 46 | <div id="stage"><canvas id="c" tabindex="0" aria-label="A winter street with a vendor's brazier and roasting pan on the pavement and your paper cone in front of it. Chestnuts pop out of the pan in arcs: drag along the pavement, or hold the Left and Right arrow keys, to slide the cone under the roasted brown ones. A burnt chestnut has a black, cracked shell and smokes: let it fall, because catching one costs a life. Three lives; 15 roasted catches win."></canvas></div> | |
| 47 | <div id="scores"> | |
| 48 | <div class="row"><b id="score">0</b><span>points</span></div> | |
| 49 | <div class="row"><span class="cones" id="cones" aria-hidden="true"></span><b id="lives">3</b><span>lives</span></div> | |
| 50 | <div class="row"><b id="catches">0</b><span id="goal">of 15 caught</span></div> | |
| 51 | <div class="row"><b id="streak">0</b><span>streak</span></div> | |
| 52 | <div class="row"><b id="best">0</b><span>best score</span></div> | |
| 53 | </div> | |
| 54 | <div class="hud" id="bottom"> | |
| 55 | <div id="line"><span id="status" role="status" aria-live="polite"></span></div> | |
| 56 | <button id="mute" type="button" aria-pressed="false">sound on</button> | |
| 57 | </div> | |
| 58 | <script src="../shell.js"></script> | |
| 59 | <script> | |
| 60 | PixelPlays.mount({ | |
| 61 | id: 'chestnut-roast', | |
| 62 | rules: [ | |
| 63 | 'Drag along the pavement, or hold Left and Right, to move the cone.', | |
| 64 | 'Catch the roasted chestnuts: 15 win.', | |
| 65 | 'A burnt, smoking one costs a life if you catch it. Three lives.', | |
| 66 | ], | |
| 67 | }); | |
| 68 | </script> | |
| 69 | <script>window.PP_SCENE = { originY: 22 };</script> | |
| 70 | <script src="../engine.js"></script> | |
| 71 | <script src="pan.js"></script> | |
| 72 | <script> | |
| 73 | 'use strict'; | |
| 74 | ||
| 75 | const M = ChestnutRoast; | |
| 76 | const BEST_KEY = 'pixel-plays-chestnut-roast-best'; | |
| 77 | const K = 2.6, Z = 2.2; // screen units per metre along the pavement, and per metre of height | |
| 78 | const D_WALL = -3.4, D_BRAZIER = -1.5, D_CONE = .9; // depth lines: shop wall, vendor and pan, cone and landing line | |
| 79 | const SQ2 = Math.SQRT2; | |
| 80 | const world = (s, d) => [(s - 5) * K / SQ2 + d, -(s - 5) * K / SQ2 + d]; | |
| 81 | const sOf = (sx) => 5 + (sx - OX) / S / K; // pavement metres under a canvas column | |
| 82 | const groundOf = (sx, sy) => { const g = unproject(sx, sy); return { s: 5 + (g.x - g.y) / SQ2 / K, d: (g.x + g.y) / 2 }; }; | |
| 83 | const WALL = [86, 100, 126], WALL_DARK = [60, 72, 98], SNOW = [236, 240, 250], SLAB = [158, 154, 150], KERB = [196, 192, 186], ROAD = [88, 90, 98]; | |
| 84 | const AWN_RED = [190, 62, 52], AWN_CREAM = [240, 230, 208], WARM_PANE = [250, 206, 112], DOOR = [96, 62, 44]; | |
| 85 | const IRON = [58, 56, 62], COAL = [250, 120, 36], CONE_PAPER = [244, 236, 214]; | |
| 86 | const NUT = [150, 84, 40], NUT_CUT = [238, 204, 140], BURNT = [36, 30, 32], EMBER = [255, 140, 40], SMOKE = [120, 118, 126]; | |
| 87 | const COAT = [176, 52, 44], SKIN = [222, 170, 130], HAT = [52, 60, 84]; | |
| 88 | ||
| 89 | /* ---------- state ---------- */ | |
| 90 | const best0 = (() => { try { return Number(localStorage.getItem(BEST_KEY)) || 0; } catch (e) { return 0; } })(); | |
| 91 | let P = M.start(Date.now() & 0xffff); | |
| 92 | const G = { time: 0, status: '', best: best0, seen: new Map(), fx: [], shake: 0, saved: false }; | |
| 93 | const nodes = {}; | |
| 94 | for (const id of ['score', 'lives', 'catches', 'streak', 'best', 'cones', 'status']) nodes[id] = document.getElementById(id); | |
| 95 | function renderHud() { | |
| 96 | nodes.score.textContent = String(P.score); | |
| 97 | nodes.lives.textContent = String(P.lives); | |
| 98 | nodes.catches.textContent = String(P.catches); | |
| 99 | nodes.streak.textContent = String(P.streak); | |
| 100 | nodes.best.textContent = String(Math.max(G.best, P.score)); // the saved best score, beaten live | |
| 101 | const cones = nodes.cones; | |
| 102 | if (cones.children.length !== M.LIVES) { | |
| 103 | cones.innerHTML = ''; | |
| 104 | for (let i = 0; i < M.LIVES; i++) cones.appendChild(document.createElement('i')); | |
| 105 | } | |
| 106 | cones.children.forEach((c, i) => { c.className = i < P.lives ? '' : 'gone'; }); | |
| 107 | } | |
| 108 | function setStatus(text) { | |
| 109 | if (text === G.status) return; | |
| 110 | G.status = text; | |
| 111 | nodes.status.textContent = text; // the live region: one line per catch, miss, burn and the end | |
| 112 | } | |
| 113 | ||
| 114 | /* ---------- baked scene: the street ---------- */ | |
| 115 | function groundColor(ix, iy) { | |
| 116 | const { s, d } = groundOf(ix + .5, iy + .5); | |
| 117 | const u = (s - 5) * K; | |
| 118 | const edge = Math.pow(Math.pow(Math.abs(u) / 16.5, 8) + Math.pow(Math.abs(d - 1.2) / 5.6, 8), .125); | |
| 119 | const rough = edge + (noise(s * .5, d * .5) - .5) * .05; | |
| 120 | if (rough > 1.03) return 0; | |
| 121 | if (rough > .93 && hash(ix, iy) < (rough - .93) / .1) return 0; | |
| 122 | const t = .92 + hash(ix, iy) * .14; | |
| 123 | if (d > 3.1) { // the road, with slush | |
| 124 | const snow = noise(s * .6 + 9, d * .6) > .7; | |
| 125 | return snow ? rgb(SNOW[0] * t, SNOW[1] * t, SNOW[2] * t) : rgb(ROAD[0] * t, ROAD[1] * t, ROAD[2] * t); | |
| 126 | } | |
| 127 | if (d > 2.5) return rgb(KERB[0] * t, KERB[1] * t, KERB[2] * t); | |
| 128 | const slab = (Math.floor(s * 1.6) + Math.floor(d * 1.3)) & 1, line = Math.abs((s * 1.6) % 1) < .06 || Math.abs((d * 1.3) % 1) < .06; | |
| 129 | if (noise(s * .45 + 3, d * .55) > .74) return rgb(SNOW[0] * t, SNOW[1] * t, SNOW[2] * t); // snow drifts on the pavement | |
| 130 | const g = line ? .8 : slab ? 1 : 1.06; | |
| 131 | return rgb(SLAB[0] * t * g, SLAB[1] * t * g, SLAB[2] * t * g); | |
| 132 | } | |
| 133 | function paintAt(ramp, s, d, z, tone) { // one point of a surface, in the depth buffer | |
| 134 | const [x, y] = world(s, d), p = project(x, y, z), ix = p.sx | 0, iy = p.sy | 0; | |
| 135 | if (ix < 0 || iy < 0 || ix >= W || iy >= H) return; | |
| 136 | const i = iy * W + ix; | |
| 137 | if (p.d <= zb[i]) return; | |
| 138 | zb[i] = p.d; | |
| 139 | pix[i] = ramp[clamp(Math.floor(tone + (bayerAt(ix, iy) - .5) * .5), 0, 4)]; | |
| 140 | } | |
| 141 | function shopFront() { | |
| 142 | const wall = rampOf(WALL), dark = rampOf(WALL_DARK), snow = rampOf(SNOW), warm = rampOf(WARM_PANE), door = rampOf(DOOR); | |
| 143 | const red = rampOf(AWN_RED), cream = rampOf(AWN_CREAM), iron = rampOf(IRON); | |
| 144 | for (let s = -.8; s <= 10.8; s += .03) for (let z = 0; z <= 6.4; z += .05) { | |
| 145 | let ramp = wall, tone = 2.2 + (Math.floor(z * 3) & 1) * .3 + (hash(Math.floor(s * 5), Math.floor(z * 5)) - .5) * .5; | |
| 146 | if (z < .3) { ramp = dark; tone = 1.8; } | |
| 147 | if (z > 6) { ramp = snow; tone = 3.2; } | |
| 148 | if (s > 7.4 && s < 9 && z < 3.6) { ramp = door; tone = 2 + (Math.floor(s * 6) & 1) * .5; } | |
| 149 | if (s > 1.3 && s < 4.1 && z > 1.6 && z < 4.2) { ramp = (s > 1.45 && s < 3.95 && z > 1.75 && z < 4.05) ? warm : dark; tone = ramp === warm ? 2.6 + (z - 1.7) * .3 : 1.4; } | |
| 150 | if (s > 1.3 && s < 4.1 && z > 1.6 && z < 4.2 && Math.abs(s - 2.7) < .05) { ramp = dark; tone = 1.4; } | |
| 151 | paintAt(ramp, s, D_WALL, z, tone); | |
| 152 | } | |
| 153 | for (let s = 1; s <= 4.4; s += .03) for (let k = 0; k <= 1; k += .05) { // striped awning over the window | |
| 154 | const stripe = Math.floor((s - 1) * 4) & 1; | |
| 155 | paintAt(stripe ? cream : red, s, D_WALL + .15 + k * .75, 4.5 - k * .5, 2.6 - k * .5); | |
| 156 | } | |
| 157 | for (let s = 1; s <= 4.4; s += .03) paintAt(red, s, D_WALL + .9, 4.0, 1.4); | |
| 158 | for (let z = 0; z <= 5.2; z += .05) for (const s of [-.3]) { // a lamp post at the left edge | |
| 159 | paintAt(iron, s, D_WALL + 1.2, z, 2); | |
| 160 | } | |
| 161 | } | |
| 162 | function bake() { | |
| 163 | ground.fill(0); shadowMask.fill(0); staticShadow.fill(0); kinds.fill(0); | |
| 164 | zb.fill(-1e9); | |
| 165 | pix.fill(rgb(255, 255, 255)); | |
| 166 | for (let iy = 0; iy < H; iy++) for (let ix = 0; ix < W; ix++) { | |
| 167 | const g = unproject(ix + .5, iy + .5), i = iy * W + ix, c = groundColor(ix, iy); | |
| 168 | if (!c) continue; | |
| 169 | pix[i] = c; ground[i] = 1; | |
| 170 | zb[i] = (g.x + g.y) / R2 * COS; | |
| 171 | } | |
| 172 | groundZ.set(zb); | |
| 173 | shopFront(); | |
| 174 | bakeOcclusion(); | |
| 175 | { const [x, y] = world(-.3, D_WALL + 1.2); for (const z of [1, 2.5, 4, 5.2]) castShadow(x, y, z, .15, 1, 0); } // the lamp post's | |
| 176 | staticShadow.set(shadowMask); | |
| 177 | applyShadows(groundZ); | |
| 178 | bakedPix.set(pix); | |
| 179 | bakedZ.set(zb); | |
| 180 | } | |
| 181 | ||
| 182 | /* ---------- figures ---------- */ | |
| 183 | const flick = () => STILL ? 0 : Math.floor(G.time * 8); | |
| 184 | function brazier() { | |
| 185 | const [x, y] = world(M.PAN_X, D_BRAZIER), top = M.PAN_HEIGHT * Z; | |
| 186 | for (const [dx, dy] of [[-.7, -.7], [.7, -.7], [0, .9]]) capsule([x + dx, y + dy, 0], [x + dx * .6, y + dy * .6, top - .5], .14, .1, () => IRON, 'solid'); | |
| 187 | sphere(x, y, top - .45, .95, IRON, 'solid'); // the bowl | |
| 188 | for (let k = 0; k < 7; k++) { // coals, flickering at 8 fps | |
| 189 | const t = k * 0.9 + (flick() + k) % 3 * .4; | |
| 190 | sphere(x + Math.cos(t) * .5, y + Math.sin(t) * .5, top - .12, .24, (k + flick()) & 1 ? COAL : EMBER, 'solid'); | |
| 191 | } | |
| 192 | for (let a = 0; a < 12; a++) { // the roasting pan's rim | |
| 193 | const t = a / 12 * Math.PI * 2; | |
| 194 | sphere(x + Math.cos(t) * .85, y + Math.sin(t) * .85, top + .02, .14, IRON, 'solid'); | |
| 195 | } | |
| 196 | for (let k = 0; k < 3; k++) { // smoke from the coals | |
| 197 | const rise = STILL ? k * .5 : ((G.time * .7 + k / 3) % 1) * 1.6; | |
| 198 | sphere(x + Math.sin(rise * 3 + k) * .25, y, top + .5 + rise, .22 + rise * .08, SMOKE, 'solid'); | |
| 199 | } | |
| 200 | castShadow(x, y, top, 1, 1, 0); | |
| 201 | castShadow(x, y, 0, .9, 1, 0); | |
| 202 | } | |
| 203 | function vendor() { | |
| 204 | const [x, y] = world(7.1, D_BRAZIER - .3); | |
| 205 | capsule([x, y, 0], [x, y, 2.2], .62, .5, () => COAT, 'solid'); | |
| 206 | sphere(x, y, 2.9, .38, SKIN, 'solid'); | |
| 207 | capsule([x, y, 3.15], [x, y, 3.45], .44, .3, () => HAT, 'solid'); | |
| 208 | castShadow(x, y, 1.6, .6, 1, 0); | |
| 209 | } | |
| 210 | function cone(s) { | |
| 211 | const [x, y] = world(s, D_CONE), jig = !STILL && G.shake > G.time ? (Math.floor(G.time * 30) % 2 ? .12 : -.12) : 0; | |
| 212 | for (let k = 0; k <= 7; k++) { | |
| 213 | const t = k / 7, z = .12 + t * 1.5, r = .08 + t * .52; | |
| 214 | sphere(x + jig, y, z, r, k & 1 ? CONE_PAPER : [228, 218, 192], 'solid'); | |
| 215 | } | |
| 216 | const heap = Math.min(P.catches, 5); // caught chestnuts heaped at the mouth | |
| 217 | for (let k = 0; k < heap; k++) sphere(x + jig + (k - 2) * .17, y + (k & 1 ? .12 : -.12), 1.62 + (k & 1) * .1, .2, NUT, 'solid'); | |
| 218 | castShadow(x, y, .9, .55, 1, 0); | |
| 219 | castShadow(x, y, 0, .3, 1, 0); | |
| 220 | } | |
| 221 | function nut(x, y, z, burnt, t) { | |
| 222 | if (!burnt) { | |
| 223 | sphere(x, y, z, .38, NUT, 'gloss'); | |
| 224 | sphere(x - .06, y + .02, z + .22, .13, NUT_CUT, 'solid'); // the pale split on a roasted shell | |
| 225 | } else { | |
| 226 | sphere(x, y, z, .38, BURNT, 'solid'); | |
| 227 | sphere(x - .1, y + .12, z + .08, .09, EMBER, 'solid'); // a glowing crack across the burnt shell | |
| 228 | sphere(x + .02, y + .02, z + .15, .09, EMBER, 'solid'); | |
| 229 | sphere(x + .14, y - .08, z + .06, .08, EMBER, 'solid'); | |
| 230 | for (let k = 0; k < 3; k++) { // smoke trailing above it | |
| 231 | const w = STILL ? 0 : Math.sin(t * 9 + k * 2) * .1; | |
| 232 | sphere(x + w, y + w, z + .5 + k * .38, .16 + k * .05, SMOKE, 'solid'); | |
| 233 | } | |
| 234 | } | |
| 235 | castShadow(x, y, z, .3, 1, 0); | |
| 236 | } | |
| 237 | function draw() { | |
| 238 | pix.set(bakedPix); | |
| 239 | zb.set(bakedZ); | |
| 240 | brazier(); | |
| 241 | vendor(); | |
| 242 | for (const p of P.pops) { | |
| 243 | if (p.result !== null || p.at > P.time) continue; | |
| 244 | const c = M.chestnutAt(p, P.time), f = Math.min(1, (P.time - p.at) / M.flight(p)); | |
| 245 | const [x, y] = world(c.x, D_BRAZIER + (D_CONE - D_BRAZIER) * f); | |
| 246 | nut(x, y, c.h * Z + .35, p.kind === 'burnt', P.time); | |
| 247 | } | |
| 248 | for (const f of G.fx) { | |
| 249 | const age = STILL ? 0 : G.time - f.at, [x, y] = world(f.s, D_CONE); | |
| 250 | if (f.kind === 'missed') nut(x, y, .3, false, 0); | |
| 251 | else if (f.kind === 'let-fall') nut(x, y, .3, true, G.time); | |
| 252 | else if (f.kind === 'caught') for (let k = 0; k < 4; k++) sphere(x + Math.cos(k * 1.6) * (.3 + age), y + Math.sin(k * 1.6) * (.3 + age), 1.9 + age * 1.5, .1, WARM_PANE, 'solid'); | |
| 253 | else if (f.kind === 'burnt-caught') for (let k = 0; k < 3; k++) sphere(x, y, 2 + age * 1.2 + k * .35, .25 + k * .06, SMOKE, 'solid'); | |
| 254 | } | |
| 255 | cone(P.cone.x); | |
| 256 | flushShadows(); | |
| 257 | applyShadows(groundZ); | |
| 258 | shadeEdges(); | |
| 259 | ctx.putImageData(image, 0, 0); | |
| 260 | } | |
| 261 | ||
| 262 | /* ---------- game ---------- */ | |
| 263 | // what changed since the last frame: every chestnut that has landed since, in order, is announced once | |
| 264 | function act() { | |
| 265 | for (const p of P.pops) { | |
| 266 | if (p.result === null || G.seen.has(p.id)) continue; | |
| 267 | G.seen.set(p.id, p.result); | |
| 268 | const s = M.landsX(p); | |
| 269 | G.fx.push({ kind: p.result, s, at: G.time }); | |
| 270 | if (p.result === 'caught') { setStatus(`caught · ${P.score} points`); sound('catch'); } | |
| 271 | else if (p.result === 'missed') { setStatus('a roasted one hit the pavement'); sound('miss'); } | |
| 272 | else if (p.result === 'burnt-caught') { G.shake = G.time + .5; setStatus(`burnt one caught · ${P.lives} ${P.lives === 1 ? 'life' : 'lives'} left`); sound('burnt'); } | |
| 273 | else setStatus('burnt one let fall'); | |
| 274 | } | |
| 275 | G.fx = G.fx.filter(f => G.time - f.at < 1.6); | |
| 276 | renderHud(); | |
| 277 | if (P.status !== 'playing') { | |
| 278 | if (!G.saved) { | |
| 279 | G.saved = true; | |
| 280 | G.record = P.score > G.best; | |
| 281 | if (G.record) { G.best = P.score; try { localStorage.setItem(BEST_KEY, String(P.score)); } catch (e) { /* private window: keep it for this visit */ } } | |
| 282 | sound(P.status === 'won' ? 'catch' : 'burnt'); | |
| 283 | } | |
| 284 | setStatus(`${P.line}${G.record ? ' · new best' : ''} · tap or press space to play again`); | |
| 285 | } | |
| 286 | } | |
| 287 | function restart() { | |
| 288 | if (P.status === 'playing') return false; | |
| 289 | P = M.start((Date.now() + G.seen.size) & 0xffff); | |
| 290 | G.seen.clear(); G.fx.length = 0; G.saved = false; G.record = false; G.shake = 0; | |
| 291 | setStatus('drag along the pavement or hold Left and Right · burnt ones smoke'); | |
| 292 | renderHud(); | |
| 293 | return true; | |
| 294 | } | |
| 295 | function update(dt) { | |
| 296 | G.time += dt; | |
| 297 | const r = M.tick(P, dt); | |
| 298 | if (r.ok) P = r.state; | |
| 299 | act(); | |
| 300 | } | |
| 301 | ||
| 302 | /* ---------- input ---------- */ | |
| 303 | function pointTo(e) { | |
| 304 | const r = canvas.getBoundingClientRect(); | |
| 305 | const x = (e.clientX - r.left) / (r.width || W) * W; | |
| 306 | const m = M.move(P, sOf(x)); | |
| 307 | P = m.state; | |
| 308 | } | |
| 309 | let dragging = null; | |
| 310 | canvas.addEventListener('pointerdown', e => { | |
| 311 | ensureAudio(); | |
| 312 | canvas.focus({ preventScroll: true }); | |
| 313 | if (restart()) return; | |
| 314 | dragging = e.pointerId; | |
| 315 | try { canvas.setPointerCapture(e.pointerId); } catch (err) { /* synthetic pointers have nothing to capture */ } | |
| 316 | pointTo(e); | |
| 317 | }); | |
| 318 | canvas.addEventListener('pointermove', e => { if (dragging === e.pointerId) pointTo(e); }); | |
| 319 | for (const type of ['pointerup', 'pointercancel']) canvas.addEventListener(type, e => { if (dragging === e.pointerId) dragging = null; }); | |
| 320 | const held = new Set(); | |
| 321 | const KEYMAP = { ArrowLeft: -1, a: -1, A: -1, ArrowRight: 1, d: 1, D: 1 }; | |
| 322 | function steer() { // heading for a pavement end while a key is held; letting go stops the cone | |
| 323 | const dir = (held.has(1) ? 1 : 0) - (held.has(-1) ? 1 : 0); | |
| 324 | P = M.move(P, dir < 0 ? 0 : dir > 0 ? M.WIDTH : P.cone.x).state; | |
| 325 | } | |
| 326 | canvas.addEventListener('keydown', e => { | |
| 327 | const k = KEYMAP[e.key]; | |
| 328 | if (!k && e.key !== ' ' && e.key !== 'Enter') return; | |
| 329 | e.preventDefault(); | |
| 330 | ensureAudio(); | |
| 331 | if (restart() || e.repeat || !k) return; | |
| 332 | dragging = null; | |
| 333 | held.add(k); | |
| 334 | steer(); | |
| 335 | }); | |
| 336 | canvas.addEventListener('keyup', e => { | |
| 337 | const k = KEYMAP[e.key]; | |
| 338 | if (!k) return; | |
| 339 | held.delete(k); | |
| 340 | steer(); | |
| 341 | }); | |
| 342 | canvas.addEventListener('blur', () => { held.clear(); steer(); }); | |
| 343 | ||
| 344 | /* ---------- sound ---------- */ | |
| 345 | let audio = null, muted = false; | |
| 346 | function ensureAudio() { | |
| 347 | if (audio) { if (audio.state === 'suspended') audio.resume(); return; } | |
| 348 | const AC = window.AudioContext || window.webkitAudioContext; | |
| 349 | if (AC) audio = new AC(); | |
| 350 | } | |
| 351 | function sound(kind) { | |
| 352 | if (!audio || muted) return; | |
| 353 | const now = audio.currentTime, gain = audio.createGain(), osc = audio.createOscillator(); | |
| 354 | const spec = { | |
| 355 | catch: [440, 660, .12, .07, 'sine'], | |
| 356 | miss: [300, 200, .1, .04, 'sine'], | |
| 357 | burnt: [200, 70, .25, .06, 'square'], | |
| 358 | }[kind]; | |
| 359 | osc.type = spec[4]; | |
| 360 | osc.frequency.setValueAtTime(spec[0], now); | |
| 361 | osc.frequency.exponentialRampToValueAtTime(spec[1], now + spec[2]); | |
| 362 | gain.gain.setValueAtTime(spec[3], now); | |
| 363 | gain.gain.exponentialRampToValueAtTime(.0005, now + spec[2]); | |
| 364 | osc.connect(gain).connect(audio.destination); | |
| 365 | osc.start(now); osc.stop(now + spec[2] + .01); | |
| 366 | } | |
| 367 | const muteButton = document.getElementById('mute'); | |
| 368 | muteButton.addEventListener('click', () => { | |
| 369 | muted = !muted; | |
| 370 | muteButton.textContent = muted ? 'sound off' : 'sound on'; | |
| 371 | muteButton.setAttribute('aria-pressed', String(muted)); | |
| 372 | }); | |
| 373 | ||
| 374 | /* ---------- fit and boot ---------- */ | |
| 375 | addEventListener('resize', fit); | |
| 376 | fit(); | |
| 377 | bake(); | |
| 378 | setStatus('drag along the pavement or hold Left and Right · burnt ones smoke'); | |
| 379 | renderHud(); | |
| 380 | canvas.focus({ preventScroll: true }); | |
| 381 | let last = performance.now(); | |
| 382 | function frame(now) { | |
| 383 | update(Math.min(.04, (now - last) / 1000)); | |
| 384 | last = now; | |
| 385 | draw(); | |
| 386 | requestAnimationFrame(frame); | |
| 387 | } | |
| 388 | requestAnimationFrame(frame); | |
| 389 | </script> | |
| 390 | </body> | |
| 391 | </html> |
chestnut-roast/page.test.mjs added +166
| @@ -0,0 +1,166 @@ | ||
| 1 | import { test, expect } from 'bun:test'; | |
| 2 | import { boot } from '../playtest.mjs'; | |
| 3 | ||
| 4 | // the pavement metre under a canvas column, the inverse of the page's own mapping | |
| 5 | const col = s => 180 + (s - 5) * 26; | |
| 6 | ||
| 7 | test('dragging along the pavement sets the cone target through the rules, and the cone follows', () => { | |
| 8 | const g = boot('chestnut-roast'); | |
| 9 | g.tap(col(8), 200); | |
| 10 | expect(g.read('P.cone.target')).toBeCloseTo(8, 0); | |
| 11 | g.run(1); | |
| 12 | expect(g.read('P.cone.x')).toBeCloseTo(8, 0); | |
| 13 | g.drag([col(8), 200], [col(1), 200], .4); | |
| 14 | expect(g.read('P.cone.target')).toBeCloseTo(1, 0); | |
| 15 | }); | |
| 16 | ||
| 17 | test('holding the arrow keys steers the cone and letting go stops it', () => { | |
| 18 | const g = boot('chestnut-roast'); | |
| 19 | g.keyDown('ArrowLeft'); | |
| 20 | g.run(.3); | |
| 21 | g.keyUp('ArrowLeft'); | |
| 22 | const stopped = g.read('P.cone.x'); | |
| 23 | expect(stopped).toBeLessThan(5); | |
| 24 | expect(stopped).toBeGreaterThan(0); | |
| 25 | g.run(.5); | |
| 26 | expect(g.read('P.cone.x')).toBeCloseTo(stopped, 5); | |
| 27 | g.keyDown('ArrowRight'); | |
| 28 | g.run(.2); | |
| 29 | expect(g.read('P.cone.x')).toBeGreaterThan(stopped); | |
| 30 | }); | |
| 31 | ||
| 32 | test('the HUD reports the rules state: points, lives as cones, catches and streak', () => { | |
| 33 | const g = boot('chestnut-roast'); | |
| 34 | while (g.read('P.catches') === 0 && g.read('P.status') === 'playing' && g.read('P.time') < 40) { | |
| 35 | const s = g.read('(() => { const p = P.pops.filter(q => q.result === null && q.kind === "roasted").sort((a, b) => ChestnutRoast.landsAt(a) - ChestnutRoast.landsAt(b))[0]; return ChestnutRoast.landsX(p); })()'); | |
| 36 | g.tap(col(s), 200); | |
| 37 | } | |
| 38 | expect(g.read('P.catches')).toBe(1); | |
| 39 | expect(g.el('score').textContent).toBe(String(g.read('P.score'))); | |
| 40 | expect(g.el('catches').textContent).toBe('1'); | |
| 41 | expect(g.el('lives').textContent).toBe(String(g.read('P.lives'))); | |
| 42 | expect(g.el('cones').children.length).toBe(3); | |
| 43 | expect(g.el('status').textContent).toContain('caught'); | |
| 44 | }); | |
| 45 | ||
| 46 | test('catching a burnt chestnut costs a life, shows one cone fewer and is announced', () => { | |
| 47 | const g = boot('chestnut-roast'); | |
| 48 | let guard = 0; | |
| 49 | while (g.read('P.lives') === 3 && guard++ < 400) { | |
| 50 | const s = g.read('(() => { const p = P.pops.filter(q => q.result === null && q.kind === "burnt").sort((a, b) => ChestnutRoast.landsAt(a) - ChestnutRoast.landsAt(b))[0]; return ChestnutRoast.landsX(p); })()'); | |
| 51 | g.tap(col(s), 200); | |
| 52 | } | |
| 53 | expect(g.read('P.lives')).toBe(2); | |
| 54 | expect(g.el('lives').textContent).toBe('2'); | |
| 55 | expect(g.el('status').textContent).toContain('burnt one caught'); | |
| 56 | expect(g.read('G.shake')).toBeGreaterThan(g.read('G.time')); | |
| 57 | expect(g.read('Array.from(nodes.cones.children).filter(c => c.className === "gone").length')).toBe(1); | |
| 58 | }); | |
| 59 | ||
| 60 | test('letting everything fall ends the round with the rules line, and a tap plays again', () => { | |
| 61 | const g = boot('chestnut-roast'); | |
| 62 | g.tap(col(0), 200); | |
| 63 | let guard = 0; | |
| 64 | while (g.read('P.status') === 'playing' && guard++ < 60) g.run(5); | |
| 65 | expect(g.read('P.status')).toBe('lost'); | |
| 66 | expect(g.el('status').textContent).toContain(g.read('P.line')); | |
| 67 | expect(g.el('status').textContent).toContain('play again'); | |
| 68 | g.tap(180, 200); | |
| 69 | expect(g.read('P.status')).toBe('playing'); | |
| 70 | expect(g.read('P.score')).toBe(0); | |
| 71 | }); | |
| 72 | ||
| 73 | test('the page has a mute button, a how-to-play label and no mp3', async () => { | |
| 74 | const html = await Bun.file(new URL('./index.html', import.meta.url)).text(); | |
| 75 | expect(html).toContain('id="mute"'); | |
| 76 | expect(html).toMatch(/aria-label="[^"]*drag[^"]*arrow keys[^"]*"/i); | |
| 77 | expect(html).not.toContain('.mp3'); | |
| 78 | }); | |
| 79 | ||
| 80 | test('A and D steer like the arrows', () => { | |
| 81 | const g = boot('chestnut-roast'); | |
| 82 | g.keyDown('a'); g.run(.3); g.keyUp('a'); | |
| 83 | const left = g.read('P.cone.x'); | |
| 84 | expect(left).toBeLessThan(5); | |
| 85 | g.keyDown('D'); g.run(.6); g.keyUp('D'); | |
| 86 | expect(g.read('P.cone.x')).toBeGreaterThan(left); | |
| 87 | }); | |
| 88 | ||
| 89 | test('Space and Enter play again from the keyboard once the round is over, and do nothing mid-round', () => { | |
| 90 | for (const key of [' ', 'Enter']) { | |
| 91 | const g = boot('chestnut-roast'); | |
| 92 | g.stopDrawing(); | |
| 93 | g.press(key); | |
| 94 | expect(g.read('P.status')).toBe('playing'); | |
| 95 | expect(g.read('P.time')).toBeGreaterThan(0); | |
| 96 | g.tap(col(0), 200); | |
| 97 | let guard = 0; | |
| 98 | while (g.read('P.status') === 'playing' && guard++ < 60) g.run(5); | |
| 99 | expect(g.read('P.status')).toBe('lost'); | |
| 100 | g.keyDown(key); | |
| 101 | expect(g.read('P.status')).toBe('playing'); | |
| 102 | expect(g.read('P.score')).toBe(0); | |
| 103 | } | |
| 104 | }); | |
| 105 | ||
| 106 | const BEST = 'pixel-plays-chestnut-roast-best'; | |
| 107 | ||
| 108 | test('the HUD best score is the one a previous visit saved, beaten live', () => { | |
| 109 | const g = boot('chestnut-roast', { stored: { [BEST]: '5' } }); | |
| 110 | expect(g.read('G.best')).toBe(5); | |
| 111 | expect(g.el('best').textContent).toBe('5'); | |
| 112 | const fresh = boot('chestnut-roast'); | |
| 113 | expect(fresh.el('best').textContent).toBe('0'); | |
| 114 | g.read('P.score = 20; renderHud()'); | |
| 115 | expect(g.el('best').textContent).toBe('20'); | |
| 116 | }); | |
| 117 | ||
| 118 | // end the round by letting everything fall, with the score forced to `score` just before | |
| 119 | function finish(g, score) { | |
| 120 | g.tap(col(0), 200); | |
| 121 | g.read(`P.score = ${score}`); | |
| 122 | let guard = 0; | |
| 123 | while (g.read('P.status') === 'playing' && guard++ < 60) g.run(5); | |
| 124 | expect(g.read('P.status')).toBe('lost'); | |
| 125 | } | |
| 126 | ||
| 127 | test('a round above the saved best is announced as a new best and saved', () => { | |
| 128 | const g = boot('chestnut-roast', { stored: { [BEST]: '5' } }); | |
| 129 | finish(g, 40); | |
| 130 | expect(g.read('G.record')).toBe(true); | |
| 131 | expect(g.el('status').textContent).toContain('new best'); | |
| 132 | expect(g.store[BEST]).toBe(String(g.read('P.score'))); | |
| 133 | expect(Number(g.store[BEST])).toBeGreaterThan(5); | |
| 134 | }); | |
| 135 | ||
| 136 | test('a round below the saved best is not announced as a new best and leaves the save alone', () => { | |
| 137 | const low = boot('chestnut-roast', { stored: { [BEST]: '999' } }); | |
| 138 | finish(low, 1); | |
| 139 | expect(low.read('G.record')).toBe(false); | |
| 140 | expect(low.el('status').textContent).not.toContain('new best'); | |
| 141 | expect(low.store[BEST]).toBe('999'); | |
| 142 | }); | |
| 143 | ||
| 144 | test('under reduced motion the coals do not flicker and a burnt catch does not shake the cone', () => { | |
| 145 | const still = boot('chestnut-roast', { reduced: true }), live = boot('chestnut-roast'); | |
| 146 | expect(still.read('STILL')).toBe(true); | |
| 147 | expect(live.read('STILL')).toBe(false); | |
| 148 | still.run(1); live.run(1); | |
| 149 | expect(still.read('flick()')).toBe(0); | |
| 150 | expect(live.read('flick()')).toBeGreaterThan(0); | |
| 151 | ||
| 152 | // the same frame twice over, with and without a shake: only the live page draws them differently | |
| 153 | const frame = (reduced, shake) => { | |
| 154 | const g = boot('chestnut-roast', { reduced }); | |
| 155 | g.read('P.pops.forEach(p => { p.at = 1e9; })'); | |
| 156 | if (shake) g.read('G.shake = G.time + 5'); | |
| 157 | const shots = []; | |
| 158 | for (let i = 0; i < 3; i++) { g.step(1); shots.push(g.picture()); } | |
| 159 | return shots; | |
| 160 | }; | |
| 161 | const liveShaken = frame(false, true), liveCalm = frame(false, false); | |
| 162 | expect(liveShaken).not.toEqual(liveCalm); | |
| 163 | const stillShaken = frame(true, true), stillCalm = frame(true, false); | |
| 164 | expect(stillShaken).toEqual(stillCalm); | |
| 165 | expect(new Set(stillShaken).size).toBe(1); | |
| 166 | }); |
index.html +1
| @@ -88,6 +88,7 @@ footer a::after { content: none; } | ||
| 88 | 88 | <li data-kind="Precision" data-input="Hold" style="--accent:#cf4b3a"><a href="tightrope/">Tightrope</a><span>Walk a wire between two rooftops, leaning against the wind to stay up: 10 plus 1 per second under 60, +5 for a steady crossing</span><span>Game forged by the crew</span><span class="tags">Precision · Hold</span></li> |
| 89 | 89 | <li data-kind="Timing" data-input="Hold" style="--accent:#9a6b3c"><a href="wood-split/">Wood Split</a><span>Split logs on a chopping block for 60 seconds: hold to raise the axe, release on the crack, a stuck axe costs two seconds</span><span>Game forged by the crew</span><span class="tags">Timing · Hold</span></li> |
| 90 | 90 | <li data-kind="Memory" data-input="Tap" style="--accent:#a8863a"><a href="bell-tower/">Bell Tower</a><span>Ring the peal back: the tower plays a sequence on four bells, pull the ropes in the same order, each round one bell longer</span><span>Game forged by the crew</span><span class="tags">Memory · Tap</span></li> |
| 91 | <li data-kind="Catching" data-input="Drag" style="--accent:#8b4513"><a href="chestnut-roast/">Chestnut Roast</a><span>Catch the roasted chestnuts in your paper cone and let the burnt ones drop</span><span>Game forged by the crew</span><span class="tags">Catching · Drag</span></li> | |
| 91 | 92 | <li data-kind="Memory" data-input="Tap" style="--accent:#7a3b2e"><a href="signal-box/">Signal Box</a><span>Watch the levers, pull them back in order, and route every train through the junction</span><span>Game forged by the crew</span><span class="tags">Memory · Tap</span></li> |
| 92 | 93 | <li data-kind="Timing" data-input="Hold" style="--accent:#6fb3d2"><a href="ice-fishing/">Ice Fishing</a><span>Drop a line through the ice and reel each fish up before it snaps: hold to let out line, a fish at the hook depth bites, hold to reel but ease off as the tension rises</span><span>Game forged by the crew</span><span class="tags">Timing · Hold</span></li> |
| 93 | 94 | <li data-kind="Sport" data-input="Drag" style="--accent:#b33a3a"><a href="archery/">Archery</a><span>Ten arrows at a straw target across a meadow: drag back to draw, read the pennant for wind, gold scores 10</span><span>Game forged by the crew</span><span class="tags">Sport · Drag</span></li> |
og/chestnut-roast.png added
Binary file not shown.
og/home.png
Binary file not shown.
playtest.mjs +4 −3
| @@ -41,9 +41,10 @@ function fakeDate(now) { | ||
| 41 | 41 | return class extends Date { constructor(...a) { super(...(a.length ? a : [now()])); } static now() { return now(); } }; |
| 42 | 42 | } |
| 43 | 43 | |
| 44 | export function boot(id, { seed = 1 } = {}) { | |
| 44 | // `stored` pre-seeds localStorage, so a page that reads it on load sees what an earlier visit saved | |
| 45 | export function boot(id, { seed = 1, reduced = false, stored = {} } = {}) { | |
| 45 | 46 | const els = {}, errors = [], frames = [], timers = [], watchers = []; |
| 46 | const store = {}; | |
| 47 | const store = { ...stored }; | |
| 47 | 48 | let clock = 1000, image = null; |
| 48 | 49 | const get = i => (els[i] ??= new Element(i)); |
| 49 | 50 | const canvas = get('c'); |
| @@ -64,7 +65,7 @@ export function boot(id, { seed = 1 } = {}) { | ||
| 64 | 65 | performance: { now: () => clock }, |
| 65 | 66 | localStorage: { getItem: k => store[k] ?? null, setItem: (k, v) => { store[k] = String(v); }, removeItem: k => { delete store[k]; } }, |
| 66 | 67 | Audio: class { play() { return Promise.resolve(); } pause() {} }, |
| 67 | matchMedia: () => ({ matches: false, addEventListener() {} }), | |
| 68 | matchMedia: q => ({ matches: reduced && /reduce/.test(q), addEventListener() {} }), | |
| 68 | 69 | navigator: { vibrate() {}, userAgent: 'playtest' }, |
| 69 | 70 | requestAnimationFrame: fn => { frames.push(fn); return frames.length; }, |
| 70 | 71 | cancelAnimationFrame() {}, |
rounds.mjs +14
| @@ -462,6 +462,20 @@ export const ROUNDS = { | ||
| 462 | 462 | else if (cup.cake !== want.cake) tap('cake'); |
| 463 | 463 | else tap(guest); |
| 464 | 464 | } }, |
| 465 | // try each cone target on the rules' own `tick` for the next second and a half, take the one that catches most and burns least, tap it on the pavement | |
| 466 | 'chestnut-roast': { over: /play again/, outcome: /You win: score \d+, best streak \d+/, beat: g => { | |
| 467 | const s = g.read(`(() => { | |
| 468 | const M = ChestnutRoast; let best = P.cone.target, top = -Infinity; | |
| 469 | for (let t = 0; t <= M.WIDTH; t += .25) { | |
| 470 | const r = M.tick(M.move(P, t).state, 1.5).state; | |
| 471 | const value = r.score - 1000 * (P.lives - r.lives) - Math.abs(t - P.cone.x) * .01; | |
| 472 | if (value > top) { top = value; best = t; } | |
| 473 | } | |
| 474 | return best; | |
| 475 | })()`); | |
| 476 | g.tap(180 + (s - 5) * 26, 200); | |
| 477 | g.run(.1); | |
| 478 | } }, | |
| 465 | 479 | // pull whenever the brush is cool; before each pull, look half a second ahead in the rules' own `tick` and change flue if smoke would hit |
| 466 | 480 | 'chimney-sweep': { over: /play again/, outcome: /You win: reached the roof with \d lives? left in [\d.]+ s, score \d+/, beat: g => { |
| 467 | 481 | const s = JSON.parse(g.read('JSON.stringify(P)')); |
serve.test.mjs +7
| @@ -720,6 +720,13 @@ test('Tea Party and its rules script are served without a track', async () => { | ||
| 720 | 720 | expect((await fetch(base + '/tea-party/table.js')).status).toBe(200); |
| 721 | 721 | }); |
| 722 | 722 | |
| 723 | test('Chestnut Roast and its rules script are served without a track', async () => { | |
| 724 | const page = await fetch(base + '/chestnut-roast/'); | |
| 725 | expect(page.status).toBe(200); | |
| 726 | expect(page.headers.get('content-type')).toBe('text/html; charset=utf-8'); | |
| 727 | expect((await fetch(base + '/chestnut-roast/pan.js')).status).toBe(200); | |
| 728 | }); | |
| 729 | ||
| 723 | 730 | test('Chimney Sweep and its rules script are served without a track', async () => { |
| 724 | 731 | const page = await fetch(base + '/chimney-sweep/'); |
| 725 | 732 | expect(page.status).toBe(200); |
shell.js +1
| @@ -71,6 +71,7 @@ window.PixelPlays = (() => { | ||
| 71 | 71 | { id: 'conkers', title: 'Conkers', blurb: 'Time your swing to crack every rival conker in the playground', kind: 'Sport', input: 'Tap', accent: '#8a4b22', music: false, forged: true }, |
| 72 | 72 | { id: 'skipping-rope', title: 'Skipping Rope', blurb: 'Jump the turning rope as it quickens and friends join the line', kind: 'Timing', input: 'Tap', accent: '#d0573c', music: false, forged: true }, |
| 73 | 73 | { id: 'tea-party', title: 'Tea Party', blurb: 'Remember every guest\'s order and pour it right as the table grows', kind: 'Memory', input: 'Tap', accent: '#c98aa0', music: false, forged: true }, |
| 74 | { id: 'chestnut-roast', title: 'Chestnut Roast', blurb: 'Catch the roasted chestnuts in your paper cone and let the burnt ones drop', kind: 'Catching', input: 'Drag', accent: '#8b4513', music: false, forged: true }, | |
| 74 | 75 | { id: 'chimney-sweep', title: 'Chimney Sweep', blurb: 'Climb the flues with your brush and duck the rising smoke', kind: 'Climbing', input: 'Tap', accent: '#4a4a4a', music: false, forged: true }, |
| 75 | 76 | ]; |
| 76 | 77 | const MUSIC_VOLUME = .4; |
new above
offline