Public: anyone can read this; the crew can change it.
Migrated from github.com/amirmushichge/pixel-plays
Add Egg Basket: slide the basket along the henhouse to catch eggs rolling off the roof before they crack #94
quill/pixel-plays:quill/84-add-egg-basket-slide-the-basket-along-th → main 3 commits fork
11 files
changed +668
−2
README.md +3 −2
| @@ -41,6 +41,7 @@ Concept by [abhi](https://x.com/awwbhi2). Game by [AmirMushich](https://x.com/Am | ||
| 41 | 41 | | [](espresso-bar/) | Twenty cups slide past under an espresso spout, each with a painted fill line and an order card (espresso a short line, latte a tall one): hold to pour, and the stream keeps falling for a quarter second after you let go; within 3% of the line scores 3, within 10% scores 1, over the brim spills and costs a life, and a cup that passes untouched costs a life; cups come faster as the round goes, three lives end it (forged by the crew on the engine and shell above, not by the original creators) | Hold space, or press and hold the canvas | |
| 42 | 42 | | [](goalkeeper/) | Ten penalties in a village football ground: each taker runs up and their hips and plant foot lean toward the side they will shoot (arms spread wide is down the middle), readable for a short tell window before the kick that shrinks with every penalty; tap the left third of the canvas to dive left, the right third to dive right and the middle third to stay up, and a dive to the right side before the ball arrives saves it, while diving before the tell window opens lets the taker switch side half the time; seven saves wins; the concept is the ordinary football penalty, with no particular game as its source (forged by the crew on the engine and shell above, not by the original creators) | Tap left, middle or right; Left and Right arrows dive, Up stays | |
| 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 | | [](egg-basket/) | Three hens on a red henhouse ridge lay eggs that roll down one of four lanes of the tin roof and drop off the eave: slide the basket along the eave to catch them, a brown egg is 1 and a rare golden one 3 (the HUD counts these points as eggs caught), an egg that reaches the ground cracks and three cracks end the round; 25 or more wins; eggs roll faster every ten laid, and the fastest still takes longer from spawn to drop than the basket needs to cross three lanes plus 200 ms (forged by the crew on the engine and shell above, not by the original creators) | Drag the basket; Left and Right arrows move it one lane | | |
| 44 | 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 | |
| 45 | 46 | |
| 46 | 47 | ## Run |
| @@ -61,7 +62,7 @@ On the forge, `forge.json` runs `bun serve.mjs` with health check `/`. State is | ||
| 61 | 62 | 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. |
| 62 | 63 | |
| 63 | 64 | ## Add a game |
| 64 | 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 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`. | |
| 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, Firefly Jar and Egg Basket 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`. | |
| 65 | 66 | |
| 66 | 67 | - `shell.js` provides the game menu, the Rules popup, the credits and the background music. |
| 67 | 68 | - `engine.js` provides the camera, depth buffer, colour ramps, shadows, edges, figures, trees and wind as globals. |
| @@ -77,7 +78,7 @@ Create `<id>/index.html`, load `../shell.js`, call `PixelPlays.mount({ id, rules | ||
| 77 | 78 | - Depth-buffer edges: darker where an object sits in front, a warm rim on upper-left edges. |
| 78 | 79 | - Large flat surfaces use closely spaced tones; wide steps make the dither read as a checkerboard. |
| 79 | 80 | - Characters stay sharp: flat bands with no dither, a dark silhouette on their own pixels, patterns at least 3px wide. |
| 80 | - 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, and Espresso Bar's machine steam and awning fringe move; all of it stops under `prefers-reduced-motion`. | |
| 81 | - 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, and Espresso Bar's machine steam and awning fringe move, and Egg Basket's hens' heads and the straw in its basket move (its rolling eggs are play, not ambience); all of it stops under `prefers-reduced-motion`. | |
| 81 | 82 | |
| 82 | 83 | ## License |
| 83 | 84 | The code is under the [](LICENSE). The music is not: see [](MUSIC.md). |
egg-basket/basket.js added +114
| @@ -0,0 +1,114 @@ | ||
| 1 | 'use strict'; | |
| 2 | // The egg basket rules: hens on a henhouse ridge lay eggs that roll down one of four roof lanes and drop | |
| 3 | // off the eave; a basket slides along the eave to catch them. Plain numbers in, plain numbers out, no DOM. | |
| 4 | (function (root) { | |
| 5 | const LANE_GAP = 1.5, LANES = [-1.5 * LANE_GAP, -.5 * LANE_GAP, .5 * LANE_GAP, 1.5 * LANE_GAP]; // x of each roof lane | |
| 6 | const RIDGE = { y: -1.4, z: 3.3 }, EAVE = { y: 1.2, z: 1.9 }; // the roof slopes down towards the front | |
| 7 | const ROOF_LEN = Math.hypot(EAVE.y - RIDGE.y, EAVE.z - RIDGE.z); // the distance an egg rolls along the slope | |
| 8 | const BASKET_Y = EAVE.y + .15, BASKET_SPEED = 12, BASKET_TOP = .7, CATCH_HALF = .7; // a falling egg within CATCH_HALF of the basket at BASKET_TOP is caught | |
| 9 | const GRAVITY = 9, FIRST_SPAWN = 1; | |
| 10 | const SPEED_BASE = 2.2, SPEED_STEP = .35, SPEED_MAX = 4.8, STEP_EVERY = 10; // roll speed in units per second, one step per ten eggs laid | |
| 11 | const GAP = 1.3, GAP_RAMP = .01, GAP_MIN = .8; // seconds between eggs, shrinking | |
| 12 | const GOLDEN = .12, POINTS = { brown: 1, golden: 3 }; | |
| 13 | const CRACKS = 3, WIN_SCORE = 25, RESTART_AFTER = .6; | |
| 14 | const REACH_MARGIN = .2; // seconds of slack over crossing three lanes | |
| 15 | ||
| 16 | const finite = v => (Number.isFinite(v) ? v : 0); | |
| 17 | const clamp = (v, a, b) => Math.max(a, Math.min(b, v)); | |
| 18 | // small seeded generator so a round is repeatable: state in, [value, state] out | |
| 19 | function roll(seed) { | |
| 20 | let t = (seed + 0x6D2B79F5) >>> 0; | |
| 21 | const out = Math.imul(t ^ (t >>> 15), 1 | t); | |
| 22 | const mixed = (out + Math.imul(out ^ (out >>> 7), 61 | out)) ^ out; | |
| 23 | return [((mixed ^ (mixed >>> 14)) >>> 0) / 4294967296, t]; | |
| 24 | } | |
| 25 | ||
| 26 | // roll speed of the egg laid after `laid` others: it rises one step every ten eggs | |
| 27 | const speedAt = laid => Math.min(SPEED_MAX, SPEED_BASE + SPEED_STEP * Math.floor(Math.max(0, finite(laid)) / STEP_EVERY)); | |
| 28 | const gapAt = time => Math.max(GAP_MIN, GAP - GAP_RAMP * Math.max(0, finite(time))); | |
| 29 | // the least time any egg takes from spawn to drop (the fastest roll), and the time the basket needs to cross three lanes | |
| 30 | const minDropTime = () => ROOF_LEN / SPEED_MAX; | |
| 31 | const crossTime = () => 3 * LANE_GAP / BASKET_SPEED; | |
| 32 | const verdict = score => (finite(score) >= WIN_SCORE ? 'win' : 'loss'); | |
| 33 | // the lane whose x is nearest to x | |
| 34 | function laneOf(x) { | |
| 35 | let best = 0; | |
| 36 | for (let i = 1; i < LANES.length; i++) if (Math.abs(LANES[i] - finite(x)) < Math.abs(LANES[best] - finite(x))) best = i; | |
| 37 | return best; | |
| 38 | } | |
| 39 | const inBasket = (eggX, basketX) => Number.isFinite(eggX) && Math.abs(eggX - finite(basketX)) <= CATCH_HALF; | |
| 40 | ||
| 41 | function newRound(best = 0, seed = 1) { | |
| 42 | return { | |
| 43 | phase: 'play', time: 0, score: 0, best: Math.max(0, finite(best)), record: false, cracks: 0, laid: 0, | |
| 44 | basketX: LANES[1], target: LANES[1], eggs: [], spawnIn: FIRST_SPAWN, seed: finite(seed) >>> 0, timer: 0, | |
| 45 | }; | |
| 46 | } | |
| 47 | // slide the basket to x (a drag), at BASKET_SPEED | |
| 48 | function aim(s, x) { s.target = clamp(finite(x), LANES[0], LANES[LANES.length - 1]); } | |
| 49 | // one lane left (-1) or right (1) of the lane the basket is heading for | |
| 50 | function step(s, dir) { | |
| 51 | const lane = clamp(laneOf(s.target) + (dir < 0 ? -1 : dir > 0 ? 1 : 0), 0, LANES.length - 1); | |
| 52 | s.target = LANES[lane]; | |
| 53 | } | |
| 54 | ||
| 55 | function spawn(s) { | |
| 56 | let a, b; | |
| 57 | [a, s.seed] = roll(s.seed); [b, s.seed] = roll(s.seed); | |
| 58 | const lane = Math.min(LANES.length - 1, Math.floor(a * LANES.length)); | |
| 59 | s.eggs.push({ | |
| 60 | kind: b < GOLDEN ? 'golden' : 'brown', lane, x: LANES[lane], y: RIDGE.y, z: RIDGE.z, | |
| 61 | speed: speedAt(s.laid), rolled: 0, vz: 0, state: 'roll', | |
| 62 | }); | |
| 63 | s.laid++; | |
| 64 | } | |
| 65 | // advance by dt seconds; returns what happened: [{ type: 'catch'|'crack', kind, points, egg }] | |
| 66 | function advance(s, dt) { | |
| 67 | dt = clamp(finite(dt), 0, .1); | |
| 68 | const events = []; | |
| 69 | if (s.phase === 'over') { s.timer += dt; return events; } | |
| 70 | s.time += dt; | |
| 71 | const reach = BASKET_SPEED * dt; | |
| 72 | s.basketX += clamp(s.target - s.basketX, -reach, reach); | |
| 73 | s.spawnIn -= dt; | |
| 74 | if (s.spawnIn <= 0) { spawn(s); s.spawnIn = gapAt(s.time); } | |
| 75 | for (const e of s.eggs) { | |
| 76 | if (e.state === 'roll') { | |
| 77 | e.rolled = Math.min(ROOF_LEN, e.rolled + e.speed * dt); | |
| 78 | const t = e.rolled / ROOF_LEN; | |
| 79 | e.y = RIDGE.y + (EAVE.y - RIDGE.y) * t; | |
| 80 | e.z = RIDGE.z + (EAVE.z - RIDGE.z) * t; | |
| 81 | if (e.rolled >= ROOF_LEN) { e.state = 'fall'; e.vz = 0; } | |
| 82 | continue; | |
| 83 | } | |
| 84 | const before = e.z; | |
| 85 | e.vz -= GRAVITY * dt; | |
| 86 | e.z += e.vz * dt; | |
| 87 | if (before > BASKET_TOP && e.z <= BASKET_TOP && inBasket(e.x, s.basketX)) { | |
| 88 | e.state = 'caught'; | |
| 89 | const points = POINTS[e.kind]; | |
| 90 | s.score += points; | |
| 91 | if (s.score > s.best) { s.best = s.score; s.record = true; } | |
| 92 | events.push({ type: 'catch', kind: e.kind, points, egg: e }); | |
| 93 | } else if (e.z <= 0) { | |
| 94 | e.z = 0; e.state = 'cracked'; | |
| 95 | s.cracks++; | |
| 96 | events.push({ type: 'crack', kind: e.kind, points: 0, egg: e }); | |
| 97 | } | |
| 98 | } | |
| 99 | s.eggs = s.eggs.filter(e => e.state === 'roll' || e.state === 'fall'); | |
| 100 | if (s.cracks >= CRACKS) { s.phase = 'over'; s.timer = 0; s.eggs = []; } | |
| 101 | return events; | |
| 102 | } | |
| 103 | // a tap after the round has been over for a moment starts the next one; otherwise null | |
| 104 | function again(s) { | |
| 105 | return s.phase === 'over' && s.timer > RESTART_AFTER ? newRound(s.best, s.seed + 1) : null; | |
| 106 | } | |
| 107 | ||
| 108 | const api = { | |
| 109 | LANES, LANE_GAP, RIDGE, EAVE, ROOF_LEN, BASKET_Y, BASKET_SPEED, BASKET_TOP, CATCH_HALF, GRAVITY, POINTS, CRACKS, WIN_SCORE, | |
| 110 | RESTART_AFTER, REACH_MARGIN, SPEED_MAX, STEP_EVERY, speedAt, gapAt, minDropTime, crossTime, verdict, laneOf, inBasket, | |
| 111 | newRound, aim, step, advance, again, | |
| 112 | }; | |
| 113 | if (typeof module !== 'undefined') module.exports = api; else root.EggBasket = api; | |
| 114 | })(typeof window !== 'undefined' ? window : globalThis); |
egg-basket/basket.test.mjs added +167
| @@ -0,0 +1,167 @@ | ||
| 1 | import { test, expect } from 'bun:test'; | |
| 2 | import { | |
| 3 | LANES, ROOF_LEN, BASKET_TOP, CATCH_HALF, POINTS, CRACKS, WIN_SCORE, BASKET_SPEED, REACH_MARGIN, SPEED_MAX, STEP_EVERY, | |
| 4 | speedAt, gapAt, minDropTime, crossTime, verdict, laneOf, inBasket, newRound, aim, step, advance, again, | |
| 5 | } from './basket.js'; | |
| 6 | ||
| 7 | const run = (s, seconds, dt = .05) => { for (let t = 0; t < seconds; t += dt) advance(s, dt); }; | |
| 8 | // an egg already falling just above the basket's rim | |
| 9 | const falling = (s, kind, x) => { | |
| 10 | s.eggs.push({ kind, lane: laneOf(x), x, y: 1.2, z: BASKET_TOP + .05, speed: 3, rolled: ROOF_LEN, vz: -2, state: 'fall' }); | |
| 11 | s.spawnIn = 99; | |
| 12 | }; | |
| 13 | // let the egg fall and return what happened | |
| 14 | const settle = s => { let ev = []; for (let n = 0; n < 40 && !ev.length; n++) ev = advance(s, .05); return ev; }; | |
| 15 | ||
| 16 | test('there are four roof lanes in order', () => { | |
| 17 | expect(LANES.length).toBe(4); | |
| 18 | expect([...LANES].sort((a, b) => a - b)).toEqual(LANES); | |
| 19 | }); | |
| 20 | ||
| 21 | test('a brown egg over the basket is caught for 1, a golden one for 3', () => { | |
| 22 | expect(POINTS).toEqual({ brown: 1, golden: 3 }); | |
| 23 | const s = newRound(0); | |
| 24 | falling(s, 'brown', s.basketX); | |
| 25 | expect(advance(s, .05).map(e => [e.type, e.kind, e.points])).toEqual([['catch', 'brown', 1]]); | |
| 26 | falling(s, 'golden', s.basketX); | |
| 27 | expect(advance(s, .05).map(e => [e.type, e.kind, e.points])).toEqual([['catch', 'golden', 3]]); | |
| 28 | expect(s.score).toBe(4); | |
| 29 | expect(s.best).toBe(4); | |
| 30 | expect(s.record).toBe(true); | |
| 31 | expect(s.cracks).toBe(0); | |
| 32 | }); | |
| 33 | ||
| 34 | test('an egg that reaches the ground cracks and scores nothing', () => { | |
| 35 | const s = newRound(0); | |
| 36 | falling(s, 'brown', s.basketX + CATCH_HALF + .3); | |
| 37 | expect(settle(s).map(e => e.type)).toEqual(['crack']); | |
| 38 | expect(s.cracks).toBe(1); | |
| 39 | expect(s.score).toBe(0); | |
| 40 | expect(s.eggs).toEqual([]); | |
| 41 | }); | |
| 42 | ||
| 43 | test('the catch window is the basket mouth and nothing wider', () => { | |
| 44 | expect(inBasket(0, 0)).toBe(true); | |
| 45 | expect(inBasket(CATCH_HALF, 0)).toBe(true); | |
| 46 | expect(inBasket(CATCH_HALF + .01, 0)).toBe(false); | |
| 47 | expect(inBasket(NaN, 0)).toBe(false); | |
| 48 | }); | |
| 49 | ||
| 50 | test('a laid egg rolls down its lane and drops off the eave', () => { | |
| 51 | const s = newRound(0); | |
| 52 | s.spawnIn = 0; | |
| 53 | advance(s, .05); | |
| 54 | expect(s.eggs.length).toBe(1); | |
| 55 | const e = s.eggs[0], x = e.x; | |
| 56 | expect(LANES).toContain(x); | |
| 57 | expect(e.state).toBe('roll'); | |
| 58 | s.spawnIn = 99; | |
| 59 | for (let n = 0; n < 200 && e.state === 'roll'; n++) advance(s, .05); | |
| 60 | expect(e.state).toBe('fall'); | |
| 61 | expect(e.x).toBe(x); | |
| 62 | expect(e.rolled).toBeCloseTo(ROOF_LEN, 6); | |
| 63 | }); | |
| 64 | ||
| 65 | test('three cracked eggs end the round, two do not', () => { | |
| 66 | const s = newRound(0); | |
| 67 | for (let n = 0; n < CRACKS - 1; n++) { falling(s, 'brown', LANES[3]); s.basketX = s.target = LANES[0]; settle(s); } | |
| 68 | expect(s.cracks).toBe(2); | |
| 69 | expect(s.phase).toBe('play'); | |
| 70 | falling(s, 'brown', LANES[3]); | |
| 71 | settle(s); | |
| 72 | expect(s.cracks).toBe(CRACKS); | |
| 73 | expect(s.phase).toBe('over'); | |
| 74 | expect(advance(s, .05)).toEqual([]); | |
| 75 | }); | |
| 76 | ||
| 77 | test('rolling speed rises every ten eggs and is capped', () => { | |
| 78 | expect(speedAt(0)).toBe(speedAt(STEP_EVERY - 1)); | |
| 79 | expect(speedAt(STEP_EVERY)).toBeGreaterThan(speedAt(STEP_EVERY - 1)); | |
| 80 | expect(speedAt(2 * STEP_EVERY)).toBeGreaterThan(speedAt(STEP_EVERY)); | |
| 81 | expect(speedAt(1e6)).toBe(SPEED_MAX); | |
| 82 | const s = newRound(0); | |
| 83 | const speeds = []; | |
| 84 | for (let n = 0; n < 12; n++) { s.spawnIn = 0; s.eggs = []; advance(s, .01); speeds.push(s.eggs[0].speed); } | |
| 85 | expect(speeds[9]).toBe(speeds[0]); | |
| 86 | expect(speeds[10]).toBeGreaterThan(speeds[9]); | |
| 87 | }); | |
| 88 | ||
| 89 | test('every lane is reachable in time: spawn to drop is at least three lanes of travel plus 200 ms', () => { | |
| 90 | expect(REACH_MARGIN).toBe(.2); | |
| 91 | expect(minDropTime()).toBeCloseTo(ROOF_LEN / SPEED_MAX, 9); | |
| 92 | expect(minDropTime()).toBeGreaterThanOrEqual(crossTime() + REACH_MARGIN); | |
| 93 | expect(crossTime()).toBeCloseTo(3 * (LANES[1] - LANES[0]) / BASKET_SPEED, 9); | |
| 94 | // no egg ever rolls faster than the cap | |
| 95 | expect(speedAt(1e9)).toBeLessThanOrEqual(SPEED_MAX); | |
| 96 | }); | |
| 97 | ||
| 98 | test('the fastest egg in the far lane is caught by a basket that starts at the other end', () => { | |
| 99 | for (const [from, to] of [[0, 3], [3, 0]]) { | |
| 100 | const s = newRound(0); | |
| 101 | s.basketX = s.target = LANES[from]; | |
| 102 | s.laid = 1e6; // the capped speed | |
| 103 | s.spawnIn = 0; | |
| 104 | // force the lane: spawn, then move the egg to the far lane | |
| 105 | advance(s, .0001); | |
| 106 | const e = s.eggs[0]; | |
| 107 | e.lane = to; e.x = LANES[to]; | |
| 108 | aim(s, LANES[to]); | |
| 109 | s.spawnIn = 99; | |
| 110 | const events = []; | |
| 111 | for (let n = 0; n < 300 && !events.length; n++) events.push(...advance(s, .01)); | |
| 112 | expect(events.map(v => v.type)).toEqual(['catch']); | |
| 113 | } | |
| 114 | }); | |
| 115 | ||
| 116 | test('stepping moves the basket exactly one lane and stops at the ends', () => { | |
| 117 | const s = newRound(0); | |
| 118 | const start = laneOf(s.target); | |
| 119 | step(s, 1); | |
| 120 | expect(laneOf(s.target)).toBe(start + 1); | |
| 121 | step(s, -1); step(s, -1); | |
| 122 | expect(laneOf(s.target)).toBe(start - 1); | |
| 123 | for (let n = 0; n < 6; n++) step(s, -1); | |
| 124 | expect(s.target).toBe(LANES[0]); | |
| 125 | for (let n = 0; n < 6; n++) step(s, 1); | |
| 126 | expect(s.target).toBe(LANES[3]); | |
| 127 | run(s, 1); | |
| 128 | expect(s.basketX).toBeCloseTo(LANES[3], 6); | |
| 129 | }); | |
| 130 | ||
| 131 | test('dragging aims the basket within the eave and ignores junk', () => { | |
| 132 | const s = newRound(0); | |
| 133 | aim(s, 99); expect(s.target).toBe(LANES[3]); | |
| 134 | aim(s, -99); expect(s.target).toBe(LANES[0]); | |
| 135 | aim(s, NaN); expect(Number.isFinite(s.target)).toBe(true); | |
| 136 | aim(s, 0.3); expect(s.target).toBe(.3); | |
| 137 | }); | |
| 138 | ||
| 139 | test('the win line is 25 and the loss line is below it', () => { | |
| 140 | expect(WIN_SCORE).toBe(25); | |
| 141 | expect(verdict(25)).toBe('win'); | |
| 142 | expect(verdict(40)).toBe('win'); | |
| 143 | expect(verdict(24)).toBe('loss'); | |
| 144 | expect(verdict(0)).toBe('loss'); | |
| 145 | expect(verdict(NaN)).toBe('loss'); | |
| 146 | }); | |
| 147 | ||
| 148 | test('a round is repeatable from its seed, and a new one keeps the best', () => { | |
| 149 | const play = () => { const s = newRound(0, 5); run(s, 12); return s.eggs.map(e => [e.kind, e.lane, e.rolled.toFixed(3)]); }; | |
| 150 | expect(play()).toEqual(play()); | |
| 151 | const s = newRound(0, 5); | |
| 152 | falling(s, 'golden', s.basketX); advance(s, .05); | |
| 153 | s.phase = 'over'; s.timer = 0; | |
| 154 | expect(again(s)).toBeNull(); | |
| 155 | run(s, 1); | |
| 156 | const next = again(s); | |
| 157 | expect(next.best).toBe(3); | |
| 158 | expect(next.score).toBe(0); | |
| 159 | expect(next.cracks).toBe(0); | |
| 160 | expect(next.phase).toBe('play'); | |
| 161 | }); | |
| 162 | ||
| 163 | test('eggs come faster as the round goes, down to a floor', () => { | |
| 164 | expect(gapAt(60)).toBeLessThan(gapAt(0)); | |
| 165 | expect(gapAt(1e6)).toBeGreaterThan(0); | |
| 166 | expect(gapAt(NaN)).toBe(gapAt(0)); | |
| 167 | }); |
egg-basket/index.html added +339
| @@ -0,0 +1,339 @@ | ||
| 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>Egg Basket</title> | |
| 7 | <!-- og:begin --> | |
| 8 | <meta name="description" content="Slide the basket under the eave and catch the eggs before three crack"> | |
| 9 | <link rel="canonical" href="https://pixel-plays--forge-trevato-apps.open-platform.sh/egg-basket/"> | |
| 10 | <meta property="og:type" content="website"> | |
| 11 | <meta property="og:site_name" content="Pixel Plays"> | |
| 12 | <meta property="og:title" content="Egg Basket"> | |
| 13 | <meta property="og:description" content="Slide the basket under the eave and catch the eggs before three crack"> | |
| 14 | <meta property="og:url" content="https://pixel-plays--forge-trevato-apps.open-platform.sh/egg-basket/"> | |
| 15 | <meta property="og:image" content="https://pixel-plays--forge-trevato-apps.open-platform.sh/og/egg-basket.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="Egg Basket: 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; : #d08a2e; } | |
| 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: ew-resize; 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 | .dots { display: flex; gap: 3px; } | |
| 37 | .dots i { width: 6px; height: 6px; background: var(); } | |
| 38 | .dots i.off { opacity: .16; } | |
| 39 | #status { color: var(); } | |
| 40 | button { pointer-events: auto; font: inherit; color: var(); background: none; border: 0; padding: 10px; margin: -10px; cursor: pointer; } | |
| 41 | button:hover, button:focus-visible { color: var(); } | |
| 42 | </style> | |
| 43 | </head> | |
| 44 | <body> | |
| 45 | <div id="stage"><canvas id="c" tabindex="0" aria-label="A red henhouse with a sloping tin roof and three hens on the ridge. Drag the basket or press the left and right arrow keys to slide it along the eave and catch the eggs rolling off the roof: brown 1, golden 3, three cracked eggs end the round."></canvas></div> | |
| 46 | <div id="scores"> | |
| 47 | <div class="row"><b id="score">0</b><span>eggs caught</span></div> | |
| 48 | <div class="row"><b id="cracks">3</b><span>cracks left</span><span class="dots" id="lives"></span></div> | |
| 49 | <div class="row"><b id="best">0</b><span>best</span></div> | |
| 50 | </div> | |
| 51 | <div class="hud" id="bottom"> | |
| 52 | <div id="status" role="status" aria-live="polite"></div> | |
| 53 | <button id="mute" type="button" aria-pressed="false">sound on</button> | |
| 54 | </div> | |
| 55 | <script src="../shell.js"></script> | |
| 56 | <script> | |
| 57 | PixelPlays.mount({ | |
| 58 | id: 'egg-basket', | |
| 59 | rules: [ | |
| 60 | 'Drag or use Left and Right to move the basket.', | |
| 61 | 'Brown eggs 1, golden 3. Eggs on the ground crack.', | |
| 62 | 'Three cracks end it; 25 or more wins.', | |
| 63 | ], | |
| 64 | }); | |
| 65 | </script> | |
| 66 | <script>window.PP_SCENE = { originY: 0, shadowX: .9, shadowY: -.12 };</script> | |
| 67 | <script src="../engine.js"></script> | |
| 68 | <script src="basket.js"></script> | |
| 69 | <script> | |
| 70 | 'use strict'; | |
| 71 | ||
| 72 | const BASKET = window.EggBasket; | |
| 73 | const EGG = { brown: [160, 106, 62], golden: [240, 194, 58] }; | |
| 74 | const RED = [176, 52, 44], RED_DARK = [96, 36, 40], TIN = [150, 162, 176], TIN_DARK = [84, 96, 114]; | |
| 75 | const WICKER = [200, 152, 86], WICKER_DARK = [92, 62, 40], STRAW = [236, 206, 108]; | |
| 76 | const DIRT = [[112, 92, 70], [138, 112, 82], [164, 136, 96], [190, 162, 116]]; | |
| 77 | const HOUSE = { x0: -3.4, x1: 3.4, y0: -1.6, y1: 1.0, wall: 1.95 }; | |
| 78 | const HENS = [ | |
| 79 | { x: -2.4, body: [182, 92, 52], wing: [140, 66, 40] }, | |
| 80 | { x: 0, body: [238, 232, 218], wing: [196, 188, 172] }, | |
| 81 | { x: 2.4, body: [128, 82, 52], wing: [92, 58, 40] }, | |
| 82 | ]; | |
| 83 | const RIDGE = BASKET.RIDGE, EAVE = BASKET.EAVE, BASKET_Y = BASKET.BASKET_Y; | |
| 84 | const roofZ = y => RIDGE.z + (EAVE.z - RIDGE.z) * (y - RIDGE.y) / (EAVE.y - RIDGE.y); | |
| 85 | ||
| 86 | function groundColor(x, y, ix, iy, i) { | |
| 87 | const edge = Math.pow(Math.pow(Math.abs(x) / 6.3, 4) + Math.pow(Math.abs(y - .1) / 3.7, 4), .25); | |
| 88 | const rough = edge + (noise(x * 1.3, y * 1.3) - .5) * .13; | |
| 89 | if (rough > 1.04) return 0; | |
| 90 | if (rough > .93 && hash(ix, iy) < (rough - .93) / .11) return 0; | |
| 91 | const t = noise(x * .9, y * .9) * 1.15 + (hash(ix, iy) - .5) * .55 - .08; | |
| 92 | if (y > .9 + (noise(x * .6, 3) - .5) * .8) return rgb(...DIRT[clamp(Math.floor(t * 4.2), 0, 3)]); | |
| 93 | return rgb(...GRASS[clamp(Math.floor(t * 4.2), 0, 3)]); | |
| 94 | } | |
| 95 | ||
| 96 | // the tin roof: a sloping plane painted with corrugation, and a dark rail between the lanes | |
| 97 | function paintRoof() { | |
| 98 | const ramp = rampOf(TIN), dark = rampOf(TIN_DARK), rails = BASKET.LANES.map((x, k) => x - BASKET.LANE_GAP / 2).concat(BASKET.LANES[3] + BASKET.LANE_GAP / 2); | |
| 99 | for (let x = -3.7; x <= 3.7; x += .04) for (let y = RIDGE.y; y <= EAVE.y; y += .04) { | |
| 100 | const p = project(x, y, roofZ(y)), ix = p.sx | 0, iy = p.sy | 0; | |
| 101 | if (ix < 0 || iy < 0 || ix >= W || iy >= H || p.d <= zb[iy * W + ix]) continue; | |
| 102 | zb[iy * W + ix] = p.d; | |
| 103 | const rail = rails.some(r => Math.abs(x - r) < .05); | |
| 104 | const rib = Math.floor(x * 5) & 1 ? 0 : 1; | |
| 105 | const along = (y - RIDGE.y) / (EAVE.y - RIDGE.y); | |
| 106 | const base = rail ? 1.2 : 2.5 + rib * .9 - along * .3 + (noise(x * 3, y * 3) - .5) * .6; | |
| 107 | pix[iy * W + ix] = (rail ? dark : ramp)[clamp(Math.floor(base + (bayerAt(ix, iy) - .5) * .5), 0, 4)]; | |
| 108 | } | |
| 109 | capsule([-3.7, RIDGE.y, RIDGE.z + .04], [3.7, RIDGE.y, RIDGE.z + .04], .09, .09, () => TIN_DARK, 'solid'); | |
| 110 | capsule([-3.7, EAVE.y + .03, EAVE.z - .02], [3.7, EAVE.y + .03, EAVE.z - .02], .08, .08, () => TIN_DARK, 'solid'); | |
| 111 | } | |
| 112 | // the three-sided gable above the right wall | |
| 113 | function paintGable() { | |
| 114 | const ramp = rampOf(RED); | |
| 115 | for (let y = HOUSE.y0; y <= HOUSE.y1; y += .04) for (let z = HOUSE.wall - .05; z <= roofZ(y); z += .04) { | |
| 116 | const p = project(HOUSE.x1, y, z), ix = p.sx | 0, iy = p.sy | 0; | |
| 117 | if (ix < 0 || iy < 0 || ix >= W || iy >= H || p.d <= zb[iy * W + ix]) continue; | |
| 118 | zb[iy * W + ix] = p.d; | |
| 119 | pix[iy * W + ix] = ramp[clamp(Math.floor(1.2 + (noise(y * .8, z * 9) - .5) * 1.3 + (bayerAt(ix, iy) - .5) * .5), 0, 4)]; | |
| 120 | } | |
| 121 | } | |
| 122 | function growHouse() { | |
| 123 | box(HOUSE.x0, HOUSE.y0, 0, HOUSE.x1, HOUSE.y1, HOUSE.wall, RED, 4); | |
| 124 | paintGable(); | |
| 125 | paintRoof(); | |
| 126 | box(-.4, HOUSE.y1, 0, .4, HOUSE.y1 + .06, 1.15, RED_DARK, 2); // pop door | |
| 127 | box(-.5, HOUSE.y1 + .02, 1.15, .5, HOUSE.y1 + .07, 1.25, WICKER, 5); | |
| 128 | box(1.4, HOUSE.y1, .8, 2.2, HOUSE.y1 + .05, 1.4, [236, 226, 196], 3); // shutter | |
| 129 | box(-2.2, HOUSE.y1, .8, -1.4, HOUSE.y1 + .05, 1.4, [236, 226, 196], 6); | |
| 130 | for (let y = HOUSE.y0; y <= HOUSE.y1 + .5; y += .4) for (let x = HOUSE.x0; x <= HOUSE.x1; x += .3) { | |
| 131 | for (let z = 0; z <= roofZ(Math.min(y, EAVE.y)); z += .35) castShadow(x, y, z, .24, 1, 0); | |
| 132 | } | |
| 133 | } | |
| 134 | function bake() { | |
| 135 | zb.fill(-1e9); | |
| 136 | pix.fill(rgb(255, 255, 255)); | |
| 137 | for (let iy = 0; iy < H; iy++) for (let ix = 0; ix < W; ix++) { | |
| 138 | const g = unproject(ix + .5, iy + .5), i = iy * W + ix, c = groundColor(g.x, g.y, ix, iy, i); | |
| 139 | if (!c) continue; | |
| 140 | pix[i] = c; ground[i] = 1; | |
| 141 | zb[i] = (g.x + g.y) / R2 * COS; | |
| 142 | } | |
| 143 | groundZ.set(zb); | |
| 144 | growHouse(); | |
| 145 | bakeOcclusion(); | |
| 146 | staticShadow.set(shadowMask); | |
| 147 | applyShadows(groundZ); | |
| 148 | bakedPix.set(pix); | |
| 149 | bakedZ.set(zb); | |
| 150 | } | |
| 151 | ||
| 152 | /* ---------- game state ---------- */ | |
| 153 | const nodes = { | |
| 154 | score: document.getElementById('score'), best: document.getElementById('best'), cracks: document.getElementById('cracks'), | |
| 155 | lives: document.getElementById('lives'), status: document.getElementById('status'), | |
| 156 | }; | |
| 157 | const G = { time: 0, status: '', splats: [], keys: new Set(), dragging: false }; | |
| 158 | const BEST_KEY = 'pixel-plays-egg-basket-best'; | |
| 159 | const best0 = (() => { try { return Number(localStorage.getItem(BEST_KEY)) || 0; } catch (e) { return 0; } })(); | |
| 160 | let R = BASKET.newRound(best0, Date.now() & 0xffff); | |
| 161 | function renderHud() { | |
| 162 | const left = BASKET.CRACKS - R.cracks; | |
| 163 | nodes.score.textContent = R.score; | |
| 164 | nodes.best.textContent = R.best; | |
| 165 | nodes.cracks.textContent = left; | |
| 166 | if (nodes.lives.children.length !== BASKET.CRACKS) nodes.lives.innerHTML = '<i></i>'.repeat(BASKET.CRACKS); | |
| 167 | [...nodes.lives.children].forEach((el, k) => el.classList.toggle('off', k >= left)); | |
| 168 | } | |
| 169 | function setStatus(text) { | |
| 170 | if (text === G.status) return; | |
| 171 | G.status = text; | |
| 172 | nodes.status.textContent = text; | |
| 173 | } | |
| 174 | const HINT = 'drag the basket or use the arrow keys'; | |
| 175 | function restart() { | |
| 176 | const next = BASKET.again(R); | |
| 177 | if (!next) return false; | |
| 178 | R = next; G.splats = []; | |
| 179 | setStatus(HINT); | |
| 180 | renderHud(); | |
| 181 | return true; | |
| 182 | } | |
| 183 | function settle(e) { | |
| 184 | if (e.type === 'crack') { | |
| 185 | G.splats.push({ x: e.egg.x, y: e.egg.y, kind: e.kind, age: 0 }); | |
| 186 | sound('crack'); | |
| 187 | setStatus('cracked'); | |
| 188 | return; | |
| 189 | } | |
| 190 | if (e.kind === 'golden') { sound('chime'); setStatus('golden · +3'); } | |
| 191 | else { sound('pop'); setStatus('caught · +1'); } | |
| 192 | } | |
| 193 | function update(dt) { | |
| 194 | G.time += dt; | |
| 195 | const was = R.phase, events = BASKET.advance(R, dt); | |
| 196 | events.forEach(settle); | |
| 197 | if (events.length) renderHud(); | |
| 198 | for (const s of G.splats) s.age += dt; | |
| 199 | G.splats = G.splats.filter(s => s.age < 1.5); | |
| 200 | if (was === 'play' && R.phase === 'over') { | |
| 201 | if (R.record) try { localStorage.setItem(BEST_KEY, String(R.best)); } catch (e) { /* private window: keep it for this visit */ } | |
| 202 | const line = BASKET.verdict(R.score) === 'win' ? 'you win, the basket is full' : 'three eggs cracked'; | |
| 203 | setStatus(`${R.score} eggs · ${line}${R.record ? ' · new best' : ` · best ${R.best}`} · tap or space to play again`); | |
| 204 | } | |
| 205 | } | |
| 206 | ||
| 207 | /* ---------- drawing ---------- */ | |
| 208 | function drawEgg(e) { | |
| 209 | const sit = e.state === 'roll' ? .17 : 0; | |
| 210 | sphere(e.x, e.y, e.z + sit, .21, EGG[e.kind], e.kind === 'golden' ? 'gloss' : 'solid'); | |
| 211 | if (e.state === 'fall') castShadow(e.x, e.y, e.z, .21, 1, clamp(e.z / 3, 0, 1) * .95); | |
| 212 | } | |
| 213 | function drawHen(h, t, k) { | |
| 214 | const bob = STILL ? 0 : [0, 1, 0, -1][Math.floor(t * 4 + k * 1.3) & 3] * .07; | |
| 215 | const y = RIDGE.y + .1, z = RIDGE.z + .55; | |
| 216 | sphere(h.x, y, z, .55, h.body, 'solid'); | |
| 217 | sphere(h.x - .2, y - .1, z + .1, .35, h.wing, 'solid'); // wing | |
| 218 | sphere(h.x, y - .55, z + .25, .3, h.wing, 'solid'); // tail | |
| 219 | sphere(h.x, y + .35, z + .55 + bob, .26, h.body, 'solid'); // head | |
| 220 | sphere(h.x, y + .35, z + .86 + bob, .12, [206, 44, 40], 'solid'); // comb | |
| 221 | sphere(h.x, y + .6, z + .5 + bob, .09, [232, 172, 44], 'solid'); // beak | |
| 222 | sphere(h.x + .2, y + .5, z + .62 + bob, .05, [28, 24, 24], 'solid'); // eye | |
| 223 | } | |
| 224 | function drawBasket(x) { | |
| 225 | const y = BASKET_Y, half = BASKET.CATCH_HALF + .1, depth = .45; | |
| 226 | box(x - half, y - depth, 0, x + half, y + depth, .62, WICKER, 3); | |
| 227 | box(x - half + .12, y - depth + .1, .62, x + half - .12, y + depth - .1, .66, STRAW, 5); | |
| 228 | const sway = STILL ? 0 : [0, 1, 0, -1][Math.floor(G.time * 8) & 3] * .07; | |
| 229 | for (let k = 0; k < 7; k++) { | |
| 230 | const sx = x - half + .2 + k * (2 * half - .4) / 6, lean = (k % 2 ? 1 : -1) * .08; | |
| 231 | capsule([sx, y, .66], [sx + lean + sway * (k % 3 + 1) * .6, y + (k % 3 - 1) * .12, .95 + (k % 3) * .06], .035, .03, () => STRAW, 'solid'); | |
| 232 | } | |
| 233 | capsule([x - half, y - depth, .66], [x - half, y + depth, .66], .06, .06, () => WICKER, 'solid'); | |
| 234 | capsule([x + half, y - depth, .66], [x + half, y + depth, .66], .06, .06, () => WICKER, 'solid'); | |
| 235 | for (let z = .1; z < .6; z += .25) castShadow(x, y, z, half * .95, 1, 0); | |
| 236 | } | |
| 237 | function drawSplats() { | |
| 238 | for (const s of G.splats) { | |
| 239 | sphere(s.x, s.y + .1, .04, .3, [250, 246, 232], 'solid'); | |
| 240 | sphere(s.x, s.y + .1, .12, .14, s.kind === 'golden' ? [240, 180, 40] : [232, 150, 36], 'solid'); | |
| 241 | } | |
| 242 | } | |
| 243 | function draw() { | |
| 244 | pix.set(bakedPix); | |
| 245 | zb.set(bakedZ); | |
| 246 | pendingShadows.length = 0; | |
| 247 | drawSplats(); | |
| 248 | drawBasket(R.basketX); | |
| 249 | HENS.forEach((h, k) => drawHen(h, G.time, k)); | |
| 250 | for (const e of R.eggs) drawEgg(e); | |
| 251 | flushShadows(); | |
| 252 | applyShadows(bakedZ); | |
| 253 | shadeEdges(); | |
| 254 | ctx.putImageData(image, 0, 0); | |
| 255 | } | |
| 256 | ||
| 257 | /* ---------- input ---------- */ | |
| 258 | // screen x on the basket's line of the ground, in world units | |
| 259 | function worldX(e) { | |
| 260 | const r = canvas.getBoundingClientRect(); | |
| 261 | const sx = (e.clientX - r.left) / r.width * W; | |
| 262 | return (sx - OX) / S * R2 + BASKET_Y; | |
| 263 | } | |
| 264 | canvas.addEventListener('pointerdown', e => { | |
| 265 | ensureAudio(); | |
| 266 | canvas.focus({ preventScroll: true }); | |
| 267 | if (restart()) return; | |
| 268 | G.dragging = true; | |
| 269 | canvas.setPointerCapture(e.pointerId); | |
| 270 | BASKET.aim(R, worldX(e)); | |
| 271 | }); | |
| 272 | canvas.addEventListener('pointermove', e => { if (G.dragging) BASKET.aim(R, worldX(e)); }); | |
| 273 | const release = () => { G.dragging = false; }; | |
| 274 | canvas.addEventListener('pointerup', release); | |
| 275 | canvas.addEventListener('pointercancel', release); | |
| 276 | const KEYS = { ArrowLeft: -1, a: -1, A: -1, ArrowRight: 1, d: 1, D: 1 }; | |
| 277 | canvas.addEventListener('keydown', e => { | |
| 278 | if (e.key === ' ' || e.key === 'Enter') { | |
| 279 | e.preventDefault(); | |
| 280 | ensureAudio(); | |
| 281 | if (!e.repeat) restart(); | |
| 282 | return; | |
| 283 | } | |
| 284 | const dir = KEYS[e.key]; | |
| 285 | if (!dir) return; | |
| 286 | e.preventDefault(); | |
| 287 | ensureAudio(); | |
| 288 | G.dragging = false; | |
| 289 | if (R.phase === 'play') BASKET.step(R, dir); | |
| 290 | }); | |
| 291 | ||
| 292 | /* ---------- sound ---------- */ | |
| 293 | let audio = null, muted = false; | |
| 294 | function ensureAudio() { | |
| 295 | if (audio) { if (audio.state === 'suspended') audio.resume(); return; } | |
| 296 | const AC = window.AudioContext || window.webkitAudioContext; | |
| 297 | if (AC) audio = new AC(); | |
| 298 | } | |
| 299 | function sound(kind) { | |
| 300 | if (!audio || muted) return; | |
| 301 | const now = audio.currentTime, gain = audio.createGain(), osc = audio.createOscillator(); | |
| 302 | const spec = { | |
| 303 | pop: [380, 560, .08, .09, 'sine'], | |
| 304 | chime: [660, 1320, .22, .09, 'triangle'], | |
| 305 | crack: [300, 80, .12, .08, 'square'], | |
| 306 | }[kind]; | |
| 307 | osc.type = spec[4]; | |
| 308 | osc.frequency.setValueAtTime(spec[0], now); | |
| 309 | osc.frequency.exponentialRampToValueAtTime(spec[1], now + spec[2]); | |
| 310 | gain.gain.setValueAtTime(spec[3], now); | |
| 311 | gain.gain.exponentialRampToValueAtTime(.0005, now + spec[2]); | |
| 312 | osc.connect(gain).connect(audio.destination); | |
| 313 | osc.start(now); osc.stop(now + spec[2] + .01); | |
| 314 | } | |
| 315 | const muteButton = document.getElementById('mute'); | |
| 316 | muteButton.addEventListener('click', () => { | |
| 317 | muted = !muted; | |
| 318 | muteButton.textContent = muted ? 'sound off' : 'sound on'; | |
| 319 | muteButton.setAttribute('aria-pressed', String(muted)); | |
| 320 | }); | |
| 321 | ||
| 322 | /* ---------- fit and boot ---------- */ | |
| 323 | addEventListener('resize', fit); | |
| 324 | fit(); | |
| 325 | bake(); | |
| 326 | setStatus(HINT); | |
| 327 | renderHud(); | |
| 328 | canvas.focus({ preventScroll: true }); | |
| 329 | let last = performance.now(); | |
| 330 | function frame(now) { | |
| 331 | update(Math.min(.04, (now - last) / 1000)); | |
| 332 | last = now; | |
| 333 | draw(); | |
| 334 | requestAnimationFrame(frame); | |
| 335 | } | |
| 336 | requestAnimationFrame(frame); | |
| 337 | </script> | |
| 338 | </body> | |
| 339 | </html> |
firefly-jar/index.html +1
| @@ -22,6 +22,7 @@ | ||
| 22 | 22 | <style> |
| 23 | 23 | :root { : #1c1b19; : #8b877f; : #fff; : #c9b43a; } |
| 24 | 24 | * { box-sizing: border-box; } |
| 25 | html, body { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent; } | |
| 25 | 26 | html, body { height: 100%; margin: 0; background: var(); color: var(); } |
| 26 | 27 | body { font: 13px/1.35 ui-monospace, "Cascadia Mono", Consolas, Menlo, monospace; overflow: hidden; } |
| 27 | 28 | #stage { position: fixed; inset: 0; display: grid; place-items: center; } |
index.html +1
| @@ -91,6 +91,7 @@ footer a::after { content: none; } | ||
| 91 | 91 | <li data-kind="Precision" data-input="Hold" style="--accent:#6f4a33"><a href="espresso-bar/">Espresso Bar</a><span>Pour twenty coffees to the line as cups slide past: hold to pour, let go early, over the brim costs a life</span><span>Game forged by the crew</span><span class="tags">Precision · Hold</span></li> |
| 92 | 92 | <li data-kind="Sport" data-input="Tap" style="--accent:#3f7a3a"><a href="goalkeeper/">Goalkeeper</a><span>Read the taker and dive left or right to save ten penalties</span><span>Game forged by the crew</span><span class="tags">Sport · Tap</span></li> |
| 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 | <li data-kind="Catching" data-input="Drag" style="--accent:#d08a2e"><a href="egg-basket/">Egg Basket</a><span>Slide the basket under the eave and catch the eggs before three crack</span><span>Game forged by the crew</span><span class="tags">Catching · Drag</span></li> | |
| 94 | 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> |
| 95 | 96 | </ul> |
| 96 | 97 | <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> |
og/egg-basket.png added
Binary file not shown.
og/home.png
Binary file not shown.
rounds.mjs +35
| @@ -18,6 +18,31 @@ export const ROUNDS = { | ||
| 18 | 18 | // tend the fire: a twig whenever the flames sink toward the lower mark |
| 19 | 19 | 'campfire': { over: /play again/, outcome: /dawn, the fire held · \d+ s/, beat: g => { if (g.read('G.heat') < 55 && g.read('G.fuel.length') < 3) g.press(' '); g.step(5); } }, |
| 20 | 20 | 'domino-run': { over: /play again/, outcome: /\d+ points/, beat: g => { g.press(' '); g.press('ArrowRight'); g.press('Enter'); g.run(1); } }, |
| 21 | // follow the egg that drops next with the arrow keys until 25 are caught, then let three crack | |
| 22 | 'egg-basket': { over: /play again/, outcome: /\d+ eggs · you win/, beat: g => { | |
| 23 | if (g.read('R.score') < 25) { | |
| 24 | const lane = g.read(`(() => { const e = R.eggs.filter(e => e.state !== 'caught').sort((a, b) => (EggBasket.ROOF_LEN - a.rolled) / a.speed - (EggBasket.ROOF_LEN - b.rolled) / b.speed)[0]; return e ? e.lane : -1; })()`); | |
| 25 | const now = g.read('EggBasket.laneOf(R.target)'); | |
| 26 | if (lane >= 0 && lane !== now) g.press(lane < now ? 'ArrowLeft' : 'ArrowRight', .02); | |
| 27 | } | |
| 28 | g.step(1); | |
| 29 | } }, | |
| 30 | // hold the pour until the fill reaches the line less what the falling stream still adds, for each cup under the spout | |
| 31 | 'espresso-bar': { over: /pour again/, outcome: /twenty cups poured · \d+/, beat: (() => { | |
| 32 | let held = false; | |
| 33 | return g => { | |
| 34 | const want = g.read('Math.abs(R.x) <= Pour.WINDOW && !R.done && R.fill < Pour.lineOf(R.cup) - Pour.RATE * Pour.DELAY'); | |
| 35 | if (want && !held) g.keyDown(' '); | |
| 36 | if (!want && held) g.keyUp(' '); | |
| 37 | held = want; | |
| 38 | g.step(1); | |
| 39 | }; | |
| 40 | })() }, | |
| 41 | // shoo every moth so none reaches the lantern, and the night runs its ninety seconds out | |
| 42 | 'firefly-jar': { over: /another night/, outcome: /the night is survived · \d+ in the jar/, beat: g => { | |
| 43 | const at = g.read(`(() => { const t = Night.targets(R).find(t => t.kind === 'moth'); if (!t) return null; const p = scr(t.u, t.v, t.z); return [p.sx, p.sy]; })()`); | |
| 44 | if (at) g.tap(at[0], at[1]); else g.step(2); | |
| 45 | } }, | |
| 21 | 46 | 'free-throw': { over: /play again/, outcome: /\d+ of 30/, beat: g => { g.hold(...MID, .6); g.run(.45); g.tap(...MID); g.run(1.5); g.tap(...MID); } }, |
| 22 | 47 | // dive the way the taker leans, once the tell shows |
| 23 | 48 | 'goalkeeper': { over: /play again/, outcome: /you win: \d+ saves/, beat: g => { |
| @@ -40,6 +65,16 @@ export const ROUNDS = { | ||
| 40 | 65 | 'sled-run': { over: /ride again/, outcome: /[1-9]\d* m · \d+ clean/, beat: g => { g.hold(...MID, .6); g.run(.6); } }, |
| 41 | 66 | 'stari-most': { over: /dive again/, outcome: /total [\d.]+/, beat: g => { g.hold(...MID, .4); g.run(.5); g.hold(...MID, .6); g.run(2); g.tap(...MID); } }, |
| 42 | 67 | 'stone-skip': { over: /play again/, outcome: /\d+ bounces/, beat: g => { g.drag([180, 140], [140, 170], .4); g.run(3); g.tap(...MID); } }, |
| 68 | // pull the wrong lever every time: three lost trains end the game short of the line | |
| 69 | 'signal-box': { over: /run again/, outcome: /0 trains routed · the junction needs \d+/, beat: g => { | |
| 70 | if (g.read('P.phase') === 'input') g.press(String((g.read('P.seq[P.got]') + 1) % 5 + 1)); | |
| 71 | g.run(.5); | |
| 72 | } }, | |
| 73 | // steer through the middle of the next gate's gap | |
| 74 | 'soapbox-derby': { over: /roll again/, outcome: /banner! [\d.]+ s/, beat: g => { | |
| 75 | const want = g.read(`(() => { for (let n = 0; n < Cart.GATES; n++) { const g = Cart.gateAt(n, R.seed); if (R.x < g.x + Cart.GATE_DEPTH + 2) return Math.max(-1, Math.min(1, g.centre / Cart.LANE_MAX)); } return 0; })()`); | |
| 76 | g.drag([180, 100], [180 + 100 * (want - g.read('G.steer')), 100], .04); | |
| 77 | } }, | |
| 43 | 78 | // brake for the mark by the distance it takes to stop, drive off when the doors close |
| 44 | 79 | 'tram-line': { over: /drive again/, outcome: /end of the line · \+\d+ for time/, beat: g => { |
| 45 | 80 | const d = g.read('Line.distanceToMark(R)'), v = g.read('R.v'); |
serve.test.mjs +7
| @@ -423,6 +423,13 @@ test('Soapbox Derby and its rules script are served without a track', async () = | ||
| 423 | 423 | expect((await fetch(base + '/soapbox-derby/cart.js')).status).toBe(200); |
| 424 | 424 | }); |
| 425 | 425 | |
| 426 | test('Egg Basket and its rules script are served without a track', async () => { | |
| 427 | const page = await fetch(base + '/egg-basket/'); | |
| 428 | expect(page.status).toBe(200); | |
| 429 | expect(page.headers.get('content-type')).toBe('text/html; charset=utf-8'); | |
| 430 | expect((await fetch(base + '/egg-basket/basket.js')).status).toBe(200); | |
| 431 | }); | |
| 432 | ||
| 426 | 433 | test('Espresso Bar and its rules script are served without a track', async () => { |
| 427 | 434 | const page = await fetch(base + '/espresso-bar/'); |
| 428 | 435 | expect(page.status).toBe(200); |
shell.js +1
| @@ -35,6 +35,7 @@ window.PixelPlays = (() => { | ||
| 35 | 35 | { id: 'espresso-bar', title: 'Espresso Bar', blurb: 'Pour twenty coffees to the line as cups slide past: hold to pour, let go early, over the brim costs a life', kind: 'Precision', input: 'Hold', accent: '#6f4a33', music: false, forged: true }, |
| 36 | 36 | { id: 'goalkeeper', title: 'Goalkeeper', blurb: 'Read the taker and dive left or right to save ten penalties', kind: 'Sport', input: 'Tap', accent: '#3f7a3a', music: false, forged: true }, |
| 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 | { id: 'egg-basket', title: 'Egg Basket', blurb: 'Slide the basket under the eave and catch the eggs before three crack', kind: 'Catching', input: 'Drag', accent: '#d08a2e', music: false, forged: true }, | |
| 38 | 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 }, |
| 39 | 40 | ]; |
| 40 | 41 | const MUSIC_VOLUME = .4; |
new above
offline