Public: anyone can read this; the crew can change it.
Migrated from github.com/amirmushichge/pixel-plays
Add Kite Hill: keep a kite in the air through 60 seconds of gusts #9
Check · Review · Land · Live done — Landed and live in production.
quill/pixel-plays:quill/5-add-kite-hill-keep-a-kite-in-the-air-thr → main 3 commits fork 6 threads · 6 unresolved
README.md +2 −1
| @@ -14,6 +14,7 @@ Concept by [abhi](https://x.com/awwbhi2). Game by [AmirMushich](https://x.com/Am | ||
| 14 | 14 | | [](laundry/) | Get the washing into the basket before the storm | Drag; space | |
| 15 | 15 | | [](tie-break/) | Clay-court tie-break against a bot, the moment you hit decides where the ball lands | Tap; space | |
| 16 | 16 | | [](mini-ramp/) | 60 seconds on a skateboard ramp, pump for height and grab in the air | Hold and release; space | |
| 17 | | [](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 | | |
| 17 | 18 | | [](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 | |
| 18 | 19 | |
| 19 | 20 | ## Run |
| @@ -28,7 +29,7 @@ Then open http://127.0.0.1:5300/. `PORT` and `HOST` override the port and addres | ||
| 28 | 29 | 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. |
| 29 | 30 | |
| 30 | 31 | ## Add a game |
| 31 | 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`. 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 plays on its own sound effects). | |
| 32 | 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`. 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 and Kite Hill 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`. | |
| 32 | 33 | |
| 33 | 34 | - `shell.js` provides the game menu, the Rules popup, the credits and the background music. |
| 34 | 35 | - `engine.js` provides the camera, depth buffer, colour ramps, shadows, edges, figures, trees and wind as globals. |
kite-hill/index.html added +326
| @@ -0,0 +1,326 @@ | ||
| 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>Kite Hill</title> | |
| 7 | <style> | |
| 8 | :root { : #1c1b19; : #8b877f; : #fff; } | |
| 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 | #scores b { font-size: 16px; font-weight: 600; font-variant-numeric: tabular-nums; } | |
| 17 | #scores span { color: var(); font-variant-numeric: tabular-nums; } | |
| 18 | #bottom { bottom: max(14px, env(safe-area-inset-bottom)); align-items: flex-end; } | |
| 19 | #status { color: var(); } | |
| 20 | button { pointer-events: auto; font: inherit; color: var(); background: none; border: 0; padding: 10px; margin: -10px; cursor: pointer; } | |
| 21 | button:hover, button:focus-visible { color: var(); } | |
| 22 | </style> | |
| 23 | </head> | |
| 24 | <body> | |
| 25 | <div id="stage"><canvas id="c" tabindex="0" aria-label="Kite Hill. Hold to pull the line and climb, let go to let out line. Watch the grass: a gust is coming, so let out line. Too tight snaps the line, too slack drops the kite."></canvas></div> | |
| 26 | <div id="scores"><b id="score">0</b><span id="meta"></span></div> | |
| 27 | <div class="hud" id="bottom"> | |
| 28 | <div id="status" role="status" aria-live="polite"></div> | |
| 29 | <button id="mute" type="button" aria-pressed="false">sound on</button> | |
| 30 | </div> | |
| 31 | <script src="../shell.js"></script> | |
| 32 | <script> | |
| 33 | PixelPlays.mount({ | |
| 34 | id: 'kite-hill', | |
| 35 | rules: [ | |
| 36 | 'Hold, tap or press space to pull the line: the kite climbs and the line tightens. Let go to let out line.', | |
| 37 | 'Gusts bend the grass a moment before they reach the kite. Let go before one lands.', | |
| 38 | 'Too tight and the line snaps, too slack and the kite drops. You relaunch at once.', | |
| 39 | 'Score is seconds aloft plus height, over 60 seconds.', | |
| 40 | ], | |
| 41 | }); | |
| 42 | </script> | |
| 43 | <script>window.PP_SCENE = { originY: 40, shadowX: 1.1, shadowY: -.3 };</script> | |
| 44 | <script src="../engine.js"></script> | |
| 45 | <script src="model.js"></script> | |
| 46 | <script> | |
| 47 | 'use strict'; | |
| 48 | ||
| 49 | const BEST_KEY = 'pixel-plays-kite-hill-best'; | |
| 50 | const KITE_BASE = 1.2, KITE_RISE = 12; | |
| 51 | const FIGURE = [-2.6, 1.8], TOWARD = [R2 / 2, -R2 / 2]; // the kite flies screen-right of the figure | |
| 52 | const KITE_LEFT = [214, 72, 52], KITE_RIGHT = [248, 200, 60], LINE_TAUT = [214, 64, 44]; | |
| 53 | const KID = { shirt: [226, 96, 56], stripe: [240, 230, 210], legs: [52, 66, 96], hair: [58, 38, 30], skin: [222, 170, 132] }; | |
| 54 | const ROCKS = [[7.1, -4.2, .7], [8.3, -4.4, .45], [-5.6, 5.2, .5]]; | |
| 55 | const TREE = [-8.5, 4.2]; | |
| 56 | ||
| 57 | /* ---------- ground ---------- */ | |
| 58 | function groundColor(x, y, ix, iy, i) { | |
| 59 | const u = (x - y) / R2, v = (x + y) / R2; | |
| 60 | const edge = Math.pow(Math.pow(Math.abs(u) / 14, 4) + Math.pow(Math.abs(v) / 6.6, 4), .25); | |
| 61 | const rough = edge + (noise(x * 1.3, y * 1.3) - .5) * .1; | |
| 62 | if (rough > 1.04) return 0; | |
| 63 | if (rough > .94 && hash(ix, iy) < (rough - .94) / .1) return 0; | |
| 64 | kinds[i] = KIND_GRASS; | |
| 65 | const t = noise(x * .9, y * .9) * 1.15 + (hash(ix, iy) - .5) * .55 - .08 + (1 - edge) * .14; | |
| 66 | return rgb(...GRASS[clamp(Math.floor(t * 4.2), 0, 3)]); | |
| 67 | } | |
| 68 | function bake() { | |
| 69 | zb.fill(-1e9); | |
| 70 | pix.fill(rgb(255, 255, 255)); | |
| 71 | for (let iy = 0; iy < H; iy++) for (let ix = 0; ix < W; ix++) { | |
| 72 | const g = unproject(ix + .5, iy + .5), i = iy * W + ix, c = groundColor(g.x, g.y, ix, iy, i); | |
| 73 | if (!c) continue; | |
| 74 | pix[i] = c; ground[i] = 1; | |
| 75 | zb[i] = (g.x + g.y) / R2 * COS; | |
| 76 | } | |
| 77 | growGrass(); | |
| 78 | groundZ.set(zb); | |
| 79 | growTree(TREE[0], TREE[1], .9, 320, 11); | |
| 80 | castShadow(TREE[0], TREE[1], 1.4, 1.2, 1, .4); | |
| 81 | for (const [x, y, r] of ROCKS) { | |
| 82 | sphere(x, y, r * .55, r, [150, 146, 150], 'rough'); | |
| 83 | castShadow(x, y, r, r * 1.1); | |
| 84 | } | |
| 85 | bakeOcclusion(); | |
| 86 | staticShadow.set(shadowMask); | |
| 87 | applyShadows(groundZ); | |
| 88 | bakedPix.set(pix); | |
| 89 | bakedZ.set(zb); | |
| 90 | } | |
| 91 | ||
| 92 | /* ---------- state ---------- */ | |
| 93 | const best0 = (() => { try { return Number(localStorage.getItem(BEST_KEY)) || 0; } catch (e) { return 0; } })(); | |
| 94 | let K = KiteModel.newState(best0), gusts = KiteModel.makeGusts(1), hold = false, time = 0, status = ''; | |
| 95 | const nodes = { score: document.getElementById('score'), meta: document.getElementById('meta'), status: document.getElementById('status') }; | |
| 96 | let shown = ''; | |
| 97 | function renderHud() { | |
| 98 | const text = Math.round(K.score) + '|' + Math.ceil(K.left) + '|' + Math.floor(K.aloft) + '|' + K.best; | |
| 99 | if (text === shown) return; | |
| 100 | shown = text; | |
| 101 | nodes.score.textContent = Math.round(K.score); | |
| 102 | nodes.meta.textContent = Math.ceil(K.left) + 's · aloft ' + Math.floor(K.aloft) + 's' + (K.best ? ' · best ' + K.best : ''); | |
| 103 | } | |
| 104 | function setStatus(text) { | |
| 105 | if (text === status) return; | |
| 106 | status = text; | |
| 107 | nodes.status.textContent = text; | |
| 108 | } | |
| 109 | function statusFor() { | |
| 110 | if (K.phase === 'ready') return 'hold to pull the line'; | |
| 111 | if (K.phase === 'over') return Math.round(K.score) + (K.record ? ' · new best' : '') + ' · tap to fly again'; | |
| 112 | if (K.phase === 'lost') return K.cause === 'snap' ? 'line snapped' : 'kite dropped'; | |
| 113 | const ahead = KiteModel.forecast(gusts, K.t); | |
| 114 | if (K.T > .8) return 'line taut · let out'; | |
| 115 | if (ahead > .15) return 'gust coming · let out'; | |
| 116 | if (K.T < .28) return 'line slack · pull'; | |
| 117 | if (ahead < -.12) return 'lull coming · pull'; | |
| 118 | return ''; | |
| 119 | } | |
| 120 | function reset() { | |
| 121 | K = KiteModel.newState(K.best); | |
| 122 | gusts = KiteModel.makeGusts((Math.random() * 1e9) | 0); | |
| 123 | hold = false; | |
| 124 | } | |
| 125 | function press() { | |
| 126 | ensureAudio(); | |
| 127 | if (K.phase === 'over') { if (overT > .8) reset(); return; } | |
| 128 | if (K.phase === 'ready') { KiteModel.launch(K); sound('launch'); } | |
| 129 | hold = true; | |
| 130 | } | |
| 131 | function release() { hold = false; } | |
| 132 | let overT = 0, lastPhase = 'ready'; | |
| 133 | function update(dt) { | |
| 134 | time += dt; | |
| 135 | KiteModel.step(K, dt, hold, gusts); | |
| 136 | if (K.phase !== lastPhase) { | |
| 137 | if (K.phase === 'lost') sound(K.cause); | |
| 138 | if (K.phase === 'over') { | |
| 139 | hold = false; overT = 0; | |
| 140 | if (K.record) try { localStorage.setItem(BEST_KEY, String(K.best)); } catch (e) { /* private window: keep it for this visit */ } | |
| 141 | } | |
| 142 | lastPhase = K.phase; | |
| 143 | } | |
| 144 | if (K.phase === 'over') overT += dt; | |
| 145 | setStatus(statusFor()); | |
| 146 | renderHud(); | |
| 147 | } | |
| 148 | ||
| 149 | /* ---------- drawing ---------- */ | |
| 150 | // tufts bend with the base breeze, plus the gust the kite will feel LEAD seconds later, sweeping left to right | |
outdated
|
||
| 151 | const reach = new Float32Array(W); | |
| 152 | function drawGrass(windT, gustT) { | |
| 153 | for (let ix = 0; ix < W; ix++) reach[ix] = STILL ? 0 : KiteModel.gustAt(gusts, gustT + KiteModel.LEAD - ix / W * .9); | |
| 154 | for (const tuft of tufts) { | |
| 155 | const sway = Math.sin(windT * .8 + tuft.phase) + .5 * Math.sin(windT * 2.1 + tuft.phase * 2.3); | |
| 156 | const push = reach[tuft.ix] * 5, gust = sway + push * .6; | |
| 157 | const colors = staticShadow[tuft.i] ? tuftDark : tuftLit; | |
| 158 | for (let k = 1; k <= tuft.height; k++) { | |
| 159 | const jx = tuft.ix + Math.round((sway * .95 + push) * k / 3), jy = tuft.iy - k; | |
| 160 | if (jx < 0 || jx >= W) continue; | |
| 161 | const j = jy * W + jx; | |
| 162 | if ((kinds[j] !== KIND_GRASS && kinds[j] !== KIND_NONE) || zb[j] !== groundZ[j]) continue; | |
| 163 | pix[j] = tuft.bloom && k === tuft.height ? tuft.bloom : colors[clamp(tuft.base + k + (gust > 1 ? 1 : 0), 1, 4)]; | |
| 164 | } | |
| 165 | } | |
| 166 | } | |
| 167 | function drawKid() { | |
| 168 | const pulling = hold && K.phase === 'fly'; | |
| 169 | const pose = { stance: .16, crouch: pulling ? .3 : 0, lean: pulling ? -.2 : .06, armR: pulling ? 2.5 : 2.1, armL: pulling ? 2.2 : 1.9 }; | |
| 170 | return figure(FIGURE[0], FIGURE[1], -Math.PI / 4, pose, KID, dynamicAdd); | |
| 171 | } | |
| 172 | // the kite hangs further out and higher as it climbs; wind jitters it, tension makes it shake | |
| 173 | function kitePosition() { | |
| 174 | const lostSnap = K.phase === 'lost' && K.cause === 'snap', away = lostSnap ? K.lostT : 0; | |
| 175 | const along = 3.4 + K.h * 5.4 + away * 7; | |
| 176 | let z = KITE_BASE + K.h * KITE_RISE + away * 3; | |
| 177 | if (K.phase === 'lost' && K.cause === 'drop') z = Math.max(.3, 1.2 - K.lostT * 6); | |
| 178 | const bob = STILL ? 0 : Math.sin(Math.floor(time * WIND_FPS) / WIND_FPS * 2.2) * .25; | |
| 179 | return [FIGURE[0] + TOWARD[0] * along, FIGURE[1] + TOWARD[1] * along, z + bob]; | |
| 180 | } | |
| 181 | function drawLine(hand, kite) { | |
| 182 | const slack = K.phase === 'lost' ? 1 : 1 - clamp(K.T, 0, 1); | |
| 183 | let end = kite; | |
| 184 | if (K.phase === 'lost' && K.cause === 'snap') { | |
| 185 | const k = Math.min(1, K.lostT * .8); | |
| 186 | end = [lerp(kite[0], hand[0], k), lerp(kite[1], hand[1], k), Math.max(.2, lerp(kite[2], hand[2], k) - K.lostT * K.lostT * 6)]; | |
| 187 | } | |
| 188 | const color = K.phase === 'fly' && K.T > .8 ? rgb(...LINE_TAUT) : INK; | |
| 189 | const steps = 56; | |
| 190 | for (let n = 0; n <= steps; n++) { | |
| 191 | const t = n / steps, sag = slack * slack * 3.2 * 4 * t * (1 - t); | |
| 192 | const p = project(lerp(hand[0], end[0], t), lerp(hand[1], end[1], t), Math.max(.1, lerp(hand[2], end[2], t) - sag)); | |
| 193 | const ix = p.sx | 0, iy = p.sy | 0; | |
| 194 | if (ix >= 0 && iy >= 0 && ix < W && iy < H) pix[iy * W + ix] = color; | |
| 195 | } | |
| 196 | } | |
| 197 | function drawKite(pos) { | |
| 198 | const p = project(...pos), shake = K.phase === 'fly' && K.T > .8 && !STILL ? Math.floor(time * WIND_FPS) % 2 : 0; | |
| 199 | const cx = Math.round(p.sx) + shake, cy = Math.round(p.sy); | |
| 200 | const left = rgb(...KITE_LEFT), right = rgb(...KITE_RIGHT), dots = [], fillL = [], fillR = []; | |
| 201 | for (let dy = -5; dy <= 7; dy++) { | |
| 202 | const half = dy <= -1 ? Math.round(4 * (dy + 5) / 4) : Math.round(4 * (7 - dy) / 8); | |
| 203 | for (let dx = -half; dx <= half; dx++) { | |
| 204 | const at = [cx + dx, cy + dy]; | |
| 205 | if (Math.abs(dx) === half || dx === 0 || dy === -5 || dy === 7) dots.push(at); | |
| 206 | else (dx < 0 ? fillL : fillR).push(at); | |
| 207 | } | |
| 208 | } | |
| 209 | haloDots(dots); | |
| 210 | drawDots(fillL, left); | |
| 211 | drawDots(fillR, right); | |
| 212 | drawDots(dots, INK); | |
| 213 | // the tail swings in whole pixels and leans harder in a gust | |
| 214 | const stepT = STILL ? 0 : Math.floor(time * WIND_FPS), lean = 1 + Math.round(KiteModel.windAt(gusts, K.t) * 2.5); | |
| 215 | const tail = [], bows = []; | |
| 216 | for (let n = 1; n <= 6; n++) { | |
| 217 | const at = [cx + Math.round(Math.sin(stepT * .9 + n * 1.1) * lean * n / 5), cy + 7 + n * 2]; | |
| 218 | (n % 2 ? bows : tail).push(at); | |
| 219 | } | |
| 220 | haloDots(tail.concat(bows)); | |
| 221 | drawDots(tail, INK); | |
| 222 | drawDots(bows, SIGNAL); | |
| 223 | } | |
| 224 | function draw() { | |
| 225 | pix.set(bakedPix); | |
| 226 | zb.set(bakedZ); | |
| 227 | pendingShadows.length = 0; | |
| 228 | const hand = drawKid(); | |
| 229 | const windT = STILL ? 0 : Math.floor(time * WIND_FPS) / WIND_FPS; | |
| 230 | drawLeaves(windT); | |
| 231 | drawGrass(windT, Math.floor(K.t * WIND_FPS) / WIND_FPS); | |
| 232 | flushShadows(); | |
| 233 | applyShadows(bakedZ); | |
| 234 | shadeEdges(); | |
| 235 | const kite = kitePosition(); | |
| 236 | drawLine(hand, kite); | |
| 237 | drawKite(kite); | |
| 238 | if (K.phase === 'ready') { | |
| 239 | const head = project(FIGURE[0], FIGURE[1], 3.4); | |
| 240 | drawArrow(head.sx, head.sy, time); | |
| 241 | } | |
| 242 | ctx.putImageData(image, 0, 0); | |
| 243 | } | |
| 244 | ||
| 245 | /* ---------- input ---------- */ | |
| 246 | canvas.addEventListener('pointerdown', e => { | |
| 247 | canvas.focus({ preventScroll: true }); | |
| 248 | canvas.setPointerCapture(e.pointerId); | |
| 249 | press(); | |
| 250 | }); | |
| 251 | canvas.addEventListener('pointerup', release); | |
| 252 | canvas.addEventListener('pointercancel', release); | |
| 253 | addEventListener('blur', release); | |
| 254 | canvas.addEventListener('keydown', e => { | |
| 255 | if (e.key !== ' ' && e.key !== 'Enter') return; | |
| 256 | e.preventDefault(); | |
| 257 | if (!e.repeat) press(); | |
| 258 | }); | |
| 259 | canvas.addEventListener('keyup', e => { | |
| 260 | if (e.key === ' ' || e.key === 'Enter') release(); | |
| 261 | }); | |
| 262 | ||
| 263 | /* ---------- sound ---------- */ | |
| 264 | let audio = null, muted = false, breeze = null; | |
| 265 | function ensureAudio() { | |
| 266 | if (audio) { if (audio.state === 'suspended') audio.resume(); return; } | |
| 267 | const AC = window.AudioContext || window.webkitAudioContext; | |
| 268 | if (!AC) return; | |
| 269 | audio = new AC(); | |
| 270 | const buffer = audio.createBuffer(1, audio.sampleRate, audio.sampleRate), data = buffer.getChannelData(0); | |
| 271 | for (let i = 0; i < data.length; i++) data[i] = Math.random() * 2 - 1; | |
| 272 | const src = audio.createBufferSource(), filter = audio.createBiquadFilter(); | |
| 273 | src.buffer = buffer; src.loop = true; | |
| 274 | filter.type = 'bandpass'; filter.frequency.value = 520; filter.Q.value = .6; | |
| 275 | breeze = audio.createGain(); | |
| 276 | breeze.gain.value = 0; | |
| 277 | src.connect(filter).connect(breeze).connect(audio.destination); | |
| 278 | src.start(); | |
| 279 | } | |
| 280 | function sound(kind) { | |
| 281 | if (!audio || muted) return; | |
| 282 | const now = audio.currentTime, gain = audio.createGain(), osc = audio.createOscillator(); | |
| 283 | const spec = { | |
| 284 | launch: [420, 760, .14, .12, 'triangle'], | |
| 285 | snap: [1500, 300, .16, .22, 'square'], | |
| 286 | drop: [260, 70, .22, .3, 'triangle'], | |
| 287 | }[kind]; | |
| 288 | if (!spec) return; | |
| 289 | osc.type = spec[4]; | |
| 290 | osc.frequency.setValueAtTime(spec[0], now); | |
| 291 | osc.frequency.exponentialRampToValueAtTime(spec[1], now + spec[2]); | |
| 292 | gain.gain.setValueAtTime(spec[3], now); | |
| 293 | gain.gain.exponentialRampToValueAtTime(.0005, now + spec[2]); | |
| 294 | osc.connect(gain).connect(audio.destination); | |
| 295 | osc.start(now); osc.stop(now + spec[2] + .01); | |
| 296 | } | |
| 297 | function updateBreeze() { | |
| 298 | if (!breeze) return; | |
| 299 | const level = KiteModel.windAt(gusts, K.t); | |
| 300 | breeze.gain.setTargetAtTime(muted ? 0 : .01 + level * level * .09, audio.currentTime, .1); | |
| 301 | } | |
| 302 | const muteButton = document.getElementById('mute'); | |
| 303 | muteButton.addEventListener('click', () => { | |
| 304 | muted = !muted; | |
| 305 | muteButton.textContent = muted ? 'sound off' : 'sound on'; | |
| 306 | muteButton.setAttribute('aria-pressed', String(muted)); | |
| 307 | }); | |
| 308 | ||
| 309 | /* ---------- boot ---------- */ | |
| 310 | addEventListener('resize', fit); | |
| 311 | fit(); | |
| 312 | bake(); | |
| 313 | setStatus(statusFor()); | |
| 314 | renderHud(); | |
| 315 | let last = performance.now(); | |
| 316 | function frame(now) { | |
| 317 | update(Math.min(.04, (now - last) / 1000)); | |
| 318 | last = now; | |
| 319 | updateBreeze(); | |
| 320 | draw(); | |
| 321 | requestAnimationFrame(frame); | |
| 322 | } | |
| 323 | requestAnimationFrame(frame); | |
| 324 | </script> | |
| 325 | </body> | |
| 326 | </html> | |
outdated
This debug hook ships to players, and no test uses it. Remove it. If a test needs to drive the page, add that test in this pull. |
kite-hill/model.js added +71
| @@ -0,0 +1,71 @@ | ||
| 1 | 'use strict'; | |
| 2 | // Kite Hill rules as plain data in, plain data out: the wind schedule, line tension and the score. | |
| 3 | // The page draws from this state and the tests drive it, so both read the same numbers. | |
| 4 | (function (root) { | |
| 5 | const ROUND = 60, LEAD = 1.4, BASE_WIND = .42, PULL = .4; | |
| 6 | const SNAP_AT = .95, SNAP_HOLD = .3, SLACK_BELOW = .35, RELAUNCH = 1.6, LAUNCH_HEIGHT = .3; | |
| 7 | const clamp = (v, a, b) => Math.max(a, Math.min(b, v)); | |
| 8 | ||
| 9 | function random(seed) { | |
| 10 | let s = (seed >>> 0) || 1; | |
| 11 | return () => (s = (Math.imul(s, 1664525) + 1013904223) >>> 0) / 4294967296; | |
| 12 | } | |
| 13 | // a gust (amp > 0) or a lull (amp < 0): a smooth bump of half-width `dur` seconds centred on `at` | |
| 14 | function makeGusts(seed) { | |
| 15 | const next = random(seed), gusts = []; | |
| 16 | for (let at = 3.5; at < ROUND + 6; at += 4.2 + next() * 2.2) { | |
| 17 | const lull = next() < .35; | |
| 18 | gusts.push({ at, dur: 1.1 + next() * .6, amp: lull ? -(.22 + next() * .1) : .3 + next() * .18 }); | |
| 19 | } | |
| 20 | return gusts; | |
| 21 | } | |
| 22 | function gustAt(gusts, t) { | |
| 23 | let sum = 0; | |
| 24 | for (const g of gusts) { | |
| 25 | const x = (t - g.at) / g.dur; | |
| 26 | if (x > -1 && x < 1) sum += g.amp * (1 - x * x) * (1 - x * x); | |
| 27 | } | |
| 28 | return sum; | |
| 29 | } | |
| 30 | const windAt = (gusts, t) => clamp(BASE_WIND + .05 * Math.sin(t * .7) + gustAt(gusts, t), .05, 1); | |
| 31 | // what the grass shows now: the change that reaches the kite LEAD seconds from now | |
| 32 | const forecast = (gusts, t) => gustAt(gusts, t + LEAD) - gustAt(gusts, t); | |
| 33 | ||
| 34 | function newState(best = 0) { | |
| 35 | return { phase: 'ready', t: 0, left: ROUND, h: LAUNCH_HEIGHT, T: .35, snapT: 0, lostT: 0, cause: '', score: 0, aloft: 0, best, record: false }; | |
| 36 | } | |
| 37 | function launch(s) { | |
| 38 | s.phase = 'fly'; s.h = LAUNCH_HEIGHT; s.T = .4; s.snapT = 0; s.lostT = 0; s.cause = ''; | |
| 39 | } | |
| 40 | function lose(s, cause) { s.phase = 'lost'; s.cause = cause; s.lostT = 0; s.snapT = 0; } | |
| 41 | function finish(s) { | |
| 42 | s.phase = 'over'; | |
| 43 | s.score = Math.round(s.score); | |
| 44 | s.record = s.score > s.best; | |
| 45 | if (s.record) s.best = s.score; | |
| 46 | } | |
| 47 | // 'ready' and 'over' wait for the player; every other phase advances with the clock | |
| 48 | function step(s, dt, hold, gusts) { | |
| 49 | if (s.phase === 'ready' || s.phase === 'over') return s; | |
| 50 | s.t += dt; | |
| 51 | s.left = Math.max(0, ROUND - s.t); | |
| 52 | if (s.phase === 'lost') { | |
| 53 | s.lostT += dt; | |
| 54 | if (s.lostT >= RELAUNCH && s.left > 0) launch(s); | |
| 55 | } else { | |
| 56 | const target = windAt(gusts, s.t) * (.55 + .45 * s.h) + (hold ? PULL : 0); | |
| 57 | s.T += (target - s.T) * Math.min(1, dt * 5); | |
| 58 | s.h = clamp(s.h + (s.T - SLACK_BELOW) * (s.T < SLACK_BELOW ? 1.1 : .55) * dt, 0, 1); | |
| 59 | s.snapT = s.T >= SNAP_AT ? s.snapT + dt : Math.max(0, s.snapT - dt); | |
| 60 | s.score += dt * (10 + 20 * s.h); | |
| 61 | s.aloft += dt; | |
| 62 | if (s.snapT >= SNAP_HOLD) lose(s, 'snap'); | |
| 63 | else if (s.h <= 0) lose(s, 'drop'); | |
| 64 | } | |
| 65 | if (s.left <= 0) finish(s); | |
| 66 | return s; | |
| 67 | } | |
| 68 | const api = { ROUND, LEAD, RELAUNCH, makeGusts, gustAt, windAt, forecast, newState, launch, step }; | |
outdated
Nothing outside this file reads |
||
| 69 | if (typeof module !== 'undefined' && module.exports) module.exports = api; | |
| 70 | else root.KiteModel = api; | |
| 71 | })(typeof window !== 'undefined' ? window : globalThis); |
kite-hill/model.test.mjs added +89
| @@ -0,0 +1,89 @@ | ||
| 1 | import { test, expect } from 'bun:test'; | |
| 2 | import { createRequire } from 'node:module'; | |
| 3 | ||
| 4 | const M = createRequire(import.meta.url)('./model.js'); | |
| 5 | const calm = []; | |
| 6 | const fly = () => { const s = M.newState(); M.launch(s); return s; }; | |
| 7 | const run = (s, seconds, hold, gusts = calm) => { for (let i = 0; i < seconds * 60; i++) M.step(s, 1 / 60, hold, gusts); return s; }; | |
| 8 | ||
| 9 | test('the same seed makes the same wind, and gusts and lulls both occur', () => { | |
| 10 | expect(M.makeGusts(7)).toEqual(M.makeGusts(7)); | |
| 11 | const g = M.makeGusts(7); | |
| 12 | expect(g.some(x => x.amp > 0)).toBe(true); | |
| 13 | expect(g.some(x => x.amp < 0)).toBe(true); | |
| 14 | }); | |
| 15 | ||
| 16 | test('a gust raises the wind at its centre and is gone outside its width', () => { | |
| 17 | const g = [{ at: 10, dur: 1.5, amp: .4 }]; | |
| 18 | expect(M.gustAt(g, 10)).toBeCloseTo(.4); | |
| 19 | expect(M.gustAt(g, 12)).toBe(0); | |
| 20 | expect(M.windAt(g, 10)).toBeGreaterThan(M.windAt(g, 12) + .3); | |
| 21 | }); | |
| 22 | ||
| 23 | test('the grass forecast leads the wind by LEAD seconds', () => { | |
| 24 | const g = [{ at: 10, dur: 1.5, amp: .4 }]; | |
| 25 | expect(M.forecast(g, 10 - M.LEAD)).toBeGreaterThan(.3); | |
| 26 | expect(M.forecast(g, 4)).toBe(0); | |
| 27 | }); | |
| 28 | ||
| 29 | test('pulling lifts the kite and tightens the line', () => { | |
| 30 | const s = fly(), h = s.h, T = s.T; | |
| 31 | run(s, 1, true); | |
| 32 | expect(s.h).toBeGreaterThan(h); | |
| 33 | expect(s.T).toBeGreaterThan(T); | |
| 34 | expect(s.phase).toBe('fly'); | |
| 35 | }); | |
| 36 | ||
| 37 | test('pulling through a gust snaps the line; letting out survives it', () => { | |
| 38 | const gust = [{ at: 2, dur: 1.5, amp: .48 }]; | |
| 39 | const pulled = fly(); pulled.h = 1; run(pulled, 2.5, true, gust); | |
| 40 | expect(pulled.phase).toBe('lost'); | |
| 41 | expect(pulled.cause).toBe('snap'); | |
| 42 | const eased = fly(); eased.h = 1; run(eased, 3, false, gust); | |
| 43 | expect(eased.phase).toBe('fly'); | |
| 44 | }); | |
| 45 | ||
| 46 | test('a slack line drops the kite in a lull, and a pull holds it up', () => { | |
| 47 | const lull = [{ at: 0, dur: 1.7, amp: -.3 }]; | |
| 48 | const slack = fly(); run(slack, 2, false, lull); | |
| 49 | expect(slack.phase).toBe('lost'); | |
outdated
This OR lets the test pass when the kite never drops. Use a lull that is sure to drop it, then assert |
||
| 50 | expect(slack.cause).toBe('drop'); | |
| 51 | const held = fly(); held.h = .5; run(held, 2, true, lull); | |
| 52 | expect(held.phase).toBe('fly'); | |
| 53 | }); | |
| 54 | ||
| 55 | test('a lost kite relaunches after RELAUNCH seconds while the round runs on', () => { | |
| 56 | const s = fly(); s.h = .01; s.T = .1; | |
| 57 | run(s, 1, false); | |
| 58 | expect(s.phase).toBe('lost'); | |
| 59 | run(s, M.RELAUNCH + .1, false); | |
| 60 | expect(s.phase).toBe('fly'); | |
| 61 | }); | |
| 62 | ||
| 63 | test('score grows with time aloft and with height, not while lost', () => { | |
| 64 | const low = fly(), high = fly(); high.h = 1; | |
| 65 | run(low, 1, false); run(high, 1, false); | |
| 66 | expect(high.score).toBeGreaterThan(low.score); | |
| 67 | const lost = fly(); lost.h = .01; lost.T = .1; run(lost, .5, false); | |
| 68 | expect(lost.phase).toBe('lost'); | |
| 69 | const before = lost.score; run(lost, .5, false); | |
| 70 | expect(lost.score).toBe(before); | |
| 71 | }); | |
| 72 | ||
| 73 | test('the round ends at 60 seconds and keeps the best', () => { | |
| 74 | const s = M.newState(100); M.launch(s); s.h = .8; | |
| 75 | run(s, 62, true, calm); | |
| 76 | expect(s.phase).toBe('over'); | |
| 77 | expect(s.left).toBe(0); | |
| 78 | expect(s.score).toBeGreaterThan(100); | |
| 79 | expect(s.record).toBe(true); | |
| 80 | expect(s.best).toBe(s.score); | |
| 81 | const t = s.t; M.step(s, 1, true, calm); | |
| 82 | expect(s.t).toBe(t); | |
| 83 | }); | |
| 84 | ||
| 85 | test('ready waits for the player', () => { | |
| 86 | const s = M.newState(); | |
| 87 | run(s, 2, true); | |
| 88 | expect(s.t).toBe(0); | |
| 89 | }); |
serve.test.mjs +7
| @@ -22,6 +22,13 @@ test('every game page and its track is served', async () => { | ||
| 22 | 22 | } |
| 23 | 23 | }); |
| 24 | 24 | |
| 25 | test('Kite Hill and its model script are served', async () => { | |
outdated
This test doesn't check "needs no track". That behaviour comes from |
||
| 26 | const page = await fetch(base + '/kite-hill/'); | |
| 27 | expect(page.status).toBe(200); | |
| 28 | expect(page.headers.get('content-type')).toBe('text/html; charset=utf-8'); | |
| 29 | expect((await fetch(base + '/kite-hill/model.js')).status).toBe(200); | |
| 30 | }); | |
| 31 | ||
| 25 | 32 | test('stone skip is served without a track', async () => { |
| 26 | 33 | const page = await fetch(base + '/stone-skip/'); |
| 27 | 34 | expect(page.status).toBe(200); |
shell.js +1
| @@ -8,6 +8,7 @@ window.PixelPlays = (() => { | ||
| 8 | 8 | { id: 'laundry', title: 'Rooftop Laundry' }, |
| 9 | 9 | { id: 'tie-break', title: 'Tie-Break' }, |
| 10 | 10 | { id: 'mini-ramp', title: 'Mini Ramp' }, |
| 11 | { id: 'kite-hill', title: 'Kite Hill', music: false, forged: true }, | |
| 11 | 12 | { id: 'stone-skip', title: 'Stone Skip', music: false, forged: true }, |
| 12 | 13 | ]; |
| 13 | 14 | const MUSIC_VOLUME = .4; |
shell.test.mjs +5
| @@ -38,6 +38,11 @@ test('a music: false game never asks for a track', guarded(async () => { | ||
| 38 | 38 | expect(audios).toEqual([]); |
| 39 | 39 | })); |
| 40 | 40 | |
| 41 | test('Kite Hill has no track, so it never asks for one', guarded(async () => { | |
| 42 | await mount('kite-hill'); | |
| 43 | expect(audios).toEqual([]); | |
| 44 | })); | |
| 45 | ||
| 41 | 46 | test('forged games credit the engine, not the game, to AmirMushich', guarded(async () => { |
| 42 | 47 | const credits = await mount('stone-skip'); |
| 43 | 48 | const t = text(credits); |
HAND_Zis never read. The hand position comes fromfigure(). Remove it.