Public: anyone can read this; the crew can change it.
Migrated from github.com/amirmushichge/pixel-plays
Add Window Washer: squeegee a grimy window clean before the cradle drops a floor #38
Check · Review · Land · Live done — Landed and live in production.
quill/pixel-plays:quill/30-add-window-washer-squeegee-a-grimy-windo → main 3 commits fork 3 threads · 3 unresolved
7 files
changed +651
−1
README.md +2 −1
| @@ -25,6 +25,7 @@ Concept by [abhi](https://x.com/awwbhi2). Game by [AmirMushich](https://x.com/Am | ||
| 25 | 25 | | [](row-home/) | Race a rowing boat across the river against a bot: stroke in rhythm with the glide, too early catches a crab, hold left or right to steer against the current (forged by the crew on the engine and shell above, not by the original creators) | Tap or space to stroke; hold left/right | |
| 26 | 26 | | [](orchard/) | Catch falling apples in a basket: ripe 1, golden 3, a wasp-bitten brown one costs a life, three lives end the round (forged by the crew on the engine and shell above, not by the original creators) | Drag; left/right arrows | |
| 27 | 27 | | [](lighthouse/) | A 90-second night: sweep the lighthouse beam so boats drifting in towards the rocks are held in the light for a second and turn away, +1 each; three boats lost on the rocks end the night early (forged by the crew on the engine and shell above, not by the original creators) | Drag; hold left/right | |
| 28 | | [](window-washer/) | Five grimy windows, twelve seconds each: wipe the glass clean with a squeegee before the cradle drops a floor, bird droppings need two passes, score the percentage clean (forged by the crew on the engine and shell above, not by the original creators) | Drag; arrows + space | | |
| 28 | 29 | | [](market-scale/) | Five customers, 15 seconds each: weigh fruit onto a hanging pan scale to match the weight on the chalk slate, exact scores 3 and within 50 g scores 1 (forged by the crew on the engine and shell above, not by the original creators) | Drag or tap fruit, tap the bell; arrows + enter, space | |
| 29 | 30 | |
| 30 | 31 | ## Run |
| @@ -39,7 +40,7 @@ Then open http://127.0.0.1:5300/. `PORT` and `HOST` override the port and addres | ||
| 39 | 40 | On the forge, `forge.json` runs `bun serve.mjs` with health check `/`. The app keeps no state, so it does not need `FORGE_APP_DATA`. `bun test` checks that the server answers every game page and track. |
| 40 | 41 | |
| 41 | 42 | ## Add a game |
| 42 | Create `<id>/index.html`, load `../shell.js`, call `PixelPlays.mount({ id, rules })`, set `window.PP_SCENE`, load `../engine.js`, then add the game to `GAMES` in `shell.js` with a one-line `blurb`; the home page lists every `GAMES` entry (a static fallback list for no-JS is kept in sync by `serve.test.mjs`, so add the matching `<li>` to `index.html` too). The page needs a `#mute` button. Put its background track at `<id>/<id>.mp3`, or set `music: false` on its `GAMES` entry when it has none (Stone Skip, Kite Hill, Paper Route, Harbour Crane, Pizza Toss, Sled Run, Pottery Wheel, Row Home, Orchard, Lighthouse and Market Scale play on their own sound effects). Rules that need testing go in a plain script with a `.test.mjs` beside it, as in `kite-hill/model.js`. | |
| 43 | Create `<id>/index.html`, load `../shell.js`, call `PixelPlays.mount({ id, rules })`, set `window.PP_SCENE`, load `../engine.js`, then add the game to `GAMES` in `shell.js` with a one-line `blurb`; the home page lists every `GAMES` entry (a static fallback list for no-JS is kept in sync by `serve.test.mjs`, so add the matching `<li>` to `index.html` too). The page needs a `#mute` button. Put its background track at `<id>/<id>.mp3`, or set `music: false` on its `GAMES` entry when it has none (Stone Skip, Kite Hill, Paper Route, Harbour Crane, Pizza Toss, Sled Run, Pottery Wheel, Row Home, Orchard, Lighthouse, Market Scale and Window Washer play on their own sound effects). Rules that need testing go in a plain script with a `.test.mjs` beside it, as in `kite-hill/model.js`. | |
| 43 | 44 | |
| 44 | 45 | - `shell.js` provides the game menu, the Rules popup, the credits and the background music. |
| 45 | 46 | - `engine.js` provides the camera, depth buffer, colour ramps, shadows, edges, figures, trees and wind as globals. |
index.html +1
| @@ -40,6 +40,7 @@ footer a { display: inline; padding: 0; font-weight: inherit; text-decoration: u | ||
| 40 | 40 | <li><a href="row-home/">Row Home</a><span>Race a rowing boat across the river against a bot: stroke in rhythm with the glide, too early catches a crab</span><span>Game forged by the crew</span></li> |
| 41 | 41 | <li><a href="orchard/">Orchard</a><span>Catch falling apples in a basket: ripe 1, golden 3, a wasp-bitten brown one costs a life</span><span>Game forged by the crew</span></li> |
| 42 | 42 | <li><a href="lighthouse/">Lighthouse</a><span>Sweep the lighthouse beam to turn boats away from the rocks: hold one in the light for a second, three wrecks end the night</span><span>Game forged by the crew</span></li> |
| 43 | <li><a href="window-washer/">Window Washer</a><span>Squeegee a grimy window clean before the cradle drops a floor: drag across it, bird droppings need two passes</span><span>Game forged by the crew</span></li> | |
| 43 | 44 | <li><a href="market-scale/">Market Scale</a><span>Weigh fruit onto a hanging pan scale to match each order: exact scores 3, within 50 g scores 1</span><span>Game forged by the crew</span></li> |
| 44 | 45 | </ul> |
| 45 | 46 | <footer>Concept by <a href="https://x.com/awwbhi2">abhi</a>. Game by <a href="https://x.com/AmirMushich">AmirMushich</a>. Visual reference for the style: shawn.'s <a href="https://x.com/zzzzshawn/status/2111546123546661289">Small plays</a>; no assets or subjects were copied.</footer> |
serve.test.mjs +7
| @@ -66,6 +66,13 @@ test('Orchard and its rules script are served without a track', async () => { | ||
| 66 | 66 | expect((await fetch(base + '/orchard/orchard.js')).status).toBe(200); |
| 67 | 67 | }); |
| 68 | 68 | |
| 69 | test('Window Washer and its rules script are served without a track', async () => { | |
| 70 | const page = await fetch(base + '/window-washer/'); | |
| 71 | expect(page.status).toBe(200); | |
| 72 | expect(page.headers.get('content-type')).toBe('text/html; charset=utf-8'); | |
| 73 | expect((await fetch(base + '/window-washer/glass.js')).status).toBe(200); | |
| 74 | }); | |
| 75 | ||
| 69 | 76 | test('stone skip is served without a track', async () => { |
| 70 | 77 | const page = await fetch(base + '/stone-skip/'); |
| 71 | 78 | expect(page.status).toBe(200); |
shell.js +1
| @@ -19,6 +19,7 @@ window.PixelPlays = (() => { | ||
| 19 | 19 | { id: 'row-home', title: 'Row Home', blurb: 'Race a rowing boat across the river against a bot: stroke in rhythm with the glide, too early catches a crab', music: false, forged: true }, |
| 20 | 20 | { id: 'orchard', title: 'Orchard', blurb: 'Catch falling apples in a basket: ripe 1, golden 3, a wasp-bitten brown one costs a life', music: false, forged: true }, |
| 21 | 21 | { id: 'lighthouse', title: 'Lighthouse', blurb: 'Sweep the lighthouse beam to turn boats away from the rocks: hold one in the light for a second, three wrecks end the night', music: false, forged: true }, |
| 22 | { id: 'window-washer', title: 'Window Washer', blurb: 'Squeegee a grimy window clean before the cradle drops a floor: drag across it, bird droppings need two passes', music: false, forged: true }, | |
| 22 | 23 | { id: 'market-scale', title: 'Market Scale', blurb: 'Weigh fruit onto a hanging pan scale to match each order: exact scores 3, within 50 g scores 1', music: false, forged: true }, |
| 23 | 24 | ]; |
| 24 | 25 | const MUSIC_VOLUME = .4; |
window-washer/glass.js added +131
| @@ -0,0 +1,131 @@ | ||
| 1 | 'use strict'; | |
| 2 | // The window washer rules: a grime mask per window, a squeegee that wipes the cells under it, | |
| 3 | // two-pass droppings, a wet streak that dries, and a cradle that drops a floor on a timer. | |
| 4 | // Plain numbers in, plain numbers out, no DOM. The page draws what this decides. | |
| 5 | (function (root) { | |
| 6 | const COLS = 37, ROWS = 26; // one glass cell is one canvas pixel | |
| 7 | const BLADE_W = 3, BLADE_H = 2; // the squeegee wipes cells within this many cells of its centre (7 x 5) | |
| 8 | const WET_TIME = 2.5; // seconds a wiped streak stays wet | |
| 9 | const FLOORS = 5, FLOOR_TIME = 12; // windows per round, seconds the cradle waits at each | |
| 10 | const RESTART_AFTER = .6; | |
| 11 | const CLEAN = 0, GRIME = 1, DROPPING = 2; | |
| 12 | ||
| 13 | const finite = v => (Number.isFinite(v) ? v : 0); | |
| 14 | const clamp = (v, a, b) => Math.max(a, Math.min(b, v)); | |
| 15 | // small seeded generator so a window is repeatable: state in, [value, state] out | |
| 16 | function roll(seed) { | |
| 17 | let t = (seed + 0x6D2B79F5) >>> 0; | |
| 18 | const out = Math.imul(t ^ (t >>> 15), 1 | t); | |
| 19 | const mixed = (out + Math.imul(out ^ (out >>> 7), 61 | out)) ^ out; | |
| 20 | return [((mixed ^ (mixed >>> 14)) >>> 0) / 4294967296, t]; | |
| 21 | } | |
| 22 | ||
| 23 | // a window covered edge to edge in grime, with `droppings` bird droppings that need two passes | |
| 24 | function newWindow(seed = 0, droppings = 3, cols = COLS, rows = ROWS) { | |
| 25 | const w = { | |
| 26 | cols, rows, | |
| 27 | grime: new Uint8Array(cols * rows).fill(GRIME), | |
| 28 | wet: new Float32Array(cols * rows), | |
| 29 | touched: new Uint8Array(cols * rows), | |
| 30 | }; | |
| 31 | let s = finite(seed) >>> 0; | |
| 32 | const next = () => { const [v, t] = roll(s); s = t; return v; }; | |
| 33 | for (let n = 0; n < droppings; n++) { | |
| 34 | const cx = 3 + Math.floor(next() * (cols - 6)), cy = 3 + Math.floor(next() * (rows - 6)), r = 1.5 + next() * 1.2; | |
| 35 | for (let y = Math.floor(cy - r); y <= cy + r; y++) for (let x = Math.floor(cx - r); x <= cx + r; x++) { | |
| 36 | if (x < 0 || y < 0 || x >= cols || y >= rows) continue; | |
| 37 | if ((x - cx) ** 2 + (y - cy) ** 2 <= r * r) w.grime[y * cols + x] = DROPPING; | |
| 38 | } | |
| 39 | } | |
| 40 | return w; | |
| 41 | } | |
| 42 | ||
| 43 | // the cell indices a squeegee sweeping from (x0, y0) to (x1, y1) passes over: the blade stamped at every cell step | |
| 44 | function cellsUnder(cols, rows, x0, y0, x1, y1) { | |
| 45 | x0 = finite(x0); y0 = finite(y0); x1 = finite(x1); y1 = finite(y1); | |
| 46 | const steps = Math.max(1, Math.ceil(Math.max(Math.abs(x1 - x0), Math.abs(y1 - y0)))); | |
| 47 | const seen = new Set(); | |
| 48 | for (let k = 0; k <= steps; k++) { | |
| 49 | const cx = Math.round(x0 + (x1 - x0) * k / steps), cy = Math.round(y0 + (y1 - y0) * k / steps); | |
| 50 | for (let y = Math.max(0, cy - BLADE_H); y <= Math.min(rows - 1, cy + BLADE_H); y++) { | |
| 51 | for (let x = Math.max(0, cx - BLADE_W); x <= Math.min(cols - 1, cx + BLADE_W); x++) seen.add(y * cols + x); | |
| 52 | } | |
| 53 | } | |
| 54 | return [...seen].sort((a, b) => a - b); | |
| 55 | } | |
| 56 | ||
| 57 | // a new stroke begins: a dropping already smeared by the last one can be wiped again | |
| 58 | function beginStroke(w) { w.touched.fill(0); } | |
| 59 | // wipe along a segment; each cell takes one layer off per stroke. Returns how many cells changed. | |
| 60 | function wipe(w, x0, y0, x1, y1) { | |
| 61 | let changed = 0; | |
| 62 | for (const i of cellsUnder(w.cols, w.rows, x0, y0, x1, y1)) { | |
| 63 | w.wet[i] = WET_TIME; | |
| 64 | if (w.touched[i]) continue; | |
| 65 | w.touched[i] = 1; | |
| 66 | if (w.grime[i] > CLEAN) { w.grime[i]--; changed++; } | |
| 67 | } | |
| 68 | return changed; | |
| 69 | } | |
| 70 | function dry(w, dt) { | |
| 71 | const d = Math.max(0, finite(dt)); | |
| 72 | for (let i = 0; i < w.wet.length; i++) if (w.wet[i] > 0) w.wet[i] = Math.max(0, w.wet[i] - d); | |
| 73 | } | |
| 74 | // whole-number percentage of cells that are clean; 100 only when none is left | |
| 75 | function percentClean(w) { | |
| 76 | let clean = 0; | |
| 77 | for (let i = 0; i < w.grime.length; i++) if (w.grime[i] === CLEAN) clean++; | |
| 78 | return Math.floor(clean * 100 / w.grime.length); | |
| 79 | } | |
| 80 | ||
| 81 | function newRound(best = 0, seed = 1) { | |
| 82 | const s = { seed: finite(seed), phase: 'play', floor: 0, timer: 0, scores: [], best: Math.max(0, finite(best)), record: false }; | |
| 83 | s.window = newWindow(s.seed * 97, droppingsOn(0)); | |
| 84 | return s; | |
| 85 | } | |
| 86 | const droppingsOn = floor => 2 + floor; | |
| 87 | const total = s => s.scores.reduce((a, b) => a + b, 0); | |
| 88 | // the running total shown on the HUD: finished windows plus the one in hand | |
| 89 | const running = s => total(s) + (s.phase === 'play' ? percentClean(s.window) : 0); | |
| 90 | const timeLeft = s => (s.phase === 'play' ? Math.max(0, FLOOR_TIME - s.timer) : 0); | |
| 91 | ||
| 92 | // the squeegee only works while the round is on | |
| 93 | function stroke(s, x0, y0, x1, y1) { return s.phase === 'play' ? wipe(s.window, x0, y0, x1, y1) : 0; } | |
| 94 | function begin(s) { if (s.phase === 'play') beginStroke(s.window); } | |
| 95 | ||
| 96 | // time passes: windows dry, and every FLOOR_TIME seconds the cradle scores the window and drops a floor | |
| 97 | function advance(s, dt) { | |
| 98 | const events = []; | |
| 99 | const d = clamp(finite(dt), 0, FLOOR_TIME); | |
| 100 | if (s.phase === 'over') { s.timer += d; return events; } | |
| 101 | dry(s.window, d); | |
| 102 | s.timer += d; | |
| 103 | if (s.timer < FLOOR_TIME) return events; | |
| 104 | const percent = percentClean(s.window); | |
| 105 | s.window.wet.fill(0); // the scored window is left dry, whatever was wiped in its last seconds | |
| 106 | s.scores.push(percent); | |
| 107 | events.push({ type: 'score', floor: s.floor, percent }); | |
| 108 | s.floor++; | |
| 109 | s.timer = 0; | |
| 110 | if (s.floor >= FLOORS) { | |
| 111 | s.phase = 'over'; | |
| 112 | const sum = total(s); | |
| 113 | if (sum > s.best) { s.best = sum; s.record = true; } | |
| 114 | events.push({ type: 'over', total: sum, record: s.record }); | |
| 115 | } else { | |
| 116 | s.window = newWindow(s.seed * 97 + s.floor, droppingsOn(s.floor)); | |
| 117 | events.push({ type: 'drop', floor: s.floor }); | |
| 118 | } | |
| 119 | return events; | |
| 120 | } | |
| 121 | // a tap after the round has been over for a moment starts the next one; otherwise null | |
| 122 | function again(s) { | |
| 123 | return s.phase === 'over' && s.timer > RESTART_AFTER ? newRound(s.best, s.seed + 1) : null; | |
| 124 | } | |
| 125 | ||
| 126 | const api = { | |
| 127 | COLS, ROWS, BLADE_W, BLADE_H, WET_TIME, FLOORS, FLOOR_TIME, RESTART_AFTER, CLEAN, GRIME, DROPPING, | |
| 128 | droppingsOn, newWindow, cellsUnder, beginStroke, wipe, dry, percentClean, newRound, total, running, timeLeft, begin, stroke, advance, again, | |
| 129 | }; | |
| 130 | if (typeof module !== 'undefined') module.exports = api; else root.Glass = api; | |
| 131 | })(typeof window !== 'undefined' ? window : globalThis); |
window-washer/glass.test.mjs added +105
| @@ -0,0 +1,105 @@ | ||
| 1 | import { test, expect } from 'bun:test'; | |
| 2 | import { COLS, ROWS, BLADE_W, BLADE_H, WET_TIME, FLOORS, FLOOR_TIME, newWindow, cellsUnder, beginStroke, wipe, dry, percentClean, newRound, running, begin, stroke, advance, again, total } from './glass.js'; | |
| 3 | ||
| 4 | const plain = () => newWindow(1, 0); | |
| 5 | const mid = Math.floor(ROWS / 2); | |
| 6 | ||
| 7 | test('a fresh window is all grime and scores zero', () => { | |
| 8 | const w = plain(); | |
| 9 | expect(w.grime.every(g => g === 1)).toBe(true); | |
| 10 | expect(percentClean(w)).toBe(0); | |
| 11 | }); | |
| 12 | ||
| 13 | test('one stroke across the middle cleans the cells under it and no others', () => { | |
| 14 | const w = plain(); | |
| 15 | const changed = wipe(w, 0, mid, COLS - 1, mid); | |
| 16 | const under = new Set(); | |
| 17 | for (let y = mid - BLADE_H; y <= mid + BLADE_H; y++) for (let x = 0; x < COLS; x++) under.add(y * COLS + x); | |
| 18 | expect(changed).toBe(under.size); | |
| 19 | for (let i = 0; i < w.grime.length; i++) expect(w.grime[i]).toBe(under.has(i) ? 0 : 1); | |
| 20 | expect(cellsUnder(COLS, ROWS, 0, mid, COLS - 1, mid).length).toBe(under.size); | |
| 21 | }); | |
| 22 | ||
| 23 | test('a stroke near the edge stays inside the glass', () => { | |
| 24 | expect(cellsUnder(COLS, ROWS, -20, -20, 2, 1).every(i => i >= 0 && i < COLS * ROWS)).toBe(true); | |
| 25 | expect(cellsUnder(COLS, ROWS, NaN, NaN, NaN, NaN).length).toBe((BLADE_W + 1) * (BLADE_H + 1)); | |
| 26 | }); | |
| 27 | ||
| 28 | test('a dropping needs two passes, one per stroke', () => { | |
| 29 | const w = newWindow(3, 1); | |
| 30 | const i = w.grime.indexOf(2); | |
| 31 | expect(i).toBeGreaterThan(-1); | |
| 32 | const x = i % COLS, y = Math.floor(i / COLS); | |
| 33 | beginStroke(w); | |
| 34 | wipe(w, x - 3, y, x + 3, y); | |
| 35 | wipe(w, x + 3, y, x - 3, y); // same stroke back and forth does not count twice | |
| 36 | expect(w.grime[i]).toBe(1); | |
| 37 | beginStroke(w); | |
| 38 | wipe(w, x - 3, y, x + 3, y); | |
| 39 | expect(w.grime[i]).toBe(0); | |
| 40 | }); | |
| 41 | ||
| 42 | test('a fully wiped window scores 100, and one cell short does not', () => { | |
| 43 | const w = plain(); | |
| 44 | for (let y = 0; y < ROWS; y += 2 * BLADE_H) wipe(w, 0, y, COLS - 1, y); | |
| 45 | wipe(w, 0, ROWS - 1, COLS - 1, ROWS - 1); | |
| 46 | expect(percentClean(w)).toBe(100); | |
| 47 | w.grime[0] = 1; | |
| 48 | expect(percentClean(w)).toBe(99); | |
| 49 | }); | |
| 50 | ||
| 51 | test('a wiped streak is wet, then dries', () => { | |
| 52 | const w = plain(); | |
| 53 | wipe(w, 5, 5, 10, 5); | |
| 54 | const i = 5 * COLS + 7; | |
| 55 | expect(w.wet[i]).toBe(WET_TIME); | |
| 56 | dry(w, WET_TIME / 2); | |
| 57 | expect(w.wet[i]).toBeCloseTo(WET_TIME / 2); | |
| 58 | dry(w, WET_TIME); | |
| 59 | expect(w.wet[i]).toBe(0); | |
| 60 | }); | |
| 61 | ||
| 62 | test('the cradle drops a floor every twelve seconds and scores the window', () => { | |
| 63 | const s = newRound(0, 1); | |
| 64 | stroke(s, 0, mid, COLS - 1, mid); | |
| 65 | const pct = percentClean(s.window); | |
| 66 | expect(pct).toBeGreaterThan(0); | |
| 67 | expect(advance(s, FLOOR_TIME - .5)).toEqual([]); | |
| 68 | expect(running(s)).toBe(pct); | |
| 69 | const events = advance(s, .5); | |
| 70 | expect(events.map(e => e.type)).toEqual(['score', 'drop']); | |
| 71 | expect(s.floor).toBe(1); | |
| 72 | expect(s.scores).toEqual([pct]); | |
| 73 | expect(percentClean(s.window)).toBe(0); | |
| 74 | }); | |
| 75 | ||
| 76 | test('five windows end the round and keep the best', () => { | |
| 77 | let s = newRound(10, 1); | |
| 78 | for (let f = 0; f < FLOORS; f++) { | |
| 79 | for (let y = 0; y < ROWS; y++) { begin(s); stroke(s, 0, y, COLS - 1, y); begin(s); stroke(s, 0, y, COLS - 1, y); } | |
| 80 | advance(s, FLOOR_TIME); | |
| 81 | } | |
| 82 | expect(s.phase).toBe('over'); | |
| 83 | expect(s.scores).toEqual([100, 100, 100, 100, 100]); | |
| 84 | expect(total(s)).toBe(500); | |
| 85 | expect(s.best).toBe(500); | |
| 86 | expect(s.record).toBe(true); | |
| 87 | expect(stroke(s, 0, 0, 5, 5)).toBe(0); | |
| 88 | expect(again(s)).toBeNull(); | |
| 89 | advance(s, 1); | |
| 90 | const next = again(s); | |
| 91 | expect(next.phase).toBe('play'); | |
| 92 | expect(next.best).toBe(500); | |
| 93 | expect(next.scores).toEqual([]); | |
| 94 | }); | |
| 95 | ||
| 96 | test('a window scored with a late streak is left dry', () => { | |
| 97 | const s = newRound(0, 1); | |
| 98 | const done = s.window; | |
| 99 | advance(s, FLOOR_TIME - 1); | |
| 100 | stroke(s, 5, 5, 30, 5); | |
| 101 | expect(Math.max(...done.wet)).toBeGreaterThan(0); | |
| 102 | advance(s, 1); | |
| 103 | expect(s.window).not.toBe(done); | |
| 104 | expect(Math.max(...done.wet)).toBe(0); | |
| 105 | }); |
window-washer/index.html added +404
| @@ -0,0 +1,404 @@ | ||
| 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>Window Washer</title> | |
| 7 | <style> | |
| 8 | :root { : #1c1b19; : #8b877f; : #fff; : #3f7fa8; } | |
| 9 | * { box-sizing: border-box; } | |
| 10 | html, body { height: 100%; margin: 0; background: var(); color: var(); } | |
| 11 | body { font: 13px/1.35 ui-monospace, "Cascadia Mono", Consolas, Menlo, monospace; overflow: hidden; } | |
| 12 | #stage { position: fixed; inset: 0; display: grid; place-items: center; } | |
| 13 | canvas { display: block; image-rendering: pixelated; touch-action: none; cursor: crosshair; outline: none; } | |
| 14 | .hud { position: fixed; left: max(16px, env(safe-area-inset-left)); right: max(16px, env(safe-area-inset-right)); display: flex; justify-content: space-between; gap: 16px; pointer-events: none; } | |
| 15 | #scores { position: fixed; top: max(14px, env(safe-area-inset-top)); left: max(16px, env(safe-area-inset-left)); display: grid; gap: 2px; pointer-events: none; } | |
| 16 | #bottom { bottom: max(14px, env(safe-area-inset-bottom)); align-items: flex-end; } | |
| 17 | .row { display: flex; align-items: center; gap: 8px; color: var(); } | |
| 18 | .row b { font-size: 16px; font-weight: 600; min-width: 1ch; font-variant-numeric: tabular-nums; } | |
| 19 | .row span { color: var(); } | |
| 20 | #status { color: var(); } | |
| 21 | button { pointer-events: auto; font: inherit; color: var(); background: none; border: 0; padding: 10px; margin: -10px; cursor: pointer; } | |
| 22 | button:hover, button:focus-visible { color: var(); } | |
| 23 | </style> | |
| 24 | </head> | |
| 25 | <body> | |
| 26 | <div id="stage"><canvas id="c" tabindex="0" aria-label="The corner of an old brick building with a window-cleaning cradle hanging on two ropes. Drag across the grimy window to wipe it clean, or hold space and press the arrow keys to steer the squeegee. Bird droppings need two passes. The cradle drops a floor every 12 seconds, for five windows."></canvas></div> | |
| 27 | <div id="scores"> | |
| 28 | <div class="row"><b id="score">0</b><span>clean</span></div> | |
| 29 | <div class="row"><b id="best">0</b><span>best</span></div> | |
| 30 | </div> | |
| 31 | <div class="hud" id="bottom"> | |
| 32 | <div id="status" role="status" aria-live="polite"></div> | |
| 33 | <button id="mute" type="button" aria-pressed="false">sound on</button> | |
| 34 | </div> | |
| 35 | <script src="../shell.js"></script> | |
| 36 | <script> | |
| 37 | PixelPlays.mount({ | |
| 38 | id: 'window-washer', | |
| 39 | rules: [ | |
| 40 | 'Drag, or hold space and use arrows, to wipe.', | |
| 41 | 'Droppings need two passes.', | |
| 42 | 'The cradle drops a floor every 12 seconds.', | |
| 43 | ], | |
| 44 | }); | |
| 45 | </script> | |
| 46 | <script>window.PP_SCENE = { originY: 96, shadowX: 1.3, shadowY: -.12 };</script> | |
| 47 | <script src="../engine.js"></script> | |
| 48 | <script src="glass.js"></script> | |
| 49 | <script> | |
| 50 | 'use strict'; | |
| 51 | ||
| 52 | const GLASS = window.Glass; | |
| 53 | const { COLS, ROWS, FLOORS } = GLASS; | |
| 54 | const FLOOR_H = 3.9, GLASS_H = ROWS / (COS * S), SILL_Z = 1.2, ROOF_Z = 20.8, WALL = 8, DEPTH = 3.5; | |
| 55 | const BRICK = [[176, 88, 66], [160, 76, 60], [188, 104, 76], [148, 70, 58]], MORTAR = [206, 190, 170]; | |
| 56 | const FRAME = [238, 232, 220], STONE = [214, 206, 192], STEEL = [112, 124, 138], ROPE = [190, 150, 96]; | |
| 57 | const LEAF = [[58, 104, 60], [84, 132, 66], [112, 150, 74]], BLOOM = [[212, 52, 66], [236, 92, 108], [244, 140, 150]]; | |
| 58 | const WOOD = [128, 92, 70]; | |
| 59 | ||
| 60 | /* ---------- where the windows are ---------- */ | |
| 61 | const SX0 = Math.round(OX + -6.9 / R2 * S); | |
| 62 | const GX0 = (SX0 - OX) / S * R2, GX1 = GX0 + COLS * R2 / S; | |
| 63 | const glassTop = k => SILL_Z + GLASS_H + (FLOORS - 1 - k) * FLOOR_H; | |
| 64 | const glassBottom = k => glassTop(k) - GLASS_H; | |
| 65 | const SY0 = k => Math.round(OY + (GX0 / R2 * SIN - glassTop(k) * COS) * S); | |
| 66 | const cellX = c => SX0 + c; | |
| 67 | const cellY = (k, c, r) => SY0(k) + r + (c >> 1); | |
| 68 | ||
| 69 | /* ---------- the wall ---------- */ | |
| 70 | const wallMask = new Uint8Array(W * H); | |
| 71 | const faceDepth = (x, y, z) => (x + y) / R2 * COS + z * SIN; | |
| 72 | function brickColor(t, z, shade) { | |
| 73 | const row = Math.floor(z / .22), off = (row & 1) * .25, col = Math.floor((t + off) / .5); | |
| 74 | const fz = z / .22 - row, ft = (t + off) / .5 - col; | |
| 75 | if (fz < .22 || ft < .07) return rgb(MORTAR[0] * shade, MORTAR[1] * shade, MORTAR[2] * shade * 1.04); | |
| 76 | const c = BRICK[Math.floor(hash(col, row) * BRICK.length)], g = .94 + noise(col * 1.7, row * 2.3) * .14; | |
| 77 | return rgb(c[0] * shade * g, c[1] * shade * g, c[2] * shade * g * 1.05); | |
| 78 | } | |
| 79 | function groundColor(x, y, ix, iy) { | |
| 80 | const edge = Math.pow(Math.pow(Math.abs(x + 2.9) / 6.4, 4) + Math.pow(Math.abs(y + 2.9) / 6.4, 4), .25); | |
| 81 | const rough = edge + (noise(x * 1.3, y * 1.3) - .5) * .12; | |
| 82 | if (rough > 1.04) return 0; | |
| 83 | if (rough > .93 && hash(ix, iy) < (rough - .93) / .11) return 0; | |
| 84 | const slab = (Math.floor(x / 1.1) + Math.floor(y / 1.1)) & 1, seam = x % 1.1 < .07 || y % 1.1 < .07; | |
| 85 | const t = .84 + (hash(ix, iy) - .5) * .12 + slab * .05 - (seam ? .12 : 0); | |
| 86 | return rgb(186 * t, 180 * t, 170 * t * 1.03); | |
| 87 | } | |
| 88 | const plants = []; | |
| 89 | function growPlants(k) { | |
| 90 | const zTop = glassBottom(k) - .35; | |
| 91 | box(GX0 - .15, 0, zTop - .55, GX1 + .15, .62, zTop, WOOD, k); | |
| 92 | box(GX0 - .25, 0, zTop + .02, GX1 + .25, .24, zTop + .2, STONE, k + 9); // the sill behind the box | |
| 93 | for (let n = 0; n < 34; n++) { | |
| 94 | const seed = k * 100 + n, bloom = n % 3 === 0; | |
| 95 | const x = GX0 + .2 + hash(seed, 1) * (GX1 - GX0 - .4), y = .14 + hash(seed, 2) * .34; | |
| 96 | const z = zTop + .06 + hash(seed, 3) * (bloom ? .2 : .14); | |
| 97 | const color = bloom ? BLOOM[Math.floor(hash(seed, 4) * 3)] : LEAF[Math.floor(hash(seed, 5) * 3)]; | |
| 98 | capture = []; | |
| 99 | sphere(x, y, z, bloom ? .13 : .17, color, 'rough'); | |
| 100 | plants.push({ k: n, y, pixels: capture, x, z }); | |
| 101 | capture = null; | |
| 102 | } | |
| 103 | } | |
| 104 | function bake() { | |
| 105 | zb.fill(-1e9); | |
| 106 | pix.fill(rgb(255, 255, 255)); | |
| 107 | for (let iy = 0; iy < H; iy++) for (let ix = 0; ix < W; ix++) { | |
| 108 | const g = unproject(ix + .5, iy + .5), i = iy * W + ix, c = groundColor(g.x, g.y, ix, iy); | |
| 109 | if (!c) continue; | |
| 110 | pix[i] = c; ground[i] = 1; | |
| 111 | zb[i] = (g.x + g.y) / R2 * COS; | |
| 112 | } | |
| 113 | groundZ.set(zb); | |
| 114 | for (let iy = 0; iy < H; iy++) for (let ix = 0; ix < W; ix++) { | |
| 115 | const u = (ix + .5 - OX) / S * R2, i = iy * W + ix; | |
| 116 | let x = 0, y = 0, shade = 1; | |
| 117 | if (u <= 0 && u >= -WALL) { x = u; shade = 1; } else if (u > 0 && u <= DEPTH) { y = -u; shade = .72; } else continue; | |
| 118 | const z = (OY + (x + y) / R2 * SIN * S - (iy + .5)) / (COS * S); | |
| 119 | if (z < 0 || z > ROOF_Z) continue; | |
| 120 | pix[i] = brickColor(u <= 0 ? x : y, z, shade); | |
| 121 | zb[i] = faceDepth(x, y, z); | |
| 122 | wallMask[i] = 1; | |
| 123 | } | |
| 124 | box(-WALL - .3, -DEPTH - .3, ROOF_Z, .3, .3, ROOF_Z + .6, STONE, 3); | |
| 125 | for (let k = 0; k < FLOORS; k++) { | |
| 126 | for (let c = -2; c < COLS + 2; c++) for (let r = -2; r < ROWS + 3; r++) { | |
| 127 | if (c >= 0 && c < COLS && r >= 0 && r < ROWS) continue; | |
| 128 | const ix = cellX(c), iy = cellY(k, c, r); | |
| 129 | if (ix < 0 || iy < 0 || ix >= W || iy >= H) continue; | |
| 130 | const i = iy * W + ix, t = r >= ROWS ? .86 : 1; | |
| 131 | pix[i] = rgb(FRAME[0] * t, FRAME[1] * t, FRAME[2] * t); | |
| 132 | } | |
| 133 | growPlants(k); | |
| 134 | } | |
| 135 | for (const x of [GX0 - .1, GX1 + .1]) capsule([x, 0, ROOF_Z + .9], [x, 1.45, ROOF_Z + .9], .09, .09, () => STEEL, 'solid'); | |
| 136 | for (let y = -DEPTH; y <= 0; y += .5) for (let z = .5; z <= 5; z += .5) castShadow(0, y, z, .4, 1, 0); | |
| 137 | for (let x = -WALL; x <= 0; x += .5) for (let z = .5; z <= 1.5; z += .5) castShadow(x, 0, z, .4, 1, 0); | |
| 138 | bakeOcclusion(); | |
| 139 | applyShadows(groundZ); | |
| 140 | bakedPix.set(pix); | |
| 141 | bakedZ.set(zb); | |
| 142 | } | |
| 143 | ||
| 144 | /* ---------- game state ---------- */ | |
| 145 | const nodes = { score: document.getElementById('score'), best: document.getElementById('best'), status: document.getElementById('status') }; | |
| 146 | const G = { | |
| 147 | time: 0, status: '', keys: new Set(), dragging: false, holding: false, hover: false, noteUntil: 0, | |
| 148 | cur: { c: COLS / 2, r: ROWS / 2 }, done: [], fromFloor: 0, drop: 1, squeak: 0, | |
| 149 | }; | |
| 150 | const BEST_KEY = 'pixel-plays-window-washer-best'; | |
| 151 | const best0 = (() => { try { return Number(localStorage.getItem(BEST_KEY)) || 0; } catch (e) { return 0; } })(); | |
| 152 | let R = GLASS.newRound(best0, Date.now() & 0xffff); | |
| 153 | function windowAt(k) { | |
| 154 | if (k < R.floor) return G.done[k]; | |
| 155 | if (k === R.floor && R.phase === 'play') return R.window; | |
| 156 | return G.preview[k]; | |
| 157 | } | |
| 158 | function previews() { G.preview = Array.from({ length: FLOORS }, (_, k) => GLASS.newWindow(R.seed * 97 + k, GLASS.droppingsOn(k))); } | |
| 159 | previews(); | |
| 160 | function renderHud() { | |
| 161 | nodes.score.textContent = GLASS.running(R); | |
| 162 | nodes.best.textContent = Math.max(R.best, GLASS.running(R)); | |
| 163 | } | |
| 164 | function note(text) { setStatus(text); G.noteUntil = G.time + 2.5; } | |
| 165 | function setStatus(text) { | |
| 166 | if (text === G.status) return; | |
| 167 | G.status = text; | |
| 168 | nodes.status.textContent = text; | |
| 169 | } | |
| 170 | const HINT = 'drag, or hold space and use the arrow keys'; | |
| 171 | function restart() { | |
| 172 | const next = GLASS.again(R); | |
| 173 | if (!next) return false; | |
| 174 | R = next; G.done = []; G.fromFloor = 0; G.drop = 1; G.holding = false; | |
| 175 | previews(); | |
| 176 | note(HINT); | |
| 177 | renderHud(); | |
| 178 | return true; | |
| 179 | } | |
| 180 | function cradleFloor() { return R.phase === 'over' ? FLOORS - 1 : R.floor; } | |
| 181 | function update(dt) { | |
| 182 | G.time += dt; | |
| 183 | if (G.drop < 1) G.drop = Math.min(1, G.drop + dt / .9); | |
| 184 | if (R.phase === 'play') { | |
| 185 | const dx = (G.keys.has('right') ? 1 : 0) - (G.keys.has('left') ? 1 : 0), dy = (G.keys.has('down') ? 1 : 0) - (G.keys.has('up') ? 1 : 0); | |
| 186 | if (dx || dy) { | |
| 187 | const from = { c: G.cur.c, r: G.cur.r }; | |
| 188 | G.cur.c = clamp(G.cur.c + dx * 44 * dt, 0, COLS - 1); | |
| 189 | G.cur.r = clamp(G.cur.r + dy * 44 * dt, 0, ROWS - 1); | |
| 190 | if (G.holding) wipe(from, G.cur); | |
| 191 | } | |
| 192 | } | |
| 193 | const was = R.phase, prev = R.window; | |
| 194 | const events = GLASS.advance(R, dt); | |
| 195 | for (const e of events) { | |
| 196 | if (e.type === 'score') { G.done[e.floor] = prev; G.holding = false; note(`window ${e.floor + 1}: ${e.percent}% clean`); } | |
| 197 | if (e.type === 'drop') { G.fromFloor = e.floor - 1; G.drop = 0; sound('chime'); } | |
| 198 | } | |
| 199 | if (events.length || R.phase === 'play') renderHud(); | |
| 200 | if (R.phase === 'play' && G.time >= G.noteUntil) setStatus(`window ${R.floor + 1} of ${FLOORS} · ${Math.ceil(GLASS.timeLeft(R))}s · ${GLASS.percentClean(R.window)}% clean`); | |
outdated
This runs on every frame with no events, so it immediately overwrites the |
||
| 201 | if (was === 'play' && R.phase === 'over') { | |
| 202 | if (R.record) try { localStorage.setItem(BEST_KEY, String(R.best)); } catch (e) { /* private window: keep it for this visit */ } | |
| 203 | setStatus(`${GLASS.total(R)} of 500${R.record ? ' · new best' : ` · best ${R.best}`} · tap or space to play again`); | |
| 204 | } | |
| 205 | } | |
| 206 | function wipe(a, b) { | |
| 207 | if (GLASS.stroke(R, a.c, a.r, b.c, b.r) && G.time - G.squeak > .12) { G.squeak = G.time; sound('squeak'); } | |
| 208 | } | |
| 209 | ||
| 210 | /* ---------- drawing ---------- */ | |
| 211 | const DIRT = [[104, 94, 76], [116, 106, 86], [92, 84, 70], [128, 118, 96]]; | |
| 212 | function glassColor(w, k, c, r) { | |
| 213 | const i = r * COLS + c, g = w.grime[i], wet = w.wet[i] / GLASS.WET_TIME; | |
| 214 | if (g === GLASS.DROPPING) { | |
| 215 | const core = hash(c, r + 40) > .55; | |
| 216 | return core ? rgb(238, 236, 226) : rgb(176, 172, 156); | |
| 217 | } | |
| 218 | if (g === GLASS.GRIME) { | |
| 219 | const streak = noise(c * .5 + k * 7, r * .07), d = DIRT[Math.min(3, Math.floor((streak * .8 + hash(c, r + k * 60) * .5) * 3))]; | |
| 220 | if (wet > bayerAt(c, r)) return rgb(d[0] * 1.35, d[1] * 1.38, d[2] * 1.42); // smeared and still wet | |
| 221 | return rgb(...d); | |
| 222 | } | |
| 223 | const sky = lerp(1.08, .86, r / ROWS), band = ((c * 2 - r + 400) % 26) < 5; | |
| 224 | let col = [150 * sky, 198 * sky, 228 * sky]; | |
| 225 | if (band) col = col.map(v => v + 22); | |
| 226 | if (wet > bayerAt(c, r) * .9) col = [236, 246, 252]; | |
| 227 | else if (wet > 0 && ((c + r) & 1) === 0) col = col.map(v => v + 14); | |
| 228 | return rgb(...col); | |
| 229 | } | |
| 230 | function drawGlass() { | |
| 231 | for (let k = 0; k < FLOORS; k++) { | |
| 232 | const w = windowAt(k); | |
| 233 | for (let c = 0; c < COLS; c++) for (let r = 0; r < ROWS; r++) { | |
| 234 | const ix = cellX(c), iy = cellY(k, c, r); | |
| 235 | if (ix >= 0 && iy >= 0 && ix < W && iy < H) pix[iy * W + ix] = glassColor(w, k, c, r); | |
| 236 | } | |
| 237 | } | |
| 238 | } | |
| 239 | function drawPlants(t) { | |
| 240 | for (const p of plants) { | |
| 241 | const sway = (Math.sin(t * 1.1 + p.x * .7) + .5 * Math.sin(t * 2.6 + p.k * 1.3)) * .9; | |
| 242 | const dx = Math.round(sway * .8), dy = Math.round(Math.sin(t * 1.6 + p.k) * .4); | |
| 243 | const px = p.pixels; | |
| 244 | for (let n = 0; n < px.length; n += 4) { | |
| 245 | const ix = px[n] + dx, iy = px[n + 1] + dy; | |
| 246 | if (ix < 0 || iy < 0 || ix >= W || iy >= H) continue; | |
| 247 | const i = iy * W + ix; | |
| 248 | if (px[n + 2] <= zb[i]) continue; | |
| 249 | zb[i] = px[n + 2]; | |
| 250 | pix[i] = px[n + 3]; | |
| 251 | } | |
| 252 | } | |
| 253 | } | |
| 254 | function cradleZ() { | |
| 255 | const to = glassBottom(cradleFloor()); | |
| 256 | if (G.drop >= 1) return to; | |
| 257 | return lerp(glassBottom(G.fromFloor), to, ease(G.drop)); | |
| 258 | } | |
| 259 | function drawCradle() { | |
| 260 | const zf = cradleZ() - 1.05, xl = GX0 - .15, xr = GX1 + .15, y0 = 1.05, y1 = 1.95, top = zf + .95; | |
| 261 | box(xl - .1, y0, zf, xr + .1, y1, zf + .2, STEEL, 2); | |
| 262 | const rail = () => STEEL; | |
| 263 | capsule([xl, y1, top], [xr, y1, top], .05, .05, rail, 'solid'); | |
| 264 | capsule([xl, y0, top], [xl, y1, top], .05, .05, rail, 'solid'); | |
| 265 | capsule([xr, y0, top], [xr, y1, top], .05, .05, rail, 'solid'); | |
| 266 | for (const x of [xl, xr, (xl + xr) / 2]) capsule([x, y1, zf + .2], [x, y1, top], .05, .05, rail, 'solid'); | |
| 267 | for (const x of [xl, xr]) { | |
| 268 | for (let z = top; z <= ROOF_Z + .85; z += .05) plot(x, (y0 + y1) / 2, z, rgb(...ROPE)); | |
| 269 | for (let y = y0; y <= y1; y += .05) plot(x, y, top, rgb(...ROPE)); | |
| 270 | } | |
| 271 | // its shadow falls screen-right on the wall behind | |
| 272 | const a = project(xl, y1, zf), b = project(xr, y0, top); | |
| 273 | const x0 = Math.floor(a.sx) + 5, x1 = Math.ceil(b.sx) + 5, y0s = Math.floor(b.sy) - 2, y1s = Math.ceil(a.sy) + 3; | |
| 274 | for (let iy = Math.max(0, y0s); iy < Math.min(H, y1s); iy++) for (let ix = Math.max(0, x0); ix < Math.min(W, x1); ix++) { | |
| 275 | const i = iy * W + ix; | |
| 276 | if (wallMask[i] && zb[i] === bakedZ[i] && (ix + iy) % 3 !== 0) tint(i, .62, .6, .78); | |
| 277 | } | |
| 278 | } | |
| 279 | const SQUEEGEE = rgb(28, 27, 25), HANDLE = rgb(244, 178, 40); | |
| 280 | function drawSqueegee() { | |
| 281 | if (R.phase !== 'play' || !(G.hover || G.dragging || G.holding || G.keys.size)) return; | |
| 282 | const k = R.floor, c0 = Math.round(G.cur.c), r0 = Math.round(G.cur.r); | |
| 283 | const put = (c, r, color) => { | |
| 284 | const ix = cellX(c), iy = cellY(k, c, r); | |
| 285 | if (ix >= 0 && iy >= 0 && ix < W && iy < H) pix[iy * W + ix] = color; | |
| 286 | }; | |
| 287 | for (let n = 1; n <= 5; n++) put(c0, r0 - n, HANDLE); | |
| 288 | for (let c = c0 - GLASS.BLADE_W; c <= c0 + GLASS.BLADE_W; c++) put(c, r0, SQUEEGEE); | |
| 289 | if (G.holding || G.dragging) for (let c = c0 - GLASS.BLADE_W; c <= c0 + GLASS.BLADE_W; c++) put(c, r0 + 1, rgb(255, 255, 255)); | |
| 290 | } | |
| 291 | function draw() { | |
| 292 | pix.set(bakedPix); | |
| 293 | zb.set(bakedZ); | |
| 294 | drawGlass(); | |
| 295 | drawCradle(); | |
| 296 | drawPlants(STILL ? 0 : Math.floor(G.time * WIND_FPS) / WIND_FPS); | |
| 297 | shadeEdges(); | |
| 298 | drawSqueegee(); | |
| 299 | ctx.putImageData(image, 0, 0); | |
| 300 | } | |
| 301 | ||
| 302 | /* ---------- input ---------- */ | |
| 303 | // canvas pixel to a cell of the window the cradle is at; clamped to the glass | |
| 304 | function cellAt(e) { | |
| 305 | const rect = canvas.getBoundingClientRect(); | |
| 306 | const sx = Math.floor((e.clientX - rect.left) / rect.width * W), sy = Math.floor((e.clientY - rect.top) / rect.height * H); | |
| 307 | const c = clamp(sx - SX0, 0, COLS - 1), r = clamp(sy - SY0(R.floor) - (c >> 1), 0, ROWS - 1); | |
| 308 | return { c, r }; | |
| 309 | } | |
| 310 | canvas.addEventListener('pointerdown', e => { | |
| 311 | ensureAudio(); | |
| 312 | canvas.focus({ preventScroll: true }); | |
| 313 | if (restart()) return; | |
| 314 | if (R.phase !== 'play') return; | |
| 315 | G.dragging = true; | |
| 316 | canvas.setPointerCapture(e.pointerId); | |
| 317 | G.cur = cellAt(e); | |
| 318 | GLASS.begin(R); | |
| 319 | wipe(G.cur, G.cur); | |
| 320 | }); | |
| 321 | canvas.addEventListener('pointermove', e => { | |
| 322 | G.hover = e.pointerType === 'mouse'; | |
| 323 | if (R.phase !== 'play') return; | |
outdated
Without a drag this returns before |
||
| 324 | const next = cellAt(e), from = G.cur; | |
| 325 | G.cur = next; | |
| 326 | if (G.dragging) wipe(from, next); | |
| 327 | }); | |
| 328 | const release = () => { G.dragging = false; }; | |
| 329 | canvas.addEventListener('pointerup', release); | |
| 330 | canvas.addEventListener('pointercancel', release); | |
| 331 | canvas.addEventListener('pointerleave', () => { G.hover = false; }); | |
| 332 | const KEYS = { ArrowLeft: 'left', a: 'left', A: 'left', ArrowRight: 'right', d: 'right', D: 'right', ArrowUp: 'up', w: 'up', W: 'up', ArrowDown: 'down', s: 'down', S: 'down' }; | |
| 333 | canvas.addEventListener('keydown', e => { | |
| 334 | if (e.key === ' ' || e.key === 'Enter') { | |
| 335 | e.preventDefault(); | |
| 336 | ensureAudio(); | |
| 337 | if (e.repeat) return; | |
| 338 | if (restart() || R.phase !== 'play') return; | |
| 339 | G.holding = true; | |
| 340 | GLASS.begin(R); | |
| 341 | wipe(G.cur, G.cur); | |
| 342 | return; | |
| 343 | } | |
| 344 | const dir = KEYS[e.key]; | |
| 345 | if (!dir) return; | |
| 346 | e.preventDefault(); | |
| 347 | ensureAudio(); | |
| 348 | G.dragging = false; | |
| 349 | G.keys.add(dir); | |
| 350 | }); | |
| 351 | canvas.addEventListener('keyup', e => { | |
| 352 | if (e.key === ' ' || e.key === 'Enter') { G.holding = false; return; } | |
| 353 | const dir = KEYS[e.key]; | |
| 354 | if (dir) G.keys.delete(dir); | |
| 355 | }); | |
| 356 | canvas.addEventListener('blur', () => { G.keys.clear(); G.holding = false; }); | |
| 357 | ||
| 358 | /* ---------- sound ---------- */ | |
| 359 | let audio = null, muted = false; | |
| 360 | function ensureAudio() { | |
| 361 | if (audio) { if (audio.state === 'suspended') audio.resume(); return; } | |
| 362 | const AC = window.AudioContext || window.webkitAudioContext; | |
| 363 | if (AC) audio = new AC(); | |
| 364 | } | |
| 365 | function sound(kind) { | |
| 366 | if (!audio || muted) return; | |
| 367 | const now = audio.currentTime, gain = audio.createGain(), osc = audio.createOscillator(); | |
| 368 | const spec = { | |
| 369 | squeak: [1500, 2300, .07, .025, 'triangle'], | |
| 370 | chime: [440, 660, .25, .08, 'sine'], | |
| 371 | }[kind]; | |
| 372 | osc.type = spec[4]; | |
| 373 | osc.frequency.setValueAtTime(spec[0], now); | |
| 374 | osc.frequency.exponentialRampToValueAtTime(spec[1], now + spec[2]); | |
| 375 | gain.gain.setValueAtTime(spec[3], now); | |
| 376 | gain.gain.exponentialRampToValueAtTime(.0005, now + spec[2]); | |
| 377 | osc.connect(gain).connect(audio.destination); | |
| 378 | osc.start(now); osc.stop(now + spec[2] + .01); | |
| 379 | } | |
| 380 | const muteButton = document.getElementById('mute'); | |
| 381 | muteButton.addEventListener('click', () => { | |
| 382 | muted = !muted; | |
| 383 | muteButton.textContent = muted ? 'sound off' : 'sound on'; | |
| 384 | muteButton.setAttribute('aria-pressed', String(muted)); | |
| 385 | }); | |
| 386 | ||
| 387 | /* ---------- fit and boot ---------- */ | |
| 388 | addEventListener('resize', fit); | |
| 389 | fit(); | |
| 390 | bake(); | |
| 391 | note(HINT); | |
| 392 | renderHud(); | |
| 393 | canvas.focus({ preventScroll: true }); | |
| 394 | let last = performance.now(); | |
| 395 | function frame(now) { | |
| 396 | update(Math.min(.04, (now - last) / 1000)); | |
| 397 | last = now; | |
| 398 | draw(); | |
| 399 | requestAnimationFrame(frame); | |
| 400 | } | |
| 401 | requestAnimationFrame(frame); | |
| 402 | </script> | |
| 403 | </body> | |
| 404 | </html> |
new above
offline
Only the current window dries. The window that just scored keeps its
wetvalues, and the page keeps drawing them inG.done, so late streaks never dry. Clear or dry it when the cradle drops, and add a test for it.