Public: anyone can read this; the crew can change it.
Migrated from github.com/amirmushichge/pixel-plays
Add Orchard: catch falling apples in a basket before the brown ones #28
Check · Review · Land · Live done — Landed and live in production.
quill/pixel-plays:quill/17-add-orchard-catch-falling-apples-in-a-ba → main 3 commits fork 5 threads · 5 unresolved
README.md +2 −1
| @@ -22,6 +22,7 @@ Concept by [abhi](https://x.com/awwbhi2). Game by [AmirMushich](https://x.com/Am | ||
| 22 | 22 | | [](sled-run/) | Ride a sledge down a snowy hill for 40 seconds: tuck for speed, release on a kicker to jump, land flat for points and nose-down to tumble (forged by the crew on the engine and shell above, not by the original creators) | Hold and release; space | |
| 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 | | [](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 | | |
| 25 | 26 | |
| 26 | 27 | ## Run |
| 27 | 28 | Requires [](https://nodejs.org/); nothing to install. |
| @@ -35,7 +36,7 @@ Then open http://127.0.0.1:5300/. `PORT` and `HOST` override the port and addres | ||
| 35 | 36 | 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. |
| 36 | 37 | |
| 37 | 38 | ## Add a game |
| 38 | 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 and Row Home 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`. | |
| 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`. | |
| 39 | 40 | |
| 40 | 41 | - `shell.js` provides the game menu, the Rules popup, the credits and the background music. |
| 41 | 42 | - `engine.js` provides the camera, depth buffer, colour ramps, shadows, edges, figures, trees and wind as globals. |
index.html +1
| @@ -37,6 +37,7 @@ footer a { display: inline; padding: 0; font-weight: inherit; text-decoration: u | ||
| 37 | 37 | <li><a href="sled-run/">Sled Run</a><span>Ride a sledge down a snowy hill: tuck for speed, release on a kicker to jump, land flat</span><span>Game forged by the crew</span></li> |
| 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 | <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> | |
| 40 | 41 | </ul> |
| 41 | 42 | <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> |
| 42 | 43 | </main> |
orchard/index.html added +301
| @@ -0,0 +1,301 @@ | ||
| 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>Orchard</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: ew-resize; 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="An apple tree with a ladder and a wicker basket. Drag or press the left and right arrow keys to slide the basket under the falling apples: ripe 1, golden 3, brown costs a life."></canvas></div> | |
| 30 | <div id="scores"> | |
| 31 | <div class="row"><b id="score">0</b><span>score</span></div> | |
| 32 | <div class="row"><b id="best">0</b><span>best</span><span class="dots" id="lives"></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: 'orchard', | |
| 42 | rules: [ | |
| 43 | 'Drag or use arrows to catch apples.', | |
| 44 | 'Ripe 1, golden 3. Brown costs a life.', | |
| 45 | 'Three lives; apples fall faster.', | |
| 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="orchard.js"></script> | |
| 52 | <script> | |
| 53 | 'use strict'; | |
| 54 | ||
| 55 | const ORCHARD = window.Orchard; | |
| 56 | const APPLE = { ripe: [204, 50, 44], golden: [240, 194, 58], brown: [112, 72, 42] }; | |
| 57 | const WICKER = [200, 152, 86], WICKER_DARK = [92, 62, 40], WOOD = [176, 130, 84]; | |
| 58 | const WASP = [236, 196, 40], WASP_DARK = [34, 28, 24]; | |
| 59 | const BASKET_Y = ORCHARD.BASKET_Y; | |
| 60 | ||
| 61 | function groundColor(x, y, ix, iy, i) { | |
| 62 | const edge = Math.pow(Math.pow(Math.abs(x) / 7.4, 4) + Math.pow(Math.abs(y + 1.1) / 3.9, 4), .25); | |
| 63 | const rough = edge + (noise(x * 1.3, y * 1.3) - .5) * .13; | |
| 64 | if (rough > 1.04) return 0; | |
| 65 | if (rough > .93 && hash(ix, iy) < (rough - .93) / .11) return 0; | |
| 66 | kinds[i] = KIND_GRASS; | |
| 67 | const t = noise(x * .9, y * .9) * 1.15 + (hash(ix, iy) - .5) * .55 - .08; | |
| 68 | return rgb(...GRASS[clamp(Math.floor(t * 4.2), 0, 3)]); | |
| 69 | } | |
| 70 | ||
| 71 | const TREE = ORCHARD.TREE; | |
| 72 | const LADDER = { bottom: [-3.1, -1.2], top: [-2.1, -1.55], height: 3.5, width: .6 }; | |
| 73 | function ladderAt(t) { | |
| 74 | return [lerp(LADDER.bottom[0], LADDER.top[0], t), lerp(LADDER.bottom[1], LADDER.top[1], t), t * LADDER.height]; | |
| 75 | } | |
| 76 | function growLadder() { | |
| 77 | const color = () => WOOD, half = LADDER.width / 2; | |
| 78 | for (const side of [-half, half]) { | |
| 79 | const a = ladderAt(0), b = ladderAt(1); | |
| 80 | capsule([a[0], a[1] + side, 0], [b[0], b[1] + side, b[2]], .06, .05, color, 'rough'); | |
| 81 | } | |
| 82 | for (let r = 1; r <= 7; r++) { | |
| 83 | const p = ladderAt(r / 8); | |
| 84 | capsule([p[0], p[1] - half, p[2]], [p[0], p[1] + half, p[2]], .04, .04, color, 'rough'); | |
| 85 | } | |
| 86 | for (let t = 0; t <= 1; t += .1) { | |
| 87 | const p = ladderAt(t); | |
| 88 | castShadow(p[0], p[1], p[2], .12, 1, 0); | |
| 89 | } | |
| 90 | } | |
| 91 | function bake() { | |
| 92 | zb.fill(-1e9); | |
| 93 | pix.fill(rgb(255, 255, 255)); | |
| 94 | for (let iy = 0; iy < H; iy++) for (let ix = 0; ix < W; ix++) { | |
| 95 | const g = unproject(ix + .5, iy + .5), i = iy * W + ix, c = groundColor(g.x, g.y, ix, iy, i); | |
| 96 | if (!c) continue; | |
| 97 | pix[i] = c; ground[i] = 1; | |
| 98 | zb[i] = (g.x + g.y) / R2 * COS; | |
| 99 | } | |
| 100 | growGrass(); | |
| 101 | groundZ.set(zb); | |
| 102 | growTree(TREE.x, TREE.y, TREE.scale, 230, 11); | |
| 103 | growLadder(); | |
| 104 | for (let z = 0; z <= 2.4; z += .4) castShadow(TREE.x, TREE.y, z, .3, 1, 0); | |
| 105 | bakeOcclusion(); | |
| 106 | staticShadow.set(shadowMask); | |
| 107 | applyShadows(groundZ); | |
| 108 | bakedPix.set(pix); | |
| 109 | bakedZ.set(zb); | |
| 110 | } | |
| 111 | ||
| 112 | /* ---------- game state ---------- */ | |
| 113 | const nodes = { | |
| 114 | score: document.getElementById('score'), best: document.getElementById('best'), | |
| 115 | lives: document.getElementById('lives'), status: document.getElementById('status'), | |
| 116 | }; | |
| 117 | const G = { time: 0, status: '', splats: [], keys: new Set(), dragging: false }; | |
| 118 | const BEST_KEY = 'pixel-plays-orchard-best'; | |
| 119 | const best0 = (() => { try { return Number(localStorage.getItem(BEST_KEY)) || 0; } catch (e) { return 0; } })(); | |
| 120 | let R = ORCHARD.newRound(best0, Date.now() & 0xffff); | |
| 121 | function renderHud() { | |
| 122 | nodes.score.textContent = R.score; | |
| 123 | nodes.best.textContent = R.best; | |
| 124 | if (nodes.lives.children.length !== ORCHARD.LIVES) nodes.lives.innerHTML = '<i></i>'.repeat(ORCHARD.LIVES); | |
| 125 | [...nodes.lives.children].forEach((el, k) => el.classList.toggle('off', k >= R.lives)); | |
| 126 | } | |
| 127 | function setStatus(text) { | |
| 128 | if (text === G.status) return; | |
| 129 | G.status = text; | |
| 130 | nodes.status.textContent = text; | |
| 131 | } | |
| 132 | const HINT = 'drag or use the arrow keys'; | |
| 133 | function restart() { | |
| 134 | const next = ORCHARD.again(R); | |
| 135 | if (!next) return false; | |
| 136 | R = next; G.splats = []; | |
| 137 | setStatus(HINT); | |
| 138 | renderHud(); | |
| 139 | return true; | |
| 140 | } | |
| 141 | function settle(e) { | |
| 142 | if (e.type === 'ground') { | |
| 143 | G.splats.push({ x: e.apple.x, y: e.apple.y, kind: e.kind, age: 0 }); | |
| 144 | sound('thud'); | |
| 145 | return; | |
| 146 | } | |
| 147 | if (e.kind === 'brown') { sound('buzz'); setStatus('wasp-bitten · lost a life'); } | |
| 148 | else if (e.kind === 'golden') { sound('chime'); setStatus('golden · +3'); } | |
| 149 | else { sound('pop'); setStatus('ripe · +1'); } | |
| 150 | } | |
| 151 | function update(dt) { | |
| 152 | G.time += dt; | |
| 153 | if (R.phase === 'play') { | |
| 154 | const dir = (G.keys.has('right') ? 1 : 0) - (G.keys.has('left') ? 1 : 0); | |
| 155 | if (dir) ORCHARD.steer(R, dir); | |
| 156 | else if (G.keys.released) { ORCHARD.steer(R, 0); G.keys.released = false; } | |
| 157 | } | |
| 158 | const was = R.phase, events = ORCHARD.advance(R, dt); | |
| 159 | events.forEach(settle); | |
| 160 | if (events.length) renderHud(); | |
| 161 | for (const s of G.splats) s.age += dt; | |
| 162 | G.splats = G.splats.filter(s => s.age < 1); | |
| 163 | if (was === 'play' && R.phase === 'over') { | |
| 164 | if (R.record) try { localStorage.setItem(BEST_KEY, String(R.best)); } catch (e) { /* private window: keep it for this visit */ } | |
| 165 | setStatus(`${R.score} points${R.record ? ' · new best' : ` · best ${R.best}`} · tap or space to play again`); | |
| 166 | } | |
| 167 | } | |
| 168 | ||
| 169 | /* ---------- drawing ---------- */ | |
| 170 | function drawApple(a) { | |
| 171 | const wobble = a.state === 'wobble'; | |
| 172 | const shake = wobble ? (Math.floor(a.age * 16) % 2 ? 1 : -1) * .06 * Math.min(1, a.age * 3) : 0; | |
| 173 | const x = a.x + shake, color = APPLE[a.kind]; | |
| 174 | sphere(x, a.y, a.z, .24, color, a.kind === 'brown' ? 'rough' : 'gloss'); | |
| 175 | if (a.kind === 'brown') { | |
| 176 | sphere(x + .14, a.y + .12, a.z + .12, .07, WASP_DARK, 'solid'); | |
| 177 | sphere(x + .2, a.y + .12, a.z + .12, .05, WASP, 'solid'); | |
| 178 | } | |
| 179 | if (!wobble) castShadow(x, a.y, a.z, .24, 1, clamp(a.z / 3, 0, 1) * .95); // soft up high, hard at the ground | |
| 180 | } | |
| 181 | function drawBasket(x) { | |
| 182 | const y = BASKET_Y, half = ORCHARD.CATCH_HALF + .1, depth = .45; | |
| 183 | box(x - half, y - depth, 0, x + half, y + depth, .62, WICKER, 3); | |
| 184 | box(x - half + .12, y - depth + .1, .62, x + half - .12, y + depth - .1, .64, WICKER_DARK, 5); | |
| 185 | capsule([x - half, y - depth, .66], [x - half, y + depth, .66], .06, .06, () => WICKER, 'solid'); | |
| 186 | capsule([x + half, y - depth, .66], [x + half, y + depth, .66], .06, .06, () => WICKER, 'solid'); | |
| 187 | for (const side of [-1, 1]) capsule([x + side * half, y, .66], [x, y, 1.3], .04, .04, () => WICKER, 'solid'); | |
| 188 | for (let z = .1; z < .6; z += .25) castShadow(x, y, z, half * .95, 1, 0); | |
| 189 | } | |
| 190 | function drawSplats() { | |
| 191 | for (const s of G.splats) { | |
| 192 | sphere(s.x, s.y, .1, .2, APPLE[s.kind], 'solid'); | |
| 193 | } | |
| 194 | } | |
| 195 | function draw() { | |
| 196 | pix.set(bakedPix); | |
| 197 | zb.set(bakedZ); | |
| 198 | pendingShadows.length = 0; | |
| 199 | drawSplats(); | |
| 200 | drawBasket(R.basketX); | |
| 201 | for (const a of R.apples) drawApple(a); | |
| 202 | const windT = STILL ? 0 : Math.floor(G.time * WIND_FPS) / WIND_FPS; | |
| 203 | drawLeaves(windT); | |
| 204 | drawTufts(windT); | |
| 205 | flushShadows(); | |
| 206 | applyShadows(bakedZ); | |
| 207 | shadeEdges(); | |
| 208 | ctx.putImageData(image, 0, 0); | |
| 209 | } | |
| 210 | ||
| 211 | /* ---------- input ---------- */ | |
| 212 | // screen x on the basket's line of the ground, in world units | |
| 213 | function worldX(e) { | |
| 214 | const r = canvas.getBoundingClientRect(); | |
| 215 | const sx = (e.clientX - r.left) / r.width * W; | |
| 216 | return (sx - OX) / S * R2 + BASKET_Y; | |
| 217 | } | |
| 218 | canvas.addEventListener('pointerdown', e => { | |
| 219 | ensureAudio(); | |
| 220 | canvas.focus({ preventScroll: true }); | |
| 221 | if (restart()) return; | |
| 222 | G.dragging = true; | |
| 223 | canvas.setPointerCapture(e.pointerId); | |
| 224 | ORCHARD.aim(R, worldX(e)); | |
| 225 | }); | |
| 226 | canvas.addEventListener('pointermove', e => { if (G.dragging) ORCHARD.aim(R, worldX(e)); }); | |
| 227 | const release = () => { G.dragging = false; }; | |
| 228 | canvas.addEventListener('pointerup', release); | |
| 229 | canvas.addEventListener('pointercancel', release); | |
| 230 | const KEYS = { ArrowLeft: 'left', a: 'left', A: 'left', ArrowRight: 'right', d: 'right', D: 'right' }; | |
| 231 | canvas.addEventListener('keydown', e => { | |
| 232 | if (e.key === ' ' || e.key === 'Enter') { | |
| 233 | e.preventDefault(); | |
| 234 | ensureAudio(); | |
| 235 | if (!e.repeat) restart(); | |
| 236 | return; | |
| 237 | } | |
| 238 | const dir = KEYS[e.key]; | |
| 239 | if (!dir) return; | |
| 240 | e.preventDefault(); | |
| 241 | ensureAudio(); | |
| 242 | G.dragging = false; | |
| 243 | G.keys.add(dir); | |
| 244 | }); | |
| 245 | canvas.addEventListener('keyup', e => { | |
| 246 | const dir = KEYS[e.key]; | |
| 247 | if (!dir) return; | |
| 248 | G.keys.delete(dir); | |
| 249 | if (!G.keys.size) G.keys.released = true; | |
| 250 | }); | |
| 251 | canvas.addEventListener('blur', () => { G.keys.clear(); G.keys.released = true; }); | |
| 252 | ||
| 253 | /* ---------- sound ---------- */ | |
| 254 | let audio = null, muted = false; | |
| 255 | function ensureAudio() { | |
| 256 | if (audio) { if (audio.state === 'suspended') audio.resume(); return; } | |
| 257 | const AC = window.AudioContext || window.webkitAudioContext; | |
| 258 | if (AC) audio = new AC(); | |
| 259 | } | |
| 260 | function sound(kind) { | |
| 261 | if (!audio || muted) return; | |
| 262 | const now = audio.currentTime, gain = audio.createGain(), osc = audio.createOscillator(); | |
| 263 | const spec = { | |
| 264 | pop: [380, 560, .08, .09, 'sine'], | |
| 265 | chime: [660, 1320, .22, .09, 'triangle'], | |
| 266 | buzz: [150, 90, .25, .07, 'sawtooth'], | |
| 267 | thud: [140, 70, .08, .12, 'sine'], | |
| 268 | }[kind]; | |
| 269 | osc.type = spec[4]; | |
| 270 | osc.frequency.setValueAtTime(spec[0], now); | |
| 271 | osc.frequency.exponentialRampToValueAtTime(spec[1], now + spec[2]); | |
| 272 | gain.gain.setValueAtTime(spec[3], now); | |
| 273 | gain.gain.exponentialRampToValueAtTime(.0005, now + spec[2]); | |
| 274 | osc.connect(gain).connect(audio.destination); | |
| 275 | osc.start(now); osc.stop(now + spec[2] + .01); | |
| 276 | } | |
| 277 | const muteButton = document.getElementById('mute'); | |
| 278 | muteButton.addEventListener('click', () => { | |
| 279 | muted = !muted; | |
| 280 | muteButton.textContent = muted ? 'sound off' : 'sound on'; | |
| 281 | muteButton.setAttribute('aria-pressed', String(muted)); | |
| 282 | }); | |
| 283 | ||
| 284 | /* ---------- fit and boot ---------- */ | |
| 285 | addEventListener('resize', fit); | |
| 286 | fit(); | |
| 287 | bake(); | |
| 288 | setStatus(HINT); | |
| 289 | renderHud(); | |
| 290 | canvas.focus({ preventScroll: true }); | |
| 291 | let last = performance.now(); | |
| 292 | function frame(now) { | |
| 293 | update(Math.min(.04, (now - last) / 1000)); | |
| 294 | last = now; | |
| 295 | draw(); | |
| 296 | requestAnimationFrame(frame); | |
| 297 | } | |
| 298 | requestAnimationFrame(frame); | |
| 299 | </script> | |
| 300 | </body> | |
| 301 | </html> |
orchard/orchard.js added +99
| @@ -0,0 +1,99 @@ | ||
| 1 | 'use strict'; | |
| 2 | // The orchard rules: one tree drops apples, a basket slides along the ground to catch them. | |
| 3 | // Plain numbers in, plain numbers out, no DOM. The page draws what this decides. | |
| 4 | (function (root) { | |
| 5 | const TREE = { x: 0, y: -1.5, scale: .9 }; // the tree the page draws; its crown reaches y + 2 * scale at the front | |
| 6 | const HANG_Y = TREE.y + 2.6, HANG = [-2.3, 2.3], HANG_Z = 2.7; // apples ripen on the crown's front and lower face, clear of the leaves | |
outdated
|
||
| 7 | const REACH = 3.6, BASKET_Y = HANG_Y, BASKET_SPEED = 10; // the basket slides on x in [-REACH, REACH] under them | |
| 8 | const WOBBLE = .9, FIRST_SPAWN = 1; // seconds an apple shakes before it lets go | |
| 9 | const BASKET_TOP = .7, CATCH_HALF = .95; // an apple falling through BASKET_TOP within CATCH_HALF of the basket is caught | |
| 10 | const GRAVITY = 5, GRAVITY_RAMP = .12, GRAVITY_MAX = 14; // fall acceleration: GRAVITY + RAMP per second of round, up to MAX | |
| 11 | const GAP = 1.5, GAP_RAMP = .02, GAP_MIN = .55; // seconds between apples, shrinking | |
| 12 | const GOLDEN = .12, BROWN = .14, BROWN_RAMP = .004, BROWN_MAX = .34; // odds of each kind | |
| 13 | const POINTS = { ripe: 1, golden: 3, brown: 0 }; | |
| 14 | const LIVES = 3, RESTART_AFTER = .6; | |
| 15 | ||
| 16 | const finite = v => (Number.isFinite(v) ? v : 0); | |
| 17 | const clamp = (v, a, b) => Math.max(a, Math.min(b, v)); | |
| 18 | // small seeded generator so a round is repeatable: state in, [value, state] out | |
| 19 | function roll(seed) { | |
| 20 | let t = (seed + 0x6D2B79F5) >>> 0; | |
| 21 | const out = Math.imul(t ^ (t >>> 15), 1 | t); | |
| 22 | const mixed = (out + Math.imul(out ^ (out >>> 7), 61 | out)) ^ out; | |
| 23 | return [((mixed ^ (mixed >>> 14)) >>> 0) / 4294967296, t]; | |
| 24 | } | |
| 25 | ||
| 26 | const gravityAt = time => Math.min(GRAVITY_MAX, GRAVITY + GRAVITY_RAMP * Math.max(0, finite(time))); | |
| 27 | const gapAt = time => Math.max(GAP_MIN, GAP - GAP_RAMP * Math.max(0, finite(time))); | |
| 28 | const brownAt = time => Math.min(BROWN_MAX, BROWN + BROWN_RAMP * Math.max(0, finite(time))); | |
| 29 | ||
| 30 | // is an apple at x, falling past the basket's rim, inside the basket's mouth? | |
outdated
With |
||
| 31 | function inBasket(appleX, basketX) { | |
| 32 | return Number.isFinite(appleX) && Math.abs(appleX - finite(basketX)) <= CATCH_HALF; | |
| 33 | } | |
| 34 | ||
| 35 | function newRound(best = 0, seed = 1) { | |
| 36 | return { | |
| 37 | phase: 'play', time: 0, score: 0, best: Math.max(0, finite(best)), record: false, lives: LIVES, | |
| 38 | basketX: 0, target: 0, apples: [], spawnIn: FIRST_SPAWN, seed: finite(seed) >>> 0, timer: 0, | |
| 39 | }; | |
| 40 | } | |
| 41 | // aim the basket at x (a drag); it slides there at BASKET_SPEED | |
| 42 | function aim(s, x) { s.target = clamp(finite(x), -REACH, REACH); } | |
| 43 | // hold a direction (-1, 0, 1), as the arrow keys do | |
| 44 | function steer(s, dir) { s.target = dir < 0 ? -REACH : dir > 0 ? REACH : s.basketX; } | |
| 45 | ||
| 46 | function spawn(s) { | |
| 47 | let a, b, c; | |
| 48 | [a, s.seed] = roll(s.seed); [b, s.seed] = roll(s.seed); [c, s.seed] = roll(s.seed); | |
| 49 | const kind = b < brownAt(s.time) ? 'brown' : c < GOLDEN ? 'golden' : 'ripe'; | |
| 50 | s.apples.push({ kind, x: HANG[0] + a * (HANG[1] - HANG[0]), y: HANG_Y, z: HANG_Z, vz: 0, state: 'wobble', age: 0 }); | |
| 51 | } | |
| 52 | // advance by dt seconds; returns what happened: [{ type: 'catch'|'ground', kind, points, apple }] | |
| 53 | function advance(s, dt) { | |
| 54 | dt = clamp(finite(dt), 0, .1); | |
| 55 | const events = []; | |
| 56 | if (s.phase === 'over') { s.timer += dt; return events; } | |
| 57 | s.time += dt; | |
| 58 | const reach = BASKET_SPEED * dt; | |
| 59 | s.basketX += clamp(s.target - s.basketX, -reach, reach); | |
| 60 | s.spawnIn -= dt; | |
| 61 | if (s.spawnIn <= 0) { spawn(s); s.spawnIn = gapAt(s.time) + WOBBLE; } | |
| 62 | const g = gravityAt(s.time); | |
| 63 | for (const a of s.apples) { | |
| 64 | if (a.state === 'wobble') { | |
| 65 | a.age += dt; | |
| 66 | if (a.age >= WOBBLE) a.state = 'fall'; | |
| 67 | continue; | |
| 68 | } | |
| 69 | if (a.state !== 'fall') continue; | |
| 70 | const before = a.z; | |
| 71 | a.vz -= g * dt; | |
| 72 | a.z += a.vz * dt; | |
| 73 | if (before > BASKET_TOP && a.z <= BASKET_TOP && inBasket(a.x, s.basketX)) { | |
| 74 | a.state = 'caught'; | |
| 75 | const points = POINTS[a.kind]; | |
| 76 | if (a.kind === 'brown') s.lives--; | |
| 77 | s.score += points; | |
outdated
|
||
| 78 | if (s.score > s.best) { s.best = s.score; s.record = true; } | |
| 79 | events.push({ type: 'catch', kind: a.kind, points, apple: a }); | |
| 80 | } else if (a.z <= 0) { | |
| 81 | a.z = 0; a.state = 'ground'; | |
| 82 | events.push({ type: 'ground', kind: a.kind, points: 0, apple: a }); | |
| 83 | } | |
| 84 | } | |
| 85 | s.apples = s.apples.filter(a => a.state === 'wobble' || a.state === 'fall'); | |
| 86 | if (s.lives <= 0) { s.phase = 'over'; s.timer = 0; s.apples = []; } | |
| 87 | return events; | |
| 88 | } | |
| 89 | // a tap after the round has been over for a moment starts the next one; otherwise null | |
| 90 | function again(s) { | |
| 91 | return s.phase === 'over' && s.timer > RESTART_AFTER ? newRound(s.best, s.seed + 1) : null; | |
| 92 | } | |
| 93 | ||
| 94 | const api = { | |
| 95 | TREE, REACH, BASKET_Y, BASKET_SPEED, HANG, HANG_Y, HANG_Z, WOBBLE, BASKET_TOP, CATCH_HALF, POINTS, LIVES, RESTART_AFTER, | |
| 96 | gravityAt, gapAt, brownAt, inBasket, newRound, aim, steer, advance, again, | |
| 97 | }; | |
| 98 | if (typeof module !== 'undefined') module.exports = api; else root.Orchard = api; | |
| 99 | })(typeof window !== 'undefined' ? window : globalThis); |
orchard/orchard.test.mjs added +159
| @@ -0,0 +1,159 @@ | ||
| 1 | import { test, expect } from 'bun:test'; | |
| 2 | import { TREE, HANG_Y, BASKET_Y, newRound, aim, steer, advance, again, inBasket, gravityAt, gapAt, brownAt, BASKET_TOP, CATCH_HALF, WOBBLE, POINTS, LIVES, REACH, HANG } from './orchard.js'; | |
| 3 | ||
| 4 | // an apple already falling just above the basket's rim | |
| 5 | const run = (s, seconds, dt = .05) => { for (let t = 0; t < seconds; t += dt) advance(s, dt); }; | |
| 6 | const falling = (s, kind, x) => { | |
| 7 | const a = { kind, x, y: 0, z: BASKET_TOP + .05, vz: -2, state: 'fall', age: 0 }; | |
| 8 | s.apples.push(a); | |
| 9 | s.spawnIn = 99; | |
| 10 | return a; | |
| 11 | }; | |
| 12 | ||
| 13 | test('an apple over the basket at ground level is caught and scores', () => { | |
| 14 | const s = newRound(0); | |
| 15 | falling(s, 'ripe', s.basketX); | |
| 16 | const events = advance(s, .05); | |
| 17 | expect(events.map(e => [e.type, e.kind, e.points])).toEqual([['catch', 'ripe', 1]]); | |
| 18 | expect(s.score).toBe(1); | |
| 19 | expect(s.lives).toBe(LIVES); | |
| 20 | expect(s.apples).toEqual([]); | |
| 21 | }); | |
| 22 | ||
| 23 | test('an apple beside the basket is missed and hits the ground', () => { | |
| 24 | const s = newRound(0); | |
| 25 | falling(s, 'ripe', s.basketX + CATCH_HALF + .3); | |
| 26 | let events = []; | |
| 27 | for (let n = 0; n < 20 && !events.length; n++) events = advance(s, .05); | |
| 28 | expect(events.map(e => e.type)).toEqual(['ground']); | |
| 29 | expect(s.score).toBe(0); | |
| 30 | expect(s.apples).toEqual([]); | |
| 31 | }); | |
| 32 | ||
| 33 | test('the catch window is the basket mouth and nothing wider', () => { | |
| 34 | expect(inBasket(0, 0)).toBe(true); | |
| 35 | expect(inBasket(CATCH_HALF, 0)).toBe(true); | |
| 36 | expect(inBasket(CATCH_HALF + .01, 0)).toBe(false); | |
| 37 | expect(inBasket(-CATCH_HALF - .01, 0)).toBe(false); | |
| 38 | expect(inBasket(NaN, 0)).toBe(false); | |
outdated
This pins a NaN apple as caught. Flip it to |
||
| 39 | expect(inBasket(Infinity, 0)).toBe(false); | |
| 40 | }); | |
| 41 | ||
| 42 | test('golden apples score 3 and ripe ones 1', () => { | |
| 43 | expect(POINTS).toEqual({ ripe: 1, golden: 3, brown: 0 }); | |
| 44 | const s = newRound(0); | |
| 45 | falling(s, 'golden', 0); | |
| 46 | advance(s, .05); | |
| 47 | expect(s.score).toBe(3); | |
| 48 | expect(s.best).toBe(3); | |
| 49 | expect(s.record).toBe(true); | |
| 50 | }); | |
| 51 | ||
| 52 | test('catching a brown apple costs a life and scores nothing', () => { | |
| 53 | const s = newRound(0); | |
| 54 | falling(s, 'brown', 0); | |
| 55 | const events = advance(s, .05); | |
| 56 | expect(events[0]).toMatchObject({ type: 'catch', kind: 'brown', points: 0 }); | |
| 57 | expect(s.lives).toBe(LIVES - 1); | |
| 58 | expect(s.score).toBe(0); | |
| 59 | }); | |
| 60 | ||
| 61 | test('letting a brown apple fall costs nothing', () => { | |
| 62 | const s = newRound(0); | |
| 63 | falling(s, 'brown', REACH); | |
| 64 | for (let n = 0; n < 20; n++) advance(s, .05); | |
| 65 | expect(s.lives).toBe(LIVES); | |
| 66 | }); | |
| 67 | ||
| 68 | test('three brown apples end the round, and a tap restarts it after a moment, keeping the best', () => { | |
| 69 | const s = newRound(0); | |
| 70 | s.score = 4; s.best = 4; | |
| 71 | for (let n = 0; n < LIVES; n++) { falling(s, 'brown', 0); advance(s, .05); } | |
| 72 | expect(s.phase).toBe('over'); | |
| 73 | expect(s.lives).toBe(0); | |
| 74 | expect(again(s)).toBe(null); | |
| 75 | run(s, 1); | |
| 76 | const next = again(s); | |
| 77 | expect(next.phase).toBe('play'); | |
| 78 | expect(next.best).toBe(4); | |
| 79 | expect(next.score).toBe(0); | |
| 80 | expect(next.lives).toBe(LIVES); | |
| 81 | }); | |
| 82 | ||
| 83 | test('apples wobble first, then fall one at a time', () => { | |
| 84 | const s = newRound(0); | |
| 85 | run(s, 1.1); | |
| 86 | expect(s.apples).toHaveLength(1); | |
| 87 | expect(s.apples[0].state).toBe('wobble'); | |
| 88 | const z = s.apples[0].z; | |
| 89 | run(s, WOBBLE / 3); | |
| 90 | expect(s.apples[0].state).toBe('wobble'); | |
| 91 | expect(s.apples[0].z).toBe(z); | |
| 92 | run(s, WOBBLE); | |
| 93 | expect(s.apples[0].z).toBeLessThan(z); | |
| 94 | for (let n = 0; n < 400; n++) { | |
| 95 | advance(s, .05); | |
| 96 | expect(s.apples.filter(a => a.state === 'wobble').length).toBeLessThanOrEqual(1); | |
| 97 | } | |
| 98 | }); | |
| 99 | ||
| 100 | test('apples fall faster and arrive sooner as the round goes on', () => { | |
| 101 | expect(gravityAt(60)).toBeGreaterThan(gravityAt(0)); | |
| 102 | expect(gravityAt(1e6)).toBe(gravityAt(1000)); | |
| 103 | expect(gapAt(60)).toBeLessThan(gapAt(0)); | |
| 104 | expect(gapAt(1e6)).toBeGreaterThan(0); | |
| 105 | expect(brownAt(60)).toBeGreaterThan(brownAt(0)); | |
| 106 | const drop = time => { | |
| 107 | const s = newRound(0); | |
| 108 | s.time = time; | |
| 109 | const a = { kind: 'ripe', x: 0, y: 0, z: 3, vz: 0, state: 'fall', age: 0 }; | |
| 110 | s.apples.push(a); s.spawnIn = 99; s.target = REACH; | |
| 111 | let t = 0; | |
| 112 | while (a.state === 'fall') { advance(s, .01); t += .01; } | |
| 113 | return t; | |
| 114 | }; | |
| 115 | expect(drop(60)).toBeLessThan(drop(0)); | |
| 116 | }); | |
| 117 | ||
| 118 | test('the basket slides towards its target at a limited speed and stays on the ground strip', () => { | |
| 119 | const s = newRound(0); | |
| 120 | s.spawnIn = 99; | |
| 121 | aim(s, 2); | |
| 122 | advance(s, .05); | |
| 123 | expect(s.basketX).toBeGreaterThan(0); | |
| 124 | expect(s.basketX).toBeLessThan(2); | |
| 125 | for (let n = 0; n < 40; n++) advance(s, .05); | |
| 126 | expect(s.basketX).toBe(2); | |
| 127 | aim(s, 1e9); | |
| 128 | expect(s.target).toBe(REACH); | |
| 129 | aim(s, NaN); | |
| 130 | expect(s.target).toBe(0); | |
| 131 | steer(s, -1); | |
| 132 | expect(s.target).toBe(-REACH); | |
| 133 | steer(s, 0); | |
| 134 | expect(s.target).toBe(s.basketX); | |
| 135 | }); | |
| 136 | ||
| 137 | test('spawned apples hang along the front of the tree and come in three kinds, same seed same round', () => { | |
| 138 | const kinds = new Set(), run = seed => { | |
| 139 | const s = newRound(0, seed), seen = []; | |
| 140 | for (let n = 0; n < 4000; n++) { | |
| 141 | advance(s, .05); | |
| 142 | for (const a of s.apples) if (!seen.includes(a)) seen.push(a); | |
| 143 | s.lives = LIVES; | |
| 144 | } | |
| 145 | return seen; | |
| 146 | }; | |
| 147 | const a = run(7), b = run(7); | |
| 148 | expect(a.map(x => x.kind + x.x)).toEqual(b.map(x => x.kind + x.x)); | |
| 149 | for (const x of a) { kinds.add(x.kind); expect(x.x).toBeGreaterThanOrEqual(HANG[0]); expect(x.x).toBeLessThanOrEqual(HANG[1]); } | |
| 150 | expect([...kinds].sort()).toEqual(['brown', 'golden', 'ripe']); | |
| 151 | }); | |
| 152 | ||
| 153 | test('apples hang in front of the tree the page draws, and the basket runs under them', () => { | |
| 154 | expect(HANG_Y).toBeGreaterThan(TREE.y + 2 * TREE.scale); // past the crown's front leaves | |
| 155 | expect(BASKET_Y).toBe(HANG_Y); | |
| 156 | const s = newRound(0); | |
| 157 | run(s, 1.1); | |
| 158 | expect(s.apples[0].y).toBe(HANG_Y); | |
| 159 | }); |
serve.test.mjs +7
| @@ -59,6 +59,13 @@ test('Row Home and its model script are served without a track', async () => { | ||
| 59 | 59 | expect((await fetch(base + '/row-home/model.js')).status).toBe(200); |
| 60 | 60 | }); |
| 61 | 61 | |
| 62 | test('Orchard and its rules script are served without a track', async () => { | |
| 63 | const page = await fetch(base + '/orchard/'); | |
| 64 | expect(page.status).toBe(200); | |
| 65 | expect(page.headers.get('content-type')).toBe('text/html; charset=utf-8'); | |
| 66 | expect((await fetch(base + '/orchard/orchard.js')).status).toBe(200); | |
| 67 | }); | |
| 68 | ||
| 62 | 69 | test('stone skip is served without a track', async () => { |
| 63 | 70 | const page = await fetch(base + '/stone-skip/'); |
| 64 | 71 | expect(page.status).toBe(200); |
shell.js +1
| @@ -16,6 +16,7 @@ window.PixelPlays = (() => { | ||
| 16 | 16 | { id: 'sled-run', title: 'Sled Run', blurb: 'Ride a sledge down a snowy hill: tuck for speed, release on a kicker to jump, land flat', music: false, forged: true }, |
| 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 | { 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 }, | |
| 19 | 20 | ]; |
| 20 | 21 | const MUSIC_VOLUME = .4; |
| 21 | 22 | const CREDITS = [ |
Space and Enter also restart, so name them: "tap or space to play again".