Public: anyone can read this; the crew can change it.
Migrated from github.com/amirmushichge/pixel-plays
Add Dinghy Race: the playable page and its place in the catalogue, on the rules already in `dinghy-race/dinghy.js` #128
Check · Review · Land · Live done — Landed and live in production.
quill/pixel-plays:quill/110-add-dinghy-race-the-playable-page-and-it → main 1 commit fork
9 files
changed +390
−2
README.md +3 −2
| @@ -45,6 +45,7 @@ Concept by [abhi](https://x.com/awwbhi2). Game by [AmirMushich](https://x.com/Am | ||
| 45 | 45 | | [](sheepdog/) | A 90-second dusk over three hillside fields, five sheep each, scattered at the top: drag the dog (or use the arrow keys) so they flee it inside a set radius, flock together and wander when calm, and push one too hard and it bolts the wrong way; each sheep that goes through the dry-stone wall's gate and then into the hurdle pen scores, one that goes round the gate scores nothing once penned; the gate moves every field, the sun lowers with the clock, and 10 or more of 15 wins (forged by the crew on the engine and shell above, not by the original creators) | Drag the dog; arrow keys | |
| 46 | 46 | | [](firefly-jar/) | A 90-second summer night in a back garden: tap a firefly while it glows to catch it into the jar (+1, golden +3), a dim one is scared off and breaks the streak, shoo the moths before they reach the lantern; the third moth ends the night (forged by the crew on the engine and shell above, not by the original creators) | Tap; Tab or arrows + space | |
| 47 | 47 | | [](egg-basket/) | A red henhouse with a sloping tin roof and three hens on the ridge: they lay brown eggs (1 point) and rare golden ones (3) that roll down one of four roof lanes and drop off the eave; slide the basket under each before it reaches the ground, where it cracks; rolling speeds up every ten eggs laid (never so fast that the basket cannot cross three lanes in time), three cracks end the round and 25 or more wins (forged by the crew on the engine and shell above, not by the original creators) | Drag the basket; Left and Right arrows move one lane | |
| 48 | | [](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 | | |
| 48 | 49 | | [](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 | |
| 49 | 50 | | [](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 | |
| 50 | 51 | | [](cloakroom/) | A coat check with two rails of coats, each with a paper tag: turn two tags at a time, a matching pair is handed back and a mismatch turns back after 0.8 s; three rounds of 6, 8 and 10 pairs, 90 seconds each, and more than 12 mismatches in all (or a clock at zero) ends it; the coats sway and light moves under the door, the number and colour on a turned tag mean it plays muted too (forged by the crew on the engine and shell above, not by the original creators) | Tap a coat; arrows move a ring, space or enter turns | |
| @@ -67,7 +68,7 @@ On the forge, `forge.json` runs `bun serve.mjs` with health check `/`. State is | ||
| 67 | 68 | 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. |
| 68 | 69 | |
| 69 | 70 | ## Add a game |
| 70 | 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, Empire Age and Egg Basket play on their own sound effects). Rules that need testing go in a plain script with a `.test.mjs` beside it, as in `kite-hill/model.js`. | |
| 71 | 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, Empire Age, Egg Basket and Dinghy Race 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`. | |
| 71 | 72 | |
| 72 | 73 | - `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. |
| 73 | 74 | - `engine.js` provides the camera, depth buffer, colour ramps, shadows, edges, figures, trees and wind as globals. |
| @@ -83,7 +84,7 @@ Create `<id>/index.html`, load `../shell.js`, call `PixelPlays.mount({ id, rules | ||
| 83 | 84 | - Depth-buffer edges: darker where an object sits in front, a warm rim on upper-left edges. |
| 84 | 85 | - Large flat surfaces use closely spaced tones; wide steps make the dither read as a checkerboard. |
| 85 | 86 | - Characters stay sharp: flat bands with no dither, a dark silhouette on their own pixels, patterns at least 3px wide. |
| 86 | - 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, and Egg Basket's hens peck and the straw in its basket stirs (its rolling eggs are play, not ambience, and still move); all of it stops under `prefers-reduced-motion`. | |
| 87 | - 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, 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); all of it stops under `prefers-reduced-motion`. | |
| 87 | 88 | |
| 88 | 89 | ## Empire Age |
| 89 | 90 |
dinghy-race/index.html added +327
| @@ -0,0 +1,327 @@ | ||
| 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>Dinghy Race</title> | |
| 7 | <!-- og:begin --> | |
| 8 | <meta name="description" content="Drag to trim the sail to the wind and round three buoys ahead of the fleet"> | |
| 9 | <link rel="canonical" href="https://pixel-plays--forge-trevato-apps.open-platform.sh/dinghy-race/"> | |
| 10 | <meta property="og:type" content="website"> | |
| 11 | <meta property="og:site_name" content="Pixel Plays"> | |
| 12 | <meta property="og:title" content="Dinghy Race"> | |
| 13 | <meta property="og:description" content="Drag to trim the sail to the wind and round three buoys ahead of the fleet"> | |
| 14 | <meta property="og:url" content="https://pixel-plays--forge-trevato-apps.open-platform.sh/dinghy-race/"> | |
| 15 | <meta property="og:image" content="https://pixel-plays--forge-trevato-apps.open-platform.sh/og/dinghy-race.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="Dinghy Race: 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; : #2e8a7a; } | |
| 24 | * { box-sizing: border-box; } | |
| 25 | html, body { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent; height: 100%; margin: 0; background: var(); color: var(); } | |
| 26 | body { font: 13px/1.35 ui-monospace, "Cascadia Mono", Consolas, Menlo, monospace; overflow: hidden; } | |
| 27 | #stage { position: fixed; inset: 0; display: grid; place-items: center; } | |
| 28 | canvas { display: block; image-rendering: pixelated; touch-action: none; cursor: ew-resize; outline: none; } | |
| 29 | .hud { position: fixed; left: max(16px, env(safe-area-inset-left)); right: max(16px, env(safe-area-inset-right)); display: flex; justify-content: space-between; gap: 16px; pointer-events: none; } | |
| 30 | #scores { position: fixed; top: max(14px, env(safe-area-inset-top)); left: max(16px, env(safe-area-inset-left)); display: grid; gap: 2px; pointer-events: none; } | |
| 31 | #bottom { bottom: max(14px, env(safe-area-inset-bottom)); align-items: flex-end; } | |
| 32 | .row { display: flex; align-items: center; gap: 8px; color: var(); } | |
| 33 | .row b { font-size: 16px; font-weight: 600; min-width: 4ch; font-variant-numeric: tabular-nums; } | |
| 34 | .row span { color: var(); } | |
| 35 | #line { color: var(); min-height: 1.35em; } | |
| 36 | button { pointer-events: auto; font: inherit; color: var(); background: none; border: 0; padding: 10px; margin: -10px; cursor: pointer; } | |
| 37 | button:hover, button:focus-visible { color: var(); } | |
| 38 | </style> | |
| 39 | </head> | |
| 40 | <body> | |
| 41 | <div id="stage"><canvas id="c" tabindex="0" aria-label="A dinghy race on a bay: your boat and two rivals sail a square course of three buoys and back over a start line marked by two flags, with the wind blowing from the south-east. Drag left and right on the canvas, or press the Up and Down arrow keys to trim the sail in and out, and keep it at about half the angle between the boat and the wind to go fastest. Round all three buoys and cross the line first or second to win."></canvas></div> | |
| 42 | <div id="scores"> | |
| 43 | <div class="row"><b id="place">1st</b><span>place</span></div> | |
| 44 | <div class="row"><b id="buoy">0/3</b><span>buoys</span></div> | |
| 45 | <div class="row"><b id="speed">0.0</b><span>m/s</span></div> | |
| 46 | <div class="row"><b id="sail">45°</b><span id="wind">sail</span></div> | |
| 47 | <div class="row"><b id="time">0.0</b><span>s</span></div> | |
| 48 | <div class="row"><b id="best">–</b><span>best s</span></div> | |
| 49 | </div> | |
| 50 | <div class="hud" id="bottom"> | |
| 51 | <div id="line"><span id="status" role="status" aria-live="polite"></span><span id="idle"></span></div> | |
| 52 | <button id="mute" type="button" aria-pressed="false">sound on</button> | |
| 53 | </div> | |
| 54 | <script src="../shell.js"></script> | |
| 55 | <script> | |
| 56 | PixelPlays.mount({ | |
| 57 | id: 'dinghy-race', | |
| 58 | rules: [ | |
| 59 | 'Drag, or press Up and Down, to trim the sail.', | |
| 60 | 'Sail near half the angle to the wind.', | |
| 61 | 'Round three buoys, finish first or second.', | |
| 62 | ], | |
| 63 | }); | |
| 64 | </script> | |
| 65 | <script>window.PP_SCENE = { originY: 4, shadowX: .15, shadowY: -.7 };</script> | |
| 66 | <script src="../engine.js"></script> | |
| 67 | <script src="dinghy.js"></script> | |
| 68 | <script> | |
| 69 | 'use strict'; | |
| 70 | ||
| 71 | const BEST_KEY = 'pixel-plays-dinghy-race-best'; | |
| 72 | const K = .16, HALF = 12; // world units per metre; half-width of the bay | |
| 73 | const SAIL_RATE = 45; // degrees a second the keys trim | |
| 74 | const PLACES = ['', '1st', '2nd', '3rd']; | |
| 75 | const WATER = [[28, 78, 104], [36, 108, 128], [46, 138, 146], [78, 168, 164], [138, 204, 190]]; | |
| 76 | const FOAM = rgb(214, 238, 232); | |
| 77 | const HULL = [[236, 232, 220], [196, 62, 52], [60, 86, 140]], SAIL = [[250, 248, 240], [244, 190, 70], [226, 120, 96]]; | |
| 78 | const POLE = [96, 74, 56], FLAG = [214, 62, 52], BUOY = [236, 150, 40], BUOY_NEXT = [250, 214, 40], BUOY_DONE = [90, 160, 110]; | |
| 79 | const PATH = [{ x: 0, y: 0 }, ...Dinghy.BUOYS, { x: 0, y: 0 }]; | |
| 80 | const world = (mx, my) => [(mx + 60) * K, (my - 60) * K]; | |
| 81 | const rad = d => d * Math.PI / 180; | |
| 82 | ||
| 83 | /* ---------- state ---------- */ | |
| 84 | const best0 = (() => { try { return Number(localStorage.getItem(BEST_KEY)) || 0; } catch (e) { return 0; } })(); | |
| 85 | let R = Dinghy.newRound(best0, Math.floor(Math.random() * 2 ** 31)); | |
| 86 | let trim = 45, held = 0; // the sail angle you ask for, and the key (-1 in, 1 out) being held | |
| 87 | const G = { time: 0, status: '', noteUntil: 0 }; | |
| 88 | const nodes = {}; | |
| 89 | for (const id of ['place', 'buoy', 'speed', 'sail', 'wind', 'time', 'best', 'status', 'idle']) nodes[id] = document.getElementById(id); | |
| 90 | function put(id, text) { if (nodes[id].textContent !== text) nodes[id].textContent = text; } | |
| 91 | function renderHud() { | |
| 92 | put('place', PLACES[R.place]); | |
| 93 | put('buoy', `${R.buoy}/3`); | |
| 94 | put('speed', R.speed.toFixed(1)); | |
| 95 | put('sail', `${Math.round(R.sail)}°`); | |
| 96 | put('wind', `sail · ${Math.round(Dinghy.windAngle(R.heading, R.wind))}° to the wind`); | |
| 97 | put('time', R.time.toFixed(1)); | |
| 98 | put('best', R.best ? R.best.toFixed(1) : '–'); | |
| 99 | } | |
| 100 | function setStatus(text) { | |
| 101 | if (text === G.status) return; | |
| 102 | G.status = text; | |
| 103 | nodes.status.textContent = text; // the live region: buoys, wind shifts and the result only | |
| 104 | nodes.idle.textContent = ''; | |
| 105 | } | |
| 106 | function setIdle(text) { | |
| 107 | if (text === G.status) return; | |
| 108 | G.status = text; | |
| 109 | nodes.status.textContent = ''; | |
| 110 | nodes.idle.textContent = text; | |
| 111 | } | |
| 112 | function note(text) { setStatus(text); G.noteUntil = G.time + 2.4; } | |
| 113 | const HINT = 'drag, or Up and Down, to trim the sail · best sail is half the angle to the wind'; | |
| 114 | ||
| 115 | /* ---------- baked scene: the bay ---------- */ | |
| 116 | function groundColor(ix, iy) { | |
| 117 | const { x, y } = unproject(ix + .5, iy + .5); | |
| 118 | const edge = Math.pow(Math.pow(Math.abs(x) / HALF, 8) + Math.pow(Math.abs(y) / HALF, 8), .125); | |
| 119 | const rough = edge + (noise(x * .5, y * .5) - .5) * .05; | |
| 120 | if (rough > 1.03) return 0; | |
| 121 | if (rough > .93 && hash(ix, iy) < (rough - .93) / .1) return 0; | |
| 122 | const shore = clamp((rough - .8) / .2, 0, 1); | |
| 123 | const t = clamp(1.7 + shore * 1.3 - (noise(x * .35, y * .35) - .5) * 1.1 + (hash(ix, iy) - .5) * .3, 0, 4); | |
| 124 | const c = WATER[Math.floor(t)]; | |
| 125 | return rgb(...c); | |
| 126 | } | |
| 127 | function bake() { | |
| 128 | ground.fill(0); shadowMask.fill(0); staticShadow.fill(0); kinds.fill(0); | |
| 129 | zb.fill(-1e9); | |
| 130 | pix.fill(rgb(255, 255, 255)); | |
| 131 | for (let iy = 0; iy < H; iy++) for (let ix = 0; ix < W; ix++) { | |
| 132 | const g = unproject(ix + .5, iy + .5), i = iy * W + ix, c = groundColor(ix, iy); | |
| 133 | if (!c) continue; | |
| 134 | pix[i] = c; ground[i] = 1; | |
| 135 | zb[i] = (g.x + g.y) / R2 * COS; | |
| 136 | } | |
| 137 | groundZ.set(zb); | |
| 138 | bakeOcclusion(); | |
| 139 | staticShadow.set(shadowMask); | |
| 140 | applyShadows(groundZ); | |
| 141 | bakedPix.set(pix); | |
| 142 | bakedZ.set(zb); | |
| 143 | } | |
| 144 | // water ripples: short pale dashes that come and go on a slow beat; one fixed frame under reduced motion | |
| 145 | const RIPPLES = Array.from({ length: 70 }, (_, k) => ({ ix: 10 + Math.floor(hash(k, 3) * 340), iy: 60 + Math.floor(hash(k, 7) * 170), len: 3 + Math.floor(hash(k, 11) * 3), phase: Math.floor(hash(k, 13) * 4) })); | |
| 146 | function ripples() { | |
| 147 | const beat = STILL ? 0 : Math.floor(G.time * 1.5); | |
| 148 | for (const r of RIPPLES) { | |
| 149 | if ((beat + r.phase) % 4 > 1) continue; | |
| 150 | const dx = STILL ? 0 : (beat + r.phase) % 4; | |
| 151 | for (let n = 0; n < r.len; n++) { | |
| 152 | const ix = r.ix + n + dx, i = r.iy * W + ix; | |
| 153 | if (ix < W && ground[i] && zb[i] === bakedZ[i]) pix[i] = FOAM; | |
| 154 | } | |
| 155 | } | |
| 156 | } | |
| 157 | ||
| 158 | /* ---------- figures ---------- */ | |
| 159 | function along(dist) { // the point `dist` metres along the course | |
| 160 | let d = clamp(dist, 0, Dinghy.COURSE); | |
| 161 | for (let i = 1; i < PATH.length; i++) { | |
| 162 | const a = PATH[i - 1], b = PATH[i], len = Math.hypot(b.x - a.x, b.y - a.y); | |
| 163 | if (d <= len || i === PATH.length - 1) { const t = len ? d / len : 0; return { x: a.x + (b.x - a.x) * t, y: a.y + (b.y - a.y) * t, heading: Math.atan2(b.x - a.x, b.y - a.y) * 180 / Math.PI }; } | |
| 164 | d -= len; | |
| 165 | } | |
| 166 | } | |
| 167 | // a dinghy at metres (mx, my) on a compass heading; the boom swings to leeward | |
| 168 | function boat(mx, my, heading, wind, sail, k) { | |
| 169 | const [x, y] = world(mx, my), h = rad(heading), fx = Math.sin(h), fy = Math.cos(h); | |
| 170 | const hull = HULL[k], cloth = SAIL[k]; | |
| 171 | const at = (along, z, side = 0) => [x + fx * along + fy * side, y + fy * along - fx * side, z]; | |
| 172 | capsule(at(-.55, .3), at(.62, .3), .2, .16, () => hull, 'solid'); | |
| 173 | capsule(at(.62, .3), at(.9, .36), .16, .05, () => hull, 'solid'); | |
| 174 | const rel = ((wind + 180 - heading) % 360 + 540) % 360 - 180, side = rel >= 0 ? 1 : -1; | |
| 175 | const boom = rad(heading + 180 - side * sail), bx = Math.sin(boom), by = Math.cos(boom), mast = .2; | |
| 176 | const mx0 = x + fx * mast, my0 = y + fy * mast; | |
| 177 | capsule([mx0, my0, .3], [mx0, my0, 2.1], .05, .03, () => [96, 74, 56], 'solid'); | |
| 178 | capsule([mx0, my0, .7], [mx0 + bx * .95, my0 + by * .95, .7], .04, .04, () => [96, 74, 56], 'solid'); | |
| 179 | for (let r = 0; r <= 5; r++) for (let c = 0; c <= 5 - r; c++) { // the sail: a triangle from the mast up and back along the boom | |
| 180 | const t = c / 5, z = .78 + r * .26; | |
| 181 | sphere(mx0 + bx * (c * .17), my0 + by * (c * .17), z, .11, cloth, 'solid'); | |
| 182 | } | |
| 183 | castShadow(x, y, .9, .55, 1, 0); | |
| 184 | castShadow(x, y, 1.6, .3, 1, 0); | |
| 185 | } | |
| 186 | function buoys() { | |
| 187 | Dinghy.BUOYS.forEach((b, n) => { | |
| 188 | const [x, y] = world(b.x, b.y), c = n < R.buoy ? BUOY_DONE : n === R.buoy ? BUOY_NEXT : BUOY; | |
| 189 | sphere(x, y, .22, .3, c, 'gloss'); | |
| 190 | capsule([x, y, .3], [x, y, .9], .05, .03, () => [60, 60, 66], 'solid'); | |
| 191 | sphere(x, y, .98, .1, c, 'solid'); | |
| 192 | castShadow(x, y, .5, .3, 1, 0); | |
| 193 | }); | |
| 194 | } | |
| 195 | function startLine() { // two poles either side of the finish, flags streaming down the wind | |
| 196 | const lee = rad(R.wind + 180), wx = Math.sin(lee), wy = Math.cos(lee); | |
| 197 | const flap = STILL ? 0 : Math.floor(G.time * 8); | |
| 198 | for (const s of [-1, 1]) { | |
| 199 | const [x, y] = world(0, s * 14); | |
| 200 | capsule([x, y, 0], [x, y, 2.2], .06, .05, () => POLE, 'solid'); | |
| 201 | for (let n = 0; n < 4; n++) { | |
| 202 | const wave = STILL ? 0 : ((flap + n + (s > 0 ? 2 : 0)) % 4 < 2 ? .06 : -.06); | |
| 203 | sphere(x + wx * (.14 + n * .13) - wy * wave, y + wy * (.14 + n * .13) + wx * wave, 2.1 - n * .02, .09, FLAG, 'solid'); | |
| 204 | } | |
| 205 | castShadow(x, y, 1.1, .2, 1, 0); | |
| 206 | } | |
| 207 | } | |
| 208 | function draw() { | |
| 209 | pix.set(bakedPix); | |
| 210 | zb.set(bakedZ); | |
| 211 | ripples(); | |
| 212 | startLine(); | |
| 213 | buoys(); | |
| 214 | R.rivals.forEach((r, k) => { const p = along(r.progress); boat(p.x, p.y, p.heading, R.wind, 40, k + 1); }); | |
| 215 | boat(R.x, R.y, R.heading, R.wind, R.sail, 0); | |
| 216 | flushShadows(); | |
| 217 | applyShadows(groundZ); | |
| 218 | shadeEdges(); | |
| 219 | ctx.putImageData(image, 0, 0); | |
| 220 | } | |
| 221 | ||
| 222 | /* ---------- game ---------- */ | |
| 223 | function act(events) { | |
| 224 | for (const e of events) { | |
| 225 | if (e.type === 'buoy') { | |
| 226 | note(e.buoy < 3 ? `buoy ${e.buoy} rounded · the wind shifts to ${Math.round(e.wind)}°` : 'last buoy rounded · back to the line'); | |
| 227 | sound('buoy'); | |
| 228 | } else if (e.type === 'won' || e.type === 'lost') { | |
| 229 | if (R.record) try { localStorage.setItem(BEST_KEY, String(R.best)); } catch (err) { /* private window: keep it for this visit */ } | |
| 230 | setStatus(`${R.line}${R.record ? ' · new best' : ''} · tap or press space to race again`); | |
| 231 | sound(e.type === 'won' ? 'win' : 'lose'); | |
| 232 | } | |
| 233 | } | |
| 234 | } | |
| 235 | function restart() { | |
| 236 | const next = Dinghy.again(R); | |
| 237 | if (!next) return false; | |
| 238 | R = next; | |
| 239 | note(HINT); | |
| 240 | return true; | |
| 241 | } | |
| 242 | function update(dt) { | |
| 243 | G.time += dt; | |
| 244 | if (held) trim = clamp(trim + held * SAIL_RATE * dt, 0, Dinghy.SAIL_MAX); | |
| 245 | act(Dinghy.advance(R, dt, { sail: trim })); | |
| 246 | renderHud(); | |
| 247 | if (R.phase === 'play' && G.time >= G.noteUntil) setIdle(HINT); | |
| 248 | } | |
| 249 | ||
| 250 | /* ---------- input ---------- */ | |
| 251 | // the canvas is the sheet: the left edge lets it right out, the right edge hauls it in | |
| 252 | function pointTo(e) { | |
| 253 | const r = canvas.getBoundingClientRect(), x = (e.clientX - r.left) / (r.width || W) * W; | |
| 254 | trim = clamp((x - 30) / 300, 0, 1) * Dinghy.SAIL_MAX; | |
| 255 | } | |
| 256 | let dragging = null; | |
| 257 | canvas.addEventListener('pointerdown', e => { | |
| 258 | ensureAudio(); | |
| 259 | canvas.focus({ preventScroll: true }); | |
| 260 | if (restart()) return; | |
| 261 | dragging = e.pointerId; | |
| 262 | try { canvas.setPointerCapture(e.pointerId); } catch (err) { /* synthetic pointers have nothing to capture */ } | |
| 263 | pointTo(e); | |
| 264 | }); | |
| 265 | canvas.addEventListener('pointermove', e => { if (dragging === e.pointerId) pointTo(e); }); | |
| 266 | for (const type of ['pointerup', 'pointercancel']) canvas.addEventListener(type, e => { if (dragging === e.pointerId) dragging = null; }); | |
| 267 | const KEYMAP = { ArrowUp: -1, w: -1, W: -1, ArrowDown: 1, s: 1, S: 1 }; | |
| 268 | canvas.addEventListener('keydown', e => { | |
| 269 | const k = KEYMAP[e.key]; | |
| 270 | if (!k && e.key !== ' ' && e.key !== 'Enter') return; | |
| 271 | e.preventDefault(); | |
| 272 | ensureAudio(); | |
| 273 | if (restart() || !k) return; | |
| 274 | dragging = null; | |
| 275 | held = k; | |
| 276 | }); | |
| 277 | canvas.addEventListener('keyup', e => { if (KEYMAP[e.key] === held) held = 0; }); | |
| 278 | canvas.addEventListener('blur', () => { held = 0; }); | |
| 279 | ||
| 280 | /* ---------- sound ---------- */ | |
| 281 | let audio = null, muted = false; | |
| 282 | function ensureAudio() { | |
| 283 | if (audio) { if (audio.state === 'suspended') audio.resume(); return; } | |
| 284 | const AC = window.AudioContext || window.webkitAudioContext; | |
| 285 | if (AC) audio = new AC(); | |
| 286 | } | |
| 287 | function sound(kind) { | |
| 288 | if (!audio || muted) return; | |
| 289 | const now = audio.currentTime, gain = audio.createGain(), osc = audio.createOscillator(); | |
| 290 | const spec = { | |
| 291 | buoy: [520, 780, .14, .06, 'sine'], | |
| 292 | win: [440, 1100, .4, .07, 'sine'], | |
| 293 | lose: [300, 110, .35, .06, 'square'], | |
| 294 | }[kind]; | |
| 295 | osc.type = spec[4]; | |
| 296 | osc.frequency.setValueAtTime(spec[0], now); | |
| 297 | osc.frequency.exponentialRampToValueAtTime(spec[1], now + spec[2]); | |
| 298 | gain.gain.setValueAtTime(spec[3], now); | |
| 299 | gain.gain.exponentialRampToValueAtTime(.0005, now + spec[2]); | |
| 300 | osc.connect(gain).connect(audio.destination); | |
| 301 | osc.start(now); osc.stop(now + spec[2] + .01); | |
| 302 | } | |
| 303 | const muteButton = document.getElementById('mute'); | |
| 304 | muteButton.addEventListener('click', () => { | |
| 305 | muted = !muted; | |
| 306 | muteButton.textContent = muted ? 'sound off' : 'sound on'; | |
| 307 | muteButton.setAttribute('aria-pressed', String(muted)); | |
| 308 | }); | |
| 309 | ||
| 310 | /* ---------- fit and boot ---------- */ | |
| 311 | addEventListener('resize', fit); | |
| 312 | fit(); | |
| 313 | bake(); | |
| 314 | note(HINT); | |
| 315 | renderHud(); | |
| 316 | canvas.focus({ preventScroll: true }); | |
| 317 | let last = performance.now(); | |
| 318 | function frame(now) { | |
| 319 | update(Math.min(.04, (now - last) / 1000)); | |
| 320 | last = now; | |
| 321 | draw(); | |
| 322 | requestAnimationFrame(frame); | |
| 323 | } | |
| 324 | requestAnimationFrame(frame); | |
| 325 | </script> | |
| 326 | </body> | |
| 327 | </html> |
dinghy-race/page.test.mjs added +46
| @@ -0,0 +1,46 @@ | ||
| 1 | import { readFileSync, readdirSync } from 'node:fs'; | |
| 2 | import { test, expect } from 'bun:test'; | |
| 3 | import { boot } from '../playtest.mjs'; | |
| 4 | ||
| 5 | const html = readFileSync(new URL('./index.html', import.meta.url), 'utf8'); | |
| 6 | ||
| 7 | test('the keys trim the sail in and out and the HUD reports it', () => { | |
| 8 | const g = boot('dinghy-race'); | |
| 9 | g.run(.5); | |
| 10 | const from = g.read('trim'); | |
| 11 | g.press('ArrowUp', .4); | |
| 12 | expect(g.read('trim')).toBeLessThan(from); | |
| 13 | const mid = g.read('trim'); | |
| 14 | g.press('ArrowDown', .8); | |
| 15 | expect(g.read('trim')).toBeGreaterThan(mid); | |
| 16 | expect(String(g.el('sail').textContent)).toBe(`${Math.round(g.read('R.sail'))}°`); | |
| 17 | expect(g.errors).toEqual([]); | |
| 18 | }); | |
| 19 | ||
| 20 | test('dragging along the sheet sets the sail from the pointer', () => { | |
| 21 | const g = boot('dinghy-race'); | |
| 22 | g.drag([60, 140], [330, 140], .5); | |
| 23 | expect(g.read('trim')).toBeCloseTo(90, 0); | |
| 24 | g.drag([330, 140], [30, 140], .5); | |
| 25 | expect(g.read('trim')).toBeCloseTo(0, 0); | |
| 26 | expect(g.errors).toEqual([]); | |
| 27 | }); | |
| 28 | ||
| 29 | test('a well-trimmed boat is faster than a sail left alone', () => { | |
| 30 | const trimmed = boot('dinghy-race'), idle = boot('dinghy-race'); | |
| 31 | const best = trimmed.read('Dinghy.bestSail(Dinghy.windAngle(R.heading, R.wind))'); | |
| 32 | trimmed.tap(30 + best / 90 * 300, 140); | |
| 33 | trimmed.run(3); idle.run(3); | |
| 34 | expect(trimmed.read('R.speed')).toBeGreaterThan(idle.read('R.speed')); | |
| 35 | }); | |
| 36 | ||
| 37 | test('the page has a mute button, says how to play on the canvas and ships no track', () => { | |
| 38 | expect(html).toContain('id="mute"'); | |
| 39 | expect(html).toMatch(/<canvas id="c"[^>]*aria-label="[^"]*[Dd]rag[^"]*Up and Down/); | |
| 40 | expect(readdirSync(import.meta.dir).filter(f => f.endsWith('.mp3'))).toEqual([]); | |
| 41 | }); | |
| 42 | ||
| 43 | test('the rules popup is at most four lines', () => { | |
| 44 | const rules = html.match(/rules: \[([\s\S]*?)\n \]/)[1].split('\n').filter(l => l.trim()); | |
| 45 | expect(rules.length).toBeLessThanOrEqual(4); | |
| 46 | }); |
index.html +1
| @@ -95,6 +95,7 @@ footer a::after { content: none; } | ||
| 95 | 95 | <li data-kind="Catching" data-input="Drag" style="--accent:#5b8a3a"><a href="sheepdog/">Sheepdog</a><span>Drag the dog to steer five sheep through the gate and into the pen before dusk</span><span>Game forged by the crew</span><span class="tags">Catching · Drag</span></li> |
| 96 | 96 | <li data-kind="Catching" data-input="Tap" style="--accent:#c9b43a"><a href="firefly-jar/">Firefly Jar</a><span>Tap the fireflies into a jar while they glow, a dim one flies off, shoo the moths before they reach the lantern</span><span>Game forged by the crew</span><span class="tags">Catching · Tap</span></li> |
| 97 | 97 | <li data-kind="Catching" data-input="Drag" style="--accent:#d08a2e"><a href="egg-basket/">Egg Basket</a><span>Slide the basket under the eave and catch the eggs before three crack</span><span>Game forged by the crew</span><span class="tags">Catching · Drag</span></li> |
| 98 | <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> | |
| 98 | 99 | <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> |
| 99 | 100 | <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> |
| 100 | 101 | <li data-kind="Memory" data-input="Tap" style="--accent:#9c3d6b"><a href="cloakroom/">Cloakroom</a><span>Turn two tags at a time and hand back every matching coat before the dance lets out</span><span>Game forged by the crew</span><span class="tags">Memory · Tap</span></li> |
og/dinghy-race.png added
Binary file not shown.
og/home.png
Binary file not shown.
rounds.mjs +5
| @@ -18,6 +18,11 @@ export const ROUNDS = { | ||
| 18 | 18 | // tend the fire: a twig whenever the flames sink toward the lower mark |
| 19 | 19 | 'campfire': { over: /play again/, outcome: /dawn, the fire held · \d+ s/, beat: g => { if (g.read('G.heat') < 55 && g.read('G.fuel.length') < 3) g.press(' '); g.step(5); } }, |
| 20 | 20 | 'domino-run': { over: /play again/, outcome: /\d+ points/, beat: g => { g.press(' '); g.press('ArrowRight'); g.press('Enter'); g.run(1); } }, |
| 21 | // hold the sail at half the angle to the wind, which is what drives the boat best | |
| 22 | 'dinghy-race': { over: /play again|race again/, outcome: /(First|Second) place in \d+\.\d s: a win/, beat: g => { | |
| 23 | const x = 30 + g.read('Dinghy.bestSail(Dinghy.windAngle(R.heading, R.wind))') / 90 * 300; | |
| 24 | g.down(x, 140); g.up(x, 140); g.step(1); | |
| 25 | } }, | |
| 21 | 26 | // stand under the egg that drops first until 25 are in the basket, then keep clear of each until three crack |
| 22 | 27 | 'egg-basket': { over: /play again/, outcome: /the basket is full · \d+ points/, beat: g => { |
| 23 | 28 | const lane = g.read('R.eggs.length ? R.eggs[0].lane : -1'); |
serve.test.mjs +7
| @@ -430,6 +430,13 @@ test('Egg Basket and its rules script are served without a track', async () => { | ||
| 430 | 430 | expect((await fetch(base + '/egg-basket/basket.js')).status).toBe(200); |
| 431 | 431 | }); |
| 432 | 432 | |
| 433 | test('Dinghy Race and its rules script are served without a track', async () => { | |
| 434 | const page = await fetch(base + '/dinghy-race/'); | |
| 435 | expect(page.status).toBe(200); | |
| 436 | expect(page.headers.get('content-type')).toBe('text/html; charset=utf-8'); | |
| 437 | expect((await fetch(base + '/dinghy-race/dinghy.js')).status).toBe(200); | |
| 438 | }); | |
| 439 | ||
| 433 | 440 | test('Empire Age and each of its scripts are served without a track', async () => { |
| 434 | 441 | const page = await fetch(base + '/empire/'); |
| 435 | 442 | expect(page.status).toBe(200); |
shell.js +1
| @@ -39,6 +39,7 @@ window.PixelPlays = (() => { | ||
| 39 | 39 | { id: 'sheepdog', title: 'Sheepdog', blurb: 'Drag the dog to steer five sheep through the gate and into the pen before dusk', kind: 'Catching', input: 'Drag', accent: '#5b8a3a', music: false, forged: true }, |
| 40 | 40 | { id: 'firefly-jar', title: 'Firefly Jar', blurb: 'Tap the fireflies into a jar while they glow, a dim one flies off, shoo the moths before they reach the lantern', kind: 'Catching', input: 'Tap', accent: '#c9b43a', music: false, forged: true }, |
| 41 | 41 | { id: 'egg-basket', title: 'Egg Basket', blurb: 'Slide the basket under the eave and catch the eggs before three crack', kind: 'Catching', input: 'Drag', accent: '#d08a2e', music: false, forged: true }, |
| 42 | { 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 }, | |
| 42 | 43 | { 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 }, |
| 43 | 44 | { 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 }, |
| 44 | 45 | { id: 'cloakroom', title: 'Cloakroom', blurb: 'Turn two tags at a time and hand back every matching coat before the dance lets out', kind: 'Memory', input: 'Tap', accent: '#9c3d6b', music: false, forged: true }, |
new above
offline