Public: anyone can read this; the crew can change it.
Migrated from github.com/amirmushichge/pixel-plays
Add Hot Air Balloon: ride the wind layers and set the basket down on the marked field #87
quill/pixel-plays:quill/72-add-hot-air-balloon-ride-the-wind-layers → main 13 commits fork 2 threads · 2 unresolved
12 files
changed +732
−1
.gitignore +13
| @@ -1,3 +1,16 @@ | ||
| 1 | 1 | .vercel/ |
| 2 | 2 | node_modules/ |
| 3 | 3 | *.log |
| 4 | # empty placeholders a sandboxed agent run leaves in the clone | |
| 5 | /.bash_profile | |
| 6 | /.bashrc | |
| 7 | /.profile | |
| 8 | /.zshrc | |
| 9 | /.zprofile | |
| 10 | /.gitconfig | |
| 11 | /.gitmodules | |
| 12 | /.ripgreprc | |
| 13 | /.idea | |
| 14 | /.vscode | |
| 15 | /.mcp.json | |
| 16 | /.claude/ |
README.md +2 −1
| @@ -43,6 +43,7 @@ Concept by [abhi](https://x.com/awwbhi2). Game by [AmirMushich](https://x.com/Am | ||
| 43 | 43 | | [](soapbox-derby/) | A 360 m cobbled hill in a wooden cart that rolls and gathers speed by itself: steer between the gaps in gates of hay bales that narrow down the hill (the cart answers late, so steer early); a bale slows it hard and costs one of three lives, a smooth flagstone strip gives a short boost, and the banner scores your time against your best (forged by the crew on the engine and shell above, not by the original creators) | Drag left or right; left/right arrows | |
| 44 | 44 | | [](sheepdog/) | A 90-second dusk over three hillside fields, five sheep each, scattered at the top: drag the dog (or use the arrow keys) so they flee it inside a set radius, flock together and wander when calm, and push one too hard and it bolts the wrong way; each sheep that goes through the dry-stone wall's gate and then into the hurdle pen scores, one that goes round the gate scores nothing once penned; the gate moves every field, the sun lowers with the clock, and 10 or more of 15 wins (forged by the crew on the engine and shell above, not by the original creators) | Drag the dog; arrow keys | |
| 45 | 45 | | [](firefly-jar/) | A 90-second summer night in a back garden: tap a firefly while it glows to catch it into the jar (+1, golden +3), a dim one is scared off and breaks the streak, shoo the moths before they reach the lantern; the third moth ends the night (forged by the crew on the engine and shell above, not by the original creators) | Tap; Tab or arrows + space | |
| 46 | | [](hot-air-balloon/) | Five flights over patchwork farm fields: hold to fire the burner and the balloon climbs slowly, with lag, while letting go sinks it; three height bands blow different ways at their own speeds, shown by drifting cloud wisps and the basket pennant, so you steer only by choosing a height; landing on the white cross scores 5, inside its field 3, a neighbouring field 1, trees or the pond 0, and 15 or more over the five flights wins (forged by the crew on the engine and shell above, not by the original creators) | Hold space or up, or press and hold the canvas | | |
| 46 | 47 | |
| 47 | 48 | ## Run |
| 48 | 49 | Requires [](https://nodejs.org/); nothing to install. |
| @@ -62,7 +63,7 @@ On the forge, `forge.json` runs `bun serve.mjs` with health check `/`. State is | ||
| 62 | 63 | Every page carries Open Graph and Twitter tags (between `og:begin` and `og:end`), and `og/<id>.png` is a 1200x630 card: the game's own opening frame, scaled 2x on white; `og/home.png` is a grid of all of them. Because the games are static, `bun scripts/og.mjs` makes them without a browser: it runs each page's scripts in a `vm` against a stub DOM, captures the canvas pixels and writes the PNG (zlib only, no dependency) and the tags. Run it after adding a game or changing a scene; `serve.test.mjs` fails when a card or its tags are missing or stale. Scenes are rendered with a seeded `Math.random`, so the cards are byte-for-byte reproducible. Absolute URLs use `ORIGIN` at the top of the script (the forge production site), so change it there if the site moves. |
| 63 | 64 | |
| 64 | 65 | ## Add a game |
| 65 | 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 as a card with its `kind`, `input` and `accent`, searchable and filterable by kind (a static fallback list for no-JS is kept in sync by `serve.test.mjs`, so add the matching `<li>` to `index.html` too, with the same `data-kind`, `data-input`, `--accent` and tags line). Then run `bun scripts/og.mjs` to make its share card and tags. 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, Window Washer, Domino Run, Campfire, Beekeeper, Bread Oven, Ice Fishing, Tightrope, Tram Line, Archery, Bell Tower, Espresso Bar, Goalkeeper, Wood Split, Signal Box, Soapbox Derby, Sheepdog and Firefly Jar 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`. | |
| 66 | 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 as a card with its `kind`, `input` and `accent`, searchable and filterable by kind (a static fallback list for no-JS is kept in sync by `serve.test.mjs`, so add the matching `<li>` to `index.html` too, with the same `data-kind`, `data-input`, `--accent` and tags line). Then run `bun scripts/og.mjs` to make its share card and tags. 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, Window Washer, Domino Run, Campfire, Beekeeper, Bread Oven, Ice Fishing, Tightrope, Tram Line, Archery, Bell Tower, Espresso Bar, Goalkeeper, Wood Split, Signal Box, Soapbox Derby, Sheepdog, Firefly Jar and Hot Air Balloon 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`. | |
| 66 | 67 | |
| 67 | 68 | - `shell.js` provides the game menu, the Rules popup, the credits and the background music. |
| 68 | 69 | - `engine.js` provides the camera, depth buffer, colour ramps, shadows, edges, figures, trees and wind as globals. |
hot-air-balloon/flight.js added +137
| @@ -0,0 +1,137 @@ | ||
| 1 | 'use strict'; | |
| 2 | // The hot air balloon rules: five flights over a patchwork of farm fields, steered only by choosing a height. | |
| 3 | // Metres and seconds; x and y are metres across the farmland (screen right is -y), z is height above the ground. | |
| 4 | // Plain numbers in, plain numbers out, no DOM. The page draws what this decides. | |
| 5 | (function (root) { | |
| 6 | const FLIGHTS = 5, WIN_AT = 15; // one game is five flights; 15 points or more wins | |
| 7 | const CELLS = 3, CELL = 6, HALF = CELLS * CELL / 2; // a 3 x 3 patchwork of 6 m fields, 18 m across | |
| 8 | const LIMIT = HALF + 3; // drift this far out and the flight is lost over the hills | |
| 9 | const CEILING = 9, BAND = CEILING / 3, START_Z = 4.5, START_SPREAD = 4; // three height bands of 3 m, launch from mid air | |
| 10 | const LAG = .6, CLIMB = 1.8, SINK = .7, VERTICAL_TAU = .9; // burner must run LAG s before it lifts; m/s up and down, and how slowly the climb rate follows | |
| 11 | const DRIFT_TAU = 1.1; // seconds for the drift to settle on the wind of the band you are in | |
| 12 | const CROSS_RADIUS = 1.2, SCORES = { cross: 5, field: 3, near: 1, none: 0 }; | |
| 13 | const BASE_ANGLE = [0, 120, 240], BASE_SPEED = [1.2, 1.6, 2], SHIFT_ANGLE = 22, SHIFT_SPEED = .2; // each band's own way, and how far a flight moves it | |
| 14 | const RESTART_AFTER = .6; | |
| 15 | ||
| 16 | const finite = v => (Number.isFinite(v) ? v : 0); | |
| 17 | const clamp = (v, a, b) => Math.max(a, Math.min(b, v)); | |
| 18 | // a repeatable number in [0, 1) for this seed and slot | |
| 19 | function noise(seed, n) { | |
| 20 | let h = (Math.imul(finite(seed) | 0, 374761393) + Math.imul(n | 0, 668265263)) | 0; | |
| 21 | h = Math.imul(h ^ (h >>> 13), 1274126177); | |
| 22 | return ((h ^ (h >>> 16)) >>> 0) / 4294967296; | |
| 23 | } | |
| 24 | ||
| 25 | // the nine fields for a game: six crops, two woods and a pond, shuffled by the seed. cell = col * 3 + row; col runs along x | |
| 26 | const centreOf = cell => ({ x: (Math.floor(cell / CELLS) - 1) * CELL, y: (cell % CELLS - 1) * CELL }); | |
| 27 | function layout(seed = 0) { | |
| 28 | const kinds = ['crop', 'crop', 'crop', 'crop', 'crop', 'crop', 'wood', 'wood', 'pond']; | |
| 29 | for (let i = kinds.length - 1; i > 0; i--) { | |
| 30 | const j = Math.floor(noise(seed, 200 + i) * (i + 1)); | |
| 31 | [kinds[i], kinds[j]] = [kinds[j], kinds[i]]; | |
| 32 | } | |
| 33 | return kinds.map((kind, cell) => ({ cell, kind, variant: Math.floor(noise(seed, 300 + cell) * 4), ...centreOf(cell) })); | |
| 34 | } | |
| 35 | const cellAt = (x, y) => { | |
| 36 | if (!(Math.abs(x) < HALF && Math.abs(y) < HALF)) return -1; | |
| 37 | return Math.floor((x + HALF) / CELL) * CELLS + Math.floor((y + HALF) / CELL); | |
| 38 | }; | |
| 39 | // the field wearing the white cross this flight: always a crop field | |
| 40 | function crossFor(fields, seed, flight) { | |
| 41 | const crops = fields.filter(f => f.kind === 'crop'); | |
| 42 | return crops[Math.floor(noise(seed, 400 + flight) * crops.length)].cell; | |
| 43 | } | |
| 44 | // the winds in the three bands for one flight: base directions nudged a little by the seed and flight | |
| 45 | function windsFor(seed, flight) { | |
| 46 | return BASE_ANGLE.map((angle, b) => { | |
| 47 | const a = (angle + (noise(seed, 500 + flight * 7 + b) * 2 - 1) * SHIFT_ANGLE) * Math.PI / 180; | |
| 48 | const speed = BASE_SPEED[b] * (1 + (noise(seed, 600 + flight * 7 + b) * 2 - 1) * SHIFT_SPEED); | |
| 49 | return { band: b, angle: a, speed, wx: Math.cos(a) * speed, wy: Math.sin(a) * speed }; | |
| 50 | }); | |
| 51 | } | |
| 52 | const bandOf = z => clamp(Math.floor(finite(z) / BAND), 0, 2); | |
| 53 | const windAt = (winds, z) => winds[bandOf(z)]; | |
| 54 | ||
| 55 | // what a landing at x, y earns: on the cross 5, inside its field 3, a neighbouring crop field 1, trees, pond or beyond 0 | |
| 56 | function landingScore(fields, cross, x, y) { | |
| 57 | const cell = cellAt(finite(x), finite(y)), c = centreOf(cross); | |
| 58 | if (cell < 0) return { score: 0, kind: 'none', cell, off: Math.hypot(x - c.x, y - c.y) }; | |
| 59 | const off = Math.hypot(x - c.x, y - c.y), f = fields[cell]; | |
| 60 | let kind = 'none'; | |
| 61 | if (cell === cross) kind = off <= CROSS_RADIUS ? 'cross' : 'field'; | |
| 62 | else if (f.kind === 'crop' && Math.abs(Math.floor(cell / CELLS) - Math.floor(cross / CELLS)) <= 1 && Math.abs(cell % CELLS - cross % CELLS) <= 1) kind = 'near'; | |
| 63 | return { score: SCORES[kind], kind, cell, off, field: f.kind }; | |
| 64 | } | |
| 65 | ||
| 66 | function startFlight(s) { | |
| 67 | const n = s.flight; | |
| 68 | s.x = (noise(s.seed, 700 + n) * 2 - 1) * START_SPREAD; | |
| 69 | s.y = (noise(s.seed, 800 + n) * 2 - 1) * START_SPREAD; | |
| 70 | s.z = START_Z; s.vz = 0; s.vx = 0; s.vy = 0; s.burn = 0; s.t = 0; s.timer = 0; | |
| 71 | s.phase = 'fly'; s.last = null; | |
| 72 | s.cross = crossFor(s.fields, s.seed, n); | |
| 73 | s.winds = windsFor(s.seed, n); | |
| 74 | const w = windAt(s.winds, s.z); s.vx = w.wx; s.vy = w.wy; | |
| 75 | } | |
| 76 | function newGame(best = 0, seed = 0) { | |
| 77 | seed = finite(seed) >>> 0; | |
| 78 | const s = { phase: 'fly', outcome: '', flight: 1, total: 0, scores: [], best: Math.max(0, finite(best)), record: false, seed, fields: layout(seed) }; | |
| 79 | startFlight(s); | |
| 80 | return s; | |
| 81 | } | |
| 82 | ||
| 83 | function finishFlight(s, landing, events) { | |
| 84 | s.last = landing; | |
| 85 | s.scores.push(landing.score); | |
| 86 | s.total += landing.score; | |
| 87 | s.timer = 0; | |
| 88 | events.push({ type: 'landed', flight: s.flight, kind: landing.kind, score: landing.score, total: s.total }); | |
| 89 | if (s.flight >= FLIGHTS) { | |
| 90 | s.phase = 'over'; | |
| 91 | s.outcome = s.total >= WIN_AT ? 'won' : 'lost'; | |
| 92 | if (s.total > s.best) { s.best = s.total; s.record = true; } | |
| 93 | events.push({ type: s.outcome, total: s.total, record: s.record }); | |
| 94 | } else s.phase = 'landed'; | |
| 95 | } | |
| 96 | // move the balloon: burner is true while the player holds | |
| 97 | function advance(s, dt, input) { | |
| 98 | const events = []; | |
| 99 | dt = Math.max(0, Math.min(.1, finite(dt))); | |
| 100 | if (s.phase !== 'fly') { s.timer += dt; return events; } | |
| 101 | s.t += dt; | |
| 102 | const burner = !!(input && input.burner); | |
| 103 | s.burn = burner ? s.burn + dt : 0; | |
| 104 | const lifting = s.burn >= LAG; | |
| 105 | const wasLifting = s.lifting; s.lifting = lifting; | |
| 106 | if (lifting && !wasLifting) events.push({ type: 'burner' }); | |
| 107 | s.vz += ((lifting ? CLIMB : -SINK) - s.vz) * (1 - Math.exp(-dt / VERTICAL_TAU)); | |
| 108 | s.z += s.vz * dt; | |
| 109 | if (s.z > CEILING) { s.z = CEILING; s.vz = Math.min(0, s.vz); } | |
| 110 | const w = windAt(s.winds, s.z), k = 1 - Math.exp(-dt / DRIFT_TAU); | |
| 111 | s.vx += (w.wx - s.vx) * k; s.vy += (w.wy - s.vy) * k; | |
| 112 | s.x += s.vx * dt; s.y += s.vy * dt; | |
| 113 | if (Math.abs(s.x) > LIMIT || Math.abs(s.y) > LIMIT) { | |
| 114 | finishFlight(s, { score: 0, kind: 'none', cell: -1, off: 99, field: 'hills' }, events); | |
| 115 | } else if (s.z <= 0) { | |
| 116 | s.z = 0; | |
| 117 | finishFlight(s, landingScore(s.fields, s.cross, s.x, s.y), events); | |
| 118 | } | |
| 119 | return events; | |
| 120 | } | |
| 121 | // a tap once the landing has been seen: the next flight, or, after the fifth, a new game; otherwise false / null | |
| 122 | function next(s) { | |
| 123 | if (s.phase !== 'landed' || s.timer <= RESTART_AFTER) return false; | |
| 124 | s.flight++; s.record = false; | |
| 125 | startFlight(s); | |
| 126 | return true; | |
| 127 | } | |
| 128 | function again(s) { | |
| 129 | return s.phase === 'over' && s.timer > RESTART_AFTER ? newGame(s.best, s.seed + 1) : null; | |
| 130 | } | |
| 131 | ||
| 132 | const api = { | |
| 133 | FLIGHTS, WIN_AT, CELLS, CELL, HALF, LIMIT, CEILING, BAND, START_Z, LAG, CLIMB, SINK, VERTICAL_TAU, DRIFT_TAU, CROSS_RADIUS, SCORES, RESTART_AFTER, | |
| 134 | layout, cellAt, centreOf, crossFor, windsFor, bandOf, windAt, landingScore, newGame, startFlight, advance, next, again, | |
| 135 | }; | |
| 136 | if (typeof module !== 'undefined') module.exports = api; else root.Flight = api; | |
| 137 | })(typeof window !== 'undefined' ? window : globalThis); |
hot-air-balloon/flight.test.mjs added +207
| @@ -0,0 +1,207 @@ | ||
| 1 | import { test, expect } from 'bun:test'; | |
| 2 | import { START_Z, FLIGHTS, WIN_AT, LAG, CEILING, BAND, HALF, CELL, CROSS_RADIUS, layout, centreOf, cellAt, crossFor, windsFor, bandOf, landingScore, newGame, advance, next, again } from './flight.js'; | |
| 3 | ||
| 4 | const run = (s, seconds, input = {}) => { const out = []; for (let t = 0; t < seconds - 1e-9; t += 1 / 60) out.push(...advance(s, 1 / 60, input)); return out; }; | |
| 5 | // a balloon hanging at height z over x, y, still, with the winds of flight 1 | |
| 6 | const hang = (z, x = 0, y = 0, seed = 0) => { const s = newGame(0, seed); s.x = x; s.y = y; s.z = z; s.vx = 0; s.vy = 0; s.vz = 0; return s; }; | |
| 7 | // a balloon one centimetre above the ground on the spot, so the next frame lands it | |
| 8 | const land = (x, y, seed = 0, cross) => { const s = hang(.01, x, y, seed); s.vz = -2; if (cross != null) s.cross = cross; s.winds = s.winds.map(w => ({ ...w, wx: 0, wy: 0 })); return s; }; | |
| 9 | ||
| 10 | test('the fields are six crops, two woods and a pond, and the cross is on a crop', () => { | |
| 11 | for (let seed = 0; seed < 30; seed++) { | |
| 12 | const f = layout(seed), count = k => f.filter(x => x.kind === k).length; | |
| 13 | expect([count('crop'), count('wood'), count('pond')]).toEqual([6, 2, 1]); | |
| 14 | for (let n = 1; n <= FLIGHTS; n++) expect(f[crossFor(f, seed, n)].kind).toBe('crop'); | |
| 15 | } | |
| 16 | expect(layout(1)).toEqual(layout(1)); | |
| 17 | expect(cellAt(0, 0)).toBe(4); | |
| 18 | expect(cellAt(HALF + 1, 0)).toBe(-1); | |
| 19 | }); | |
| 20 | ||
| 21 | test('holding raises the climb rate only after the lag', () => { | |
| 22 | const s = hang(2); | |
| 23 | run(s, LAG - .1, { burner: true }); | |
| 24 | expect(s.vz).toBeLessThan(0); | |
| 25 | expect(s.vz).toBeGreaterThanOrEqual(-1); | |
| 26 | const z = s.z; | |
| 27 | run(s, .1 + 1.5, { burner: true }); | |
| 28 | expect(s.vz).toBeGreaterThan(0); | |
| 29 | expect(s.z).toBeGreaterThan(z - 1); | |
| 30 | run(s, 2, { burner: true }); | |
| 31 | expect(s.vz).toBeGreaterThan(1.2); | |
| 32 | }); | |
| 33 | ||
| 34 | test('the climb is slow: a lag-length burn does not lift at once', () => { | |
| 35 | const s = hang(6); | |
| 36 | run(s, LAG + .2, { burner: true }); | |
| 37 | expect(s.vz).toBeLessThan(1); | |
| 38 | }); | |
| 39 | ||
| 40 | test('releasing makes the balloon sink, and a burn shorter than the lag does nothing', () => { | |
| 41 | const s = hang(8); | |
| 42 | run(s, 5, { burner: true }); | |
| 43 | const up = s.z; | |
| 44 | run(s, 6); | |
| 45 | expect(s.vz).toBeLessThan(0); | |
| 46 | expect(s.z).toBeLessThan(up); | |
| 47 | const t = hang(8); | |
| 48 | for (let i = 0; i < 6; i++) { run(t, LAG - .2, { burner: true }); run(t, .05); } | |
| 49 | expect(t.vz).toBeLessThan(0); | |
| 50 | }); | |
| 51 | ||
| 52 | test('the balloon never rises above the ceiling', () => { | |
| 53 | const s = hang(CEILING - .5, 0, 0, 3); | |
| 54 | s.winds = s.winds.map(w => ({ ...w, wx: 0, wy: 0 })); | |
| 55 | run(s, 12, { burner: true }); | |
| 56 | expect(s.z).toBe(CEILING); | |
| 57 | }); | |
| 58 | ||
| 59 | test('each band drifts the balloon its own way', () => { | |
| 60 | const dirs = []; | |
| 61 | for (const [band, z] of [[0, BAND / 2], [1, BAND * 1.5], [2, BAND * 2.5]]) { | |
| 62 | const s = hang(z, 0, 0, 5); | |
| 63 | s.vx = s.winds[band].wx; s.vy = s.winds[band].wy; | |
| 64 | const hold = () => { s.vz = 0; }; | |
| 65 | for (let i = 0; i < 180; i++) { hold(); s.z = z; advance(s, 1 / 60, { burner: false }); } | |
| 66 | expect(bandOf(z)).toBe(band); | |
| 67 | const w = s.winds[band]; | |
| 68 | expect(Math.sign(s.x)).toBe(Math.sign(w.wx)); | |
| 69 | expect(Math.sign(s.y)).toBe(Math.sign(w.wy)); | |
| 70 | expect(Math.hypot(s.x, s.y)).toBeGreaterThan(w.speed * 2.5); | |
| 71 | dirs.push(Math.atan2(s.y, s.x)); | |
| 72 | } | |
| 73 | expect(Math.abs(dirs[0] - dirs[1])).toBeGreaterThan(1); | |
| 74 | expect(Math.abs(dirs[1] - dirs[2])).toBeGreaterThan(1); | |
| 75 | expect(Math.abs(dirs[0] - dirs[2])).toBeGreaterThan(1); | |
| 76 | }); | |
| 77 | ||
| 78 | test('the drift turns slowly when the balloon changes band', () => { | |
| 79 | const s = hang(2, 0, 0, 5); | |
| 80 | s.vx = s.winds[0].wx; s.vy = s.winds[0].wy; | |
| 81 | s.z = 10; s.vz = 0; | |
| 82 | const keep = () => { s.z = 10; s.vz = 0; }; | |
| 83 | keep(); advance(s, .1); | |
| 84 | const early = Math.atan2(s.vy, s.vx); | |
| 85 | for (let i = 0; i < 300; i++) { keep(); advance(s, 1 / 60); } | |
| 86 | expect(Math.hypot(s.vx - s.winds[2].wx, s.vy - s.winds[2].wy)).toBeLessThan(.05); | |
| 87 | expect(early).not.toBeCloseTo(Math.atan2(s.winds[2].wy, s.winds[2].wx), 1); | |
| 88 | }); | |
| 89 | ||
| 90 | test('the winds shift a little from flight to flight but stay three different ways', () => { | |
| 91 | const a = windsFor(4, 1), b = windsFor(4, 2); | |
| 92 | expect(a).not.toEqual(b); | |
| 93 | for (const w of [a, b]) { | |
| 94 | expect(w).toHaveLength(3); | |
| 95 | for (let i = 0; i < 3; i++) for (let j = i + 1; j < 3; j++) { | |
| 96 | const d = Math.abs(Math.atan2(Math.sin(w[i].angle - w[j].angle), Math.cos(w[i].angle - w[j].angle))); | |
| 97 | expect(d).toBeGreaterThan(1.2); | |
| 98 | } | |
| 99 | } | |
| 100 | for (let i = 0; i < 3; i++) expect(Math.abs(a[i].angle - b[i].angle)).toBeLessThan(.9); | |
| 101 | }); | |
| 102 | ||
| 103 | test('each flight starts from a new launch point', () => { | |
| 104 | const s = newGame(0, 2); | |
| 105 | const seen = new Set([`${s.x.toFixed(2)},${s.y.toFixed(2)}`]); | |
| 106 | for (let n = 1; n < FLIGHTS; n++) { | |
| 107 | s.phase = 'landed'; s.timer = 1; | |
| 108 | expect(next(s)).toBe(true); | |
| 109 | seen.add(`${s.x.toFixed(2)},${s.y.toFixed(2)}`); | |
| 110 | expect(s.z).toBe(START_Z); | |
| 111 | expect(s.phase).toBe('fly'); | |
| 112 | } | |
| 113 | expect(seen.size).toBe(FLIGHTS); | |
| 114 | }); | |
| 115 | ||
| 116 | test('landing scores by distance: cross 5, its field 3, a neighbouring field 1, trees or pond 0', () => { | |
| 117 | const seed = 0, f = layout(seed); | |
| 118 | const cross = f.find(x => x.kind === 'crop').cell, c = centreOf(cross); | |
| 119 | const s = land(c.x + .5, c.y, seed, cross); | |
| 120 | const ev = run(s, .1); | |
| 121 | expect(ev.find(e => e.type === 'landed')).toMatchObject({ kind: 'cross', score: 5 }); | |
| 122 | expect(s.total).toBe(5); | |
| 123 | ||
| 124 | const inField = land(c.x + CROSS_RADIUS + 1, c.y, seed, cross); | |
| 125 | expect(run(inField, .1).find(e => e.type === 'landed')).toMatchObject({ kind: 'field', score: 3 }); | |
| 126 | ||
| 127 | // the cross sits on a different crop per seed: take the first seed that has both a neighbouring and a distant crop | |
| 128 | let near, far, nf, nc, seed2, cross2; | |
| 129 | for (let sd = 0; sd < 40 && !(near && far); sd++) { | |
| 130 | nf = layout(sd); const cr = nf.find(x => x.kind === 'crop').cell; nc = centreOf(cr); | |
| 131 | const others = nf.filter(x => x.kind === 'crop' && x.cell !== cr); | |
| 132 | near = others.find(x => Math.abs(x.x - nc.x) <= CELL && Math.abs(x.y - nc.y) <= CELL); | |
| 133 | far = others.find(x => Math.abs(x.x - nc.x) > CELL || Math.abs(x.y - nc.y) > CELL); | |
| 134 | if (near && far) { seed2 = sd; cross2 = cr; } | |
| 135 | } | |
| 136 | expect(near).toBeDefined(); | |
| 137 | expect(far).toBeDefined(); | |
| 138 | expect(run(land(near.x, near.y, seed2, cross2), .1).find(e => e.type === 'landed')).toMatchObject({ kind: 'near', score: 1 }); | |
| 139 | expect(landingScore(nf, cross2, far.x, far.y).score).toBe(0); | |
| 140 | for (const k of ['wood', 'pond']) for (const x of f.filter(q => q.kind === k)) expect(landingScore(f, cross, x.x, x.y).score).toBe(0); | |
| 141 | }); | |
| 142 | ||
| 143 | test('a wood or the pond next to the cross still scores nothing, and so does the sea beyond', () => { | |
| 144 | let seen = 0; | |
| 145 | for (let seed = 0; seed < 40; seed++) { | |
| 146 | const f = layout(seed), cross = crossFor(f, seed, 1), c = centreOf(cross); | |
| 147 | for (const x of f) if (x.kind !== 'crop' && Math.abs(x.x - c.x) <= CELL && Math.abs(x.y - c.y) <= CELL) { | |
| 148 | expect(landingScore(f, cross, x.x, x.y).score).toBe(0); seen++; | |
| 149 | } | |
| 150 | } | |
| 151 | expect(seen).toBeGreaterThan(5); | |
| 152 | expect(landingScore(layout(0), 4, HALF + 2, 0)).toMatchObject({ score: 0, kind: 'none' }); | |
| 153 | }); | |
| 154 | ||
| 155 | test('touching the ground ends the flight; drifting far off the farms loses it for nothing', () => { | |
| 156 | const s = hang(.4); s.vz = -1; | |
| 157 | const ev = run(s, 1); | |
| 158 | expect(ev.some(e => e.type === 'landed')).toBe(true); | |
| 159 | expect(s.phase).not.toBe('fly'); | |
| 160 | expect(s.z).toBe(0); | |
| 161 | const lost = hang(5, HALF + 2.9, 0); | |
| 162 | lost.winds = lost.winds.map(w => ({ ...w, wx: 3, wy: 0 })); lost.vx = 3; | |
| 163 | run(lost, 1); | |
| 164 | expect(lost.scores).toEqual([0]); | |
| 165 | }); | |
| 166 | ||
| 167 | test('the fifth landing ends the game: 15 wins, 14 loses', () => { | |
| 168 | for (const [points, outcome] of [[WIN_AT - 5, 'won'], [WIN_AT - 6, 'lost']]) { | |
| 169 | const s = newGame(0, 0); | |
| 170 | s.flight = FLIGHTS; s.total = points; s.scores = [points]; | |
| 171 | const c = centreOf(s.cross), t = land(c.x, c.y, 0, s.cross); | |
| 172 | Object.assign(t, { flight: FLIGHTS, total: points, scores: [points], fields: s.fields }); | |
| 173 | const ev = run(t, .1); | |
| 174 | expect(t.total).toBe(points + 5); | |
| 175 | expect(t.phase).toBe('over'); | |
| 176 | expect(t.outcome).toBe(outcome); | |
| 177 | expect(ev.some(e => e.type === outcome)).toBe(true); | |
| 178 | } | |
| 179 | }); | |
| 180 | ||
| 181 | test('the game is five flights, a tap moves on only after a beat, and the best total is kept', () => { | |
| 182 | const s = newGame(7, 1); | |
| 183 | for (let n = 1; n <= FLIGHTS; n++) { | |
| 184 | expect(s.flight).toBe(n); | |
| 185 | s.z = .01; s.vz = -2; | |
| 186 | run(s, .1); | |
| 187 | expect(s.phase).toBe(n === FLIGHTS ? 'over' : 'landed'); | |
| 188 | if (n < FLIGHTS) { expect(next(s)).toBe(false); run(s, 1); expect(next(s)).toBe(true); } | |
| 189 | } | |
| 190 | expect(s.scores).toHaveLength(FLIGHTS); | |
| 191 | expect(s.total).toBe(s.scores.reduce((a, b) => a + b, 0)); | |
| 192 | expect(s.best).toBe(Math.max(7, s.total)); | |
| 193 | expect(again(s)).toBeNull(); | |
| 194 | run(s, 1); | |
| 195 | const g = again(s); | |
| 196 | expect(g.flight).toBe(1); | |
| 197 | expect(g.total).toBe(0); | |
| 198 | expect(g.best).toBe(s.best); | |
| 199 | expect(next(g)).toBe(false); | |
| 200 | }); | |
| 201 | ||
| 202 | test('bad numbers do not break a flight', () => { | |
| 203 | const s = newGame(NaN, NaN); | |
| 204 | advance(s, NaN, { burner: true }); | |
| 205 | advance(s, -5, null); | |
| 206 | expect(Number.isFinite(s.z) && Number.isFinite(s.x)).toBe(true); | |
| 207 | }); |
hot-air-balloon/index.html added +324
| @@ -0,0 +1,324 @@ | ||
| 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>Hot Air Balloon</title> | |
| 7 | <!-- og:begin --> | |
| 8 | <meta name="description" content="Five flights over the farms: hold the burner to climb into a different wind, land on the cross for 5"> | |
| 9 | <link rel="canonical" href="https://pixel-plays--forge-trevato-apps.open-platform.sh/hot-air-balloon/"> | |
| 10 | <meta property="og:type" content="website"> | |
| 11 | <meta property="og:site_name" content="Pixel Plays"> | |
| 12 | <meta property="og:title" content="Hot Air Balloon"> | |
| 13 | <meta property="og:description" content="Five flights over the farms: hold the burner to climb into a different wind, land on the cross for 5"> | |
| 14 | <meta property="og:url" content="https://pixel-plays--forge-trevato-apps.open-platform.sh/hot-air-balloon/"> | |
| 15 | <meta property="og:image" content="https://pixel-plays--forge-trevato-apps.open-platform.sh/og/hot-air-balloon.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="Hot Air Balloon: the opening scene of the game"> | |
| 20 | <meta name="twitter:card" content="summary_large_image"> | |
| 21 | <!-- og:end --> | |
| 22 | <style> | |
| 23 | :root { : #1c1b19; : #8b877f; : #fff; : #d0453c; } | |
| 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; gap: 16px; pointer-events: none; } | |
| 31 | #scores { position: fixed; top: max(14px, env(safe-area-inset-top)); left: max(16px, env(safe-area-inset-left)); display: grid; gap: 2px; pointer-events: none; } | |
| 32 | #bottom { bottom: max(14px, env(safe-area-inset-bottom)); align-items: flex-end; } | |
| 33 | .row { display: flex; align-items: center; gap: 8px; color: var(); } | |
| 34 | .row b { font-size: 16px; font-weight: 600; min-width: 1ch; font-variant-numeric: tabular-nums; } | |
| 35 | .row span { color: var(); } | |
| 36 | #status { color: var(); min-height: 1.35em; } | |
| 37 | button { pointer-events: auto; font: inherit; color: var(); background: none; border: 0; padding: 10px; margin: -10px; cursor: pointer; } | |
| 38 | button:hover, button:focus-visible { color: var(); } | |
| 39 | </style> | |
| 40 | </head> | |
| 41 | <body> | |
| 42 | <div id="stage"><canvas id="c" tabindex="0" aria-label="A hot air balloon over patchwork farm fields with hedgerows, a windsock and one field marked with a white cross. Press and hold anywhere on the canvas, or hold Space or the up arrow, to fire the burner: the balloon climbs slowly, and sinks when you let go. The air has three height bands, low, middle and high, and each blows a different way, shown by the drifting cloud wisps and the pennant on the basket. You steer only by choosing a height. Touching the ground ends the flight; land on the cross for 5, inside its field for 3, a neighbouring field for 1, trees or the pond for 0. Five flights make a game; 15 or more wins."></canvas></div> | |
| 43 | <div id="scores"> | |
| 44 | <div class="row"><b id="flight">1</b><span>of 5 flights</span></div> | |
| 45 | <div class="row"><b id="height">5</b><span id="band">m up</span></div> | |
| 46 | <div class="row"><b id="total">0</b><span>total</span></div> | |
| 47 | <div class="row"><b id="best">0</b><span>best</span></div> | |
| 48 | </div> | |
| 49 | <div class="hud" id="bottom"> | |
| 50 | <div id="status" role="status" aria-live="polite"></div> | |
| 51 | <button id="mute" type="button" aria-pressed="false">sound on</button> | |
| 52 | </div> | |
| 53 | <script src="../shell.js"></script> | |
| 54 | <script> | |
| 55 | PixelPlays.mount({ | |
| 56 | id: 'hot-air-balloon', | |
| 57 | rules: [ | |
| 58 | 'Hold (press anywhere, or Space / Up) to fire the burner and climb; let go and the balloon sinks.', | |
| 59 | 'Low, middle and high air each blow a different way: the wisps and the pennant show it. Choose your height to drift.', | |
| 60 | 'Touching the ground ends the flight: cross 5, its field 3, a neighbouring field 1, trees or pond 0.', | |
| 61 | 'Five flights; 15 or more wins.', | |
| 62 | ], | |
| 63 | }); | |
| 64 | </script> | |
| 65 | <script>window.PP_SCENE = { originY: 50, shadowX: 1.3, shadowY: -.12 };</script> | |
| 66 | <script src="../engine.js"></script> | |
| 67 | <script src="flight.js"></script> | |
| 68 | <script> | |
| 69 | 'use strict'; | |
| 70 | ||
| 71 | const BEST_KEY = 'pixel-plays-hot-air-balloon-best'; | |
| 72 | const BAND_NAMES = ['low wind', 'middle wind', 'high wind']; | |
| 73 | const HINT = 'hold to fire the burner · let go to sink · each height has its own wind'; | |
| 74 | const HEDGE = [58, 104, 56], STEEL = [150, 150, 160], CREAM = [240, 230, 205], RED = [208, 69, 60], WICKER = [160, 112, 62]; | |
| 75 | const CROPS = [[[214, 184, 84], [194, 162, 68]], [[142, 100, 70], [120, 82, 58]], [[116, 164, 78], [96, 142, 64]], [[184, 200, 76], [152, 178, 62]]]; | |
| 76 | const WOODFLOOR = [[70, 108, 62], [58, 94, 54]], WATER = [[84, 132, 178], [112, 160, 204]]; | |
| 77 | const WISP = [[236, 226, 214], [240, 240, 244], [214, 226, 244]]; // low air is warm, high air is cool | |
| 78 | const SOCK = [-8.2, 8.2], WISPS = 7; | |
| 79 | ||
| 80 | /* ---------- state ---------- */ | |
| 81 | const best0 = (() => { try { return Number(localStorage.getItem(BEST_KEY)) || 0; } catch (e) { return 0; } })(); | |
| 82 | let F = Flight.newGame(best0, Date.now() & 0xffff); | |
| 83 | const G = { time: 0, status: '', noteUntil: 0, wait: false }; | |
| 84 | const nodes = {}; | |
| 85 | for (const id of ['flight', 'height', 'band', 'total', 'best', 'status']) nodes[id] = document.getElementById(id); | |
| 86 | let shown = ''; | |
| 87 | function renderHud() { | |
| 88 | const band = Flight.bandOf(F.z), text = [F.flight, Math.round(F.z), band, F.total, F.best].join('|'); | |
| 89 | if (text === shown) return; | |
| 90 | shown = text; | |
| 91 | nodes.flight.textContent = F.flight; | |
| 92 | nodes.height.textContent = Math.round(F.z); | |
| 93 | nodes.band.textContent = 'm up · ' + BAND_NAMES[band]; | |
| 94 | nodes.total.textContent = F.total; | |
| 95 | nodes.best.textContent = F.best; | |
| 96 | } | |
| 97 | function setStatus(text) { | |
| 98 | if (text === G.status) return; | |
| 99 | G.status = text; | |
| 100 | nodes.status.textContent = text; | |
| 101 | } | |
| 102 | function note(text) { setStatus(text); G.noteUntil = G.time + 2.4; } | |
| 103 | ||
| 104 | /* ---------- baked farmland ---------- */ | |
| 105 | const HALF = Flight.HALF, CELL = Flight.CELL; | |
| 106 | function tone(pair, t) { return rgb(...pair[t > .5 ? 1 : 0]); } | |
| 107 | function fieldColor(x, y, ix, iy) { | |
| 108 | const field = F.fields[Flight.cellAt(x, y)]; | |
| 109 | const grain = hash(ix, iy); | |
| 110 | if (field.kind === 'pond') { | |
| 111 | const r = Math.hypot(x - field.x, y - field.y), ring = noise(x * 2.2, y * 2.2); | |
| 112 | return r < 2.6 + ring * .5 ? tone(WATER, grain > .86 ? 1 : ring > .62 ? 1 : 0) : rgb(...WOODFLOOR[grain > .5 ? 1 : 0]); | |
| 113 | } | |
| 114 | if (field.kind === 'wood') return tone(WOODFLOOR, noise(x * 1.6, y * 1.6) + (grain - .5) * .4); | |
| 115 | const along = field.cell % 2 ? x : y, stripe = Math.floor((along + 20) / .75) & 1; | |
| 116 | const t = stripe * .6 + (grain - .5) * .3 + noise(x * 1.4, y * 1.4) * .3; | |
| 117 | return tone(CROPS[field.variant], t > .45 ? 1 : 0); | |
| 118 | } | |
| 119 | function bake() { | |
| 120 | ground.fill(0); zb.fill(-1e9); | |
| 121 | pix.fill(rgb(255, 255, 255)); | |
| 122 | for (let iy = 0; iy < H; iy++) for (let ix = 0; ix < W; ix++) { | |
| 123 | const g = unproject(ix + .5, iy + .5), i = iy * W + ix; | |
| 124 | const edge = Math.max(Math.abs(g.x), Math.abs(g.y)) / HALF, rough = edge + (noise(g.x * 1.3, g.y * 1.3) - .5) * .08; | |
| 125 | if (rough > 1.04 || (rough > .95 && hash(ix, iy) < (rough - .95) / .09)) continue; | |
| 126 | pix[i] = fieldColor(Math.max(-HALF + .01, Math.min(HALF - .01, g.x)), Math.max(-HALF + .01, Math.min(HALF - .01, g.y)), ix, iy); | |
| 127 | ground[i] = 1; | |
| 128 | zb[i] = (g.x + g.y) / R2 * COS; | |
| 129 | } | |
| 130 | groundZ.set(zb); | |
| 131 | for (let n = 0; n <= Flight.CELLS; n++) for (let k = 0; k * .42 <= HALF * 2; k++) { | |
| 132 | const line = -HALF + n * CELL, along = -HALF + k * .42, jitter = (hash(n, k) - .5) * .06; | |
| 133 | for (const [x, y] of [[line, along], [along, line]]) { | |
| 134 | sphere(x + jitter, y - jitter, .32, .3 + hash(k, n) * .06, HEDGE, 'rough'); | |
| 135 | if (k % 2 === 0) castShadow(x, y, .5, .34, 1, .6); | |
| 136 | } | |
| 137 | } | |
| 138 | for (const f of F.fields) if (f.kind === 'wood') { | |
| 139 | [[-1.4, -1.2], [1.3, -.4], [-.2, 1.5]].forEach(([dx, dy], k) => { | |
| 140 | growTree(f.x + dx, f.y + dy, .75, 130, f.cell * 17 + k * 5); | |
| 141 | castShadow(f.x + dx, f.y + dy, 1.7, 1.1, 1, .4); | |
| 142 | }); | |
| 143 | } | |
| 144 | bakeOcclusion(); | |
| 145 | staticShadow.set(shadowMask); | |
| 146 | applyShadows(groundZ); | |
| 147 | bakedPix.set(pix); | |
| 148 | bakedZ.set(zb); | |
| 149 | } | |
| 150 | ||
| 151 | /* ---------- the cross, the windsock, the wisps, the balloon ---------- */ | |
| 152 | const WHITE = rgb(252, 250, 244); | |
| 153 | function paintCross(cell) { | |
| 154 | const c = Flight.centreOf(cell), arm = 1.3, half = .22; | |
| 155 | for (let a = -arm; a <= arm; a += .05) for (let b = -half; b <= half; b += .05) for (const [x, y] of [[c.x + a, c.y + b], [c.x + b, c.y + a]]) { | |
| 156 | const p = project(x, y, 0), ix = p.sx | 0, iy = p.sy | 0; | |
| 157 | if (ix < 0 || iy < 0 || ix >= W || iy >= H) continue; | |
| 158 | const i = iy * W + ix; | |
| 159 | if (ground[i] && zb[i] === groundZ[i]) pix[i] = WHITE; | |
| 160 | } | |
| 161 | } | |
| 162 | const dirOf = w => [Math.cos(w.angle), Math.sin(w.angle)]; | |
| 163 | function windsock(step) { | |
| 164 | const [x, y] = SOCK, w = F.winds[0], [dx, dy] = dirOf(w), top = 2.7; | |
| 165 | capsule([x, y, 0], [x, y, top], .07, .06, () => STEEL, 'solid'); | |
| 166 | sphere(x, y, top, .1, STEEL, 'solid'); | |
| 167 | const stiff = Math.min(1, w.speed / 1.6); | |
| 168 | for (let k = 1; k <= 4; k++) { | |
| 169 | const flutter = STILL ? 0 : Math.sin(step * 1.2 + k * 1.1) * .07 * k; | |
| 170 | sphere(x + dx * k * .4 - dy * flutter, y + dy * k * .4 + dx * flutter, top - k * .13 * (1.25 - stiff), .34 - k * .05, k % 2 ? RED : CREAM, 'solid'); | |
| 171 | } | |
| 172 | castShadow(x, y, top * .8, .3, 1, .3); | |
| 173 | } | |
| 174 | // wisps at every height, every frame: the player reads each band's direction before choosing one | |
| 175 | function wisps(t) { | |
| 176 | const span = Flight.LIMIT * 2; | |
| 177 | for (let b = 0; b < 3; b++) { | |
| 178 | const w = F.winds[b], [dx, dy] = dirOf(w); | |
| 179 | for (let n = 0; n < WISPS; n++) { | |
| 180 | const bx = hash(n + 1, b * 3 + 1) * span, by = hash(n + 5, b * 3 + 2) * span; | |
| 181 | const x = ((bx + w.wx * t) % span + span) % span - Flight.LIMIT, y = ((by + w.wy * t) % span + span) % span - Flight.LIMIT; | |
| 182 | const z = b * Flight.BAND + Flight.BAND * .5 + (hash(n, b + 9) - .5) * .9, len = 1.3 + hash(n, b + 17) * .8; | |
| 183 | capsule([x - dx * len, y - dy * len, z], [x, y, z], .05, .24, () => WISP[b], 'rough'); | |
| 184 | sphere(x, y, z, .27, WISP[b], 'rough'); | |
| 185 | } | |
| 186 | } | |
| 187 | } | |
| 188 | function balloon(burning) { | |
| 189 | const { x, y, z } = F, w = Flight.windAt(F.winds, z), [dx, dy] = dirOf(w); | |
| 190 | for (const [sx, sy] of [[-1, -1], [-1, 1], [1, -1], [1, 1]]) capsule([x + sx * .32, y + sy * .32, z + .55], [x + sx * .55, y + sy * .55, z + 2.1], .035, .035, () => [90, 70, 50], 'solid'); | |
| 191 | box(x - .4, y - .4, z, x + .4, y + .4, z + .55, WICKER, 5); | |
| 192 | box(x - .42, y - .42, z + .5, x + .42, y + .42, z + .6, [120, 82, 46], 6); | |
| 193 | capsule([x, y, z + 2], [x, y, z + 3.9], .75, 1.55, zz => (Math.floor(zz * 1.8) & 1 ? CREAM : RED), 'solid'); | |
| 194 | sphere(x, y, z + 4, 1.55, RED, 'solid'); | |
| 195 | sphere(x, y, z + 4.55, .8, [226, 96, 80], 'solid'); | |
| 196 | if (burning) { sphere(x, y, z + 1.55, .17, [255, 214, 90], 'solid'); sphere(x, y, z + 1.85, .12, [255, 150, 60], 'solid'); } | |
| 197 | // the pennant on the basket streams the way the air under the balloon is blowing, longer when it blows harder | |
| 198 | const px = x - .36, py = y + .36, tip = z + 1.45, len = .45 + w.speed * .4; | |
| 199 | capsule([px, py, z + .55], [px, py, tip + .1], .03, .03, () => STEEL, 'solid'); | |
| 200 | capsule([px, py, tip], [px + dx * len, py + dy * len, tip - .06], .13, .03, () => [255, 214, 10], 'solid'); | |
| 201 | castShadow(x, y, 0, 1.25, 1, .4); // straight down: the shadow is where the basket would land | |
| 202 | } | |
| 203 | function draw() { | |
| 204 | pix.set(bakedPix); | |
| 205 | zb.set(bakedZ); | |
| 206 | pendingShadows.length = 0; | |
| 207 | paintCross(F.cross); | |
| 208 | const step = STILL ? 0 : Math.floor(G.time * 8); | |
| 209 | windsock(step); | |
| 210 | wisps(step / 8); | |
| 211 | balloon(Flight.LAG <= F.burn && F.phase === 'fly'); | |
| 212 | applyShadows(bakedZ); | |
| 213 | shadeEdges(); | |
| 214 | ctx.putImageData(image, 0, 0); | |
| 215 | } | |
| 216 | ||
| 217 | /* ---------- game ---------- */ | |
| 218 | function act(events) { | |
| 219 | for (const e of events) { | |
| 220 | if (e.type === 'burner') sound('burn'); | |
| 221 | if (e.type === 'landed') { | |
| 222 | const say = { cross: 'on the cross', field: 'in the marked field', near: 'a neighbouring field', none: 'trees, pond or open ground' }[e.kind]; | |
| 223 | setStatus(`${say}: ${e.score} · ` + (F.phase === 'over' ? '' : 'tap to launch the next flight')); | |
| 224 | sound(e.score ? 'land' : 'thud'); | |
| 225 | } | |
| 226 | if (e.type === 'won' || e.type === 'lost') { | |
| 227 | if (F.record) try { localStorage.setItem(BEST_KEY, String(F.best)); } catch (err) { /* private window: keep it for this visit */ } | |
| 228 | const line = e.type === 'won' ? `${F.total} points: a fine day's flying, you win` : `${F.total} points: short of ${Flight.WIN_AT}, you lose`; | |
| 229 | setStatus(`${line}${F.record ? ' · new best' : ''} · tap to fly again`); | |
| 230 | sound(e.type === 'won' ? 'chime' : 'buzz'); | |
| 231 | } | |
| 232 | } | |
| 233 | renderHud(); | |
| 234 | } | |
| 235 | function advanceGame() { | |
| 236 | if (F.phase === 'landed' && Flight.next(F)) { G.wait = true; note(`flight ${F.flight}: look at the wisps, then choose a height`); renderHud(); return true; } | |
| 237 | if (F.phase === 'over') { | |
| 238 | const g = Flight.again(F); | |
| 239 | if (!g) return true; | |
| 240 | F = g; G.wait = true; bake(); note(HINT); renderHud(); | |
| 241 | return true; | |
| 242 | } | |
| 243 | return F.phase !== 'fly'; | |
| 244 | } | |
| 245 | const holds = new Set(); | |
| 246 | function update(dt) { | |
| 247 | G.time += dt; | |
| 248 | const burner = holds.size > 0 && !G.wait && F.phase === 'fly'; | |
| 249 | if (!holds.size) G.wait = false; | |
| 250 | act(Flight.advance(F, dt, { burner })); | |
| 251 | if (F.phase === 'fly' && G.time >= G.noteUntil) setStatus(`flight ${F.flight}: ${BAND_NAMES[Flight.bandOf(F.z)]} here`); | |
| 252 | } | |
| 253 | ||
| 254 | /* ---------- input ---------- */ | |
| 255 | const KEYS = new Set([' ', 'ArrowUp']); | |
| 256 | canvas.addEventListener('keydown', e => { | |
| 257 | if (!KEYS.has(e.key) && e.key !== 'Enter') return; | |
| 258 | e.preventDefault(); | |
| 259 | ensureAudio(); | |
| 260 | if (e.repeat) return; | |
| 261 | if (advanceGame()) return; | |
| 262 | if (KEYS.has(e.key)) holds.add(e.key); | |
| 263 | }); | |
| 264 | canvas.addEventListener('keyup', e => holds.delete(e.key)); | |
| 265 | canvas.addEventListener('blur', () => holds.clear()); | |
| 266 | canvas.addEventListener('pointerdown', e => { | |
| 267 | ensureAudio(); | |
| 268 | canvas.focus({ preventScroll: true }); | |
| 269 | try { canvas.setPointerCapture(e.pointerId); } catch (err) { /* synthetic pointers have nothing to capture */ } | |
| 270 | if (advanceGame()) return; | |
| 271 | holds.add('p' + e.pointerId); | |
| 272 | }); | |
| 273 | for (const type of ['pointerup', 'pointercancel']) canvas.addEventListener(type, e => holds.delete('p' + e.pointerId)); | |
| 274 | ||
| 275 | /* ---------- sound ---------- */ | |
| 276 | let audio = null, muted = false; | |
| 277 | function ensureAudio() { | |
| 278 | if (audio) { if (audio.state === 'suspended') audio.resume(); return; } | |
| 279 | const AC = window.AudioContext || window.webkitAudioContext; | |
| 280 | if (AC) audio = new AC(); | |
| 281 | } | |
| 282 | function sound(kind) { | |
| 283 | if (!audio || muted) return; | |
| 284 | const now = audio.currentTime, gain = audio.createGain(), osc = audio.createOscillator(); | |
| 285 | const spec = { | |
| 286 | burn: [110, 70, .35, .07, 'sawtooth'], | |
| 287 | land: [420, 640, .25, .08, 'triangle'], | |
| 288 | thud: [150, 55, .2, .1, 'square'], | |
| 289 | chime: [520, 880, .4, .07, 'sine'], | |
| 290 | buzz: [150, 60, .4, .06, 'sawtooth'], | |
| 291 | }[kind]; | |
| 292 | osc.type = spec[4]; | |
| 293 | osc.frequency.setValueAtTime(spec[0], now); | |
| 294 | osc.frequency.exponentialRampToValueAtTime(spec[1], now + spec[2]); | |
| 295 | gain.gain.setValueAtTime(spec[3], now); | |
| 296 | gain.gain.exponentialRampToValueAtTime(.0005, now + spec[2]); | |
| 297 | osc.connect(gain).connect(audio.destination); | |
| 298 | osc.start(now); osc.stop(now + spec[2] + .01); | |
| 299 | } | |
| 300 | const muteButton = document.getElementById('mute'); | |
| 301 | muteButton.addEventListener('click', () => { | |
| 302 | muted = !muted; | |
| 303 | muteButton.textContent = muted ? 'sound off' : 'sound on'; | |
| 304 | muteButton.setAttribute('aria-pressed', String(muted)); | |
| 305 | }); | |
| 306 | ||
| 307 | /* ---------- fit and boot ---------- */ | |
| 308 | addEventListener('resize', fit); | |
| 309 | fit(); | |
| 310 | bake(); | |
| 311 | note(HINT); | |
| 312 | renderHud(); | |
| 313 | canvas.focus({ preventScroll: true }); | |
| 314 | let last = performance.now(); | |
| 315 | function frame(now) { | |
| 316 | update(Math.min(.04, (now - last) / 1000)); | |
| 317 | last = now; | |
| 318 | draw(); | |
| 319 | requestAnimationFrame(frame); | |
| 320 | } | |
| 321 | requestAnimationFrame(frame); | |
| 322 | </script> | |
| 323 | </body> | |
| 324 | </html> |
hot-air-balloon/page.test.mjs added +38
| @@ -0,0 +1,38 @@ | ||
| 1 | import { test, expect } from 'bun:test'; | |
| 2 | import { boot } from '../playtest.mjs'; | |
| 3 | ||
| 4 | const state = g => g.read('F'); | |
| 5 | ||
| 6 | test('holding the canvas fires the burner and the balloon climbs; letting go sinks it', () => { | |
| 7 | const g = boot('hot-air-balloon'); | |
| 8 | g.run(.3); | |
| 9 | expect(g.text()).toContain('m up'); | |
| 10 | const z0 = state(g).z; | |
| 11 | g.down(180, 140); | |
| 12 | g.run(4); | |
| 13 | const up = state(g).z; | |
| 14 | expect(up).toBeGreaterThan(z0); | |
| 15 | g.up(180, 140); | |
| 16 | g.run(2); | |
| 17 | expect(state(g).z).toBeLessThan(up); | |
| 18 | expect(g.errors).toEqual([]); | |
| 19 | }, 30000); | |
| 20 | ||
| 21 | test('Space held fires the burner too', () => { | |
| 22 | const g = boot('hot-air-balloon'); | |
| 23 | const z0 = state(g).z; | |
| 24 | g.keyDown(' '); g.run(3); g.keyUp(' '); | |
| 25 | expect(state(g).z).toBeGreaterThan(z0); | |
| 26 | expect(g.errors).toEqual([]); | |
| 27 | }, 30000); | |
| 28 | ||
| 29 | test('a landing is followed by a tap for the next flight', () => { | |
| 30 | const g = boot('hot-air-balloon'); | |
| 31 | for (let i = 0; i < 40 && state(g).phase === 'fly'; i++) g.run(1); | |
| 32 | expect(state(g).phase).toBe('landed'); | |
| 33 | g.tap(180, 140); | |
| 34 | g.run(.2); | |
| 35 | expect(state(g).flight).toBe(2); | |
| 36 | expect(state(g).phase).toBe('fly'); | |
| 37 | expect(g.errors).toEqual([]); | |
| 38 | }, 30000); |
index.html +1
| @@ -93,6 +93,7 @@ footer a::after { content: none; } | ||
| 93 | 93 | <li data-kind="Racing" data-input="Drag" style="--accent:#b5562b"><a href="soapbox-derby/">Soapbox Derby</a><span>Steer a wooden cart down a cobbled hill between the hay bales, fastest time wins</span><span>Game forged by the crew</span><span class="tags">Racing · Drag</span></li> |
| 94 | 94 | <li data-kind="Catching" data-input="Drag" style="--accent:#5b8a3a"><a href="sheepdog/">Sheepdog</a><span>Drag the dog to steer five sheep through the gate and into the pen before dusk</span><span>Game forged by the crew</span><span class="tags">Catching · Drag</span></li> |
| 95 | 95 | <li data-kind="Catching" data-input="Tap" style="--accent:#c9b43a"><a href="firefly-jar/">Firefly Jar</a><span>Tap the fireflies into a jar while they glow, a dim one flies off, shoo the moths before they reach the lantern</span><span>Game forged by the crew</span><span class="tags">Catching · Tap</span></li> |
| 96 | <li data-kind="Precision" data-input="Hold" style="--accent:#d0453c"><a href="hot-air-balloon/">Hot Air Balloon</a><span>Five flights over the farms: hold the burner to climb into a different wind, land on the cross for 5</span><span>Game forged by the crew</span><span class="tags">Precision · Hold</span></li> | |
| 96 | 97 | </ul> |
| 97 | 98 | <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> |
| 98 | 99 | </main> |
og/home.png
Binary file not shown.
og/hot-air-balloon.png added
Binary file not shown.
rounds.mjs +2
| @@ -38,6 +38,8 @@ export const ROUNDS = { | ||
| 38 | 38 | g.step(1); |
| 39 | 39 | } }, |
| 40 | 40 | 'harbour-crane': { over: /play again/, outcome: /\d+ of 10 crates/, beat: tapping(1.3) }, |
| 41 | // never fire the burner: each flight sinks where it starts, and a tap launches the next | |
| 42 | 'hot-air-balloon': { over: /fly again/, outcome: /\d+ points: (a fine day's flying, you win|short of \d+, you lose)/, beat: tapping(.5) }, | |
| 41 | 43 | 'ice-fishing': { over: /play again/, outcome: /three lines snapped · \d+ points/, beat: g => g.hold(...MID, 3) }, |
| 42 | 44 | 'kite-hill': { over: /fly again/, outcome: /\d+ · (new best|best)/, beat: g => g.hold(...MID, 1) }, |
| 43 | 45 | 'laundry': { over: /hang it again/, outcome: /\d+ dry · \d+ wet/, beat: g => { g.press(' '); g.run(1); } }, |
serve.test.mjs +7
| @@ -430,6 +430,13 @@ test('Sheepdog and its rules script are served without a track', async () => { | ||
| 430 | 430 | expect((await fetch(base + '/sheepdog/flock.js')).status).toBe(200); |
| 431 | 431 | }); |
| 432 | 432 | |
| 433 | test('Hot Air Balloon and its rules script are served without a track', async () => { | |
| 434 | const page = await fetch(base + '/hot-air-balloon/'); | |
| 435 | expect(page.status).toBe(200); | |
| 436 | expect(page.headers.get('content-type')).toBe('text/html; charset=utf-8'); | |
| 437 | expect((await fetch(base + '/hot-air-balloon/flight.js')).status).toBe(200); | |
| 438 | }); | |
| 439 | ||
| 433 | 440 | test('Espresso Bar and its rules script are served without a track', async () => { |
| 434 | 441 | const page = await fetch(base + '/espresso-bar/'); |
| 435 | 442 | expect(page.status).toBe(200); |
shell.js +1
| @@ -37,6 +37,7 @@ window.PixelPlays = (() => { | ||
| 37 | 37 | { id: 'soapbox-derby', title: 'Soapbox Derby', blurb: 'Steer a wooden cart down a cobbled hill between the hay bales, fastest time wins', kind: 'Racing', input: 'Drag', accent: '#b5562b', music: false, forged: true }, |
| 38 | 38 | { id: 'sheepdog', title: 'Sheepdog', blurb: 'Drag the dog to steer five sheep through the gate and into the pen before dusk', kind: 'Catching', input: 'Drag', accent: '#5b8a3a', music: false, forged: true }, |
| 39 | 39 | { id: 'firefly-jar', title: 'Firefly Jar', blurb: 'Tap the fireflies into a jar while they glow, a dim one flies off, shoo the moths before they reach the lantern', kind: 'Catching', input: 'Tap', accent: '#c9b43a', music: false, forged: true }, |
| 40 | { id: 'hot-air-balloon', title: 'Hot Air Balloon', blurb: 'Five flights over the farms: hold the burner to climb into a different wind, land on the cross for 5', kind: 'Precision', input: 'Hold', accent: '#d0453c', music: false, forged: true }, | |
| 40 | 41 | ]; |
| 41 | 42 | const MUSIC_VOLUME = .4; |
| 42 | 43 | const CREDITS = [ |
new above
offline
Conditional assertions: this test passes without checking the 'near' score if no such cell exists for the seed. Make it unconditional.