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 #116
Check · Review · Land · Live done — Landed and live in production.
vesper/pixel-plays:vesper/72-add-hot-air-balloon-ride-the-wind-layers → main 3 commits fork 3 threads · 3 unresolved
README.md +3 −2
| @@ -44,6 +44,7 @@ Concept by [abhi](https://x.com/awwbhi2). Game by [AmirMushich](https://x.com/Am | ||
| 44 | 44 | | [](tug-of-war/) | A village fete: two teams of four on a rope, a ribbon tied at its middle and a chalk line at each end. A caller beats a steady heave shown as a pulsing marker; a tap within 150 ms of the beat pulls the ribbon towards your line, a tap well off it (over 300 ms) makes your team stumble and the rope slips the other way, and not tapping lets the rival pull steadily, stronger each round. The ribbon crossing your line wins the pull, crossing theirs loses it; five pulls, three won is a win. The marker shows as a lit dot without animation under reduced motion, so it plays muted too (forged by the crew on the engine and shell above, not by the original creators; rules in `tug-of-war/rope.js`) | Tap anywhere on the picture; space | |
| 45 | 45 | | [](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 | |
| 46 | 46 | | [](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 | |
| 47 | | [](hot-air-balloon/) | Five flights over patchwork farm fields with hedgerows, a windsock and one field marked with a white cross: hold the burner and the balloon climbs slowly after a short lag, let go and it sinks; the air has three height bands that each blow their own way and a little differently each flight, shown by drifting cloud wisps, three arrows at the left and a pennant on the basket, so you steer only by choosing a height (the ready screen before each flight already shows that flight's winds, so read them first); touching the ground ends the flight and the landing scores by distance from the cross (on it 5, inside its field 3, a neighbouring field 1, trees or the pond 0), each flight from a new launch point; 15 or more of 25 wins; the balloon's shadow on the fields shows where it is over (forged by the crew on the engine and shell above, not by the original creators) | Press and hold the canvas; hold space or the up arrow | | |
| 47 | 48 | |
| 48 | 49 | ## Run |
| 49 | 50 | Requires [](https://nodejs.org/); nothing to install. |
| @@ -63,7 +64,7 @@ On the forge, `forge.json` runs `bun serve.mjs` with health check `/`. State is | ||
| 63 | 64 | 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. |
| 64 | 65 | |
| 65 | 66 | ## Add a game |
| 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 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`. | |
| 67 | 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`. | |
| 67 | 68 | |
| 68 | 69 | - `shell.js` provides the game menu, the Rules popup, the credits and the background music. |
| 69 | 70 | - `engine.js` provides the camera, depth buffer, colour ramps, shadows, edges, figures, trees and wind as globals. |
| @@ -79,7 +80,7 @@ Create `<id>/index.html`, load `../shell.js`, call `PixelPlays.mount({ id, rules | ||
| 79 | 80 | - Depth-buffer edges: darker where an object sits in front, a warm rim on upper-left edges. |
| 80 | 81 | - Large flat surfaces use closely spaced tones; wide steps make the dither read as a checkerboard. |
| 81 | 82 | - Characters stay sharp: flat bands with no dither, a dark silhouette on their own pixels, patterns at least 3px wide. |
| 82 | - Ambient motion only where the scene names it: grass and leaves sway in whole pixels at 8 fps, Tram Line's waiting passengers shift their weight, Espresso Bar's machine steam and awning fringe move, and Sheepdog's crow on the wall shifts and dips its head; all of it stops under `prefers-reduced-motion`. | |
| 83 | - Ambient motion only where the scene names it: grass and leaves sway in whole pixels at 8 fps, Tram Line's waiting passengers shift their weight, Espresso Bar's machine steam and awning fringe move, Sheepdog's crow on the wall shifts and dips its head, and Hot Air Balloon's windsock and cloud wisps drift; all of it stops under `prefers-reduced-motion`. | |
| 83 | 84 | |
| 84 | 85 | ## License |
| 85 | 86 | The code is under the [](LICENSE). The music is not: see [](MUSIC.md). |
hot-air-balloon/flight.js added +155
| @@ -0,0 +1,155 @@ | ||
| 1 | 'use strict'; | |
| 2 | // The Hot Air Balloon rules: a burner that lifts after a lag and a balloon that sinks when it is let go, three height | |
| 3 | // bands of wind that each blow their own way, a patchwork of farm fields with one marked with a cross, and five | |
| 4 | // flights scored by where the basket touches down. Positions are in field units: x and y lie on the ground, z is | |
| 5 | // height. Every random choice comes from the game's seed, so one seed and one sequence of inputs always play out the | |
| 6 | // same. Plain numbers in, plain numbers out, no DOM. | |
| 7 | (function (root) { | |
| 8 | const FLIGHTS = 5, WIN_AT = 15, MAX_SCORE = 5, TOTAL_MAX = FLIGHTS * MAX_SCORE; | |
| 9 | const HALF = 11, GRID = 4, CELL = 2 * HALF / GRID; // the farms: 22 wide, 4 by 4 fields | |
| 10 | const CEIL = 10, BANDS = [0, 3.4, 6.7, CEIL]; // low, middle and high wind | |
| 11 | const LAG = .5, CLIMB = 1.5, SINK = 1.2, ACCEL = 2.2; // burner lag in s, rates in units per s | |
| 12 | const DRAG = 1.1, LAUNCH_Z = 2.6, STEP = 1 / 60; // how fast the balloon takes up the wind | |
| 13 | const CROSS_R = 1, POND_R = 1.9, TREE_R = 1.1, TREES = 3; | |
| 14 | const BASE = [{ angle: 180, speed: 1.4 }, { angle: -20, speed: 1.1 }, { angle: 100, speed: 1.6 }]; // screen degrees, 0 is right, 90 is down | |
| 15 | const R2 = Math.SQRT2; | |
| 16 | const clamp = (v, a, b) => Math.max(a, Math.min(b, v)); | |
| 17 | ||
| 18 | function random(seed) { | |
| 19 | let s = (Math.imul(seed | 0, 2654435761) >>> 0) || 1; | |
| 20 | return () => (s = (Math.imul(s, 1664525) + 1013904223) >>> 0) / 4294967296; | |
| 21 | } | |
| 22 | const cellCentre = (i, j) => ({ x: -HALF + (i + .5) * CELL, y: -HALF + (j + .5) * CELL }); | |
| 23 | const cellOf = (x, y) => ({ i: clamp(Math.floor((x + HALF) / CELL), 0, GRID - 1), j: clamp(Math.floor((y + HALF) / CELL), 0, GRID - 1) }); | |
| 24 | ||
| 25 | // one farm per game: the marked field is one of the four in the middle, the pond and the trees sit in other fields | |
| 26 | function layoutOf(seed) { | |
| 27 | const next = random(seed * 31 + 7); | |
| 28 | const cross = { i: 1 + Math.floor(next() * 2), j: 1 + Math.floor(next() * 2) }; | |
| 29 | const spare = []; | |
| 30 | for (let j = 0; j < GRID; j++) for (let i = 0; i < GRID; i++) if (i !== cross.i || j !== cross.j) spare.push({ i, j }); | |
| 31 | const take = () => spare.splice(Math.floor(next() * spare.length), 1)[0]; | |
| 32 | const c = cellCentre(cross.i, cross.j); | |
| 33 | const at = { x: c.x + (next() - .5) * 2, y: c.y + (next() - .5) * 2 }; | |
| 34 | const pondCell = take(), p = cellCentre(pondCell.i, pondCell.j); | |
| 35 | const trees = []; | |
| 36 | for (let n = 0; n < TREES; n++) { | |
| 37 | const t = take(), q = cellCentre(t.i, t.j); | |
| 38 | trees.push({ x: q.x + (next() - .5) * 2, y: q.y + (next() - .5) * 2, r: TREE_R }); | |
| 39 | } | |
| 40 | const tones = []; | |
| 41 | for (let k = 0; k < GRID * GRID; k++) tones.push(Math.floor(next() * 5)); | |
| 42 | return { cross: { i: cross.i, j: cross.j, x: at.x, y: at.y, r: CROSS_R }, pond: { x: p.x, y: p.y, r: POND_R }, trees, tones }; | |
| 43 | } | |
| 44 | ||
| 45 | // where a basket touching down scores: the cross 5, its field 3, a field next to it 1, a tree or the pond 0 | |
| 46 | function landing(layout, x, y) { | |
| 47 | if (Math.abs(x) > HALF || Math.abs(y) > HALF) return { score: 0, kind: 'away' }; | |
| 48 | if (Math.hypot(x - layout.pond.x, y - layout.pond.y) < layout.pond.r) return { score: 0, kind: 'pond' }; | |
| 49 | for (const t of layout.trees) if (Math.hypot(x - t.x, y - t.y) < t.r) return { score: 0, kind: 'tree' }; | |
| 50 | if (Math.hypot(x - layout.cross.x, y - layout.cross.y) <= CROSS_R) return { score: 5, kind: 'cross' }; | |
| 51 | const c = cellOf(x, y), di = Math.abs(c.i - layout.cross.i), dj = Math.abs(c.j - layout.cross.j); | |
| 52 | if (!di && !dj) return { score: 3, kind: 'field' }; | |
| 53 | if (di <= 1 && dj <= 1) return { score: 1, kind: 'neighbour' }; | |
| 54 | return { score: 0, kind: 'far' }; | |
| 55 | } | |
| 56 | ||
| 57 | const bandOf = z => (z < BANDS[1] ? 0 : z < BANDS[2] ? 1 : 2); | |
| 58 | ||
| 59 | // the wind in each band for one flight: the base directions shift a little, as do the speeds. Also a world vector | |
| 60 | // (the screen right is +x -y, the screen down is +x +y) and the screen angle the page draws | |
| 61 | function windsOf(seed, flight) { | |
| 62 | const next = random(seed * 1009 + flight * 17 + 3); | |
| 63 | return BASE.map(b => { | |
| 64 | const angle = b.angle + (next() - .5) * 40, speed = b.speed * (.85 + next() * .3); | |
| 65 | const a = Math.cos(angle * Math.PI / 180) * speed, d = Math.sin(angle * Math.PI / 180) * speed; | |
| 66 | return { angle, speed, wx: (a + d) / R2, wy: (d - a) / R2 }; | |
| 67 | }); | |
| 68 | } | |
| 69 | ||
| 70 | // a launch point from which some mix of the three winds carries the balloon to the cross | |
| 71 | function launchOf(layout, winds, seed, flight) { | |
| 72 | const next = random(seed * 4001 + flight * 29 + 5), lim = HALF - 1.5; | |
| 73 | let p = null; | |
| 74 | for (let tries = 0; tries < 24 && !p; tries++) { | |
| 75 | let x = layout.cross.x, y = layout.cross.y; | |
| 76 | for (const w of winds) { const t = 2.5 + next() * 4.5; x -= w.wx * t; y -= w.wy * t; } | |
| 77 | if (Math.abs(x) <= lim && Math.abs(y) <= lim) p = { x, y }; | |
| 78 | else if (tries === 23) p = { x: clamp(x, -lim, lim), y: clamp(y, -lim, lim) }; | |
| 79 | } | |
| 80 | return p; | |
| 81 | } | |
| 82 | ||
| 83 | function prepare(s) { | |
| 84 | s.winds = windsOf(s.seed, s.flight); | |
| 85 | const p = launchOf(s.layout, s.winds, s.seed, s.flight); | |
| 86 | Object.assign(s, { x: p.x, y: p.y, z: LAUNCH_Z, vx: s.winds[0].wx, vy: s.winds[0].wy, vz: 0, burn: false, held: 0, t: 0, landed: null }); | |
| 87 | s.launch = { x: p.x, y: p.y }; | |
| 88 | } | |
| 89 | function newGame(seed = 1, best = 0) { | |
| 90 | const s = { seed, layout: layoutOf(seed), flight: 1, phase: 'ready', total: 0, scores: [], best, record: false }; | |
| 91 | prepare(s); | |
| 92 | return s; | |
| 93 | } | |
| 94 | const begin = s => { if (s.phase === 'ready') s.phase = 'fly'; return s; }; | |
| 95 | function burner(s, on) { | |
| 96 | s.burn = !!on; | |
| 97 | if (!on) s.held = 0; | |
| 98 | } | |
| 99 | ||
| 100 | function touchDown(s) { | |
| 101 | const hit = landing(s.layout, s.x, s.y); | |
| 102 | s.landed = { x: s.x, y: s.y, score: hit.score, kind: hit.kind }; | |
| 103 | s.scores.push(hit.score); | |
| 104 | s.total += hit.score; | |
| 105 | s.burn = false; s.held = 0; s.vz = 0; | |
| 106 | s.phase = s.flight >= FLIGHTS ? 'over' : 'landed'; | |
| 107 | const events = [{ type: 'land', score: hit.score, kind: hit.kind }]; | |
| 108 | if (s.phase === 'over') { | |
| 109 | if (s.total > s.best) { s.best = s.total; s.record = true; } | |
| 110 | events.push({ type: 'over', total: s.total }); | |
| 111 | } | |
| 112 | return events; | |
| 113 | } | |
| 114 | ||
| 115 | // run `dt` seconds of flight in short steps; the events say what happened | |
| 116 | function advance(s, dt) { | |
| 117 | if (s.phase !== 'fly') return []; | |
| 118 | const events = []; | |
| 119 | for (let left = dt; left > 1e-9 && s.phase === 'fly'; left -= STEP) { | |
| 120 | const h = Math.min(STEP, left); | |
| 121 | s.held = s.burn ? s.held + h : 0; | |
| 122 | const target = s.held >= LAG ? CLIMB : -SINK; | |
| 123 | s.vz += clamp(target - s.vz, -ACCEL * h, ACCEL * h); | |
| 124 | s.z = Math.min(CEIL, s.z + s.vz * h); | |
| 125 | if (s.z >= CEIL && s.vz > 0) s.vz = 0; | |
| 126 | const w = s.winds[bandOf(s.z)], k = Math.min(1, DRAG * h); | |
| 127 | s.vx += (w.wx - s.vx) * k; s.vy += (w.wy - s.vy) * k; | |
| 128 | s.x += s.vx * h; s.y += s.vy * h; | |
| 129 | s.t += h; | |
| 130 | if (s.z <= 0) { s.z = 0; events.push(...touchDown(s)); } | |
| 131 | else if (Math.abs(s.x) > HALF || Math.abs(s.y) > HALF) events.push(...touchDown(s)); | |
| 132 | } | |
| 133 | return events; | |
| 134 | } | |
| 135 | ||
| 136 | // the next flight: a new launch point and a little new wind | |
| 137 | function next(s) { | |
| 138 | if (s.phase !== 'landed') return s; | |
| 139 | s.flight += 1; s.phase = 'ready'; | |
| 140 | prepare(s); | |
| 141 | return s; | |
| 142 | } | |
| 143 | const again = s => newGame(s.seed + 1, s.best); | |
| 144 | const verdict = s => { | |
| 145 | if (s.phase !== 'over') return null; | |
| 146 | const win = s.total >= WIN_AT; | |
| 147 | return { win, line: win ? `good flying · ${s.total} of ${TOTAL_MAX}` : `blown off course · ${s.total} of ${TOTAL_MAX}` }; | |
| 148 | }; | |
| 149 | ||
| 150 | const api = { | |
| 151 | FLIGHTS, WIN_AT, TOTAL_MAX, HALF, GRID, CELL, CEIL, BANDS, LAG, CLIMB, SINK, ACCEL, DRAG, LAUNCH_Z, CROSS_R, POND_R, TREE_R, BASE, | |
| 152 | layoutOf, cellCentre, cellOf, landing, bandOf, windsOf, launchOf, newGame, begin, burner, advance, next, again, verdict, | |
| 153 | }; | |
| 154 | if (typeof module !== 'undefined') module.exports = api; else root.Flight = api; | |
| 155 | })(typeof window !== 'undefined' ? window : globalThis); |
hot-air-balloon/flight.test.mjs added +204
| @@ -0,0 +1,204 @@ | ||
| 1 | import { test, expect } from 'bun:test'; | |
| 2 | import { | |
| 3 | FLIGHTS, WIN_AT, HALF, CELL, BANDS, LAG, CLIMB, SINK, LAUNCH_Z, CROSS_R, | |
| 4 | layoutOf, cellCentre, landing, bandOf, windsOf, newGame, begin, burner, advance, next, again, verdict, | |
| 5 | } from './flight.js'; | |
| 6 | ||
| 7 | const DT = 1 / 30; | |
| 8 | const run = (s, seconds, each) => { const out = []; for (let t = 0; t < seconds - 1e-9; t += DT) { each?.(s); out.push(...advance(s, DT)); } return out; }; | |
| 9 | const fly = (seed = 1) => { const s = newGame(seed); begin(s); return s; }; | |
| 10 | // park the balloon at a height with no vertical speed and the wind of its band already blowing | |
| 11 | const at = (s, z, x = 0, y = 0) => { Object.assign(s, { x, y, z, vz: 0 }); const w = s.winds[bandOf(z)]; s.vx = w.wx; s.vy = w.wy; }; | |
| 12 | ||
| 13 | test('holding the burner raises the climb only after the lag, and letting go sinks', () => { | |
| 14 | const s = fly(); | |
| 15 | at(s, 5); | |
| 16 | burner(s, true); | |
| 17 | run(s, LAG - .1); | |
| 18 | expect(s.vz).toBeLessThan(0); // still sinking inside the lag | |
| 19 | const dipped = s.z; | |
| 20 | run(s, 1.5); | |
| 21 | expect(s.vz).toBeGreaterThan(0); // then the climb takes over, slowly | |
| 22 | expect(s.vz).toBeLessThanOrEqual(CLIMB + 1e-9); | |
| 23 | expect(s.z).toBeGreaterThan(dipped); | |
| 24 | burner(s, false); | |
| 25 | run(s, .1); | |
| 26 | expect(s.vz).toBeLessThan(CLIMB); | |
| 27 | run(s, 2); | |
| 28 | expect(s.vz).toBeCloseTo(-SINK, 1); | |
| 29 | }); | |
| 30 | ||
| 31 | test('a tap shorter than the lag never lifts the balloon', () => { | |
| 32 | const s = fly(); | |
| 33 | at(s, 5); | |
| 34 | burner(s, true); run(s, LAG - .15); burner(s, false); | |
| 35 | const z = s.z; | |
| 36 | run(s, 1); | |
| 37 | expect(s.z).toBeLessThan(z); | |
| 38 | expect(s.vz).toBeLessThan(0); | |
| 39 | }); | |
| 40 | ||
| 41 | test('the climb stops at the ceiling', () => { | |
| 42 | const s = fly(); | |
| 43 | at(s, 9.5); | |
| 44 | burner(s, true); | |
| 45 | run(s, 6); | |
| 46 | expect(s.z).toBe(BANDS[3]); | |
| 47 | expect(s.phase).toBe('fly'); | |
| 48 | }); | |
| 49 | ||
| 50 | test('three bands, each drifting the balloon its own way', () => { | |
| 51 | expect(BANDS.length).toBe(4); | |
| 52 | expect([bandOf(1), bandOf(5), bandOf(9)]).toEqual([0, 1, 2]); | |
| 53 | const ends = [1.5, 5, 8.5].map(z => { | |
| 54 | const s = fly(); | |
| 55 | at(s, z); | |
| 56 | // hold height by hand so only the wind moves it | |
| 57 | run(s, 3, t => { t.z = z; t.vz = 0; }); | |
| 58 | return [s.x - 0, s.y - 0]; | |
| 59 | }); | |
| 60 | const [lo, mid, hi] = ends; | |
| 61 | const dist = (a, b) => Math.hypot(a[0] - b[0], a[1] - b[1]); | |
| 62 | expect(dist(lo, mid)).toBeGreaterThan(2); | |
| 63 | expect(dist(mid, hi)).toBeGreaterThan(2); | |
| 64 | expect(dist(lo, hi)).toBeGreaterThan(2); | |
| 65 | const w = fly().winds; | |
| 66 | expect(new Set(w.map(v => Math.round(v.angle))).size).toBe(3); | |
| 67 | // each band drifts along its own wind vector | |
| 68 | ends.forEach((e, k) => { expect(Math.sign(e[0])).toBe(Math.sign(w[k].wx) || 0); expect(Math.sign(e[1])).toBe(Math.sign(w[k].wy) || 0); }); | |
| 69 | }); | |
| 70 | ||
| 71 | test('the balloon takes up a new wind gradually, not at once', () => { | |
| 72 | const s = fly(); | |
| 73 | at(s, 1.5); | |
| 74 | run(s, 2, t => { t.z = 1.5; t.vz = 0; }); | |
| 75 | const before = [s.vx, s.vy]; | |
| 76 | Object.assign(s, { z: 8.5, vz: 0 }); | |
| 77 | advance(s, DT); | |
| 78 | expect(Math.hypot(s.vx - before[0], s.vy - before[1])).toBeLessThan(Math.hypot(s.winds[2].wx - before[0], s.winds[2].wy - before[1]) * .2); | |
| 79 | }); | |
| 80 | ||
| 81 | test('winds shift a little from one flight to the next, and are the same for the same seed', () => { | |
| 82 | const a = windsOf(1, 1), b = windsOf(1, 2); | |
| 83 | expect(windsOf(1, 1)).toEqual(a); | |
| 84 | expect(b).not.toEqual(a); | |
| 85 | a.forEach((w, k) => { expect(Math.abs(w.angle - b[k].angle)).toBeLessThan(45); expect(Math.abs(w.speed - b[k].speed)).toBeLessThan(.6); }); | |
| 86 | }); | |
| 87 | ||
| 88 | test('each flight starts from a new launch point', () => { | |
| 89 | const s = newGame(3); | |
| 90 | const seen = [[s.x, s.y]]; | |
| 91 | for (let f = 1; f < FLIGHTS; f++) { s.phase = 'landed'; next(s); seen.push([s.x, s.y]); expect(s.phase).toBe('ready'); expect(s.z).toBe(LAUNCH_Z); } | |
| 92 | expect(new Set(seen.map(p => p.join())).size).toBe(FLIGHTS); | |
| 93 | for (const [x, y] of seen) { expect(Math.abs(x)).toBeLessThan(HALF); expect(Math.abs(y)).toBeLessThan(HALF); } | |
| 94 | }); | |
| 95 | ||
| 96 | test('the farm has a cross in a middle field, a pond and trees clear of it', () => { | |
| 97 | for (let seed = 1; seed <= 30; seed++) { | |
| 98 | const L = layoutOf(seed); | |
| 99 | expect([1, 2]).toContain(L.cross.i); | |
| 100 | expect([1, 2]).toContain(L.cross.j); | |
| 101 | const c = cellCentre(L.cross.i, L.cross.j); | |
| 102 | expect(Math.abs(L.cross.x - c.x)).toBeLessThanOrEqual(1); | |
| 103 | expect(L.trees.length).toBe(3); | |
| 104 | expect(landing(L, L.pond.x, L.pond.y).kind).toBe('pond'); | |
| 105 | for (const t of L.trees) expect(landing(L, t.x, t.y).kind).toBe('tree'); | |
| 106 | } | |
| 107 | expect(layoutOf(1)).toEqual(layoutOf(1)); | |
| 108 | expect(layoutOf(2)).not.toEqual(layoutOf(1)); | |
| 109 | }); | |
| 110 | ||
| 111 | test('landing scores by distance from the cross: 5, 3, 1, then 0 for trees and the pond', () => { | |
| 112 | const L = layoutOf(1), { cross } = L, c = cellCentre(cross.i, cross.j); | |
| 113 | expect(landing(L, cross.x, cross.y)).toEqual({ score: 5, kind: 'cross' }); | |
| 114 | expect(landing(L, cross.x + CROSS_R * .9, cross.y).score).toBe(5); | |
| 115 | // a spot in the marked field but off the cross | |
| 116 | const inField = [c.x + (cross.x > c.x ? -2.3 : 2.3), c.y + (cross.y > c.y ? -2.3 : 2.3)]; | |
| 117 | expect(landing(L, ...inField)).toEqual({ score: 3, kind: 'field' }); | |
| 118 | // a field next door that has neither a tree nor the pond | |
| 119 | let neighbour = null; | |
| 120 | for (let j = 0; j < 4; j++) for (let i = 0; i < 4; i++) { | |
| 121 | const p = cellCentre(i, j); | |
| 122 | if (Math.max(Math.abs(i - cross.i), Math.abs(j - cross.j)) === 1 && landing(L, p.x + 2.6, p.y + 2.6).kind === 'neighbour') neighbour = [p.x + 2.6, p.y + 2.6]; | |
| 123 | } | |
| 124 | expect(neighbour).not.toBeNull(); | |
| 125 | expect(landing(L, ...neighbour).score).toBe(1); | |
| 126 | expect(landing(L, L.pond.x, L.pond.y).score).toBe(0); | |
| 127 | expect(landing(L, L.trees[0].x, L.trees[0].y).score).toBe(0); | |
| 128 | expect(landing(L, HALF + 1, 0)).toEqual({ score: 0, kind: 'away' }); | |
| 129 | // a field two away from the cross scores nothing | |
| 130 | const far = [...Array(16).keys()].map(k => cellCentre(k % 4, Math.floor(k / 4))).find(p => { | |
| 131 | const i = Math.floor((p.x + HALF) / CELL), j = Math.floor((p.y + HALF) / CELL); | |
| 132 | return Math.max(Math.abs(i - cross.i), Math.abs(j - cross.j)) >= 2 && landing(L, p.x + 2.6, p.y + 2.6).kind === 'far'; | |
| 133 | }); | |
| 134 | expect(far).toBeDefined(); | |
| 135 | }); | |
| 136 | ||
| 137 | test('touching the ground ends the flight with the landing score', () => { | |
| 138 | const s = fly(); | |
| 139 | at(s, .2, s.layout.cross.x, s.layout.cross.y); | |
| 140 | s.winds.forEach(w => { w.wx = 0; w.wy = 0; }); s.vx = 0; s.vy = 0; | |
| 141 | const events = run(s, 2); | |
| 142 | expect(events[0]).toEqual({ type: 'land', score: 5, kind: 'cross' }); | |
| 143 | expect(s.phase).toBe('landed'); | |
| 144 | expect(s.total).toBe(5); | |
| 145 | expect(s.scores).toEqual([5]); | |
| 146 | expect(s.z).toBe(0); | |
| 147 | expect(advance(s, 1)).toEqual([]); // nothing moves after touchdown | |
| 148 | }); | |
| 149 | ||
| 150 | test('drifting off the farms ends the flight with nothing', () => { | |
| 151 | const s = fly(); | |
| 152 | at(s, 5, HALF - .05, 0); | |
| 153 | s.winds[1].wx = 3; s.vx = 3; | |
| 154 | const events = run(s, 2); | |
| 155 | expect(events[0]).toEqual({ type: 'land', score: 0, kind: 'away' }); | |
| 156 | }); | |
| 157 | ||
| 158 | test('the fifth landing ends the game: 15 or more wins, below that loses', () => { | |
| 159 | const play = scores => { | |
| 160 | const s = newGame(5, 0); | |
| 161 | scores.forEach((want, n) => { | |
| 162 | begin(s); | |
| 163 | const L = s.layout; | |
| 164 | const spot = want === 5 ? [L.cross.x, L.cross.y] : want === 3 ? [cellCentre(L.cross.i, L.cross.j).x + (L.cross.x > cellCentre(L.cross.i, L.cross.j).x ? -2.3 : 2.3), cellCentre(L.cross.i, L.cross.j).y + (L.cross.y > cellCentre(L.cross.i, L.cross.j).y ? -2.3 : 2.3)] : [L.pond.x, L.pond.y]; | |
| 165 | s.winds.forEach(w => { w.wx = 0; w.wy = 0; }); | |
| 166 | Object.assign(s, { x: spot[0], y: spot[1], z: .1, vx: 0, vy: 0, vz: 0 }); | |
| 167 | const ev = run(s, 1); | |
| 168 | expect(ev.some(e => e.type === 'land' && e.score === want)).toBe(true); | |
| 169 | if (n < scores.length - 1) { expect(s.phase).toBe('landed'); expect(verdict(s)).toBeNull(); next(s); } | |
| 170 | }); | |
| 171 | return s; | |
| 172 | }; | |
| 173 | const win = play([5, 3, 3, 3, 3]); // 17 | |
| 174 | expect(win.phase).toBe('over'); | |
| 175 | expect(win.total).toBe(17); | |
| 176 | expect(verdict(win)).toEqual({ win: true, line: 'good flying · 17 of 25' }); | |
| 177 | const edge = play([5, 5, 5, 0, 0]); // exactly WIN_AT | |
| 178 | expect(WIN_AT).toBe(15); | |
| 179 | expect(edge.total).toBe(15); | |
| 180 | expect(verdict(edge).win).toBe(true); | |
| 181 | const lose = play([5, 5, 3, 0, 0]); // 13 | |
| 182 | expect(verdict(lose)).toEqual({ win: false, line: 'blown off course · 13 of 25' }); | |
| 183 | expect(lose.flight).toBe(FLIGHTS); | |
| 184 | expect(lose.best).toBe(13); | |
| 185 | expect(lose.record).toBe(true); | |
| 186 | }); | |
| 187 | ||
| 188 | test('a new game keeps the best and changes the farm', () => { | |
| 189 | const s = newGame(1, 9); | |
| 190 | const t = again(s); | |
| 191 | expect(t.best).toBe(9); | |
| 192 | expect(t.flight).toBe(1); | |
| 193 | expect(t.phase).toBe('ready'); | |
| 194 | expect(t.layout).not.toEqual(s.layout); | |
| 195 | }); | |
| 196 | ||
| 197 | test('the launch point is one the winds can carry to the cross', () => { | |
| 198 | for (let seed = 1; seed <= 20; seed++) { | |
| 199 | const s = newGame(seed); | |
| 200 | const d = Math.hypot(s.x - s.layout.cross.x, s.y - s.layout.cross.y); | |
| 201 | expect(d).toBeGreaterThan(1.5); | |
| 202 | expect(d).toBeLessThan(26); | |
| 203 | } | |
| 204 | }); |
hot-air-balloon/index.html added +382
| @@ -0,0 +1,382 @@ | ||
| 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; height: 100%; margin: 0; background: var(); color: var(); } | |
| 26 | body { font: 13px/1.35 ui-monospace, "Cascadia Mono", Consolas, Menlo, monospace; overflow: hidden; } | |
| 27 | #stage { position: fixed; inset: 0; display: grid; place-items: center; } | |
| 28 | canvas { display: block; image-rendering: pixelated; touch-action: none; cursor: pointer; outline: none; } | |
| 29 | .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; } | |
| 30 | #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; } | |
| 31 | #bottom { bottom: max(14px, env(safe-area-inset-bottom)); align-items: flex-end; } | |
| 32 | .row { display: flex; align-items: center; gap: 8px; color: var(); } | |
| 33 | .row b { font-size: 16px; font-weight: 600; min-width: 1ch; font-variant-numeric: tabular-nums; } | |
| 34 | .row span { color: var(); } | |
| 35 | #status { color: var(); min-height: 1.35em; } | |
| 36 | button { pointer-events: auto; font: inherit; color: var(); background: none; border: 0; padding: 10px; margin: -10px; cursor: pointer; } | |
| 37 | button:hover, button:focus-visible { color: var(); } | |
| 38 | </style> | |
| 39 | </head> | |
| 40 | <body> | |
| 41 | <div id="stage"><canvas id="c" tabindex="0" aria-label="Patchwork farm fields seen from above with hedgerows, a windsock and one field marked with a white cross, and a hot air balloon over them. Press and hold anywhere on the canvas, or hold Space or the Up arrow, to fire the burner: after a short lag the balloon climbs slowly, and when you let go it sinks. The air has three height bands and each blows a different way, shown by the drifting cloud wisps, the three arrows at the left and the pennant on the basket. You steer only by choosing a height. Touching the ground ends the flight: on the cross scores 5, inside its field 3, a neighbouring field 1, trees or the pond 0. Five flights, and 15 or more of 25 wins."></canvas></div> | |
| 42 | <div id="scores"> | |
| 43 | <div class="row"><b id="flight">1</b><span>of 5 flights</span></div> | |
| 44 | <div class="row"><b id="height">26</b><span>m up</span></div> | |
| 45 | <div class="row"><b id="total">0</b><span>of 25 in all</span></div> | |
| 46 | <div class="row"><b id="best">0</b><span>best</span></div> | |
| 47 | </div> | |
| 48 | <div class="hud" id="bottom"> | |
| 49 | <div id="status" role="status" aria-live="polite"></div> | |
| 50 | <button id="mute" type="button" aria-pressed="false">sound on</button> | |
| 51 | </div> | |
| 52 | <script src="../shell.js"></script> | |
| 53 | <script> | |
| 54 | PixelPlays.mount({ | |
| 55 | id: 'hot-air-balloon', | |
| 56 | rules: [ | |
| 57 | 'Hold the canvas, Space or Up to fire the burner. The balloon climbs slowly after a lag, and sinks when you let go.', | |
| 58 | 'Each height band blows a different way, shown by the cloud wisps and the pennant. Choose a height to steer.', | |
| 59 | 'Touching the ground ends the flight: the cross 5, its field 3, a neighbouring field 1, trees or the pond 0. Five flights, 15 wins.', | |
| 60 | ], | |
| 61 | }); | |
| 62 | </script> | |
| 63 | <script>window.PP_SCENE = { originY: 45, shadowX: 1.3, shadowY: -.12 };</script> | |
| 64 | <script src="../engine.js"></script> | |
| 65 | <script src="flight.js"></script> | |
| 66 | <script> | |
| 67 | 'use strict'; | |
| 68 | ||
| 69 | const BEST_KEY = 'pixel-plays-hot-air-balloon-best'; | |
| 70 | const { HALF, GRID, CELL, BANDS } = Flight; | |
| 71 | const FIELD_TONES = [ | |
| 72 | [[84, 132, 60], [96, 144, 62], [108, 152, 66], [120, 160, 70]], // meadow | |
| 73 | [[190, 164, 76], [202, 176, 82], [212, 188, 90], [222, 198, 100]], // wheat | |
| 74 | [[126, 92, 64], [138, 102, 70], [150, 112, 76], [162, 122, 84]], // ploughed | |
| 75 | [[56, 102, 66], [64, 112, 70], [72, 122, 74], [82, 132, 80]], // dark crop | |
| 76 | [[178, 182, 72], [190, 192, 78], [200, 200, 86], [210, 208, 96]], // rape | |
| 77 | ].map(t => t.map(c => rgb(...c))); | |
| 78 | const WATER = [rgb(58, 104, 156), rgb(76, 126, 172), rgb(98, 148, 188)]; | |
| 79 | const HEDGE = [52, 92, 54], POLE = [74, 70, 64]; | |
| 80 | const RED = [208, 69, 60], CREAM = [244, 232, 208], BASKET = [140, 98, 60]; | |
| 81 | const WISP = rgb(132, 156, 196), WISP_HEAD = rgb(78, 104, 160), CELL_BG = rgb(250, 248, 240); | |
| 82 | const SOCK_AT = [-HALF + 1.6, HALF - 1.6]; | |
| 83 | const BAND_Z = [1.7, 5.05, 8.35], WISPS = 7, WISP_R = HALF * 1.25; | |
| 84 | const NAMES = ['low', 'middle', 'high']; | |
| 85 | const WHERE = { cross: 'on the cross', field: 'in the marked field', neighbour: 'in a neighbouring field', tree: 'in the trees', pond: 'in the pond', far: 'far from the cross', away: 'blown off the farms' }; | |
| 86 | ||
| 87 | let G = null, time = 0, status = '', overT = 0, landT = 0, wispT = 0; | |
| 88 | const cloud = [0, 1, 2].map(() => ({ x: 0, y: 0 })); | |
| 89 | const best0 = (() => { try { return Number(localStorage.getItem(BEST_KEY)) || 0; } catch (e) { return 0; } })(); | |
| 90 | const nodes = {}; | |
| 91 | for (const id of ['flight', 'height', 'total', 'best', 'status']) nodes[id] = document.getElementById(id); | |
| 92 | ||
| 93 | /* ---------- ground ---------- */ | |
| 94 | function groundColor(x, y, ix, iy) { | |
| 95 | const L = G.layout; | |
| 96 | const rough = Math.max(Math.abs(x), Math.abs(y)) / HALF + (noise(x * 1.3, y * 1.3) - .5) * .08; | |
| 97 | if (rough > 1.03) return 0; | |
| 98 | if (rough > .94 && hash(ix, iy) < (rough - .94) / .09) return 0; | |
| 99 | const dx = x - L.cross.x, dy = y - L.cross.y; | |
| 100 | if ((Math.abs(dx) < .17 && Math.abs(dy) < Flight.CROSS_R) || (Math.abs(dy) < .17 && Math.abs(dx) < Flight.CROSS_R)) return PAPER; | |
| 101 | const d = Math.hypot(x - L.pond.x, y - L.pond.y); | |
| 102 | if (d < L.pond.r + (noise(x * 2.1, y * 2.1) - .5) * .5) return WATER[clamp(Math.floor(hash(ix, iy) * .5 + d / L.pond.r * .6 + noise(x * 3, y * 3) * .3), 0, 2)]; | |
| 103 | const c = Flight.cellOf(x, y), tones = FIELD_TONES[L.tones[c.j * GRID + c.i]]; | |
| 104 | const stripe = Math.floor(((c.i + c.j) & 1 ? x : y) * 1.5) & 1; | |
| 105 | return tones[clamp(Math.floor(noise(x * .8, y * .8) * 2.2 + hash(ix, iy) * .7 + stripe * 1.1 - .3), 0, 3)]; | |
| 106 | } | |
| 107 | function bake() { | |
| 108 | leaves.length = 0; | |
| 109 | zb.fill(-1e9); pix.fill(PAPER); ground.fill(0); shadowMask.fill(0); staticShadow.fill(0); | |
| 110 | for (let iy = 0; iy < H; iy++) for (let ix = 0; ix < W; ix++) { | |
| 111 | const g = unproject(ix + .5, iy + .5), i = iy * W + ix, c = groundColor(g.x, g.y, ix, iy); | |
| 112 | if (!c) continue; | |
| 113 | pix[i] = c; ground[i] = 1; | |
| 114 | zb[i] = (g.x + g.y) / R2 * COS; | |
| 115 | } | |
| 116 | groundZ.set(zb); | |
| 117 | // hedgerows along every field edge, a little shorter than a person | |
| 118 | for (let k = 0; k <= GRID; k++) { | |
| 119 | const edge = -HALF + k * CELL, a = k === 0 || k === GRID ? .16 : .11; | |
| 120 | for (let m = 0; m < GRID; m++) { | |
| 121 | const lo = -HALF + m * CELL + .1, hi = lo + CELL - .2; | |
| 122 | box(edge - a, lo, 0, edge + a, hi, .42, HEDGE, k * 5 + m); | |
| 123 | box(lo, edge - a, 0, hi, edge + a, .42, HEDGE, k * 5 + m + 40); | |
| 124 | for (let t = lo; t <= hi; t += .4) { castShadow(edge, t, .45, .25, 1, 0); castShadow(t, edge, .45, .25, 1, 0); } | |
| 125 | } | |
| 126 | } | |
| 127 | G.layout.trees.forEach((t, n) => { | |
| 128 | growTree(t.x, t.y, .9, 300, 11 + n * 97); | |
| 129 | castShadow(t.x, t.y, 1.4, 1.2, 1, .4); | |
| 130 | }); | |
| 131 | drawLeaves(0); // the trees are painted once and stay still | |
| 132 | capsule([SOCK_AT[0], SOCK_AT[1], 0], [SOCK_AT[0], SOCK_AT[1], 3], .07, .07, () => POLE, 'solid'); | |
| 133 | for (let z = .3; z < 3; z += .5) castShadow(SOCK_AT[0], SOCK_AT[1], z, .1, 1, 0); | |
| 134 | bakeOcclusion(); | |
| 135 | staticShadow.set(shadowMask); | |
| 136 | applyShadows(groundZ); | |
| 137 | bakedPix.set(pix); | |
| 138 | bakedZ.set(zb); | |
| 139 | } | |
| 140 | ||
| 141 | /* ---------- state ---------- */ | |
| 142 | function setStatus(text) { | |
| 143 | if (text === status) return; | |
| 144 | status = text; | |
| 145 | nodes.status.textContent = text; | |
| 146 | } | |
| 147 | function statusFor() { | |
| 148 | const v = Flight.verdict(G); | |
| 149 | if (G.phase === 'over') return v.line + (G.record ? ' · new best' : '') + ' · tap to play again'; | |
| 150 | if (G.phase === 'landed') return WHERE[G.landed.kind] + ' · +' + G.landed.score + ' · tap for flight ' + (G.flight + 1); | |
| 151 | if (G.phase === 'ready') return (G.flight === 1 ? 'read the winds' : 'flight ' + G.flight) + ' · hold to fire the burner'; | |
| 152 | return NAMES[Flight.bandOf(G.z)] + ' wind' + (G.burn && G.held < Flight.LAG ? ' · burner warming' : ''); | |
| 153 | } | |
| 154 | let shown = ''; | |
| 155 | function renderHud() { | |
| 156 | const text = [G.flight, Math.round(G.z * 10), G.total, G.best].join('|'); | |
| 157 | if (text === shown) return; | |
| 158 | shown = text; | |
| 159 | nodes.flight.textContent = G.flight; | |
| 160 | nodes.height.textContent = Math.round(G.z * 10); | |
| 161 | nodes.total.textContent = G.total; | |
| 162 | nodes.best.textContent = G.best; | |
| 163 | } | |
| 164 | function reset() { | |
| 165 | G = Flight.again(G); | |
| 166 | overT = 0; landT = 0; | |
| 167 | bake(); | |
| 168 | } | |
| 169 | function press() { | |
| 170 | ensureAudio(); | |
| 171 | if (G.phase === 'over') { if (overT > .8) reset(); return; } | |
| 172 | if (G.phase === 'landed') { if (landT > .6) Flight.next(G); return; } | |
outdated
Dead code: an empty branch with a comment. Delete this line and the |
||
| 173 | Flight.begin(G); | |
| 174 | Flight.burner(G, true); | |
| 175 | } | |
| 176 | function release() { Flight.burner(G, false); } | |
| 177 | function update(dt) { | |
| 178 | time += dt; | |
| 179 | for (const e of Flight.advance(G, dt)) { | |
| 180 | if (e.type === 'land') { sound(e.score); landT = 0; } | |
| 181 | if (e.type === 'over') { | |
| 182 | overT = 0; | |
| 183 | if (G.record) try { localStorage.setItem(BEST_KEY, String(G.best)); } catch (err) { /* private window: keep it for this visit */ } | |
| 184 | } | |
| 185 | } | |
| 186 | if (G.phase === 'landed') landT += dt; | |
| 187 | if (G.phase === 'over') { overT += dt; landT += dt; } | |
| 188 | if (!STILL) { | |
| 189 | wispT += dt; | |
| 190 | while (wispT >= 1 / WIND_FPS) { | |
| 191 | wispT -= 1 / WIND_FPS; | |
| 192 | G.winds.forEach((w, b) => { cloud[b].x += w.wx / WIND_FPS; cloud[b].y += w.wy / WIND_FPS; }); | |
| 193 | } | |
| 194 | } | |
| 195 | setStatus(statusFor()); | |
| 196 | renderHud(); | |
| 197 | } | |
| 198 | ||
| 199 | /* ---------- drawing ---------- */ | |
| 200 | const rect = (x0, y0, w, h, c) => { for (let y = y0; y < y0 + h; y++) for (let x = x0; x < x0 + w; x++) drawDots([[x, y]], c); }; | |
| 201 | function line(x0, y0, x1, y1, c) { | |
| 202 | const n = Math.max(1, Math.ceil(Math.hypot(x1 - x0, y1 - y0))); | |
| 203 | const dots = []; | |
| 204 | for (let k = 0; k <= n; k++) dots.push([Math.round(lerp(x0, x1, k / n)), Math.round(lerp(y0, y1, k / n))]); | |
| 205 | drawDots(dots, c); | |
| 206 | } | |
| 207 | // a streak that ends in a head, pointing the way the wind blows on screen | |
| 208 | function streak(sx, sy, w, len, tail, head) { | |
| 209 | const a = w.angle * Math.PI / 180, ca = Math.cos(a), sa = Math.sin(a), dots = [], body = [], tip = []; | |
| 210 | for (let k = 0; k <= len; k++) { | |
| 211 | const px = Math.round(sx - ca * (len - k)), py = Math.round(sy - sa * (len - k)); | |
| 212 | (k > len - 3 ? tip : body).push([px, py]); | |
| 213 | if (k % 3 !== 2 || k > len - 3) dots.push([px, py + 1]); | |
| 214 | } | |
| 215 | haloDots(body.concat(tip)); | |
| 216 | drawDots(body, tail); | |
| 217 | drawDots(dots, tail); | |
| 218 | drawDots(tip, head); | |
| 219 | } | |
| 220 | const wrap = v => ((v + WISP_R) % (2 * WISP_R) + 2 * WISP_R) % (2 * WISP_R) - WISP_R; | |
| 221 | // every band's wisps, every frame: that is how the player reads which way each height blows | |
| 222 | function drawWisps() { | |
| 223 | for (let b = 0; b < 3; b++) { | |
| 224 | const w = G.winds[b]; | |
| 225 | for (let k = 0; k < WISPS; k++) { | |
| 226 | const x = wrap((hash(k * 3 + b, 1) * 2 - 1) * WISP_R + cloud[b].x), y = wrap((hash(k * 3 + b, 2) * 2 - 1) * WISP_R + cloud[b].y); | |
| 227 | const p = project(x, y, BAND_Z[b] + (hash(k, b + 5) - .5) * 1.2); | |
| 228 | streak(Math.round(p.sx), Math.round(p.sy), w, 5 + Math.round(w.speed * 3), WISP, WISP_HEAD); | |
| 229 | } | |
| 230 | } | |
| 231 | } | |
| 232 | // three cells at the left, high over middle over low; the arrow in each is that band's wind, the lit one is yours | |
| 233 | function drawGauge() { | |
| 234 | const band = Flight.bandOf(G.z); | |
| 235 | for (let b = 2; b >= 0; b--) { | |
| 236 | const x0 = 4, y0 = 74 + (2 - b) * 32, cx = x0 + 13, cy = y0 + 13; | |
| 237 | rect(x0 - 1, y0 - 1, 28, 28, INK); | |
| 238 | rect(x0, y0, 26, 26, b === band ? SIGNAL : CELL_BG); | |
| 239 | const w = G.winds[b], a = w.angle * Math.PI / 180, len = 4 + w.speed * 4; | |
| 240 | const tx = cx + Math.cos(a) * len, ty = cy + Math.sin(a) * len; | |
| 241 | line(cx - Math.cos(a) * len, cy - Math.sin(a) * len, tx, ty, INK); | |
| 242 | for (const s of [-1, 1]) line(tx, ty, tx - Math.cos(a + s * .55) * 4, ty - Math.sin(a + s * .55) * 4, INK); | |
| 243 | } | |
| 244 | } | |
| 245 | function drawSock() { | |
| 246 | const w = G.winds[0], n = Math.hypot(w.wx, w.wy), dx = w.wx / n, dy = w.wy / n; | |
| 247 | const sway = STILL ? 0 : Math.sin(Math.floor(time * WIND_FPS) * 1.3) * .22; | |
| 248 | const ca = Math.cos(sway), sa = Math.sin(sway), ux = dx * ca - dy * sa, uy = dx * sa + dy * ca; | |
| 249 | const lift = clamp(w.speed / 1.6, .3, 1); | |
| 250 | for (let k = 0; k < 4; k++) { | |
| 251 | const t = .28 + k * .42; | |
| 252 | sphere(SOCK_AT[0] + ux * t, SOCK_AT[1] + uy * t, 3 - (1 - lift) * t * 1.4 - k * .04, .3 - k * .045, k % 2 ? CREAM : RED, 'solid'); | |
| 253 | } | |
| 254 | } | |
| 255 | function drawBalloon() { | |
| 256 | const { x, y, z } = G; | |
| 257 | const stripes = zz => (Math.floor(zz * 3.2) % 2 ? CREAM : RED); | |
| 258 | capsule([x, y, z + 1.7], [x, y, z + 3.1], .55, 1.35, stripes, 'solid'); | |
| 259 | capsule([x, y, z + 3.1], [x, y, z + 3.9], 1.35, .8, stripes, 'solid'); | |
| 260 | box(x - .28, y - .28, z, x + .28, y + .28, z + .5, BASKET, 3); | |
| 261 | } | |
| 262 | // ropes, the flame and the pennant go on the finished picture | |
| 263 | function drawBasketDetail() { | |
| 264 | const { x, y, z } = G; | |
| 265 | for (const s of [-1, 1]) { | |
| 266 | const a = project(x + s * .28, y - s * .28, z + .5), b = project(x + s * .45, y - s * .45, z + 1.75); | |
| 267 | line(a.sx, a.sy, b.sx, b.sy, INK); | |
| 268 | } | |
| 269 | if (G.phase === 'fly' && G.burn) { | |
| 270 | const f = project(x, y, z + 1.15), jit = STILL ? 0 : Math.floor(time * WIND_FPS) % 2; | |
| 271 | const strong = G.held >= Flight.LAG, h = strong ? 5 + jit : 2; | |
| 272 | const sx = Math.round(f.sx), sy = Math.round(f.sy), flame = []; | |
| 273 | for (let k = 0; k < h; k++) for (let d = -(k < h - 2 ? 1 : 0); d <= (k < h - 2 ? 1 : 0); d++) flame.push([sx + d, sy - k]); | |
| 274 | haloDots(flame); | |
| 275 | drawDots(flame, strong ? SIGNAL : rgb(240, 150, 60)); | |
| 276 | } | |
| 277 | const top = project(x + .3, y + .3, z + 1.45), base = project(x + .3, y + .3, z + .5); | |
| 278 | line(base.sx, base.sy, top.sx, top.sy, INK); | |
| 279 | const w = G.winds[Flight.bandOf(z)], a = w.angle * Math.PI / 180, ca = Math.cos(a), sa = Math.sin(a), len = 5 + Math.round(w.speed * 4); | |
| 280 | const flutter = STILL ? 0 : Math.floor(time * WIND_FPS) % 2, flag = [], edge = []; | |
| 281 | for (let k = 0; k <= len; k++) { | |
| 282 | const half = Math.round((1 - k / len) * 2), px = top.sx + ca * k, py = top.sy + sa * k + (k > 2 ? flutter * (k % 2) : 0); | |
| 283 | for (let d = -half; d <= half; d++) (Math.abs(d) === half ? edge : flag).push([Math.round(px - sa * d), Math.round(py + ca * d)]); | |
| 284 | } | |
| 285 | drawDots(edge, INK); | |
| 286 | drawDots(flag, rgb(...RED)); | |
| 287 | } | |
| 288 | function draw() { | |
| 289 | pix.set(bakedPix); | |
| 290 | zb.set(bakedZ); | |
| 291 | drawWisps(); | |
| 292 | castShadow(G.x, G.y, 0, 1 + Math.min(G.z, 4) * .05, 1, 0); | |
| 293 | castShadow(G.x, G.y, 0, .55, 2, 0); | |
| 294 | drawSock(); | |
| 295 | drawBalloon(); | |
| 296 | applyShadows(bakedZ); | |
| 297 | shadeEdges(); | |
| 298 | drawBasketDetail(); | |
| 299 | drawGauge(); | |
| 300 | if (G.phase === 'ready') { | |
outdated
Release is wired to window blur only. If focus moves off the canvas (Tab, a dialog) while Space or Up is held, keyup never reaches the canvas and the burner stays on, burning for a player who cannot see it. Add |
||
| 301 | const head = project(G.x, G.y, G.z + 4.4); | |
| 302 | drawArrow(head.sx, head.sy, time); | |
| 303 | } | |
| 304 | ctx.putImageData(image, 0, 0); | |
| 305 | } | |
| 306 | ||
| 307 | /* ---------- input ---------- */ | |
| 308 | canvas.addEventListener('pointerdown', e => { | |
| 309 | canvas.focus({ preventScroll: true }); | |
| 310 | canvas.setPointerCapture(e.pointerId); | |
| 311 | press(); | |
| 312 | }); | |
| 313 | canvas.addEventListener('pointerup', release); | |
| 314 | canvas.addEventListener('pointercancel', release); | |
| 315 | addEventListener('blur', release); | |
| 316 | canvas.addEventListener('blur', release); | |
| 317 | const FIRE = k => k === ' ' || k === 'ArrowUp'; | |
| 318 | canvas.addEventListener('keydown', e => { | |
| 319 | if (!FIRE(e.key)) return; | |
| 320 | e.preventDefault(); | |
| 321 | if (!e.repeat) press(); | |
| 322 | }); | |
| 323 | canvas.addEventListener('keyup', e => { if (FIRE(e.key)) release(); }); | |
| 324 | ||
| 325 | /* ---------- sound ---------- */ | |
| 326 | let audio = null, muted = false, whoosh = null; | |
| 327 | function ensureAudio() { | |
| 328 | if (audio) { if (audio.state === 'suspended') audio.resume(); return; } | |
| 329 | const AC = window.AudioContext || window.webkitAudioContext; | |
| 330 | if (!AC) return; | |
| 331 | audio = new AC(); | |
| 332 | const buffer = audio.createBuffer(1, audio.sampleRate, audio.sampleRate), data = buffer.getChannelData(0); | |
| 333 | for (let i = 0; i < data.length; i++) data[i] = Math.random() * 2 - 1; | |
| 334 | const src = audio.createBufferSource(), filter = audio.createBiquadFilter(); | |
| 335 | src.buffer = buffer; src.loop = true; | |
| 336 | filter.type = 'bandpass'; filter.frequency.value = 380; filter.Q.value = .7; | |
| 337 | whoosh = audio.createGain(); | |
| 338 | whoosh.gain.value = 0; | |
| 339 | src.connect(filter).connect(whoosh).connect(audio.destination); | |
| 340 | src.start(); | |
| 341 | } | |
| 342 | function sound(score) { | |
| 343 | if (!audio || muted) return; | |
| 344 | const now = audio.currentTime, gain = audio.createGain(), osc = audio.createOscillator(); | |
| 345 | osc.type = score ? 'triangle' : 'square'; | |
| 346 | osc.frequency.setValueAtTime(score ? 330 + score * 90 : 140, now); | |
| 347 | gain.gain.setValueAtTime(.12, now); | |
| 348 | gain.gain.exponentialRampToValueAtTime(.0005, now + .3); | |
| 349 | osc.connect(gain).connect(audio.destination); | |
| 350 | osc.start(now); osc.stop(now + .31); | |
| 351 | } | |
| 352 | function updateWhoosh() { | |
| 353 | if (!whoosh) return; | |
| 354 | const on = G.phase === 'fly' && G.burn && G.held >= Flight.LAG; | |
| 355 | whoosh.gain.setTargetAtTime(muted ? 0 : on ? .06 : 0, audio.currentTime, .08); | |
| 356 | } | |
| 357 | const muteButton = document.getElementById('mute'); | |
| 358 | muteButton.addEventListener('click', () => { | |
| 359 | muted = !muted; | |
| 360 | muteButton.textContent = muted ? 'sound off' : 'sound on'; | |
| 361 | muteButton.setAttribute('aria-pressed', String(muted)); | |
| 362 | }); | |
| 363 | ||
| 364 | /* ---------- boot ---------- */ | |
| 365 | G = Flight.newGame((Date.now() % 100000) + 1, best0); | |
| 366 | addEventListener('resize', fit); | |
| 367 | fit(); | |
| 368 | bake(); | |
| 369 | setStatus(statusFor()); | |
| 370 | renderHud(); | |
| 371 | let last = performance.now(); | |
| 372 | function frame(now) { | |
| 373 | update(Math.min(.04, (now - last) / 1000)); | |
| 374 | last = now; | |
| 375 | updateWhoosh(); | |
| 376 | draw(); | |
| 377 | requestAnimationFrame(frame); | |
| 378 | } | |
| 379 | requestAnimationFrame(frame); | |
| 380 | </script> | |
| 381 | </body> | |
| 382 | </html> |
hot-air-balloon/page.test.mjs added +74
| @@ -0,0 +1,74 @@ | ||
| 1 | import { readFileSync, readdirSync } from 'node:fs'; | |
| 2 | import { test, expect } from 'bun:test'; | |
| 3 | import { boot } from '../playtest.mjs'; | |
| 4 | ||
| 5 | const state = g => g.read('G'); | |
| 6 | ||
| 7 | test('the page shows flight, height, total and best, and holding fires the burner and climbs', () => { | |
| 8 | const g = boot('hot-air-balloon'); | |
| 9 | expect(String(g.el('flight').textContent)).toBe('1'); | |
| 10 | expect(String(g.el('total').textContent)).toBe('0'); | |
| 11 | expect(String(g.el('best').textContent)).toBe('0'); | |
| 12 | expect(state(g).phase).toBe('ready'); | |
| 13 | const z0 = state(g).z; | |
| 14 | g.down(180, 140); | |
| 15 | g.run(.3); | |
| 16 | expect(state(g).phase).toBe('fly'); | |
| 17 | expect(state(g).burn).toBe(true); | |
| 18 | g.run(3); | |
| 19 | expect(state(g).z).toBeGreaterThan(z0); | |
| 20 | expect(Number(g.el('height').textContent)).toBe(Math.round(state(g).z * 10)); | |
| 21 | g.up(180, 140); | |
| 22 | g.run(.2); | |
| 23 | expect(state(g).burn).toBe(false); | |
| 24 | expect(g.errors).toEqual([]); | |
| 25 | }); | |
| 26 | ||
| 27 | test('Space and the Up arrow fire the burner', () => { | |
| 28 | for (const key of [' ', 'ArrowUp']) { | |
| 29 | const g = boot('hot-air-balloon'); | |
| 30 | g.keyDown(key); | |
| 31 | g.run(2.5); | |
| 32 | expect(state(g).burn).toBe(true); | |
| 33 | expect(state(g).vz).toBeGreaterThan(0); | |
| 34 | g.keyUp(key); | |
| 35 | g.run(.1); | |
| 36 | expect(state(g).burn).toBe(false); | |
| 37 | } | |
| 38 | }); | |
| 39 | ||
| 40 | test('losing focus while a key is held lets go of the burner', () => { | |
| 41 | const g = boot('hot-air-balloon'); | |
| 42 | g.keyDown(' '); | |
| 43 | g.run(.3); | |
| 44 | expect(state(g).burn).toBe(true); | |
| 45 | g.canvas.fire('blur'); | |
| 46 | expect(state(g).burn).toBe(false); | |
| 47 | }); | |
| 48 | ||
| 49 | test('every height band shows its wisps on every frame, and the picture changes with the wind', () => { | |
| 50 | const g = boot('hot-air-balloon'); | |
| 51 | const a = g.picture(); | |
| 52 | g.run(1); | |
| 53 | expect(g.picture()).not.toBe(a); | |
| 54 | expect(g.read('G.winds.length')).toBe(3); | |
| 55 | }); | |
| 56 | ||
| 57 | test('touching down names where it landed, and a tap goes on to the next flight', () => { | |
| 58 | const g = boot('hot-air-balloon'); | |
| 59 | g.tap(180, 140); | |
| 60 | g.run(4); | |
| 61 | expect(state(g).phase).toBe('landed'); | |
| 62 | expect(g.el('status').textContent).toMatch(/\+\d · tap for flight 2/); | |
| 63 | g.tap(180, 140); | |
| 64 | expect(state(g).phase).toBe('ready'); | |
| 65 | expect(state(g).flight).toBe(2); | |
| 66 | expect(g.errors).toEqual([]); | |
| 67 | }); | |
| 68 | ||
| 69 | test('the page has a mute button, says how to play on the canvas and ships no track', () => { | |
| 70 | const html = readFileSync(new URL('./index.html', import.meta.url), 'utf8'); | |
| 71 | expect(html).toContain('id="mute"'); | |
| 72 | expect(html).toMatch(/<canvas id="c"[^>]*aria-label="[^"]*hold[^"]*burner/i); | |
| 73 | expect(readdirSync(import.meta.dir).filter(f => f.endsWith('.mp3'))).toEqual([]); | |
| 74 | }); |
index.html +1
| @@ -94,6 +94,7 @@ footer a::after { content: none; } | ||
| 94 | 94 | <li data-kind="Sport" data-input="Tap" style="--accent:#c2402f"><a href="tug-of-war/">Tug of War</a><span>Tap on the caller's beat to heave the ribbon over your line, five pulls at the fete</span><span>Game forged by the crew</span><span class="tags">Sport · Tap</span></li> |
| 95 | 95 | <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> |
| 96 | 96 | <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> |
| 97 | <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> | |
| 97 | 98 | </ul> |
| 98 | 99 | <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> |
| 99 | 100 | </main> |
og/home.png
og/hot-air-balloon.png added
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 | // fire the burner for a blink, which is under the lag, so each flight sinks to a landing; tap on to the next flight | |
| 42 | 'hot-air-balloon': { over: /play again/, outcome: /(good flying|blown off course) · \d+ of 25/, beat: g => { g.tap(...MID); g.run(3); } }, | |
| 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
| @@ -38,6 +38,7 @@ window.PixelPlays = (() => { | ||
| 38 | 38 | { id: 'tug-of-war', title: 'Tug of War', blurb: 'Tap on the caller\'s beat to heave the ribbon over your line, five pulls at the fete', kind: 'Sport', input: 'Tap', accent: '#c2402f', music: false, forged: true }, |
| 39 | 39 | { 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 }, |
| 40 | 40 | { 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 }, |
| 41 | { 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 }, | |
| 41 | 42 | ]; |
| 42 | 43 | const MUSIC_VOLUME = .4; |
| 43 | 44 | const CREDITS = [ |
Stray extra indentation on the pole
capsule(...)line; align it with its neighbours. While there,const hedge = HEDGE;above is a pointless alias; use HEDGE directly.