Public: anyone can read this; the crew can change it.
Migrated from github.com/amirmushichge/pixel-plays
Add Bell Tower: ring the peal back on four bells, one bell longer each round #69
Check · Review · Land · Live done — Landed and live in production.
quill/pixel-plays:quill/59-add-bell-tower-ring-the-peal-back-on-fou → main 4 commits fork 1 thread · 1 unresolved
10 files
changed +663
−3
README.md +2 −1
| @@ -32,6 +32,7 @@ Concept by [abhi](https://x.com/awwbhi2). Game by [AmirMushich](https://x.com/Am | ||
| 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 | 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 | |
| 35 | | [](bell-tower/) | The tower rings a peal on four bells of different sizes and you ring it back by pulling the ropes in the same order; a bell must swing back before it rings again, a right peal scores its length and the next is the same peal plus one bell (it starts at three), and a wrong bell ends that peal without scoring, three wrong peals end the game; each bell flashes as it swings, so it plays muted too (forged by the crew on the engine and shell above, not by the original creators) | Tap a rope; 1-4 or the arrows, one per bell | | |
| 35 | 36 | | [](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 | |
| 36 | 37 | | [](archery/) | Ten arrows at a straw target across a meadow: drag back from the archer to draw (the drag length is power, its angle is aim), release to shoot; the wind changes every arrow and shows on the pennant and the grass, holding past three seconds makes the aim sway wider, and rings score 10 (gold), 8, 6, 4 and 2 (forged by the crew on the engine and shell above, not by the original creators) | Drag back, release; arrows + hold space | |
| 37 | 38 | | [](tram-line/) | A 120-second timetable over eight stops: hold to power, release to coast, brake so the front door lands on the yellow mark; within 0.5 m scores 3, within 2 m scores 1, overshooting the shelter scores 0 and strands the passengers, stopping between stops costs 1, and each second left at the last stop adds 1 (forged by the crew on the engine and shell above, not by the original creators) | Hold up or W (right half) to power, down or S (left half) to brake | |
| @@ -54,7 +55,7 @@ On the forge, `forge.json` runs `bun serve.mjs` with health check `/`. State is | ||
| 54 | 55 | 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. |
| 55 | 56 | |
| 56 | 57 | ## Add a game |
| 57 | 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 and Archery 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`. | |
| 58 | 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 and Bell Tower 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`. | |
| 58 | 59 | |
| 59 | 60 | - `shell.js` provides the game menu, the Rules popup, the credits and the background music. |
| 60 | 61 | - `engine.js` provides the camera, depth buffer, colour ramps, shadows, edges, figures, trees and wind as globals. |
bell-tower/index.html added +372
| @@ -0,0 +1,372 @@ | ||
| 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>Bell Tower</title> | |
| 7 | <!-- og:begin --> | |
| 8 | <meta name="description" content="Ring the peal back: the tower plays a sequence on four bells, pull the ropes in the same order, each round one bell longer"> | |
| 9 | <link rel="canonical" href="https://pixel-plays--forge-trevato-apps.open-platform.sh/bell-tower/"> | |
| 10 | <meta property="og:type" content="website"> | |
| 11 | <meta property="og:site_name" content="Pixel Plays"> | |
| 12 | <meta property="og:title" content="Bell Tower"> | |
| 13 | <meta property="og:description" content="Ring the peal back: the tower plays a sequence on four bells, pull the ropes in the same order, each round one bell longer"> | |
| 14 | <meta property="og:url" content="https://pixel-plays--forge-trevato-apps.open-platform.sh/bell-tower/"> | |
| 15 | <meta property="og:image" content="https://pixel-plays--forge-trevato-apps.open-platform.sh/og/bell-tower.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="Bell Tower: 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; : #a8863a; } | |
| 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 | #status { 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="An open stone bell tower with four bells of different sizes hung in a row, numbered 1 to 4 from the smallest, a ringer at the ropes and pigeons on the parapet. The tower rings a peal, then you ring it back: press 1 to 4 or the arrow keys, or tap a rope, in the same order. A bell must swing back before it rings again. Each peal is one bell longer; three wrong peals end the game."></canvas></div> | |
| 42 | <div id="scores"> | |
| 43 | <div class="row"><b id="score">0</b><span>score</span></div> | |
| 44 | <div class="row"><b id="length">3</b><span>peal length</span></div> | |
| 45 | <div class="row"><b id="misses">0/3</b><span>misses</span></div> | |
| 46 | <div class="row"><b id="best">0</b><span>best</span></div> | |
| 47 | </div> | |
| 48 | <div class="hud" id="bottom"> | |
| 49 | <div id="status" role="status" aria-live="polite"></div> | |
| 50 | <button id="mute" type="button" aria-pressed="false">sound on</button> | |
| 51 | </div> | |
| 52 | <script src="../shell.js"></script> | |
| 53 | <script> | |
| 54 | PixelPlays.mount({ | |
| 55 | id: 'bell-tower', | |
| 56 | rules: [ | |
| 57 | 'The tower rings a peal on four bells; ring it back in the same order.', | |
| 58 | 'Tap a rope, or press 1 to 4 or the arrows (left, down, up, right).', | |
| 59 | 'A bell must swing back before it rings again.', | |
| 60 | 'A right peal scores its length and grows by one bell. Three wrong peals end it.', | |
| 61 | ], | |
| 62 | }); | |
| 63 | </script> | |
| 64 | <script>window.PP_SCENE = { originY: 66, shadowX: 1.3, shadowY: -.12 };</script> | |
| 65 | <script src="../engine.js"></script> | |
| 66 | <script src="peal.js"></script> | |
| 67 | <script> | |
| 68 | 'use strict'; | |
| 69 | ||
| 70 | const STONE = [200, 194, 180], STONE_DARK = [160, 154, 142], FLOOR = [214, 208, 194], FLOOR_ALT = [198, 192, 178]; | |
| 71 | const WOOD = [112, 82, 56], ROPE = [214, 196, 150], SALLY = [200, 64, 52], SALLY_STRIPE = [240, 232, 214]; | |
| 72 | const PIGEON = [150, 154, 166], PIGEON_DARK = [96, 100, 114], NECK = [112, 130, 150], BEAK = [226, 170, 90]; | |
| 73 | const RINGER = { shirt: [64, 92, 140], stripe: [240, 232, 214], legs: [58, 52, 48], hair: [60, 40, 30], skin: [222, 170, 132] }; | |
| 74 | const GLOW = [255, 238, 150]; | |
| 75 | // four bells from smallest to largest, each its own metal so they are told apart by eye as well as by ear | |
| 76 | const BELLS = [ | |
| 77 | { u: -13.5, r: 1.5, color: [150, 172, 196], note: 784.0 }, | |
| 78 | { u: -4.5, r: 1.85, color: [96, 156, 124], note: 659.3 }, | |
| 79 | { u: 4.5, r: 2.15, color: [204, 164, 62], note: 523.3 }, | |
| 80 | { u: 13.5, r: 2.45, color: [184, 96, 62], note: 392.0 }, | |
| 81 | ]; | |
| 82 | const PIVOT_Z = 10.2, HALF_U = 17.4, HALF_V = 7, PILLAR_U = 16.6, BACK_V = -6.2, FRONT_V = 6.2, ROPE_V = 3.2, RINGER_V = 4.6, PERCH_Z = 1.95, SALLY_Z = 5.6; | |
| 83 | const PIGEONS = [{ u: -12.4, face: 1, phase: 0 }, { u: -7.6, face: -1, phase: 5 }, { u: -1.5, face: 1, phase: 9 }, { u: 4.2, face: 1, phase: 3 }, { u: 9.4, face: -1, phase: 12 }, { u: 14.6, face: -1, phase: 7 }]; | |
| 84 | const BEST_KEY = 'pixel-plays-bell-tower-best'; | |
| 85 | const GLOW_STEPS = 6; | |
| 86 | const SWING_AMP = STILL ? .12 : .5; | |
| 87 | ||
| 88 | const world = (u, v, z) => [(u + v) / R2, (v - u) / R2, z]; | |
| 89 | const mix = (a, b, t) => [lerp(a[0], b[0], t), lerp(a[1], b[1], t), lerp(a[2], b[2], t)]; | |
| 90 | // stable colour arrays: the engine caches its shading ramps per array, so none is made per frame | |
| 91 | const PALETTE = BELLS.map(b => Array.from({ length: GLOW_STEPS + 1 }, (_, k) => mix(b.color, GLOW, k / GLOW_STEPS * .8))); | |
| 92 | const LIP = BELLS.map(b => mix(b.color, [40, 36, 34], .45)); | |
| 93 | const PROFILE = [[0, .4], [.2, .4], [.5, .48], [.75, .66], [.9, .94], [1, 1.12]]; | |
| 94 | const DIGITS = { | |
| 95 | 1: ['010', '110', '010', '010', '111'], | |
| 96 | 2: ['111', '001', '111', '100', '111'], | |
| 97 | 3: ['111', '001', '111', '001', '111'], | |
| 98 | 4: ['101', '101', '111', '001', '001'], | |
| 99 | }; | |
| 100 | const INK_PIX = rgb(28, 27, 25), ACCENT_PIX = rgb(168, 134, 58); | |
| 101 | ||
| 102 | /* ---------- state ---------- */ | |
| 103 | const best0 = (() => { try { return Number(localStorage.getItem(BEST_KEY)) || 0; } catch (e) { return 0; } })(); | |
| 104 | let P = Peal.newGame(best0, Date.now() & 0xffff); | |
| 105 | const G = { time: 0, status: '', noteUntil: 0, ringer: BELLS[1].u, pull: 0, pulled: -1 }; | |
| 106 | const nodes = { score: document.getElementById('score'), length: document.getElementById('length'), misses: document.getElementById('misses'), best: document.getElementById('best'), status: document.getElementById('status') }; | |
| 107 | function renderHud() { | |
| 108 | nodes.score.textContent = P.score; | |
| 109 | nodes.length.textContent = P.seq.length; | |
| 110 | nodes.misses.textContent = `${P.misses}/${Peal.MAX_MISSES}`; | |
| 111 | nodes.best.textContent = Math.max(P.best, P.score); | |
| 112 | } | |
| 113 | function setStatus(text) { | |
| 114 | if (text === G.status) return; | |
| 115 | G.status = text; | |
| 116 | nodes.status.textContent = text; | |
| 117 | } | |
| 118 | function note(text) { setStatus(text); G.noteUntil = G.time + 1.8; } | |
| 119 | ||
| 120 | /* ---------- baked scene ---------- */ | |
| 121 | function groundColor(x, y, ix, iy) { | |
| 122 | const u = (x - y) / R2, v = (x + y) / R2; | |
| 123 | if (Math.abs(u) > HALF_U || Math.abs(v) > HALF_V) return 0; | |
| 124 | if (Math.abs(u) > HALF_U - .5 || Math.abs(v) > HALF_V - .5) return rgb(...STONE_DARK); | |
| 125 | const slab = (Math.floor((u + 40) / 2.2) + Math.floor((v + 40) / 2.2)) & 1; | |
| 126 | const t = .94 + (hash(ix, iy) - .5) * .08; | |
| 127 | const c = slab ? FLOOR : FLOOR_ALT; | |
| 128 | return rgb(c[0] * t, c[1] * t, c[2] * t); | |
| 129 | } | |
| 130 | function post(u, v, h) { | |
| 131 | capsule(world(u, v, 0), world(u, v, h), .7, .7, () => STONE, 'rough'); | |
| 132 | capsule(world(u, v, h), world(u, v, h + .3), .9, .9, () => STONE_DARK, 'rough'); | |
| 133 | for (let z = .6; z <= h; z += 1) castShadow(...world(u, v, z).slice(0, 2), z, .75, 1, 0); | |
| 134 | } | |
| 135 | function bakeTower() { | |
| 136 | for (const u of [-PILLAR_U, PILLAR_U]) for (const v of [BACK_V, FRONT_V - .4]) post(u, v, 15); | |
| 137 | // lintels along the row and across it, and the timber the bells hang from | |
| 138 | for (const v of [BACK_V, FRONT_V - .4]) capsule(world(-PILLAR_U, v, 15.5), world(PILLAR_U, v, 15.5), .45, .45, () => STONE, 'rough'); | |
| 139 | for (const u of [-PILLAR_U, PILLAR_U]) capsule(world(u, BACK_V, 15.5), world(u, FRONT_V - .4, 15.5), .45, .45, () => STONE, 'rough'); | |
| 140 | capsule(world(-PILLAR_U, 0, PIVOT_Z + .35), world(PILLAR_U, 0, PIVOT_Z + .35), .32, .32, () => WOOD, 'solid'); | |
| 141 | // the back parapet the pigeons sit on | |
| 142 | capsule(world(-PILLAR_U, BACK_V, .55), world(PILLAR_U, BACK_V, .55), .75, .75, () => STONE, 'rough'); | |
| 143 | capsule(world(-PILLAR_U, BACK_V, 1.25), world(PILLAR_U, BACK_V, 1.25), .65, .65, () => STONE_DARK, 'rough'); | |
| 144 | for (let u = -PILLAR_U; u <= PILLAR_U; u += .9) castShadow(...world(u, BACK_V, 0).slice(0, 2), 1.9, .75, 1, 0); | |
| 145 | // the holes the ropes come through | |
| 146 | for (const b of BELLS) capsule(world(b.u, ROPE_V, 0), world(b.u, ROPE_V, .02), .32, .32, () => STONE_DARK, 'solid'); | |
| 147 | } | |
| 148 | function bake() { | |
| 149 | ground.fill(0); shadowMask.fill(0); staticShadow.fill(0); kinds.fill(0); | |
| 150 | tufts.length = 0; leaves.length = 0; | |
| 151 | zb.fill(-1e9); | |
| 152 | pix.fill(rgb(255, 255, 255)); | |
| 153 | for (let iy = 0; iy < H; iy++) for (let ix = 0; ix < W; ix++) { | |
| 154 | const g = unproject(ix + .5, iy + .5), i = iy * W + ix, c = groundColor(g.x, g.y, ix, iy); | |
| 155 | if (!c) continue; | |
| 156 | pix[i] = c; ground[i] = 1; | |
| 157 | zb[i] = (g.x + g.y) / R2 * COS; | |
| 158 | } | |
| 159 | groundZ.set(zb); | |
| 160 | bakeTower(); | |
| 161 | bakeOcclusion(); | |
| 162 | staticShadow.set(shadowMask); | |
| 163 | applyShadows(groundZ); | |
| 164 | bakedPix.set(pix); | |
| 165 | bakedZ.set(zb); | |
| 166 | } | |
| 167 | ||
| 168 | /* ---------- the bells, the ringer, the pigeons ---------- */ | |
| 169 | // how far a bell has left to swing back, 0..1 | |
| 170 | const swingOf = i => P.swing[i] / Peal.SWING; | |
| 171 | const glowOf = i => Math.round(Math.pow(swingOf(i), 1.4) * GLOW_STEPS); | |
| 172 | function drawBell(i) { | |
| 173 | const b = BELLS[i], s = swingOf(i); | |
| 174 | // out fast, then back and still: the angle dies away with the time left | |
| 175 | const a = SWING_AMP * s * Math.sin((1 - s) * Math.PI * 3) * (i % 2 ? -1 : 1); | |
| 176 | const hh = b.r * 1.7 + .6, glow = PALETTE[i][glowOf(i)], lip = LIP[i]; | |
| 177 | const at = t => world(b.u + Math.sin(a) * t, 0, PIVOT_Z - Math.cos(a) * t); | |
| 178 | const lipZ = PIVOT_Z - Math.cos(a) * hh * .84; | |
| 179 | for (let k = 0; k < PROFILE.length - 1; k++) { | |
| 180 | const [t0, r0] = PROFILE[k], [t1, r1] = PROFILE[k + 1]; | |
| 181 | dynamicAdd(at(t0 * hh), at(t1 * hh), r0 * b.r, r1 * b.r, z => (z < lipZ ? lip : glow)); | |
| 182 | } | |
| 183 | const c = at(hh); | |
| 184 | dynamicAdd(c, c, b.r * .8, b.r * .8, () => lip); | |
| 185 | const clapper = at(hh * .94); | |
| 186 | dynamicAdd(clapper, clapper, .26, .26, () => STONE_DARK); | |
| 187 | } | |
| 188 | // rope and sally: the grip drops when the bell is pulled | |
| 189 | function drawRope(i) { | |
| 190 | const b = BELLS[i], drop = G.pulled === i ? G.pull : 0, sallyZ = SALLY_Z - drop * 1.1; | |
| 191 | capsule(world(b.u, 0, PIVOT_Z + .1), world(b.u, ROPE_V, PIVOT_Z + .1), .06, .06, () => ROPE, 'solid'); | |
| 192 | capsule(world(b.u, ROPE_V, PIVOT_Z + .1), world(b.u, ROPE_V, sallyZ + .8), .07, .07, () => ROPE, 'solid'); | |
| 193 | capsule(world(b.u, ROPE_V, sallyZ + .8), world(b.u, ROPE_V, sallyZ - .8), .17, .17, z => (Math.floor(z * 3.2) % 2 ? SALLY : SALLY_STRIPE), 'solid'); | |
| 194 | capsule(world(b.u, ROPE_V, sallyZ - .8), world(b.u, ROPE_V, .1), .06, .06, () => ROPE, 'solid'); | |
| 195 | } | |
| 196 | function drawRinger() { | |
| 197 | const u = G.ringer, k = 1.5, hands = SALLY_Z - .2 - G.pull * 1.1, bow = G.pull * .5; | |
| 198 | const hip = world(u, RINGER_V, 1.15 * k), sh = world(u, RINGER_V - bow * .4, (2.2 - bow) * k), head = world(u, RINGER_V - bow * .5, (2.62 - bow) * k); | |
| 199 | const handL = world(u - .3, ROPE_V + .15, hands), handR = world(u + .3, ROPE_V + .15, hands - .4); | |
| 200 | const foot = (du, dv) => world(u + du, RINGER_V + dv, .15); | |
| 201 | dynamicAdd(hip, sh, .3 * k, .26 * k, h => (Math.floor(h * 2.2) % 2 ? RINGER.shirt : RINGER.stripe)); | |
| 202 | dynamicAdd(sh, head, .09 * k, .06 * k, () => RINGER.skin); | |
| 203 | dynamicAdd(head, head, .26 * k, .26 * k, () => RINGER.skin); | |
| 204 | const cap = [head[0], head[1], head[2] + .22]; | |
| 205 | dynamicAdd(cap, cap, .27 * k, .2 * k, () => RINGER.hair); | |
| 206 | dynamicAdd(world(u - .2, RINGER_V, 1.15 * k), foot(-.25, .08), .13 * k, .11 * k, () => RINGER.legs); | |
| 207 | dynamicAdd(world(u + .2, RINGER_V, 1.15 * k), foot(.25, -.08), .13 * k, .11 * k, () => RINGER.legs); | |
| 208 | dynamicAdd(world(u - .3, RINGER_V - bow * .4, (2.05 - bow) * k), handL, .12, .11, () => RINGER.skin); | |
| 209 | dynamicAdd(world(u + .3, RINGER_V - bow * .4, (2.05 - bow) * k), handR, .12, .11, () => RINGER.skin); | |
| 210 | } | |
| 211 | // a pigeon: body, a head that pecks every so often, a tail; stepped at 8 fps and still when motion is reduced | |
| 212 | function drawPigeon(p, step) { | |
| 213 | const peck = !STILL && (step + p.phase) % 16 < 3 ? 1 : 0, bob = !STILL && (step + p.phase) % 16 === 9 ? .08 : 0; | |
| 214 | const f = p.face, z = PERCH_Z + .6, k = 1.5; | |
| 215 | const at = (du, dz) => world(p.u + f * du * k, BACK_V, z + dz * k); | |
| 216 | dynamicAdd(at(-.35, .02), at(.28, .08), .5, .45, () => PIGEON); | |
| 217 | dynamicAdd(at(-.62, .0), at(-.3, .04), .18, .3, () => PIGEON_DARK); | |
| 218 | const neck = at(.42, .3 - peck * .22), head = at(.58, .38 - peck * .42 + bob); | |
| 219 | dynamicAdd(at(.2, .15), neck, .3, .25, () => NECK); | |
| 220 | dynamicAdd(head, head, .28, .28, () => PIGEON); | |
| 221 | const beak = at(.78, .34 - peck * .46 + bob); | |
| 222 | dynamicAdd(head, beak, .09, .045, () => BEAK); | |
| 223 | } | |
| 224 | // 1-4 on the floor below each rope, lit while that bell swings | |
| 225 | function drawNumbers() { | |
| 226 | BELLS.forEach((b, i) => { | |
| 227 | const p = project(...world(b.u, FRONT_V + .2, 0)), color = swingOf(i) > 0 ? ACCENT_PIX : INK_PIX; | |
| 228 | const rows = DIGITS[i + 1], x0 = Math.round(p.sx) - 3, y0 = Math.round(p.sy) - 5; | |
| 229 | rows.forEach((row, ry) => { | |
| 230 | for (let cx = 0; cx < 3; cx++) { | |
| 231 | if (row[cx] !== '1') continue; | |
| 232 | for (let dy = 0; dy < 2; dy++) for (let dx = 0; dx < 2; dx++) { | |
| 233 | const x = x0 + cx * 2 + dx, y = y0 + ry * 2 + dy; | |
| 234 | if (x >= 0 && x < W && y >= 0 && y < H) pix[y * W + x] = color; | |
| 235 | } | |
| 236 | } | |
| 237 | }); | |
| 238 | }); | |
| 239 | } | |
| 240 | ||
| 241 | function draw() { | |
| 242 | pix.set(bakedPix); | |
| 243 | zb.set(bakedZ); | |
| 244 | pendingShadows.length = 0; | |
| 245 | const step = STILL ? 0 : Math.floor(G.time * WIND_FPS); | |
| 246 | BELLS.forEach((b, i) => { drawRope(i); drawBell(i); }); | |
| 247 | drawRinger(); | |
| 248 | for (const p of PIGEONS) drawPigeon(p, step); | |
| 249 | flushShadows(); | |
| 250 | applyShadows(bakedZ); | |
| 251 | shadeEdges(); | |
| 252 | drawNumbers(); | |
| 253 | ctx.putImageData(image, 0, 0); | |
| 254 | } | |
| 255 | ||
| 256 | /* ---------- game ---------- */ | |
| 257 | function act(events) { | |
| 258 | for (const e of events) { | |
| 259 | if (e.type === 'chime') sound(e.bell); | |
| 260 | if (e.type === 'turn') note(`your turn · ${P.seq.length} bells`); | |
| 261 | if (e.type === 'listen') { note(`listen · peal of ${e.length}`); renderHud(); } | |
| 262 | if (e.type === 'ring') { | |
| 263 | G.ringer = BELLS[e.bell].u; G.pulled = e.bell; G.pull = 1; | |
| 264 | sound(e.bell); if (!e.right) sound('dull'); | |
| 265 | } | |
| 266 | if (e.type === 'ignored') note('wait for that bell to swing back'); | |
| 267 | if (e.type === 'peal') { note(`peal of ${e.length} rung · +${e.length}`); renderHud(); } | |
| 268 | if (e.type === 'miss') { note(`bell ${e.bell + 1}, not bell ${e.wanted + 1} · miss ${e.misses} of ${Peal.MAX_MISSES}`); renderHud(); } | |
| 269 | if (e.type === 'over') { | |
| 270 | if (P.record) try { localStorage.setItem(BEST_KEY, String(P.best)); } catch (err) { /* private window: keep it for this visit */ } | |
| 271 | setStatus(`three wrong peals · ${P.score}${P.record ? ' · new best' : ` · best ${P.best}`} · tap or press a key to ring again`); | |
| 272 | renderHud(); | |
| 273 | } | |
| 274 | } | |
| 275 | } | |
| 276 | function pullBell(bell) { | |
| 277 | ensureAudio(); | |
| 278 | if (P.phase === 'over') { | |
| 279 | const next = Peal.again(P); | |
| 280 | if (next) { P = next; note(HINT); renderHud(); } | |
| 281 | return; | |
| 282 | } | |
| 283 | const before = P.got; | |
| 284 | act(Peal.pull(P, bell)); | |
| 285 | if (P.phase === 'ring' && P.got > before) setStatus(`${P.got} of ${P.seq.length} rung`); | |
| 286 | } | |
| 287 | const HINT = 'listen to the peal, then ring it back'; | |
| 288 | function update(dt) { | |
| 289 | G.time += dt; | |
| 290 | G.pull = Math.max(0, G.pull - dt * 4); | |
| 291 | act(Peal.advance(P, dt)); | |
| 292 | if (G.time < G.noteUntil) return; | |
| 293 | if (P.phase === 'listen') setStatus(`listen · peal of ${P.seq.length}`); | |
| 294 | else if (P.phase === 'ring') setStatus(`your turn · ${P.got} of ${P.seq.length} rung`); | |
| 295 | } | |
| 296 | ||
| 297 | /* ---------- input ---------- */ | |
| 298 | canvas.addEventListener('pointerdown', e => { | |
| 299 | canvas.focus({ preventScroll: true }); | |
| 300 | const rect = canvas.getBoundingClientRect(); | |
| 301 | const col = Math.floor((e.clientX - rect.left) / rect.width * Peal.BELLS); | |
| 302 | pullBell(Math.max(0, Math.min(Peal.BELLS - 1, col))); | |
| 303 | }); | |
| 304 | // left to right, as the bells hang | |
| 305 | const KEYS = { 1: 0, 2: 1, 3: 2, 4: 3, ArrowLeft: 0, ArrowDown: 1, ArrowUp: 2, ArrowRight: 3 }; | |
| 306 | canvas.addEventListener('keydown', e => { | |
| 307 | const bell = KEYS[e.key]; | |
| 308 | if (bell === undefined && e.key !== ' ' && e.key !== 'Enter') return; | |
| 309 | e.preventDefault(); | |
| 310 | if (e.repeat) return; | |
| 311 | pullBell(bell === undefined ? -1 : bell); | |
| 312 | }); | |
| 313 | ||
| 314 | /* ---------- sound ---------- */ | |
| 315 | let audio = null, muted = false; | |
| 316 | function ensureAudio() { | |
| 317 | if (audio) { if (audio.state === 'suspended') audio.resume(); return; } | |
| 318 | const AC = window.AudioContext || window.webkitAudioContext; | |
| 319 | if (AC) audio = new AC(); | |
| 320 | } | |
| 321 | // a bell: a hum, the strike note, a minor third above it and the nominal an octave up, each dying at its own pace | |
| 322 | const PARTIALS = [[.5, .5, 1.9], [1, 1, 1.5], [1.2, .3, 1], [2, .4, .6]]; | |
| 323 | function sound(kind) { | |
| 324 | if (!audio || muted) return; | |
| 325 | const now = audio.currentTime; | |
| 326 | if (kind === 'dull') { | |
| 327 | const gain = audio.createGain(), osc = audio.createOscillator(); | |
| 328 | osc.type = 'sawtooth'; | |
| 329 | osc.frequency.setValueAtTime(150, now); | |
| 330 | osc.frequency.exponentialRampToValueAtTime(60, now + .3); | |
| 331 | gain.gain.setValueAtTime(.06, now); | |
| 332 | gain.gain.exponentialRampToValueAtTime(.0005, now + .3); | |
| 333 | osc.connect(gain).connect(audio.destination); | |
| 334 | osc.start(now); osc.stop(now + .32); | |
| 335 | return; | |
| 336 | } | |
| 337 | for (const [ratio, level, life] of PARTIALS) { | |
| 338 | const gain = audio.createGain(), osc = audio.createOscillator(); | |
| 339 | osc.type = 'sine'; | |
| 340 | osc.frequency.setValueAtTime(BELLS[kind].note * ratio, now); | |
| 341 | gain.gain.setValueAtTime(.0001, now); | |
| 342 | gain.gain.exponentialRampToValueAtTime(.09 * level, now + .01); | |
| 343 | gain.gain.exponentialRampToValueAtTime(.0005, now + life); | |
| 344 | osc.connect(gain).connect(audio.destination); | |
| 345 | osc.start(now); osc.stop(now + life + .02); | |
| 346 | } | |
| 347 | } | |
| 348 | const muteButton = document.getElementById('mute'); | |
| 349 | muteButton.addEventListener('click', () => { | |
| 350 | muted = !muted; | |
| 351 | muteButton.textContent = muted ? 'sound off' : 'sound on'; | |
| 352 | muteButton.setAttribute('aria-pressed', String(muted)); | |
| 353 | }); | |
| 354 | ||
| 355 | /* ---------- fit and boot ---------- */ | |
| 356 | addEventListener('resize', fit); | |
| 357 | fit(); | |
| 358 | bake(); | |
| 359 | note(HINT); | |
| 360 | renderHud(); | |
| 361 | canvas.focus({ preventScroll: true }); | |
| 362 | let last = performance.now(); | |
| 363 | function frame(now) { | |
| 364 | update(Math.min(.04, (now - last) / 1000)); | |
| 365 | last = now; | |
| 366 | draw(); | |
| 367 | requestAnimationFrame(frame); | |
| 368 | } | |
| 369 | requestAnimationFrame(frame); | |
| 370 | </script> | |
| 371 | </body> | |
| 372 | </html> |
bell-tower/page.test.mjs added +20
| @@ -0,0 +1,20 @@ | ||
| 1 | import vm from 'node:vm'; | |
| 2 | import { test, expect } from 'bun:test'; | |
| 3 | import { boot } from '../playtest.mjs'; | |
| 4 | ||
| 5 | // ring the tower's peal back by tapping the rope columns (the four bells sit at x = 45, 135, 225, 315) | |
| 6 | const state = g => vm.runInContext('P', g.window); | |
| 7 | function untilTurn(g) { for (let n = 0; state(g).phase !== 'ring' && n < 400; n++) g.run(.05); expect(state(g).phase).toBe('ring'); } | |
| 8 | ||
| 9 | test('the HUD peal length follows the peal the tower is ringing', () => { | |
| 10 | const g = boot('bell-tower'); | |
| 11 | g.run(1); | |
| 12 | untilTurn(g); | |
| 13 | expect(String(g.el('length').textContent)).toBe('3'); | |
| 14 | for (const bell of [...state(g).seq]) { g.tap(45 + bell * 90, 150); g.run(.6); } | |
| 15 | expect(String(g.el('score').textContent)).toBe('3'); | |
| 16 | untilTurn(g); | |
| 17 | expect(state(g).seq.length).toBe(4); | |
| 18 | expect(String(g.el('length').textContent)).toBe('4'); | |
| 19 | expect(g.errors).toEqual([]); | |
| 20 | }); |
bell-tower/peal.js added +96
| @@ -0,0 +1,96 @@ | ||
| 1 | 'use strict'; | |
| 2 | // The bell tower rules: a peal of bells made from a seed, played to the player, rung back in order, one bell longer | |
| 3 | // each time it is right. A bell that is still swinging cannot be pulled. Three wrong peals end the game. | |
| 4 | // Plain numbers in, plain numbers out, no DOM. The page draws and sounds what this decides. | |
| 5 | (function (root) { | |
| 6 | const BELLS = 4; | |
| 7 | const FIRST = 3; // bells in the first peal | |
| 8 | const MAX_MISSES = 3; | |
| 9 | const SWING = .5; // seconds a rung bell takes to swing back before it can ring again | |
| 10 | const LEAD = .9; // seconds before the tower starts a peal | |
| 11 | const GAP = .7; // seconds between bells of the tower's peal | |
| 12 | const TAIL = .5; // seconds after the last bell before the player's turn | |
| 13 | const PAUSE = 1.5; // seconds the result of a peal stays up | |
| 14 | const RESTART_AFTER = .6; | |
| 15 | ||
| 16 | const finite = v => (Number.isFinite(v) ? v : 0); | |
| 17 | // the bell at place i of the peal for a seed: the peal for n+1 bells is the peal for n with one more | |
| 18 | function bellAt(seed, i) { | |
| 19 | let h = (finite(seed) >>> 0) ^ Math.imul(i + 1, 0x9e3779b1); | |
| 20 | h = Math.imul(h ^ h >>> 15, 0x85ebca6b); | |
| 21 | h = Math.imul(h ^ h >>> 13, 0xc2b2ae35); | |
| 22 | return ((h ^ h >>> 16) >>> 0) % BELLS; | |
| 23 | } | |
| 24 | function peal(seed, length) { | |
| 25 | const out = []; | |
| 26 | for (let i = 0; i < Math.max(0, Math.floor(finite(length))); i++) out.push(bellAt(seed, i)); | |
| 27 | return out; | |
| 28 | } | |
| 29 | // the next peal: the last one and one bell more | |
| 30 | function extend(seq, seed) { return [...seq, bellAt(seed, seq.length)]; } | |
| 31 | ||
| 32 | function newGame(best = 0, seed = 0) { | |
| 33 | const sd = finite(seed) >>> 0; | |
| 34 | return { | |
| 35 | phase: 'listen', timer: 0, seed: sd, seq: peal(sd, FIRST), played: 0, got: 0, | |
| 36 | swing: new Array(BELLS).fill(0), last: -1, score: 0, misses: 0, lastPeal: 0, wanted: -1, | |
| 37 | best: Math.max(0, finite(best)), record: false, ok: false, | |
| 38 | }; | |
| 39 | } | |
| 40 | function finish(s, events) { | |
| 41 | s.phase = 'over'; s.timer = 0; | |
| 42 | if (s.score > s.best) { s.best = s.score; s.record = true; } | |
| 43 | events.push({ type: 'over', score: s.score, record: s.record }); | |
| 44 | } | |
| 45 | // the player pulls rope `bell`; only in their turn, and not while that bell still swings | |
| 46 | function pull(s, bell) { | |
| 47 | const events = []; | |
| 48 | if (s.phase !== 'ring' || !Number.isInteger(bell) || bell < 0 || bell >= BELLS) return events; | |
| 49 | if (s.swing[bell] > 0) { events.push({ type: 'ignored', bell }); return events; } | |
| 50 | s.swing[bell] = SWING; s.last = bell; | |
| 51 | if (bell === s.seq[s.got]) { | |
| 52 | s.got++; | |
| 53 | events.push({ type: 'ring', bell, right: true }); | |
| 54 | if (s.got >= s.seq.length) { | |
| 55 | s.score += s.seq.length; s.lastPeal = s.seq.length; s.ok = true; | |
| 56 | s.phase = 'between'; s.timer = 0; | |
| 57 | events.push({ type: 'peal', length: s.seq.length, score: s.score }); | |
| 58 | } | |
| 59 | } else { | |
| 60 | s.misses++; s.ok = false; s.wanted = s.seq[s.got]; | |
| 61 | events.push({ type: 'ring', bell, right: false }); | |
| 62 | events.push({ type: 'miss', bell, wanted: s.wanted, misses: s.misses }); | |
| 63 | if (s.misses >= MAX_MISSES) finish(s, events); else { s.phase = 'between'; s.timer = 0; } | |
| 64 | } | |
| 65 | return events; | |
| 66 | } | |
| 67 | function advance(s, dt) { | |
| 68 | const events = []; | |
| 69 | dt = Math.min(1, Math.max(0, finite(dt))); | |
| 70 | for (let i = 0; i < BELLS; i++) s.swing[i] = Math.max(0, s.swing[i] - dt); | |
| 71 | s.timer += dt; | |
| 72 | if (s.phase === 'listen') { | |
| 73 | // the tower rings each bell of the peal in turn | |
| 74 | while (s.played < s.seq.length && s.timer >= LEAD + s.played * GAP) { | |
| 75 | const bell = s.seq[s.played++]; | |
| 76 | s.swing[bell] = SWING; s.last = bell; | |
| 77 | events.push({ type: 'chime', bell, index: s.played - 1 }); | |
| 78 | } | |
| 79 | if (s.played >= s.seq.length && s.timer >= LEAD + (s.seq.length - 1) * GAP + TAIL) { | |
| 80 | s.phase = 'ring'; s.timer = 0; s.got = 0; | |
| 81 | events.push({ type: 'turn' }); | |
| 82 | } | |
| 83 | } else if (s.phase === 'between' && s.timer >= PAUSE) { | |
| 84 | // right: one bell longer; wrong: the same peal once more | |
| 85 | if (s.ok) s.seq = extend(s.seq, s.seed); | |
| 86 | s.phase = 'listen'; s.timer = 0; s.played = 0; s.got = 0; s.ok = false; s.wanted = -1; | |
| 87 | events.push({ type: 'listen', length: s.seq.length }); | |
| 88 | } | |
| 89 | return events; | |
| 90 | } | |
| 91 | // a tap after the game has been over for a moment starts the next one; otherwise null | |
| 92 | function again(s) { return s.phase === 'over' && s.timer > RESTART_AFTER ? newGame(s.best, s.seed + 1) : null; } | |
| 93 | ||
| 94 | const api = { BELLS, FIRST, MAX_MISSES, SWING, LEAD, GAP, TAIL, PAUSE, RESTART_AFTER, bellAt, peal, extend, newGame, pull, advance, again }; | |
| 95 | if (typeof module !== 'undefined') module.exports = api; else root.Peal = api; | |
| 96 | })(typeof window !== 'undefined' ? window : globalThis); |
bell-tower/peal.test.mjs added +162
| @@ -0,0 +1,162 @@ | ||
| 1 | import { test, expect } from 'bun:test'; | |
| 2 | import { BELLS, FIRST, MAX_MISSES, SWING, PAUSE, peal, extend, newGame, pull, advance, again } from './peal.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 | // let the tower play its peal until it is the player's turn | |
| 6 | const listen = s => { let n = 0; while (s.phase !== 'ring' && n++ < 2000) advance(s, .05); expect(s.phase).toBe('ring'); }; | |
| 7 | const wrongFor = s => (s.seq[s.got] + 1) % BELLS; | |
| 8 | // ring the whole peal back, waiting out each swing | |
| 9 | function ringBack(s) { | |
| 10 | const ev = []; | |
| 11 | for (const bell of [...s.seq]) { run(s, SWING + .05); ev.push(...pull(s, bell)); } | |
| 12 | return ev; | |
| 13 | } | |
| 14 | ||
| 15 | test('the same seed gives the same peal, and other seeds differ', () => { | |
| 16 | expect(peal(5, 8)).toEqual(peal(5, 8)); | |
| 17 | expect(newGame(0, 5).seq).toEqual(newGame(0, 5).seq); | |
| 18 | const seen = new Set(Array.from({ length: 40 }, (_, k) => peal(k, 6).join())); | |
| 19 | expect(seen.size).toBeGreaterThan(20); | |
| 20 | }); | |
| 21 | ||
| 22 | test('a peal only holds the four bells, and the first is three long', () => { | |
| 23 | for (const bell of peal(9, 200)) expect([0, 1, 2, 3]).toContain(bell); | |
| 24 | expect(new Set(peal(9, 200)).size).toBe(BELLS); | |
| 25 | expect(newGame(0, 3).seq.length).toBe(FIRST); | |
| 26 | expect(FIRST).toBe(3); | |
| 27 | }); | |
| 28 | ||
| 29 | test('the next peal is the last plus one bell', () => { | |
| 30 | const a = peal(11, 3); | |
| 31 | expect(extend(a, 11)).toEqual(peal(11, 4)); | |
| 32 | expect(extend(a, 11).slice(0, 3)).toEqual(a); | |
| 33 | }); | |
| 34 | ||
| 35 | test('the tower rings its peal bell by bell, each swinging, then it is the player\'s turn', () => { | |
| 36 | const s = newGame(0, 4); | |
| 37 | expect(s.phase).toBe('listen'); | |
| 38 | const ev = run(s, 6); | |
| 39 | expect(ev.filter(e => e.type === 'chime').map(e => e.bell)).toEqual(s.seq); | |
| 40 | expect(ev.some(e => e.type === 'turn')).toBe(true); | |
| 41 | expect(s.phase).toBe('ring'); | |
| 42 | }); | |
| 43 | ||
| 44 | test('pulling before the tower is done is ignored', () => { | |
| 45 | const s = newGame(0, 4); | |
| 46 | expect(pull(s, s.seq[0])).toEqual([]); | |
| 47 | expect(s.got).toBe(0); | |
| 48 | expect(s.score).toBe(0); | |
| 49 | }); | |
| 50 | ||
| 51 | test('a correct peal of length n scores n and the next one is a bell longer', () => { | |
| 52 | const s = newGame(0, 6); | |
| 53 | listen(s); | |
| 54 | const ev = ringBack(s); | |
| 55 | expect(ev.some(e => e.type === 'peal' && e.length === 3)).toBe(true); | |
| 56 | expect(s.score).toBe(3); | |
| 57 | const before = [...s.seq]; | |
| 58 | run(s, PAUSE + .1); | |
| 59 | expect(s.phase).toBe('listen'); | |
| 60 | expect(s.seq.length).toBe(4); | |
| 61 | expect(s.seq.slice(0, 3)).toEqual(before); | |
| 62 | listen(s); | |
| 63 | ringBack(s); | |
| 64 | expect(s.score).toBe(3 + 4); | |
| 65 | }); | |
| 66 | ||
| 67 | test('a wrong bell ends the peal with a miss and no score; the same peal is played again', () => { | |
| 68 | const s = newGame(0, 6); | |
| 69 | listen(s); | |
| 70 | const seq = [...s.seq], bad = wrongFor(s); | |
| 71 | const ev = pull(s, bad); | |
| 72 | expect(ev.find(e => e.type === 'miss')).toMatchObject({ bell: bad, wanted: seq[0], misses: 1 }); | |
| 73 | expect(s.misses).toBe(1); | |
| 74 | expect(s.score).toBe(0); | |
| 75 | expect(s.phase).toBe('between'); | |
| 76 | expect(pull(s, seq[0])).toEqual([]); | |
| 77 | run(s, PAUSE + .1); | |
| 78 | expect(s.phase).toBe('listen'); | |
| 79 | expect(s.seq).toEqual(seq); | |
| 80 | }); | |
| 81 | ||
| 82 | test('a wrong bell part way through a peal scores nothing for the bells before it', () => { | |
| 83 | const s = newGame(0, 8); | |
| 84 | listen(s); | |
| 85 | pull(s, s.seq[0]); | |
| 86 | run(s, SWING + .05); | |
| 87 | pull(s, (s.seq[1] + 1) % BELLS); | |
| 88 | expect(s.misses).toBe(1); | |
| 89 | expect(s.score).toBe(0); | |
| 90 | }); | |
| 91 | ||
| 92 | test('a bell still swinging cannot ring again, and rings once it has swung back', () => { | |
| 93 | let seed = 0, s; | |
| 94 | // find a peal that starts with the same bell twice, so a rushed second pull has to be turned away | |
| 95 | do { s = newGame(0, ++seed); } while (s.seq[0] !== s.seq[1]); | |
| 96 | listen(s); | |
| 97 | const bell = s.seq[0]; | |
| 98 | expect(pull(s, bell).some(e => e.type === 'ring')).toBe(true); | |
| 99 | expect(s.swing[bell]).toBeGreaterThan(0); | |
| 100 | expect(pull(s, bell)).toEqual([{ type: 'ignored', bell }]); | |
| 101 | expect(s.got).toBe(1); | |
| 102 | expect(s.misses).toBe(0); | |
| 103 | run(s, SWING + .05); | |
| 104 | expect(s.swing[bell]).toBe(0); | |
| 105 | expect(pull(s, bell).some(e => e.type === 'ring')).toBe(true); | |
| 106 | expect(s.got).toBe(2); | |
| 107 | }); | |
| 108 | ||
| 109 | test('the third miss ends the game, the second does not', () => { | |
| 110 | const s = newGame(0, 2); | |
| 111 | for (let miss = 1; miss <= MAX_MISSES; miss++) { | |
| 112 | listen(s); | |
| 113 | const ev = pull(s, wrongFor(s)); | |
| 114 | expect(s.misses).toBe(miss); | |
| 115 | if (miss < MAX_MISSES) { | |
| 116 | expect(s.phase).toBe('between'); | |
| 117 | expect(ev.some(e => e.type === 'over')).toBe(false); | |
| 118 | run(s, PAUSE + .1); | |
| 119 | } else { | |
| 120 | expect(s.phase).toBe('over'); | |
| 121 | expect(ev.some(e => e.type === 'over')).toBe(true); | |
| 122 | } | |
| 123 | } | |
| 124 | }); | |
| 125 | ||
| 126 | test('the score is kept as best, and only a better one is a record', () => { | |
| 127 | const s = newGame(2, 6); | |
| 128 | listen(s); ringBack(s); run(s, PAUSE + .1); | |
| 129 | for (let i = 0; i < MAX_MISSES; i++) { listen(s); pull(s, wrongFor(s)); run(s, PAUSE + .1); } | |
| 130 | expect(s.phase).toBe('over'); | |
| 131 | expect(s.best).toBe(3); | |
| 132 | expect(s.record).toBe(true); | |
| 133 | const low = newGame(50, 6); | |
| 134 | for (let i = 0; i < MAX_MISSES; i++) { listen(low); pull(low, wrongFor(low)); run(low, PAUSE + .1); } | |
| 135 | expect(low.best).toBe(50); | |
| 136 | expect(low.record).toBe(false); | |
| 137 | }); | |
| 138 | ||
| 139 | test('a new game starts only after the old one has been over a moment, carrying the best', () => { | |
| 140 | const s = newGame(0, 6); | |
| 141 | expect(again(s)).toBe(null); | |
| 142 | listen(s); ringBack(s); run(s, PAUSE + .1); | |
| 143 | for (let i = 0; i < MAX_MISSES; i++) { listen(s); pull(s, wrongFor(s)); if (i < MAX_MISSES - 1) run(s, PAUSE + .1); } | |
| 144 | expect(again(s)).toBe(null); | |
| 145 | run(s, 1); | |
| 146 | const next = again(s); | |
| 147 | expect(next.phase).toBe('listen'); | |
| 148 | expect(next.best).toBe(3); | |
| 149 | expect(next.score).toBe(0); | |
| 150 | expect(next.misses).toBe(0); | |
| 151 | expect(next.seq.length).toBe(3); | |
| 152 | }); | |
| 153 | ||
| 154 | test('nonsense is refused: unknown bells, bad times and a bad seed', () => { | |
| 155 | const s = newGame(NaN, NaN); | |
| 156 | expect(s.best).toBe(0); | |
| 157 | listen(s); | |
| 158 | for (const bad of [-1, 4, 1.5, NaN, undefined, '1']) expect(pull(s, bad)).toEqual([]); | |
| 159 | expect(s.misses).toBe(0); | |
| 160 | expect(advance(s, NaN)).toEqual([]); | |
| 161 | expect(advance(s, -5)).toEqual([]); | |
| 162 | }); |
index.html +1
| @@ -82,6 +82,7 @@ footer a::after { content: none; } | ||
| 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 | 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> |
| 85 | <li data-kind="Memory" data-input="Tap" style="--accent:#a8863a"><a href="bell-tower/">Bell Tower</a><span>Ring the peal back: the tower plays a sequence on four bells, pull the ropes in the same order, each round one bell longer</span><span>Game forged by the crew</span><span class="tags">Memory · Tap</span></li> | |
| 85 | 86 | <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> |
| 86 | 87 | <li data-kind="Sport" data-input="Drag" style="--accent:#b33a3a"><a href="archery/">Archery</a><span>Ten arrows at a straw target across a meadow: drag back to draw, read the pennant for wind, gold scores 10</span><span>Game forged by the crew</span><span class="tags">Sport · Drag</span></li> |
| 87 | 88 | <li data-kind="Precision" data-input="Hold" style="--accent:#c9a227"><a href="tram-line/">Tram Line</a><span>Drive a tram along eight stops: brake so the door lands on the yellow mark, exact scores 3, overshoot scores nothing</span><span>Game forged by the crew</span><span class="tags">Precision · Hold</span></li> |
og/bell-tower.png added
Binary file not shown.
og/home.png
Binary file not shown.
serve.test.mjs +8 −1
| @@ -177,6 +177,13 @@ test('Tightrope and its rules script are served without a track', async () => { | ||
| 177 | 177 | expect((await fetch(base + '/tightrope/wire.js')).status).toBe(200); |
| 178 | 178 | }); |
| 179 | 179 | |
| 180 | test('Bell Tower and its rules script are served without a track', async () => { | |
| 181 | const page = await fetch(base + '/bell-tower/'); | |
| 182 | expect(page.status).toBe(200); | |
| 183 | expect(page.headers.get('content-type')).toBe('text/html; charset=utf-8'); | |
| 184 | expect((await fetch(base + '/bell-tower/peal.js')).status).toBe(200); | |
| 185 | }); | |
| 186 | ||
| 180 | 187 | test('Ice Fishing and its rules script are served without a track', async () => { |
| 181 | 188 | const page = await fetch(base + '/ice-fishing/'); |
| 182 | 189 | expect(page.status).toBe(200); |
| @@ -307,7 +314,7 @@ test('every game has a kind, input and accent, and the static list carries them' | ||
| 307 | 314 | delete globalThis.PixelPlays; |
| 308 | 315 | } |
| 309 | 316 | for (const g of games) { |
| 310 | expect(['Sport', 'Throwing', 'Timing', 'Catching', 'Precision', 'Racing']).toContain(g.kind); | |
| 317 | expect(['Sport', 'Throwing', 'Timing', 'Catching', 'Precision', 'Racing', 'Memory']).toContain(g.kind); | |
| 311 | 318 | expect(['Tap', 'Hold', 'Drag']).toContain(g.input); |
| 312 | 319 | expect(g.accent).toMatch(/^#[0-9a-f]{6}$/i); |
| 313 | 320 | expect(html).toContain(`<li data-kind="${g.kind}" data-input="${g.input}" style="--accent:${g.accent}"><a href="${g.id}/">`); |
shell.js +2 −1
| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | 'use strict'; |
| 2 | 2 | // Shared chrome for every game: game menu (top centre) and rules popup (top right). |
| 3 | 3 | window.PixelPlays = (() => { |
| 4 | // add a game here and give it a folder, a one-line `blurb`, a `kind` (Sport, Throwing, Timing, Catching, Precision, Racing), an `input` (Tap, Hold, Drag) and an `accent` colour for its card (the home page lists it from here) (`music: false` when it has no track, `forged: true` when it was made on the forge rather than by the original creators); `soon` entries show in the menu but do not link | |
| 4 | // add a game here and give it a folder, a one-line `blurb`, a `kind` (Sport, Throwing, Timing, Catching, Precision, Racing, Memory), an `input` (Tap, Hold, Drag) and an `accent` colour for its card (the home page lists it from here) (`music: false` when it has no track, `forged: true` when it was made on the forge rather than by the original creators); `soon` entries show in the menu but do not link | |
| 5 | 5 | const GAMES = [ |
| 6 | 6 | { id: 'balote', title: 'Balote', blurb: 'Bocce against a bot, first to 5', kind: 'Throwing', input: 'Drag', accent: '#d9a441' }, |
| 7 | 7 | { id: 'stari-most', title: 'Stari Most', blurb: 'Five dives off the old bridge, flip and enter the water vertically', kind: 'Timing', input: 'Hold', accent: '#3f8fb5' }, |
| @@ -26,6 +26,7 @@ window.PixelPlays = (() => { | ||
| 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 | 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 }, |
| 29 | { id: 'bell-tower', title: 'Bell Tower', blurb: 'Ring the peal back: the tower plays a sequence on four bells, pull the ropes in the same order, each round one bell longer', kind: 'Memory', input: 'Tap', accent: '#a8863a', music: false, forged: true }, | |
| 29 | 30 | { 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 }, |
| 30 | 31 | { id: 'archery', title: 'Archery', blurb: 'Ten arrows at a straw target across a meadow: drag back to draw, read the pennant for wind, gold scores 10', kind: 'Sport', input: 'Drag', accent: '#b33a3a', music: false, forged: true }, |
| 31 | 32 | { id: 'tram-line', title: 'Tram Line', blurb: 'Drive a tram along eight stops: brake so the door lands on the yellow mark, exact scores 3, overshoot scores nothing', kind: 'Precision', input: 'Hold', accent: '#c9a227', music: false, forged: true }, |
new above
offline
seqwas just extended inPeal.advance, but the HUD isn't redrawn here, so#lengthshows the previous peal's length for the whole round. Useif (e.type === 'listen') { note(...); renderHud(); }, plus a page test asserting#lengthreads 4 at the second turn.