Public: anyone can read this; the crew can change it.
Migrated from github.com/amirmushichge/pixel-plays
Add Sandcastle Tide: the playable page and its place in the catalogue, on the rules in `sandcastle/tide.js` #183
Check · Review · Land · Live done — Landed and live in production.
quill/pixel-plays:quill/149-add-sandcastle-tide-the-playable-page-an → main 1 commit fork
8 files
changed +403
−1
README.md +2 −1
| @@ -51,6 +51,7 @@ Concept by [abhi](https://x.com/awwbhi2). Game by [AmirMushich](https://x.com/Am | ||
| 51 | 51 | | [](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 | |
| 52 | 52 | | [](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 | |
| 53 | 53 | | [](ferryman/) | A river with a near bank, a far bank and a ferry moored at the near landing: tap a passenger to load or unload it, then tap the ferry to cross (or press 1-4 to pick a passenger and Space to cross); the fox, the goose and the grain start on the near bank, and left alone together on a bank the fox eats the goose and the goose eats the grain, so the ferry carries one at a time; three levels, the later ones add a dog, a two-seat ferry and a crossing limit, and a round is won by getting everyone across in par crossings or fewer, the HUD showing level, crossings and par as the rules report them (forged by the crew on the engine and shell above, not by the original creators; rules in `ferryman/ferry.js`) | Tap; 1-4 and Space | |
| 54 | | [](sandcastle/) | A square beach with a sandcastle on the far edge, rocks scattered over the sand and the sea along the near edge: tap a sand square to pat down a wall (or move a cursor with the arrow keys and press Space), four walls before each tide, then tap tide (or press Enter) and the water floods every square it can reach from the sea, a little further each time, never through a wall or a rock, and washes away half the walls it touches; keep the castle dry through five tides, the HUD showing the tide, the walls left and the dry squares; input is ignored while the water spreads, the surf line ripples (stopped under reduced motion), and sand, water, walls and rocks differ by texture as well as colour (forged by the crew on the engine and shell above, not by the original creators; rules in `sandcastle/tide.js`) | Tap a sand square, then tide; arrows and Space to build, Enter for the tide | | |
| 54 | 55 | | [](lock-keeper/) | A flight of three canal locks climbing left to right, red narrowboats waiting at the bottom to go up and blue ones at the top to come down, in a seeded order (three rounds shipped, each with a par): tap a paddle or gate to work it and a boat to move it into or out of its lock, or Left and Right to pick a lock and 1 to 5 for its top paddle, bottom paddle, top gate, bottom gate and a boat; a gate opens only on level water, a paddle fills or empties the lock but not while the far gate is open, two open paddles flood the lock and lose the round, a refusal says why, and the end shows operations against par (forged by the crew on the engine and shell above, not by the original creators; rules in `lock-keeper/lock.js`) | Tap; Left and Right, 1 to 5 | |
| 55 | 56 | | [](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 | |
| 56 | 57 | | [](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 | |
| @@ -76,7 +77,7 @@ On the forge, `forge.json` runs `bun serve.mjs` with health check `/`. State is | ||
| 76 | 77 | 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. |
| 77 | 78 | |
| 78 | 79 | ## Add a game |
| 79 | 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, Seed Packets, Hoopla Stall, Lock Keeper, Ferryman and Tower Duel 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`. | |
| 80 | 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, Seed Packets, Hoopla Stall, Lock Keeper, Ferryman, Tower Duel and Sandcastle Tide 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`. | |
| 80 | 81 | |
| 81 | 82 | - `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. |
| 82 | 83 | - `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. |
index.html +1
| @@ -105,6 +105,7 @@ footer a::after { content: none; } | ||
| 105 | 105 | <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> |
| 106 | 106 | <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> |
| 107 | 107 | <li data-kind="Strategy" data-input="Tap" style="--accent:#4f7f6a"><a href="ferryman/">Ferryman</a><span>Row the fox, the goose and the grain across without leaving the wrong pair alone</span><span>Game forged by the crew</span><span class="tags">Strategy · Tap</span></li> |
| 108 | <li data-kind="Strategy" data-input="Tap" style="--accent:#d8b25a"><a href="sandcastle/">Sandcastle Tide</a><span>Pat down sand walls between tides and keep the castle dry through five high waters</span><span>Game forged by the crew</span><span class="tags">Strategy · Tap</span></li> | |
| 108 | 109 | <li data-kind="Strategy" data-input="Tap" style="--accent:#5a6e8a"><a href="lock-keeper/">Lock Keeper</a><span>Work the paddles and gates to bring every narrowboat through the flight of locks</span><span>Game forged by the crew</span><span class="tags">Strategy · Tap</span></li> |
| 109 | 110 | <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> |
| 110 | 111 | <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> |
og/home.png
Binary file not shown.
og/sandcastle.png added
Binary file not shown.
rounds.mjs +9
| @@ -158,6 +158,15 @@ export const ROUNDS = { | ||
| 158 | 158 | g.tap(...g.read('ferryAt()')); |
| 159 | 159 | g.run(.3); |
| 160 | 160 | } }, |
| 161 | // wall off the row above the sea, then call the tide with Enter; the flood plays out before the next beat | |
| 162 | 'sandcastle': { over: /castle stayed dry|sea reached the castle/, outcome: /The (castle stayed dry through \d+ tides|sea reached the castle in tide \d+)/, beat: g => { | |
| 163 | if (g.read('busy()')) { g.run(.3); return; } | |
| 164 | const row = g.read('R.grid[Sandcastle.H - 2]'); | |
| 165 | let n = 0; | |
| 166 | for (let x = 0; x < row.length && n < 4; x++) if (row[x] === '.') { g.tap(...g.read(`centreOf(${x}, Sandcastle.H - 2)`)); n++; } | |
| 167 | g.press('Enter'); | |
| 168 | g.run(.3); | |
| 169 | } }, | |
| 161 | 170 | // work the fewest operations that win (found by a search from the state on the page), each by the keys: Left and Right to the lock, then 1 to 5 |
| 162 | 171 | 'lock-keeper': { over: /play again/, outcome: /All \d+ boats through in \d+ operations/, beat: g => { |
| 163 | 172 | for (const op of solve(g.read('R'))) { |
sandcastle/index.html added +383
| @@ -0,0 +1,383 @@ | ||
| 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>Sandcastle Tide</title> | |
| 7 | <!-- og:begin --> | |
| 8 | <meta name="description" content="Pat down sand walls between tides and keep the castle dry through five high waters"> | |
| 9 | <link rel="canonical" href="https://pixel-plays--forge-trevato-apps.open-platform.sh/sandcastle/"> | |
| 10 | <meta property="og:type" content="website"> | |
| 11 | <meta property="og:site_name" content="Pixel Plays"> | |
| 12 | <meta property="og:title" content="Sandcastle Tide"> | |
| 13 | <meta property="og:description" content="Pat down sand walls between tides and keep the castle dry through five high waters"> | |
| 14 | <meta property="og:url" content="https://pixel-plays--forge-trevato-apps.open-platform.sh/sandcastle/"> | |
| 15 | <meta property="og:image" content="https://pixel-plays--forge-trevato-apps.open-platform.sh/og/sandcastle.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="Sandcastle Tide: 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; : #d8b25a; } | |
| 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 | #buttons { display: flex; gap: 24px; } | |
| 34 | .row { display: flex; align-items: center; gap: 8px; color: #8a6a1c; } | |
| 35 | .row b { font-size: 16px; font-weight: 600; min-width: 1ch; font-variant-numeric: tabular-nums; } | |
| 36 | .row span { color: var(); } | |
| 37 | #status { color: var(); min-height: 1.35em; } | |
| 38 | button { pointer-events: auto; font: inherit; color: var(); background: none; border: 0; padding: 10px; margin: -10px; cursor: pointer; } | |
| 39 | #tide { color: #8a6a1c; } | |
| 40 | button:hover, button:focus-visible { color: var(); } | |
| 41 | </style> | |
| 42 | </head> | |
| 43 | <body> | |
| 44 | <div id="stage"><canvas id="c" tabindex="0" aria-label="A square beach seen from above at an angle: a sandcastle on the far edge, grey rocks scattered over the sand and the sea along the near edge. Tap a sand square to build a wall, or move the cursor with the arrow keys and press Space. Then press Tide or Enter to call the tide in: water floods every square it can reach, never through walls or rocks. Keep the castle dry through five tides."></canvas></div> | |
| 45 | <div id="scores"> | |
| 46 | <div class="row"><b id="tides">-</b><span>tide</span></div> | |
| 47 | <div class="row"><b id="walls">-</b><span>walls left</span></div> | |
| 48 | <div class="row"><b id="dry">-</b><span>dry squares</span></div> | |
| 49 | </div> | |
| 50 | <div class="hud" id="bottom"> | |
| 51 | <div id="status" role="status" aria-live="polite"></div> | |
| 52 | <div id="buttons"><button id="tide" type="button">tide</button><button id="mute" type="button" aria-pressed="false">sound on</button></div> | |
| 53 | </div> | |
| 54 | <script src="../shell.js"></script> | |
| 55 | <script> | |
| 56 | PixelPlays.mount({ | |
| 57 | id: 'sandcastle', | |
| 58 | rules: [ | |
| 59 | 'Tap a sand square, or move with the arrows and press Space, to build a wall (four before each tide).', | |
| 60 | 'Tap tide or press Enter: the water floods every square it reaches, but not through walls or rocks.', | |
| 61 | 'It washes away half the walls it touches. Keep the castle dry through five tides.', | |
| 62 | ], | |
| 63 | }); | |
| 64 | </script> | |
| 65 | <script>window.PP_SCENE = { originY: 6, shadowX: 1.3, shadowY: -.12 };</script> | |
| 66 | <script src="../engine.js"></script> | |
| 67 | <script src="tide.js"></script> | |
| 68 | <script> | |
| 69 | 'use strict'; | |
| 70 | ||
| 71 | const SAND = [226, 200, 142], WET = [74, 144, 186], ROCK = [124, 122, 128], BRICK = [190, 150, 98], CASTLE_C = [238, 214, 156], FLAG = [214, 48, 40]; | |
| 72 | const CELL = 3, HALF = Sandcastle.W * CELL / 2, WALL_Z = 1.1, SPREAD_STEP = .28; | |
| 73 | const CURSOR_PIX = rgb(28, 27, 25), FOAM = rgb(214, 236, 244), LOST_PIX = rgb(214, 48, 40); | |
| 74 | ||
| 75 | const world = (u, v, z) => [(u + v) / R2, (v - u) / R2, z]; | |
| 76 | const cellU = cx => -HALF + cx * CELL, cellV = cy => -HALF + cy * CELL; | |
| 77 | const centreU = cx => cellU(cx) + CELL / 2, centreV = cy => cellV(cy) + CELL / 2; | |
| 78 | ||
| 79 | /* ---------- state ---------- */ | |
| 80 | let seed = 1, R = Sandcastle.newRound(seed); | |
| 81 | const G = { time: 0, noteUntil: 0, cx: Sandcastle.CASTLE.x, cy: Sandcastle.H - 3, view: R.grid, spread: null }; | |
| 82 | const nodes = {}; | |
| 83 | for (const k of ['tides', 'walls', 'dry', 'status']) nodes[k] = document.getElementById(k); | |
| 84 | const shown = {}; | |
| 85 | function setText(key, text) { | |
| 86 | if (shown[key] === text) return; | |
| 87 | shown[key] = text; | |
| 88 | nodes[key].textContent = text; | |
| 89 | } | |
| 90 | const busy = () => G.spread !== null; | |
| 91 | function renderHud() { | |
| 92 | setText('tides', `${Math.min(R.tide + (R.status === 'playing' ? 1 : 0), Sandcastle.TIDES)} of ${Sandcastle.TIDES}`); | |
| 93 | setText('walls', String(R.left)); | |
| 94 | setText('dry', String(R.dry)); | |
| 95 | } | |
| 96 | function note(text) { setText('status', text); G.noteUntil = G.time + 2.5; } | |
| 97 | ||
| 98 | /* ---------- painting ---------- */ | |
| 99 | function put(ramp, u, v, z, tone) { | |
| 100 | const p = project((u + v) / R2, (v - u) / R2, z), ix = p.sx | 0, iy = p.sy | 0; | |
| 101 | if (ix < 0 || iy < 0 || ix >= W || iy >= H) return; | |
| 102 | const i = iy * W + ix; | |
| 103 | if (p.d <= zb[i]) return; | |
| 104 | zb[i] = p.d; | |
| 105 | pix[i] = ramp[clamp(Math.floor(tone + (bayerAt(ix, iy) - .5) * .5), 0, 4)]; | |
| 106 | } | |
| 107 | function top(ramp, u0, u1, v0, v1, z, tone, step = .1) { | |
| 108 | for (let u = u0; u <= u1; u += step) for (let v = v0; v <= v1; v += step) put(ramp, u, v, z, typeof tone === 'function' ? tone(u, v) : tone); | |
| 109 | } | |
| 110 | function front(ramp, u0, u1, v, z0, z1, tone, step = .1) { | |
| 111 | for (let u = u0; u <= u1; u += step) for (let z = z0; z <= z1; z += step) put(ramp, u, v, z, typeof tone === 'function' ? tone(u, z) : tone); | |
| 112 | } | |
| 113 | function side(ramp, u, v0, v1, z0, z1, tone, step = .1) { | |
| 114 | for (let v = v0; v <= v1; v += step) for (let z = z0; z <= z1; z += step) put(ramp, u, v, z, typeof tone === 'function' ? tone(v, z) : tone); | |
| 115 | } | |
| 116 | ||
| 117 | const water = new Uint8Array(W * H); | |
| 118 | const sandRamp = rampOf(SAND), wetRamp = rampOf(WET), brickRamp = rampOf(BRICK), castleRamp = rampOf(CASTLE_C); | |
| 119 | // the four ground textures: dry sand is grainy with dark specks, water is banded ripples | |
| 120 | function sandTone(u, v) { | |
| 121 | const h = hash(Math.round(u * 9), Math.round(v * 9)); | |
| 122 | return h > .93 ? 1.5 : 2.9 + (h - .5) * .8; | |
| 123 | } | |
| 124 | function waterTone(u, v) { return (Math.floor(v * 2.3 + noise(u * .5, v * .7) * 2.2) & 1) ? 3 : 2.1; } | |
| 125 | ||
| 126 | // the flat ground: every square at z = 0, with a ragged edge that dissolves into the white | |
| 127 | function paintGround(grid) { | |
| 128 | for (let cy = 0; cy < Sandcastle.H; cy++) for (let cx = 0; cx < Sandcastle.W; cx++) { | |
| 129 | const wet = grid[cy][cx] === '~', ramp = wet ? wetRamp : sandRamp, tone = wet ? waterTone : sandTone; | |
| 130 | for (let u = cellU(cx); u <= cellU(cx) + CELL; u += .05) for (let v = cellV(cy); v <= cellV(cy) + CELL; v += .05) { | |
| 131 | const p = project((u + v) / R2, (v - u) / R2, 0), ix = p.sx | 0, iy = p.sy | 0; | |
| 132 | if (ix < 0 || iy < 0 || ix >= W || iy >= H) continue; | |
| 133 | if (HALF - Math.max(Math.abs(u), Math.abs(v)) < (hash(ix, iy) - .35) * 1.2) continue; | |
| 134 | const i = iy * W + ix; | |
| 135 | if (p.d <= zb[i]) continue; | |
| 136 | zb[i] = p.d; | |
| 137 | pix[i] = ramp[clamp(Math.floor(tone(u, v) + (bayerAt(ix, iy) - .5) * .5), 0, 4)]; | |
| 138 | water[i] = wet ? 1 : 0; | |
| 139 | } | |
| 140 | } | |
| 141 | } | |
| 142 | function paintWall(cx, cy) { | |
| 143 | const u0 = cellU(cx) + .3, u1 = cellU(cx) + CELL - .3, v0 = cellV(cy) + .3, v1 = cellV(cy) + CELL - .3; | |
| 144 | top(brickRamp, u0, u1, v0, v1, WALL_Z, (u, v) => ((u - u0) % .8 < .1 || (v - v0) % .8 < .1) ? 2 : 3.3); | |
| 145 | front(brickRamp, u0, u1, v1, 0, WALL_Z, (u, z) => (((z / .37) % 1) < .14 || (((u - u0) / .8 + (Math.floor(z / .37) & 1) * .5) % 1) < .1 ? 1.4 : 2.6)); | |
| 146 | side(brickRamp, u1, v0, v1, 0, WALL_Z, (v, z) => (((z / .37) % 1) < .14 ? 1 : 1.7)); | |
| 147 | castShadow(...world(centreU(cx), centreV(cy), 0).slice(0, 2), WALL_Z, 1.2, 1, 0); | |
| 148 | } | |
| 149 | function paintRock(cx, cy) { | |
| 150 | const p = world(centreU(cx), centreV(cy), .85 + hash(cx, cy) * .3); | |
| 151 | sphere(...p, 1.25 + hash(cx, cy + 9) * .2, ROCK, 'solid'); | |
| 152 | castShadow(p[0], p[1], 1.4, 1.2, 1, 0); | |
| 153 | } | |
| 154 | function paintCastle(cx, cy, flooded) { | |
| 155 | const u = centreU(cx), v = centreV(cy), grain = (a, b) => 2.5 + (noise(a * 3, b * 3) - .5) * 1.4; | |
| 156 | if (flooded) return; | |
| 157 | top(castleRamp, u - 1.1, u + 1.1, v - 1.1, v + 1.1, 2.2, grain); | |
| 158 | front(castleRamp, u - 1.1, u + 1.1, v + 1.1, 0, 2.2, (a, z) => 1.6 + (noise(a * 3, z * 3) - .5) * 1.2); | |
| 159 | side(castleRamp, u + 1.1, v - 1.1, v + 1.1, 0, 2.2, (b, z) => .9 + (noise(b * 3, z * 3) - .5)); | |
| 160 | top(castleRamp, u - .6, u + .6, v - .6, v + .6, 3.6, grain); | |
| 161 | front(castleRamp, u - .6, u + .6, v + .6, 2.2, 3.6, (a, z) => 1.7 + (noise(a * 4, z * 4) - .5)); | |
| 162 | side(castleRamp, u + .6, v - .6, v + .6, 2.2, 3.6, (b, z) => 1 + (noise(b * 4, z * 4) - .5)); | |
| 163 | for (const [du, dv] of [[-.55, -.55], [.35, -.55], [-.55, .35], [.35, .35]]) { // battlements | |
| 164 | top(castleRamp, u + du, u + du + .2, v + dv, v + dv + .2, 3.95, 3.2, .05); | |
| 165 | front(castleRamp, u + du, u + du + .2, v + dv + .2, 3.6, 3.95, 2, .05); | |
| 166 | } | |
| 167 | capsule(world(u, v, 3.6), world(u, v, 5.2), .06, .06, () => [90, 70, 56], 'solid'); | |
| 168 | top(rampOf(FLAG), u, u + .9, v - .02, v + .02, 5.1, 3, .05); | |
| 169 | top(rampOf(FLAG), u, u + .9, v - .02, v + .02, 4.8, 3, .05); | |
| 170 | castShadow(...world(u, v, 0).slice(0, 2), 2.2, 1.6, 1, 0); | |
| 171 | castShadow(...world(u, v, 0).slice(0, 2), 4.2, .8, 1, 0); | |
| 172 | } | |
| 173 | // the sand's thickness along the near and right edges, so the beach sits on the white like a slab | |
| 174 | function paintSkirt() { | |
| 175 | const dark = rampOf([170, 142, 96]); | |
| 176 | front(dark, -HALF, HALF, HALF, -.9, 0, (u, z) => 1.6 + (hash(Math.round(u * 9), Math.round(z * 9)) - .5) * .8, .1); | |
| 177 | side(dark, HALF, -HALF, HALF, -.9, 0, () => 1, .1); | |
| 178 | } | |
| 179 | // everything that stands still between moves, drawn once into the baked buffers | |
| 180 | function rebuild(grid) { | |
| 181 | G.view = grid; | |
| 182 | ground.fill(0); shadowMask.fill(0); water.fill(0); | |
| 183 | zb.fill(-1e9); | |
| 184 | pix.fill(rgb(255, 255, 255)); | |
| 185 | paintGround(grid); | |
| 186 | for (let i = 0; i < ground.length; i++) ground[i] = zb[i] > -1e8 ? 1 : 0; | |
| 187 | groundZ.set(zb); | |
| 188 | paintSkirt(); | |
| 189 | for (let cy = 0; cy < Sandcastle.H; cy++) for (let cx = 0; cx < Sandcastle.W; cx++) { | |
| 190 | const c = grid[cy][cx]; | |
| 191 | if (c === 'W') paintWall(cx, cy); | |
| 192 | else if (c === '#') paintRock(cx, cy); | |
| 193 | else if (c === 'C' || (cx === Sandcastle.CASTLE.x && cy === Sandcastle.CASTLE.y)) paintCastle(cx, cy, c === '~'); | |
| 194 | } | |
| 195 | applyShadows(groundZ); | |
| 196 | shadeEdges(); | |
| 197 | bakedPix.set(pix); | |
| 198 | bakedZ.set(zb); | |
| 199 | } | |
| 200 | ||
| 201 | /* ---------- drawn each frame ---------- */ | |
| 202 | // the surf line: foam along the sea's inner edge that rolls in and out by whole pixels at 8 fps; still under reduced motion | |
| 203 | const SURF = []; | |
| 204 | for (let u = -HALF + .2; u <= HALF - .2; u += .12) { const p = project(...world(u, cellV(Sandcastle.H - 1), 0)); SURF.push([p.sx | 0, p.sy | 0]); } | |
| 205 | function surf() { | |
| 206 | const beat = STILL ? 0 : Math.floor(G.time * 8); | |
| 207 | SURF.forEach(([ix, iy], k) => { | |
| 208 | if ((ix + (STILL ? 0 : beat >> 2)) % 3 === 0) return; | |
| 209 | const y = iy + 1 + (STILL ? 0 : Math.round(Math.sin(beat * .5 + k * .2))); | |
| 210 | if (y < 0 || y >= H) return; | |
| 211 | if (water[y * W + ix]) pix[y * W + ix] = FOAM; | |
| 212 | }); | |
| 213 | } | |
| 214 | function cursor() { | |
| 215 | if (busy() || R.status !== 'playing') return; | |
| 216 | const u0 = cellU(G.cx), u1 = u0 + CELL, v0 = cellV(G.cy), v1 = v0 + CELL; | |
| 217 | const dot = (u, v) => { const p = project(...world(u, v, 0)), ix = p.sx | 0, iy = p.sy | 0; if (ix >= 0 && iy >= 0 && ix < W && iy < H) pix[iy * W + ix] = CURSOR_PIX; }; | |
| 218 | for (let t = 0; t <= CELL; t += .1) { dot(u0 + t, v0); dot(u0 + t, v1); dot(u0, v0 + t); dot(u1, v0 + t); } | |
| 219 | } | |
| 220 | function draw() { | |
| 221 | pix.set(bakedPix); | |
| 222 | surf(); | |
| 223 | cursor(); | |
| 224 | if (R.status === 'lost') { // a red cross where the castle stood | |
| 225 | const p = project(...world(centreU(Sandcastle.CASTLE.x), centreV(Sandcastle.CASTLE.y), 1)), x = Math.round(p.sx), y = Math.round(p.sy); | |
| 226 | for (let k = -4; k <= 4; k++) for (const [a, b] of [[k, k], [k, -k]]) for (const d of [0, 1]) { const px = x + a + d, py = y + b; if (px >= 0 && py >= 0 && px < W && py < H) pix[py * W + px] = LOST_PIX; } | |
| 227 | } | |
| 228 | ctx.putImageData(image, 0, 0); | |
| 229 | } | |
| 230 | ||
| 231 | /* ---------- game ---------- */ | |
| 232 | const HINT = 'tap sand to build a wall, then tap tide'; | |
| 233 | const REFUSALS = { rock: 'that is a rock', castle: 'that is the castle', wet: 'the water has that square', wall: 'a wall is already there', budget: 'no walls left: call the tide', bounds: 'out of the beach' }; | |
| 234 | function line() { | |
| 235 | if (R.status !== 'playing') return `${R.line} · tap or press a key to ${R.status === 'won' ? 'build on a new beach' : 'try again'}`; | |
| 236 | return R.line; | |
| 237 | } | |
| 238 | function restart() { | |
| 239 | ensureAudio(); | |
| 240 | seed++; | |
| 241 | R = Sandcastle.newRound(seed); | |
| 242 | G.spread = null; G.cx = Sandcastle.CASTLE.x; G.cy = Sandcastle.H - 3; | |
| 243 | rebuild(R.grid); renderHud(); note(HINT); | |
| 244 | } | |
| 245 | function build(cx, cy) { | |
| 246 | ensureAudio(); | |
| 247 | if (R.status !== 'playing') { restart(); return; } | |
| 248 | if (busy()) return; | |
| 249 | const out = Sandcastle.place(R, cx, cy); | |
| 250 | if (!out.ok) { sound(140, .15, 'sawtooth'); note(REFUSALS[out.why] || out.why); return; } | |
| 251 | R = out.state; | |
| 252 | sound(380 + hash(cx, cy) * 60, .08, 'triangle'); | |
| 253 | rebuild(R.grid); renderHud(); note(R.line); | |
| 254 | } | |
| 255 | // distance of each flooded square from the sea through water, so the flood can arrive ring by ring | |
| 256 | function ringsOf(grid) { | |
| 257 | const dist = new Map(), queue = []; | |
| 258 | for (let x = 0; x < Sandcastle.W; x++) { dist.set((Sandcastle.H - 1) * Sandcastle.W + x, 0); queue.push([x, Sandcastle.H - 1]); } | |
| 259 | for (let q = 0; q < queue.length; q++) { | |
| 260 | const [x, y] = queue[q], d = dist.get(y * Sandcastle.W + x); | |
| 261 | for (const [dx, dy] of [[1, 0], [-1, 0], [0, 1], [0, -1]]) { | |
| 262 | const nx = x + dx, ny = y + dy; | |
| 263 | if (nx < 0 || ny < 0 || nx >= Sandcastle.W || ny >= Sandcastle.H || dist.has(ny * Sandcastle.W + nx) || grid[ny][nx] !== '~') continue; | |
| 264 | dist.set(ny * Sandcastle.W + nx, d + 1); queue.push([nx, ny]); | |
| 265 | } | |
| 266 | } | |
| 267 | return dist; | |
| 268 | } | |
| 269 | function callTide() { | |
| 270 | ensureAudio(); | |
| 271 | if (R.status !== 'playing') { restart(); return; } | |
| 272 | if (busy()) return; | |
| 273 | const before = R.grid, out = Sandcastle.flood(R); | |
| 274 | if (!out.ok) return; | |
| 275 | const after = out.state; | |
| 276 | sound(160, .5, 'sine'); | |
| 277 | if (STILL) { R = after; rebuild(R.grid); renderHud(); finish(); return; } | |
| 278 | G.spread = { before, after, rings: ringsOf(after.grid), step: 0, at: G.time }; | |
| 279 | setText('status', 'the tide is coming in…'); | |
| 280 | G.noteUntil = G.time + 99; | |
| 281 | } | |
| 282 | function finish() { | |
| 283 | if (R.status === 'won') { sound(520, .3, 'sine'); sound(780, .5, 'sine', .15); } else if (R.status === 'lost') sound(110, .6, 'sawtooth'); | |
| 284 | setText('status', line()); G.noteUntil = 0; | |
| 285 | } | |
| 286 | // show the flood as of ring k: new water within k steps of the sea, walls still standing until the tide has finished | |
| 287 | function viewAt(sp, k) { | |
| 288 | return sp.after.grid.map((row, y) => row.split('').map((c, x) => { | |
| 289 | const was = sp.before[y][x]; | |
| 290 | if (c === '~') return was === '~' || sp.rings.get(y * Sandcastle.W + x) <= k ? '~' : was; | |
| 291 | return was === 'W' ? 'W' : c; | |
| 292 | }).join('')); | |
| 293 | } | |
| 294 | function update(dt) { | |
| 295 | G.time += dt; | |
| 296 | const sp = G.spread; | |
| 297 | if (sp) { | |
| 298 | const k = Math.floor((G.time - sp.at) / SPREAD_STEP); | |
| 299 | if (k !== sp.step) { | |
| 300 | sp.step = k; | |
| 301 | const last = Math.max(0, ...sp.rings.values()); | |
| 302 | if (k >= last + 1) { G.spread = null; R = sp.after; rebuild(R.grid); renderHud(); finish(); } | |
| 303 | else { rebuild(viewAt(sp, k)); sound(220 - k * 12, .12, 'sine'); } | |
| 304 | } | |
| 305 | return; | |
| 306 | } | |
| 307 | if (R.status !== 'playing' || G.time >= G.noteUntil) setText('status', line()); | |
| 308 | } | |
| 309 | ||
| 310 | /* ---------- input ---------- */ | |
| 311 | // the square under a point on the canvas, or null off the beach | |
| 312 | function cellAt(px, py) { | |
| 313 | const g = unproject(px, py), u = (g.x - g.y) / R2, v = (g.x + g.y) / R2; | |
| 314 | const cx = Math.floor((u + HALF) / CELL), cy = Math.floor((v + HALF) / CELL); | |
| 315 | return cx >= 0 && cy >= 0 && cx < Sandcastle.W && cy < Sandcastle.H ? [cx, cy] : null; | |
| 316 | } | |
| 317 | const centreOf = (cx, cy) => { const p = project(...world(centreU(cx), centreV(cy), 0)); return [p.sx, p.sy]; }; | |
| 318 | canvas.addEventListener('pointerdown', e => { | |
| 319 | canvas.focus({ preventScroll: true }); | |
| 320 | const r = canvas.getBoundingClientRect(), px = (e.clientX - r.left) / r.width * W, py = (e.clientY - r.top) / r.height * H; | |
| 321 | if (R.status !== 'playing') { restart(); return; } | |
| 322 | const c = cellAt(px, py); | |
| 323 | if (!c || busy()) return; | |
| 324 | [G.cx, G.cy] = c; | |
| 325 | build(c[0], c[1]); | |
| 326 | }); | |
| 327 | const MOVES = { ArrowLeft: [-1, 0], ArrowRight: [1, 0], ArrowUp: [0, -1], ArrowDown: [0, 1] }; | |
| 328 | canvas.addEventListener('keydown', e => { | |
| 329 | const move = MOVES[e.key], place = e.key === ' ', tide = e.key === 'Enter'; | |
| 330 | if (!move && !place && !tide) return; | |
| 331 | e.preventDefault(); | |
| 332 | if (e.repeat) return; | |
| 333 | if (R.status !== 'playing') { restart(); return; } | |
| 334 | if (busy()) return; | |
| 335 | if (move) { G.cx = clamp(G.cx + move[0], 0, Sandcastle.W - 1); G.cy = clamp(G.cy + move[1], 0, Sandcastle.H - 1); sound(300, .04, 'triangle'); } | |
| 336 | else if (place) build(G.cx, G.cy); | |
| 337 | else callTide(); | |
| 338 | }); | |
| 339 | document.getElementById('tide').addEventListener('click', () => { canvas.focus({ preventScroll: true }); callTide(); }); | |
| 340 | ||
| 341 | /* ---------- sound ---------- */ | |
| 342 | let audio = null, muted = false; | |
| 343 | function ensureAudio() { | |
| 344 | if (audio) { if (audio.state === 'suspended') audio.resume(); return; } | |
| 345 | const AC = window.AudioContext || window.webkitAudioContext; | |
| 346 | if (AC) audio = new AC(); | |
| 347 | } | |
| 348 | function sound(freq, life, type, delay = 0) { | |
| 349 | if (!audio || muted) return; | |
| 350 | const now = audio.currentTime + delay, gain = audio.createGain(), osc = audio.createOscillator(); | |
| 351 | osc.type = type; | |
| 352 | osc.frequency.setValueAtTime(Math.max(40, freq), now); | |
| 353 | gain.gain.setValueAtTime(.0001, now); | |
| 354 | gain.gain.exponentialRampToValueAtTime(.07, now + .01); | |
| 355 | gain.gain.exponentialRampToValueAtTime(.0005, now + life); | |
| 356 | osc.connect(gain).connect(audio.destination); | |
| 357 | osc.start(now); osc.stop(now + life + .02); | |
| 358 | } | |
| 359 | const muteButton = document.getElementById('mute'); | |
| 360 | muteButton.addEventListener('click', () => { | |
| 361 | muted = !muted; | |
| 362 | muteButton.textContent = muted ? 'sound off' : 'sound on'; | |
| 363 | muteButton.setAttribute('aria-pressed', String(muted)); | |
| 364 | }); | |
| 365 | ||
| 366 | /* ---------- fit and boot ---------- */ | |
| 367 | addEventListener('resize', fit); | |
| 368 | fit(); | |
| 369 | rebuild(R.grid); | |
| 370 | note(HINT); | |
| 371 | renderHud(); | |
| 372 | canvas.focus({ preventScroll: true }); | |
| 373 | let last = performance.now(); | |
| 374 | function frame(now) { | |
| 375 | update(Math.min(.04, (now - last) / 1000)); | |
| 376 | last = now; | |
| 377 | draw(); | |
| 378 | requestAnimationFrame(frame); | |
| 379 | } | |
| 380 | requestAnimationFrame(frame); | |
| 381 | </script> | |
| 382 | </body> | |
| 383 | </html> |
serve.test.mjs +7
| @@ -465,6 +465,13 @@ test('Ferryman and its rules script are served without a track', async () => { | ||
| 465 | 465 | expect((await fetch(base + '/ferryman/ferry.js')).status).toBe(200); |
| 466 | 466 | }); |
| 467 | 467 | |
| 468 | test('Sandcastle Tide and its rules script are served without a track', async () => { | |
| 469 | const page = await fetch(base + '/sandcastle/'); | |
| 470 | expect(page.status).toBe(200); | |
| 471 | expect(page.headers.get('content-type')).toBe('text/html; charset=utf-8'); | |
| 472 | expect((await fetch(base + '/sandcastle/tide.js')).status).toBe(200); | |
| 473 | }); | |
| 474 | ||
| 468 | 475 | test('Seed Packets and its rules script are served without a track', async () => { |
| 469 | 476 | const page = await fetch(base + '/seed-packets/'); |
| 470 | 477 | expect(page.status).toBe(200); |
shell.js +1
| @@ -45,6 +45,7 @@ window.PixelPlays = (() => { | ||
| 45 | 45 | { 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 }, |
| 46 | 46 | { 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 }, |
| 47 | 47 | { id: 'ferryman', title: 'Ferryman', blurb: 'Row the fox, the goose and the grain across without leaving the wrong pair alone', kind: 'Strategy', input: 'Tap', accent: '#4f7f6a', music: false, forged: true }, |
| 48 | { id: 'sandcastle', title: 'Sandcastle Tide', blurb: 'Pat down sand walls between tides and keep the castle dry through five high waters', kind: 'Strategy', input: 'Tap', accent: '#d8b25a', music: false, forged: true }, | |
| 48 | 49 | { id: 'lock-keeper', title: 'Lock Keeper', blurb: 'Work the paddles and gates to bring every narrowboat through the flight of locks', kind: 'Strategy', input: 'Tap', accent: '#5a6e8a', music: false, forged: true }, |
| 49 | 50 | { 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 }, |
| 50 | 51 | { 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 }, |
new above
offline