Public: anyone can read this; the crew can change it.
Migrated from github.com/amirmushichge/pixel-plays
Add Jam Jars: the playable page and its place in the catalogue, on the rules in `jam-jars/shelf.js` #284
Check · Review · Land · Live done — Landed and live in production.
quill/pixel-plays:quill/259-add-jam-jars-the-playable-page-and-its-p → main 2 commits fork
9 files
changed +466
−2
README.md +3 −2
Diff not shown — a line 2,756 characters wide (limit 2,000). +3 −2
View fileindex.html +1
| @@ -126,6 +126,7 @@ footer a::after { content: none; } | ||
| 126 | 126 | <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 | 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> |
| 128 | 128 | <li data-kind="Puzzle" data-input="Tap" style="--accent:#5a6fb8"><a href="stained-glass/">Stained Glass</a><span>Colour the window so no two neighbouring panes match</span><span>Game forged by the crew</span><span class="tags">Puzzle · Tap</span></li> |
| 129 | <li data-kind="Puzzle" data-input="Tap" style="--accent:#b8324a"><a href="jam-jars/">Jam Jars</a><span>Pour the fruit between jars until every jar holds just one jam</span><span>Game forged by the crew</span><span class="tags">Puzzle · Tap</span></li> | |
| 129 | 130 | <li data-kind="Sport" data-input="Tap" style="--accent:#8a4b22"><a href="conkers/">Conkers</a><span>Time your swing to crack every rival conker in the playground</span><span>Game forged by the crew</span><span class="tags">Sport · Tap</span></li> |
| 130 | 131 | </ul> |
| 131 | 132 | <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> |
jam-jars/index.html added +349
| @@ -0,0 +1,349 @@ | ||
| 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>Jam Jars</title> | |
| 7 | <!-- og:begin --> | |
| 8 | <meta name="description" content="Pour the fruit between jars until every jar holds just one jam"> | |
| 9 | <link rel="canonical" href="https://pixel-plays--forge-trevato-apps.open-platform.sh/jam-jars/"> | |
| 10 | <meta property="og:type" content="website"> | |
| 11 | <meta property="og:site_name" content="Pixel Plays"> | |
| 12 | <meta property="og:title" content="Jam Jars"> | |
| 13 | <meta property="og:description" content="Pour the fruit between jars until every jar holds just one jam"> | |
| 14 | <meta property="og:url" content="https://pixel-plays--forge-trevato-apps.open-platform.sh/jam-jars/"> | |
| 15 | <meta property="og:image" content="https://pixel-plays--forge-trevato-apps.open-platform.sh/og/jam-jars.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="Jam Jars: 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; : #b8324a; } | |
| 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(); color: var(); } | |
| 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(); } | |
| 34 | .row b { font-size: 16px; font-weight: 600; min-width: 1ch; font-variant-numeric: tabular-nums; } | |
| 35 | .row span { color: var(); } | |
| 36 | #status { color: var(); min-height: 1.35em; } | |
| 37 | .keys { display: flex; gap: 8px; } | |
| 38 | button { pointer-events: auto; font: inherit; color: var(); background: none; border: 0; padding: 13px 10px; margin: -13px -10px; min-height: 44px; min-width: 44px; cursor: pointer; } | |
| 39 | button:hover, button:focus-visible { color: var(); } | |
| 40 | button:focus-visible { outline: 2px solid var(); outline-offset: 2px; } | |
| 41 | noscript p { position: fixed; inset: 40% 16px auto; margin: 0; text-align: center; color: var(); } | |
| 42 | </style> | |
| 43 | </head> | |
| 44 | <body> | |
| 45 | <div id="stage"><canvas id="c" tabindex="0" aria-label="A pantry shelf of glass jars with layers of fruit showing through the glass. Tap a jar, or press its number key, to pick it up, then tap another jar to pour its top fruit into it. Arrow keys move along the shelf, Space picks up or pours, U undoes the last pour, R starts the shelf again. Every jar must end empty or full of a single fruit. Each fruit has its own pip pattern as well as its colour: plum dots, fig stripes, quince checks. Four shelves, par pours each."></canvas></div> | |
| 46 | <noscript><p>Jam Jars needs JavaScript to play.</p></noscript> | |
| 47 | <div id="scores"> | |
| 48 | <div class="row"><b id="level">1 of 4</b><span id="name">shelf</span></div> | |
| 49 | <div class="row"><b id="pours">0</b><span>pours</span></div> | |
| 50 | <div class="row"><b id="par">0</b><span>par</span></div> | |
| 51 | <div class="row"><b id="score">0</b><span>points</span></div> | |
| 52 | </div> | |
| 53 | <div class="hud" id="bottom"> | |
| 54 | <div id="status" role="status" aria-live="polite"></div> | |
| 55 | <div class="keys"><button id="undo" type="button">undo</button><button id="mute" type="button" aria-pressed="false">sound on</button></div> | |
| 56 | </div> | |
| 57 | <script src="../shell.js"></script> | |
| 58 | <script> | |
| 59 | PixelPlays.mount({ | |
| 60 | id: 'jam-jars', | |
| 61 | rules: [ | |
| 62 | 'Tap a jar, or press its number, to pick it up; tap another to pour into it.', | |
| 63 | 'Only the top run of one fruit pours, onto the same fruit or into an empty space.', | |
| 64 | 'Every jar must end empty or full of one fruit. Undo costs a pour; U undoes, R restarts.', | |
| 65 | 'Solve all 4 shelves, within par pours for a bonus.', | |
| 66 | ], | |
| 67 | }); | |
| 68 | </script> | |
| 69 | <script>window.PP_SCENE = { originY: 8, shadowX: 1, shadowY: -.12 };</script> | |
| 70 | <script src="../engine.js"></script> | |
| 71 | <script src="shelf.js"></script> | |
| 72 | <script> | |
| 73 | 'use strict'; | |
| 74 | ||
| 75 | const M = window.JamJars; | |
| 76 | const WALL = [[238, 228, 208], [230, 219, 197]], WOOD = [[168, 124, 84], [156, 114, 76]], WOOD_FRONT = [112, 80, 54]; | |
| 77 | const INK_PIX = rgb(28, 27, 25), SIGNAL_PIX = rgb(255, 214, 10), LID_PIX = rgb(176, 170, 158), LID_DARK = rgb(116, 110, 102); | |
| 78 | const GLASS = [rgb(222, 234, 232), rgb(236, 244, 242)], SHINE = rgb(255, 255, 255); | |
| 79 | const FRUIT = { plum: [122, 52, 132], fig: [98, 150, 62], quince: [232, 188, 66] }; | |
| 80 | const RAMPS = Object.fromEntries(Object.entries(FRUIT).map(([k, c]) => [k, rampOf(c)])); | |
| 81 | // the pip on each fruit's layer, so colour is never the only cue: plum dots, fig stripes, quince checks | |
| 82 | const MARKS = { | |
| 83 | plum: (x, y) => x % 6 < 2 && (y + (x % 12 < 6 ? 0 : 3)) % 6 < 2, | |
| 84 | fig: (x, y) => (x + y) % 6 < 2, | |
| 85 | quince: (x, y) => ((x >> 1) + (y >> 1)) % 2 === 0, | |
| 86 | }; | |
| 87 | const JW = 40, LH = 13, GAP = 16, BASE = 176, LIFT = 12; // jar width, layer height, gap between jars, shelf line, lift when held | |
| 88 | const PLANK = { x0: 12, x1: 348, top: BASE - 14, front: BASE + 4, bottom: BASE + 14 }; | |
| 89 | const NAMES = { plum: 'plum', fig: 'fig', quince: 'quince' }; | |
| 90 | ||
| 91 | /* ---------- state ---------- */ | |
| 92 | const nodes = {}; | |
| 93 | for (const k of ['level', 'name', 'pours', 'par', 'score', 'status']) nodes[k] = document.getElementById(k); | |
| 94 | const shown = {}; | |
| 95 | function setText(key, text) { | |
| 96 | if (shown[key] === text) return; | |
| 97 | shown[key] = text; | |
| 98 | nodes[key].textContent = text; | |
| 99 | } | |
| 100 | let P = M.start(0); | |
| 101 | // sel: the jar held (or null); cursor: a jar the keys stand on; dirty: the shelf must be drawn again; layers counts how often | |
| 102 | const G = { time: 0, noteUntil: 0, sel: null, cursor: 0, keyed: false, dirty: true, layers: 0 }; | |
| 103 | const REFUSED = { | |
| 104 | empty: 'that jar is empty', full: 'that jar is full', mismatch: 'the top fruit does not match that jar', | |
| 105 | 'same-jar': 'put it back, or pick another jar', nothing: 'nothing to undo', over: 'the shelf is done', 'bad-jar': 'tap a jar', | |
| 106 | }; | |
| 107 | const START = 'tap a jar to pick it up'; | |
| 108 | ||
| 109 | function renderHud() { | |
| 110 | setText('level', `${P.level + 1} of ${P.levels}`); | |
| 111 | setText('name', P.name); | |
| 112 | setText('pours', String(P.moves)); | |
| 113 | setText('par', String(P.par)); | |
| 114 | setText('score', String(P.score)); | |
| 115 | } | |
| 116 | function note(text) { setText('status', text); G.noteUntil = G.time + 2; } | |
| 117 | const describe = j => { | |
| 118 | const jar = P.jars[j], t = M.top(jar); | |
| 119 | return `Jar ${j + 1}: ${t ? `${jar.join(', ')} (top ${t.count} ${NAMES[t.fruit]})` : 'empty'}`; | |
| 120 | }; | |
| 121 | ||
| 122 | /* ---------- geometry: where each jar stands, laid once per shelf ---------- */ | |
| 123 | const jarX = j => Math.round(W / 2 + (j - (P.jars.length - 1) / 2) * (JW + GAP)); | |
| 124 | const bodyTop = () => BASE - 3 - P.capacity * LH; | |
| 125 | const liftOf = j => (G.sel === j ? LIFT : 0); | |
| 126 | const jarAtPoint = (sx, sy) => { | |
| 127 | for (let j = 0; j < P.jars.length; j++) { | |
| 128 | if (Math.abs(sx - jarX(j)) <= JW / 2 + GAP / 2 - 1 && sy >= bodyTop() - 8 - LIFT && sy <= PLANK.bottom) return j; | |
| 129 | } | |
| 130 | return null; | |
| 131 | }; | |
| 132 | const jarPoint = j => [jarX(j), BASE - 20]; | |
| 133 | ||
| 134 | /* ---------- baked scene: pantry wall and a plank ---------- */ | |
| 135 | function bake() { | |
| 136 | for (let iy = 0; iy < H; iy++) for (let ix = 0; ix < W; ix++) { | |
| 137 | const board = (ix >> 4) & 1, t = .97 + (hash(ix, iy) - .5) * .05, c = WALL[board]; | |
| 138 | pix[iy * W + ix] = rgb(c[0] * t, c[1] * t, c[2] * t); | |
| 139 | } | |
| 140 | for (let iy = PLANK.top; iy < PLANK.bottom; iy++) for (let ix = PLANK.x0; ix < PLANK.x1; ix++) { | |
| 141 | const top = iy < PLANK.front, grain = noise(ix * .08, iy * .9) > .55 ? .9 : 1, c = top ? WOOD[(iy >> 1) & 1] : WOOD_FRONT; | |
| 142 | pix[iy * W + ix] = rgb(c[0] * grain, c[1] * grain, c[2] * grain); | |
| 143 | } | |
| 144 | for (let ix = PLANK.x0; ix < PLANK.x1; ix++) pix[PLANK.front * W + ix] = INK_PIX; | |
| 145 | bakedPix.set(pix); | |
| 146 | } | |
| 147 | ||
| 148 | /* ---------- the shelf layer: drawn once per change and cached ---------- */ | |
| 149 | const layerPix = new Uint32Array(W * H); | |
| 150 | let glassIdx = new Int32Array(0); | |
| 151 | const darken = (i, k) => { const c = pix[i]; pix[i] = rgb((c & 255) * k, (c >> 8 & 255) * k, (c >> 16 & 255) * k); }; | |
| 152 | function jarShape(cx, top, lift) { | |
| 153 | // the jar's body rows: x half-width per row, with the shoulder and the rounded base | |
| 154 | const rows = []; | |
| 155 | for (let y = top - lift; y <= BASE - lift; y++) { | |
| 156 | const fromBase = BASE - lift - y, edge = fromBase < 2 ? 2 - fromBase : 0; | |
| 157 | rows.push([y, cx - JW / 2 + edge, cx + JW / 2 - 1 - edge]); | |
| 158 | } | |
| 159 | return rows; | |
| 160 | } | |
| 161 | function paintLayer() { | |
| 162 | pix.set(bakedPix); | |
| 163 | const n = P.jars.length, top = bodyTop(), glass = []; | |
| 164 | // hard shadows, screen-right: on the wall behind and on the plank under each jar | |
| 165 | for (let j = 0; j < n; j++) { | |
| 166 | const cx = jarX(j); | |
| 167 | for (let y = top + 6; y < BASE - 1; y++) for (let x = cx + JW / 2; x < cx + JW / 2 + 7; x++) if (x < W) darken(y * W + x, .78); | |
| 168 | for (let k = 0; k < 5; k++) for (let x = cx + JW / 2; x < cx + JW / 2 + 16 - k * 2; x++) if (x < W) darken((BASE - 1 + k) * W + x, .7); | |
| 169 | } | |
| 170 | for (let j = 0; j < n; j++) { | |
| 171 | const cx = jarX(j), lift = liftOf(j), left = cx - JW / 2, jar = P.jars[j], held = G.sel === j; | |
| 172 | const edge = held ? SIGNAL_PIX : INK_PIX; | |
| 173 | for (const [y, x0, x1] of jarShape(cx, top, lift)) { | |
| 174 | for (let x = x0; x <= x1; x++) { | |
| 175 | const lx = x - left, ly = BASE - lift - y, layer = Math.floor((ly - 2) / LH), i = y * W + x; | |
| 176 | let c = GLASS[(x + y) & 1 && bayerAt(x, y) > .6 ? 1 : 0]; | |
| 177 | if (layer >= 0 && layer < jar.length) { | |
| 178 | const ramp = RAMPS[jar[layer]], seam = (ly - 2) % LH === LH - 1; | |
| 179 | c = seam ? ramp[0] : MARKS[jar[layer]](lx, ly) ? ramp[1] : lx < 5 ? ramp[4] : ramp[3]; | |
| 180 | } else glass.push(i); | |
| 181 | if (lx >= 4 && lx < 6 && ly > 4 && ly < P.capacity * LH - 2) c = SHINE; | |
| 182 | pix[i] = c; | |
| 183 | } | |
| 184 | pix[y * W + x0] = edge; pix[y * W + x1] = edge; | |
| 185 | } | |
| 186 | // the outline above and below, and the lid | |
| 187 | for (let x = left + 2; x < left + JW - 2; x++) { pix[(BASE - lift) * W + x] = edge; pix[(top - lift) * W + x] = edge; } | |
| 188 | for (let y = top - 6 - lift; y < top - lift; y++) for (let x = left + 5; x < left + JW - 5; x++) { | |
| 189 | pix[y * W + x] = y === top - 6 - lift || x === left + 5 || x === left + JW - 6 ? edge : y < top - 4 - lift ? LID_PIX : LID_DARK; | |
| 190 | } | |
| 191 | if (G.keyed && G.cursor === j && P.status === 'playing') for (let x = left; x < left + JW; x++) { pix[(BASE + 2) * W + x] = SIGNAL_PIX; pix[(BASE + 3) * W + x] = SIGNAL_PIX; } | |
| 192 | } | |
| 193 | glassIdx = Int32Array.from(glass); | |
| 194 | layerPix.set(pix); | |
| 195 | G.layers++; | |
| 196 | G.dirty = false; | |
| 197 | } | |
| 198 | // sunlight: a pale diagonal band creeping across the empty glass, stepped at 8 fps; not drawn at all under reduced motion | |
| 199 | function glint() { | |
| 200 | const step = Math.floor(G.time * WIND_FPS), at = step % 90; | |
| 201 | for (let k = 0; k < glassIdx.length; k++) { | |
| 202 | const i = glassIdx[k], ix = i % W, iy = (i - ix) / W; | |
| 203 | if (((ix + iy * 2) % 90 + 90 - at) % 90 < 4) darken(i, .94); | |
| 204 | } | |
| 205 | } | |
| 206 | function draw() { | |
| 207 | if (G.dirty) paintLayer(); | |
| 208 | pix.set(layerPix); | |
| 209 | if (!STILL) glint(); | |
| 210 | ctx.putImageData(image, 0, 0); | |
| 211 | } | |
| 212 | ||
| 213 | /* ---------- game: every rule is the rules script's ---------- */ | |
| 214 | function show() { G.dirty = true; renderHud(); } | |
| 215 | function pourTo(from, to) { | |
| 216 | const before = P, r = M.pour(P, from, to); | |
| 217 | if (!r.ok) { note(REFUSED[r.why] || 'cannot do that'); sound(110, .12, 'sawtooth'); return; } | |
| 218 | G.sel = null; | |
| 219 | P = r.state; | |
| 220 | const fruit = M.top(before.jars[from]).fruit; | |
| 221 | if (r.event === 'poured') { note(`Poured ${NAMES[fruit]} into jar ${to + 1}. ${P.line}`); sound(330, .09, 'sine'); sound(280, .09, 'sine', .07); } | |
| 222 | else if (r.event === 'solved') { | |
| 223 | note(`${before.name} solved in ${before.moves + 1} pours, par ${before.par}. Next: ${P.name}`); | |
| 224 | G.cursor = 0; | |
| 225 | sound(523, .12, 'triangle'); sound(659, .2, 'triangle', .1); | |
| 226 | } else if (r.event === 'won') { G.noteUntil = 0; sound(523, .12, 'triangle'); sound(659, .12, 'triangle', .12); sound(784, .4, 'triangle', .24); } | |
| 227 | show(); | |
| 228 | } | |
| 229 | function apply(j) { | |
| 230 | ensureAudio(); | |
| 231 | if (P.status !== 'playing') return; | |
| 232 | G.cursor = j; | |
| 233 | if (G.sel === null) { | |
| 234 | if (!P.jars[j].length) { note(REFUSED.empty); sound(110, .12, 'sawtooth'); return; } | |
| 235 | G.sel = j; | |
| 236 | note(`${describe(j)}. Tap a jar to pour into`); | |
| 237 | sound(440, .05, 'sine'); | |
| 238 | show(); | |
| 239 | return; | |
| 240 | } | |
| 241 | if (G.sel === j) { G.sel = null; note(START); sound(360, .05, 'sine'); show(); return; } | |
| 242 | pourTo(G.sel, j); | |
| 243 | } | |
| 244 | function undo() { | |
| 245 | ensureAudio(); | |
| 246 | if (P.status !== 'playing') return; | |
| 247 | const r = M.undo(P); | |
| 248 | if (!r.ok) { note(REFUSED[r.why] || 'cannot do that'); sound(110, .12, 'sawtooth'); return; } | |
| 249 | P = r.state; G.sel = null; | |
| 250 | note(`Undone, it costs a pour. ${P.line}`); | |
| 251 | sound(300, .07, 'triangle'); | |
| 252 | show(); | |
| 253 | } | |
| 254 | function restart() { | |
| 255 | P = M.start(0); G.sel = null; G.cursor = 0; G.keyed = false; | |
| 256 | show(); | |
| 257 | note(START); | |
| 258 | } | |
| 259 | function redo() { | |
| 260 | if (P.status !== 'playing') return; | |
| 261 | P = M.restart(P); G.sel = null; | |
| 262 | show(); | |
| 263 | note(`${P.name} begun again`); | |
| 264 | } | |
| 265 | function update(dt) { | |
| 266 | G.time += dt; | |
| 267 | if (G.time < G.noteUntil) return; | |
| 268 | setText('status', P.status === 'won' ? `${P.line} · tap or press a key to play again` : P.line); | |
| 269 | } | |
| 270 | ||
| 271 | /* ---------- input ---------- */ | |
| 272 | const point = e => { const r = canvas.getBoundingClientRect(); return [(e.clientX - r.left) / r.width * W, (e.clientY - r.top) / r.height * H]; }; | |
| 273 | canvas.addEventListener('pointerdown', e => { | |
| 274 | canvas.focus({ preventScroll: true }); | |
| 275 | ensureAudio(); | |
| 276 | if (P.status !== 'playing') { restart(); return; } | |
| 277 | const [sx, sy] = point(e), j = jarAtPoint(sx, sy); | |
| 278 | G.keyed = false; | |
| 279 | G.dirty = true; | |
| 280 | if (j === null) { if (G.sel !== null) { G.sel = null; show(); } note(START); return; } | |
| 281 | apply(j); | |
| 282 | }); | |
| 283 | canvas.addEventListener('keydown', e => { | |
| 284 | if (e.ctrlKey || e.metaKey || e.altKey || e.key === 'Tab') return; | |
| 285 | const next = e.key === 'ArrowRight' || e.key === 'ArrowDown' ? 1 : e.key === 'ArrowLeft' || e.key === 'ArrowUp' ? -1 : 0; | |
| 286 | const act = e.key === ' ' || e.key === 'Enter', digit = /^[1-9]$/.test(e.key), again = e.key === 'r' || e.key === 'R'; | |
| 287 | const back = e.key === 'u' || e.key === 'U', esc = e.key === 'Escape'; | |
| 288 | if (!next && !act && !digit && !again && !back && !esc) return; | |
| 289 | e.preventDefault(); | |
| 290 | if (e.repeat && !next) return; | |
| 291 | if (P.status !== 'playing') { restart(); return; } | |
| 292 | G.keyed = true; | |
| 293 | G.dirty = true; | |
| 294 | if (digit) { if (Number(e.key) <= P.jars.length) apply(Number(e.key) - 1); return; } | |
| 295 | if (again) { redo(); return; } | |
| 296 | if (back) { undo(); return; } | |
| 297 | if (esc) { G.sel = null; note(START); return; } | |
| 298 | if (next) { | |
| 299 | G.cursor = (G.cursor + next + P.jars.length) % P.jars.length; | |
| 300 | note(describe(G.cursor)); | |
| 301 | return; | |
| 302 | } | |
| 303 | apply(G.cursor); | |
| 304 | }); | |
| 305 | document.getElementById('undo').addEventListener('click', undo); | |
| 306 | ||
| 307 | /* ---------- sound ---------- */ | |
| 308 | let audio = null, muted = false; | |
| 309 | function ensureAudio() { | |
| 310 | if (audio) { if (audio.state === 'suspended') audio.resume(); return; } | |
| 311 | const AC = window.AudioContext || window.webkitAudioContext; | |
| 312 | if (AC) audio = new AC(); | |
| 313 | } | |
| 314 | function sound(freq, life, type, delay = 0) { | |
| 315 | if (!audio || muted) return; | |
| 316 | const now = audio.currentTime + delay, gain = audio.createGain(), osc = audio.createOscillator(); | |
| 317 | osc.type = type; | |
| 318 | osc.frequency.setValueAtTime(freq, now); | |
| 319 | gain.gain.setValueAtTime(.0001, now); | |
| 320 | gain.gain.exponentialRampToValueAtTime(.07, now + .01); | |
| 321 | gain.gain.exponentialRampToValueAtTime(.0005, now + life); | |
| 322 | osc.connect(gain).connect(audio.destination); | |
| 323 | osc.start(now); osc.stop(now + life + .02); | |
| 324 | } | |
| 325 | const muteButton = document.getElementById('mute'); | |
| 326 | muteButton.addEventListener('click', () => { | |
| 327 | muted = !muted; | |
| 328 | muteButton.textContent = muted ? 'sound off' : 'sound on'; | |
| 329 | muteButton.setAttribute('aria-pressed', String(muted)); | |
| 330 | }); | |
| 331 | ||
| 332 | /* ---------- fit and boot ---------- */ | |
| 333 | addEventListener('resize', fit); | |
| 334 | fit(); | |
| 335 | bake(); | |
| 336 | renderHud(); | |
| 337 | note(START); | |
| 338 | canvas.focus({ preventScroll: true }); | |
| 339 | let last = performance.now(); | |
| 340 | function frame(now) { | |
| 341 | update(Math.min(.04, (now - last) / 1000)); | |
| 342 | last = now; | |
| 343 | draw(); | |
| 344 | requestAnimationFrame(frame); | |
| 345 | } | |
| 346 | requestAnimationFrame(frame); | |
| 347 | </script> | |
| 348 | </body> | |
| 349 | </html> |
jam-jars/page.test.mjs added +78
| @@ -0,0 +1,78 @@ | ||
| 1 | import { test, expect } from 'bun:test'; | |
| 2 | import { boot } from '../playtest.mjs'; | |
| 3 | ||
| 4 | const tapJar = (g, j) => g.tap(...g.read(`jarPoint(${j})`)); | |
| 5 | ||
| 6 | test('a tap on a jar lifts it, a tap on another pours into it, and the HUD reports the rules state', () => { | |
| 7 | const g = boot('jam-jars', { seed: 3 }); | |
| 8 | tapJar(g, 0); | |
| 9 | expect(g.read('G.sel')).toBe(0); | |
| 10 | tapJar(g, 2); | |
| 11 | expect(g.read('G.sel')).toBe(null); | |
| 12 | expect(g.read('P.jars[2]')).toEqual(['plum']); | |
| 13 | expect(g.el('pours').textContent).toBe('1'); | |
| 14 | expect(g.el('par').textContent).toBe(String(g.read('P.par'))); | |
| 15 | expect(g.el('level').textContent).toBe('1 of 4'); | |
| 16 | expect(g.el('name').textContent).toBe('First Shelf'); | |
| 17 | expect(g.el('status').textContent).toContain(g.read('P.line')); | |
| 18 | }); | |
| 19 | ||
| 20 | test('a refused pour says why and leaves the shelf as it was', () => { | |
| 21 | const g = boot('jam-jars', { seed: 3 }); | |
| 22 | tapJar(g, 2); // the empty jar cannot be lifted | |
| 23 | expect(g.read('G.sel')).toBe(null); | |
| 24 | expect(g.el('status').textContent).toContain('empty'); | |
| 25 | tapJar(g, 0); | |
| 26 | tapJar(g, 1); // onto a full jar | |
| 27 | expect(g.read('P.moves')).toBe(0); | |
| 28 | expect(g.el('status').textContent).toContain('full'); | |
| 29 | }); | |
| 30 | ||
| 31 | test('digits pick up and pour, U undoes at the cost of a pour, and the undo button does the same', () => { | |
| 32 | const g = boot('jam-jars', { seed: 3 }); | |
| 33 | g.press('1'); g.press('3'); | |
| 34 | expect(g.read('P.moves')).toBe(1); | |
| 35 | g.press('u'); | |
| 36 | expect(g.read('P.jars[0]')).toEqual(['plum', 'fig', 'plum']); | |
| 37 | expect(g.read('P.moves')).toBe(2); | |
| 38 | expect(g.el('pours').textContent).toBe('2'); | |
| 39 | g.press('1'); g.press('3'); | |
| 40 | g.el('undo').fire('click'); | |
| 41 | expect(g.read('P.moves')).toBe(4); | |
| 42 | }); | |
| 43 | ||
| 44 | test('every fruit carries its own pip pattern, so colour is never the only cue', () => { | |
| 45 | const g = boot('jam-jars', { seed: 3 }); | |
| 46 | const grids = ['plum', 'fig', 'quince'].map(f => g.read(`(() => { let s = ''; for (let y = 0; y < 24; y++) for (let x = 0; x < 24; x++) s += MARKS.${f}(x, y) ? 1 : 0; return s; })()`)); | |
| 47 | expect(new Set(grids).size).toBe(3); | |
| 48 | for (const s of grids) { const on = [...s].filter(c => c === '1').length; expect(on).toBeGreaterThan(30); expect(on).toBeLessThan(400); } | |
| 49 | }); | |
| 50 | ||
| 51 | test('the shelf is drawn once per change, not once per frame', () => { | |
| 52 | const g = boot('jam-jars', { seed: 3 }); | |
| 53 | g.run(2); | |
| 54 | const base = g.read('G.layers'); | |
| 55 | g.run(3); | |
| 56 | expect(g.read('G.layers')).toBe(base); | |
| 57 | tapJar(g, 0); | |
| 58 | g.run(1); | |
| 59 | expect(g.read('G.layers')).toBe(base + 1); | |
| 60 | }); | |
| 61 | ||
| 62 | test('mute toggles its label and aria-pressed', () => { | |
| 63 | const g = boot('jam-jars', { seed: 3 }); | |
| 64 | g.el('mute').fire('click'); | |
| 65 | expect(g.el('mute').textContent).toBe('sound off'); | |
| 66 | expect(g.el('mute').attrs['aria-pressed']).toBe('true'); | |
| 67 | }); | |
| 68 | ||
| 69 | test('the page source says how to play, keeps the 44px targets, stills the sunlight, holds the rules to four lines and names no .mp3', async () => { | |
| 70 | const html = await Bun.file(new URL('./index.html', import.meta.url)).text(); | |
| 71 | expect(html).toMatch(/<canvas id="c"[^>]*aria-label="[^"]*Tap a jar[^"]*number key[^"]*pour/); | |
| 72 | expect(html).toMatch(/min-height: 44px; min-width: 44px/); | |
| 73 | expect(html).toMatch(/id="mute"[^>]*aria-pressed="false"/); | |
| 74 | expect(html).toContain('if (!STILL) glint()'); | |
| 75 | expect(html).not.toContain('.mp3'); | |
| 76 | const rules = html.match(/rules: \[([\s\S]*?)\]/)[1].split('\n').filter(l => l.trim()); | |
| 77 | expect(rules.length).toBeLessThanOrEqual(4); | |
| 78 | }); |
og/home.png
Binary file not shown.
og/jam-jars.png added
Binary file not shown.
rounds.mjs +27
| @@ -8,6 +8,7 @@ import Allotment from './allotment/plot.js'; | ||
| 8 | 8 | import { toss, MAX_AIM } from './hoopla/hoop.js'; |
| 9 | 9 | import Wheel from './ferris-wheel/wheel.js'; |
| 10 | 10 | import Quilt from './quilt/patch.js'; |
| 11 | import Jars from './jam-jars/shelf.js'; | |
| 11 | 12 | |
| 12 | 13 | const MID = [180, 140]; |
| 13 | 14 | const tapping = (wait = .3) => g => { g.tap(...MID); g.run(wait); }; |
| @@ -27,6 +28,23 @@ function glassPlan({ panes, pairs, fixed, colours, palette }) { | ||
| 27 | 28 | return free.map(p => [p, set[p]]); |
| 28 | 29 | } |
| 29 | 30 | |
| 31 | // the fewest pours that solve a shelf: a breadth-first search over the rules' own `pour`, as [from, to] pairs | |
| 32 | function jarPlan(start) { | |
| 33 | const key = s => JSON.stringify(s.jars), seen = new Set([key(start)]); | |
| 34 | let frontier = [{ s: start, path: [] }]; | |
| 35 | while (frontier.length) { | |
| 36 | const next = []; | |
| 37 | for (const { s, path } of frontier) for (let a = 0; a < s.jars.length; a++) for (let b = 0; b < s.jars.length; b++) { | |
| 38 | const r = Jars.pour(s, a, b); | |
| 39 | if (!r.ok) continue; | |
| 40 | if (r.event !== 'poured') return [...path, [a, b]]; | |
| 41 | if (!seen.has(key(r.state))) { seen.add(key(r.state)); next.push({ s: r.state, path: [...path, [a, b]] }); } | |
| 42 | } | |
| 43 | frontier = next; | |
| 44 | } | |
| 45 | return []; | |
| 46 | } | |
| 47 | ||
| 30 | 48 | // the operations of a shortest win from a round state: a breadth-first search over the rules' own `apply` |
| 31 | 49 | function solve(start) { |
| 32 | 50 | const ops = []; |
| @@ -205,6 +223,15 @@ export const ROUNDS = { | ||
| 205 | 223 | if (g.read('G.colour') !== colour) g.tap(...g.read(`swatchPoint(${colour})`)); |
| 206 | 224 | else g.tap(...g.read(`panePoint(${pane})`)); |
| 207 | 225 | } }, |
| 226 | // pour along the shortest plan the rules' own search finds for each shelf: tap the jar to lift, then the jar to pour into | |
| 227 | 'jam-jars': { over: /play again/, outcome: /Shelf done, you win: \d+ pours against a par of \d+/, beat: g => { | |
| 228 | if (g.read('P.status') !== 'playing') { g.run(.3); return; } | |
| 229 | const level = g.read('P.level'); | |
| 230 | if (g.plan?.level !== level) g.plan = { level, pours: jarPlan(Jars.start(level)) }; | |
| 231 | const [from, to] = g.plan.pours[g.read('P.moves')]; | |
| 232 | g.tap(...g.read(`jarPoint(${from})`)); | |
| 233 | g.tap(...g.read(`jarPoint(${to})`)); | |
| 234 | } }, | |
| 208 | 235 | // wait for the meter to sweep into the sweet spot, then swing: the rival's swings come from the seed |
| 209 | 236 | 'conkers': { over: /play again/, outcome: /All 3 rivals cracked, you win: \d+ points/, beat: g => { |
| 210 | 237 | const meter = g.read('G.meter'); |
serve.test.mjs +7
| @@ -685,6 +685,13 @@ test('Stained Glass and its rules script are served without a track', async () = | ||
| 685 | 685 | expect((await fetch(base + '/stained-glass/window.js')).status).toBe(200); |
| 686 | 686 | }); |
| 687 | 687 | |
| 688 | test('Jam Jars and its rules script are served without a track', async () => { | |
| 689 | const page = await fetch(base + '/jam-jars/'); | |
| 690 | expect(page.status).toBe(200); | |
| 691 | expect(page.headers.get('content-type')).toBe('text/html; charset=utf-8'); | |
| 692 | expect((await fetch(base + '/jam-jars/shelf.js')).status).toBe(200); | |
| 693 | }); | |
| 694 | ||
| 688 | 695 | test('Conkers and its rules script are served without a track', async () => { |
| 689 | 696 | const page = await fetch(base + '/conkers/'); |
| 690 | 697 | expect(page.status).toBe(200); |
shell.js +1
| @@ -66,6 +66,7 @@ window.PixelPlays = (() => { | ||
| 66 | 66 | { 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 | 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 }, |
| 68 | 68 | { id: 'stained-glass', title: 'Stained Glass', blurb: 'Colour the window so no two neighbouring panes match', kind: 'Puzzle', input: 'Tap', accent: '#5a6fb8', music: false, forged: true }, |
| 69 | { id: 'jam-jars', title: 'Jam Jars', blurb: 'Pour the fruit between jars until every jar holds just one jam', kind: 'Puzzle', input: 'Tap', accent: '#b8324a', music: false, forged: true }, | |
| 69 | 70 | { 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 }, |
| 70 | 71 | ]; |
| 71 | 72 | const MUSIC_VOLUME = .4; |
new above
offline