Public: anyone can read this; the crew can change it.
Migrated from github.com/amirmushichge/pixel-plays
Add Harbour Crane: stack crates onto a bobbing boat #18
Check · Review · Land · Live done — Landed and live in production.
quill/pixel-plays:quill/10-add-harbour-crane-stack-crates-onto-a-bo → main 3 commits fork 5 threads · 5 unresolved
README.md +2 −1
| @@ -17,6 +17,7 @@ Concept by [abhi](https://x.com/awwbhi2). Game by [AmirMushich](https://x.com/Am | ||
| 17 | 17 | | [](mini-ramp/) | 60 seconds on a skateboard ramp, pump for height and grab in the air | Hold and release; space | |
| 18 | 18 | | [](kite-hill/) | Keep a kite in the air through 60 seconds of gusts: pull to climb, let out line when the grass bends (forged by the crew on the engine and shell above, not by the original creators) | Hold; space | |
| 19 | 19 | | [](stone-skip/) | Skim a flat stone across a lake, five throws, count the bounces (forged by the crew on the engine and shell above, not by the original creators) | Drag back and release; arrows + space | |
| 20 | | [](harbour-crane/) | Stack ten crates onto a bobbing fishing boat from a swinging crane hook; one hanging more than half off tips into the water (forged by the crew on the engine and shell above, not by the original creators) | Tap; space | | |
| 20 | 21 | |
| 21 | 22 | ## Run |
| 22 | 23 | Requires [](https://nodejs.org/); nothing to install. |
| @@ -30,7 +31,7 @@ Then open http://127.0.0.1:5300/. `PORT` and `HOST` override the port and addres | ||
| 30 | 31 | 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. |
| 31 | 32 | |
| 32 | 33 | ## Add a game |
| 33 | 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 and Paper Route 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`. | |
| 34 | 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 and Harbour Crane 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`. | |
| 34 | 35 | |
| 35 | 36 | - `shell.js` provides the game menu, the Rules popup, the credits and the background music. |
| 36 | 37 | - `engine.js` provides the camera, depth buffer, colour ramps, shadows, edges, figures, trees and wind as globals. |
harbour-crane/crane.js added +50
| @@ -0,0 +1,50 @@ | ||
| 1 | 'use strict'; | |
| 2 | // The rules of the harbour crane: plain numbers in, plain numbers out, no DOM. The page draws what this decides. | |
| 3 | (function (root) { | |
| 4 | const CRATES = 10; // crates per round | |
| 5 | const CRATE_W = 1.2, CRATE_H = .9; | |
| 6 | const DECK_HALF = 2; // the deck runs from -2 to 2 along the swing | |
| 7 | const AMP = 2.6; // how far the hook swings either side of the boat's middle | |
| 8 | const lerp = (a, b, t) => a + (b - a) * t; | |
| 9 | const num = v => (Number.isFinite(v) ? v : 0); | |
| 10 | ||
| 11 | // seconds for one full swing: each crate on deck makes the next one quicker | |
| 12 | const period = loaded => lerp(2.8, 1.7, Math.min(1, Math.max(0, num(loaded)) / (CRATES - 1))); | |
| 13 | // where the hook is t seconds after the crate was hooked; it starts at the far left and swings in | |
| 14 | function swingX(t, loaded) { | |
| 15 | return -AMP * Math.cos(2 * Math.PI * Math.max(0, num(t)) / period(loaded)); | |
| 16 | } | |
| 17 | ||
| 18 | function newRound() { return { crates: [], dropped: 0 }; } | |
| 19 | const over = round => round.dropped >= CRATES; | |
| 20 | ||
| 21 | // drop a crate at x onto the stack: it stays when its middle is over what holds it, otherwise it tips into the water | |
| 22 | function drop(round, x) { | |
| 23 | if (over(round)) return { result: 'none' }; | |
| 24 | const at = Math.min(AMP, Math.max(-AMP, num(x))); | |
| 25 | const top = round.crates[round.crates.length - 1]; | |
| 26 | const centre = top ? top.x : 0, half = top ? CRATE_W / 2 : DECK_HALF; | |
| 27 | round.dropped++; | |
| 28 | // it tips when its middle is past the edge of what holds it | |
| 29 | if (Math.abs(at - centre) > half) return { result: 'tip', x: at, side: at < centre ? -1 : 1, z: round.crates.length * CRATE_H }; | |
| 30 | round.crates.push({ x: at }); | |
| 31 | return { result: 'stay', x: at, z: (round.crates.length - 1) * CRATE_H }; | |
| 32 | } | |
| 33 | ||
| 34 | // how the boat sits under its load: slope is height gained per unit toward +x (negative leans the stern down), sink is height lost | |
| 35 | function trim(round) { | |
| 36 | const moment = round.crates.reduce((m, c, i) => m + c.x * (i + 1), 0); | |
| 37 | const slope = Math.max(-.22, Math.min(.22, moment * .012)); | |
| 38 | return { slope, sink: round.crates.length * .05 }; | |
| 39 | } | |
| 40 | ||
| 41 | // where crate i rests: the first on the deck at its own x, every later one on the top of the crate below, | |
| 42 | // so a leaning stack still stacks. `deckAt(x)` is the height of the (possibly heeled) deck at x; `x` is crate i's x when it is not yet in `crates` | |
| 43 | function stackBase(crates, i, x, deckAt) { | |
| 44 | const first = i === 0 ? x : crates[0].x; | |
| 45 | return deckAt(first) + i * CRATE_H; | |
| 46 | } | |
| 47 | ||
| 48 | const api = { CRATES, CRATE_W, CRATE_H, DECK_HALF, AMP, period, swingX, newRound, drop, trim, over, stackBase }; | |
| 49 | if (typeof module !== 'undefined') module.exports = api; else root.HarbourCrane = api; | |
| 50 | })(typeof window !== 'undefined' ? window : globalThis); |
harbour-crane/crane.test.mjs added +95
| @@ -0,0 +1,95 @@ | ||
| 1 | import { test, expect } from 'bun:test'; | |
| 2 | import { CRATES, CRATE_W, DECK_HALF, AMP, period, swingX, newRound, drop, trim, over, stackBase, CRATE_H } from './crane.js'; | |
| 3 | ||
| 4 | test('a crate dropped over the middle of the deck stays', () => { | |
| 5 | const r = newRound(); | |
| 6 | expect(drop(r, 0).result).toBe('stay'); | |
| 7 | expect(r.crates.length).toBe(1); | |
| 8 | }); | |
| 9 | ||
| 10 | test('the hook swings within its reach and starts at the left', () => { | |
| 11 | expect(swingX(0, 0)).toBeCloseTo(-AMP); | |
| 12 | for (let t = 0; t < 6; t += .05) expect(Math.abs(swingX(t, 3))).toBeLessThanOrEqual(AMP + 1e-9); | |
| 13 | expect(swingX(period(0) / 2, 0)).toBeCloseTo(AMP); | |
| 14 | }); | |
| 15 | ||
| 16 | test('each crate on deck speeds the swing up', () => { | |
| 17 | expect(period(5)).toBeLessThan(period(0)); | |
| 18 | expect(period(CRATES - 1)).toBeLessThan(period(5)); | |
| 19 | }); | |
| 20 | ||
| 21 | test('over the water beyond the deck edge a crate tips', () => { | |
| 22 | const r = newRound(); | |
| 23 | const out = drop(r, DECK_HALF + .3); | |
| 24 | expect(out.result).toBe('tip'); | |
| 25 | expect(out.side).toBe(1); | |
| 26 | expect(r.crates.length).toBe(0); | |
| 27 | expect(r.dropped).toBe(1); | |
| 28 | }); | |
| 29 | ||
| 30 | test('a crate overhanging the one below by more than half tips, by less stays', () => { | |
| 31 | const r = newRound(); | |
| 32 | drop(r, 0); | |
| 33 | expect(drop(r, CRATE_W / 2 + .05).result).toBe('tip'); | |
| 34 | expect(drop(r, -CRATE_W / 2 - .05).side).toBe(-1); | |
| 35 | expect(drop(r, CRATE_W / 2 - .05).result).toBe('stay'); | |
| 36 | expect(r.crates.length).toBe(2); | |
| 37 | }); | |
| 38 | ||
| 39 | test('a landed crate rests on the stack', () => { | |
| 40 | const r = newRound(); | |
| 41 | expect(drop(r, 0).z).toBe(0); | |
| 42 | expect(drop(r, .1).z).toBeCloseTo(.9); | |
| 43 | }); | |
| 44 | ||
| 45 | test('a round is ten crates and then nothing more drops', () => { | |
| 46 | const r = newRound(); | |
| 47 | for (let k = 0; k < CRATES; k++) drop(r, 0); | |
| 48 | expect(over(r)).toBe(true); | |
| 49 | expect(r.crates.length).toBe(CRATES); | |
| 50 | expect(drop(r, 0).result).toBe('none'); | |
| 51 | expect(r.dropped).toBe(CRATES); | |
| 52 | }); | |
| 53 | ||
| 54 | test('the boat sinks and leans as the stack grows', () => { | |
| 55 | const r = newRound(); | |
| 56 | expect(trim(r)).toEqual({ slope: 0, sink: 0 }); | |
| 57 | drop(r, 1); drop(r, 1.4); | |
| 58 | const a = trim(r); | |
| 59 | drop(r, 1.2); drop(r, 1.4); | |
| 60 | const b = trim(r); | |
| 61 | expect(b.sink).toBeGreaterThan(a.sink); | |
| 62 | expect(b.slope).toBeGreaterThan(a.slope); | |
| 63 | expect(a.slope).toBeGreaterThan(0); | |
| 64 | }); | |
| 65 | ||
| 66 | test('a load on the other side leans the other way', () => { | |
| 67 | const r = newRound(); | |
| 68 | drop(r, -1.5); | |
| 69 | expect(trim(r).slope).toBeLessThan(0); | |
| 70 | }); | |
| 71 | ||
| 72 | test('inputs are clamped at the boundary', () => { | |
| 73 | const r = newRound(); | |
| 74 | expect(drop(r, NaN).result).toBe('stay'); | |
| 75 | expect(drop(r, 99).result).toBe('tip'); | |
| 76 | expect(swingX(NaN, NaN)).toBeCloseTo(-AMP); | |
| 77 | expect(Number.isFinite(trim(r).slope)).toBe(true); | |
| 78 | }); | |
| 79 | ||
| 80 | test('a crate tips only when its middle is past the edge: the deck edge itself holds', () => { | |
| 81 | expect(drop(newRound(), DECK_HALF).result).toBe('stay'); | |
| 82 | expect(drop(newRound(), DECK_HALF + .01).result).toBe('tip'); | |
| 83 | expect(drop(newRound(), -DECK_HALF).result).toBe('stay'); | |
| 84 | expect(drop(newRound(), -DECK_HALF - .01).result).toBe('tip'); | |
| 85 | }); | |
| 86 | ||
| 87 | test('on a leaning deck each crate rests exactly on the top of the one below', () => { | |
| 88 | const r = newRound(); | |
| 89 | for (const x of [1.2, 1.5, 1.1, 1.4, 1.2, 1.5]) drop(r, x); | |
| 90 | const deckAt = x => .55 + x * .22; | |
| 91 | for (let i = 1; i < r.crates.length; i++) { | |
| 92 | expect(stackBase(r.crates, i, r.crates[i].x, deckAt)).toBeCloseTo(stackBase(r.crates, i - 1, r.crates[i - 1].x, deckAt) + CRATE_H, 9); | |
| 93 | } | |
| 94 | expect(stackBase(r.crates, 0, r.crates[0].x, deckAt)).toBeCloseTo(deckAt(1.2), 9); | |
| 95 | }); |
harbour-crane/index.html added +358
| @@ -0,0 +1,358 @@ | ||
| 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>Harbour Crane</title> | |
| 7 | <style> | |
| 8 | :root { : #1c1b19; : #8b877f; : #fff; : #b0642e; } | |
| 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: pointer; 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="Harbour quay with a crane swinging a crate over a fishing boat. Tap or press space to drop the crate onto the boat."></canvas></div> | |
| 30 | <div id="scores"> | |
| 31 | <div class="row"><b id="score">0</b><span>on deck</span></div> | |
| 32 | <div class="row"><b id="best">0</b><span>best</span><span class="dots" id="left"></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: 'harbour-crane', | |
| 42 | rules: [ | |
| 43 | 'Stack ten crates on the boat. Score is the crates left on deck; your best counts.', | |
| 44 | 'Tap, click or press space to drop the crate swinging on the hook. It lands where it was.', | |
| 45 | 'A crate hanging more than half off what holds it tips into the water. The stack leans the boat.', | |
| 46 | ], | |
| 47 | }); | |
| 48 | </script> | |
| 49 | <script>window.PP_SCENE = { originY: 22, shadowX: 1.3, shadowY: -.12 };</script> | |
| 50 | <script src="../engine.js"></script> | |
| 51 | <script src="crane.js"></script> | |
| 52 | <script> | |
| 53 | 'use strict'; | |
| 54 | ||
| 55 | const HC = window.HarbourCrane; | |
| 56 | const KIND_WATER = 4, KIND_QUAY = 5; | |
| 57 | const WATER_TONES = [[58, 118, 154], [64, 127, 161], [72, 137, 170], [84, 150, 181]]; | |
| 58 | const QUAY_TONES = [[142, 140, 146], [158, 156, 160], [174, 171, 172], [190, 186, 182]]; | |
| 59 | const FOAM = rgb(232, 244, 248); | |
| 60 | const PXZ = 1 / (S * COS); // one screen pixel of height, so the boat only ever moves in whole pixels | |
| 61 | const CRANE = [150, 98, 70], STEEL = [96, 100, 112], HULL = [176, 70, 58], DECK = [196, 160, 108], CABIN = [232, 226, 212]; | |
| 62 | const WOOD = [[188, 134, 72], [170, 118, 62], [200, 150, 88], [178, 126, 66]]; | |
| 63 | const BOAT_LEN = 2.9, DECK_Z = .55, HOOK = [0, 0, 6.3], QUAY_X = -6, QUAY_Y = -3.6; | |
| 64 | const FALL_G = 16, RIPPLE_LIFE = 1.2; | |
| 65 | ||
| 66 | const onQuay = (x, y) => x < QUAY_X || y < QUAY_Y; | |
| 67 | function groundColor(x, y, ix, iy, i) { | |
| 68 | const edge = Math.pow(Math.pow(Math.abs(x + 2.2) / 12.4, 4) + Math.pow(Math.abs(y + .4) / 7.4, 4), .25); | |
| 69 | const rough = edge + (noise(x * 1.3, y * 1.3) - .5) * .13; | |
| 70 | if (rough > 1.04) return 0; | |
| 71 | if (rough > .93 && hash(ix, iy) < (rough - .93) / .11) return 0; | |
| 72 | const fine = hash(ix, iy); | |
| 73 | if (!onQuay(x, y)) { | |
| 74 | kinds[i] = KIND_WATER; | |
| 75 | const depth = noise(x * .35, y * .5) * .8 + noise(x * 1.7, y * 2.4) * .25 + (fine - .5) * .12; | |
| 76 | return rgb(...WATER_TONES[clamp(Math.floor(depth * 4 + (bayerAt(ix, iy) - .5) * .5), 0, 3)]); | |
| 77 | } | |
| 78 | if (x < QUAY_X - 3 || y < QUAY_Y - 1.6) { | |
| 79 | kinds[i] = KIND_GRASS; | |
| 80 | const t = noise(x * .9, y * .9) * 1.15 + (fine - .5) * .55 - .08; | |
| 81 | return rgb(...GRASS[clamp(Math.floor(t * 4.2), 0, 3)]); | |
| 82 | } | |
| 83 | kinds[i] = KIND_QUAY; | |
| 84 | // flagstones: a dark seam every couple of units, otherwise dithered grey | |
| 85 | const seam = Math.abs(((x + 40) % 1.6) - .8) > .76 || Math.abs(((y + 40) % 1.6) - .8) > .76; | |
| 86 | if (seam) return rgb(...QUAY_TONES[0]); | |
| 87 | return rgb(...QUAY_TONES[clamp(Math.floor(noise(x * .8, y * .8) * 3 + 1 + (bayerAt(ix, iy) - .5) * .6), 1, 3)]); | |
| 88 | } | |
| 89 | ||
| 90 | function bakeCrane() { | |
outdated
The comment says this adds a hard shadow, but |
||
| 91 | const bx = -7.6; | |
| 92 | box(bx - .6, -.7, 0, bx + .6, .7, .5, STEEL, 1); // footing | |
| 93 | box(bx - .25, -.25, .5, bx + .25, .25, 6.1, CRANE, 2); // mast | |
| 94 | box(bx - .9, -.2, 6.1, 0.15, .2, 6.5, CRANE, 3); // jib, out over the boat | |
| 95 | box(bx - 1.5, -.35, 5.9, bx - .9, .35, 6.5, STEEL, 4); // counterweight | |
| 96 | box(bx - .15, -.15, 6.5, bx + .15, .15, 7.1, STEEL, 5); // cab-top post | |
| 97 | for (let z = .5; z <= 6.5; z += .3) castShadow(bx, 0, z, .25); | |
| 98 | for (let x = bx - 1; x <= 0; x += .3) castShadow(x, 0, 6.3, .25); | |
| 99 | castShadow(bx - 1.2, 0, 6.2, .5); | |
| 100 | } | |
| 101 | function bake() { | |
| 102 | zb.fill(-1e9); | |
| 103 | pix.fill(rgb(255, 255, 255)); | |
| 104 | for (let iy = 0; iy < H; iy++) for (let ix = 0; ix < W; ix++) { | |
| 105 | const g = unproject(ix + .5, iy + .5), i = iy * W + ix, c = groundColor(g.x, g.y, ix, iy, i); | |
| 106 | if (!c) continue; | |
| 107 | pix[i] = c; ground[i] = 1; | |
| 108 | zb[i] = (g.x + g.y) / R2 * COS; | |
| 109 | } | |
| 110 | growGrass(); | |
| 111 | groundZ.set(zb); | |
| 112 | ||
| 113 | growTree(-10.2, -6.6, .9, 380, 5); | |
| 114 | bakeCrane(); | |
| 115 | // bollards along the quay edge | |
| 116 | for (const [x, y] of [[-5.5, 2.4], [-5.5, -2.4], [-1.8, -3.1], [2.6, -3.1]]) { | |
| 117 | sphere(x, y, .22, .24, STEEL, 'rough'); | |
| 118 | castShadow(x, y, .35, .26); | |
| 119 | } | |
| 120 | bakeOcclusion(); | |
| 121 | staticShadow.set(shadowMask); | |
| 122 | applyShadows(groundZ); | |
| 123 | bakedPix.set(pix); | |
| 124 | bakedZ.set(zb); | |
| 125 | } | |
| 126 | ||
| 127 | /* ---------- game state ---------- */ | |
| 128 | const G = { | |
| 129 | phase: 'swing', round: HC.newRound(), clock: 0, time: 0, timer: 0, | |
| 130 | fall: null, raised: false, tips: [], ripples: [], best: 0, status: '', | |
| 131 | }; | |
| 132 | const nodes = { | |
| 133 | score: document.getElementById('score'), best: document.getElementById('best'), | |
outdated
|
||
| 134 | left: document.getElementById('left'), status: document.getElementById('status'), | |
| 135 | }; | |
| 136 | function renderHud() { | |
| 137 | nodes.score.textContent = G.round.crates.length; | |
| 138 | nodes.best.textContent = G.best; | |
| 139 | if (nodes.left.children.length !== HC.CRATES) nodes.left.innerHTML = '<i></i>'.repeat(HC.CRATES); | |
| 140 | [...nodes.left.children].forEach((el, k) => el.classList.toggle('off', k < G.round.dropped)); | |
| 141 | } | |
| 142 | function setStatus(text) { | |
| 143 | if (text === G.status) return; | |
| 144 | G.status = text; | |
| 145 | nodes.status.textContent = text; | |
| 146 | } | |
| 147 | function hookNext() { | |
| 148 | G.phase = 'swing'; G.clock = 0; G.fall = null; | |
| 149 | setStatus('tap or space to drop'); | |
| 150 | renderHud(); | |
| 151 | } | |
| 152 | function resetGame() { | |
| 153 | G.round = HC.newRound(); G.raised = false; G.tips = []; G.ripples = []; | |
| 154 | hookNext(); | |
| 155 | } | |
| 156 | const hookX = () => HC.swingX(G.clock, G.round.crates.length); | |
| 157 | function release() { | |
| 158 | if (G.phase !== 'swing') return; | |
| 159 | const x = hookX(), out = HC.drop(G.round, x); | |
| 160 | if (out.result === 'none') return; | |
| 161 | G.fall = { x: out.x, z: HOOK[2] - 1.3, vz: 0, out }; | |
| 162 | G.phase = 'fall'; | |
| 163 | sound('release'); | |
| 164 | setStatus(''); | |
| 165 | } | |
| 166 | function land() { | |
| 167 | const f = G.fall, out = f.out; | |
| 168 | G.timer = 0; | |
| 169 | if (out.result === 'stay') { | |
| 170 | sound('thud'); | |
| 171 | } else { | |
| 172 | G.tips.push({ x: f.x, y: 0, z: f.z, vx: out.side * 1.1, vy: 1.7, vz: 0, splashed: false, seed: G.round.dropped }); | |
| 173 | sound('tip'); | |
| 174 | } | |
| 175 | G.fall = null; | |
| 176 | const left = HC.CRATES - G.round.dropped, on = G.round.crates.length; | |
| 177 | if (out.result === 'stay' && on > G.best) { G.best = on; G.raised = true; } | |
| 178 | if (left === 0) { | |
| 179 | G.phase = 'done'; | |
| 180 | if (G.raised) sound('point'); | |
| 181 | setStatus(`${on} of ${HC.CRATES} crates on deck · best ${G.best} · tap to play again`); | |
| 182 | } else { | |
| 183 | G.phase = 'settle'; | |
| 184 | setStatus(out.result === 'stay' ? `${left} to go` : `it tips into the water · ${left} to go`); | |
outdated
This also fires when the round only ties the earlier best. Play |
||
| 185 | } | |
| 186 | renderHud(); | |
| 187 | } | |
| 188 | function ripple(x, y) { G.ripples.push({ x, y, age: 0 }); } | |
| 189 | ||
| 190 | // the boat rides the swell in whole pixels | |
| 191 | function bobPixels() { return Math.round(Math.sin(G.time * 1.7) * 1.4); } | |
| 192 | function boatFrame() { | |
| 193 | const t = HC.trim(G.round); | |
| 194 | return { lift: bobPixels() * PXZ - t.sink, slope: t.slope }; | |
| 195 | } | |
| 196 | // the height of a point at x on the boat, snapped to a screen pixel | |
| 197 | const heel = (f, x) => Math.round(x * f.slope / PXZ) * PXZ + f.lift; | |
| 198 | const topOf = f => x => DECK_Z + heel(f, x); | |
| 199 | const stackBase = (f, crates, i, x) => HC.stackBase(crates, i, x, topOf(f)); | |
| 200 | ||
| 201 | function update(dt) { | |
| 202 | G.time += dt; | |
| 203 | for (const r of G.ripples) r.age += dt; | |
outdated
Each crate gets the heel at its own x, so on a leaning boat crate i floats above (or sinks into) crate i-1 by slope*(Δx). Stack each crate on the top of the one below, and move this into crane.js with a test. |
||
| 204 | G.ripples = G.ripples.filter(r => r.age < RIPPLE_LIFE); | |
| 205 | for (const c of G.tips) { | |
| 206 | c.vz -= FALL_G * dt; c.x += c.vx * dt; c.y += c.vy * dt; c.z += c.vz * dt; | |
| 207 | if (c.z < 0 && !c.splashed) { c.splashed = true; ripple(c.x, c.y); sound('splash'); } | |
| 208 | if (c.splashed) c.vz = Math.max(c.vz, -2.2); | |
| 209 | } | |
| 210 | G.tips = G.tips.filter(c => c.z > -1.6); | |
| 211 | if (G.phase === 'swing') { | |
| 212 | G.clock += dt; | |
| 213 | } else if (G.phase === 'fall') { | |
| 214 | const f = G.fall, fr = boatFrame(), n = G.round.crates.length; | |
| 215 | f.vz -= FALL_G * dt; f.z += f.vz * dt; | |
| 216 | // a crate that stays lands on the stack; one that tips lands on the edge of the deck or the stack and goes over | |
| 217 | const stayed = f.out.result === 'stay'; | |
| 218 | const rest = stackBase(fr, G.round.crates, stayed ? n - 1 : n, f.x); | |
| 219 | if (f.z <= rest) { f.z = rest; land(); } | |
| 220 | } else if (G.phase === 'settle') { | |
| 221 | G.timer += dt; | |
| 222 | if (G.timer > .6) hookNext(); | |
| 223 | } else if (G.phase === 'done') { | |
| 224 | G.timer += dt; | |
| 225 | } | |
| 226 | } | |
| 227 | ||
| 228 | /* ---------- drawing ---------- */ | |
| 229 | const hullHalf = x => { | |
| 230 | const a = Math.abs(x); | |
| 231 | return a < 2.3 ? .95 : Math.max(.12, .95 * Math.sqrt(Math.max(0, 1 - ((a - 2.3) / (BOAT_LEN - 2.3 + .15)) ** 2))); | |
| 232 | }; | |
| 233 | function drawBoat(f) { | |
| 234 | const step = .25; | |
| 235 | for (let x = -BOAT_LEN; x < BOAT_LEN; x += step) { | |
| 236 | const xm = x + step / 2, hw = hullHalf(xm), top = topOf(f)(xm); | |
| 237 | box(x, -hw, top - 1.6, x + step, hw, top - .12, HULL, (x * 7) | 0); | |
| 238 | box(x, -hw + .12, top - .13, x + step, hw - .12, top, DECK, (x * 11) | 0); // planked deck | |
| 239 | } | |
| 240 | const cx = -2.7, top = topOf(f)(cx); | |
| 241 | box(cx - .3, -.55, top, cx + .45, .55, top + 1, CABIN, 3); | |
| 242 | box(cx - .34, -.6, top + 1, cx + .5, .6, top + 1.12, HULL, 4); | |
| 243 | // the boat's shadow on the water, hard and to the screen right | |
| 244 | for (let x = -BOAT_LEN + .3; x <= BOAT_LEN - .3; x += .45) castShadow(x, 0, 1.1 + f.lift, .7); | |
| 245 | } | |
| 246 | function drawCrate(x, y, z, seed) { | |
| 247 | box(x - HC.CRATE_W / 2, y - HC.CRATE_W / 2, z - .15, x + HC.CRATE_W / 2, y + HC.CRATE_W / 2, z + HC.CRATE_H, WOOD[seed & 3], seed); | |
| 248 | castShadow(x, y, z + HC.CRATE_H / 2, .62, 1, 0); | |
| 249 | } | |
| 250 | function drawRope(x, z) { | |
| 251 | const a = project(HOOK[0], HOOK[1], HOOK[2]), b = project(x, 0, z + HC.CRATE_H + .15); | |
| 252 | const n = Math.ceil(Math.hypot(b.sx - a.sx, b.sy - a.sy)); | |
| 253 | const dark = rgb(40, 38, 36); | |
| 254 | for (let k = 0; k <= n; k++) { | |
| 255 | const t = k / n, ix = Math.round(lerp(a.sx, b.sx, t)), iy = Math.round(lerp(a.sy, b.sy, t)); | |
| 256 | if (ix >= 0 && iy >= 0 && ix < W && iy < H) { pix[iy * W + ix] = dark; zb[iy * W + ix] = 1e6; } | |
| 257 | } | |
| 258 | sphere(x, 0, z + HC.CRATE_H + .1, .1, STEEL, 'solid'); | |
| 259 | } | |
| 260 | function drawRipples() { | |
| 261 | for (const r of G.ripples) { | |
| 262 | const t = r.age / RIPPLE_LIFE, radius = .3 + ease(t) * 1.3, fade = 1 - t; | |
| 263 | const n = Math.ceil(radius * S * 5); | |
| 264 | for (let k = 0; k < n; k++) { | |
| 265 | const a = k / n * Math.PI * 2, p = project(r.x + Math.cos(a) * radius, r.y + Math.sin(a) * radius, 0); | |
| 266 | const ix = p.sx | 0, iy = p.sy | 0; | |
| 267 | if (ix < 0 || iy < 0 || ix >= W || iy >= H) continue; | |
| 268 | const i = iy * W + ix; | |
| 269 | if (kinds[i] === KIND_WATER && bayerAt(ix, iy) < fade) pix[i] = FOAM; | |
| 270 | } | |
| 271 | } | |
| 272 | } | |
| 273 | function draw() { | |
| 274 | pix.set(bakedPix); | |
| 275 | zb.set(bakedZ); | |
| 276 | pendingShadows.length = 0; | |
| 277 | const fr = boatFrame(); | |
| 278 | drawBoat(fr); | |
| 279 | G.round.crates.forEach((c, i) => drawCrate(c.x, 0, stackBase(fr, G.round.crates, i, c.x), i * 3 + 1)); | |
| 280 | if (G.phase === 'swing') { | |
| 281 | const x = hookX(); | |
| 282 | drawCrate(x, 0, HOOK[2] - 1.3, G.round.dropped + 5); | |
| 283 | drawRope(x, HOOK[2] - 1.3); | |
| 284 | } else if (G.phase === 'fall') { | |
| 285 | drawCrate(G.fall.x, 0, G.fall.z, G.round.dropped + 5); | |
| 286 | drawRope(G.fall.x, HOOK[2] - 1.3); | |
| 287 | } | |
| 288 | for (const c of G.tips) drawCrate(c.x, c.y, c.z, c.seed + 5); | |
| 289 | const windT = STILL ? 0 : Math.floor(G.time * WIND_FPS) / WIND_FPS; | |
| 290 | drawLeaves(windT); | |
| 291 | drawTufts(windT); | |
| 292 | applyShadows(bakedZ); | |
| 293 | shadeEdges(); | |
| 294 | drawRipples(); | |
| 295 | ctx.putImageData(image, 0, 0); | |
| 296 | } | |
| 297 | ||
| 298 | /* ---------- input ---------- */ | |
| 299 | function act() { | |
| 300 | ensureAudio(); | |
| 301 | if (G.phase === 'done' && G.timer > .5) return resetGame(); | |
| 302 | release(); | |
| 303 | } | |
| 304 | canvas.addEventListener('pointerdown', e => { e.preventDefault(); canvas.focus({ preventScroll: true }); act(); }); | |
| 305 | canvas.addEventListener('keydown', e => { | |
| 306 | if (e.key !== ' ' && e.key !== 'Enter') return; | |
| 307 | e.preventDefault(); | |
| 308 | if (!e.repeat) act(); | |
| 309 | }); | |
| 310 | ||
| 311 | /* ---------- sound ---------- */ | |
| 312 | let audio = null, muted = false; | |
| 313 | function ensureAudio() { | |
| 314 | if (audio) { if (audio.state === 'suspended') audio.resume(); return; } | |
| 315 | const AC = window.AudioContext || window.webkitAudioContext; | |
| 316 | if (AC) audio = new AC(); | |
| 317 | } | |
| 318 | function sound(kind) { | |
| 319 | if (!audio || muted) return; | |
| 320 | const now = audio.currentTime, gain = audio.createGain(), osc = audio.createOscillator(); | |
| 321 | const spec = { | |
| 322 | release: [380, 200, .08, .04, 'triangle'], | |
| 323 | thud: [140, 60, .12, .24, 'sine'], | |
| 324 | tip: [220, 110, .14, .14, 'triangle'], | |
| 325 | splash: [300, 80, .24, .12, 'sine'], | |
| 326 | point: [660, 880, .16, .08, 'triangle'], | |
| 327 | }[kind]; | |
| 328 | osc.type = spec[4]; | |
| 329 | osc.frequency.setValueAtTime(spec[0], now); | |
| 330 | osc.frequency.exponentialRampToValueAtTime(spec[1], now + spec[2]); | |
| 331 | gain.gain.setValueAtTime(spec[3], now); | |
| 332 | gain.gain.exponentialRampToValueAtTime(.0005, now + spec[2]); | |
| 333 | osc.connect(gain).connect(audio.destination); | |
| 334 | osc.start(now); osc.stop(now + spec[2] + .01); | |
| 335 | } | |
| 336 | const muteButton = document.getElementById('mute'); | |
| 337 | muteButton.addEventListener('click', () => { | |
| 338 | muted = !muted; | |
| 339 | muteButton.textContent = muted ? 'sound off' : 'sound on'; | |
| 340 | muteButton.setAttribute('aria-pressed', String(muted)); | |
| 341 | }); | |
| 342 | ||
| 343 | /* ---------- fit and boot ---------- */ | |
| 344 | addEventListener('resize', fit); | |
| 345 | fit(); | |
| 346 | bake(); | |
| 347 | hookNext(); | |
| 348 | let last = performance.now(); | |
| 349 | function frame(now) { | |
| 350 | update(Math.min(.04, (now - last) / 1000)); | |
| 351 | last = now; | |
| 352 | draw(); | |
| 353 | requestAnimationFrame(frame); | |
| 354 | } | |
| 355 | requestAnimationFrame(frame); | |
| 356 | </script> | |
| 357 | </body> | |
| 358 | </html> |
index.html +1
| @@ -32,6 +32,7 @@ footer a { display: inline; padding: 0; font-weight: inherit; text-decoration: u | ||
| 32 | 32 | <li><a href="kite-hill/">Kite Hill</a><span>Keep a kite in the air through 60 seconds of gusts: pull to climb, let out line when the grass bends</span><span>Game forged by the crew</span></li> |
| 33 | 33 | <li><a href="stone-skip/">Stone Skip</a><span>Skim a flat stone across a lake, five throws, count the bounces</span><span>Game forged by the crew</span></li> |
| 34 | 34 | <li><a href="paper-route/">Paper Route</a><span>Ten newspapers thrown from a passing bike: porch 3, lawn 1, a window costs 2</span><span>Game forged by the crew</span></li> |
| 35 | <li><a href="harbour-crane/">Harbour Crane</a><span>Stack ten crates onto a bobbing fishing boat from a swinging crane hook</span><span>Game forged by the crew</span></li> | |
| 35 | 36 | </ul> |
| 36 | 37 | <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> |
| 37 | 38 | </main> |
serve.test.mjs +7
| @@ -78,3 +78,10 @@ test('missing files 404, malformed paths 400, and traversal stays inside', async | ||
| 78 | 78 | const res = await fetch(base + '/%2e%2e/%2e%2e/etc/passwd'); |
| 79 | 79 | expect([403, 404]).toContain(res.status); |
| 80 | 80 | }); |
| 81 | ||
| 82 | test('harbour crane is served without a track', async () => { | |
| 83 | const page = await fetch(base + '/harbour-crane/'); | |
| 84 | expect(page.status).toBe(200); | |
| 85 | expect(page.headers.get('content-type')).toBe('text/html; charset=utf-8'); | |
| 86 | expect((await fetch(base + '/harbour-crane/crane.js')).status).toBe(200); | |
| 87 | }); |
shell.js +1
| @@ -11,6 +11,7 @@ window.PixelPlays = (() => { | ||
| 11 | 11 | { id: 'kite-hill', title: 'Kite Hill', blurb: 'Keep a kite in the air through 60 seconds of gusts: pull to climb, let out line when the grass bends', music: false, forged: true }, |
| 12 | 12 | { id: 'stone-skip', title: 'Stone Skip', blurb: 'Skim a flat stone across a lake, five throws, count the bounces', music: false, forged: true }, |
| 13 | 13 | { id: 'paper-route', title: 'Paper Route', blurb: 'Ten newspapers thrown from a passing bike: porch 3, lawn 1, a window costs 2', music: false, forged: true }, |
| 14 | { id: 'harbour-crane', title: 'Harbour Crane', blurb: 'Stack ten crates onto a bobbing fishing boat from a swinging crane hook', music: false, forged: true }, | |
| 14 | 15 | ]; |
| 15 | 16 | const MUSIC_VOLUME = .4; |
| 16 | 17 | const CREDITS = [ |
This works out to
Math.abs(at - centre) > half. Say it that way and test the exact deck edge.