Public: anyone can read this; the crew can change it.
Migrated from github.com/amirmushichge/pixel-plays
Add Seed Packets: turn over the packets on the potting bench and match every pair before the gardener comes back #136
Check · Review · Land · Live done — Landed and live in production.
vesper/pixel-plays:vesper/95-add-seed-packets-turn-over-the-packets-o → main 5 commits fork 2 threads · 2 unresolved
10 files
changed +643
−2
README.md +3 −2
| @@ -48,6 +48,7 @@ Concept by [abhi](https://x.com/awwbhi2). Game by [AmirMushich](https://x.com/Am | ||
| 48 | 48 | | [](coconut-shy/) | A village-fete stall with bunting and a stallholder, five coconuts on wooden posts: drag back and release to throw (or Left and Right to aim, hold Space for power, release to throw); a ball inside the square window knocks a coconut off its post, two glancing hits knock it too, and clearing all five wins a bonus ball; five balls a round, three rounds with the posts moved further back each time, and nine down or more of the game wins (forged by the crew on the engine and shell above, not by the original creators; rules in `coconut-shy/shy.js`) | Drag back and release; Left and Right to aim, hold Space | |
| 49 | 49 | | [](bowls-green/) | A flat lawn bowls green floating on white, a white jack at the far end, a mat at the near end, two rival woods already resting near the jack and a low hedge behind: drag back from the mat and release to roll (or Left and Right to set the line, hold Space to build weight, release to roll); weight sets how far the wood runs (quadratic in weight, against a pace that is one seeded constant per game) and each wood bends towards its bias side as it slows, so aiming straight at the jack misses; a wood off the back of the green (or into the ditch at the side) is dead; one end of four woods, and one or more resting nearer the jack than the nearest rival wood wins (forged by the crew on the engine and shell above, not by the original creators; rules in `bowls-green/bowls.js`) | Drag back and release; Left and Right to aim, hold Space | |
| 50 | 50 | | [](dinghy-race/) | A bay with a square course of three buoys and back over a start line, the wind from the south-east and shifting a little at each buoy: the boat steers itself buoy to buoy and you trim the sail; it drives best at half the angle between boat and wind, and loses speed quadratically as the trim drifts from that, so every leg and every wind shift asks for a new setting; two seeded rivals sail at a steady pace, first or second place is a win (forged by the crew on the engine and shell above, not by the original creators; rules in `dinghy-race/dinghy.js`) | Drag left and right along the sheet; Up and Down arrows trim in and out | |
| 51 | | [](seed-packets/) | A potting-shed bench with sixteen face-down seed packets in a 4x4 grid, a watering can and a clock on the wall: tap a packet to turn it face up to show its flower, then a second; a matching pair stays face up and a mismatch turns both back after 0.8 s (taps in that beat are ignored); the layout is a seeded shuffle of eight pairs and the gardener returns after 16 turns (a turn is two packets), so you can afford eight mismatches; the end shows pairs found and turns used, and the best is the fewest turns in a win (forged by the crew on the engine and shell above, not by the original creators) | Tap; arrows + space | | |
| 51 | 52 | | [](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 | |
| 52 | 53 | | [](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 | |
| 53 | 54 | | [](go-kart/) | Three races of three laps round a village track against three rivals: hold to drive, let go to brake, and a yellow board before each bend shows the fastest speed to enter it at; go in faster and you sit a second in the hay bales; first scores 3, second 2, third 1, and 6 points wins (forged by the crew on the engine and shell above, not by the original creators) | Hold; space | |
| @@ -71,7 +72,7 @@ On the forge, `forge.json` runs `bun serve.mjs` with health check `/`. State is | ||
| 71 | 72 | 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. |
| 72 | 73 | |
| 73 | 74 | ## Add a game |
| 74 | 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, Cloakroom, Go-Kart Fete, Empire Age, Egg Basket, Dinghy Race, Coconut Shy and Bowls Green 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`. | |
| 75 | 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, Cloakroom, Go-Kart Fete, Empire Age, Egg Basket, Dinghy Race, Coconut Shy, Bowls Green and Seed Packets 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`. | |
| 75 | 76 | |
| 76 | 77 | - `town.js` is the town's layout, pure data with no drawing: `window.PixelTown.layout(GAMES)` gives one isometric building per game (plot, screen position under the fixed 30-degree camera, accent, id) with trees in the spare plots, and `pick(town, x, y, filter)` gives the id of the front-most building under a screen point, or null. Nothing loads it yet; the drawing and the page are the second half of #99. |
| 77 | 78 | - `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. |
| @@ -88,7 +89,7 @@ Create `<id>/index.html`, load `../shell.js`, call `PixelPlays.mount({ id, rules | ||
| 88 | 89 | - Depth-buffer edges: darker where an object sits in front, a warm rim on upper-left edges. |
| 89 | 90 | - Large flat surfaces use closely spaced tones; wide steps make the dither read as a checkerboard. |
| 90 | 91 | - Characters stay sharp: flat bands with no dither, a dark silhouette on their own pixels, patterns at least 3px wide. |
| 91 | - 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, Cloakroom's coats sway and the light under its door moves, Go-Kart Fete's chequered flags flutter and its spectators wave, Egg Basket's hens peck and the straw in its basket stirs (its rolling eggs are play, not ambience, and still move), and Dinghy Race's water ripples come and go and its start-line flags flutter (the boats are play, and still sail), and Coconut Shy's bunting flutters and its stallholder shifts and waves (the thrown ball and wobbling coconuts are play, and still move), and Bowls Green's hedge leaves stir and a cloud shadow drifts across its green (the rolling woods are play, and still move); all of it stops under `prefers-reduced-motion`. | |
| 92 | - 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, Cloakroom's coats sway and the light under its door moves, Go-Kart Fete's chequered flags flutter and its spectators wave, Egg Basket's hens peck and the straw in its basket stirs (its rolling eggs are play, not ambience, and still move), and Dinghy Race's water ripples come and go and its start-line flags flutter (the boats are play, and still sail), and Coconut Shy's bunting flutters and its stallholder shifts and waves (the thrown ball and wobbling coconuts are play, and still move), and Bowls Green's hedge leaves stir and a cloud shadow drifts across its green (the rolling woods are play, and still move), and Seed Packets' wall clock ticks its second hand and its watering can drips; all of it stops under `prefers-reduced-motion`. | |
| 92 | 93 | |
| 93 | 94 | ## Empire Age |
| 94 | 95 |
index.html +1
| @@ -98,6 +98,7 @@ footer a::after { content: none; } | ||
| 98 | 98 | <li data-kind="Throwing" data-input="Drag" style="--accent:#7a4a2a"><a href="coconut-shy/">Coconut Shy</a><span>Drag and release to knock the coconuts off their posts, five balls a round</span><span>Game forged by the crew</span><span class="tags">Throwing · Drag</span></li> |
| 99 | 99 | <li data-kind="Sport" data-input="Drag" style="--accent:#4f7f3f"><a href="bowls-green/">Bowls Green</a><span>Drag back to set line and weight and curve your woods in close to the jack</span><span>Game forged by the crew</span><span class="tags">Sport · Drag</span></li> |
| 100 | 100 | <li data-kind="Racing" data-input="Drag" style="--accent:#2e8a7a"><a href="dinghy-race/">Dinghy Race</a><span>Drag to trim the sail to the wind and round three buoys ahead of the fleet</span><span>Game forged by the crew</span><span class="tags">Racing · Drag</span></li> |
| 101 | <li data-kind="Memory" data-input="Tap" style="--accent:#7d9a4a"><a href="seed-packets/">Seed Packets</a><span>Tap to turn over seed packets and match every pair before the gardener comes back</span><span>Game forged by the crew</span><span class="tags">Memory · Tap</span></li> | |
| 101 | 102 | <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> |
| 102 | 103 | <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> |
| 103 | 104 | <li data-kind="Racing" data-input="Hold" style="--accent:#e0a526"><a href="go-kart/">Go-Kart Fete</a><span>Hold to race, let go for the bends: three laps round the hay bales against three rivals</span><span>Game forged by the crew</span><span class="tags">Racing · Hold</span></li> |
og/home.png
Binary file not shown.
og/seed-packets.png added
Binary file not shown.
rounds.mjs +6
| @@ -95,6 +95,12 @@ export const ROUNDS = { | ||
| 95 | 95 | 'pizza-toss': { over: /play again/, outcome: /\d+ px across/, beat: g => { g.hold(...MID, 1); g.run(1); g.tap(...MID); } }, |
| 96 | 96 | 'pottery-wheel': { over: /new round/, outcome: /\d+ px in total/, beat: g => { g.hold(...MID, 2); g.drag([180, 200], [180, 60], 1); g.tap(...MID); } }, |
| 97 | 97 | 'row-home': { over: /row again/, outcome: /[\d.]+s · (new best|best)|you won/, beat: tapping(.8) }, |
| 98 | // know the layout: turn each flower's two packets in turn until the bench is full | |
| 99 | 'seed-packets': { over: /play again/, outcome: /full bench · 8 of 8 pairs · \d+ turns used/, beat: g => { | |
| 100 | const pair = g.read('(() => { const d = R.bench.filter(p => p.state === "down"); if (R.phase !== "play" || !d.length) return null; const m = d.find(p => p !== d[0] && p.flower === d[0].flower); return [centreOf(d[0].id), centreOf(m.id)]; })()'); | |
| 101 | if (pair) { g.tap(...pair[0]); g.tap(...pair[1]); } | |
| 102 | g.run(.9); | |
| 103 | } }, | |
| 98 | 104 | // herd by pointer: hold the dog 3 units beyond the sheep furthest from its goal (the gate, then the pen) |
| 99 | 105 | 'sheepdog': { over: /play again/, outcome: /the flock is home · \d+ of 15/, beat: (() => { |
| 100 | 106 | let down = false; |
seed-packets/index.html added +382
| @@ -0,0 +1,382 @@ | ||
| 1 | ||
| 2 | <html lang="en"> | |
| 3 | <head> | |
| 4 | <meta charset="utf-8"> | |
| 5 | <meta name="viewport" content="width=device-width,initial-scale=1,viewport-fit=cover"> | |
| 6 | <title>Seed Packets</title> | |
| 7 | <!-- og:begin --> | |
| 8 | <meta name="description" content="Tap to turn over seed packets and match every pair before the gardener comes back"> | |
| 9 | <link rel="canonical" href="https://pixel-plays--forge-trevato-apps.open-platform.sh/seed-packets/"> | |
| 10 | <meta property="og:type" content="website"> | |
| 11 | <meta property="og:site_name" content="Pixel Plays"> | |
| 12 | <meta property="og:title" content="Seed Packets"> | |
| 13 | <meta property="og:description" content="Tap to turn over seed packets and match every pair before the gardener comes back"> | |
| 14 | <meta property="og:url" content="https://pixel-plays--forge-trevato-apps.open-platform.sh/seed-packets/"> | |
| 15 | <meta property="og:image" content="https://pixel-plays--forge-trevato-apps.open-platform.sh/og/seed-packets.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="Seed Packets: 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; : #7d9a4a; } | |
| 24 | * { box-sizing: border-box; } | |
| 25 | html, body { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent; } | |
| 26 | html, body { height: 100%; margin: 0; background: var(); color: var(); } | |
| 27 | body { font: 13px/1.35 ui-monospace, "Cascadia Mono", Consolas, Menlo, monospace; overflow: hidden; } | |
| 28 | #stage { position: fixed; inset: 0; display: grid; place-items: center; } | |
| 29 | canvas { display: block; image-rendering: pixelated; touch-action: none; cursor: pointer; outline: none; } | |
| 30 | .hud { position: fixed; left: max(16px, env(safe-area-inset-left)); right: max(16px, env(safe-area-inset-right)); display: flex; justify-content: space-between; gap: 16px; pointer-events: none; } | |
| 31 | #scores { position: fixed; top: max(14px, env(safe-area-inset-top)); left: max(16px, env(safe-area-inset-left)); display: grid; gap: 2px; pointer-events: none; } | |
| 32 | #bottom { bottom: max(14px, env(safe-area-inset-bottom)); align-items: flex-end; } | |
| 33 | .row { display: flex; align-items: center; gap: 8px; color: var(); } | |
| 34 | .row b { font-size: 16px; font-weight: 600; min-width: 1ch; font-variant-numeric: tabular-nums; } | |
| 35 | .row span { color: var(); } | |
| 36 | #status { color: var(); min-height: 1.35em; } | |
| 37 | button { pointer-events: auto; font: inherit; color: var(); background: none; border: 0; padding: 10px; margin: -10px; cursor: pointer; } | |
| 38 | button:hover, button:focus-visible { color: var(); } | |
| 39 | </style> | |
| 40 | </head> | |
| 41 | <body> | |
| 42 | <div id="stage"><canvas id="c" tabindex="0" aria-label="A potting-shed bench with sixteen face-down seed packets in a four by four grid, a watering can and a clock on the wall. Tap a packet to turn it face up and show its flower, then tap a second: a matching pair stays face up, a mismatch turns back. Or move the focus ring with the arrow keys and press Space or Enter to turn a packet. Match all eight pairs within 16 turns before the gardener comes back."></canvas></div> | |
| 43 | <div id="scores"> | |
| 44 | <div class="row"><b id="found">0/8</b><span>pairs found</span></div> | |
| 45 | <div class="row"><b id="left">16</b><span>turns left</span></div> | |
| 46 | <div class="row"><b id="best">-</b><span>best</span></div> | |
| 47 | </div> | |
| 48 | <div class="hud" id="bottom"> | |
| 49 | <div id="status" role="status" aria-live="polite"></div> | |
| 50 | <button id="mute" type="button" aria-pressed="false">sound on</button> | |
| 51 | </div> | |
| 52 | <script src="../shell.js"></script> | |
| 53 | <script> | |
| 54 | PixelPlays.mount({ | |
| 55 | id: 'seed-packets', | |
| 56 | rules: [ | |
| 57 | 'Tap a packet, or move the ring with the arrows and press Space, to turn it over.', | |
| 58 | 'Turn two: a match stays up, a mismatch turns back after a beat.', | |
| 59 | 'Find all eight pairs in 16 turns before the gardener comes back.', | |
| 60 | ], | |
| 61 | }); | |
| 62 | </script> | |
| 63 | <script>window.PP_SCENE = { originY: 46, shadowX: 1.3, shadowY: -.12 };</script> | |
| 64 | <script src="../engine.js"></script> | |
| 65 | <script src="packets.js"></script> | |
| 66 | <script> | |
| 67 | 'use strict'; | |
| 68 | ||
| 69 | const BENCH = [168, 128, 84], BENCH_ALT = [158, 118, 76], FRAME = [110, 78, 52], WALL = [148, 128, 98], WALL_TOP = [104, 80, 56]; | |
| 70 | const KRAFT = [196, 152, 98], SEEDPAPER = [218, 230, 200], LEAF = [74, 150, 70], FOUND = [112, 156, 70]; | |
| 71 | const SECOND = [196, 56, 48], TIN = [96, 148, 120], DROP = [120, 176, 232], WET = [112, 80, 52]; | |
| 72 | const CLOCK_FACE = [248, 242, 226], BRASS = [196, 164, 80], CLOCK_INK = [46, 40, 36]; | |
| 73 | // the eight flowers: a colour, a petal count and a centre, told apart by eye by both | |
| 74 | const FLOWERS = [ | |
| 75 | { c: [214, 56, 48], n: 5, mid: [60, 40, 40] }, { c: [240, 196, 48], n: 8, mid: [110, 70, 36] }, | |
| 76 | { c: [70, 110, 210], n: 6, mid: [240, 232, 140] }, { c: [136, 84, 180], n: 5, mid: [240, 220, 120] }, | |
| 77 | { c: [236, 120, 170], n: 4, mid: [250, 226, 120] }, { c: [236, 138, 44], n: 10, mid: [120, 60, 30] }, | |
| 78 | { c: [244, 244, 236], n: 9, mid: [236, 184, 44] }, { c: [64, 176, 168], n: 7, mid: [250, 240, 190] }, | |
| 79 | ]; | |
| 80 | const NAMES = ['poppy', 'sunflower', 'cornflower', 'pansy', 'cosmos', 'marigold', 'daisy', 'forget-me-not']; | |
| 81 | const HALF_U = 16.5, HALF_V = 13.5, WALL_Z = 11, PU = 6.4, PV = 5.4, HU = 2.8, HV = 2.3, THICK = .3, LIFT = .15; | |
| 82 | const CLOCK = { u: 5, z: 7.2, r: 2.9 }; | |
| 83 | const CAN = { u: -13.2, v: -11.6 }; | |
| 84 | const SPOUT = { u: CAN.u + 3.4, v: CAN.v, z: 3.7 }; | |
| 85 | const BEST_KEY = 'pixel-plays-seed-packets-best'; | |
| 86 | const ACCENT_PIX = rgb(125, 154, 74), RING_PIX = rgb(255, 255, 255); | |
| 87 | ||
| 88 | const world = (u, v, z) => [(u + v) / R2, (v - u) / R2, z]; | |
| 89 | const slot = i => ({ col: i % 4, row: Math.floor(i / 4), u: ((i % 4) - 1.5) * PU, v: (Math.floor(i / 4) - 1.5) * PV }); | |
| 90 | ||
| 91 | /* ---------- state ---------- */ | |
| 92 | const best0 = (() => { try { const n = parseFloat(localStorage.getItem(BEST_KEY)); return Number.isFinite(n) && n >= 0 ? n : null; } catch (e) { return null; } })(); | |
| 93 | let R = Packets.newGame(best0, Date.now() & 0xffff); | |
| 94 | const G = { time: 0, status: '', noteUntil: 0, focus: 0, ring: false }; | |
| 95 | const nodes = {}; | |
| 96 | for (const k of ['found', 'left', 'best', 'status']) nodes[k] = document.getElementById(k); | |
| 97 | const shown = {}; | |
| 98 | function setText(key, text) { | |
| 99 | if (shown[key] === text) return; | |
| 100 | shown[key] = text; | |
| 101 | nodes[key].textContent = text; | |
| 102 | } | |
| 103 | function renderHud() { | |
| 104 | setText('found', `${R.found}/${Packets.PAIRS}`); | |
| 105 | setText('left', String(Packets.turnsLeft(R))); | |
| 106 | setText('best', R.best === null ? '-' : String(R.best)); | |
| 107 | } | |
| 108 | function note(text) { setText('status', text); G.noteUntil = G.time + 1.6; } | |
| 109 | ||
| 110 | /* ---------- painting on the bench ---------- */ | |
| 111 | // one point of a surface at (u, v, z), in the depth buffer | |
| 112 | function put(ramp, u, v, z, tone) { | |
| 113 | const p = project((u + v) / R2, (v - u) / R2, z), ix = p.sx | 0, iy = p.sy | 0; | |
| 114 | if (ix < 0 || iy < 0 || ix >= W || iy >= H) return; | |
| 115 | const i = iy * W + ix; | |
| 116 | if (p.d <= zb[i]) return; | |
| 117 | zb[i] = p.d; | |
| 118 | pix[i] = ramp[clamp(Math.floor(tone + (bayerAt(ix, iy) - .5) * .5), 0, 4)]; | |
| 119 | } | |
| 120 | // a flat rectangle facing up, and a strip facing the camera | |
| 121 | function top(ramp, u0, u1, v0, v1, z, tone) { | |
| 122 | for (let u = u0; u <= u1; u += .08) for (let v = v0; v <= v1; v += .08) put(ramp, u, v, z, typeof tone === 'function' ? tone(u, v) : tone); | |
| 123 | } | |
| 124 | function front(ramp, u0, u1, v, z0, z1, tone) { | |
| 125 | for (let u = u0; u <= u1; u += .08) for (let z = z0; z <= z1; z += .08) put(ramp, u, v, z, typeof tone === 'function' ? tone(u, z) : tone); | |
| 126 | } | |
| 127 | ||
| 128 | /* ---------- baked scene: the bench, the wall, the can, the clock, the packets face down ---------- */ | |
| 129 | function groundColor(x, y, ix, iy) { | |
| 130 | const u = (x - y) / R2, v = (x + y) / R2; | |
| 131 | if (Math.abs(u) > HALF_U || Math.abs(v) > HALF_V) return 0; | |
| 132 | if (Math.abs(u) > HALF_U - .5 || Math.abs(v) > HALF_V - .5) return rgb(...FRAME); | |
| 133 | const board = Math.floor((v + 40) / 3.4) & 1; | |
| 134 | const t = .95 + (hash(ix, iy) - .5) * .08 + (noise(u * .5, v * 2.4) - .5) * .1; | |
| 135 | const c = board ? BENCH : BENCH_ALT; | |
| 136 | return rgb(c[0] * t, c[1] * t, c[2] * t); | |
| 137 | } | |
| 138 | function bakeWall() { | |
| 139 | const wall = rampOf(WALL), cap = rampOf(WALL_TOP), apron = rampOf(FRAME), v = -HALF_V; | |
| 140 | front(wall, -HALF_U, HALF_U, v, 0, WALL_Z, (u, z) => 2.3 + (Math.floor((u + 40) / 2.2) & 1) * .5 - .3 + (hash(Math.floor(u * 5), Math.floor(z * 5)) - .5) * .25); | |
| 141 | top(cap, -HALF_U, HALF_U, v - .8, v, WALL_Z, 2.6); | |
| 142 | front(cap, -HALF_U, HALF_U, v, WALL_Z - .6, WALL_Z, 2); | |
| 143 | front(apron, -HALF_U, HALF_U, HALF_V, -1.6, 0, (u, z) => 1.8 + (Math.floor((u + 40) / 4.4) & 1) * .5 + z * .2); // the bench's front apron | |
| 144 | } | |
| 145 | function bakeClock() { | |
| 146 | const face = rampOf(CLOCK_FACE), rim = rampOf(BRASS), ink = rampOf(CLOCK_INK), v = -HALF_V + .15; | |
| 147 | for (let u = -CLOCK.r; u <= CLOCK.r; u += .05) for (let z = -CLOCK.r; z <= CLOCK.r; z += .05) { | |
| 148 | const d = Math.hypot(u, z); | |
| 149 | if (d > CLOCK.r) continue; | |
| 150 | put(d > CLOCK.r - .4 ? rim : face, CLOCK.u + u, v, CLOCK.z + z, d > CLOCK.r - .4 ? 2.6 + u * .08 : 3.2); | |
| 151 | } | |
| 152 | for (let k = 0; k < 12; k++) { | |
| 153 | const a = k * Math.PI / 6; | |
| 154 | for (let t = k % 3 ? 1.9 : 1.6; t <= 2.2; t += .05) put(ink, CLOCK.u + Math.sin(a) * t, v + .05, CLOCK.z + Math.cos(a) * t, 1); | |
| 155 | } | |
| 156 | for (const [len, a] of [[1.2, Math.PI * 5.7 / 6], [1.8, Math.PI * 2 / 6]]) { // hour and minute hands, at ten past ten | |
| 157 | for (let t = 0; t <= len; t += .05) for (let w = -.08; w <= .08; w += .04) put(ink, CLOCK.u + Math.sin(a) * t + Math.cos(a) * w, v + .06, CLOCK.z + Math.cos(a) * t - Math.sin(a) * w, 1); | |
| 158 | } | |
| 159 | for (let u = -.3; u <= .3; u += .05) for (let z = -.3; z <= .3; z += .05) if (Math.hypot(u, z) < .3) put(ink, CLOCK.u + u, v + .08, CLOCK.z + z, 1); | |
| 160 | } | |
| 161 | function bakeCan() { | |
| 162 | const at = (du, dz) => world(CAN.u + du, CAN.v, dz); | |
| 163 | capsule(at(0, .9), at(0, 2.7), 1.25, 1.25, () => TIN, 'solid'); | |
| 164 | sphere(...at(0, 2.85), 1.15, TIN, 'solid'); | |
| 165 | capsule(at(.9, 1.5), at(3.2, 3.5), .3, .22, () => TIN, 'solid'); // the spout | |
| 166 | sphere(...at(3.45, 3.7), .5, TIN, 'solid'); // the rose | |
| 167 | capsule(at(-1, 3), at(-2.2, 2.2), .17, .17, () => TIN, 'solid'); // the handle | |
| 168 | capsule(at(-2.2, 2.2), at(-1.2, .9), .17, .17, () => TIN, 'solid'); | |
| 169 | castShadow(...at(0, 1.8).slice(0, 2), 1.8, 1.25, 1, 0); | |
| 170 | castShadow(...at(2.2, 2.6).slice(0, 2), 2.6, .6, 1, 0); | |
| 171 | } | |
| 172 | function bakePackets() { | |
| 173 | const kraft = rampOf(KRAFT), seam = rampOf(FRAME); | |
| 174 | for (let i = 0; i < Packets.SIZE; i++) { | |
| 175 | const s = slot(i), u0 = s.u - HU, u1 = s.u + HU, v0 = s.v - HV, v1 = s.v + HV; | |
| 176 | top(kraft, u0, u1, v0, v1, THICK, (u, v) => { | |
| 177 | if (v < v0 + .8) return 1.7 + (Math.floor((u - u0) / .45) & 1) * .3; // the folded top flap | |
| 178 | if (Math.abs(u - s.u) < 1.3 && Math.abs(v - s.v - .3) < .9) return 3.5; // the printed label | |
| 179 | return 2.7 + (hash(Math.floor(u * 4), Math.floor(v * 4) + i) - .5) * .3; | |
| 180 | }); | |
| 181 | front(kraft, u0, u1, v1, 0, THICK, 2); | |
| 182 | top(seam, s.u - .3, s.u + .3, s.v + .15, s.v + .45, THICK + .01, 1.2); | |
| 183 | castShadow(...world(s.u, s.v, 0).slice(0, 2), THICK, 2.5, 1, 0); | |
| 184 | } | |
| 185 | } | |
| 186 | function bake() { | |
| 187 | ground.fill(0); shadowMask.fill(0); staticShadow.fill(0); kinds.fill(0); | |
| 188 | tufts.length = 0; leaves.length = 0; | |
| 189 | zb.fill(-1e9); | |
| 190 | pix.fill(rgb(255, 255, 255)); | |
| 191 | for (let iy = 0; iy < H; iy++) for (let ix = 0; ix < W; ix++) { | |
| 192 | const g = unproject(ix + .5, iy + .5), i = iy * W + ix, c = groundColor(g.x, g.y, ix, iy); | |
| 193 | if (!c) continue; | |
| 194 | pix[i] = c; ground[i] = 1; | |
| 195 | zb[i] = (g.x + g.y) / R2 * COS; | |
| 196 | } | |
| 197 | groundZ.set(zb); | |
| 198 | bakeWall(); | |
| 199 | bakeClock(); | |
| 200 | bakeCan(); | |
| 201 | bakePackets(); | |
| 202 | bakeOcclusion(); | |
| 203 | staticShadow.set(shadowMask); | |
| 204 | applyShadows(groundZ); | |
| 205 | bakedPix.set(pix); | |
| 206 | bakedZ.set(zb); | |
| 207 | } | |
| 208 | ||
| 209 | /* ---------- drawn each frame ---------- */ | |
| 210 | // a packet turned over: paper with its flower standing up out of it | |
| 211 | function drawUp(i) { | |
| 212 | const p = R.bench[i], s = slot(i), found = p.state === 'found', fl = FLOWERS[p.flower]; | |
| 213 | const paper = rampOf(SEEDPAPER), edge = rampOf(found ? FOUND : KRAFT), z = THICK + LIFT; | |
| 214 | const u0 = s.u - HU, u1 = s.u + HU, v0 = s.v - HV, v1 = s.v + HV; | |
| 215 | top(edge, u0, u1, v0, v1, z, 2.5); | |
| 216 | top(paper, u0 + .35, u1 - .35, v0 + .35, v1 - .35, z + .01, 3.1); | |
| 217 | front(edge, u0, u1, v1, 0, z, 2); | |
| 218 | const at = (du, dz) => world(s.u + du, s.v, dz); | |
| 219 | const stem = at(0, z), head = at(0, 3.4); | |
| 220 | capsule(stem, head, .13, .1, () => LEAF, 'solid'); | |
| 221 | sphere(...at(.55, 1.4), .32, LEAF, 'solid'); sphere(...at(-.55, 1.9), .3, LEAF, 'solid'); | |
| 222 | const ring = 1.15, pr = clamp(ring * Math.sin(Math.PI / fl.n) * 1.25, .34, .62); | |
| 223 | for (let k = 0; k < fl.n; k++) { | |
| 224 | const a = k / fl.n * Math.PI * 2 + (fl.n % 2 ? 0 : Math.PI / fl.n); | |
| 225 | sphere(...at(Math.sin(a) * ring, 3.4 + Math.cos(a) * ring * .95), pr, fl.c, 'solid'); | |
| 226 | } | |
| 227 | sphere(...world(s.u, s.v + .4, 3.4), .52, fl.mid, 'solid'); | |
| 228 | pendingShadows.push(stem, .12, head, .12, at(0, 3.4), 1.2, at(0, 3.4), 1.2); | |
| 229 | } | |
| 230 | // the second hand ticks once a second; the drip swells, falls, splashes and rests, in 8 fps steps | |
| 231 | function drawAmbient() { | |
| 232 | const sec = STILL ? 0 : Math.floor(G.time) % 60, a = sec / 60 * Math.PI * 2, v = -HALF_V + .3; | |
| 233 | const tip = world(CLOCK.u + Math.sin(a) * 2.2, v, CLOCK.z + Math.cos(a) * 2.2), tail = world(CLOCK.u - Math.sin(a) * .5, v, CLOCK.z - Math.cos(a) * .5); | |
| 234 | capsule(tail, tip, .07, .05, () => SECOND, 'solid'); | |
| 235 | const k = STILL ? 0 : Math.floor(G.time * WIND_FPS) % 16; | |
| 236 | const drop = (z, r) => sphere(...world(SPOUT.u + .1, SPOUT.v, z), r, DROP, 'gloss'); | |
| 237 | if (k < 4) drop(SPOUT.z - .3, .1 + k * .035); | |
| 238 | else if (k < 10) { const t = (k - 3) / 6; drop(SPOUT.z - .3 - 3.3 * t * t, .2); } | |
| 239 | else if (k < 14) sphere(...world(SPOUT.u + .1, SPOUT.v, .08), .35 + (k - 10) * .05, WET, 'solid'); | |
| 240 | } | |
| 241 | function drawRing() { | |
| 242 | if (!G.ring || R.over) return; | |
| 243 | const s = slot(G.focus), a = project(...world(s.u - HU, s.v - HV, THICK)), b = project(...world(s.u + HU, s.v + HV, THICK)); | |
| 244 | const x0 = Math.round(a.sx) - 2, x1 = Math.round(b.sx) + 2, y0 = Math.round(a.sy) - 2, y1 = Math.round(b.sy) + 2; | |
| 245 | const dot = (x, y, c) => { if (x >= 0 && x < W && y >= 0 && y < H) pix[y * W + x] = c; }; | |
| 246 | for (let x = x0 - 1; x <= x1 + 1; x++) for (const y of [y0 - 1, y1 + 1]) dot(x, y, RING_PIX); | |
| 247 | for (let y = y0 - 1; y <= y1 + 1; y++) for (const x of [x0 - 1, x1 + 1]) dot(x, y, RING_PIX); | |
| 248 | for (let x = x0; x <= x1; x++) { dot(x, y0, ACCENT_PIX); dot(x, y1, ACCENT_PIX); } | |
| 249 | for (let y = y0; y <= y1; y++) { dot(x0, y, ACCENT_PIX); dot(x1, y, ACCENT_PIX); } | |
| 250 | } | |
| 251 | function draw() { | |
| 252 | pix.set(bakedPix); | |
| 253 | zb.set(bakedZ); | |
| 254 | pendingShadows.length = 0; | |
| 255 | for (let i = 0; i < R.bench.length; i++) if (R.bench[i].state !== 'down') drawUp(i); | |
| 256 | drawAmbient(); | |
| 257 | flushShadows(); | |
| 258 | applyShadows(groundZ); | |
| 259 | shadeEdges(); | |
| 260 | drawRing(); | |
| 261 | ctx.putImageData(image, 0, 0); | |
| 262 | } | |
| 263 | ||
| 264 | /* ---------- game ---------- */ | |
| 265 | // what a screen reader hears for a slot: where it is, and its flower when it is face up | |
| 266 | const slotName = i => `row ${(i >> 2) + 1} column ${(i & 3) + 1}`; | |
| 267 | const describe = i => R.bench[i].state === 'down' ? `${slotName(i)}, face down` : `${slotName(i)}, ${NAMES[R.bench[i].flower]}`; | |
| 268 | const HINT = 'turn two packets to find a pair'; | |
| 269 | function act(events) { | |
| 270 | for (const e of events) { | |
| 271 | if (e.type === 'turn') { sound(300 + R.bench[e.id].flower * 40, .08, 'triangle'); note(`${slotName(e.id)}: ${NAMES[R.bench[e.id].flower]}`); } | |
| 272 | if (e.type === 'match') { sound(660 + e.flower * 40, .25, 'sine'); sound(990 + e.flower * 60, .35, 'sine', .08); note(`a pair of ${NAMES[e.flower]}, planted`); } | |
| 273 | if (e.type === 'mismatch') { sound(140, .3, 'sawtooth'); note(`${NAMES[R.bench[e.ids[0]].flower]} and ${NAMES[R.bench[e.ids[1]].flower]}: no match`); } | |
| 274 | if (e.type === 'won' || e.type === 'lost') { | |
| 275 | if (e.type === 'won') { sound(520, .3, 'sine'); sound(780, .5, 'sine', .15); } else sound(110, .6, 'sawtooth'); | |
| 276 | if (R.record) try { localStorage.setItem(BEST_KEY, String(R.best)); } catch (err) { /* private window: keep it for this visit */ } | |
| 277 | const head = R.won ? 'the gardener finds a full bench' : 'the gardener is back'; | |
| 278 | const tail = R.won ? ` · ${R.record ? 'new best' : `best ${R.best}`}` : ''; | |
| 279 | setText('status', `${head} · ${R.found} of ${Packets.PAIRS} pairs · ${R.turns} turns used${tail} · tap or press a key to play again`); | |
| 280 | G.noteUntil = 0; | |
| 281 | } | |
| 282 | } | |
| 283 | renderHud(); | |
| 284 | } | |
| 285 | function turnPacket(id) { | |
| 286 | ensureAudio(); | |
| 287 | if (R.over) { | |
| 288 | const next = Packets.again(R); | |
| 289 | if (next) { R = next; G.focus = 0; G.ring = false; note(HINT); renderHud(); } | |
| 290 | return; | |
| 291 | } | |
| 292 | act(Packets.turn(R, id)); | |
| 293 | } | |
| 294 | function update(dt) { | |
| 295 | G.time += dt; | |
| 296 | act(Packets.advance(R, dt)); | |
| 297 | if (R.over || G.time < G.noteUntil) return; | |
| 298 | setText('status', `${R.found} of ${Packets.PAIRS} pairs · ${Packets.turnsLeft(R)} turns left`); | |
| 299 | } | |
| 300 | ||
| 301 | /* ---------- input ---------- */ | |
| 302 | // where a packet's top is on the canvas, for a tap (and for the playtest) | |
| 303 | const centreOf = i => { const s = slot(i), p = project(...world(s.u, s.v, THICK)); return [p.sx, p.sy]; }; | |
| 304 | // the packet under the point, or -1; a flower standing up counts as part of its packet | |
| 305 | function packetAt(px, py) { | |
| 306 | for (const lift of [0, 2.4]) { | |
| 307 | const g = unproject(px, py + lift * S * COS), u = (g.x - g.y) / R2, v = (g.x + g.y) / R2; | |
| 308 | const col = Math.floor(u / PU + 2), row = Math.floor(v / PV + 2); | |
| 309 | if (col >= 0 && col < 4 && row >= 0 && row < 4 && Math.abs(u - slot(row * 4 + col).u) <= HU + .4 && Math.abs(v - slot(row * 4 + col).v) <= HV + .4) return row * 4 + col; | |
| 310 | } | |
| 311 | return -1; | |
| 312 | } | |
| 313 | canvas.addEventListener('pointerdown', e => { | |
| 314 | canvas.focus({ preventScroll: true }); | |
| 315 | G.ring = false; | |
| 316 | const r = canvas.getBoundingClientRect(); | |
| 317 | const id = packetAt((e.clientX - r.left) / r.width * W, (e.clientY - r.top) / r.height * H); | |
| 318 | if (id >= 0 || R.over) { if (id >= 0) G.focus = id; turnPacket(id); } | |
| 319 | }); | |
| 320 | canvas.addEventListener('focus', () => { if (canvas.matches(':focus-visible')) G.ring = true; }); | |
| 321 | const MOVES = { ArrowLeft: [-1, 0], ArrowRight: [1, 0], ArrowUp: [0, -1], ArrowDown: [0, 1] }; | |
| 322 | canvas.addEventListener('keydown', e => { | |
| 323 | const move = MOVES[e.key]; | |
| 324 | if (!move && e.key !== ' ' && e.key !== 'Enter') return; | |
| 325 | e.preventDefault(); | |
| 326 | if (e.repeat) return; | |
| 327 | if (R.over) { turnPacket(-1); return; } | |
| 328 | const hidden = !G.ring; | |
| 329 | G.ring = true; | |
| 330 | if (move) { // an arrow moves at once, even when it is the key that shows the ring | |
| 331 | const s = slot(G.focus); | |
| 332 | G.focus = clamp(s.row + move[1], 0, 3) * 4 + clamp(s.col + move[0], 0, 3); | |
| 333 | note(describe(G.focus)); | |
| 334 | return; | |
| 335 | } | |
| 336 | if (hidden) { note(describe(G.focus)); return; } // Space or Enter on a hidden ring only shows where it is | |
| 337 | turnPacket(G.focus); | |
| 338 | }); | |
| 339 | ||
| 340 | /* ---------- sound ---------- */ | |
| 341 | let audio = null, muted = false; | |
| 342 | function ensureAudio() { | |
| 343 | if (audio) { if (audio.state === 'suspended') audio.resume(); return; } | |
| 344 | const AC = window.AudioContext || window.webkitAudioContext; | |
| 345 | if (AC) audio = new AC(); | |
| 346 | } | |
| 347 | function sound(freq, life, type, delay = 0) { | |
| 348 | if (!audio || muted) return; | |
| 349 | const now = audio.currentTime + delay, gain = audio.createGain(), osc = audio.createOscillator(); | |
| 350 | osc.type = type; | |
| 351 | osc.frequency.setValueAtTime(freq, now); | |
| 352 | gain.gain.setValueAtTime(.0001, now); | |
| 353 | gain.gain.exponentialRampToValueAtTime(.07, now + .01); | |
| 354 | gain.gain.exponentialRampToValueAtTime(.0005, now + life); | |
| 355 | osc.connect(gain).connect(audio.destination); | |
| 356 | osc.start(now); osc.stop(now + life + .02); | |
| 357 | } | |
| 358 | const muteButton = document.getElementById('mute'); | |
| 359 | muteButton.addEventListener('click', () => { | |
| 360 | muted = !muted; | |
| 361 | muteButton.textContent = muted ? 'sound off' : 'sound on'; | |
| 362 | muteButton.setAttribute('aria-pressed', String(muted)); | |
| 363 | }); | |
| 364 | ||
| 365 | /* ---------- fit and boot ---------- */ | |
| 366 | addEventListener('resize', fit); | |
| 367 | fit(); | |
| 368 | bake(); | |
| 369 | note(HINT); | |
| 370 | renderHud(); | |
| 371 | canvas.focus({ preventScroll: true }); | |
| 372 | let last = performance.now(); | |
| 373 | function frame(now) { | |
| 374 | update(Math.min(.04, (now - last) / 1000)); | |
| 375 | last = now; | |
| 376 | draw(); | |
| 377 | requestAnimationFrame(frame); | |
| 378 | } | |
| 379 | requestAnimationFrame(frame); | |
| 380 | </script> | |
| 381 | </body> | |
| 382 | </html> |
seed-packets/packets.js added +88
| @@ -0,0 +1,88 @@ | ||
| 1 | 'use strict'; | |
| 2 | // The seed-packet rules: sixteen packets face down on a potting bench (a 4x4 grid), turned two at a time to find | |
| 3 | // the eight pairs of flowers. The gardener comes back after 16 turns (a turn is two packets turned). Plain data in, | |
| 4 | // plain data out, no DOM, no timers; the mismatch beat is a state (`phase: 'beat'`) that `advance` runs down, and | |
| 5 | // randomness comes from the seed passed in. The page draws what this decides. | |
| 6 | (function (root) { | |
| 7 | const PAIRS = 8, SIZE = PAIRS * 2, TURNS = 16; | |
| 8 | const BEAT = .8; // seconds a mismatched pair stays face up before it turns back | |
| 9 | const RESTART_AFTER = .6; | |
| 10 | ||
| 11 | const finite = v => (Number.isFinite(v) ? v : 0); | |
| 12 | // a repeatable number in [0, 1) for this seed and slot | |
| 13 | function noise(seed, n) { | |
| 14 | let h = (Math.imul(finite(seed) | 0, 374761393) + Math.imul(n | 0, 668265263)) | 0; | |
| 15 | h = Math.imul(h ^ (h >>> 13), 1274126177); | |
| 16 | return ((h ^ (h >>> 16)) >>> 0) / 4294967296; | |
| 17 | } | |
| 18 | // a bench for this seed: each flower 0..7 twice, shuffled. A packet is { id, flower, state: 'down' | 'up' | 'found' } | |
| 19 | function deal(seed = 0) { | |
| 20 | const flowers = []; | |
| 21 | for (let f = 0; f < PAIRS; f++) flowers.push(f, f); | |
| 22 | for (let i = flowers.length - 1; i > 0; i--) { | |
| 23 | const j = Math.floor(noise(seed, i) * (i + 1)); | |
| 24 | [flowers[i], flowers[j]] = [flowers[j], flowers[i]]; | |
| 25 | } | |
| 26 | return flowers.map((flower, id) => ({ id, flower, state: 'down' })); | |
| 27 | } | |
| 28 | function newGame(best = null, seed = 0) { | |
| 29 | seed = finite(seed) >>> 0; | |
| 30 | return { | |
| 31 | phase: 'play', over: false, won: false, outcome: '', seed, bench: deal(seed), up: [], found: 0, turns: 0, beat: 0, timer: 0, | |
| 32 | best: typeof best === 'number' && Number.isFinite(best) && best >= 0 ? best : null, record: false, | |
| 33 | }; | |
| 34 | } | |
| 35 | const turnsLeft = s => TURNS - s.turns; | |
| 36 | function finish(s, won, events) { | |
finish() turns the shown packets face down immediately. On a losing last mismatch the player never sees the final pair. Let the beat run before the end (or reveal the bench), and keep a test for it. |
||
| 37 | s.phase = 'over'; s.over = true; s.won = won; s.timer = 0; s.beat = 0; | |
| 38 | s.outcome = won ? 'won' : 'lost'; | |
| 39 | for (const i of s.up) s.bench[i].state = 'down'; // packets still shown turn back, so the bench and `up` agree | |
| 40 | s.up = []; | |
| 41 | if (won && (s.best === null || s.turns < s.best)) { s.best = s.turns; s.record = true; } | |
| 42 | events.push({ type: s.outcome }); | |
| 43 | } | |
| 44 | // turn the packet with this id; returns the events it caused. Ignored when over, during the beat, or for a packet not face down. | |
| 45 | function turn(s, id) { | |
| 46 | const events = []; | |
| 47 | if (s.over || s.phase !== 'play' || !Number.isInteger(id)) return events; | |
| 48 | const packet = s.bench[id]; | |
| 49 | if (!packet || packet.state !== 'down') return events; | |
| 50 | packet.state = 'up'; s.up.push(id); | |
| 51 | events.push({ type: 'turn', id }); | |
| 52 | if (s.up.length < 2) return events; | |
| 53 | const [a, b] = s.up.map(i => s.bench[i]); | |
| 54 | s.turns++; | |
| 55 | if (a.flower === b.flower) { | |
| 56 | a.state = b.state = 'found'; s.up = []; s.found++; | |
| 57 | events.push({ type: 'match', ids: [a.id, b.id], flower: a.flower }); | |
| 58 | if (s.found === PAIRS) finish(s, true, events); | |
| 59 | } else { | |
| 60 | s.phase = 'beat'; s.beat = BEAT; | |
| 61 | events.push({ type: 'mismatch', ids: [a.id, b.id] }); | |
| 62 | } | |
| 63 | if (!s.over && s.turns >= TURNS && s.phase === 'play') finish(s, false, events); // a last mismatch finishes after its beat, so the pair is seen | |
| 64 | return events; | |
| 65 | } | |
| 66 | function advance(s, dt) { | |
| 67 | const events = []; | |
| 68 | dt = Math.max(0, Math.min(finite(dt), .25)); | |
| 69 | if (s.over) { s.timer += dt; return events; } | |
| 70 | if (s.phase === 'beat') { | |
| 71 | s.beat = Math.max(0, s.beat - dt); | |
| 72 | if (s.beat <= 0) { | |
| 73 | for (const i of s.up) s.bench[i].state = 'down'; | |
| 74 | s.up = []; s.phase = 'play'; | |
| 75 | events.push({ type: 'turnback' }); | |
| 76 | if (s.turns >= TURNS) finish(s, false, events); | |
| 77 | } | |
| 78 | } | |
| 79 | return events; | |
| 80 | } | |
| 81 | // a tap after the game has been over for a moment starts the next bench; otherwise null | |
| 82 | function again(s) { | |
| 83 | return s.over && s.timer > RESTART_AFTER ? newGame(s.best, s.seed + 1) : null; | |
| 84 | } | |
| 85 | ||
| 86 | const api = { PAIRS, SIZE, TURNS, BEAT, RESTART_AFTER, deal, newGame, turnsLeft, turn, advance, again }; | |
| 87 | if (typeof module !== 'undefined') module.exports = api; else root.Packets = api; | |
| 88 | })(typeof window !== 'undefined' ? window : globalThis); |
seed-packets/packets.test.mjs added +155
| @@ -0,0 +1,155 @@ | ||
| 1 | import { test, expect } from 'bun:test'; | |
| 2 | import { PAIRS, SIZE, TURNS, BEAT, deal, newGame, turnsLeft, turn, advance, again } from './packets.js'; | |
| 3 | ||
| 4 | const run = (s, seconds) => { for (let t = 0; t < seconds - 1e-9; t += 1 / 60) advance(s, 1 / 60); }; | |
| 5 | const partner = (s, id) => s.bench.find(p => p.id !== id && p.flower === s.bench[id].flower).id; | |
| 6 | const stranger = (s, id) => s.bench.find(p => p.flower !== s.bench[id].flower && p.state === 'down').id; | |
| 7 | const first = s => s.bench.find(p => p.state === 'down').id; | |
| 8 | const match = s => { const id = first(s); turn(s, id); turn(s, partner(s, id)); }; | |
| 9 | const miss = s => { const id = first(s); turn(s, id); turn(s, stranger(s, id)); run(s, BEAT + .1); }; | |
| 10 | ||
| 11 | test('the gardener returns after 16 turns, and the bench is sixteen packets', () => { | |
| 12 | expect(TURNS).toBe(16); | |
| 13 | expect(PAIRS).toBe(8); | |
| 14 | expect(SIZE).toBe(16); | |
| 15 | expect(turnsLeft(newGame(null, 1))).toBe(16); | |
| 16 | }); | |
| 17 | ||
| 18 | test('the seeded shuffle holds exactly eight pairs, and the same seed lays the same bench', () => { | |
| 19 | const bench = deal(5); | |
| 20 | expect(bench.length).toBe(16); | |
| 21 | for (let f = 0; f < 8; f++) expect(bench.filter(p => p.flower === f).length).toBe(2); | |
| 22 | expect(bench.every(p => p.state === 'down')).toBe(true); | |
| 23 | expect(bench.map(p => p.id)).toEqual([...Array(16).keys()]); | |
| 24 | expect(deal(5)).toEqual(deal(5)); | |
| 25 | expect(deal(5).map(p => p.flower)).not.toEqual(deal(6).map(p => p.flower)); | |
| 26 | }); | |
| 27 | ||
| 28 | test('one packet then a second turns both face up; a turn is counted when the second is up', () => { | |
| 29 | const s = newGame(null, 1); | |
| 30 | turn(s, 0); | |
| 31 | expect(s.bench[0].state).toBe('up'); | |
| 32 | expect(s.turns).toBe(0); | |
| 33 | turn(s, partner(s, 0) === 1 ? 2 : 1); | |
| 34 | expect(s.up.length).toBe(2); | |
| 35 | expect(s.turns).toBe(1); | |
| 36 | expect(turnsLeft(s)).toBe(15); | |
| 37 | }); | |
| 38 | ||
| 39 | test('a match stays face up and counts as found', () => { | |
| 40 | const s = newGame(null, 1); | |
| 41 | turn(s, 0); const events = turn(s, partner(s, 0)); | |
| 42 | expect(events.map(e => e.type)).toEqual(['turn', 'match']); | |
| 43 | expect(s.found).toBe(1); | |
| 44 | expect(s.bench.filter(p => p.state === 'found').length).toBe(2); | |
| 45 | expect(s.phase).toBe('play'); | |
| 46 | run(s, BEAT * 3); | |
| 47 | expect(s.bench.filter(p => p.state === 'found').length).toBe(2); | |
| 48 | expect(turn(s, 0)).toEqual([]); // a found packet cannot be turned again | |
| 49 | }); | |
| 50 | ||
| 51 | test('a mismatch shows both for the beat, then turns both back', () => { | |
| 52 | const s = newGame(null, 1); | |
| 53 | turn(s, 0); const b = stranger(s, 0); | |
| 54 | expect(turn(s, b).map(e => e.type)).toEqual(['turn', 'mismatch']); | |
| 55 | expect(s.phase).toBe('beat'); | |
| 56 | expect(s.found).toBe(0); | |
| 57 | run(s, BEAT / 2); | |
| 58 | expect(s.up.length).toBe(2); | |
| 59 | expect(s.bench[0].state).toBe('up'); | |
| 60 | run(s, BEAT); | |
| 61 | expect(s.phase).toBe('play'); | |
| 62 | expect(s.up).toEqual([]); | |
| 63 | expect(s.bench.every(p => p.state === 'down')).toBe(true); | |
| 64 | }); | |
| 65 | ||
| 66 | test('a third tap during the beat is ignored', () => { | |
| 67 | const s = newGame(null, 1); | |
| 68 | turn(s, 0); turn(s, stranger(s, 0)); | |
| 69 | const third = s.bench.find(p => p.state === 'down').id; | |
| 70 | expect(turn(s, third)).toEqual([]); | |
| 71 | expect(s.bench[third].state).toBe('down'); | |
| 72 | expect(s.up.length).toBe(2); | |
| 73 | expect(s.turns).toBe(1); | |
| 74 | run(s, BEAT + .1); | |
| 75 | expect(turn(s, third).map(e => e.type)).toEqual(['turn']); | |
| 76 | }); | |
| 77 | ||
| 78 | test('taps for ids that are not packets are ignored', () => { | |
| 79 | const s = newGame(null, 1); | |
| 80 | for (const id of [-1, 16, 1.5, NaN, undefined, '3', null]) expect(turn(s, id)).toEqual([]); | |
| 81 | expect(s.up).toEqual([]); | |
| 82 | }); | |
| 83 | ||
| 84 | test('all eight pairs before the gardener returns is a win, with turns used and a best', () => { | |
| 85 | const s = newGame(null, 2); | |
| 86 | miss(s); miss(s); | |
| 87 | while (!s.over) match(s); | |
| 88 | expect(s.won).toBe(true); | |
| 89 | expect(s.outcome).toBe('won'); | |
| 90 | expect(s.found).toBe(8); | |
| 91 | expect(s.turns).toBe(10); | |
| 92 | expect(s.best).toBe(10); | |
| 93 | expect(s.record).toBe(true); | |
| 94 | expect(turn(s, 0)).toEqual([]); | |
| 95 | }); | |
| 96 | ||
| 97 | test('finding the last pair on the last turn is still a win', () => { | |
| 98 | const s = newGame(null, 3); | |
| 99 | for (let i = 0; i < TURNS - PAIRS; i++) miss(s); | |
| 100 | while (!s.over) match(s); | |
| 101 | expect(s.turns).toBe(TURNS); | |
| 102 | expect(s.won).toBe(true); | |
| 103 | }); | |
| 104 | ||
| 105 | test('the gardener returning on the sixteenth turn with pairs left is a loss', () => { | |
| 106 | const s = newGame(null, 4); | |
| 107 | for (let i = 0; i < TURNS - 1; i++) { miss(s); expect(s.over).toBe(false); } | |
| 108 | expect(turnsLeft(s)).toBe(1); | |
| 109 | const id = first(s); turn(s, id); | |
| 110 | const events = turn(s, stranger(s, id)); | |
| 111 | expect(events.map(e => e.type)).toEqual(['turn', 'mismatch']); | |
| 112 | // the last mismatch is seen: both packets stay up for the beat, taps are ignored, then the gardener is back | |
| 113 | expect(s.over).toBe(false); | |
| 114 | expect(s.up.length).toBe(2); | |
| 115 | expect(s.bench[id].state).toBe('up'); | |
| 116 | expect(turn(s, s.bench.find(p => p.state === 'down').id)).toEqual([]); | |
| 117 | run(s, BEAT / 2); | |
| 118 | expect(s.over).toBe(false); | |
| 119 | run(s, BEAT); | |
| 120 | expect(s.over).toBe(true); | |
| 121 | expect(s.won).toBe(false); | |
| 122 | expect(s.outcome).toBe('lost'); | |
| 123 | expect(s.turns).toBe(TURNS); | |
| 124 | expect(s.found).toBe(0); | |
| 125 | expect(s.bench.every(p => p.state === 'down')).toBe(true); | |
| 126 | expect(s.record).toBe(false); | |
| 127 | }); | |
| 128 | ||
| 129 | test('a better win replaces the best, a worse one does not, and a tap after a moment starts a new bench', () => { | |
| 130 | const s = newGame(12, 5); | |
| 131 | while (!s.over) match(s); | |
| 132 | expect(s.best).toBe(8); | |
| 133 | expect(s.record).toBe(true); | |
| 134 | expect(again(s)).toBeNull(); | |
| 135 | run(s, 1); | |
| 136 | const next = again(s); | |
| 137 | expect(next.over).toBe(false); | |
| 138 | expect(next.best).toBe(8); | |
| 139 | expect(next.found).toBe(0); | |
| 140 | expect(next.bench.map(p => p.flower)).not.toEqual(s.bench.map(p => p.flower)); | |
| 141 | const worse = newGame(6, 5); | |
| 142 | miss(worse); | |
| 143 | while (!worse.over) match(worse); | |
| 144 | expect(worse.best).toBe(6); | |
| 145 | expect(worse.record).toBe(false); | |
| 146 | expect(again(newGame(null, 1))).toBeNull(); | |
| 147 | }); | |
| 148 | ||
| 149 | test('bad input is tamed', () => { | |
| 150 | expect(newGame(-3, NaN).best).toBeNull(); | |
| 151 | const s = newGame('x', Infinity); | |
| 152 | expect(s.best).toBeNull(); | |
| 153 | expect(advance(s, NaN)).toEqual([]); | |
| 154 | expect(advance(s, -5)).toEqual([]); | |
| 155 | }); |
serve.test.mjs +7
| @@ -451,6 +451,13 @@ test('Dinghy Race and its rules script are served without a track', async () => | ||
| 451 | 451 | expect((await fetch(base + '/dinghy-race/dinghy.js')).status).toBe(200); |
| 452 | 452 | }); |
| 453 | 453 | |
| 454 | test('Seed Packets and its rules script are served without a track', async () => { | |
| 455 | const page = await fetch(base + '/seed-packets/'); | |
| 456 | expect(page.status).toBe(200); | |
| 457 | expect(page.headers.get('content-type')).toBe('text/html; charset=utf-8'); | |
| 458 | expect((await fetch(base + '/seed-packets/packets.js')).status).toBe(200); | |
| 459 | }); | |
| 460 | ||
| 454 | 461 | test('Empire Age and each of its scripts are served without a track', async () => { |
| 455 | 462 | const page = await fetch(base + '/empire/'); |
| 456 | 463 | expect(page.status).toBe(200); |
shell.js +1
| @@ -42,6 +42,7 @@ window.PixelPlays = (() => { | ||
| 42 | 42 | { id: 'coconut-shy', title: 'Coconut Shy', blurb: 'Drag and release to knock the coconuts off their posts, five balls a round', kind: 'Throwing', input: 'Drag', accent: '#7a4a2a', music: false, forged: true }, |
| 43 | 43 | { id: 'bowls-green', title: 'Bowls Green', blurb: 'Drag back to set line and weight and curve your woods in close to the jack', kind: 'Sport', input: 'Drag', accent: '#4f7f3f', music: false, forged: true }, |
| 44 | 44 | { id: 'dinghy-race', title: 'Dinghy Race', blurb: 'Drag to trim the sail to the wind and round three buoys ahead of the fleet', kind: 'Racing', input: 'Drag', accent: '#2e8a7a', music: false, forged: true }, |
| 45 | { id: 'seed-packets', title: 'Seed Packets', blurb: 'Tap to turn over seed packets and match every pair before the gardener comes back', kind: 'Memory', input: 'Tap', accent: '#7d9a4a', music: false, forged: true }, | |
| 45 | 46 | { 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 }, |
| 46 | 47 | { 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 }, |
| 47 | 48 | { id: 'go-kart', title: 'Go-Kart Fete', blurb: 'Hold to race, let go for the bends: three laps round the hay bales against three rivals', kind: 'Racing', input: 'Hold', accent: '#e0a526', music: false, forged: true }, |
new above
offline
INK_PIX is never used. Delete it.