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 #127
ledger/pixel-plays:ledger/84-add-egg-basket-slide-the-basket-along-th → main 9 commits fork
11 files
changed +742
−3
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 | | [](egg-basket/) | A red henhouse with a sloping tin roof and three hens on the ridge: they lay brown eggs (1 point) and rare golden ones (3) that roll down one of four roof lanes and drop off the eave; slide the basket under each before it reaches the ground, where it cracks; rolling speeds up every ten eggs laid (never so fast that the basket cannot cross three lanes in time), three cracks end the round and 25 or more wins (forged by the crew on the engine and shell above, not by the original creators) | Drag the basket; Left and Right arrows move one lane | | |
| 47 | 48 | | [](empire/) | A real-time strategy game on a 44 by 44 map against a red bot (easy, normal or hard): four villagers and a town center each, gather food, wood, gold and stone, build houses, camps, farms and military buildings, climb the Dark, Feudal, Castle and Imperial Ages, research upgrades, train militia, spearmen, archers, knights and rams, and destroy every town center and castle of the enemy; fog of war, a minimap, attack alerts, pause, 1x/2x/4x speed, an autopilot, a save that survives a reload and a best time per level (forged by the crew on the engine and shell above, not by the original creators) | Tap or drag a box to select, tap to command, panel buttons to build and train; arrows move the view, Enter taps its centre cross, 1-9 press buttons | |
| 48 | 49 | | [](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 | |
| 49 | 50 | |
| @@ -65,7 +66,7 @@ On the forge, `forge.json` runs `bun serve.mjs` with health check `/`. State is | ||
| 65 | 66 | 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. |
| 66 | 67 | |
| 67 | 68 | ## Add a game |
| 68 | 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, Hot Air Balloon and Empire Age 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`. | |
| 69 | 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, Hot Air Balloon, Empire Age 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`. | |
| 69 | 70 | |
| 70 | 71 | - `shell.js` provides the game menu, the Rules popup, the credits and the background music. The menu lists the games grouped under their kind (the not-yet-made ones last, under Soon) and scrolls within the screen; type in its find box to narrow it by a word of the title, kind, input or blurb (keys typed there never reach the game), and press Escape to close it. |
| 71 | 72 | - `engine.js` provides the camera, depth buffer, colour ramps, shadows, edges, figures, trees and wind as globals. |
| @@ -81,7 +82,7 @@ Create `<id>/index.html`, load `../shell.js`, call `PixelPlays.mount({ id, rules | ||
| 81 | 82 | - Depth-buffer edges: darker where an object sits in front, a warm rim on upper-left edges. |
| 82 | 83 | - Large flat surfaces use closely spaced tones; wide steps make the dither read as a checkerboard. |
| 83 | 84 | - Characters stay sharp: flat bands with no dither, a dark silhouette on their own pixels, patterns at least 3px wide. |
| 84 | - 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`. | |
| 85 | - 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, Hot Air Balloon's windsock and cloud wisps drift, and Egg Basket's hens peck and the straw in its basket stirs (its rolling eggs are play, not ambience, and still move); all of it stops under `prefers-reduced-motion`. | |
| 85 | 86 | |
| 86 | 87 | ## Empire Age |
| 87 | 88 |
egg-basket/basket.js added +116
| @@ -0,0 +1,116 @@ | ||
| 1 | 'use strict'; | |
| 2 | // The egg basket rules: three hens on a henhouse ridge lay eggs that roll down one of four roof lanes and drop off the | |
| 3 | // eave, and a basket slides along the eave to catch them. A brown egg scores 1, a golden one 3; an egg that reaches the | |
| 4 | // ground cracks, and the third crack ends the round. Rolling speeds up every ten eggs laid. Lanes are numbered 0 to 3 | |
| 5 | // across the eave and the basket's place is a number on that scale. Every random choice comes from the game's seed. | |
| 6 | // Plain numbers in, plain numbers out, no DOM. | |
| 7 | (function (root) { | |
| 8 | const LANES = 4, HENS = 3, CRACKS = 3, WIN_AT = 25; | |
| 9 | const EGG_POINTS = 1, GOLD_POINTS = 3, GOLD_CHANCE = .14; | |
| 10 | const BASKET_SPEED = 6; // lanes per second | |
| 11 | const CATCH_HALF = .5; // the basket covers its own lane and half of each neighbour | |
| 12 | const EVERY = 10, ROLL_START = 2.6, ROLL_STEP = .9, ROLL_FLOOR = 1.15; // seconds roof to eave; each ten eggs multiply it by ROLL_STEP | |
| 13 | const REACH_MARGIN = .2; // an egg never drops sooner than the longest slide plus this | |
| 14 | const SLACK_BASE = .9, SLACK_FLOOR = .3, SLACK_SPREAD = .35, FIRST_LAY = .8; | |
| 15 | const STEP = 1 / 60, EPS = 1e-9; | |
| 16 | ||
| 17 | const finite = v => (Number.isFinite(v) ? v : 0); | |
| 18 | const clamp = (v, a, b) => Math.max(a, Math.min(b, v)); | |
| 19 | function rand(s) { // mulberry32 over the game's own state | |
| 20 | s.rng = (s.rng + 0x6D2B79F5) >>> 0; | |
| 21 | let t = s.rng; | |
| 22 | t = Math.imul(t ^ t >>> 15, t | 1); | |
| 23 | t ^= t + Math.imul(t ^ t >>> 7, t | 61); | |
| 24 | return ((t ^ t >>> 14) >>> 0) / 4294967296; | |
| 25 | } | |
| 26 | const levelOf = laid => Math.floor(Math.max(0, finite(laid)) / EVERY); | |
| 27 | const rollTime = level => Math.max(ROLL_FLOOR, ROLL_START * Math.pow(ROLL_STEP, Math.max(0, finite(level)))); | |
| 28 | const slideTime = (a, b) => Math.abs(finite(a) - finite(b)) / BASKET_SPEED; | |
| 29 | const slackOf = level => Math.max(SLACK_FLOOR, SLACK_BASE - .06 * level); | |
| 30 | // the hen that lays for a lane: the three sit over lanes 0, 1.5 and 3, and each lays for the lane nearest her | |
| 31 | const henOf = lane => clamp(Math.round(clamp(finite(lane), 0, LANES - 1) * (HENS - 1) / (LANES - 1)), 0, HENS - 1); | |
| 32 | ||
| 33 | function newGame(best = 0, seed = 1) { | |
| 34 | const s = { | |
| 35 | phase: 'play', clock: 0, score: 0, caught: 0, cracks: 0, laid: 0, eggs: [], nextId: 1, | |
| 36 | basket: 1, target: 1, lastDrop: 0, lastLane: 1, nextLane: 0, nextGold: false, slack: 0, layAt: FIRST_LAY, | |
| 37 | seed: finite(seed) | 0, rng: (finite(seed) | 0) >>> 0, best: Math.max(0, finite(best)), record: false, win: false, timer: 0, | |
| 38 | }; | |
| 39 | planNext(s); | |
| 40 | s.layAt = FIRST_LAY; | |
| 41 | return s; | |
| 42 | } | |
| 43 | // choose the next egg's lane and kind, and the earliest the basket could be there after the last drop | |
| 44 | function planNext(s) { | |
| 45 | s.nextLane = Math.floor(rand(s) * LANES); | |
| 46 | s.nextGold = rand(s) < GOLD_CHANCE; | |
| 47 | s.slack = slackOf(levelOf(s.laid)) + rand(s) * SLACK_SPREAD; | |
| 48 | const roll = rollTime(levelOf(s.laid)); | |
| 49 | s.layAt = Math.max(s.clock + .05, s.lastDrop + slideTime(s.lastLane, s.nextLane) + REACH_MARGIN + s.slack - roll); | |
| 50 | } | |
| 51 | // slide the basket one lane: dir is -1 or 1 | |
| 52 | function nudge(s, dir) { | |
| 53 | const d = finite(dir) < 0 ? -1 : finite(dir) > 0 ? 1 : 0; | |
| 54 | s.target = clamp(Math.round(s.target) + d, 0, LANES - 1); | |
| 55 | } | |
| 56 | // drag the basket to a place on the eave, in lanes | |
| 57 | function aim(s, lane) { s.target = clamp(finite(lane), 0, LANES - 1); } | |
| 58 | ||
| 59 | function lay(s, events) { | |
| 60 | const level = levelOf(s.laid), roll = rollTime(level); | |
| 61 | const egg = { id: s.nextId++, lane: s.nextLane, gold: s.nextGold, born: s.clock, roll, dropAt: s.clock + roll, hen: henOf(s.nextLane) }; | |
| 62 | s.eggs.push(egg); | |
| 63 | s.laid++; | |
| 64 | s.lastDrop = egg.dropAt; s.lastLane = egg.lane; | |
| 65 | events.push({ type: 'lay', egg: { ...egg }, level: levelOf(s.laid) }); | |
| 66 | if (levelOf(s.laid) > level) events.push({ type: 'faster', level: levelOf(s.laid), roll: rollTime(levelOf(s.laid)) }); | |
| 67 | planNext(s); | |
| 68 | } | |
| 69 | // how far down the roof an egg is: 0 at the ridge, 1 at the eave | |
| 70 | const progress = (s, egg) => clamp((s.clock - egg.born) / egg.roll, 0, 1); | |
| 71 | ||
| 72 | function drop(s, egg, events) { | |
| 73 | if (Math.abs(s.basket - egg.lane) <= CATCH_HALF + EPS) { | |
| 74 | const points = egg.gold ? GOLD_POINTS : EGG_POINTS; | |
| 75 | s.score += points; s.caught++; | |
| 76 | events.push({ type: 'catch', id: egg.id, lane: egg.lane, gold: egg.gold, points, score: s.score }); | |
| 77 | } else { | |
| 78 | s.cracks++; | |
| 79 | events.push({ type: 'crack', id: egg.id, lane: egg.lane, gold: egg.gold, cracks: s.cracks, left: CRACKS - s.cracks }); | |
| 80 | if (s.cracks >= CRACKS) finish(s, events); | |
| 81 | } | |
| 82 | } | |
| 83 | function finish(s, events) { | |
| 84 | s.phase = 'over'; s.timer = 0; | |
| 85 | s.win = verdict(s.score) === 'win'; | |
| 86 | if (s.score > s.best) { s.best = s.score; s.record = true; } | |
| 87 | events.push({ type: 'over', score: s.score, win: s.win, record: s.record }); | |
| 88 | } | |
| 89 | const verdict = score => (finite(score) >= WIN_AT ? 'win' : 'loss'); | |
| 90 | ||
| 91 | // move the basket and the eggs over dt seconds; returns what happened, in order | |
| 92 | function advance(s, dt) { | |
| 93 | const events = []; | |
| 94 | dt = clamp(finite(dt), 0, .1); | |
| 95 | if (s.phase !== 'play') { s.timer += dt; return events; } | |
| 96 | for (let left = dt; left > EPS && s.phase === 'play'; left -= STEP) { | |
| 97 | const h = Math.min(STEP, left); | |
| 98 | const d = s.target - s.basket, move = BASKET_SPEED * h; | |
| 99 | s.basket = Math.abs(d) <= move ? s.target : s.basket + Math.sign(d) * move; | |
| 100 | s.clock += h; | |
| 101 | if (s.clock >= s.layAt) lay(s, events); | |
| 102 | for (const egg of s.eggs.slice()) { | |
| 103 | if (s.phase !== 'play') break; | |
| 104 | if (s.clock >= egg.dropAt - EPS) { s.eggs.splice(s.eggs.indexOf(egg), 1); drop(s, egg, events); } | |
| 105 | } | |
| 106 | } | |
| 107 | return events; | |
| 108 | } | |
| 109 | const again = (s, seed) => (s.phase === 'over' && s.timer > .6 ? newGame(s.best, seed == null ? s.seed + 1 : seed) : null); | |
| 110 | ||
| 111 | const api = { | |
| 112 | LANES, HENS, CRACKS, WIN_AT, EGG_POINTS, GOLD_POINTS, BASKET_SPEED, CATCH_HALF, EVERY, ROLL_START, ROLL_FLOOR, REACH_MARGIN, | |
| 113 | levelOf, rollTime, slideTime, henOf, progress, newGame, nudge, aim, advance, again, verdict, | |
| 114 | }; | |
| 115 | if (typeof module !== 'undefined') module.exports = api; else root.Basket = api; | |
| 116 | })(typeof window !== 'undefined' ? window : globalThis); |
egg-basket/basket.test.mjs added +179
| @@ -0,0 +1,179 @@ | ||
| 1 | import { test, expect } from 'bun:test'; | |
| 2 | import Basket from './basket.js'; | |
| 3 | ||
| 4 | // run the game for `seconds`, with `steer(s)` called each step; returns every event | |
| 5 | function play(s, seconds, steer = () => {}) { | |
| 6 | const events = []; | |
| 7 | for (let t = 0; t < seconds && s.phase === 'play'; t += 1 / 30) { steer(s); events.push(...Basket.advance(s, 1 / 30)); } | |
| 8 | return events; | |
| 9 | } | |
| 10 | // stand under the egg that will drop first | |
| 11 | const chase = s => { const e = s.eggs[0]; if (e) Basket.aim(s, e.lane); }; | |
| 12 | // stay as far as possible from the egg that will drop first | |
| 13 | const avoid = s => { const e = s.eggs[0]; if (e) Basket.aim(s, e.lane < 1.5 ? 3 : 0); }; | |
| 14 | ||
| 15 | test('eggs are laid by one of three hens and roll down one of four lanes', () => { | |
| 16 | const s = Basket.newGame(0, 5), laid = play(s, 40, chase).filter(e => e.type === 'lay'); | |
| 17 | expect(laid.length).toBeGreaterThan(10); | |
| 18 | for (const { egg } of laid) { | |
| 19 | expect([0, 1, 2, 3]).toContain(egg.lane); | |
| 20 | expect([0, 1, 2]).toContain(egg.hen); | |
| 21 | } | |
| 22 | expect(new Set(laid.map(e => e.egg.lane)).size).toBe(4); | |
| 23 | expect(Basket.henOf(0)).toBe(0); | |
| 24 | expect(Basket.henOf(3)).toBe(2); | |
| 25 | }); | |
| 26 | ||
| 27 | test('an egg rolls from the ridge to the eave and drops there', () => { | |
| 28 | const s = Basket.newGame(0, 1); | |
| 29 | play(s, 1.2, () => {}); | |
| 30 | const egg = s.eggs[0]; | |
| 31 | expect(egg).toBeTruthy(); | |
| 32 | const p = Basket.progress(s, egg); | |
| 33 | expect(p).toBeGreaterThan(0); | |
| 34 | expect(p).toBeLessThan(1); | |
| 35 | const events = play(s, egg.roll, () => {}); | |
| 36 | expect(events.some(e => e.type === 'catch' || e.type === 'crack')).toBe(true); | |
| 37 | }); | |
| 38 | ||
| 39 | test('a brown egg caught is worth 1 and a golden egg 3', () => { | |
| 40 | const s = Basket.newGame(0, 3), events = play(s, 90, chase).filter(e => e.type === 'catch'); | |
| 41 | const brown = events.find(e => !e.gold), gold = events.find(e => e.gold); | |
| 42 | expect(brown.points).toBe(1); | |
| 43 | expect(gold.points).toBe(3); | |
| 44 | expect(Basket.EGG_POINTS).toBe(1); | |
| 45 | expect(Basket.GOLD_POINTS).toBe(3); | |
| 46 | expect(s.score).toBe(events.reduce((n, e) => n + e.points, 0)); | |
| 47 | }); | |
| 48 | ||
| 49 | test('golden eggs are rare', () => { | |
| 50 | const s = Basket.newGame(0, 9), laid = play(s, 120, chase).filter(e => e.type === 'lay'); | |
| 51 | const gold = laid.filter(e => e.egg.gold).length; | |
| 52 | expect(gold).toBeGreaterThan(0); | |
| 53 | expect(gold).toBeLessThan(laid.length / 3); | |
| 54 | }); | |
| 55 | ||
| 56 | test('an egg that reaches the ground cracks and costs one of three', () => { | |
| 57 | const s = Basket.newGame(0, 2), events = play(s, 4, avoid); | |
| 58 | const first = events.find(e => e.type === 'crack'); | |
| 59 | expect(first.cracks).toBe(1); | |
| 60 | expect(first.left).toBe(2); | |
| 61 | expect(s.cracks).toBe(1); | |
| 62 | expect(s.score).toBe(0); | |
| 63 | }); | |
| 64 | ||
| 65 | test('the third crack ends the round and nothing moves after', () => { | |
| 66 | const s = Basket.newGame(0, 2), events = play(s, 60, avoid); | |
| 67 | expect(s.phase).toBe('over'); | |
| 68 | expect(s.cracks).toBe(3); | |
| 69 | expect(events.filter(e => e.type === 'crack').length).toBe(3); | |
| 70 | expect(events.filter(e => e.type === 'over').length).toBe(1); | |
| 71 | const clock = s.clock; | |
| 72 | expect(Basket.advance(s, .1)).toEqual([]); | |
| 73 | expect(s.clock).toBe(clock); | |
| 74 | }); | |
| 75 | ||
| 76 | test('rolling speeds up every ten eggs laid', () => { | |
| 77 | expect(Basket.levelOf(0)).toBe(0); | |
| 78 | expect(Basket.levelOf(9)).toBe(0); | |
| 79 | expect(Basket.levelOf(10)).toBe(1); | |
| 80 | expect(Basket.levelOf(25)).toBe(2); | |
| 81 | expect(Basket.rollTime(1)).toBeLessThan(Basket.rollTime(0)); | |
| 82 | expect(Basket.rollTime(2)).toBeLessThan(Basket.rollTime(1)); | |
| 83 | const s = Basket.newGame(0, 4), events = play(s, 80, chase); | |
| 84 | const laid = events.filter(e => e.type === 'lay').map(e => e.egg); | |
| 85 | expect(laid[9].roll).toBe(Basket.rollTime(0)); | |
| 86 | expect(laid[10].roll).toBe(Basket.rollTime(1)); | |
| 87 | expect(laid[10].roll).toBeLessThan(laid[9].roll); | |
| 88 | expect(events.find(e => e.type === 'faster').level).toBe(1); | |
| 89 | }); | |
| 90 | ||
| 91 | test('no egg ever drops sooner than the slide across three lanes plus 200 ms', () => { | |
| 92 | const crossing = 3 / Basket.BASKET_SPEED + Basket.REACH_MARGIN; | |
| 93 | expect(Basket.ROLL_FLOOR).toBeGreaterThanOrEqual(crossing); | |
| 94 | expect(Basket.rollTime(0)).toBeGreaterThanOrEqual(crossing); | |
| 95 | expect(Basket.rollTime(500)).toBeGreaterThanOrEqual(crossing); | |
| 96 | const s = Basket.newGame(0, 6), laid = play(s, 200, chase).filter(e => e.type === 'lay'); | |
| 97 | for (const { egg } of laid) expect(egg.dropAt - egg.born).toBeGreaterThanOrEqual(crossing); | |
| 98 | }); | |
| 99 | ||
| 100 | test('a player who goes straight to each egg catches them all, however fast it rolls', () => { | |
| 101 | const s = Basket.newGame(0, 8), events = play(s, 150, chase); | |
| 102 | expect(s.laid).toBeGreaterThan(30); | |
| 103 | expect(s.cracks).toBe(0); | |
| 104 | expect(events.filter(e => e.type === 'catch').length).toBeGreaterThan(30); | |
| 105 | }); | |
| 106 | ||
| 107 | test('the keys move the basket one lane at a time, and the drag puts it where it is dragged', () => { | |
| 108 | const s = Basket.newGame(0, 1); | |
| 109 | Basket.aim(s, 1); Basket.advance(s, .1); | |
| 110 | Basket.nudge(s, 1); expect(s.target).toBe(2); | |
| 111 | Basket.nudge(s, 1); Basket.nudge(s, 1); expect(s.target).toBe(3); | |
| 112 | Basket.nudge(s, -1); expect(s.target).toBe(2); | |
| 113 | Basket.aim(s, -5); expect(s.target).toBe(0); | |
| 114 | Basket.aim(s, 9); expect(s.target).toBe(3); | |
| 115 | Basket.aim(s, NaN); expect(s.target).toBe(0); | |
| 116 | const from = s.basket; | |
| 117 | Basket.aim(s, 0); Basket.advance(s, .1); | |
| 118 | expect(s.basket).toBeLessThan(from); | |
| 119 | expect(from - s.basket).toBeLessThanOrEqual(Basket.BASKET_SPEED * .1 + 1e-9); | |
| 120 | }); | |
| 121 | ||
| 122 | test('the basket catches an egg over its lane or half a lane either side, not beyond', () => { | |
| 123 | const trial = place => { | |
| 124 | const t = Basket.newGame(0, 1); | |
| 125 | t.basket = t.target = place; | |
| 126 | t.eggs.push({ id: 99, lane: 2, gold: false, born: 0, roll: .1, dropAt: .02, hen: 1 }); | |
| 127 | t.layAt = 1e9; | |
| 128 | return Basket.advance(t, .05).map(e => e.type); | |
| 129 | }; | |
| 130 | expect(trial(2)).toEqual(['catch']); | |
| 131 | expect(trial(2.5)).toEqual(['catch']); | |
| 132 | expect(trial(1.6)).toEqual(['catch']); | |
| 133 | expect(trial(2.7)).toEqual(['crack']); | |
| 134 | expect(trial(0)).toEqual(['crack']); | |
| 135 | }); | |
| 136 | ||
| 137 | test('25 or more wins and anything below is a loss', () => { | |
| 138 | expect(Basket.WIN_AT).toBe(25); | |
| 139 | expect(Basket.verdict(25)).toBe('win'); | |
| 140 | expect(Basket.verdict(40)).toBe('win'); | |
| 141 | expect(Basket.verdict(24)).toBe('loss'); | |
| 142 | expect(Basket.verdict(0)).toBe('loss'); | |
| 143 | const finish = score => { | |
| 144 | const s = Basket.newGame(0, 1); | |
| 145 | s.score = score; s.cracks = 2; s.basket = s.target = 0; s.layAt = 1e9; | |
| 146 | s.eggs.push({ id: 1, lane: 3, gold: false, born: 0, roll: .1, dropAt: .02, hen: 2 }); | |
| 147 | Basket.advance(s, .05); | |
| 148 | return s; | |
| 149 | }; | |
| 150 | expect(finish(25).win).toBe(true); | |
| 151 | expect(finish(24).win).toBe(false); | |
| 152 | expect(finish(24).phase).toBe('over'); | |
| 153 | }); | |
| 154 | ||
| 155 | test('the best is kept, flagged as a record only when beaten, and a new round starts after a pause', () => { | |
| 156 | const s = Basket.newGame(3, 2); | |
| 157 | play(s, 5, chase); | |
| 158 | const hold = s.score; | |
| 159 | play(s, 60, avoid); | |
| 160 | expect(s.phase).toBe('over'); | |
| 161 | expect(s.best).toBe(Math.max(3, s.score)); | |
| 162 | expect(s.record).toBe(s.score > 3); | |
| 163 | expect(hold).toBeLessThanOrEqual(s.score); | |
| 164 | expect(Basket.again(s)).toBeNull(); | |
| 165 | for (let i = 0; i < 7; i++) Basket.advance(s, .1); | |
| 166 | const next = Basket.again(s); | |
| 167 | expect(next.phase).toBe('play'); | |
| 168 | expect(next.best).toBe(s.best); | |
| 169 | expect(next.score).toBe(0); | |
| 170 | expect(next.cracks).toBe(0); | |
| 171 | }); | |
| 172 | ||
| 173 | test('one seed and one set of moves play out the same, and odd input is tolerated', () => { | |
| 174 | const a = Basket.newGame(0, 12), b = Basket.newGame(0, 12); | |
| 175 | expect(play(a, 30, chase)).toEqual(play(b, 30, chase)); | |
| 176 | const s = Basket.newGame(NaN, NaN); | |
| 177 | Basket.nudge(s, NaN); Basket.aim(s, undefined); Basket.advance(s, NaN); Basket.advance(s, -1); Basket.advance(s, Infinity); | |
| 178 | expect(Number.isFinite(s.basket)).toBe(true); | |
| 179 | }); |
egg-basket/index.html added +373
| @@ -0,0 +1,373 @@ | ||
| 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; 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 | #line { 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="A red henhouse with a sloping tin roof, three hens on the ridge and a straw-lined basket on the yard below the eave. Hens lay eggs that roll down one of four roof lanes and drop off the eave: drag on the canvas, or press the Left and Right arrow keys to move the basket one lane, to catch them. A brown egg is worth 1, a golden egg 3, and an egg that reaches the ground cracks. Three cracks end the round; 25 or more wins."></canvas></div> | |
| 42 | <div id="scores"> | |
| 43 | <div class="row"><b id="caught">0</b><span>points</span></div> | |
| 44 | <div class="row"><b id="cracks">3</b><span>cracks left</span></div> | |
| 45 | <div class="row"><b id="best">0</b><span>best</span></div> | |
| 46 | </div> | |
| 47 | <div class="hud" id="bottom"> | |
| 48 | <div id="line"><span id="status" role="status" aria-live="polite"></span><span id="idle"></span></div> | |
| 49 | <button id="mute" type="button" aria-pressed="false">sound on</button> | |
| 50 | </div> | |
| 51 | <script src="../shell.js"></script> | |
| 52 | <script> | |
| 53 | PixelPlays.mount({ | |
| 54 | id: 'egg-basket', | |
| 55 | rules: [ | |
| 56 | 'Drag, or press Left and Right, to slide the basket.', | |
| 57 | 'Brown egg 1, golden 3. Ground: it cracks.', | |
| 58 | 'Three cracks end it. 25 or more wins.', | |
| 59 | ], | |
| 60 | }); | |
| 61 | </script> | |
| 62 | <script>window.PP_SCENE = { originY: 6, shadowX: .15, shadowY: -.7 };</script> | |
| 63 | <script src="../engine.js"></script> | |
| 64 | <script src="basket.js"></script> | |
| 65 | <script> | |
| 66 | 'use strict'; | |
| 67 | ||
| 68 | const BEST_KEY = 'pixel-plays-egg-basket-best'; | |
| 69 | const LANE_W = 2.6, HALF = 6.2; // lane spacing, and half the house's width along the eave | |
| 70 | const WB0 = -3.5, WB1 = 3.5, B0 = -3.8, B1 = 4.2, ZR = 5.7, ZE = 3.1, WALLZ = 3.3, BASKET_B = 5.7; | |
| 71 | const FALL = .32; // seconds an egg takes to drop from the eave to the basket or the ground | |
| 72 | const RED = [176, 52, 44], RED_DARK = [128, 38, 36], TRIM = [236, 226, 204], DOOR = [96, 62, 44], PANE = [72, 90, 112]; | |
| 73 | const TIN = [150, 160, 170], TIN_DARK = [96, 106, 120]; | |
| 74 | const WICKER = [190, 138, 70], STRAW = [226, 196, 108]; | |
| 75 | const EGG = [176, 112, 62], GOLD = [236, 184, 44], YOLK = [244, 200, 60], SHELL = [240, 232, 214]; | |
| 76 | const HENS = [{ body: [196, 118, 58], wing: [150, 84, 40] }, { body: [240, 232, 208], wing: [206, 194, 166] }, { body: [86, 76, 82], wing: [56, 50, 58] }]; | |
| 77 | const COMB = [214, 46, 44], BEAK = [236, 170, 40]; | |
| 78 | const laneA = lane => (lane - 1.5) * LANE_W; | |
| 79 | const world = (a, b) => [b, -a]; // lanes run screen right-up, the roof's slope screen right-down | |
| 80 | const groundOf = (sx, sy) => { const g = unproject(sx, sy); return { a: -g.y, b: g.x }; }; | |
| 81 | const roofZ = b => ZE + (ZR - ZE) * (B1 - b) / (B1 - B0); | |
| 82 | const eggAt = (egg, t) => { const b = B0 + .5 + (B1 - B0 - .5) * t; return [b, laneA(egg.lane), roofZ(b)]; }; | |
| 83 | ||
| 84 | /* ---------- state ---------- */ | |
| 85 | const best0 = (() => { try { return Number(localStorage.getItem(BEST_KEY)) || 0; } catch (e) { return 0; } })(); | |
| 86 | let R = Basket.newGame(best0, Date.now() & 0xffff); | |
| 87 | const G = { time: 0, status: '', noteUntil: 0, falls: [], splats: [], laid: [0, 0, 0], sparkle: 0 }; | |
| 88 | const nodes = {}; | |
| 89 | for (const id of ['caught', 'cracks', 'best', 'status', 'idle']) nodes[id] = document.getElementById(id); | |
| 90 | function renderHud() { | |
| 91 | nodes.caught.textContent = R.score; | |
| 92 | nodes.cracks.textContent = Basket.CRACKS - R.cracks; | |
| 93 | nodes.best.textContent = R.best; | |
| 94 | } | |
| 95 | function setStatus(text) { | |
| 96 | if (text === G.status) return; | |
| 97 | G.status = text; | |
| 98 | nodes.status.textContent = text; // the live region: catch, crack, faster and game-over notes only | |
| 99 | nodes.idle.textContent = ''; | |
| 100 | } | |
| 101 | function setIdle(text) { // the roof count is shown but never announced | |
| 102 | if (text === G.status) return; | |
| 103 | G.status = text; | |
| 104 | nodes.status.textContent = ''; | |
| 105 | nodes.idle.textContent = text; | |
| 106 | } | |
| 107 | function note(text) { setStatus(text); G.noteUntil = G.time + 2.2; } | |
| 108 | const HINT = 'drag or Left and Right to slide the basket · golden eggs count 3'; | |
| 109 | ||
| 110 | /* ---------- baked scene: the yard, the henhouse ---------- */ | |
| 111 | // the yard is a rounded patch of dirt and grass floating on white | |
| 112 | function groundColor(ix, iy) { | |
| 113 | const { a, b } = groundOf(ix + .5, iy + .5); | |
| 114 | const edge = Math.pow(Math.pow(Math.abs(a) / 10, 8) + Math.pow(Math.abs(b - 1.8) / 8.6, 8), .125); | |
| 115 | const rough = edge + (noise(a * .5, b * .5) - .5) * .05; | |
| 116 | if (rough > 1.03) return 0; | |
| 117 | if (rough > .93 && hash(ix, iy) < (rough - .93) / .1) return 0; | |
| 118 | const t = .86 + hash(ix, iy) * .2 + (noise(a * .3, b * .3) - .5) * .25; | |
| 119 | if (b > BASKET_B - .9 && b < BASKET_B + .9) { // the packed dirt strip the basket runs on, lane marks in it | |
| 120 | const lane = (a / LANE_W + 2) % 1, mark = Math.abs(lane) < .04 || Math.abs(lane - 1) < .04; | |
| 121 | return mark ? rgb(150, 124, 92) : rgb(186 * t, 156 * t, 112 * t); | |
| 122 | } | |
| 123 | const g = GRASS[clamp(Math.floor(noise(a * .22, b * .22) * 3 + hash(ix, iy) * .9 + .4), 0, 3)]; | |
| 124 | return rgb(g[0] * (.92 + hash(ix, iy) * .12), g[1] * (.92 + hash(ix, iy) * .12), g[2] * (.92 + hash(ix, iy) * .12)); | |
| 125 | } | |
| 126 | function paintAt(ramp, b, a, z, tone) { // one point of a surface, in the depth buffer | |
| 127 | const p = project(b, -a, z), ix = p.sx | 0, iy = p.sy | 0; | |
| 128 | if (ix < 0 || iy < 0 || ix >= W || iy >= H) return; | |
| 129 | const i = iy * W + ix; | |
| 130 | if (p.d <= zb[i]) return; | |
| 131 | zb[i] = p.d; | |
| 132 | pix[i] = ramp[clamp(Math.floor(tone + (bayerAt(ix, iy) - .5) * .5), 0, 4)]; | |
| 133 | } | |
| 134 | function walls() { | |
| 135 | box(WB0, -HALF, 0, WB1, HALF, WALLZ, RED, 3); | |
| 136 | const red = rampOf(RED), dark = rampOf(RED_DARK), trim = rampOf(TRIM), door = rampOf(DOOR), pane = rampOf(PANE); | |
| 137 | for (let b = WB0; b <= WB1; b += .05) for (let z = WALLZ; z <= roofZ(b); z += .05) { // the gable end under the roof | |
| 138 | paintAt(red, b, -HALF - .005, z, 2.3 + (Math.floor(b * 2.5) & 1) * .5 - .3); | |
| 139 | } | |
| 140 | for (let a = -HALF; a <= HALF; a += .05) for (let z = 0; z <= WALLZ; z += .05) { // the eave wall: planks, door, windows | |
| 141 | const plank = Math.floor((a + 8) * 2.4) & 1 ? 1.9 : 2.3; | |
| 142 | let tone = plank, ramp = red; | |
| 143 | if (Math.abs(a) < 1.05 && z < 1.95) { ramp = door; tone = 2 + (Math.floor((a + 2) * 4) & 1) * .6 - .3; } | |
| 144 | for (const w of [-3.9, 3.9]) { | |
| 145 | if (Math.abs(a - w) < .85 && z > 1.2 && z < 2.5) { ramp = Math.abs(a - w) < .62 && z > 1.35 && z < 2.35 ? pane : trim; tone = ramp === pane ? 2 + (a > w) * .8 : 3.2; } | |
| 146 | } | |
| 147 | if (Math.abs(a) > HALF - .18 || z < .14) { ramp = trim; tone = 2.4; } | |
| 148 | paintAt(ramp, WB1 + .004, a, z, tone); | |
| 149 | } | |
| 150 | for (let a = -.4; a <= .4; a += .05) for (let z = 1.95; z < 2.15; z += .05) paintAt(dark, WB1 + .006, a, z, 1.4); // hatch above the door | |
| 151 | for (let a = -.9; a <= .9; a += .05) for (let b = 0; b <= 1.4; b += .05) paintAt(trim, WB1 + b, a, .06 + b * .52, 2.4); // the ramp up to the door | |
| 152 | } | |
| 153 | function roof() { | |
| 154 | const tin = rampOf(TIN), edge = rampOf(TIN_DARK); | |
| 155 | for (let b = B0; b <= B1; b += .05) for (let a = -HALF - .6; a <= HALF + .6; a += .05) { | |
| 156 | const gap = Math.abs(((a / LANE_W + 2) % 1 + 1) % 1), groove = Math.min(gap, 1 - gap) < .03 && Math.abs(a) < HALF + .2; | |
| 157 | const rib = Math.floor((a + 10) * 3) & 1; | |
| 158 | paintAt(groove ? edge : tin, b, a, roofZ(b), groove ? 1.6 : 2.9 + rib * .45 + (hash(Math.floor(a * 6), Math.floor(b * 6)) - .5) * .5); | |
| 159 | } | |
| 160 | box(B1 - .14, -HALF - .6, ZE - .16, B1, HALF + .6, ZE, TIN_DARK, 5); // the eave lip the eggs roll off | |
| 161 | box(B0 - .1, -HALF - .6, ZR - .08, B0 + .45, HALF + .6, ZR + .14, TIN_DARK, 8); // ridge cap | |
| 162 | } | |
| 163 | function houseShadow() { | |
| 164 | for (let a = -HALF; a <= HALF; a += .45) for (const [b, z] of [[WB0, .3], [WB1, .3], [WB0, WALLZ], [WB1, WALLZ], [0, ZR], [B1, ZE], [B0, ZR]]) { | |
| 165 | const [x, y] = world(a, b); | |
| 166 | castShadow(x, y, z, .5, 1, 0); | |
| 167 | } | |
| 168 | } | |
| 169 | function bake() { | |
| 170 | ground.fill(0); shadowMask.fill(0); staticShadow.fill(0); kinds.fill(0); | |
| 171 | zb.fill(-1e9); | |
| 172 | pix.fill(rgb(255, 255, 255)); | |
| 173 | for (let iy = 0; iy < H; iy++) for (let ix = 0; ix < W; ix++) { | |
| 174 | const g = unproject(ix + .5, iy + .5), i = iy * W + ix, c = groundColor(ix, iy); | |
| 175 | if (!c) continue; | |
| 176 | pix[i] = c; ground[i] = 1; | |
| 177 | zb[i] = (g.x + g.y) / R2 * COS; | |
| 178 | } | |
| 179 | groundZ.set(zb); | |
| 180 | walls(); | |
| 181 | roof(); | |
| 182 | bakeOcclusion(); | |
| 183 | houseShadow(); | |
| 184 | staticShadow.set(shadowMask); | |
| 185 | applyShadows(groundZ); | |
| 186 | bakedPix.set(pix); | |
| 187 | bakedZ.set(zb); | |
| 188 | } | |
| 189 | ||
| 190 | /* ---------- figures ---------- */ | |
| 191 | function egg(b, a, z, gold) { | |
| 192 | const [x, y] = world(a, b); | |
| 193 | sphere(x, y, z, .3, gold ? GOLD : EGG, 'gloss'); | |
| 194 | sphere(x - .04, y + .05, z + .1, .22, gold ? GOLD : EGG, 'gloss'); | |
| 195 | } | |
| 196 | // hens sit over lanes 0, 1.5 and 3; the head pecks now and then and dips to lay | |
| 197 | function hen(k) { | |
| 198 | const lane = k * 1.5, [x, y] = world(laneA(lane), B0 + .75), z = ZR + .12, pal = HENS[k]; | |
| 199 | const laying = G.time - G.laid[k] < .4 ? 1 : 0; | |
| 200 | const peck = STILL ? 0 : (Math.floor(G.time * 2.5 + k * 1.7) % 6 === 0 ? 1 : 0); | |
| 201 | const sink = laying ? .14 : 0; | |
| 202 | sphere(x - .05, y, z + .58 - sink, .55, pal.body, 'solid'); | |
| 203 | sphere(x - .05, y + (k === 1 ? .38 : -.38), z + .58 - sink, .38, pal.wing, 'solid'); | |
| 204 | capsule([x - .45, y, z + .6 - sink], [x - .85, y, z + 1.05 + laying * .3], .2, .12, () => pal.wing, 'solid'); // tail | |
| 205 | const hx = x + .5 + peck * .22, hz = z + 1.18 - sink - peck * .42; | |
| 206 | capsule([x + .3, y, z + .8 - sink], [hx, y, hz], .2, .17, () => pal.body, 'solid'); | |
| 207 | sphere(hx, y, hz, .26, pal.body, 'solid'); | |
| 208 | sphere(hx, y, hz + .27, .1, COMB, 'solid'); | |
| 209 | sphere(hx + .26, y, hz - .04, .08, BEAK, 'solid'); | |
| 210 | sphere(hx + .2, y, hz - .2, .07, COMB, 'solid'); | |
| 211 | castShadow(x, y, z + .5, .55, 1, 0); | |
| 212 | } | |
| 213 | function basket(a) { | |
| 214 | const [x, y] = world(a, BASKET_B); | |
| 215 | for (const [z, r] of [[.22, .86], [.55, 1], [.88, 1.12]]) { | |
| 216 | for (let k = 0; k < 10; k++) { | |
| 217 | const t = k / 10 * Math.PI * 2, px = x + Math.cos(t) * r * .95, py = y + Math.sin(t) * r * .95; | |
| 218 | sphere(px, py, z, .3, (k + Math.round(z * 3)) & 1 ? WICKER : [166, 116, 58], 'rough'); | |
| 219 | } | |
| 220 | } | |
| 221 | sphere(x, y, .86, 1.05, STRAW, 'rough'); | |
| 222 | const sway = STILL ? 0 : Math.floor(G.time * 8); | |
| 223 | for (let k = 0; k < 7; k++) { // straw ends stirring at the rim, in whole pixels at 8 fps | |
| 224 | const t = k * 2.4 + (sway + k) % 3 * .3, px = x + Math.cos(t) * .8, py = y + Math.sin(t) * .8; | |
| 225 | capsule([px, py, 1.05], [px + Math.cos(t + sway) * .14, py + Math.sin(t) * .14, 1.32], .03, .02, () => STRAW, 'solid'); | |
| 226 | } | |
| 227 | capsule([x - 1.1, y - 1.1, .95], [x - 1.05, y + 1.15, .95], .06, .06, () => WICKER, 'rough'); // rim hoop | |
| 228 | castShadow(x, y, .7, 1.05, 1, 0); | |
| 229 | castShadow(x, y, 0, 1.1, 1, 0); | |
| 230 | } | |
| 231 | function draw() { | |
| 232 | pix.set(bakedPix); | |
| 233 | zb.set(bakedZ); | |
| 234 | HENS.forEach((h, k) => hen(k)); | |
| 235 | for (const e of R.eggs) { const [b, a, z] = eggAt(e, Basket.progress(R, e)); egg(b, a, z + .28, e.gold); } | |
| 236 | for (const f of G.falls) { | |
| 237 | const t = clamp((G.time - f.at) / FALL, 0, 1), a = laneA(f.lane) + (f.caught ? (laneA(R.basket) - laneA(f.lane)) * t : 0); | |
| 238 | const end = f.caught ? 1.05 : .32, z = ZE + .2 - (ZE + .2 - end) * t * t, b = B1 + (BASKET_B - B1 + (f.caught ? 0 : .2)) * t; | |
| 239 | egg(b, a, z, f.gold); | |
| 240 | const [sx, sy] = world(a, b); | |
| 241 | castShadow(sx, sy, z, .3, 1, 0); | |
| 242 | } | |
| 243 | for (const s of G.splats) { | |
| 244 | const [x, y] = world(laneA(s.lane), BASKET_B + .2); | |
| 245 | sphere(x, y, .06, .55, s.gold ? GOLD : SHELL, 'solid'); | |
| 246 | sphere(x + .05, y, .12, .26, YOLK, 'solid'); | |
| 247 | for (const [dx, dy] of [[.5, .3], [-.4, .5], [.2, -.55]]) sphere(x + dx, y + dy, .1, .13, SHELL, 'solid'); | |
| 248 | } | |
| 249 | basket(laneA(R.basket)); | |
| 250 | flushShadows(); | |
| 251 | applyShadows(groundZ); | |
| 252 | shadeEdges(); | |
| 253 | ctx.putImageData(image, 0, 0); | |
| 254 | } | |
| 255 | ||
| 256 | /* ---------- game ---------- */ | |
| 257 | function act(events) { | |
| 258 | for (const e of events) { | |
| 259 | if (e.type === 'lay') G.laid[e.egg.hen] = G.time; | |
| 260 | else if (e.type === 'catch') { | |
| 261 | G.falls.push({ at: G.time, lane: e.lane, gold: e.gold, caught: true }); | |
| 262 | note(e.gold ? `golden egg · +3 · ${e.score}` : `caught · ${e.score}`); | |
| 263 | sound(e.gold ? 'gold' : 'catch'); | |
| 264 | } else if (e.type === 'crack') { | |
| 265 | G.falls.push({ at: G.time, lane: e.lane, gold: e.gold, caught: false }); | |
| 266 | note(e.left ? `cracked · ${e.left} left` : 'cracked'); | |
| 267 | sound('crack'); | |
| 268 | } else if (e.type === 'faster') note('the eggs roll faster'); | |
| 269 | else if (e.type === 'over') { | |
| 270 | if (R.record) try { localStorage.setItem(BEST_KEY, String(R.best)); } catch (err) { /* private window: keep it for this visit */ } | |
| 271 | const tail = `${R.record ? ' · new best' : ` · best ${R.best}`} · tap or press space to play again`; | |
| 272 | setStatus(R.win ? `the basket is full · ${R.score} points${tail}` : `three cracked · ${R.score} points, 25 wins${tail}`); | |
| 273 | sound(R.win ? 'gold' : 'crack'); | |
| 274 | } | |
| 275 | } | |
| 276 | renderHud(); | |
| 277 | } | |
| 278 | function restart() { | |
| 279 | const next = Basket.again(R); | |
| 280 | if (!next) return false; | |
| 281 | R = next; | |
| 282 | G.falls.length = 0; G.splats.length = 0; | |
| 283 | note(HINT); | |
| 284 | renderHud(); | |
| 285 | return true; | |
| 286 | } | |
| 287 | function update(dt) { | |
| 288 | G.time += dt; | |
| 289 | act(Basket.advance(R, dt)); | |
| 290 | for (const f of G.falls.slice()) { | |
| 291 | if (G.time - f.at < FALL) continue; | |
| 292 | G.falls.splice(G.falls.indexOf(f), 1); | |
| 293 | if (!f.caught) G.splats.push({ lane: f.lane, gold: f.gold, at: G.time }); | |
| 294 | } | |
| 295 | G.splats = G.splats.filter(s => G.time - s.at < 2.4).slice(-6); | |
| 296 | if (R.phase === 'play' && G.time >= G.noteUntil) setIdle(`${R.eggs.length} on the roof · golden eggs count 3`); | |
| 297 | } | |
| 298 | ||
| 299 | /* ---------- input ---------- */ | |
| 300 | function pointTo(e) { | |
| 301 | const r = canvas.getBoundingClientRect(), g = groundOf((e.clientX - r.left) / (r.width || W) * W, (e.clientY - r.top) / (r.height || H) * H); | |
| 302 | Basket.aim(R, g.a / LANE_W + 1.5); | |
| 303 | } | |
| 304 | let dragging = null; | |
| 305 | canvas.addEventListener('pointerdown', e => { | |
| 306 | ensureAudio(); | |
| 307 | canvas.focus({ preventScroll: true }); | |
| 308 | if (restart()) return; | |
| 309 | dragging = e.pointerId; | |
| 310 | try { canvas.setPointerCapture(e.pointerId); } catch (err) { /* synthetic pointers have nothing to capture */ } | |
| 311 | pointTo(e); | |
| 312 | }); | |
| 313 | canvas.addEventListener('pointermove', e => { if (dragging === e.pointerId) pointTo(e); }); | |
| 314 | for (const type of ['pointerup', 'pointercancel']) canvas.addEventListener(type, e => { if (dragging === e.pointerId) dragging = null; }); | |
| 315 | const KEYMAP = { ArrowLeft: -1, a: -1, A: -1, ArrowRight: 1, d: 1, D: 1 }; | |
| 316 | canvas.addEventListener('keydown', e => { | |
| 317 | const k = KEYMAP[e.key]; | |
| 318 | if (!k && e.key !== ' ' && e.key !== 'Enter') return; | |
| 319 | e.preventDefault(); | |
| 320 | ensureAudio(); | |
| 321 | if (restart() || e.repeat || !k) return; | |
| 322 | dragging = null; | |
| 323 | Basket.nudge(R, k); | |
| 324 | }); | |
| 325 | ||
| 326 | /* ---------- sound ---------- */ | |
| 327 | let audio = null, muted = false; | |
| 328 | function ensureAudio() { | |
| 329 | if (audio) { if (audio.state === 'suspended') audio.resume(); return; } | |
| 330 | const AC = window.AudioContext || window.webkitAudioContext; | |
| 331 | if (AC) audio = new AC(); | |
| 332 | } | |
| 333 | function sound(kind) { | |
| 334 | if (!audio || muted) return; | |
| 335 | const now = audio.currentTime, gain = audio.createGain(), osc = audio.createOscillator(); | |
| 336 | const spec = { | |
| 337 | catch: [440, 620, .12, .07, 'sine'], | |
| 338 | gold: [660, 1100, .26, .07, 'sine'], | |
| 339 | crack: [220, 90, .2, .06, 'square'], | |
| 340 | }[kind]; | |
| 341 | osc.type = spec[4]; | |
| 342 | osc.frequency.setValueAtTime(spec[0], now); | |
| 343 | osc.frequency.exponentialRampToValueAtTime(spec[1], now + spec[2]); | |
| 344 | gain.gain.setValueAtTime(spec[3], now); | |
| 345 | gain.gain.exponentialRampToValueAtTime(.0005, now + spec[2]); | |
| 346 | osc.connect(gain).connect(audio.destination); | |
| 347 | osc.start(now); osc.stop(now + spec[2] + .01); | |
| 348 | } | |
| 349 | const muteButton = document.getElementById('mute'); | |
| 350 | muteButton.addEventListener('click', () => { | |
| 351 | muted = !muted; | |
| 352 | muteButton.textContent = muted ? 'sound off' : 'sound on'; | |
| 353 | muteButton.setAttribute('aria-pressed', String(muted)); | |
| 354 | }); | |
| 355 | ||
| 356 | /* ---------- fit and boot ---------- */ | |
| 357 | addEventListener('resize', fit); | |
| 358 | fit(); | |
| 359 | bake(); | |
| 360 | note(HINT); | |
| 361 | renderHud(); | |
| 362 | canvas.focus({ preventScroll: true }); | |
| 363 | let last = performance.now(); | |
| 364 | function frame(now) { | |
| 365 | update(Math.min(.04, (now - last) / 1000)); | |
| 366 | last = now; | |
| 367 | draw(); | |
| 368 | requestAnimationFrame(frame); | |
| 369 | } | |
| 370 | requestAnimationFrame(frame); | |
| 371 | </script> | |
| 372 | </body> | |
| 373 | </html> |
egg-basket/page.test.mjs added +52
| @@ -0,0 +1,52 @@ | ||
| 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('R'); | |
| 6 | ||
| 7 | test('the page shows points, cracks left and best, and the keys move the basket one lane', () => { | |
| 8 | const g = boot('egg-basket'); | |
| 9 | g.run(.5); | |
| 10 | expect(String(g.el('caught').textContent)).toBe('0'); | |
| 11 | expect(String(g.el('cracks').textContent)).toBe('3'); | |
| 12 | expect(String(g.el('best').textContent)).toBe('0'); | |
| 13 | const from = state(g).basket; | |
| 14 | g.press('ArrowRight', .3); | |
| 15 | expect(state(g).basket).toBe(from + 1); | |
| 16 | g.press('ArrowLeft', .3); g.press('ArrowLeft', .3); | |
| 17 | expect(state(g).basket).toBe(from - 1); | |
| 18 | expect(g.errors).toEqual([]); | |
| 19 | }); | |
| 20 | ||
| 21 | test('dragging the basket across the yard puts it under the pointer', () => { | |
| 22 | const g = boot('egg-basket'); | |
| 23 | const [lx, sy] = g.read('(() => { const p = project(5.7, -laneA(3), 0); return [p.sx, p.sy]; })()'); | |
| 24 | g.drag([lx - 60, sy], [lx, sy], .6); | |
| 25 | expect(state(g).basket).toBeCloseTo(3, 1); | |
| 26 | expect(g.errors).toEqual([]); | |
| 27 | }); | |
| 28 | ||
| 29 | test('the page has a mute button, says how to play on the canvas and ships no track', () => { | |
| 30 | const html = readFileSync(new URL('./index.html', import.meta.url), 'utf8'); | |
| 31 | expect(html).toContain('id="mute"'); | |
| 32 | expect(html).toMatch(/<canvas id="c"[^>]*aria-label="[^"]*drag[^"]*Left and Right/); | |
| 33 | expect(readdirSync(import.meta.dir).filter(f => f.endsWith('.mp3'))).toEqual([]); | |
| 34 | }); | |
| 35 | ||
| 36 | test('the rules popup is at most four lines', () => { | |
| 37 | const html = readFileSync(new URL('./index.html', import.meta.url), 'utf8'); | |
| 38 | const rules = html.match(/rules: \[([\s\S]*?)\n \]/)[1].split('\n').filter(l => l.trim()); | |
| 39 | expect(rules.length).toBeLessThanOrEqual(4); | |
| 40 | }); | |
| 41 | ||
| 42 | test('the roof count is shown outside the live region, so only notes are announced', () => { | |
| 43 | const g = boot('egg-basket'); | |
| 44 | g.run(2.6); | |
| 45 | expect(String(g.el('status').textContent)).toBe(''); | |
| 46 | expect(String(g.el('idle').textContent)).toMatch(/on the roof/); | |
| 47 | const html = readFileSync(new URL('./index.html', import.meta.url), 'utf8'); | |
| 48 | expect(html).toMatch(/id="status" role="status" aria-live="polite"/); | |
| 49 | expect(html).not.toMatch(/id="idle"[^>]*aria-live/); | |
| 50 | expect(html).toContain('<span>points</span>'); | |
| 51 | expect(g.errors).toEqual([]); | |
| 52 | }); |
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="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> | |
| 97 | 98 | <li data-kind="Strategy" data-input="Tap" style="--accent:#3a5fa8"><a href="empire/">Empire Age</a><span>A real-time strategy game against a bot: gather, build, climb four ages, train an army and destroy the enemy town center</span><span>Game forged by the crew</span><span class="tags">Strategy · Tap</span></li> |
| 98 | 99 | <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> |
| 99 | 100 | </ul> |
og/egg-basket.png added
Binary file not shown.
og/home.png
Binary file not shown.
rounds.mjs +9
| @@ -18,6 +18,15 @@ 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 | // stand under the egg that drops first until 25 are in the basket, then keep clear of each until three crack | |
| 22 | 'egg-basket': { over: /play again/, outcome: /the basket is full · \d+ points/, beat: g => { | |
| 23 | const lane = g.read('R.eggs.length ? R.eggs[0].lane : -1'); | |
| 24 | if (lane >= 0) { | |
| 25 | const want = g.read('R.score') < 25 ? lane : lane < 2 ? 3 : 0; | |
| 26 | for (let n = 0; n < 4 && Math.round(g.read('R.target')) !== want; n++) { const k = want < g.read('R.target') ? 'ArrowLeft' : 'ArrowRight'; g.keyDown(k); g.keyUp(k); } | |
| 27 | } | |
| 28 | g.step(1); | |
| 29 | } }, | |
| 21 | 30 | // pour while the cup is under the spout, and let go in time for the stream's tail to land on the line |
| 22 | 31 | // build nothing, march nowhere, and give up: the second press of R confirms |
| 23 | 32 | 'empire': { over: /victory|defeat/, outcome: /defeat · you gave up at \d+:\d\d · \d+ trained, \d+ defeated/, beat: g => { g.drag([140, 120], [220, 200], .4); g.press('r'); g.run(.5); g.press('r'); g.run(.5); } }, |
serve.test.mjs +8 −1
| @@ -248,7 +248,7 @@ test('every committed card is what the games render now, and capture is determin | ||
| 248 | 248 | expect(card(frame).equals(readFileSync(new URL(`./og/${g.id}.png`, import.meta.url)))).toBe(true); |
| 249 | 249 | } |
| 250 | 250 | expect(collage(frames).equals(readFileSync(new URL('./og/home.png', import.meta.url)))).toBe(true); |
| 251 | }, 60000); | |
| 251 | }, 240000); // captures every game in a software renderer, about a second and a half each: it grows with the catalogue | |
| 252 | 252 | |
| 253 | 253 | test('a card is not blank: the scene is captured from the game, not a placeholder', async () => { |
| 254 | 254 | const { capture } = await import('./scripts/og.mjs'); |
| @@ -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('Empire Age and each of its scripts are served without a track', async () => { |
| 427 | 434 | const page = await fetch(base + '/empire/'); |
| 428 | 435 | 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: '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 }, | |
| 41 | 42 | { id: 'empire', title: 'Empire Age', blurb: 'A real-time strategy game against a bot: gather, build, climb four ages, train an army and destroy the enemy town center', kind: 'Strategy', input: 'Tap', accent: '#3a5fa8', music: false, forged: true }, |
| 42 | 43 | { 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 }, |
| 43 | 44 | ]; |
new above
offline