Public: anyone can read this; the crew can change it.
Migrated from github.com/amirmushichge/pixel-plays
Add Tightrope: walk a wire between two rooftops, leaning to keep your balance in the wind #60
Check · Review · Land · Live done — Landed and live in production.
quill/pixel-plays:quill/51-add-tightrope-walk-a-wire-between-two-ro → main 3 commits fork 9 threads · 9 unresolved
README.md +2 −1
| @@ -31,6 +31,7 @@ Concept by [abhi](https://x.com/awwbhi2). Game by [AmirMushich](https://x.com/Am | ||
| 31 | 31 | | [](beekeeper/) | A 60-second hive inspection: hold the smoker to calm the bees (its fuel runs low and refills slowly), tap a frame to lift it into the crate, golden frames score 3 and half-capped 1, a dark brood frame costs a sting; three stings or a swarm end the round (forged by the crew on the engine and shell above, not by the original creators) | Tap a frame, hold the smoke button; arrows + space, hold S | |
| 32 | 32 | | [](campfire/) | A 90-second night: keep a campfire at a good height by dragging twigs, branches and logs into the ring; a log smothers a small fire and rain damps the flames (forged by the crew on the engine and shell above, not by the original creators) | Drag from the pile; arrows or 1/2/3 + space | |
| 33 | 33 | | [](bread-oven/) | A 60-second shift at a brick oven with four slots: slide dough in with the peel, crusts go pale, golden, dark, burnt, and slots nearer the fire bake faster; pulling golden scores 3, dark 1, pale 0, and a burnt loaf is a strike, three end the round (forged by the crew on the engine and shell above, not by the original creators) | Tap a slot, or arrows + space to slide in, enter to pull | |
| 34 | | [](tightrope/) | Three crossings of a wire between two rooftops: the walker steps on alone while a lean angle falls away from centre and wind gusts (shown in the grass and the chimney flag a beat early) tip it; hold left or right to lean back, but a long hold overcorrects, and past 30 degrees you drop to the net and restart from the last of three marks; a crossing scores 10 plus 1 per second under 60, and 5 more if the lean never passed half the limit (forged by the crew on the engine and shell above, not by the original creators) | Hold left or right half of the canvas; arrows or A/D | | |
| 34 | 35 | | [](ice-fishing/) | A 60-second round on a frozen lake: hold to let line out through the hole and watch the hook sink in the cut-away, a perch (1), trout (2) or pike (3) crossing the hook at its depth bites, then hold to reel and ease off to let the tension fall; full tension snaps the line and three snaps end the round (forged by the crew on the engine and shell above, not by the original creators) | Hold the canvas; hold space | |
| 35 | 36 | |
| 36 | 37 | ## Run |
| @@ -51,7 +52,7 @@ On the forge, `forge.json` runs `bun serve.mjs` with health check `/`. State is | ||
| 51 | 52 | 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. |
| 52 | 53 | |
| 53 | 54 | ## Add a game |
| 54 | 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 and Bread Oven 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`. | |
| 55 | 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 and Tightrope 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`. | |
| 55 | 56 | |
| 56 | 57 | - `shell.js` provides the game menu, the Rules popup, the credits and the background music. |
| 57 | 58 | - `engine.js` provides the camera, depth buffer, colour ramps, shadows, edges, figures, trees and wind as globals. |
index.html +1
| @@ -81,6 +81,7 @@ footer a::after { content: none; } | ||
| 81 | 81 | <li data-kind="Timing" data-input="Hold" style="--accent:#d9a02a"><a href="beekeeper/">Beekeeper</a><span>Smoke the hive calm and lift out honey frames before the bees swarm: gold 3, half 1, a dark brood frame stings</span><span>Game forged by the crew</span><span class="tags">Timing · Hold</span></li> |
| 82 | 82 | <li data-kind="Precision" data-input="Drag" style="--accent:#e0793a"><a href="campfire/">Campfire</a><span>Keep a campfire burning through a 90 second night: drag twigs, branches and logs into the ring, a log smothers a small fire and rain damps the flames</span><span>Game forged by the crew</span><span class="tags">Precision · Drag</span></li> |
| 83 | 83 | <li data-kind="Timing" data-input="Tap" style="--accent:#c0612b"><a href="bread-oven/">Bread Oven</a><span>Slide loaves into a wood-fired oven and pull each one out golden: golden 3, dark 1, a burnt loaf is a strike</span><span>Game forged by the crew</span><span class="tags">Timing · Tap</span></li> |
| 84 | <li data-kind="Precision" data-input="Hold" style="--accent:#cf4b3a"><a href="tightrope/">Tightrope</a><span>Walk a wire between two rooftops, leaning against the wind to stay up: 10 plus 1 per second under 60, +5 for a steady crossing</span><span>Game forged by the crew</span><span class="tags">Precision · Hold</span></li> | |
| 84 | 85 | <li data-kind="Timing" data-input="Hold" style="--accent:#6fb3d2"><a href="ice-fishing/">Ice Fishing</a><span>Drop a line through the ice and reel each fish up before it snaps: hold to let out line, a fish at the hook depth bites, hold to reel but ease off as the tension rises</span><span>Game forged by the crew</span><span class="tags">Timing · Hold</span></li> |
| 85 | 86 | </ul> |
| 86 | 87 | <footer>Concept by <a href="https://x.com/awwbhi2">abhi</a>. Game by <a href="https://x.com/AmirMushich">AmirMushich</a>. Visual reference for the style: shawn.'s <a href="https://x.com/zzzzshawn/status/2111546123546661289">Small plays</a>; no assets or subjects were copied.<br><a href="stats/">Most played</a></footer> |
og/home.png
og/tightrope.png added
serve.test.mjs +7
| @@ -170,6 +170,13 @@ test('Bread Oven and its rules script are served without a track', async () => { | ||
| 170 | 170 | expect((await fetch(base + '/bread-oven/oven.js')).status).toBe(200); |
| 171 | 171 | }); |
| 172 | 172 | |
| 173 | test('Tightrope and its rules script are served without a track', async () => { | |
| 174 | const page = await fetch(base + '/tightrope/'); | |
| 175 | expect(page.status).toBe(200); | |
| 176 | expect(page.headers.get('content-type')).toBe('text/html; charset=utf-8'); | |
| 177 | expect((await fetch(base + '/tightrope/wire.js')).status).toBe(200); | |
| 178 | }); | |
| 179 | ||
| 173 | 180 | test('Ice Fishing and its rules script are served without a track', async () => { |
| 174 | 181 | const page = await fetch(base + '/ice-fishing/'); |
| 175 | 182 | expect(page.status).toBe(200); |
shell.js +1
| @@ -25,6 +25,7 @@ window.PixelPlays = (() => { | ||
| 25 | 25 | { id: 'beekeeper', title: 'Beekeeper', blurb: 'Smoke the hive calm and lift out honey frames before the bees swarm: gold 3, half 1, a dark brood frame stings', kind: 'Timing', input: 'Hold', accent: '#d9a02a', music: false, forged: true }, |
| 26 | 26 | { id: 'campfire', title: 'Campfire', blurb: 'Keep a campfire burning through a 90 second night: drag twigs, branches and logs into the ring, a log smothers a small fire and rain damps the flames', kind: 'Precision', input: 'Drag', accent: '#e0793a', music: false, forged: true }, |
| 27 | 27 | { id: 'bread-oven', title: 'Bread Oven', blurb: 'Slide loaves into a wood-fired oven and pull each one out golden: golden 3, dark 1, a burnt loaf is a strike', kind: 'Timing', input: 'Tap', accent: '#c0612b', music: false, forged: true }, |
| 28 | { id: 'tightrope', title: 'Tightrope', blurb: 'Walk a wire between two rooftops, leaning against the wind to stay up: 10 plus 1 per second under 60, +5 for a steady crossing', kind: 'Precision', input: 'Hold', accent: '#cf4b3a', music: false, forged: true }, | |
| 28 | 29 | { id: 'ice-fishing', title: 'Ice Fishing', blurb: 'Drop a line through the ice and reel each fish up before it snaps: hold to let out line, a fish at the hook depth bites, hold to reel but ease off as the tension rises', kind: 'Timing', input: 'Hold', accent: '#6fb3d2', music: false, forged: true }, |
| 29 | 30 | ]; |
| 30 | 31 | const MUSIC_VOLUME = .4; |
tightrope/index.html added +415
| @@ -0,0 +1,415 @@ | ||
| 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>Tightrope</title> | |
| 7 | <!-- og:begin --> | |
| 8 | <meta name="description" content="Walk a wire between two rooftops, leaning against the wind to stay up: 10 plus 1 per second under 60, +5 for a steady crossing"> | |
| 9 | <link rel="canonical" href="https://pixel-plays--forge-trevato-apps.open-platform.sh/tightrope/"> | |
| 10 | <meta property="og:type" content="website"> | |
| 11 | <meta property="og:site_name" content="Pixel Plays"> | |
| 12 | <meta property="og:title" content="Tightrope"> | |
| 13 | <meta property="og:description" content="Walk a wire between two rooftops, leaning against the wind to stay up: 10 plus 1 per second under 60, +5 for a steady crossing"> | |
| 14 | <meta property="og:url" content="https://pixel-plays--forge-trevato-apps.open-platform.sh/tightrope/"> | |
| 15 | <meta property="og:image" content="https://pixel-plays--forge-trevato-apps.open-platform.sh/og/tightrope.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="Tightrope: 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; : #cf4b3a; } | |
| 24 | * { box-sizing: border-box; } | |
| 25 | html, body { height: 100%; margin: 0; background: var(); color: var(); } | |
| 26 | body { font: 13px/1.35 ui-monospace, "Cascadia Mono", Consolas, Menlo, monospace; overflow: hidden; } | |
| 27 | #stage { position: fixed; inset: 0; display: grid; place-items: center; } | |
| 28 | canvas { display: block; image-rendering: pixelated; touch-action: none; cursor: pointer; outline: none; } | |
| 29 | .hud { position: fixed; left: max(16px, env(safe-area-inset-left)); right: max(16px, env(safe-area-inset-right)); display: flex; justify-content: space-between; gap: 16px; pointer-events: none; } | |
| 30 | #scores { position: fixed; top: max(14px, env(safe-area-inset-top)); left: max(16px, env(safe-area-inset-left)); display: grid; gap: 2px; pointer-events: none; } | |
| 31 | #bottom { bottom: max(14px, env(safe-area-inset-bottom)); align-items: flex-end; } | |
| 32 | .row { display: flex; align-items: center; gap: 8px; color: var(); } | |
| 33 | .row b { font-size: 16px; font-weight: 600; min-width: 1ch; font-variant-numeric: tabular-nums; } | |
| 34 | .row span { color: var(); } | |
| 35 | #lean { position: fixed; top: max(14px, env(safe-area-inset-top)); right: max(16px, env(safe-area-inset-right)); width: 120px; height: 28px; pointer-events: none; } | |
| 36 | #lean i { position: absolute; left: 0; right: 0; top: 13px; height: 2px; background: #d9d6cf; } | |
| 37 | #lean u { position: absolute; top: 7px; width: 2px; height: 14px; background: var(); text-decoration: none; } | |
| 38 | #lean u:nth-of-type(1) { left: 0; } #lean u:nth-of-type(2) { left: 50%; margin-left: -1px; height: 10px; top: 9px; } #lean u:nth-of-type(3) { right: 0; } | |
| 39 | #lean s { position: absolute; left: 50%; top: 5px; width: 8px; height: 18px; margin-left: -4px; background: var(); text-decoration: none; will-change: transform; } | |
| 40 | #lean[data-warn="1"] i, #lean[data-warn="-1"] i { background: var(); } | |
| 41 | #status { color: var(); min-height: 1.35em; } | |
| 42 | button { pointer-events: auto; font: inherit; color: var(); background: none; border: 0; padding: 10px; margin: -10px; cursor: pointer; } | |
| 43 | button:hover, button:focus-visible { color: var(); } | |
| 44 | </style> | |
| 45 | </head> | |
| 46 | <body> | |
| 47 | <div id="stage"><canvas id="c" tabindex="0" aria-label="Two flat rooftops with a wire strung between them and a walker with a balance pole stepping across on their own, a safety net below. Hold the left or right arrow, A or D, or press and hold the left or right half of the canvas, to lean that way and catch a tilt before it passes 30 degrees. Grass and the chimney flag lean one beat before a gust of wind hits. Cross three times; three falls end the round."></canvas></div> | |
| 48 | <div id="scores"> | |
| 49 | <div class="row"><b id="score">0</b><span>score</span></div> | |
| 50 | <div class="row"><b id="falls">0</b><span>falls of 3</span></div> | |
| 51 | <div class="row"><b id="best">0</b><span>best</span></div> | |
| 52 | </div> | |
| 53 | <div id="lean" aria-hidden="true"><i></i><u></u><u></u><u></u><s id="meter"></s></div> | |
| 54 | <div class="hud" id="bottom"> | |
| 55 | <div id="status" role="status" aria-live="polite"></div> | |
| 56 | <button id="mute" type="button" aria-pressed="false">sound on</button> | |
| 57 | </div> | |
| 58 | <script src="../shell.js"></script> | |
| 59 | <script> | |
| 60 | PixelPlays.mount({ | |
| 61 | id: 'tightrope', | |
| 62 | rules: [ | |
| 63 | 'Hold left or right (arrows, A/D, or half the canvas) to lean that way.', | |
| 64 | 'The walker steps on alone; wind tips you, and the grass and flag show it first.', | |
| 65 | 'Past the limit you fall to the net and restart from the last mark.', | |
| 66 | 'Cross for 10 + 1 per second under 60, +5 if steady. Three crossings or falls end it.', | |
| 67 | ], | |
| 68 | }); | |
| 69 | </script> | |
| 70 | <script>window.PP_SCENE = { originY: 6, shadowX: 1.3, shadowY: -.12 };</script> | |
| 71 | <script src="../engine.js"></script> | |
| 72 | <script src="wire.js"></script> | |
| 73 | <script> | |
| 74 | 'use strict'; | |
| 75 | ||
| 76 | const BRICK = [168, 98, 78], CAP = [206, 198, 184], TAR = [120, 124, 134], TAR_LIGHT = [150, 152, 158]; | |
| 77 | const ROPE = [92, 78, 66], WIRE_COLOR = [58, 58, 64], TAPE = [226, 72, 52], POLE = [120, 84, 52], POLE_TIP = [232, 226, 210]; | |
| 78 | const TANK = [150, 110, 76], TANK_BAND = [74, 70, 70], STEEL = [150, 156, 166]; | |
| 79 | const WALKER = { shirt: [222, 96, 64], stripe: [240, 230, 210], legs: [44, 52, 82], hair: [52, 36, 30], skin: [222, 170, 132] }; | |
| 80 | const ROOFS = [{ x0: -16.5, x1: -9.6, y0: -5, y1: 5 }, { x0: 9.6, x1: 16.5, y0: -5, y1: 5 }]; | |
| 81 | const WALL = 6.5, WZ = .95, WX0 = -9.2, WX1 = 9.2, NET_Z = -2.6, NET_SAG = 2.2; | |
| 82 | const CHIMNEYS = [[-13.2, -3, 4.6], [-14.2, 2.6, 3.2], [12.2, 3, 3.6]]; | |
| 83 | const TANK_AT = [13.4, -2.6]; | |
| 84 | const FLAGS = [[-13.2, -3, 4.6 + .3], [12.2, 3, 3.6 + .3]]; | |
| 85 | const BEST_KEY = 'pixel-plays-tightrope-best'; | |
| 86 | const FALL_TURN = 1.35; | |
| 87 | ||
| 88 | const wireX = pos => lerp(WX0, WX1, pos); | |
| 89 | const netZ = x => NET_Z - NET_SAG * Math.max(0, 1 - Math.pow(x / WX1, 2)); | |
| 90 | const inRoof = (x, y, m = 0) => ROOFS.some(r => x >= r.x0 - m && x <= r.x1 + m && y >= r.y0 - m && y <= r.y1 + m); | |
| 91 | ||
| 92 | /* ---------- state ---------- */ | |
| 93 | const best0 = (() => { try { return Number(localStorage.getItem(BEST_KEY)) || 0; } catch (e) { return 0; } })(); | |
| 94 | let R = Wire.newRound(best0, Date.now() & 0xffff); | |
| 95 | const G = { time: 0, status: '', noteUntil: 0, keys: new Set(), pointers: new Map(), steps: 0 }; | |
| 96 | const nodes = { score: document.getElementById('score'), falls: document.getElementById('falls'), best: document.getElementById('best'), status: document.getElementById('status'), meter: document.getElementById('meter'), lean: document.getElementById('lean') }; | |
outdated
|
||
| 97 | function renderHud() { | |
| 98 | nodes.score.textContent = R.score; | |
| 99 | nodes.falls.textContent = R.falls; | |
| 100 | nodes.best.textContent = Math.max(R.best, R.score); | |
| 101 | } | |
| 102 | function setStatus(text) { | |
| 103 | if (text === G.status) return; | |
| 104 | G.status = text; | |
| 105 | nodes.status.textContent = text; | |
| 106 | } | |
| 107 | function note(text) { setStatus(text); G.noteUntil = G.time + 2.4; } | |
| 108 | const HINT = 'hold left or right to lean · wind shows in the grass first'; | |
| 109 | ||
| 110 | /* ---------- baked scene ---------- */ | |
| 111 | function groundColor(x, y, ix, iy, i) { | |
| 112 | if (!inRoof(x, y)) return 0; | |
| 113 | const rim = !inRoof(x, y, -.35); | |
| 114 | if (rim) return rgb(...CAP); | |
| 115 | const patch = noise(x * .55 + 4, y * .55 - 2); | |
| 116 | if (patch > .56) { kinds[i] = KIND_GRASS; const t = .9 + (hash(ix, iy) - .5) * .16; return rgb(98 * t, 134 * t, 70 * t); } | |
| 117 | const tile = ((Math.floor((x + 40) / 1.2) + Math.floor((y + 40) / 1.2)) & 1) ? .045 : 0; | |
| 118 | const t = .9 + (hash(ix, iy) - .5) * .08 + tile; | |
| 119 | return rgb(TAR[0] * t, TAR[1] * t, TAR[2] * t); | |
| 120 | } | |
| 121 | function chimney(x, y, h) { | |
| 122 | box(x - .7, y - .7, 0, x + .7, y + .7, h, BRICK, Math.round(x * 3)); | |
| 123 | box(x - .9, y - .9, h, x + .9, y + .9, h + .3, CAP, 5); | |
| 124 | for (let z = .3; z <= h + .3; z += .9) castShadow(x, y, z, .55, 1, 0); | |
| 125 | } | |
| 126 | function waterTank() { | |
| 127 | const [x, y] = TANK_AT; | |
| 128 | for (const [dx, dy] of [[-.9, -.9], [.9, -.9], [-.9, .9], [.9, .9]]) capsule([x + dx, y + dy, 0], [x + dx * .8, y + dy * .8, 2], .12, .12, () => STEEL, 'solid'); | |
| 129 | capsule([x, y, 2.3], [x, y, 4.1], 1.35, 1.35, z => (Math.abs(((z - 2.3) % 1.1) - .55) > .48 ? TANK_BAND : TANK), 'rough'); | |
| 130 | sphere(x, y, 4.1, 1.35, TANK, 'rough'); | |
| 131 | capsule([x, y, 5.3], [x, y, 5.7], .2, .2, () => STEEL, 'solid'); | |
| 132 | for (let z = .4; z <= 5.4; z += .8) castShadow(x, y, z, .9, 1, 0); | |
| 133 | } | |
| 134 | function bakeNet() { | |
| 135 | const line = (a, b) => capsule(a, b, .05, .05, () => ROPE, 'solid'); | |
| 136 | const step = 1.6; | |
| 137 | for (let x = -9.6; x <= 9.61; x += step) for (let y = -4.4; y < 4.4; y += step) { | |
| 138 | line([x, y, netZ(x)], [x, y + step, netZ(x)]); | |
| 139 | line([x, y, netZ(x)], [Math.min(9.6, x + step), y, netZ(Math.min(9.6, x + step))]); | |
| 140 | } | |
| 141 | for (let x = -9.6; x < 9.6; x += step) line([x, 4.4, netZ(x)], [Math.min(9.6, x + step), 4.4, netZ(Math.min(9.6, x + step))]); | |
| 142 | } | |
| 143 | function bakeWire() { | |
| 144 | for (const x of [WX0, WX1]) { | |
| 145 | capsule([x, 0, 0], [x, 0, WZ + .5], .16, .12, () => STEEL, 'solid'); | |
| 146 | castShadow(x, 0, .6, .3, 1, 0); | |
| 147 | } | |
| 148 | capsule([WX0, 0, WZ], [WX1, 0, WZ], .06, .06, () => WIRE_COLOR, 'solid'); | |
| 149 | // tape marks at the two checkpoints past the start | |
| 150 | for (const m of Wire.MARKS.slice(1)) { | |
| 151 | const x = wireX(m); | |
| 152 | capsule([x - .12, 0, WZ], [x + .12, 0, WZ], .17, .17, () => TAPE, 'solid'); | |
| 153 | capsule([x, 0, 0], [x, 0, WZ - .1], .04, .04, () => TAPE, 'solid'); | |
| 154 | } | |
| 155 | } | |
| 156 | function bake() { | |
| 157 | ground.fill(0); shadowMask.fill(0); staticShadow.fill(0); kinds.fill(0); | |
| 158 | tufts.length = 0; leaves.length = 0; | |
| 159 | zb.fill(-1e9); | |
| 160 | pix.fill(rgb(255, 255, 255)); | |
| 161 | for (let iy = 0; iy < H; iy++) for (let ix = 0; ix < W; ix++) { | |
| 162 | const g = unproject(ix + .5, iy + .5), i = iy * W + ix, c = groundColor(g.x, g.y, ix, iy, i); | |
| 163 | if (!c) continue; | |
| 164 | pix[i] = c; ground[i] = 1; | |
| 165 | zb[i] = (g.x + g.y) / R2 * COS; | |
| 166 | } | |
| 167 | growGrass(); | |
| 168 | groundZ.set(zb); | |
| 169 | // the building walls drop away under each roof; the street is far below, off the canvas | |
| 170 | for (const r of ROOFS) box(r.x0, r.y0, -WALL, r.x1, r.y1, -.06, BRICK, 3); | |
| 171 | bakeNet(); | |
| 172 | bakeWire(); | |
| 173 | CHIMNEYS.forEach(([x, y, h]) => chimney(x, y, h)); | |
| 174 | waterTank(); | |
| 175 | for (const [x, y, h] of FLAGS) capsule([x, y, h - .3], [x, y, h + 1.9], .05, .05, () => STEEL, 'solid'); | |
| 176 | for (const r of ROOFS) for (let y = r.y0; y <= r.y1; y += .9) castShadow(r.x1 + .1, y, .1, .3, 1, 0); | |
| 177 | bakeOcclusion(); | |
| 178 | staticShadow.set(shadowMask); | |
| 179 | applyShadows(groundZ); | |
| 180 | bakedPix.set(pix); | |
| 181 | bakedZ.set(zb); | |
| 182 | } | |
| 183 | ||
| 184 | /* ---------- wind: grass and flags ---------- */ | |
| 185 | // -1..1: how far the wind is bending things right now. A gust is half-shown while it is announced and full while it blows. | |
| 186 | function windNow() { | |
| 187 | const w = Wire.warning(R); | |
| 188 | if (w) return w.dir * (.45 + .2 * (1 - w.left / Wire.WARN)); | |
| 189 | const g = Wire.gustNow(R); | |
| 190 | return g ? Math.sign(g) : 0; | |
| 191 | } | |
| 192 | function drawGrass(t, wind) { | |
| 193 | for (const tuft of tufts) { | |
| 194 | const sway = STILL ? 0 : Math.sin(t * .8 + tuft.phase) + .5 * Math.sin(t * 2.1 + tuft.phase * 2.3); | |
| 195 | const colors = staticShadow[tuft.i] ? tuftDark : tuftLit; | |
| 196 | for (let k = 1; k <= tuft.height; k++) { | |
| 197 | const jx = tuft.ix + Math.round(sway * .95 * k / 3 + wind * k * .85), jy = tuft.iy - k; | |
| 198 | if (jx < 0 || jx >= W) continue; | |
| 199 | const j = jy * W + jx; | |
| 200 | if ((kinds[j] !== KIND_GRASS && kinds[j] !== KIND_NONE) || zb[j] !== groundZ[j]) continue; | |
| 201 | pix[j] = tuft.bloom && k === tuft.height ? tuft.bloom : colors[clamp(tuft.base + k + (sway > 1 ? 1 : 0), 1, 4)]; | |
| 202 | } | |
| 203 | } | |
| 204 | } | |
| 205 | // a pennant on a pole: hangs slack, then streams in the wind's direction; the flutter steps at 8 fps and stops when motion is reduced | |
| 206 | function drawFlag(at, wind, t) { | |
| 207 | const p = project(at[0], at[1], at[2] + 1.6), px = Math.round(p.sx), py = Math.round(p.sy), dir = wind < 0 ? -1 : 1, power = Math.abs(wind); | |
| 208 | const len = 4 + Math.round(power * 8), ink = rgb(28, 27, 25), red = rgb(...TAPE), step = STILL ? 0 : Math.floor(t * WIND_FPS); | |
| 209 | for (let k = 1; k <= len; k++) { | |
outdated
Still unmet: the comment says the pole turns the other way from the lean, but |
||
| 210 | const flutter = power > .1 && !STILL ? Math.round(Math.sin(step * 1.7 + k * .9) * (.6 + power)) : 0; | |
| 211 | const droop = Math.round((1 - power) * k * .55) + flutter; | |
| 212 | const x = px + dir * k, hang = Math.round(2 + (len - k) * .15); | |
| 213 | for (let h = 0; h < hang; h++) { | |
| 214 | const y = py + droop + h; | |
| 215 | if (x >= 0 && x < W && y >= 0 && y < H) pix[y * W + x] = h === 0 || h === hang - 1 ? ink : red; | |
| 216 | } | |
| 217 | } | |
| 218 | } | |
| 219 | ||
| 220 | /* ---------- the walker ---------- */ | |
| 221 | // lean: degrees, positive to screen-right, which is -y in the world | |
| 222 | function walker(x, z, leanDeg, stepPhase) { | |
| 223 | const a = leanDeg * Math.PI / 180, side = v => -v; | |
| 224 | const stride = Math.sin(stepPhase) * .32, lift = Math.max(0, Math.cos(stepPhase)) * .12; | |
| 225 | const hip = [x, 0, z + 1.15]; | |
| 226 | const sh = [x, side(Math.sin(a)) * 1.0, hip[2] + Math.cos(a) * 1.0]; | |
outdated
|
||
| 227 | const head = [x, side(Math.sin(a)) * 1.34, hip[2] + Math.cos(a) * 1.34 + .1]; | |
| 228 | const footB = [x - stride, side(-.04), z + .12 + (stride > 0 ? 0 : lift)], footF = [x + stride, side(.04), z + .12 + (stride > 0 ? lift : 0)]; | |
| 229 | const knee = (f) => [(hip[0] + f[0]) / 2, (hip[1] + f[1]) / 2, (hip[2] + f[2]) / 2 + .05]; | |
| 230 | const shirt = h => (Math.floor(h * 3.2) % 2 ? WALKER.shirt : WALKER.stripe); | |
| 231 | dynamicAdd(hip, sh, .3, .26, shirt); | |
| 232 | dynamicAdd(sh, head, .09, .06, () => WALKER.skin); | |
| 233 | sphere2(dynamicAdd, [head[0], head[1], head[2] + .12], .24, WALKER.skin); | |
| 234 | dynamicAdd([head[0], head[1], head[2] + .2], [head[0] - .05, head[1], head[2] + .27], .26, .2, () => WALKER.hair); | |
| 235 | dynamicAdd([x - stride * .3, side(-.12), hip[2]], footB, .13, .11, () => WALKER.legs); | |
| 236 | dynamicAdd([x + stride * .3, side(.12), hip[2]], footF, .13, .11, () => WALKER.legs); | |
| 237 | // the balance pole tips against the lean, at under half its angle | |
| 238 | const pa = -a * .45, half = 2.7, ps = Math.sin(pa), pc = Math.cos(pa); | |
outdated
|
||
| 239 | const mid = [x, side(Math.sin(a)) * .9, hip[2] + Math.cos(a) * .85]; | |
| 240 | const endR = [x, mid[1] + side(pc) * half, mid[2] - ps * half], endL = [x, mid[1] - side(pc) * half, mid[2] + ps * half]; | |
| 241 | dynamicAdd(endL, endR, .06, .06, () => POLE); | |
| 242 | dynamicAdd(endL, [endL[0], endL[1] + (endR[1] - endL[1]) * .08, endL[2] + (endR[2] - endL[2]) * .08], .1, .1, () => POLE_TIP); | |
| 243 | dynamicAdd([endR[0], endR[1], endR[2]], [endR[0], endR[1] + (endL[1] - endR[1]) * .08, endR[2] + (endL[2] - endR[2]) * .08], .1, .1, () => POLE_TIP); | |
| 244 | dynamicAdd(sh, mid, .08, .07, () => WALKER.skin); | |
| 245 | return mid; | |
| 246 | } | |
| 247 | // where the walker is and how it looks this frame, from the round | |
| 248 | function pose() { | |
| 249 | const t = R.timer; | |
| 250 | if (R.phase === 'fall') { | |
| 251 | const x = wireX(R.pos), side = R.lean < 0 ? -1 : 1, e = Math.min(1, t / FALL_TURN); | |
| 252 | const drop = Math.min(1, t / .55), floorZ = netZ(x) + .15; | |
| 253 | const bounce = t > .55 ? Math.abs(Math.sin((t - .55) * 7)) * .8 * Math.max(0, 1 - (t - .55) / .8) : 0; | |
| 254 | const z = lerp(WZ, floorZ, drop * drop) + bounce; | |
| 255 | return { x, z, lean: R.lean + side * (e * 70), phase: 1.2 }; | |
| 256 | } | |
| 257 | if (R.phase === 'arrive') { | |
| 258 | const e = Math.min(1, t / .7); | |
| 259 | return { x: WX1 + e * 2.2, z: WZ * (1 - e), lean: lerp(R.lean, 0, e), phase: G.steps }; | |
| 260 | } | |
| 261 | return { x: wireX(R.pos), z: WZ, lean: R.lean, phase: G.steps }; | |
| 262 | } | |
| 263 | ||
| 264 | function drawMeter() { | |
| 265 | const f = clamp(R.lean / Wire.LIMIT, -1, 1); | |
| 266 | nodes.meter.style.transform = `translateX(${(f * 56).toFixed(1)}px)`; | |
| 267 | const w = Wire.warning(R); | |
| 268 | nodes.lean.dataset.warn = w ? String(w.dir) : '0'; | |
| 269 | } | |
| 270 | function draw() { | |
| 271 | pix.set(bakedPix); | |
| 272 | zb.set(bakedZ); | |
| 273 | pendingShadows.length = 0; | |
| 274 | const windT = STILL ? 0 : Math.floor(G.time * WIND_FPS) / WIND_FPS, wind = windNow(); | |
| 275 | drawGrass(windT, wind); | |
| 276 | const p = pose(); | |
| 277 | walker(p.x, p.z, p.lean, p.phase); | |
| 278 | flushShadows(); | |
| 279 | applyShadows(bakedZ); | |
| 280 | shadeEdges(); | |
| 281 | for (const f of FLAGS) drawFlag(f, wind, windT); | |
| 282 | ctx.putImageData(image, 0, 0); | |
| 283 | drawMeter(); | |
| 284 | } | |
| 285 | ||
| 286 | /* ---------- game ---------- */ | |
| 287 | function act(events) { | |
| 288 | if (!events) return; | |
| 289 | for (const e of events) { | |
| 290 | if (e.type === 'gust') sound('wind'); | |
| 291 | if (e.type === 'mark') { note(`mark ${e.mark + 1} of 3`); sound('tick'); } | |
| 292 | if (e.type === 'fall') { note(`fall ${e.falls}: into the net`); sound('buzz'); } | |
| 293 | if (e.type === 'cross') { note(`across · +${e.points}${e.steady ? ' · steady' : ''}`); sound('chime'); } | |
| 294 | if (e.type === 'restart') note(e.mark ? `back to mark ${e.mark + 1}` : `crossing ${R.crossings + 1} of 3`); | |
| 295 | if (e.type === 'over') { | |
| 296 | if (R.record) try { localStorage.setItem(BEST_KEY, String(R.best)); } catch (err) { /* private window: keep it for this visit */ } | |
| 297 | setStatus(`${e.reason === 'falls' ? 'three falls' : 'three crossings'} · ${R.score}${R.record ? ' · new best' : ` · best ${R.best}`} · tap or press a key to walk again`); | |
| 298 | } | |
| 299 | } | |
| 300 | if (events.length) renderHud(); | |
outdated
Still unmet: |
||
| 301 | } | |
| 302 | function restart() { | |
| 303 | const next = Wire.again(R); | |
| 304 | if (!next) return false; | |
| 305 | R = next; | |
| 306 | syncInput(); | |
| 307 | note(HINT); | |
| 308 | renderHud(); | |
| 309 | return true; | |
| 310 | } | |
| 311 | function syncInput() { | |
| 312 | let dir = 0; | |
| 313 | for (const side of G.pointers.values()) dir += side; | |
| 314 | if (G.keys.has('left')) dir -= 1; | |
| 315 | if (G.keys.has('right')) dir += 1; | |
| 316 | Wire.setInput(R, dir); | |
| 317 | } | |
| 318 | function update(dt) { | |
| 319 | G.time += dt; | |
outdated
|
||
| 320 | if (R.phase === 'walk') G.steps += dt * 5.2; | |
| 321 | act(Wire.advance(R, dt)); | |
| 322 | if (R.phase !== 'walk' || G.time < G.noteUntil) return; | |
| 323 | const w = Wire.warning(R); | |
| 324 | const text = w ? `gust from the ${w.dir > 0 ? 'left, tipping you right' : 'right, tipping you left'}` : `crossing ${R.crossings + 1} of 3 · ${Math.round(R.pos * 100)}%`; | |
| 325 | setStatus(text); | |
| 326 | } | |
| 327 | ||
| 328 | /* ---------- input ---------- */ | |
| 329 | const sideOf = e => { | |
| 330 | const rect = canvas.getBoundingClientRect(); | |
| 331 | return e.clientX < rect.left + rect.width / 2 ? -1 : 1; | |
| 332 | }; | |
| 333 | canvas.addEventListener('pointerdown', e => { | |
| 334 | ensureAudio(); | |
| 335 | canvas.focus({ preventScroll: true }); | |
| 336 | try { canvas.setPointerCapture(e.pointerId); } catch (err) { /* synthetic pointers have nothing to capture */ } | |
| 337 | if (restart() || R.phase === 'over') return; | |
| 338 | G.pointers.set(e.pointerId, sideOf(e)); | |
| 339 | syncInput(); | |
| 340 | }); | |
| 341 | canvas.addEventListener('pointermove', e => { | |
| 342 | if (!G.pointers.has(e.pointerId)) return; | |
| 343 | G.pointers.set(e.pointerId, sideOf(e)); | |
| 344 | syncInput(); | |
| 345 | }); | |
| 346 | const lift = e => { if (G.pointers.delete(e.pointerId)) syncInput(); }; | |
| 347 | canvas.addEventListener('pointerup', lift); | |
| 348 | canvas.addEventListener('pointercancel', lift); | |
| 349 | const KEYS = { ArrowLeft: 'left', a: 'left', A: 'left', ArrowRight: 'right', d: 'right', D: 'right' }; | |
| 350 | canvas.addEventListener('keydown', e => { | |
| 351 | const side = KEYS[e.key]; | |
| 352 | if (!side && e.key !== ' ' && e.key !== 'Enter') return; | |
| 353 | e.preventDefault(); | |
| 354 | ensureAudio(); | |
| 355 | if (e.repeat) return; | |
| 356 | if (restart()) return; | |
| 357 | if (side) { G.keys.add(side); syncInput(); } | |
| 358 | }); | |
| 359 | canvas.addEventListener('keyup', e => { | |
| 360 | const side = KEYS[e.key]; | |
| 361 | if (!side) return; | |
| 362 | G.keys.delete(side); | |
| 363 | syncInput(); | |
| 364 | }); | |
| 365 | canvas.addEventListener('blur', () => { G.keys.clear(); G.pointers.clear(); syncInput(); }); | |
| 366 | ||
| 367 | /* ---------- sound ---------- */ | |
| 368 | let audio = null, muted = false; | |
| 369 | function ensureAudio() { | |
| 370 | if (audio) { if (audio.state === 'suspended') audio.resume(); return; } | |
| 371 | const AC = window.AudioContext || window.webkitAudioContext; | |
| 372 | if (AC) audio = new AC(); | |
| 373 | } | |
| 374 | function sound(kind) { | |
| 375 | if (!audio || muted) return; | |
| 376 | const now = audio.currentTime, gain = audio.createGain(), osc = audio.createOscillator(); | |
| 377 | const spec = { | |
| 378 | tick: [700, 1000, .07, .03, 'triangle'], | |
| 379 | chime: [520, 880, .3, .07, 'sine'], | |
| 380 | buzz: [200, 70, .35, .07, 'sawtooth'], | |
| 381 | wind: [260, 180, .5, .018, 'triangle'], | |
| 382 | }[kind]; | |
| 383 | osc.type = spec[4]; | |
| 384 | osc.frequency.setValueAtTime(spec[0], now); | |
| 385 | osc.frequency.exponentialRampToValueAtTime(spec[1], now + spec[2]); | |
| 386 | gain.gain.setValueAtTime(spec[3], now); | |
| 387 | gain.gain.exponentialRampToValueAtTime(.0005, now + spec[2]); | |
| 388 | osc.connect(gain).connect(audio.destination); | |
| 389 | osc.start(now); osc.stop(now + spec[2] + .01); | |
| 390 | } | |
| 391 | const muteButton = document.getElementById('mute'); | |
| 392 | muteButton.addEventListener('click', () => { | |
| 393 | muted = !muted; | |
| 394 | muteButton.textContent = muted ? 'sound off' : 'sound on'; | |
| 395 | muteButton.setAttribute('aria-pressed', String(muted)); | |
| 396 | }); | |
| 397 | ||
| 398 | /* ---------- fit and boot ---------- */ | |
| 399 | addEventListener('resize', fit); | |
| 400 | fit(); | |
| 401 | bake(); | |
| 402 | note(HINT); | |
| 403 | renderHud(); | |
| 404 | canvas.focus({ preventScroll: true }); | |
| 405 | let last = performance.now(); | |
| 406 | function frame(now) { | |
| 407 | update(Math.min(.04, (now - last) / 1000)); | |
| 408 | last = now; | |
| 409 | draw(); | |
| 410 | requestAnimationFrame(frame); | |
| 411 | } | |
| 412 | requestAnimationFrame(frame); | |
| 413 | </script> | |
| 414 | </body> | |
| 415 | </html> |
tightrope/wire.js added +136
| @@ -0,0 +1,136 @@ | ||
| 1 | 'use strict'; | |
| 2 | // The tightrope rules: a lean angle that falls away from centre, wind gusts announced one beat early, a held | |
| 3 | // lean that overcorrects, a net below, three marks along the wire, and a round of three crossings or three falls. | |
| 4 | // Plain numbers in, plain numbers out, no DOM. The page draws what this decides. | |
| 5 | (function (root) { | |
| 6 | const LIMIT = 30; // degrees of lean past which the walker drops | |
| 7 | const STEADY = LIMIT / 2; // a crossing that never passes this earns the steady bonus | |
| 8 | const GRAVITY = 1.4; // deg/s^2 per degree off centre: the further over, the faster it falls | |
| 9 | const DAMPING = 1.1; // per second, bleeds lean speed | |
| 10 | const PUSH = 15; // deg/s^2 from holding a lean key ... | |
| 11 | const PUSH_GAIN = .9, PUSH_RAMP = 1.2; // ... which grows by this much per second held, up to PUSH_RAMP seconds' worth | |
| 12 | const CROSS_TIME = 26; // seconds to walk the wire at a steady pace | |
| 13 | const MARKS = [0, 1 / 3, 2 / 3]; // where a fall restarts: the last mark passed | |
| 14 | const WARN = 1.4; // seconds a gust shows in the grass and flag before it hits | |
| 15 | const GUST_FIRST = 4, GUST_GAP = 3, GUST_GAP_VAR = 3; | |
| 16 | const GUST_FORCE = 4.5, GUST_FORCE_VAR = 2, GUST_TIME = 1.3, GUST_TIME_VAR = .8; | |
| 17 | const CROSSINGS = 3, MAX_FALLS = 3; | |
| 18 | const BASE = 10, STEADY_BONUS = 5, PAR = 60; | |
| 19 | const FALL_TIME = 1.4, ARRIVE_TIME = 1.6, RESTART_AFTER = .6; | |
| 20 | ||
| 21 | const finite = v => (Number.isFinite(v) ? v : 0); | |
| 22 | const clamp = (v, a, b) => Math.max(a, Math.min(b, v)); | |
| 23 | function roll(s) { s.rng = (Math.imul(s.rng, 1664525) + 1013904223) >>> 0; return s.rng / 4294967296; } | |
| 24 | ||
| 25 | function markFor(pos) { | |
| 26 | let m = 0; | |
| 27 | MARKS.forEach((at, i) => { if (finite(pos) >= at) m = i; }); | |
| 28 | return m; | |
| 29 | } | |
| 30 | // schedule the next gust `wait` seconds from now | |
| 31 | function planGust(s, wait) { | |
| 32 | const dir = roll(s) < .5 ? -1 : 1; | |
| 33 | s.gust = s.calm ? null : { at: s.clock + wait, dir, force: GUST_FORCE + roll(s) * GUST_FORCE_VAR, dur: GUST_TIME + roll(s) * GUST_TIME_VAR }; | |
| 34 | } | |
| 35 | // a fresh start on the wire: centred but for a small nudge the walker has to answer | |
| 36 | function setOff(s, pos) { | |
| 37 | s.pos = pos; s.mark = markFor(pos); | |
| 38 | s.lean = (roll(s) < .5 ? -1 : 1) * (1 + roll(s)); s.vel = 0; s.held = 0; | |
| 39 | planGust(s, GUST_FIRST); | |
| 40 | } | |
| 41 | ||
| 42 | function newRound(best = 0, seed = 0, opts = {}) { | |
| 43 | const s = { | |
| 44 | phase: 'walk', clock: 0, timer: 0, crossTime: 0, pos: 0, mark: 0, lean: 0, vel: 0, input: 0, held: 0, | |
| 45 | peak: 0, crossings: 0, falls: 0, score: 0, reason: '', gust: null, | |
| 46 | best: Math.max(0, finite(best)), record: false, seed: finite(seed) >>> 0, rng: (finite(seed) >>> 0) || 1, calm: !!opts.calm, | |
| 47 | }; | |
| 48 | setOff(s, 0); | |
| 49 | return s; | |
| 50 | } | |
| 51 | const timeLeft = s => Math.max(0, PAR - s.crossTime); | |
| 52 | // seconds until the next gust lands while it is still announced, else null; with its direction | |
| 53 | function warning(s) { | |
| 54 | const g = s.gust; | |
| 55 | if (!g || s.phase !== 'walk') return null; | |
| 56 | const left = g.at - s.clock; | |
| 57 | return left > 0 && left <= WARN ? { dir: g.dir, left } : null; | |
| 58 | } | |
| 59 | // the wind acting on the lean now, in deg/s^2 (sign is the direction) | |
| 60 | function gustNow(s) { | |
| 61 | const g = s.gust; | |
| 62 | if (!g || s.phase !== 'walk') return 0; | |
| 63 | const t = s.clock - g.at; | |
| 64 | return t >= 0 && t < g.dur ? g.dir * g.force : 0; | |
| 65 | } | |
| 66 | // -1 leans back left, 1 right, 0 lets go | |
| 67 | function setInput(s, dir) { | |
| 68 | const d = dir > 0 ? 1 : dir < 0 ? -1 : 0; | |
| 69 | if (d !== s.input) s.held = 0; | |
| 70 | s.input = d; | |
| 71 | } | |
| 72 | ||
| 73 | function finish(s, reason, events) { | |
| 74 | s.phase = 'over'; s.reason = reason; s.timer = 0; | |
| 75 | if (s.score > s.best) { s.best = s.score; s.record = true; } | |
| 76 | events.push({ type: 'over', reason, score: s.score, record: s.record }); | |
| 77 | } | |
| 78 | function fall(s, events) { | |
| 79 | const side = s.lean < 0 ? -1 : 1; | |
| 80 | s.falls++; s.phase = 'fall'; s.timer = 0; s.vel = 0; s.gust = null; | |
| 81 | events.push({ type: 'fall', side, mark: s.mark, falls: s.falls }); | |
| 82 | } | |
| 83 | function cross(s, events) { | |
| 84 | const steady = s.peak <= STEADY, seconds = Math.floor(timeLeft(s)); | |
| 85 | const points = BASE + seconds + (steady ? STEADY_BONUS : 0); | |
| 86 | s.score += points; s.crossings++; s.phase = 'arrive'; s.timer = 0; s.pos = 1; s.gust = null; | |
outdated
|
||
| 87 | events.push({ type: 'cross', points, seconds, steady, crossings: s.crossings }); | |
| 88 | } | |
| 89 | ||
| 90 | function advance(s, dt) { | |
| 91 | const events = []; | |
| 92 | dt = clamp(finite(dt), 0, 1); | |
| 93 | if (s.phase === 'over') { s.timer += dt; return events; } | |
| 94 | if (s.phase === 'fall' || s.phase === 'arrive') { | |
| 95 | s.timer += dt; s.crossTime += s.phase === 'fall' ? dt : 0; s.clock += dt; | |
| 96 | if (s.phase === 'fall' && s.timer >= FALL_TIME) { | |
| 97 | if (s.falls >= MAX_FALLS) { finish(s, 'falls', events); return events; } | |
| 98 | s.phase = 'walk'; s.peak = 0; setOff(s, MARKS[s.mark]); | |
| 99 | events.push({ type: 'restart', mark: s.mark }); | |
| 100 | } else if (s.phase === 'arrive' && s.timer >= ARRIVE_TIME) { | |
| 101 | if (s.crossings >= CROSSINGS) { finish(s, 'crossed', events); return events; } | |
| 102 | s.phase = 'walk'; s.crossTime = 0; s.peak = 0; setOff(s, 0); | |
| 103 | events.push({ type: 'restart', mark: 0 }); | |
| 104 | } | |
| 105 | return events; | |
| 106 | } | |
| 107 | s.clock += dt; s.crossTime += dt; | |
| 108 | // the lean keys: strength grows the longer one is held, so a long hold carries the walker past centre | |
| 109 | if (s.input) s.held += dt; | |
| 110 | const push = s.input * PUSH * (1 + PUSH_GAIN * Math.min(s.held, PUSH_RAMP)); | |
| 111 | const wind = gustNow(s); | |
| 112 | const before = s.gust && s.clock - dt < s.gust.at ? s.gust : null; | |
| 113 | s.vel += (GRAVITY * s.lean + wind + push) * dt; | |
| 114 | s.vel -= DAMPING * s.vel * dt; | |
| 115 | s.lean += s.vel * dt; | |
| 116 | s.peak = Math.max(s.peak, Math.abs(s.lean)); | |
| 117 | if (before && s.clock >= before.at) events.push({ type: 'gust', dir: before.dir }); | |
| 118 | if (s.gust && s.clock >= s.gust.at + s.gust.dur) planGust(s, GUST_GAP + roll(s) * GUST_GAP_VAR); | |
| 119 | if (Math.abs(s.lean) >= LIMIT) { fall(s, events); return events; } | |
| 120 | s.pos = Math.min(1, s.pos + dt / CROSS_TIME); | |
| 121 | const m = markFor(s.pos); | |
| 122 | if (m > s.mark) { s.mark = m; events.push({ type: 'mark', mark: m }); } | |
| 123 | if (s.pos >= 1) cross(s, events); | |
| 124 | return events; | |
| 125 | } | |
| 126 | // a tap after the round has been over for a moment starts the next one; otherwise null | |
| 127 | function again(s) { | |
| 128 | return s.phase === 'over' && s.timer > RESTART_AFTER ? newRound(s.best, s.seed + 1, { calm: s.calm }) : null; | |
| 129 | } | |
| 130 | ||
| 131 | const api = { | |
| 132 | LIMIT, STEADY, GRAVITY, PUSH, CROSS_TIME, MARKS, WARN, GUST_FIRST, GUST_GAP, CROSSINGS, MAX_FALLS, BASE, STEADY_BONUS, PAR, FALL_TIME, ARRIVE_TIME, RESTART_AFTER, | |
| 133 | markFor, newRound, setInput, advance, again, timeLeft, warning, gustNow, | |
| 134 | }; | |
| 135 | if (typeof module !== 'undefined') module.exports = api; else root.Wire = api; | |
| 136 | })(typeof window !== 'undefined' ? window : globalThis); |
tightrope/wire.test.mjs added +226
| @@ -0,0 +1,226 @@ | ||
| 1 | import { test, expect } from 'bun:test'; | |
| 2 | import { LIMIT, STEADY, MARKS, WARN, GUST_FIRST, GUST_GAP, CROSS_TIME, MAX_FALLS, FALL_TIME, CROSSINGS, markFor, newRound, setInput, advance, again, timeLeft, warning, gustNow } from './wire.js'; | |
| 3 | ||
| 4 | const run = (s, seconds, dt = .02) => { const ev = []; for (let t = 0; t < seconds - 1e-9; t += dt) ev.push(...advance(s, dt)); return ev; }; | |
| 5 | // a walker who steadies themselves: lean back against the lean and its speed | |
| 6 | function steady(s, seconds) { | |
| 7 | const ev = []; | |
| 8 | for (let t = 0; t < seconds - 1e-9; t += .02) { | |
| 9 | const e = s.lean + .3 * s.vel; | |
| 10 | setInput(s, Math.abs(e) > 4 ? (e > 0 ? -1 : 1) : 0); | |
| 11 | ev.push(...advance(s, .02)); | |
| 12 | } | |
| 13 | return ev; | |
| 14 | } | |
| 15 | ||
| 16 | test('with no input the lean grows away from centre, faster the further off', () => { | |
| 17 | for (const sign of [-1, 1]) { | |
| 18 | const s = newRound(0, 1, { calm: true }); | |
| 19 | s.lean = 3 * sign; | |
| 20 | run(s, .5); | |
| 21 | expect(Math.abs(s.lean)).toBeGreaterThan(3); | |
| 22 | expect(Math.sign(s.lean)).toBe(sign); | |
| 23 | } | |
| 24 | const near = newRound(0, 1, { calm: true }), far = newRound(0, 1, { calm: true }); | |
| 25 | near.lean = 2; far.lean = 8; | |
| 26 | run(near, .3); run(far, .3); | |
| 27 | expect(far.lean - 8).toBeGreaterThan(near.lean - 2); | |
| 28 | }); | |
| 29 | ||
| 30 | test('a gust pushes the lean in its direction', () => { | |
| 31 | for (const dir of [-1, 1]) { | |
| 32 | const windy = newRound(0, 1), still = newRound(0, 1, { calm: true }); | |
| 33 | windy.lean = still.lean = 0; | |
| 34 | windy.gust = { at: 0, dir, force: 6, dur: 2 }; | |
| 35 | run(windy, .6); run(still, .6); | |
| 36 | expect(gustNow(windy)).toBe(dir * 6); | |
| 37 | expect(Math.sign(windy.lean)).toBe(dir); | |
| 38 | expect(Math.abs(windy.lean)).toBeGreaterThan(Math.abs(still.lean)); | |
| 39 | } | |
| 40 | }); | |
| 41 | ||
| 42 | test('a gust is announced before it lands, and not after', () => { | |
| 43 | const s = newRound(0, 3); | |
| 44 | s.gust = { at: 5, dir: -1, force: 5, dur: 1.5 }; | |
| 45 | s.clock = 5 - WARN - .1; | |
| 46 | expect(warning(s)).toBe(null); | |
| 47 | s.clock = 5 - WARN + .1; | |
| 48 | expect(warning(s)).toMatchObject({ dir: -1 }); | |
| 49 | expect(gustNow(s)).toBe(0); | |
| 50 | s.clock = 5.1; | |
| 51 | expect(warning(s)).toBe(null); | |
| 52 | expect(gustNow(s)).toBe(-5); | |
| 53 | }); | |
| 54 | ||
| 55 | test('the first gust is announced well after the walker steps out, and every later one has a gap', () => { | |
This test name says first-gust delay and gaps, but the body checks neither. Assert the clock of each Still unmet from last round: this test only checks the count and direction of the gusts. Assert their timing (first at or after |
||
| 56 | // a gust is seen when the wind starts acting on the lean, whether or not an event announced it | |
| 57 | const s = newRound(0, 7), gusts = []; | |
| 58 | let warnedSince = null, blowing = false; | |
| 59 | for (let t = 0; t < 30 - 1e-9; t += .02) { | |
| 60 | const e = s.lean + .3 * s.vel; | |
| 61 | setInput(s, Math.abs(e) > 4 ? (e > 0 ? -1 : 1) : 0); | |
| 62 | const w = warning(s), dur = s.gust && s.gust.dur, from = s.clock; | |
| 63 | if (w && warnedSince === null) warnedSince = from; | |
| 64 | if (!w) warnedSince = null; | |
| 65 | const before = warnedSince, events = advance(s, .02), wind = gustNow(s); | |
| 66 | if (wind && !blowing) gusts.push({ at: s.clock, dir: Math.sign(wind), dur, warnedSince: before, event: events.find(ev => ev.type === 'gust') }); | |
| 67 | blowing = !!wind; | |
| 68 | } | |
| 69 | expect(s.falls).toBe(0); | |
| 70 | expect(gusts.length).toBeGreaterThan(2); | |
| 71 | expect(GUST_FIRST).toBeGreaterThanOrEqual(2 * WARN); | |
| 72 | expect(gusts[0].at).toBeGreaterThanOrEqual(GUST_FIRST); | |
| 73 | for (let n = 1; n < gusts.length; n++) expect(gusts[n].at - gusts[n - 1].at).toBeGreaterThanOrEqual(gusts[n - 1].dur + GUST_GAP); | |
| 74 | expect(GUST_GAP).toBeGreaterThanOrEqual(WARN); | |
| 75 | for (const g of gusts) { | |
| 76 | expect(g.event).toMatchObject({ type: 'gust', dir: g.dir }); | |
| 77 | // the grass and flag lean for the whole warning: it began a full WARN before the gust landed (one step of slack) | |
| 78 | expect(g.warnedSince).not.toBe(null); | |
| 79 | expect(g.at - g.warnedSince).toBeGreaterThanOrEqual(WARN - .02 - 1e-9); | |
| 80 | } | |
| 81 | }); | |
| 82 | ||
| 83 | test('holding a lean key pushes back, and holding too long overcorrects', () => { | |
| 84 | const s = newRound(0, 1, { calm: true }); | |
| 85 | s.lean = 8; | |
| 86 | setInput(s, -1); | |
| 87 | run(s, .6); | |
| 88 | expect(s.lean).toBeLessThan(8); | |
| 89 | const long = newRound(0, 1, { calm: true }); | |
| 90 | long.lean = 6; | |
| 91 | setInput(long, -1); | |
| 92 | run(long, 1.6); | |
| 93 | expect(long.lean < -6 || long.phase === 'fall').toBe(true); | |
| 94 | }); | |
| 95 | ||
| 96 | test('input is validated: anything else lets go', () => { | |
| 97 | const s = newRound(); | |
| 98 | setInput(s, 5); expect(s.input).toBe(1); | |
| 99 | setInput(s, -0.2); expect(s.input).toBe(-1); | |
| 100 | setInput(s, NaN); expect(s.input).toBe(0); | |
| 101 | setInput(s, 'left'); expect(s.input).toBe(0); | |
| 102 | }); | |
| 103 | ||
| 104 | test('leaning past the limit adds a fall and returns to the last mark', () => { | |
| 105 | const s = newRound(0, 1, { calm: true }); | |
| 106 | s.pos = MARKS[1] + .1; s.mark = 1; s.lean = LIMIT - .1; s.vel = 20; | |
| 107 | const ev = run(s, .1); | |
| 108 | expect(ev.find(e => e.type === 'fall')).toMatchObject({ mark: 1, falls: 1 }); | |
| 109 | expect(s.falls).toBe(1); | |
| 110 | expect(s.phase).toBe('fall'); | |
| 111 | const back = run(s, FALL_TIME + .02); | |
| 112 | expect(back.some(e => e.type === 'restart')).toBe(true); | |
| 113 | expect(s.phase).toBe('walk'); | |
| 114 | expect(Math.abs(s.pos - MARKS[1])).toBeLessThan(.01); | |
| 115 | expect(Math.abs(s.lean)).toBeLessThan(3); | |
| 116 | }); | |
| 117 | ||
| 118 | test('marks are passed in order and the last one is kept', () => { | |
| 119 | expect(markFor(0)).toBe(0); | |
| 120 | expect(markFor(MARKS[1])).toBe(1); | |
| 121 | expect(markFor(.99)).toBe(2); | |
| 122 | expect(markFor(NaN)).toBe(0); | |
| 123 | const s = newRound(0, 2, { calm: true }); | |
| 124 | const ev = steady(s, CROSS_TIME * .7); | |
| 125 | expect(ev.filter(e => e.type === 'mark').map(e => e.mark)).toEqual([1, 2]); | |
| 126 | expect(s.mark).toBe(2); | |
| 127 | }); | |
| 128 | ||
| 129 | test('a fast steady crossing scores 10 + seconds left + 5', () => { | |
| 130 | const s = newRound(0, 4, { calm: true }); | |
| 131 | s.crossTime = 20; s.pos = 1 - .001; s.lean = 0; s.vel = 0; s.peak = STEADY - 1; | |
| 132 | const ev = run(s, .1); | |
| 133 | const done = ev.find(e => e.type === 'cross'); | |
| 134 | const left = Math.floor(60 - 20.02); | |
| 135 | expect(done).toMatchObject({ points: 10 + left + 5, seconds: left, steady: true }); | |
| 136 | expect(s.score).toBe(10 + left + 5); | |
| 137 | expect(s.crossings).toBe(1); | |
| 138 | }); | |
| 139 | ||
| 140 | test('a crossing that leaned past half the limit gets no steady bonus', () => { | |
| 141 | const s = newRound(0, 4, { calm: true }); | |
| 142 | s.crossTime = 20; s.pos = 1 - .001; s.lean = 0; s.vel = 0; s.peak = STEADY + 1; | |
| 143 | const done = run(s, .1).find(e => e.type === 'cross'); | |
| 144 | expect(done.steady).toBe(false); | |
| 145 | expect(done.points).toBe(10 + Math.floor(60 - 20.02)); | |
| 146 | }); | |
| 147 | ||
| 148 | test('a slow crossing past a minute earns only the 10', () => { | |
| 149 | const s = newRound(0, 4, { calm: true }); | |
| 150 | s.crossTime = 75; s.pos = 1 - .001; s.lean = 0; s.peak = 0; | |
| 151 | expect(timeLeft(s)).toBe(0); | |
| 152 | expect(run(s, .1).find(e => e.type === 'cross').points).toBe(10 + 5); | |
| 153 | }); | |
| 154 | ||
| 155 | test('a real steady walker crosses on the clock', () => { | |
| 156 | const s = newRound(0, 5, { calm: true }); | |
| 157 | const ev = steady(s, CROSS_TIME + .5); | |
| 158 | expect(s.crossings).toBe(1); | |
| 159 | expect(ev.find(e => e.type === 'cross').seconds).toBeGreaterThanOrEqual(Math.floor(60 - CROSS_TIME) - 1); | |
| 160 | }); | |
| 161 | ||
| 162 | test('three falls end the round', () => { | |
| 163 | const s = newRound(0, 1, { calm: true }); | |
| 164 | for (let n = 1; n <= MAX_FALLS; n++) { | |
| 165 | s.lean = LIMIT + 1; | |
| 166 | run(s, .05); | |
| 167 | expect(s.falls).toBe(n); | |
| 168 | const ev = run(s, 2); | |
| 169 | if (n < MAX_FALLS) expect(s.phase).toBe('walk'); | |
| 170 | else expect(ev.find(e => e.type === 'over')).toMatchObject({ reason: 'falls' }); | |
| 171 | } | |
| 172 | expect(s.phase).toBe('over'); | |
| 173 | expect(s.crossings).toBe(0); | |
| 174 | }); | |
| 175 | ||
| 176 | test('three crossings end the round, and a record is kept', () => { | |
| 177 | const s = newRound(10, 6, { calm: true }); | |
| 178 | let over = null; | |
| 179 | for (let n = 0; n < CROSSINGS; n++) { | |
| 180 | s.crossTime = 30; s.pos = 1 - .001; s.lean = 0; s.vel = 0; s.peak = 0; | |
| 181 | const ev = run(s, 2); | |
| 182 | over = ev.find(e => e.type === 'over') || over; | |
| 183 | } | |
| 184 | expect(s.crossings).toBe(CROSSINGS); | |
| 185 | expect(over).toMatchObject({ reason: 'crossed', record: true }); | |
| 186 | expect(s.best).toBe(s.score); | |
| 187 | expect(advance(s, .1)).toEqual([]); | |
| 188 | }); | |
| 189 | ||
| 190 | test('a finished round restarts on a tap after a moment, keeping the best', () => { | |
| 191 | const s = newRound(0, 1, { calm: true }); | |
| 192 | s.lean = LIMIT + 1; s.falls = MAX_FALLS - 1; s.score = 40; | |
| 193 | run(s, FALL_TIME + .1); | |
| 194 | expect(s.phase).toBe('over'); | |
| 195 | expect(again(s)).toBe(null); | |
| 196 | run(s, 1); | |
| 197 | const next = again(s); | |
| 198 | expect(next.phase).toBe('walk'); | |
| 199 | expect(next.falls).toBe(0); | |
| 200 | expect(next.best).toBe(40); | |
| 201 | }); | |
| 202 | ||
| 203 | test('the same seed gives the same round, and a bad dt cannot break it', () => { | |
| 204 | const a = newRound(0, 9), b = newRound(0, 9); | |
| 205 | run(a, 12); run(b, 12); | |
| 206 | expect(a.lean).toBe(b.lean); | |
| 207 | advance(a, NaN); advance(a, -3); advance(a, Infinity); | |
| 208 | expect(Number.isFinite(a.lean)).toBe(true); | |
| 209 | expect(newRound(NaN, NaN).best).toBe(0); | |
| 210 | }); | |
| 211 | ||
| 212 | test('a first-time walker with a slow glance still gets across about once', () => { | |
| 213 | // reacts to the lean alone, a third of a second late: the drift must leave room for that | |
| 214 | let crossings = 0; | |
| 215 | for (let seed = 1; seed <= 12; seed++) { | |
| 216 | const s = newRound(0, seed), seen = []; | |
| 217 | for (let t = 0; t < 120 && s.phase !== 'over'; t += .02) { | |
| 218 | seen.push(s.lean); | |
| 219 | const l = seen[Math.max(0, seen.length - 1 - 15)]; | |
| 220 | setInput(s, Math.abs(l) > 4 ? (l > 0 ? -1 : 1) : 0); | |
| 221 | advance(s, .02); | |
| 222 | } | |
| 223 | crossings += s.crossings; | |
| 224 | } | |
| 225 | expect(crossings / 12).toBeGreaterThanOrEqual(1); | |
| 226 | }); |
No
<small>exists inside#lean, so this rule is dead.