Public: anyone can read this; the crew can change it.
Migrated from github.com/amirmushichge/pixel-plays
Add Lighthouse: sweep the beam to turn boats away from the rocks #34
Check · Review · Land · Live done — Landed and live in production.
quill/pixel-plays:quill/21-add-lighthouse-sweep-the-beam-to-turn-bo → main 4 commits fork
7 files
changed +579
−1
README.md +2 −1
| @@ -23,6 +23,7 @@ Concept by [abhi](https://x.com/awwbhi2). Game by [AmirMushich](https://x.com/Am | ||
| 23 | 23 | | [](pottery-wheel/) | Three pots on a kick wheel: hold to centre the clay without pressing too hard, then pull the wall up, too fast tears it and too slow slumps, and each pot has 40 seconds (clay still being centred then dries and is lost, a pull is fired as it stands); the score is the total height in pixels (forged by the crew on the engine and shell above, not by the original creators) | Hold to press, drag up to pull; space, up arrow, F to fire | |
| 24 | 24 | | [](row-home/) | Race a rowing boat across the river against a bot: stroke in rhythm with the glide, too early catches a crab, hold left or right to steer against the current (forged by the crew on the engine and shell above, not by the original creators) | Tap or space to stroke; hold left/right | |
| 25 | 25 | | [](orchard/) | Catch falling apples in a basket: ripe 1, golden 3, a wasp-bitten brown one costs a life, three lives end the round (forged by the crew on the engine and shell above, not by the original creators) | Drag; left/right arrows | |
| 26 | | [](lighthouse/) | A 90-second night: sweep the lighthouse beam so boats drifting in towards the rocks are held in the light for a second and turn away, +1 each; three boats lost on the rocks end the night early (forged by the crew on the engine and shell above, not by the original creators) | Drag; hold left/right | | |
| 26 | 27 | |
| 27 | 28 | ## Run |
| 28 | 29 | Requires [](https://nodejs.org/); nothing to install. |
| @@ -36,7 +37,7 @@ Then open http://127.0.0.1:5300/. `PORT` and `HOST` override the port and addres | ||
| 36 | 37 | On the forge, `forge.json` runs `bun serve.mjs` with health check `/`. The app keeps no state, so it does not need `FORGE_APP_DATA`. `bun test` checks that the server answers every game page and track. |
| 37 | 38 | |
| 38 | 39 | ## Add a game |
| 39 | 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 (a static fallback list for no-JS is kept in sync by `serve.test.mjs`, so add the matching `<li>` to `index.html` too). 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 and Orchard 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`. | |
| 40 | 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 (a static fallback list for no-JS is kept in sync by `serve.test.mjs`, so add the matching `<li>` to `index.html` too). 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 and Lighthouse 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`. | |
| 40 | 41 | |
| 41 | 42 | - `shell.js` provides the game menu, the Rules popup, the credits and the background music. |
| 42 | 43 | - `engine.js` provides the camera, depth buffer, colour ramps, shadows, edges, figures, trees and wind as globals. |
index.html +1
| @@ -38,6 +38,7 @@ footer a { display: inline; padding: 0; font-weight: inherit; text-decoration: u | ||
| 38 | 38 | <li><a href="pottery-wheel/">Pottery Wheel</a><span>Shape a pot on a kick wheel: hold to centre the clay without collapsing it, then pull the wall as tall as you can before it tears</span><span>Game forged by the crew</span></li> |
| 39 | 39 | <li><a href="row-home/">Row Home</a><span>Race a rowing boat across the river against a bot: stroke in rhythm with the glide, too early catches a crab</span><span>Game forged by the crew</span></li> |
| 40 | 40 | <li><a href="orchard/">Orchard</a><span>Catch falling apples in a basket: ripe 1, golden 3, a wasp-bitten brown one costs a life</span><span>Game forged by the crew</span></li> |
| 41 | <li><a href="lighthouse/">Lighthouse</a><span>Sweep the lighthouse beam to turn boats away from the rocks: hold one in the light for a second, three wrecks end the night</span><span>Game forged by the crew</span></li> | |
| 41 | 42 | </ul> |
| 42 | 43 | <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.</footer> |
| 43 | 44 | </main> |
lighthouse/beam.js added +92
| @@ -0,0 +1,92 @@ | ||
| 1 | 'use strict'; | |
| 2 | // The night watch: boats drift in towards the rocks, the beam turns them away. Plain numbers in, plain numbers out, no DOM. | |
| 3 | (function (root) { | |
| 4 | const NIGHT = 90, LIVES = 3, ROCK_R = 3, SPAWN_R = 15, BEAM_RANGE = 14.5; | |
| 5 | const ARC = .52, DWELL = 1, DECAY = 2; // beam width in radians; seconds a boat must stay lit; dwell drains this much faster than it fills | |
| 6 | const TURN_RATE = 3.2, KEY_RATE = 2.2; // rad/s the beam follows a drag; rad/s while an arrow is held | |
| 7 | const SPAWN_EVERY = [2.6, .9], SPEED = [1.2, 2], AWAY_SPEED = 2.6, SWIRL = .05; | |
| 8 | const TAU = Math.PI * 2; | |
| 9 | ||
| 10 | const finite = v => (Number.isFinite(v) ? v : 0); | |
| 11 | const wrap = a => { a = finite(a) % TAU; return a > Math.PI ? a - TAU : a < -Math.PI ? a + TAU : a; }; | |
| 12 | const angleOf = (x, y) => Math.atan2(finite(y), finite(x)); | |
| 13 | const mix = (pair, t) => pair[0] + (pair[1] - pair[0]) * Math.min(1, Math.max(0, t)); | |
| 14 | // mulberry32 over a number kept in the state, so a night replays from its seed | |
| 15 | function random(s) { | |
| 16 | s.rng = (s.rng + 0x6D2B79F5) >>> 0; | |
| 17 | let t = s.rng; | |
| 18 | t = Math.imul(t ^ t >>> 15, t | 1); | |
| 19 | t ^= t + Math.imul(t ^ t >>> 7, t | 61); | |
| 20 | return ((t ^ t >>> 14) >>> 0) / 4294967296; | |
| 21 | } | |
| 22 | function place(b) { b.x = Math.cos(b.a) * b.r; b.y = Math.sin(b.a) * b.r; return b; } | |
| 23 | ||
| 24 | function newNight(best = 0, seed = 1) { | |
| 25 | return { phase: 'ready', time: 0, angle: -Math.PI / 4, target: null, boats: [], saved: 0, lives: LIVES, best: Math.max(0, finite(best)), record: false, spawnIn: 0, nextId: 1, rng: seed >>> 0, ended: null }; | |
| 26 | } | |
| 27 | function begin(s) { | |
| 28 | if (s.phase === 'ready') s.phase = 'play'; | |
| 29 | return s.phase === 'play'; | |
| 30 | } | |
| 31 | // a boat at angle a, radius r (default: the edge of the water), drifting inwards | |
| 32 | function spawn(s, a = (random(s) * 2 - 1) * Math.PI, r = SPAWN_R) { | |
| 33 | const rush = s.time / NIGHT; | |
| 34 | const boat = place({ id: s.nextId++, a: wrap(a), r, speed: mix(SPEED, rush) * (.9 + random(s) * .2), swirl: (random(s) < .5 ? -1 : 1) * SWIRL, dwell: 0, state: 'in', lit: false }); | |
| 35 | s.boats.push(boat); | |
| 36 | return boat; | |
| 37 | } | |
| 38 | // the beam follows a drag towards a world angle; arrows move it directly | |
| 39 | function aimAt(s, angle) { s.target = wrap(angle); } | |
| 40 | function steer(s, dir, dt) { | |
| 41 | if (!dir) return; | |
| 42 | s.target = null; | |
| 43 | s.angle = wrap(s.angle + Math.sign(dir) * KEY_RATE * Math.max(0, finite(dt))); | |
| 44 | } | |
| 45 | const inBeam = (s, b) => b.r > ROCK_R && b.r <= BEAM_RANGE && Math.abs(wrap(b.a - s.angle)) <= ARC / 2; | |
| 46 | ||
| 47 | // advance the night by dt seconds; returns what happened: [{ type: 'saved' | 'lost' | 'dawn' | 'wreck', boat? }] | |
| 48 | function advance(s, dt) { | |
| 49 | const events = []; | |
| 50 | if (s.phase !== 'play') return events; | |
| 51 | dt = Math.max(0, Math.min(.25, finite(dt))); | |
| 52 | if (s.target != null) { | |
| 53 | const gap = wrap(s.target - s.angle), turn = TURN_RATE * dt; | |
| 54 | s.angle = Math.abs(gap) <= turn ? s.target : wrap(s.angle + Math.sign(gap) * turn); | |
| 55 | } | |
| 56 | s.time += dt; | |
| 57 | s.spawnIn -= dt; | |
| 58 | while (s.spawnIn <= 0 && s.time < NIGHT) { | |
| 59 | spawn(s); | |
| 60 | s.spawnIn += mix(SPAWN_EVERY, s.time / NIGHT); | |
| 61 | } | |
| 62 | for (const b of s.boats) { | |
| 63 | if (b.state === 'away') { b.r += AWAY_SPEED * dt; place(b); continue; } | |
| 64 | b.lit = inBeam(s, b); | |
| 65 | b.dwell = b.lit ? b.dwell + dt : Math.max(0, b.dwell - DECAY * dt); | |
| 66 | if (b.dwell >= DWELL) { | |
| 67 | b.state = 'away'; b.lit = false; b.dwell = DWELL; | |
| 68 | s.saved++; | |
| 69 | if (s.saved > s.best) { s.best = s.saved; s.record = true; } | |
| 70 | events.push({ type: 'saved', boat: b }); | |
| 71 | continue; | |
| 72 | } | |
| 73 | b.r -= b.speed * dt; | |
| 74 | b.a = wrap(b.a + b.swirl * dt); | |
| 75 | place(b); | |
| 76 | if (b.r <= ROCK_R) { | |
| 77 | b.state = 'lost'; | |
| 78 | s.lives--; | |
| 79 | events.push({ type: 'lost', boat: b }); | |
| 80 | } | |
| 81 | } | |
| 82 | s.boats = s.boats.filter(b => b.state === 'in' || (b.state === 'away' && b.r < SPAWN_R + 1)); | |
| 83 | if (s.lives <= 0) { s.phase = 'over'; s.ended = 'wreck'; events.push({ type: 'wreck' }); } | |
| 84 | else if (s.time >= NIGHT) { s.phase = 'over'; s.ended = 'dawn'; events.push({ type: 'dawn' }); } | |
| 85 | return events; | |
| 86 | } | |
| 87 | // after a night is over, a new one keeps the best | |
| 88 | function again(s) { return s.phase === 'over' ? newNight(s.best, s.rng + 1) : null; } | |
| 89 | ||
| 90 | const api = { NIGHT, LIVES, ROCK_R, SPAWN_R, BEAM_RANGE, ARC, DWELL, DECAY, TURN_RATE, KEY_RATE, SPAWN_EVERY, SPEED, newNight, begin, spawn, aimAt, steer, advance, again, inBeam, angleOf, wrap }; | |
| 91 | if (typeof module !== 'undefined') module.exports = api; else root.Beam = api; | |
| 92 | })(typeof window !== 'undefined' ? window : globalThis); |
lighthouse/beam.test.mjs added +162
| @@ -0,0 +1,162 @@ | ||
| 1 | import { test, expect } from 'bun:test'; | |
| 2 | import { newNight, begin, spawn, aimAt, steer, advance, again, inBeam, wrap, NIGHT, LIVES, ROCK_R, SPAWN_R, ARC, DWELL, KEY_RATE, SPEED } from './beam.js'; | |
| 3 | ||
| 4 | // a night with no arrivals of its own, so a test places the boats it cares about | |
| 5 | function quiet() { | |
| 6 | const s = newNight(0, 7); | |
| 7 | begin(s); | |
| 8 | s.spawnIn = 1e9; | |
| 9 | s.angle = 0; | |
| 10 | return s; | |
| 11 | } | |
| 12 | const run = (s, seconds, dt = .05) => { const out = []; for (let t = 0; t < seconds - 1e-9; t += dt) out.push(...advance(s, dt)); return out; }; | |
| 13 | ||
| 14 | test('a night waits for the first input, then runs', () => { | |
| 15 | const s = newNight(); | |
| 16 | expect(s.phase).toBe('ready'); | |
| 17 | expect(advance(s, 1)).toEqual([]); | |
| 18 | expect(s.time).toBe(0); | |
| 19 | expect(begin(s)).toBe(true); | |
| 20 | advance(s, .1); | |
| 21 | expect(s.time).toBeCloseTo(.1); | |
| 22 | expect(s.boats.length).toBe(1); | |
| 23 | }); | |
| 24 | ||
| 25 | test('a boat held in the beam for a second turns away safely and scores 1', () => { | |
| 26 | const s = quiet(); | |
| 27 | const b = spawn(s, 0, 10); | |
| 28 | const events = run(s, DWELL + .2); | |
| 29 | expect(events.filter(e => e.type === 'saved').length).toBe(1); | |
| 30 | expect(b.state).toBe('away'); | |
| 31 | expect(s.saved).toBe(1); | |
| 32 | expect(s.lives).toBe(LIVES); | |
| 33 | const r = b.r; | |
| 34 | run(s, .5); | |
| 35 | expect(b.r).toBeGreaterThan(r); | |
| 36 | }); | |
| 37 | ||
| 38 | test('a boat outside the beam keeps coming and is not saved', () => { | |
| 39 | const s = quiet(); | |
| 40 | const b = spawn(s, Math.PI / 2, 10); | |
| 41 | expect(inBeam(s, b)).toBe(false); | |
| 42 | const r = b.r; | |
| 43 | run(s, 1); | |
| 44 | expect(b.r).toBeLessThan(r); | |
| 45 | expect(b.dwell).toBe(0); | |
| 46 | expect(b.state).toBe('in'); | |
| 47 | expect(s.saved).toBe(0); | |
| 48 | }); | |
| 49 | ||
| 50 | test('a boat that slips out of the beam before a second is up starts again', () => { | |
| 51 | const s = quiet(); | |
| 52 | const b = spawn(s, 0, 12); | |
| 53 | run(s, DWELL * .6); | |
| 54 | expect(b.dwell).toBeGreaterThan(.4); | |
| 55 | s.angle = Math.PI; | |
| 56 | run(s, 1); | |
| 57 | expect(b.dwell).toBe(0); | |
| 58 | s.angle = 0; | |
| 59 | const events = run(s, DWELL * .7); | |
| 60 | expect(events.some(e => e.type === 'saved')).toBe(false); | |
| 61 | }); | |
| 62 | ||
| 63 | test('the beam covers its arc and no more', () => { | |
| 64 | const s = quiet(); | |
| 65 | const inside = spawn(s, ARC / 2 - .02, 8), outside = spawn(s, ARC / 2 + .02, 8); | |
| 66 | expect(inBeam(s, inside)).toBe(true); | |
| 67 | expect(inBeam(s, outside)).toBe(false); | |
| 68 | s.angle = Math.PI - .1; | |
| 69 | const across = spawn(s, -Math.PI + .1, 8); // across the seam at +-pi | |
| 70 | expect(inBeam(s, across)).toBe(true); | |
| 71 | }); | |
| 72 | ||
| 73 | test('a boat reaching the rocks costs a life and is gone', () => { | |
| 74 | const s = quiet(); | |
| 75 | s.angle = Math.PI; | |
| 76 | const b = spawn(s, 0, ROCK_R + .5); | |
| 77 | const events = run(s, 2); | |
| 78 | expect(events.filter(e => e.type === 'lost').length).toBe(1); | |
| 79 | expect(s.lives).toBe(LIVES - 1); | |
| 80 | expect(s.boats.includes(b)).toBe(false); | |
| 81 | }); | |
| 82 | ||
| 83 | test('three lost boats end the night early', () => { | |
| 84 | const s = quiet(); | |
| 85 | s.angle = Math.PI; | |
| 86 | for (let n = 0; n < LIVES; n++) spawn(s, 0, ROCK_R + .3); | |
| 87 | const events = run(s, 1); | |
| 88 | expect(s.lives).toBe(0); | |
| 89 | expect(s.phase).toBe('over'); | |
| 90 | expect(s.ended).toBe('wreck'); | |
| 91 | expect(events.some(e => e.type === 'wreck')).toBe(true); | |
| 92 | expect(s.time).toBeLessThan(NIGHT); | |
| 93 | expect(advance(s, 1)).toEqual([]); | |
| 94 | }); | |
| 95 | ||
| 96 | test('the night lasts 90 seconds and ends at dawn', () => { | |
| 97 | const s = quiet(); | |
| 98 | const events = run(s, NIGHT + 1, .1); | |
| 99 | expect(s.phase).toBe('over'); | |
| 100 | expect(s.ended).toBe('dawn'); | |
| 101 | expect(events.at(-1)).toEqual({ type: 'dawn' }); | |
| 102 | expect(NIGHT).toBe(90); | |
| 103 | }); | |
| 104 | ||
| 105 | test('boats arrive faster and drift faster as the night goes on', () => { | |
| 106 | const arrivals = from => { | |
| 107 | const s = newNight(0, 3); | |
| 108 | begin(s); | |
| 109 | s.time = from; | |
| 110 | s.angle = 0; | |
| 111 | s.lives = 999; | |
| 112 | const before = s.nextId; | |
| 113 | run(s, 20); | |
| 114 | return s.nextId - before; | |
| 115 | }; | |
| 116 | expect(arrivals(60)).toBeGreaterThan(arrivals(0) * 1.5); | |
| 117 | const early = newNight(0, 3), late = newNight(0, 3); | |
| 118 | late.time = NIGHT * .9; | |
| 119 | expect(spawn(late).speed).toBeGreaterThan(spawn(early).speed); | |
| 120 | expect(SPEED[1]).toBeGreaterThan(SPEED[0]); | |
| 121 | }); | |
| 122 | ||
| 123 | test('boats come in from the edge of the water', () => { | |
| 124 | const s = newNight(0, 5); | |
| 125 | for (let n = 0; n < 20; n++) { | |
| 126 | const b = spawn(s); | |
| 127 | expect(Math.hypot(b.x, b.y)).toBeCloseTo(SPAWN_R); | |
| 128 | } | |
| 129 | }); | |
| 130 | ||
| 131 | test('the arrows turn the beam, a drag aims it, and bad numbers change nothing', () => { | |
| 132 | const s = quiet(); | |
| 133 | steer(s, 1, 1); | |
| 134 | expect(s.angle).toBeCloseTo(KEY_RATE); | |
| 135 | steer(s, -1, 1); | |
| 136 | expect(s.angle).toBeCloseTo(0); | |
| 137 | steer(s, 0, 1); | |
| 138 | steer(s, 1, NaN); | |
| 139 | expect(s.angle).toBeCloseTo(0); | |
| 140 | aimAt(s, 1.5); | |
| 141 | run(s, .1); | |
| 142 | expect(s.angle).toBeGreaterThan(0); | |
| 143 | expect(s.angle).toBeLessThan(1.5); | |
| 144 | run(s, 2); | |
| 145 | expect(s.angle).toBeCloseTo(1.5); | |
| 146 | aimAt(s, 7 * Math.PI); | |
| 147 | expect(s.target).toBeCloseTo(wrap(Math.PI)); | |
| 148 | steer(s, 1, .1); | |
| 149 | expect(s.target).toBe(null); | |
| 150 | }); | |
| 151 | ||
| 152 | test('a new night keeps the best', () => { | |
| 153 | const s = quiet(); | |
| 154 | spawn(s, 0, 10); | |
| 155 | run(s, DWELL + .2); | |
| 156 | s.phase = 'over'; | |
| 157 | const next = again(s); | |
| 158 | expect(next.best).toBe(1); | |
| 159 | expect(next.saved).toBe(0); | |
| 160 | expect(next.phase).toBe('ready'); | |
| 161 | expect(again(next)).toBe(null); | |
| 162 | }); |
lighthouse/index.html added +314
| @@ -0,0 +1,314 @@ | ||
| 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>Lighthouse</title> | |
| 7 | <style> | |
| 8 | :root { : #1c1b19; : #8b877f; : #fff; : #c4503c; } | |
| 9 | * { box-sizing: border-box; } | |
| 10 | html, body { height: 100%; margin: 0; background: var(); color: var(); } | |
| 11 | body { font: 13px/1.35 ui-monospace, "Cascadia Mono", Consolas, Menlo, monospace; overflow: hidden; } | |
| 12 | #stage { position: fixed; inset: 0; display: grid; place-items: center; } | |
| 13 | canvas { display: block; image-rendering: pixelated; touch-action: none; cursor: crosshair; outline: none; } | |
| 14 | .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; } | |
| 15 | #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; } | |
| 16 | #bottom { bottom: max(14px, env(safe-area-inset-bottom)); align-items: flex-end; } | |
| 17 | .row { display: flex; align-items: center; gap: 8px; color: var(); } | |
| 18 | .row b { font-size: 16px; font-weight: 600; min-width: 1ch; font-variant-numeric: tabular-nums; } | |
| 19 | .row span { color: var(); } | |
| 20 | .dots { display: flex; gap: 3px; } | |
| 21 | .dots i { width: 6px; height: 6px; background: var(); } | |
| 22 | .dots i.off { opacity: .16; } | |
| 23 | #status { color: var(); } | |
| 24 | button { pointer-events: auto; font: inherit; color: var(); background: none; border: 0; padding: 10px; margin: -10px; cursor: pointer; } | |
| 25 | button:hover, button:focus-visible { color: var(); } | |
| 26 | </style> | |
| 27 | </head> | |
| 28 | <body> | |
| 29 | <div id="stage"><canvas id="c" tabindex="0" aria-label="A lighthouse on a rocky islet at night, with small boats drifting in towards the rocks. Drag across the picture, or hold the left and right arrow keys, to turn the beam. Keep a boat in the light for a second to turn it away; three lost boats end the night."></canvas></div> | |
| 30 | <div id="scores"> | |
| 31 | <div class="row"><b id="saved">0</b><span>boats saved</span><span class="dots" id="lives"></span></div> | |
| 32 | <div class="row"><b id="best">0</b><span>best</span><span id="clock"></span></div> | |
| 33 | </div> | |
| 34 | <div class="hud" id="bottom"> | |
| 35 | <div id="status" role="status" aria-live="polite"></div> | |
| 36 | <button id="mute" type="button" aria-pressed="false">sound on</button> | |
| 37 | </div> | |
| 38 | <script src="../shell.js"></script> | |
| 39 | <script> | |
| 40 | PixelPlays.mount({ | |
| 41 | id: 'lighthouse', | |
| 42 | rules: [ | |
| 43 | 'Drag, or hold the left and right arrows, to turn the lighthouse beam.', | |
| 44 | 'Hold a boat in the light for a second and it turns away safely: +1.', | |
| 45 | 'A boat that reaches the rocks is lost; three lost boats end the night.', | |
| 46 | 'The night lasts 90 seconds and the boats come faster.', | |
| 47 | ], | |
| 48 | }); | |
| 49 | </script> | |
| 50 | <script>window.PP_SCENE = { originY: 10, shadowX: 1.3, shadowY: -.12 };</script> | |
| 51 | <script src="../engine.js"></script> | |
| 52 | <script src="beam.js"></script> | |
| 53 | <script> | |
| 54 | 'use strict'; | |
| 55 | ||
| 56 | const BEAM = window.Beam; | |
| 57 | const KIND_ROCK = 6, KIND_WATER = 7; | |
| 58 | const WATER_RADIUS = 15.6, ISLET = 2.9, TURF = 1.75; | |
| 59 | const WATER = [[16, 28, 56], [22, 38, 70], [28, 48, 86]], ROCK = [[96, 98, 112], [118, 118, 128], [140, 138, 144]]; | |
| 60 | const RED = [206, 70, 56], WHITE = [238, 234, 224], GLASS = [255, 226, 140], STONE = [104, 106, 122]; | |
| 61 | const HULLS = [[168, 112, 66], [86, 124, 160], [196, 96, 70]], DIM_SAIL = [112, 124, 168], BRIGHT_SAIL = [252, 246, 222]; | |
| 62 | const BEAM_TINT = [255, 236, 172], BEAM_MIX = [0, .16, .3, .5]; | |
| 63 | const TOWER_H = 6; | |
| 64 | const polar = new Float32Array(W * H), angle = new Float32Array(W * H), dark = new Uint8Array(W * H); | |
| 65 | ||
| 66 | /* ---------- ground ---------- */ | |
| 67 | function groundColor(x, y, ix, iy, i) { | |
| 68 | const r = Math.hypot(x, y), rough = r + (noise(x * 1.1, y * 1.1) - .5) * 1.5; | |
| 69 | if (rough > WATER_RADIUS + 1) return 0; | |
| 70 | if (rough > WATER_RADIUS - .9 && hash(ix, iy) < (rough - WATER_RADIUS + .9) / 1.9) return 0; | |
| 71 | polar[i] = r; angle[i] = Math.atan2(y, x); | |
| 72 | const islet = ISLET + (noise(x * .8 + 9, y * .8) - .5) * 1.4; | |
| 73 | if (r < islet) { | |
| 74 | if (r < TURF + (noise(x * 1.4, y * 1.4) - .5) * .7) { | |
| 75 | kinds[i] = KIND_GRASS; | |
| 76 | const t = noise(x * .9, y * .9) * 1.15 + (hash(ix, iy) - .5) * .55 - .08; | |
| 77 | return rgb(...GRASS[clamp(Math.floor(t * 4.2), 0, 3)]); | |
| 78 | } | |
| 79 | kinds[i] = KIND_ROCK; | |
| 80 | return rgb(...ROCK[clamp(Math.floor(noise(x * 1.6, y * 1.6) * 3 + (bayerAt(ix, iy) - .5) * .8), 0, 2)]); | |
| 81 | } | |
| 82 | kinds[i] = KIND_WATER; dark[i] = 1; | |
| 83 | return rgb(...WATER[clamp(Math.floor(noise(x * .55, y * .8) * 3 + (bayerAt(ix, iy) - .5) * .5), 0, 2)]); | |
| 84 | } | |
| 85 | ||
| 86 | function lighthouse() { | |
| 87 | const stripe = z => (Math.floor(z / 1.5) % 2 ? WHITE : RED), radius = z => lerp(1, .66, z / TOWER_H); | |
| 88 | for (let z = 0; z < TOWER_H; z += .05) capsule([0, 0, z], [0, 0, z + .06], radius(z), radius(z), stripe, undefined); | |
| 89 | for (let n = 0; n < 18; n++) { | |
| 90 | const a0 = n / 18 * Math.PI * 2, a1 = (n + 1) / 18 * Math.PI * 2; | |
| 91 | capsule([Math.cos(a0) * .9, Math.sin(a0) * .9, TOWER_H + .1], [Math.cos(a1) * .9, Math.sin(a1) * .9, TOWER_H + .1], .1, .1, () => STONE, 'solid'); | |
| 92 | } | |
| 93 | capsule([0, 0, TOWER_H + .15], [0, 0, TOWER_H + .95], .5, .5, () => GLASS, 'gloss'); | |
| 94 | capsule([0, 0, TOWER_H + .95], [0, 0, TOWER_H + 1.9], .66, .06, () => RED); | |
| 95 | } | |
| 96 | function rocks() { | |
| 97 | for (let n = 0; n < 16; n++) { | |
| 98 | const a = n / 16 * Math.PI * 2 + hash(n, 1), r = 2.1 + hash(n, 2) * .7, size = .35 + hash(n, 3) * .4; | |
| 99 | const x = Math.cos(a) * r, y = Math.sin(a) * r; | |
| 100 | sphere(x, y, size * .3, size, ROCK[1], 'rough'); | |
| 101 | castShadow(x, y, size, size); | |
| 102 | } | |
| 103 | } | |
| 104 | ||
| 105 | function bake() { | |
| 106 | zb.fill(-1e9); | |
| 107 | pix.fill(rgb(255, 255, 255)); | |
| 108 | for (let iy = 0; iy < H; iy++) for (let ix = 0; ix < W; ix++) { | |
| 109 | const g = unproject(ix + .5, iy + .5), i = iy * W + ix, c = groundColor(g.x, g.y, ix, iy, i); | |
| 110 | if (!c) continue; | |
| 111 | pix[i] = c; ground[i] = 1; | |
| 112 | zb[i] = (g.x + g.y) / R2 * COS; | |
| 113 | } | |
| 114 | growGrass(); | |
| 115 | groundZ.set(zb); | |
| 116 | lighthouse(); | |
| 117 | rocks(); | |
| 118 | for (let z = 0; z <= TOWER_H + 1.9; z += .6) for (let k = -1; k <= 1; k++) castShadow(k * .35, 0, z, .55); | |
| 119 | staticShadow.set(shadowMask); | |
| 120 | applyShadows(groundZ); | |
| 121 | bakedPix.set(pix); | |
| 122 | bakedZ.set(zb); | |
| 123 | } | |
| 124 | ||
| 125 | /* ---------- game state ---------- */ | |
| 126 | const nodes = { | |
| 127 | saved: document.getElementById('saved'), best: document.getElementById('best'), lives: document.getElementById('lives'), | |
| 128 | clock: document.getElementById('clock'), status: document.getElementById('status'), | |
| 129 | }; | |
| 130 | const G = { time: 0, status: '', clock: -1 }; | |
| 131 | let N = BEAM.newNight(readBest()); | |
| 132 | function readBest() { try { return Number(localStorage.getItem('pixel-plays-lighthouse-best')) || 0; } catch (e) { return 0; } } | |
| 133 | function renderHud() { | |
| 134 | nodes.saved.textContent = N.saved; | |
| 135 | nodes.best.textContent = N.best; | |
| 136 | if (nodes.lives.children.length !== BEAM.LIVES) nodes.lives.innerHTML = '<i></i>'.repeat(BEAM.LIVES); | |
| 137 | [...nodes.lives.children].forEach((el, k) => el.classList.toggle('off', k >= N.lives)); | |
| 138 | const left = Math.max(0, Math.ceil(BEAM.NIGHT - N.time)); | |
| 139 | if (left !== G.clock) { G.clock = left; nodes.clock.textContent = left + 's left'; } | |
| 140 | } | |
| 141 | function setStatus(text) { | |
| 142 | if (text === G.status) return; | |
| 143 | G.status = text; | |
| 144 | nodes.status.textContent = text; | |
| 145 | } | |
| 146 | const READY = 'drag or hold ← → to turn the beam'; | |
| 147 | function start() { | |
| 148 | const next = BEAM.again(N); | |
| 149 | if (next) { | |
| 150 | N = next; G.clock = -1; | |
| 151 | setStatus(READY); | |
| 152 | renderHud(); | |
| 153 | return; | |
| 154 | } | |
| 155 | BEAM.begin(N); | |
| 156 | if (N.phase === 'play') setStatus('keep a boat in the light for a second'); | |
| 157 | } | |
| 158 | function update(dt) { | |
| 159 | G.time += dt; | |
| 160 | if (N.phase === 'play') BEAM.steer(N, keys.right - keys.left, dt); | |
| 161 | const events = BEAM.advance(N, dt); | |
| 162 | for (const e of events) { | |
| 163 | if (e.type === 'saved') { sound('bell'); setStatus('safe · +1'); } | |
| 164 | else if (e.type === 'lost') { sound('crash'); setStatus('a boat hit the rocks'); } | |
| 165 | else if (e.type === 'dawn') setStatus(`dawn · ${N.saved} boats saved · tap to play again`); | |
| 166 | else if (e.type === 'wreck') setStatus(`${N.saved} boats saved before the third wreck · tap to play again`); | |
| 167 | } | |
| 168 | if (events.length) { | |
| 169 | renderHud(); | |
| 170 | if (N.phase === 'over' && N.record) try { localStorage.setItem('pixel-plays-lighthouse-best', String(N.best)); } catch (e) { /* private window: keep it for this visit */ } | |
| 171 | } | |
| 172 | else if (N.phase === 'play') renderHud(); | |
| 173 | } | |
| 174 | ||
| 175 | /* ---------- drawing ---------- */ | |
| 176 | // the beam lights the water in dithered bands, brightest in the middle and fading with distance | |
| 177 | function lightWater() { | |
| 178 | const half = BEAM.ARC / 2; | |
| 179 | for (let i = 0; i < pix.length; i++) { | |
| 180 | if (!dark[i] || zb[i] !== groundZ[i]) continue; | |
| 181 | const r = polar[i]; | |
| 182 | if (r > BEAM.BEAM_RANGE) continue; | |
| 183 | const da = Math.abs(BEAM.wrap(angle[i] - N.angle)); | |
| 184 | if (da > half) continue; | |
| 185 | const power = Math.min(1, (1 - da / half) * 2.2) * (.4 + .6 * (1 - r / BEAM.BEAM_RANGE)); | |
| 186 | const band = clamp(Math.floor(power * 4 + (BAYER[((i / W | 0) & 3) * 4 + (i % W & 3)] - .5) * .9), 0, 3); | |
| 187 | if (!band) continue; | |
| 188 | const m = BEAM_MIX[band], p = pix[i]; | |
| 189 | pix[i] = rgb(lerp(p & 255, BEAM_TINT[0], m), lerp(p >> 8 & 255, BEAM_TINT[1], m), lerp(p >> 16 & 255, BEAM_TINT[2], m)); | |
| 190 | } | |
| 191 | } | |
| 192 | // a lit boat throws its shadow straight away from the lighthouse | |
| 193 | function boatShadow(b) { | |
| 194 | const ux = Math.cos(b.a), uy = Math.sin(b.a); | |
| 195 | for (let t = .15; t < 1.9; t += .07) { | |
| 196 | const q = project(b.x + ux * t, b.y + uy * t, 0), ix = q.sx | 0, iy = q.sy | 0, wide = 1 - t / 2.1; | |
| 197 | for (let dx = -1; dx <= 1; dx++) for (let dy = 0; dy <= 1; dy++) { | |
| 198 | const x = ix + dx, y = iy + dy; | |
| 199 | if (x < 0 || y < 0 || x >= W || y >= H || Math.abs(dx) + dy > 1 + wide) continue; | |
| 200 | const j = y * W + x; | |
| 201 | if (dark[j] && zb[j] === groundZ[j]) tint(j, .5, .5, .74); | |
| 202 | } | |
| 203 | } | |
| 204 | } | |
| 205 | function drawBoat(b) { | |
| 206 | const heading = b.state === 'away' ? b.a : b.a + Math.PI, dx = Math.cos(heading), dy = Math.sin(heading); | |
| 207 | const hull = HULLS[b.id % HULLS.length], p = clamp(b.dwell / BEAM.DWELL, 0, 1); | |
| 208 | const sail = DIM_SAIL.map((v, k) => lerp(v, BRIGHT_SAIL[k], p)); | |
| 209 | const z = .12; | |
| 210 | capsule([b.x - dx * .42, b.y - dy * .42, z + .08], [b.x + dx * .42, b.y + dy * .42, z], .25, .1, () => hull, 'solid'); | |
| 211 | capsule([b.x, b.y, z + .1], [b.x, b.y, z + 1.05], .045, .045, () => BRIGHT_SAIL, 'solid'); | |
| 212 | sphere(b.x - dx * .05, b.y - dy * .05, z + .7, .3, sail, 'solid'); | |
| 213 | sphere(b.x - dx * .45, b.y - dy * .45, z + .45, .1, GLASS, 'gloss'); | |
| 214 | } | |
| 215 | function draw() { | |
| 216 | pix.set(bakedPix); | |
| 217 | zb.set(bakedZ); | |
| 218 | lightWater(); | |
| 219 | for (const b of N.boats) if (b.lit) boatShadow(b); | |
| 220 | for (const b of N.boats) drawBoat(b); | |
| 221 | const windT = STILL ? 0 : Math.floor(G.time * WIND_FPS) / WIND_FPS; | |
| 222 | drawTufts(windT); | |
| 223 | shadeEdges(); | |
| 224 | ctx.putImageData(image, 0, 0); | |
| 225 | } | |
| 226 | ||
| 227 | /* ---------- input ---------- */ | |
| 228 | const keys = { left: 0, right: 0 }; | |
| 229 | let dragging = false; | |
| 230 | function aim(e) { | |
| 231 | const box = canvas.getBoundingClientRect(); | |
| 232 | if (!box.width || !box.height) return; | |
| 233 | const g = unproject((e.clientX - box.left) / box.width * W, (e.clientY - box.top) / box.height * H); | |
| 234 | if (Math.hypot(g.x, g.y) > .5) BEAM.aimAt(N, Math.atan2(g.y, g.x)); | |
| 235 | } | |
| 236 | canvas.addEventListener('pointerdown', e => { | |
| 237 | ensureAudio(); | |
| 238 | canvas.focus({ preventScroll: true }); | |
| 239 | canvas.setPointerCapture(e.pointerId); | |
| 240 | const wasPlaying = N.phase === 'play'; | |
| 241 | start(); | |
| 242 | dragging = N.phase !== 'over'; | |
| 243 | if (wasPlaying || N.phase === 'play') aim(e); | |
| 244 | }); | |
| 245 | canvas.addEventListener('pointermove', e => { if (dragging) aim(e); }); | |
| 246 | const letGo = () => { dragging = false; }; | |
| 247 | canvas.addEventListener('pointerup', letGo); | |
| 248 | canvas.addEventListener('pointercancel', letGo); | |
| 249 | addEventListener('blur', () => { dragging = false; keys.left = keys.right = 0; }); | |
| 250 | const LEFT = ['ArrowLeft', 'a', 'A'], RIGHT = ['ArrowRight', 'd', 'D']; | |
| 251 | canvas.addEventListener('keydown', e => { | |
| 252 | const left = LEFT.includes(e.key), right = RIGHT.includes(e.key); | |
| 253 | if (left || right) { | |
| 254 | e.preventDefault(); | |
| 255 | ensureAudio(); | |
| 256 | if (N.phase === 'ready') start(); | |
| 257 | keys[left ? 'left' : 'right'] = 1; | |
| 258 | return; | |
| 259 | } | |
| 260 | if (e.key !== ' ' && e.key !== 'Enter') return; | |
| 261 | e.preventDefault(); | |
| 262 | if (e.repeat) return; | |
| 263 | ensureAudio(); | |
| 264 | start(); | |
| 265 | }); | |
| 266 | canvas.addEventListener('keyup', e => { | |
| 267 | if (LEFT.includes(e.key)) keys.left = 0; | |
| 268 | if (RIGHT.includes(e.key)) keys.right = 0; | |
| 269 | }); | |
| 270 | ||
| 271 | /* ---------- sound ---------- */ | |
| 272 | let audio = null, muted = false; | |
| 273 | function ensureAudio() { | |
| 274 | if (audio) { if (audio.state === 'suspended') audio.resume(); return; } | |
| 275 | const AC = window.AudioContext || window.webkitAudioContext; | |
| 276 | if (AC) audio = new AC(); | |
| 277 | } | |
| 278 | function sound(kind) { | |
| 279 | if (!audio || muted) return; | |
| 280 | const now = audio.currentTime, gain = audio.createGain(), osc = audio.createOscillator(); | |
| 281 | const spec = { bell: [880, 660, .35, .06, 'sine'], crash: [140, 50, .3, .16, 'sawtooth'] }[kind]; | |
| 282 | osc.type = spec[4]; | |
| 283 | osc.frequency.setValueAtTime(spec[0], now); | |
| 284 | osc.frequency.exponentialRampToValueAtTime(spec[1], now + spec[2]); | |
| 285 | gain.gain.setValueAtTime(spec[3], now); | |
| 286 | gain.gain.exponentialRampToValueAtTime(.0005, now + spec[2]); | |
| 287 | osc.connect(gain).connect(audio.destination); | |
| 288 | osc.start(now); osc.stop(now + spec[2] + .01); | |
| 289 | } | |
| 290 | const muteButton = document.getElementById('mute'); | |
| 291 | muteButton.addEventListener('click', () => { | |
| 292 | muted = !muted; | |
| 293 | muteButton.textContent = muted ? 'sound off' : 'sound on'; | |
| 294 | muteButton.setAttribute('aria-pressed', String(muted)); | |
| 295 | }); | |
| 296 | ||
| 297 | /* ---------- fit and boot ---------- */ | |
| 298 | addEventListener('resize', fit); | |
| 299 | fit(); | |
| 300 | bake(); | |
| 301 | setStatus(READY); | |
| 302 | renderHud(); | |
| 303 | canvas.focus({ preventScroll: true }); | |
| 304 | let last = performance.now(); | |
| 305 | function frame(now) { | |
| 306 | update(Math.min(.04, (now - last) / 1000)); | |
| 307 | last = now; | |
| 308 | draw(); | |
| 309 | requestAnimationFrame(frame); | |
| 310 | } | |
| 311 | requestAnimationFrame(frame); | |
| 312 | </script> | |
| 313 | </body> | |
| 314 | </html> |
serve.test.mjs +7
| @@ -120,3 +120,10 @@ test('harbour crane is served without a track', async () => { | ||
| 120 | 120 | expect(page.headers.get('content-type')).toBe('text/html; charset=utf-8'); |
| 121 | 121 | expect((await fetch(base + '/harbour-crane/crane.js')).status).toBe(200); |
| 122 | 122 | }); |
| 123 | ||
| 124 | test('lighthouse is served without a track', async () => { | |
| 125 | const page = await fetch(base + '/lighthouse/'); | |
| 126 | expect(page.status).toBe(200); | |
| 127 | expect(page.headers.get('content-type')).toBe('text/html; charset=utf-8'); | |
| 128 | expect((await fetch(base + '/lighthouse/beam.js')).status).toBe(200); | |
| 129 | }); |
shell.js +1
| @@ -17,6 +17,7 @@ window.PixelPlays = (() => { | ||
| 17 | 17 | { id: 'pottery-wheel', title: 'Pottery Wheel', blurb: 'Shape a pot on a kick wheel: hold to centre the clay without collapsing it, then pull the wall as tall as you can before it tears', music: false, forged: true }, |
| 18 | 18 | { id: 'row-home', title: 'Row Home', blurb: 'Race a rowing boat across the river against a bot: stroke in rhythm with the glide, too early catches a crab', music: false, forged: true }, |
| 19 | 19 | { id: 'orchard', title: 'Orchard', blurb: 'Catch falling apples in a basket: ripe 1, golden 3, a wasp-bitten brown one costs a life', music: false, forged: true }, |
| 20 | { id: 'lighthouse', title: 'Lighthouse', blurb: 'Sweep the lighthouse beam to turn boats away from the rocks: hold one in the light for a second, three wrecks end the night', music: false, forged: true }, | |
| 20 | 21 | ]; |
| 21 | 22 | const MUSIC_VOLUME = .4; |
| 22 | 23 | const CREDITS = [ |
new above
offline