Public: anyone can read this; the crew can change it.
Migrated from github.com/amirmushichge/pixel-plays
Add Mill Race: the playable page and its place in the catalogue, on the rules in `mill-race/race.js`, inside the per-game test budget #252
Check · Review · Land · Live done — Landed and live in production.
quill/pixel-plays:quill/228-add-mill-race-the-playable-page-and-its → main 4 commits fork
11 files
changed +570
−4
README.md +3 −2
Diff not shown — a line 2,451 characters wide (limit 2,000). +3 −2
View fileindex.html +1
| @@ -123,6 +123,7 @@ footer a::after { content: none; } | ||
| 123 | 123 | <li data-kind="Strategy" data-input="Tap" style="--accent:#3d7f6e"><a href="bunting/">Bunting</a><span>Hang the flags for the fete so no two neighbours match</span><span>Game forged by the crew</span><span class="tags">Strategy · Tap</span></li> |
| 124 | 124 | <li data-kind="Catching" data-input="Tap" style="--accent:#5f86a3"><a href="rain-barrel/">Rain Barrel</a><span>Slide the barrel under each spout before the yard floods</span><span>Game forged by the crew</span><span class="tags">Catching · Tap</span></li> |
| 125 | 125 | <li data-kind="Puzzle" data-input="Tap" style="--accent:#b5656b"><a href="quilt/">Patchwork Quilt</a><span>Turn and swap the patches until every stitch lines up</span><span>Game forged by the crew</span><span class="tags">Puzzle · Tap</span></li> |
| 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> | |
| 126 | 127 | </ul> |
| 127 | 128 | <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> |
| 128 | 129 | </main> |
mill-race/index.html added +425
| @@ -0,0 +1,425 @@ | ||
| 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>Mill Race</title> | |
| 7 | <!-- og:begin --> | |
| 8 | <meta name="description" content="Turn the channels until the stream reaches the mill wheel"> | |
| 9 | <link rel="canonical" href="https://pixel-plays--forge-trevato-apps.open-platform.sh/mill-race/"> | |
| 10 | <meta property="og:type" content="website"> | |
| 11 | <meta property="og:site_name" content="Pixel Plays"> | |
| 12 | <meta property="og:title" content="Mill Race"> | |
| 13 | <meta property="og:description" content="Turn the channels until the stream reaches the mill wheel"> | |
| 14 | <meta property="og:url" content="https://pixel-plays--forge-trevato-apps.open-platform.sh/mill-race/"> | |
| 15 | <meta property="og:image" content="https://pixel-plays--forge-trevato-apps.open-platform.sh/og/mill-race.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="Mill Race: 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; : #3d6f8f; } | |
| 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 | 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; } | |
| 38 | button:hover, button:focus-visible { color: var(); } | |
| 39 | button:focus-visible { outline: 2px solid var(); outline-offset: 2px; } | |
| 40 | noscript p { position: fixed; inset: 40% 16px auto; margin: 0; text-align: center; color: var(); } | |
| 41 | </style> | |
| 42 | </head> | |
| 43 | <body> | |
| 44 | <div id="stage"><canvas id="c" tabindex="0" aria-label="A stone slab of channel tiles with a sluice gate on its west side and a water wheel on its east side. Turn the tiles until the stream runs from the sluice to the wheel: tap a tile to turn it a quarter, or move the cursor with the arrow keys and press Space to turn. Wet channels are wide and blue, dry channels are narrow grooves; solid stone blocks do not turn. Five levels, par moves each."></canvas></div> | |
| 45 | <noscript><p>Mill Race needs JavaScript to play.</p></noscript> | |
| 46 | <div id="scores"> | |
| 47 | <div class="row"><b id="level">1 of 5</b><span>level</span></div> | |
| 48 | <div class="row"><b id="moves">0</b><span>moves</span></div> | |
| 49 | <div class="row"><b id="par">0</b><span>par</span></div> | |
| 50 | </div> | |
| 51 | <div class="hud" id="bottom"> | |
| 52 | <div id="status" role="status" aria-live="polite"></div> | |
| 53 | <button id="mute" type="button" aria-pressed="false">sound on</button> | |
| 54 | </div> | |
| 55 | <script src="../shell.js"></script> | |
| 56 | <script> | |
| 57 | PixelPlays.mount({ | |
| 58 | id: 'mill-race', | |
| 59 | rules: [ | |
| 60 | 'Tap a stone tile to turn it a quarter, or use arrows and Space.', | |
| 61 | 'Join the channels so the stream runs from the sluice to the mill wheel.', | |
| 62 | 'Each turn is a move; stay near par, 6 moves over in all at most.', | |
| 63 | 'Clear all 5 levels to win.', | |
| 64 | ], | |
| 65 | }); | |
| 66 | </script> | |
| 67 | <script>window.PP_SCENE = { originY: 10, shadowX: 1.3, shadowY: -.12 };</script> | |
| 68 | <script src="../engine.js"></script> | |
| 69 | <script src="race.js"></script> | |
| 70 | <script> | |
| 71 | 'use strict'; | |
| 72 | ||
| 73 | const M = window.MillRace; | |
| 74 | const HS = .7, SPAN = 20, FLOURISH = 1; // slab height, the slab's edge in world units, seconds the cleared level stays on show | |
| 75 | const SIDE = ['north', 'east', 'south', 'west']; | |
| 76 | const KIND = { straight: 'straight channel', bend: 'bend', tee: 'tee junction', blank: 'solid stone' }; | |
| 77 | ||
| 78 | const STONES = [[178, 172, 160], [168, 164, 154]], GROOVE = [92, 86, 78], WATER = [64, 134, 186], WOOD = [150, 112, 76], PIER = [190, 186, 176]; | |
| 79 | const LAWN = [[104, 156, 88], [96, 148, 82]]; | |
| 80 | const STONE_RAMPS = STONES.map(rampOf), WATER_RAMP = rampOf(WATER), GROOVE_RAMP = rampOf(GROOVE); | |
| 81 | const INK_PIX = rgb(28, 27, 25), SIGNAL_PIX = rgb(255, 214, 10), GLINT_PIX = rgb(214, 236, 250); | |
| 82 | ||
| 83 | /* ---------- state ---------- */ | |
| 84 | const nodes = {}; | |
| 85 | for (const k of ['level', 'moves', 'par', 'status']) nodes[k] = document.getElementById(k); | |
| 86 | const shown = {}; | |
| 87 | function setText(key, text) { | |
| 88 | if (shown[key] === text) return; | |
| 89 | shown[key] = text; | |
| 90 | nodes[key].textContent = text; | |
| 91 | } | |
| 92 | const fresh = () => M.start(Math.floor(Math.random() * 4294967296), 1); | |
| 93 | let P = fresh(); | |
| 94 | // view: the state on show (the cleared level stays for a moment); cx, cy the cursor tile; keyed: the keys are in use; | |
| 95 | // dirty: the tile layer must be drawn again; layers counts how often it was; hold: seconds left of the cleared level | |
| 96 | const G = { time: 0, noteUntil: 0, view: P, cx: 0, cy: 0, keyed: false, dirty: true, layers: 0, hold: 0, spin: 0, fed: false, baked: '' }; | |
| 97 | const REFUSED = { blank: 'that block is solid stone and does not turn', 'off-grid': 'tap a channel tile', over: 'the race is over' }; | |
| 98 | ||
| 99 | function renderHud() { | |
| 100 | setText('level', `${P.level} of ${P.last}`); | |
| 101 | setText('moves', String(P.moves)); | |
| 102 | setText('par', String(P.par)); | |
| 103 | } | |
| 104 | function note(text) { setText('status', text); G.noteUntil = G.time + 1.8; } | |
| 105 | const openings = t => M.sides(t).map(s => SIDE[s]).join(', '); | |
| 106 | const describe = (x, y) => { | |
| 107 | const t = G.view.tiles[y][x]; | |
| 108 | return `Column ${x + 1}, row ${y + 1}: ${KIND[t.type]}${t.type === 'blank' ? '' : ` opening ${openings(t)}`}`; | |
| 109 | }; | |
| 110 | ||
| 111 | /* ---------- the slab on the green ---------- */ | |
| 112 | // L holds what depends on the grid's size only: tile edge e, half the slab, and every pixel of the slab's top face | |
| 113 | let L = null; | |
| 114 | const groundAt = (sx, sy, z) => { | |
| 115 | const u = (sx - OX) / S, v = ((sy - OY) / S + z * COS) / SIN; | |
| 116 | return { x: (u + v) / R2, y: (v - u) / R2 }; | |
| 117 | }; | |
| 118 | const edgeOf = size => Math.min(5, SPAN / size); | |
| 119 | function lay(s) { | |
| 120 | const size = s.size; | |
| 121 | const e = edgeOf(size), half = size * e / 2, I = [], T = [], A = [], B = []; | |
| 122 | for (let iy = 0; iy < H; iy++) for (let ix = 0; ix < W; ix++) { | |
| 123 | const g = groundAt(ix + .5, iy + .5, HS); | |
| 124 | if (Math.abs(g.x) >= half || Math.abs(g.y) >= half) continue; | |
| 125 | const gx = (g.x + half) / e, gy = (g.y + half) / e, tx = Math.floor(gx), ty = Math.floor(gy); | |
| 126 | I.push(iy * W + ix); T.push(ty * size + tx); A.push(gx - tx); B.push(gy - ty); | |
| 127 | } | |
| 128 | L = { size, e, half, wheelRow: s.wheel.y, top: Int32Array.from(I), tile: Int32Array.from(T), a: Float32Array.from(A), b: Float32Array.from(B) }; | |
| 129 | } | |
| 130 | const centre = (x, y) => { const p = project(-L.half + (x + .5) * L.e, -L.half + (y + .5) * L.e, HS); return [Math.round(p.sx), Math.round(p.sy)]; }; | |
| 131 | const tileAt = (sx, sy) => { | |
| 132 | const g = groundAt(sx, sy, HS), x = Math.floor((g.x + L.half) / L.e), y = Math.floor((g.y + L.half) / L.e); | |
| 133 | return x >= 0 && y >= 0 && x < L.size && y < L.size ? [x, y] : null; | |
| 134 | }; | |
| 135 | const rowY = row => -L.half + (row + .5) * L.e; | |
| 136 | const wheelGeometry = () => { | |
| 137 | const R = Math.min(2.3, Math.max(1.5, L.e * .55)); | |
| 138 | return { R, x: L.half + R + .15, y: rowY(L.wheelRow), z: R + .25, reach: L.e * .38 }; | |
| 139 | }; | |
| 140 | ||
| 141 | /* ---------- baked scene: meadow, slab, sluice and piers; redone when the level's grid changes size ---------- */ | |
| 142 | function groundColor(x, y, ix, iy) { | |
| 143 | const u = (x - y) / R2, v = (x + y) / R2; | |
| 144 | const edge = Math.pow(Math.abs(u) / 17.8, 4) + Math.pow(Math.abs(v) / 19, 4); | |
| 145 | const rough = Math.pow(edge, .25) + (hash(ix, iy) - .5) * .05; | |
| 146 | if (rough > 1) return 0; | |
| 147 | const c = LAWN[Math.floor((u + 40) / 3) & 1], t = .96 + (hash(ix, iy) - .5) * .06; | |
| 148 | return rgb(c[0] * t, c[1] * t, c[2] * t); | |
| 149 | } | |
| 150 | const stoneAt = (tile, ix, iy, level) => STONE_RAMPS[tile & 1][clamp(Math.floor(level + (hash(ix, iy) - .5) * 1.2), 1, 4)]; | |
| 151 | function face(fixed, c, level) { | |
| 152 | // a vertical side of the slab: fixed 'x' or 'y' at the slab's edge | |
| 153 | const ramp = STONE_RAMPS[0]; | |
| 154 | for (let t = -L.half; t <= L.half; t += .05) for (let z = 0; z <= HS; z += .05) { | |
| 155 | plot(fixed === 'x' ? L.half : t, fixed === 'x' ? t : L.half, z, ramp[clamp(Math.floor(level + (noise(t * .8 + c, z * 9) - .5) * 1.3), 0, 4)]); | |
| 156 | } | |
| 157 | } | |
| 158 | const shade = (x, y, z, r) => castShadow(x, y, z, r, 1, 0); | |
| 159 | function bake(s) { | |
| 160 | const row = s.sluice.y; | |
| 161 | lay(s); | |
| 162 | const w = wheelGeometry(); | |
| 163 | ground.fill(0); shadowMask.fill(0); staticShadow.fill(0); kinds.fill(0); | |
| 164 | tufts.length = 0; leaves.length = 0; | |
| 165 | zb.fill(-1e9); | |
| 166 | pix.fill(rgb(255, 255, 255)); | |
| 167 | for (let iy = 0; iy < H; iy++) for (let ix = 0; ix < W; ix++) { | |
| 168 | const g = unproject(ix + .5, iy + .5), i = iy * W + ix, c = groundColor(g.x, g.y, ix, iy); | |
| 169 | if (!c) continue; | |
| 170 | pix[i] = c; ground[i] = 1; | |
| 171 | zb[i] = (g.x + g.y) / R2 * COS; | |
| 172 | } | |
| 173 | groundZ.set(zb); | |
| 174 | face('y', 3, 2.3); face('x', 5, 1.2); | |
| 175 | for (let k = 0; k < L.top.length; k++) { | |
| 176 | const i = L.top[k], iy = Math.floor(i / W), ix = i % W; | |
| 177 | pix[i] = stoneAt(L.tile[k], ix, iy, 3); | |
| 178 | const g = groundAt(ix + .5, iy + .5, HS); | |
| 179 | zb[i] = (g.x + g.y) / R2 * COS + HS * SIN; | |
| 180 | } | |
| 181 | // the sluice: a stone trough west of the slab with a wooden gate at its head | |
| 182 | const sy = rowY(row), sw = L.e * .32; | |
| 183 | box(-L.half - 1.3, sy - sw, 0, -L.half, sy + sw, HS, PIER, 4); | |
| 184 | for (const side of [-1, 1]) capsule([-L.half - 1.2, sy + side * sw, 0], [-L.half - 1.2, sy + side * sw, HS + 1.1], .16, .14, () => WOOD, 'rough'); | |
| 185 | capsule([-L.half - 1.2, sy - sw, HS + 1], [-L.half - 1.2, sy + sw, HS + 1], .1, .1, () => WOOD, 'rough'); | |
| 186 | for (let x = -L.half - 1.2; x <= -L.half; x += .06) for (let y = sy - sw * .55; y <= sy + sw * .55; y += .06) plot(x, y, HS + .02, WATER_RAMP[3]); | |
| 187 | // the wheel's two stone piers | |
| 188 | for (const side of [-1, 1]) box(w.x - .45, w.y + side * w.reach - .3, 0, w.x + .45, w.y + side * w.reach + .3, w.z + .2, PIER, 7); | |
| 189 | bakeOcclusion(); | |
| 190 | for (let t = -L.half; t <= L.half; t += .5) { shade(L.half, t, HS, .5); shade(t, L.half, HS, .5); } | |
| 191 | shade(-L.half - 1.2, sy - sw, HS + 1.1, .3); shade(-L.half - 1.2, sy + sw, HS + 1.1, .3); | |
| 192 | for (let z = 0; z <= w.z; z += .5) for (const side of [-1, 1]) shade(w.x, w.y + side * w.reach, z, .45); | |
| 193 | staticShadow.set(shadowMask); | |
| 194 | applyShadows(groundZ); | |
| 195 | bakedPix.set(pix); | |
| 196 | bakedZ.set(zb); | |
| 197 | G.baked = `${s.seed}:${s.level}`; | |
| 198 | } | |
| 199 | ||
| 200 | /* ---------- the tile layer: drawn once per change and cached ---------- */ | |
| 201 | const layerPix = new Uint32Array(W * H), layerZ = new Float32Array(W * H); | |
| 202 | let waterPix = new Int32Array(0); | |
| 203 | const ARM = [(a, b, h) => Math.abs(a - .5) <= h && b <= .5, (a, b, h) => Math.abs(b - .5) <= h && a >= .5, (a, b, h) => Math.abs(a - .5) <= h && b >= .5, (a, b, h) => Math.abs(b - .5) <= h && a <= .5]; | |
| 204 | function inChannel(mask, a, b, h) { | |
| 205 | if (Math.abs(a - .5) <= h && Math.abs(b - .5) <= h) return true; | |
| 206 | for (let s = 0; s < 4; s++) if (mask & 1 << s && ARM[s](a, b, h)) return true; | |
| 207 | return false; | |
| 208 | } | |
| 209 | const line = (x0, y0, x1, y1) => { | |
| 210 | const n = Math.max(1, Math.ceil(Math.hypot(x1 - x0, y1 - y0))), pts = []; | |
| 211 | for (let i = 0; i <= n; i++) pts.push([Math.round(x0 + (x1 - x0) * i / n), Math.round(y0 + (y1 - y0) * i / n)]); | |
| 212 | return pts; | |
| 213 | }; | |
| 214 | function paintLayer() { | |
| 215 | const { size, e, half } = L, view = G.view; | |
| 216 | const masks = [], wet = new Set(M.flow(view).map(c => c.y * size + c.x)), water = []; | |
| 217 | for (let y = 0; y < size; y++) for (let x = 0; x < size; x++) { | |
| 218 | const t = view.tiles[y][x]; | |
| 219 | masks.push(t.type === 'blank' ? 0 : M.sides(t).reduce((m, s) => m | 1 << s, 0)); | |
| 220 | } | |
| 221 | pix.set(bakedPix); | |
| 222 | zb.set(bakedZ); | |
| 223 | for (let k = 0; k < L.top.length; k++) { | |
| 224 | const i = L.top[k], ix = i % W, iy = (i - ix) / W, n = L.tile[k], a = L.a[k], b = L.b[k], mask = masks[n]; | |
| 225 | if (a < .035 || a > .965 || b < .035 || b > .965) { pix[i] = STONE_RAMPS[n & 1][1]; continue; } | |
| 226 | if (view.tiles[Math.floor(n / size)][n % size].type === 'blank') { | |
| 227 | const dx = a - .5, dy = b - .5, r2 = dx * dx + dy * dy; | |
| 228 | pix[i] = r2 < .05 ? STONE_RAMPS[n & 1][clamp(Math.floor(3.4 + (-dx - dy) * 4), 2, 4)] : r2 < .08 && dx + dy > 0 ? STONE_RAMPS[n & 1][1] : stoneAt(n, ix, iy, 3); | |
| 229 | continue; | |
| 230 | } | |
| 231 | if (wet.has(n) && inChannel(mask, a, b, .21)) { | |
| 232 | pix[i] = WATER_RAMP[clamp(Math.floor(2.2 + (bayerAt(ix, iy) - .5) * 1.6 + ((a + b) * 6 % 1) * .8), 1, 4)]; | |
| 233 | water.push(i); | |
| 234 | } else if (!wet.has(n) && inChannel(mask, a, b, .1)) pix[i] = GROOVE_RAMP[(ix + iy & 1) && bayerAt(ix, iy) > .5 ? 1 : 0]; | |
| 235 | else if (inChannel(mask, a, b, wet.has(n) ? .26 : .15)) pix[i] = STONE_RAMPS[n & 1][1]; | |
| 236 | else pix[i] = stoneAt(n, ix, iy, 3); | |
| 237 | } | |
| 238 | waterPix = Int32Array.from(water); | |
| 239 | shadeEdges(); | |
| 240 | if (G.keyed && !G.hold) { | |
| 241 | const c = [[0, 0], [1, 0], [1, 1], [0, 1]].map(([u, v]) => { | |
| 242 | const p = project(-half + (G.cx + u) * e, -half + (G.cy + v) * e, HS + .02); | |
| 243 | return [Math.round(p.sx), Math.round(p.sy)]; | |
| 244 | }); | |
| 245 | for (const [dy, color] of [[1, INK_PIX], [0, SIGNAL_PIX]]) for (let s = 0; s < 4; s++) { | |
| 246 | const [x0, y0] = c[s], [x1, y1] = c[(s + 1) % 4]; | |
| 247 | drawDots(line(x0, y0 + dy, x1, y1 + dy), color); | |
| 248 | } | |
| 249 | } | |
| 250 | layerPix.set(pix); | |
| 251 | layerZ.set(zb); | |
| 252 | G.layers++; | |
| 253 | G.dirty = false; | |
| 254 | } | |
| 255 | ||
| 256 | /* ---------- per frame: the water's shimmer and the turning wheel ---------- */ | |
| 257 | // glints stepped at 8 fps; held still under reduced motion | |
| 258 | function shimmer() { | |
| 259 | const step = STILL ? 0 : Math.floor(G.time * WIND_FPS); | |
| 260 | for (let k = 0; k < waterPix.length; k++) { | |
| 261 | const i = waterPix[k]; | |
| 262 | if (((i % W) * 7 + Math.floor(i / W) * 13 + step * 5) % 19 === 0) pix[i] = GLINT_PIX; | |
| 263 | } | |
| 264 | } | |
| 265 | function drawWheel() { | |
| 266 | const w = wheelGeometry(), spokes = 6, a0 = G.spin; | |
| 267 | const paddle = G.fed ? WATER : WOOD; | |
| 268 | const rim = k => [w.x + Math.cos(a0 + k * Math.PI * 2 / 12) * w.R, w.y, w.z + Math.sin(a0 + k * Math.PI * 2 / 12) * w.R]; | |
| 269 | const hub = [w.x, w.y, w.z]; | |
| 270 | for (let k = 0; k < 12; k++) dynamicAdd(rim(k), rim(k + 1), .17, .17, () => WOOD); | |
| 271 | for (let k = 0; k < spokes; k++) { | |
| 272 | const r = rim(k * 2); | |
| 273 | dynamicAdd(hub, r, .13, .13, () => WOOD); | |
| 274 | dynamicAdd([r[0], w.y - w.reach * .8, r[2]], [r[0], w.y + w.reach * .8, r[2]], .2, .2, () => paddle); | |
| 275 | } | |
| 276 | dynamicAdd([w.x, w.y - w.reach, w.z], [w.x, w.y + w.reach, w.z], .24, .24, () => WOOD); | |
| 277 | } | |
| 278 | function draw() { | |
| 279 | if (G.dirty) paintLayer(); | |
| 280 | pix.set(layerPix); | |
| 281 | zb.set(layerZ); | |
| 282 | pendingShadows.length = 0; | |
| 283 | shimmer(); | |
| 284 | drawWheel(); | |
| 285 | flushShadows(); | |
| 286 | applyShadows(bakedZ); | |
| 287 | ctx.putImageData(image, 0, 0); | |
| 288 | } | |
| 289 | ||
| 290 | /* ---------- game ---------- */ | |
| 291 | // show a state: redo the baked scene when the level's grid is new, and say whether the rules put water at the wheel | |
| 292 | function show(state) { | |
| 293 | G.view = state; | |
| 294 | G.fed = M.reaches(state); | |
| 295 | if (G.baked !== `${state.seed}:${state.level}`) bake(state); | |
| 296 | G.dirty = true; | |
| 297 | } | |
| 298 | function cursorTo(x, y) { | |
| 299 | if (G.cx === x && G.cy === y) return; | |
| 300 | G.cx = x; G.cy = y; | |
| 301 | G.dirty = true; | |
| 302 | } | |
| 303 | function newLevel() { | |
| 304 | cursorTo(0, P.sluice.y); | |
| 305 | show(P); | |
| 306 | renderHud(); | |
| 307 | } | |
| 308 | function finish() { | |
| 309 | G.noteUntil = 0; | |
| 310 | setText('status', `${P.line} · tap or press a key to play again`); | |
| 311 | if (P.cleared >= P.last && P.over <= M.WIN_OVER) sound(523, .5, 'sine'); | |
| 312 | else sound(90, .5, 'sawtooth'); | |
| 313 | } | |
| 314 | function turnAt(x, y) { | |
| 315 | ensureAudio(); | |
| 316 | if (G.hold > 0 || P.status !== 'playing') return; | |
| 317 | const before = P, r = M.turn(P, x, y); | |
| 318 | if (!r.ok) { note(REFUSED[r.why] || 'cannot turn that'); sound(110, .12, 'sawtooth'); return; } | |
| 319 | P = r.state; | |
| 320 | sound(300 + (x + y) % 5 * 40, .07, 'triangle'); | |
| 321 | if (r.event === 'cleared') { | |
| 322 | // the rules lay the next level at once; the level just won stays on show for a second, its tile turned | |
| 323 | const view = structuredClone(before); | |
| 324 | view.tiles[y][x].rot = (view.tiles[y][x].rot + 1) % 4; | |
| 325 | G.hold = FLOURISH; | |
| 326 | show(view); | |
| 327 | note(`Level ${before.level} cleared in ${before.moves + 1} moves, par ${before.par}: the wheel turns`); | |
| 328 | sound(660, .3, 'sine', .08); | |
| 329 | return; | |
| 330 | } | |
| 331 | show(P); | |
| 332 | renderHud(); | |
| 333 | if (r.event === 'over') { finish(); return; } | |
| 334 | note(`${describe(x, y)} now; water fills ${M.flow(P).length} of ${P.size * P.size} tiles`); | |
| 335 | } | |
| 336 | function restart() { | |
| 337 | P = fresh(); G.hold = 0; G.keyed = false; | |
| 338 | newLevel(); | |
| 339 | note('tap a tile to turn it, or use the arrow keys and Space'); | |
| 340 | } | |
| 341 | function update(dt) { | |
| 342 | G.time += dt; | |
| 343 | if (G.fed && !STILL) G.spin += dt * 1.8; | |
| 344 | if (G.hold > 0) { | |
| 345 | G.hold -= dt; | |
| 346 | if (G.hold <= 0) { G.hold = 0; newLevel(); note(P.line); } | |
| 347 | return; | |
| 348 | } | |
| 349 | if (P.status !== 'playing' || G.time < G.noteUntil) return; | |
| 350 | setText('status', P.line); | |
| 351 | } | |
| 352 | ||
| 353 | /* ---------- input ---------- */ | |
| 354 | const point = e => { const r = canvas.getBoundingClientRect(); return [(e.clientX - r.left) / r.width * W, (e.clientY - r.top) / r.height * H]; }; | |
| 355 | canvas.addEventListener('pointerdown', e => { | |
| 356 | canvas.focus({ preventScroll: true }); | |
| 357 | ensureAudio(); | |
| 358 | if (P.status !== 'playing') { restart(); return; } | |
| 359 | const at = tileAt(...point(e)); | |
| 360 | G.keyed = false; | |
| 361 | G.dirty = true; | |
| 362 | if (!at) { note('tap a stone tile to turn it'); return; } | |
| 363 | cursorTo(...at); | |
| 364 | turnAt(...at); | |
| 365 | }); | |
| 366 | const MOVES = { ArrowLeft: [-1, 0], ArrowRight: [1, 0], ArrowUp: [0, -1], ArrowDown: [0, 1] }; | |
| 367 | canvas.addEventListener('keydown', e => { | |
| 368 | if (e.ctrlKey || e.metaKey || e.altKey || e.key === 'Tab') return; | |
| 369 | const move = MOVES[e.key], pick = e.key === ' ' || e.key === 'Enter'; | |
| 370 | if (!move && !pick) return; | |
| 371 | e.preventDefault(); | |
| 372 | if (e.repeat && pick) return; | |
| 373 | if (P.status !== 'playing') { if (!e.repeat) restart(); return; } | |
| 374 | if (G.hold > 0) return; | |
| 375 | if (!G.keyed) { G.keyed = true; G.dirty = true; } | |
| 376 | if (move) { | |
| 377 | cursorTo(clamp(G.cx + move[0], 0, P.size - 1), clamp(G.cy + move[1], 0, P.size - 1)); | |
| 378 | note(describe(G.cx, G.cy)); | |
| 379 | return; | |
| 380 | } | |
| 381 | turnAt(G.cx, G.cy); | |
| 382 | }); | |
| 383 | ||
| 384 | /* ---------- sound ---------- */ | |
| 385 | let audio = null, muted = false; | |
| 386 | function ensureAudio() { | |
| 387 | if (audio) { if (audio.state === 'suspended') audio.resume(); return; } | |
| 388 | const AC = window.AudioContext || window.webkitAudioContext; | |
| 389 | if (AC) audio = new AC(); | |
| 390 | } | |
| 391 | function sound(freq, life, type, delay = 0) { | |
| 392 | if (!audio || muted) return; | |
| 393 | const now = audio.currentTime + delay, gain = audio.createGain(), osc = audio.createOscillator(); | |
| 394 | osc.type = type; | |
| 395 | osc.frequency.setValueAtTime(freq, now); | |
| 396 | gain.gain.setValueAtTime(.0001, now); | |
| 397 | gain.gain.exponentialRampToValueAtTime(.07, now + .01); | |
| 398 | gain.gain.exponentialRampToValueAtTime(.0005, now + life); | |
| 399 | osc.connect(gain).connect(audio.destination); | |
| 400 | osc.start(now); osc.stop(now + life + .02); | |
| 401 | } | |
| 402 | const muteButton = document.getElementById('mute'); | |
| 403 | muteButton.addEventListener('click', () => { | |
| 404 | muted = !muted; | |
| 405 | muteButton.textContent = muted ? 'sound off' : 'sound on'; | |
| 406 | muteButton.setAttribute('aria-pressed', String(muted)); | |
| 407 | }); | |
| 408 | ||
| 409 | /* ---------- fit and boot ---------- */ | |
| 410 | addEventListener('resize', fit); | |
| 411 | fit(); | |
| 412 | newLevel(); | |
| 413 | note('tap a tile to turn it, or use the arrow keys and Space'); | |
| 414 | canvas.focus({ preventScroll: true }); | |
| 415 | let last = performance.now(); | |
| 416 | function frame(now) { | |
| 417 | update(Math.min(.04, (now - last) / 1000)); | |
| 418 | last = now; | |
| 419 | draw(); | |
| 420 | requestAnimationFrame(frame); | |
| 421 | } | |
| 422 | requestAnimationFrame(frame); | |
| 423 | </script> | |
| 424 | </body> | |
| 425 | </html> |
mill-race/page.test.mjs added +115
| @@ -0,0 +1,115 @@ | ||
| 1 | import { test, expect } from 'bun:test'; | |
| 2 | import { boot } from '../playtest.mjs'; | |
| 3 | ||
| 4 | const turnsOf = g => g.read('P.scramble').flatMap(q => Array(q.undo).fill([q.x, q.y])); | |
| 5 | const tapTile = (g, [x, y]) => g.tap(...g.read(`centre(${x}, ${y})`)); | |
| 6 | ||
| 7 | test('a tap on a tile turns it once, counts the move and announces it', () => { | |
| 8 | const g = boot('mill-race', { seed: 3 }); | |
| 9 | const [x, y] = turnsOf(g)[0]; | |
| 10 | const rot = g.read(`P.tiles[${y}][${x}].rot`); | |
| 11 | tapTile(g, [x, y]); | |
| 12 | expect(g.read(`P.tiles[${y}][${x}].rot`)).toBe((rot + 1) % 4); | |
| 13 | expect(g.el('moves').textContent).toBe('1'); | |
| 14 | expect(g.el('par').textContent).toBe(String(g.read('P.par'))); | |
| 15 | expect(g.el('level').textContent).toBe('1 of 5'); | |
| 16 | expect(g.el('status').textContent).toContain(`Column ${x + 1}, row ${y + 1}`); | |
| 17 | }); | |
| 18 | ||
| 19 | test('arrows move a cursor, say what is under it, and Space turns that tile', () => { | |
| 20 | const g = boot('mill-race', { seed: 3 }); | |
| 21 | g.press('ArrowDown'); | |
| 22 | const [cx, cy] = g.read('[G.cx, G.cy]'); | |
| 23 | expect(cx).toBe(0); | |
| 24 | expect(g.el('status').textContent).toContain(`Column 1, row ${cy + 1}`); | |
| 25 | const rot = g.read(`P.tiles[${cy}][${cx}].rot`), type = g.read(`P.tiles[${cy}][${cx}].type`); | |
| 26 | g.press(' '); | |
| 27 | expect(g.read(`P.tiles[${cy}][${cx}].rot`)).toBe(type === 'blank' ? rot : (rot + 1) % 4); | |
| 28 | expect(g.read('P.moves')).toBe(type === 'blank' ? 0 : 1); | |
| 29 | }); | |
| 30 | ||
| 31 | test('a solid stone block refuses the turn and says so', () => { | |
| 32 | const g = boot('mill-race', { seed: 3 }); | |
| 33 | const at = g.read('(() => { for (let y = 0; y < P.size; y++) for (let x = 0; x < P.size; x++) if (P.tiles[y][x].type === "blank") return [x, y]; })()'); | |
| 34 | expect(at).toBeDefined(); | |
| 35 | tapTile(g, at); | |
| 36 | expect(g.read('P.moves')).toBe(0); | |
| 37 | expect(g.el('status').textContent).toContain('solid stone'); | |
| 38 | }); | |
| 39 | ||
| 40 | test('the tile layer is drawn once per change, not once per frame', () => { | |
| 41 | const g = boot('mill-race', { seed: 3 }); | |
| 42 | g.run(2); | |
| 43 | const base = g.read('G.layers'); | |
| 44 | g.run(3); | |
| 45 | expect(g.read('G.layers')).toBe(base); // seconds of frames, no redraw | |
| 46 | tapTile(g, turnsOf(g)[0]); | |
| 47 | g.run(1); | |
| 48 | expect(g.read('G.layers')).toBe(base + 1); // one turn, one redraw | |
| 49 | g.press('ArrowRight'); | |
| 50 | g.run(1); | |
| 51 | expect(g.read('G.layers')).toBeGreaterThan(base + 1); // the cursor is a change too | |
| 52 | }); | |
| 53 | ||
| 54 | test('the wheel turns only once the rules put water at it, and the picture moves while it does', () => { | |
| 55 | const g = boot('mill-race', { seed: 3 }); | |
| 56 | g.run(1); | |
| 57 | expect(g.read('G.fed')).toBe(false); | |
| 58 | const before = g.read('G.spin'); | |
| 59 | g.run(1); | |
| 60 | expect(g.read('G.spin')).toBe(before); // dry: it holds still | |
| 61 | const turns = turnsOf(g); | |
| 62 | for (const t of turns) tapTile(g, t); | |
| 63 | expect(g.read('G.fed')).toBe(true); | |
| 64 | expect(g.read('G.hold')).toBeGreaterThan(0); // the cleared level stays on show | |
| 65 | expect(g.el('status').textContent).toContain('cleared'); | |
| 66 | const spun = g.read('G.spin'); | |
| 67 | g.step(5); | |
| 68 | expect(g.read('G.spin')).toBeGreaterThan(spun); | |
| 69 | g.run(2); | |
| 70 | expect(g.el('level').textContent).toBe('2 of 5'); // then the next level is laid, dry again | |
| 71 | expect(g.read('G.fed')).toBe(false); | |
| 72 | }); | |
| 73 | ||
| 74 | test('the wet cells drawn are the cells the rules report, and no dry tile is painted', () => { | |
| 75 | const g = boot('mill-race', { seed: 3 }); | |
| 76 | for (const t of turnsOf(g).slice(0, 2)) tapTile(g, t); | |
| 77 | g.run(.2); | |
| 78 | const drawn = g.read(`(() => { const on = new Set(waterPix), s = new Set(), dry = []; const wet = new Set(MillRace.flow(G.view).map(c => c.y * L.size + c.x)); | |
| 79 | for (let k = 0; k < L.top.length; k++) if (on.has(L.top[k])) { s.add(L.tile[k]); if (!wet.has(L.tile[k])) dry.push(k); } | |
| 80 | return { tiles: [...s].sort((a, b) => a - b), dry: dry.length, pix: on.size }; })()`); | |
| 81 | const want = g.read('MillRace.flow(G.view).map(c => c.y * L.size + c.x).sort((a, b) => a - b)'); | |
| 82 | expect(want.length).toBeGreaterThan(0); | |
| 83 | expect(drawn.dry).toBe(0); | |
| 84 | expect(drawn.pix).toBeGreaterThan(0); | |
| 85 | expect(drawn.tiles.every(n => want.includes(n))).toBe(true); | |
| 86 | // blank tiles hold no channel, so every reported wet tile with a channel shows water | |
| 87 | const blank = g.read(`MillRace.flow(G.view).filter(c => G.view.tiles[c.y][c.x].type === 'blank').length`); | |
| 88 | expect(drawn.tiles.length).toBe(want.length - blank); | |
| 89 | }); | |
| 90 | ||
| 91 | test('mute toggles its label and aria-pressed', () => { | |
| 92 | const g = boot('mill-race', { seed: 3 }); | |
| 93 | g.el('mute').fire('click'); | |
| 94 | expect(g.el('mute').textContent).toBe('sound off'); | |
| 95 | expect(g.el('mute').attrs['aria-pressed']).toBe('true'); | |
| 96 | }); | |
| 97 | ||
| 98 | test('a lost race and a won one end on different sounds, decided from state', () => { | |
| 99 | const g = boot('mill-race', { seed: 3 }); | |
| 100 | const played = over => g.read(`(() => { const calls = []; const keep = sound; sound = (...a) => calls.push(a); | |
| 101 | P = Object.assign({}, P, { status: 'over', cleared: P.last, over: ${over}, line: 'x' }); finish(); sound = keep; return calls; })()`); | |
| 102 | const won = played(0), lost = played(99); | |
| 103 | expect(won).toEqual([[523, .5, 'sine']]); | |
| 104 | expect(lost).toEqual([[90, .5, 'sawtooth']]); | |
| 105 | }); | |
| 106 | ||
| 107 | test('the page source says how to play, keeps the 44px mute target, holds the rules to four lines and names no .mp3', async () => { | |
| 108 | const html = await Bun.file(new URL('./index.html', import.meta.url)).text(); | |
| 109 | expect(html).toMatch(/<canvas id="c"[^>]*aria-label="[^"]*tap a tile[^"]*arrow keys[^"]*Space/); | |
| 110 | expect(html).toMatch(/min-height: 44px; min-width: 44px/); | |
| 111 | expect(html).toMatch(/id="mute"[^>]*aria-pressed="false"/); | |
| 112 | expect(html).not.toContain('.mp3'); | |
| 113 | const rules = html.match(/rules: \[([\s\S]*?)\]/)[1].split('\n').filter(l => l.trim()); | |
| 114 | expect(rules.length).toBeLessThanOrEqual(4); | |
| 115 | }); |
mill-race/race.js +1 −1
| @@ -160,6 +160,6 @@ | ||
| 160 | 160 | return { ok: true, why: '', event: '', state: refresh(n) }; |
| 161 | 161 | } |
| 162 | 162 | |
| 163 | const api = { LAST_LEVEL, WIN_OVER, TYPES, sizeOf, sides, opens, start, turn, flow }; | |
| 163 | const api = { LAST_LEVEL, WIN_OVER, TYPES, sizeOf, sides, opens, start, turn, flow, reaches }; | |
| 164 | 164 | if (typeof module !== 'undefined') module.exports = api; else root.MillRace = api; |
| 165 | 165 | })(typeof window !== 'undefined' ? window : globalThis); |
mill-race/race.test.mjs +9 −1
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | import { test, expect } from 'bun:test'; |
| 2 | import { LAST_LEVEL, WIN_OVER, sizeOf, sides, opens, start, turn, flow } from './race.js'; | |
| 2 | import { LAST_LEVEL, WIN_OVER, sizeOf, sides, opens, start, turn, flow, reaches as wheelFed } from './race.js'; | |
| 3 | 3 | |
| 4 | 4 | const tile = (type, rot = 0) => ({ type, rot }); |
| 5 | 5 | // a hand-made grid (plain data): rows of tiles, sluice west of the top-left cell, wheel east of the last cell of the top row |
| @@ -124,3 +124,11 @@ test('the last level ends the game: a win line at or under the stated total over | ||
| 124 | 124 | expect(loss.state.line).toContain(`${WIN_OVER + 1} moves over par`); |
| 125 | 125 | expect(turn(loss.state, 0, 0)).toMatchObject({ ok: false, why: 'over' }); |
| 126 | 126 | }); |
| 127 | ||
| 128 | test('the exported reaches agrees with the water: false while broken, true once joined', () => { | |
| 129 | const broken = hand([[tile('straight', 1), tile('straight', 0), tile('straight', 1)], [blank(), blank(), blank()], [blank(), blank(), blank()]]); | |
| 130 | expect(wheelFed(broken)).toBe(false); | |
| 131 | const joined = { ...broken, tiles: broken.tiles.map((row, y) => row.map((t, x) => (x === 1 && y === 0 ? tile('straight', 1) : t))) }; | |
| 132 | expect(wheelFed(joined)).toBe(true); | |
| 133 | expect(wheelFed(start(5, 1))).toBe(false); | |
| 134 | }); |
og/home.png
Binary file not shown.
og/mill-race.png added
Binary file not shown.
rounds.mjs +8
| @@ -174,6 +174,14 @@ export const ROUNDS = { | ||
| 174 | 174 | else if (ds) g.press(ds > 0 ? 'ArrowDown' : 'ArrowUp', .02); |
| 175 | 175 | else g.press(String(want + 1), .02); |
| 176 | 176 | } }, |
| 177 | // turn the tiles of the path the level was laid from, as many quarter turns as each needs (the rules' own `scramble` and `undo`), one tap per beat | |
| 178 | 'mill-race': { over: /play again/, outcome: /The wheel turns: 5 levels cleared, \d+ moves? over par/, beat: g => { | |
| 179 | if (g.read('P.status') !== 'playing' || g.read('G.hold') > 0) { g.run(.3); return; } | |
| 180 | const level = g.read('P.level'); | |
| 181 | if (g.plan?.level !== level) g.plan = { level, turns: g.read('P.scramble').flatMap(q => Array(q.undo).fill([q.x, q.y])) }; | |
| 182 | const at = g.plan.turns[g.read('P.moves')]; | |
| 183 | g.tap(...g.read(`centre(${at[0]}, ${at[1]})`)); | |
| 184 | } }, | |
| 177 | 185 | // hop along the route the rules planned for this pond, one arrow key per pad |
| 178 | 186 | 'lily-pond': { over: /play again/, outcome: /Pond cleared: 6 levels, \d+ retries?/, beat: g => { |
| 179 | 187 | const [a, b] = g.read('[P.route[P.hops], P.route[P.hops + 1]]'); |
serve.test.mjs +7
| @@ -663,3 +663,10 @@ test('the home page loads the town: /ramp.js, /town.js and /plaza.js are served | ||
| 663 | 663 | expect(list.split('<li ').length - 1).toBe(games.length); |
| 664 | 664 | for (const g of games) expect(list).toContain(`<a href="${g.id}/">`); |
| 665 | 665 | }); |
| 666 | ||
| 667 | test('Mill Race and its rules script are served without a track', async () => { | |
| 668 | const page = await fetch(base + '/mill-race/'); | |
| 669 | expect(page.status).toBe(200); | |
| 670 | expect(page.headers.get('content-type')).toBe('text/html; charset=utf-8'); | |
| 671 | expect((await fetch(base + '/mill-race/race.js')).status).toBe(200); | |
| 672 | }); |
shell.js +1
| @@ -63,6 +63,7 @@ window.PixelPlays = (() => { | ||
| 63 | 63 | { id: 'bunting', title: 'Bunting', blurb: 'Hang the flags for the fete so no two neighbours match', kind: 'Strategy', input: 'Tap', accent: '#3d7f6e', music: false, forged: true }, |
| 64 | 64 | { id: 'rain-barrel', title: 'Rain Barrel', blurb: 'Slide the barrel under each spout before the yard floods', kind: 'Catching', input: 'Tap', accent: '#5f86a3', music: false, forged: true }, |
| 65 | 65 | { id: 'quilt', title: 'Patchwork Quilt', blurb: 'Turn and swap the patches until every stitch lines up', kind: 'Puzzle', input: 'Tap', accent: '#b5656b', music: false, forged: true }, |
| 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 }, | |
| 66 | 67 | ]; |
| 67 | 68 | const MUSIC_VOLUME = .4; |
| 68 | 69 | const CREDITS = [ |
new above
offline