Public: anyone can read this; the crew can change it.
Migrated from github.com/amirmushichge/pixel-plays
Add Conkers: the playable page and its place in the catalogue, on the rules in `conkers/match.js` #283
Check · Review · Land · Live done — Landed and live in production.
vesper/pixel-plays:vesper/258-add-conkers-the-playable-page-and-its-pl → main 1 commit fork
9 files
changed +492
−2
README.md +3 −2
Diff not shown — a line 2,702 characters wide (limit 2,000). +3 −2
View fileconkers/index.html added +365
| @@ -0,0 +1,365 @@ | ||
| 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>Conkers</title> | |
| 7 | <!-- og:begin --> | |
| 8 | <meta name="description" content="Time your swing to crack every rival conker in the playground"> | |
| 9 | <link rel="canonical" href="https://pixel-plays--forge-trevato-apps.open-platform.sh/conkers/"> | |
| 10 | <meta property="og:type" content="website"> | |
| 11 | <meta property="og:site_name" content="Pixel Plays"> | |
| 12 | <meta property="og:title" content="Conkers"> | |
| 13 | <meta property="og:description" content="Time your swing to crack every rival conker in the playground"> | |
| 14 | <meta property="og:url" content="https://pixel-plays--forge-trevato-apps.open-platform.sh/conkers/"> | |
| 15 | <meta property="og:image" content="https://pixel-plays--forge-trevato-apps.open-platform.sh/og/conkers.png"> | |
| 16 | <meta property="og:image:type" content="image/png"> | |
| 17 | <meta property="og:image:width" content="1200"> | |
| 18 | <meta property="og:image:height" content="630"> | |
| 19 | <meta property="og:image:alt" content="Conkers: the opening scene of the game"> | |
| 20 | <meta name="twitter:card" content="summary_large_image"> | |
| 21 | <!-- og:end --> | |
| 22 | <style> | |
| 23 | :root { : #1c1b19; : #6b675f; : #fff; : #8a4b22; } | |
| 24 | * { box-sizing: border-box; } | |
| 25 | html, body { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent; } | |
| 26 | html, body { height: 100%; margin: 0; background: var(); color: var(); } | |
| 27 | body { font: 13px/1.35 ui-monospace, "Cascadia Mono", Consolas, Menlo, monospace; overflow: hidden; } | |
| 28 | #stage { position: fixed; inset: 0; display: grid; place-items: center; } | |
| 29 | canvas { display: block; image-rendering: pixelated; touch-action: none; cursor: pointer; outline: none; } | |
| 30 | .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; align-items: flex-end; gap: 16px; pointer-events: none; } | |
| 31 | #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; } | |
| 32 | #bottom { bottom: max(20px, env(safe-area-inset-bottom)); } | |
| 33 | .row { display: flex; align-items: center; gap: 8px; color: var(); } | |
| 34 | .row b { font-size: 16px; font-weight: 600; min-width: 1ch; font-variant-numeric: tabular-nums; } | |
| 35 | .row span { color: var(); } | |
| 36 | #status { color: var(); min-height: 1.35em; } | |
| 37 | button { pointer-events: auto; font: inherit; color: var(); background: none; border: 0; padding: 13px 10px; margin: -13px -10px; min-height: 44px; min-width: 44px; cursor: pointer; } | |
| 38 | button:hover, button:focus-visible { color: var(); } | |
| 39 | button:focus-visible { outline: 2px solid var(); outline-offset: 2px; } | |
| 40 | noscript p { position: fixed; inset: 40% 16px auto; margin: 0; text-align: center; color: var(); } | |
| 41 | </style> | |
| 42 | </head> | |
| 43 | <body> | |
| 44 | <div id="stage"><canvas id="c" tabindex="0" aria-label="A school playground under a horse-chestnut tree: your conker, with a double knot and a chalk cross, and the rival's conker hang side by side on their strings, with a power meter below. Tap the canvas, or press Space, to swing when the meter is where you want it: the notched gold sweet spot in the middle does 4 damage, anywhere else up to the hatched end does 1, and the hatched end tangles the strings and does nothing. You and the rival take turns; crack all 3 rivals' conkers before yours cracks."></canvas></div> | |
| 45 | <noscript><p>Conkers needs JavaScript to play.</p></noscript> | |
| 46 | <div id="scores"> | |
| 47 | <div class="row"><b id="bout">1 of 3</b><span id="name">Smudge</span></div> | |
| 48 | <div class="row"><b id="mine">12</b><span>hit points, your conker</span></div> | |
| 49 | <div class="row"><b id="theirs">6</b><span>hit points, theirs</span></div> | |
| 50 | <div class="row"><b id="score">0</b><span>points</span></div> | |
| 51 | </div> | |
| 52 | <div class="hud" id="bottom"> | |
| 53 | <div id="status" role="status" aria-live="polite"></div> | |
| 54 | <button id="mute" type="button" aria-pressed="false">sound on</button> | |
| 55 | </div> | |
| 56 | <script src="../shell.js"></script> | |
| 57 | <script> | |
| 58 | PixelPlays.mount({ | |
| 59 | id: 'conkers', | |
| 60 | rules: [ | |
| 61 | 'Tap the canvas, or press Space, to swing when the meter is where you want it.', | |
| 62 | 'The notched gold sweet spot does 4 damage, elsewhere 1; the hatched end tangles and does 0.', | |
| 63 | 'You and the rival swing in turn; their meter comes from the seed.', | |
| 64 | 'Crack all 3 rivals before your conker cracks.', | |
| 65 | ], | |
| 66 | }); | |
| 67 | </script> | |
| 68 | <script>window.PP_SCENE = { originY: 0, shadowX: 1, shadowY: -.12 };</script> | |
| 69 | <script src="../engine.js"></script> | |
| 70 | <script src="match.js"></script> | |
| 71 | <script> | |
| 72 | 'use strict'; | |
| 73 | ||
| 74 | const M = window.Conkers; | |
| 75 | const REACH = 11; | |
| 76 | const AX = 1.9, AY = -1, AZ = 4.6, LEN = 2.9, REST = .75; // where the strings hang from, in world units | |
| 77 | const TARMAC = [[104, 108, 116], [98, 102, 110]], VERGE = [[84, 132, 74], [74, 122, 66]], CHALK_PIX = rgb(240, 238, 226); | |
| 78 | const MINE = [86, 44, 22], THEIRS = [[158, 84, 44], [120, 62, 34], [58, 36, 30]], STRING = [232, 214, 170], BRANCH = [96, 76, 62]; | |
| 79 | const INK_PIX = rgb(28, 27, 25), SIGNAL_PIX = rgb(255, 214, 10), WHITE_PIX = rgb(255, 255, 255), STRING_PIX = rgb(...STRING); | |
| 80 | const TANGLE_PIX = rgb(200, 74, 78), BAR_PIX = rgb(214, 208, 192); | |
| 81 | const BAR = { x0: 80, x1: 280, y0: 246, y1: 256 }; // the power meter, in canvas pixels | |
| 82 | const PERIOD = 1.8; // seconds for the meter to sweep up and back | |
| 83 | const swingTime = 0.85; // seconds a swing takes, hit or miss | |
| 84 | const meterAt = t => { const f = ((t / PERIOD) % 1 + 1) % 1; return f < .5 ? f * 2 : 2 - f * 2; }; | |
| 85 | ||
| 86 | /* ---------- baked scene: playground, chalk, tree and branches ---------- */ | |
| 87 | function chalk(x, y) { | |
| 88 | const ring = Math.abs(Math.hypot(x, y - 3.2) - 5.2) < .11; | |
| 89 | const hop = x > -10.2 && x < -7.4 && y > -1 && y < 8.6 && (Math.abs(((y + 1) % 2.4)) < .1 || Math.abs(x + 10.1) < .1 || Math.abs(x + 7.5) < .1); | |
| 90 | return ring || hop; | |
| 91 | } | |
| 92 | function floorColor(x, y, ix, iy) { | |
| 93 | const rough = Math.pow(Math.pow(Math.abs(x) / REACH, 6) + Math.pow(Math.abs(y - .4) / REACH, 6), 1 / 6) + (noise(x * 1.2, y * 1.2) - .5) * .05; | |
| 94 | if (rough > 1) return 0; | |
| 95 | const t = .97 + (hash(ix, iy) - .5) * .06; | |
| 96 | if (rough > .9) { const c = VERGE[(ix + iy >> 1) & 1]; return rgb(c[0] * t, c[1] * t, c[2] * t); } | |
| 97 | if (chalk(x, y) && hash(ix * 3, iy) > .15) return CHALK_PIX; | |
| 98 | const c = TARMAC[(ix + (iy >> 1)) & 1]; | |
| 99 | return rgb(c[0] * t, c[1] * t, c[2] * t); | |
| 100 | } | |
| 101 | const shade = (x, y, z, r) => castShadow(x, y, z, r, 1, 0); | |
| 102 | function bake() { | |
| 103 | ground.fill(0); shadowMask.fill(0); staticShadow.fill(0); kinds.fill(0); | |
| 104 | tufts.length = 0; leaves.length = 0; | |
| 105 | zb.fill(-1e9); | |
| 106 | pix.fill(WHITE_PIX); | |
| 107 | for (let iy = 0; iy < H; iy++) for (let ix = 0; ix < W; ix++) { | |
| 108 | const g = unproject(ix + .5, iy + .5), i = iy * W + ix, c = floorColor(g.x, g.y, ix, iy); | |
| 109 | if (!c) continue; | |
| 110 | pix[i] = c; ground[i] = 1; | |
| 111 | zb[i] = (g.x + g.y) / R2 * COS; | |
| 112 | } | |
| 113 | groundZ.set(zb); | |
| 114 | growTree(0, -5, 1.5, 460, 3); | |
| 115 | const bark = () => BRANCH; | |
| 116 | capsule([.4, -4.8, 3.3], [-AX, AY, AZ], .2, .15, bark, 'rough'); | |
| 117 | capsule([.4, -4.8, 3.3], [AX, AY, AZ], .2, .15, bark, 'rough'); | |
| 118 | for (let z = 0; z <= 3.4; z += .5) shade(0.1, -5, z, .5); | |
| 119 | for (let t = 0; t <= 1; t += .1) { shade(.4 - (.4 + AX) * t, -4.8 + (4.8 + AY) * t, 3.3 + (AZ - 3.3) * t, .3); shade(.4 + (AX - .4) * t, -4.8 + (4.8 + AY) * t, 3.3 + (AZ - 3.3) * t, .3); } | |
| 120 | bakeOcclusion(); | |
| 121 | staticShadow.set(shadowMask); | |
| 122 | applyShadows(groundZ); | |
| 123 | bakedPix.set(pix); | |
| 124 | bakedZ.set(zb); | |
| 125 | } | |
| 126 | ||
| 127 | /* ---------- state ---------- */ | |
| 128 | const nodes = {}; | |
| 129 | for (const k of ['bout', 'name', 'mine', 'theirs', 'score', 'status']) nodes[k] = document.getElementById(k); | |
| 130 | const shown = {}; | |
| 131 | function setText(key, text) { | |
| 132 | if (shown[key] === text) return; | |
| 133 | shown[key] = text; | |
| 134 | nodes[key].textContent = text; | |
| 135 | } | |
| 136 | const fresh = () => M.start(Math.floor(Math.random() * 4294967296)); | |
| 137 | let P = fresh(); | |
| 138 | // meterT: seconds the meter has swept; anim: the swing being shown; note: what the status says while it plays | |
| 139 | const G = { time: 0, meterT: 0, meter: 0, anim: null, note: '' }; | |
| 140 | const REFUSED = { 'not-your-turn': 'wait for their swing', over: 'the match is over', 'bad-meter': 'the meter is out of range' }; | |
| 141 | const START = 'tap or press Space when the meter is in the gold'; | |
| 142 | const playing = () => P.status === 'playing'; | |
| 143 | ||
| 144 | function renderHud() { | |
| 145 | setText('bout', `${P.bout + 1} of ${P.opponents.length}`); | |
| 146 | setText('name', P.opponent); | |
| 147 | setText('mine', String(P.playerHp)); | |
| 148 | setText('theirs', String(P.opponentHp)); | |
| 149 | setText('score', String(P.score)); | |
| 150 | } | |
| 151 | function say(text) { setText('status', text); } | |
| 152 | const wordsOf = { sweet: 'sweet spot', hit: 'a glancing hit', tangle: 'tangled strings, no damage', cracked: 'a crack', won: 'a crack', lost: 'a crack' }; | |
| 153 | function describe(by, event, meter) { | |
| 154 | const who = by === 'player' ? 'You' : P.opponent, at = Math.round(meter * 100); | |
| 155 | const text = event === 'cracked' || event === 'won' ? (by === 'player' ? 'crack their conker' : 'crack yours') : event === 'lost' ? 'crack your conker' : wordsOf[event]; | |
| 156 | return `${who} swing at ${at}%: ${text}`; | |
| 157 | } | |
| 158 | ||
| 159 | /* ---------- drawing the conkers ---------- */ | |
| 160 | const sideOf = by => (by === 'player' ? 0 : 1); | |
| 161 | const dirOf = side => (side === 0 ? 1 : -1); // the way a side swings toward the other | |
| 162 | // the swing angle of each side now (0 hangs straight down) and whether it is hidden | |
| 163 | function pose(side) { | |
| 164 | const a = G.anim; | |
| 165 | let theta = 0, hidden = false; | |
| 166 | if (!STILL) theta = Math.round(Math.sin(G.time * 1.4 + side * 2) * 3) / 3 * .05; | |
| 167 | if (a) { | |
| 168 | const u = Math.min(1, a.t / swingTime), mine = sideOf(a.by) === side; | |
| 169 | if (mine) theta = dirOf(side) * a.reach * Math.sin(Math.PI * u); | |
| 170 | else if (u > .5) theta = dirOf(sideOf(a.by)) * a.recoil * Math.sin(Math.PI * Math.min(1, (u - .5) / .5)); | |
| 171 | if (!mine && a.broken && u >= .5 && (u < .85 || !playing())) hidden = true; | |
| 172 | } | |
| 173 | if (!playing() && !a) hidden = P.won ? side === 1 : side === 0; | |
| 174 | return { theta, hidden }; | |
| 175 | } | |
| 176 | const bobAt = (side, theta) => [(side === 0 ? -AX : AX) + LEN * Math.sin(theta), AY, AZ - LEN * Math.cos(theta)]; | |
| 177 | function drawString(side, theta, bob, r, knot) { | |
| 178 | const ax = side === 0 ? -AX : AX, n = 36, top = bob[2] + r * 1.05 + knot; | |
| 179 | for (let k = 0; k <= n; k++) { | |
| 180 | const t = k / n; | |
| 181 | plot(ax + (bob[0] - ax) * t, AY, AZ + (top - AZ) * t, STRING_PIX); | |
| 182 | } | |
| 183 | } | |
| 184 | function drawConker(side, theta, r) { | |
| 185 | const bob = bobAt(side, theta); | |
| 186 | const mine = side === 0, tone = mine ? MINE : THEIRS[Math.min(P.bout, 2)]; | |
| 187 | sphere(bob[0], bob[1], bob[2], r, tone, 'gloss'); | |
| 188 | castShadow(bob[0], bob[1], bob[2], r * .9, 1, 0); | |
| 189 | const knotR = mine ? .3 : .17; | |
| 190 | sphere(bob[0], bob[1], bob[2] + r + knotR * .6, knotR, STRING, 'solid'); | |
| 191 | if (mine) sphere(bob[0] + .12, bob[1], bob[2] + r + .45, .2, STRING, 'solid'); | |
| 192 | drawString(side, theta, bob, r, mine ? .55 : knotR); | |
| 193 | return bob; | |
| 194 | } | |
| 195 | const FACE = [[-2, -2], [2, -2], [-1, -1], [1, -1], [0, 0], [-1, 1], [1, 1], [-2, 2], [2, 2]]; // the chalk cross | |
| 196 | const CRACK = [[0, -4], [1, -3], [1, -2], [2, -1], [1, 0], [2, 1], [3, 2], [-1, 1], [-2, 2], [-3, 3], [0, 2], [-2, -3]]; | |
| 197 | // marks drawn over the shaded picture: the chalk cross on your conker and a crack for each hit point lost | |
| 198 | function drawMarks(side, bob, hp, max) { | |
| 199 | const p = project(bob[0], bob[1], bob[2]), cx = Math.round(p.sx), cy = Math.round(p.sy); | |
| 200 | if (side === 0) drawDots(FACE.map(([x, y]) => [cx + x - 1, cy + y + 1]), WHITE_PIX); | |
| 201 | const lost = Math.min(CRACK.length, Math.floor((1 - hp / max) * CRACK.length)); | |
| 202 | drawDots(CRACK.slice(0, lost).map(([x, y]) => [cx + x + (side === 0 ? 2 : -1), cy + y]), INK_PIX); | |
| 203 | } | |
| 204 | function drawShards() { | |
| 205 | const a = G.anim; | |
| 206 | if (!a || !a.broken || a.t < swingTime * .5) return; | |
| 207 | const u = (a.t - swingTime * .5), side = 1 - sideOf(a.by), bob = bobAt(side, 0), tone = side === 0 ? MINE : THEIRS[Math.min(a.bout, 2)]; | |
| 208 | for (let k = 0; k < 12; k++) { | |
| 209 | const vx = (hash(k, 1) - .5) * 5, vy = (hash(k, 2) - .5) * 3, vz = 1 + hash(k, 3) * 3; | |
| 210 | const z = Math.max(.1, bob[2] + vz * u - 6 * u * u); | |
| 211 | plot(bob[0] + vx * u, bob[1] + vy * u, z, rgb(...tone.map(v => v * 1.3 + 20))); | |
| 212 | plot(bob[0] + vx * u + .1, bob[1] + vy * u, z, rgb(...tone.map(v => v * 1.3 + 20))); | |
| 213 | } | |
| 214 | } | |
| 215 | function drawMeter() { | |
| 216 | const { x0, x1, y0, y1 } = BAR, at = v => Math.round(x0 + (x1 - x0) * v); | |
| 217 | const dots = [], tangle = [], fill = [], notch = [], marker = []; | |
| 218 | for (let x = x0 - 1; x <= x1 + 1; x++) for (let y = y0 - 1; y <= y1 + 1; y++) { | |
| 219 | if (x < x0 || x > x1 || y < y0 || y > y1) { dots.push([x, y]); continue; } | |
| 220 | const v = (x - x0) / (x1 - x0); | |
| 221 | if (v > M.TANGLE_ABOVE) ((x + y) % 4 < 2 ? tangle : fill).push([x, y]); | |
| 222 | else if (v >= M.SWEET_LOW && v <= M.SWEET_HIGH) notch.push([x, y]); | |
| 223 | else fill.push([x, y]); | |
| 224 | } | |
| 225 | drawDots(dots, INK_PIX); drawDots(fill, BAR_PIX); drawDots(tangle, TANGLE_PIX); drawDots(notch, SIGNAL_PIX); | |
| 226 | // the notch: a tick at each end of the sweet spot and a pointer under its middle | |
| 227 | const lo = at(M.SWEET_LOW), hi = at(M.SWEET_HIGH), mid = Math.round((lo + hi) / 2), ticks = []; | |
| 228 | for (let y = y0 - 5; y < y0; y++) { ticks.push([lo, y], [hi, y]); } | |
| 229 | for (let k = 0; k < 5; k++) for (let x = mid - 4 + k; x <= mid + 4 - k; x++) ticks.push([x, y1 + 2 + k]); | |
| 230 | drawDots(ticks, INK_PIX); | |
| 231 | for (let y = y0 - 2; y <= y1 + 2; y++) marker.push([at(G.meter), y], [at(G.meter) + 1, y]); | |
| 232 | haloDots(marker); | |
| 233 | drawDots(marker, INK_PIX); | |
| 234 | if (G.anim && G.anim.by === 'opponent') { // their reading, as a ghost mark above the bar | |
| 235 | const gx = at(G.anim.meter), ghost = []; | |
| 236 | for (let k = 0; k < 4; k++) for (let x = gx - 3 + k; x <= gx + 3 - k; x++) ghost.push([x, y0 - 7 + k]); | |
| 237 | drawDots(ghost, TANGLE_PIX); | |
| 238 | } | |
| 239 | } | |
| 240 | function draw() { | |
| 241 | pix.set(bakedPix); | |
| 242 | zb.set(bakedZ); | |
| 243 | const marks = []; | |
| 244 | for (const side of [0, 1]) { | |
| 245 | const { theta, hidden } = pose(side); | |
| 246 | if (hidden) { drawString(side, 0, bobAt(side, 0), .75, 0); continue; } | |
| 247 | const r = side === 0 ? .75 : [.7, .8, .9][P.bout]; | |
| 248 | const hp = side === 0 ? P.playerHp : P.opponentHp, max = side === 0 ? M.PLAYER_HP : P.opponents[P.bout].hp; | |
| 249 | marks.push([side, drawConker(side, theta, r), hp, max]); | |
| 250 | } | |
| 251 | drawShards(); | |
| 252 | const windT = STILL ? 0 : Math.floor(G.time * WIND_FPS) / WIND_FPS; | |
| 253 | drawLeaves(windT); | |
| 254 | applyShadows(bakedZ); | |
| 255 | shadeEdges(); | |
| 256 | for (const m of marks) drawMarks(...m); | |
| 257 | drawMeter(); | |
| 258 | ctx.putImageData(image, 0, 0); | |
| 259 | } | |
| 260 | ||
| 261 | /* ---------- game: every rule is the rules script's ---------- */ | |
| 262 | function begin(r, by) { | |
| 263 | const wasBout = P.bout; | |
| 264 | const broken = r.event === 'cracked' || r.event === 'won' || r.event === 'lost'; | |
| 265 | P = r.state; | |
| 266 | G.anim = { | |
| 267 | by, event: r.event, t: 0, meter: by === 'player' ? G.meter : r.meter, broken, bout: broken && r.event !== 'lost' ? wasBout : P.bout, | |
| 268 | reach: r.event === 'tangle' ? .45 : .85, recoil: r.event === 'sweet' || broken ? .7 : r.event === 'hit' ? .35 : 0, | |
| 269 | }; | |
| 270 | G.note = describe(by, r.event, G.anim.meter); | |
| 271 | renderHud(); | |
| 272 | say(G.note); | |
| 273 | if (r.event === 'tangle') sound(150, .18, 'triangle'); | |
| 274 | else if (broken) { sound(190, .2, 'square'); sound(90, .3, 'sawtooth', .05); } | |
| 275 | else sound(r.event === 'sweet' ? 420 : 300, .09, 'triangle'); | |
| 276 | } | |
| 277 | function swing() { | |
| 278 | ensureAudio(); | |
| 279 | if (G.anim) return; | |
| 280 | if (!playing()) { restart(); return; } | |
| 281 | const r = M.swing(P, G.meter); | |
| 282 | if (!r.ok) { say(REFUSED[r.why] || 'cannot swing now'); sound(110, .1, 'sawtooth'); return; } | |
| 283 | begin(r, 'player'); | |
| 284 | } | |
| 285 | function restart() { | |
| 286 | P = fresh(); G.anim = null; G.note = ''; G.meterT = 0; | |
| 287 | renderHud(); | |
| 288 | say(START); | |
| 289 | } | |
| 290 | function update(dt) { | |
| 291 | G.time += dt; | |
| 292 | if (!G.anim && playing() && P.turn === 'player') G.meterT += dt; | |
| 293 | G.meter = G.anim ? G.meter : meterAt(G.meterT); | |
| 294 | const a = G.anim; | |
| 295 | if (!a) return; | |
| 296 | a.t += dt; | |
| 297 | if (a.t < swingTime) return; | |
| 298 | G.anim = null; | |
| 299 | if (playing() && P.turn === 'opponent') { | |
| 300 | const r = M.opponentTurn(P); | |
| 301 | if (r.ok) begin(r, 'opponent'); | |
| 302 | return; | |
| 303 | } | |
| 304 | if (!playing()) { | |
| 305 | say(`${P.line} · tap or press a key to play again`); | |
| 306 | if (P.won) { sound(523, .12, 'triangle'); sound(659, .12, 'triangle', .12); sound(784, .4, 'triangle', .24); } | |
| 307 | else sound(110, .5, 'sawtooth'); | |
| 308 | return; | |
| 309 | } | |
| 310 | say(P.line); | |
| 311 | } | |
| 312 | ||
| 313 | /* ---------- input ---------- */ | |
| 314 | canvas.addEventListener('pointerdown', () => { canvas.focus({ preventScroll: true }); swing(); }); | |
| 315 | canvas.addEventListener('keydown', e => { | |
| 316 | if (e.ctrlKey || e.metaKey || e.altKey || e.key === 'Tab') return; | |
| 317 | if (e.key !== ' ' && e.key !== 'Enter') return; | |
| 318 | e.preventDefault(); | |
| 319 | if (e.repeat) return; | |
| 320 | swing(); | |
| 321 | }); | |
| 322 | ||
| 323 | /* ---------- sound ---------- */ | |
| 324 | let audio = null, muted = false; | |
| 325 | function ensureAudio() { | |
| 326 | if (audio) { if (audio.state === 'suspended') audio.resume(); return; } | |
| 327 | const AC = window.AudioContext || window.webkitAudioContext; | |
| 328 | if (AC) audio = new AC(); | |
| 329 | } | |
| 330 | function sound(freq, life, type, delay = 0) { | |
| 331 | if (!audio || muted) return; | |
| 332 | const now = audio.currentTime + delay, gain = audio.createGain(), osc = audio.createOscillator(); | |
| 333 | osc.type = type; | |
| 334 | osc.frequency.setValueAtTime(freq, now); | |
| 335 | gain.gain.setValueAtTime(.0001, now); | |
| 336 | gain.gain.exponentialRampToValueAtTime(.07, now + .01); | |
| 337 | gain.gain.exponentialRampToValueAtTime(.0005, now + life); | |
| 338 | osc.connect(gain).connect(audio.destination); | |
| 339 | osc.start(now); osc.stop(now + life + .02); | |
| 340 | } | |
| 341 | const muteButton = document.getElementById('mute'); | |
| 342 | muteButton.addEventListener('click', () => { | |
| 343 | muted = !muted; | |
| 344 | muteButton.textContent = muted ? 'sound off' : 'sound on'; | |
| 345 | muteButton.setAttribute('aria-pressed', String(muted)); | |
| 346 | }); | |
| 347 | ||
| 348 | /* ---------- fit and boot ---------- */ | |
| 349 | addEventListener('resize', fit); | |
| 350 | fit(); | |
| 351 | bake(); | |
| 352 | renderHud(); | |
| 353 | say(START); | |
| 354 | canvas.focus({ preventScroll: true }); | |
| 355 | let last = performance.now(); | |
| 356 | function frame(now) { | |
| 357 | update(Math.min(.04, (now - last) / 1000)); | |
| 358 | last = now; | |
| 359 | draw(); | |
| 360 | requestAnimationFrame(frame); | |
| 361 | } | |
| 362 | requestAnimationFrame(frame); | |
| 363 | </script> | |
| 364 | </body> | |
| 365 | </html> |
conkers/page.test.mjs added +109
| @@ -0,0 +1,109 @@ | ||
| 1 | import { test, expect } from 'bun:test'; | |
| 2 | import { boot } from '../playtest.mjs'; | |
| 3 | ||
| 4 | const MID = [180, 140]; | |
| 5 | // run until the meter reads inside [lo, hi], then tap | |
| 6 | function swingAt(g, lo, hi) { | |
| 7 | for (let i = 0; i < 200 && !(g.read('G.meter') >= lo && g.read('G.meter') <= hi); i++) g.run(.04); | |
| 8 | const meter = g.read('G.meter'); | |
| 9 | g.tap(...MID); | |
| 10 | return meter; | |
| 11 | } | |
| 12 | ||
| 13 | test('a tap in the sweet spot swings through the rules, and the HUD reports the rules state', () => { | |
| 14 | const g = boot('conkers', { seed: 3 }); | |
| 15 | expect(g.el('theirs').textContent).toBe('6'); | |
| 16 | const meter = swingAt(g, .45, .55); | |
| 17 | expect(g.read('P.opponentHp')).toBe(2); | |
| 18 | expect(g.read('P.swings')).toBe(1); | |
| 19 | expect(g.el('theirs').textContent).toBe('2'); | |
| 20 | expect(g.el('status').textContent).toContain(`swing at ${Math.round(meter * 100)}%: sweet spot`); | |
| 21 | g.run(2); | |
| 22 | expect(g.el('status').textContent).toContain(g.read('P.line')); | |
| 23 | expect(g.el('bout').textContent).toBe('1 of 3'); | |
| 24 | expect(g.el('name').textContent).toBe('Smudge'); | |
| 25 | }); | |
| 26 | ||
| 27 | test('a swing in the tangle zone does nothing and the rival swings next', () => { | |
| 28 | const g = boot('conkers', { seed: 3 }); | |
| 29 | swingAt(g, .88, 1); | |
| 30 | expect(g.read('P.opponentHp')).toBe(6); | |
| 31 | expect(g.el('status').textContent).toContain('tangled'); | |
| 32 | g.run(2); | |
| 33 | expect(g.read('P.swings')).toBe(2); // the rival's swing came after | |
| 34 | expect(g.read('P.turn')).toBe('player'); | |
| 35 | }); | |
| 36 | ||
| 37 | test('a tap while a swing is playing is ignored', () => { | |
| 38 | const g = boot('conkers', { seed: 3 }); | |
| 39 | swingAt(g, .1, .3); | |
| 40 | g.tap(...MID); | |
| 41 | expect(g.read('P.swings')).toBe(1); | |
| 42 | }); | |
| 43 | ||
| 44 | test('Space swings and the meter holds still while the rival swings', () => { | |
| 45 | const g = boot('conkers', { seed: 3 }); | |
| 46 | g.run(.5); | |
| 47 | const before = g.read('P.swings'); | |
| 48 | g.press(' '); | |
| 49 | expect(g.read('P.swings')).toBe(before + 1); | |
| 50 | const m = g.read('G.meter'); | |
| 51 | g.run(.2); | |
| 52 | expect(g.read('G.meter')).toBe(m); | |
| 53 | }); | |
| 54 | ||
| 55 | test('the meter sweeps up and back and stays in range', () => { | |
| 56 | const g = boot('conkers', { seed: 3 }); | |
| 57 | const seen = []; | |
| 58 | for (let i = 0; i < 60; i++) { g.run(.04); seen.push(g.read('G.meter')); } | |
| 59 | expect(Math.min(...seen)).toBeGreaterThanOrEqual(0); | |
| 60 | expect(Math.max(...seen)).toBeLessThanOrEqual(1); | |
| 61 | expect(Math.max(...seen)).toBeGreaterThan(.9); | |
| 62 | expect(seen.some((v, i) => i && v < seen[i - 1])).toBe(true); | |
| 63 | }); | |
| 64 | ||
| 65 | test('the sweet spot is marked by a notch of ink ticks, not only by its tint', () => { | |
| 66 | const g = boot('conkers', { seed: 3 }); | |
| 67 | g.run(.1); | |
| 68 | const x = g.read('Math.round(BAR.x0 + (BAR.x1 - BAR.x0) * M.SWEET_LOW)'), y = g.read('BAR.y0 - 3'); | |
| 69 | expect(g.read(`image.data[(${y} * W + ${x}) * 4]`)).toBe(28); // the ink of INK_PIX's red channel | |
| 70 | }); | |
| 71 | ||
| 72 | test('your conker differs from the rival\'s by a knot and a chalk cross as well as colour', () => { | |
| 73 | const g = boot('conkers', { seed: 3 }); | |
| 74 | expect(g.read('FACE.length')).toBeGreaterThan(5); | |
| 75 | expect(g.read('MINE.join()')).not.toBe(g.read('THEIRS[0].join()')); | |
| 76 | expect(g.read('drawConker.toString()')).toContain('mine ? .3 : .17'); | |
| 77 | }); | |
| 78 | ||
| 79 | test('a full match to the last rival reaches the win line and a tap plays again', () => { | |
| 80 | const g = boot('conkers', { seed: 1 }); | |
| 81 | for (let i = 0; i < 2000 && g.read('P.status') === 'playing'; i++) { | |
| 82 | const m = g.read('G.meter'); | |
| 83 | if (!g.read('G.anim') && m >= .44 && m <= .56) g.tap(...MID); | |
| 84 | g.run(.04); | |
| 85 | } | |
| 86 | g.run(1); | |
| 87 | expect(g.el('status').textContent).toContain('All 3 rivals cracked, you win'); | |
| 88 | g.tap(...MID); | |
| 89 | expect(g.read('P.status')).toBe('playing'); | |
| 90 | expect(g.read('P.score')).toBe(0); | |
| 91 | }); | |
| 92 | ||
| 93 | test('mute toggles its label and aria-pressed', () => { | |
| 94 | const g = boot('conkers', { seed: 3 }); | |
| 95 | g.el('mute').fire('click'); | |
| 96 | expect(g.el('mute').textContent).toBe('sound off'); | |
| 97 | expect(g.el('mute').attrs['aria-pressed']).toBe('true'); | |
| 98 | }); | |
| 99 | ||
| 100 | test('the page source says how to play, keeps the 44px mute target, stills the sway, holds the rules to four lines and names no .mp3', async () => { | |
| 101 | const html = await Bun.file(new URL('./index.html', import.meta.url)).text(); | |
| 102 | expect(html).toMatch(/<canvas id="c"[^>]*aria-label="[^"]*Tap the canvas, or press Space[^"]*sweet spot/); | |
| 103 | expect(html).toMatch(/min-height: 44px; min-width: 44px/); | |
| 104 | expect(html).toMatch(/id="mute"[^>]*aria-pressed="false"/); | |
| 105 | expect(html).toContain('if (!STILL) theta'); | |
| 106 | expect(html).not.toContain('.mp3'); | |
| 107 | const rules = html.match(/rules: \[([\s\S]*?)\]/)[1].split('\n').filter(l => l.trim()); | |
| 108 | expect(rules.length).toBeLessThanOrEqual(4); | |
| 109 | }); |
index.html +1
| @@ -126,6 +126,7 @@ footer a::after { content: none; } | ||
| 126 | 126 | <li data-kind="Puzzle" data-input="Tap" style="--accent:#3d6f8f"><a href="mill-race/">Mill Race</a><span>Turn the channels until the stream reaches the mill wheel</span><span>Game forged by the crew</span><span class="tags">Puzzle · Tap</span></li> |
| 127 | 127 | <li data-kind="Catching" data-input="Tap" style="--accent:#3a8fa3"><a href="rock-pool/">Rock Pool</a><span>Scoop the darting crabs into your bucket before the tide comes back</span><span>Game forged by the crew</span><span class="tags">Catching · Tap</span></li> |
| 128 | 128 | <li data-kind="Puzzle" data-input="Tap" style="--accent:#5a6fb8"><a href="stained-glass/">Stained Glass</a><span>Colour the window so no two neighbouring panes match</span><span>Game forged by the crew</span><span class="tags">Puzzle · Tap</span></li> |
| 129 | <li data-kind="Sport" data-input="Tap" style="--accent:#8a4b22"><a href="conkers/">Conkers</a><span>Time your swing to crack every rival conker in the playground</span><span>Game forged by the crew</span><span class="tags">Sport · Tap</span></li> | |
| 129 | 130 | </ul> |
| 130 | 131 | <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.<br><a href="stats/">Most played</a></footer> |
| 131 | 132 | </main> |
og/conkers.png added
Binary file not shown.
og/home.png
Binary file not shown.
rounds.mjs +6
| @@ -205,6 +205,12 @@ export const ROUNDS = { | ||
| 205 | 205 | if (g.read('G.colour') !== colour) g.tap(...g.read(`swatchPoint(${colour})`)); |
| 206 | 206 | else g.tap(...g.read(`panePoint(${pane})`)); |
| 207 | 207 | } }, |
| 208 | // wait for the meter to sweep into the sweet spot, then swing: the rival's swings come from the seed | |
| 209 | 'conkers': { over: /play again/, outcome: /All 3 rivals cracked, you win: \d+ points/, beat: g => { | |
| 210 | const meter = g.read('G.meter'); | |
| 211 | if (g.read('P.status') === 'playing' && !g.read('G.anim') && meter >= .44 && meter <= .56) g.tap(...MID); | |
| 212 | g.run(.04); | |
| 213 | } }, | |
| 208 | 214 | // hop along the route the rules planned for this pond, one arrow key per pad |
| 209 | 215 | 'lily-pond': { over: /play again/, outcome: /Pond cleared: 6 levels, \d+ retries?/, beat: g => { |
| 210 | 216 | const [a, b] = g.read('[P.route[P.hops], P.route[P.hops + 1]]'); |
serve.test.mjs +7
| @@ -684,3 +684,10 @@ test('Stained Glass and its rules script are served without a track', async () = | ||
| 684 | 684 | expect(page.headers.get('content-type')).toBe('text/html; charset=utf-8'); |
| 685 | 685 | expect((await fetch(base + '/stained-glass/window.js')).status).toBe(200); |
| 686 | 686 | }); |
| 687 | ||
| 688 | test('Conkers and its rules script are served without a track', async () => { | |
| 689 | const page = await fetch(base + '/conkers/'); | |
| 690 | expect(page.status).toBe(200); | |
| 691 | expect(page.headers.get('content-type')).toBe('text/html; charset=utf-8'); | |
| 692 | expect((await fetch(base + '/conkers/match.js')).status).toBe(200); | |
| 693 | }); |
shell.js +1
| @@ -66,6 +66,7 @@ window.PixelPlays = (() => { | ||
| 66 | 66 | { id: 'mill-race', title: 'Mill Race', blurb: 'Turn the channels until the stream reaches the mill wheel', kind: 'Puzzle', input: 'Tap', accent: '#3d6f8f', music: false, forged: true }, |
| 67 | 67 | { id: 'rock-pool', title: 'Rock Pool', blurb: 'Scoop the darting crabs into your bucket before the tide comes back', kind: 'Catching', input: 'Tap', accent: '#3a8fa3', music: false, forged: true }, |
| 68 | 68 | { id: 'stained-glass', title: 'Stained Glass', blurb: 'Colour the window so no two neighbouring panes match', kind: 'Puzzle', input: 'Tap', accent: '#5a6fb8', music: false, forged: true }, |
| 69 | { id: 'conkers', title: 'Conkers', blurb: 'Time your swing to crack every rival conker in the playground', kind: 'Sport', input: 'Tap', accent: '#8a4b22', music: false, forged: true }, | |
| 69 | 70 | ]; |
| 70 | 71 | const MUSIC_VOLUME = .4; |
| 71 | 72 | const CREDITS = [ |
new above
offline