Public: anyone can read this; the crew can change it.
Migrated from github.com/amirmushichge/pixel-plays
Add Goalkeeper: dive left or right to save ten penalties in a village football ground #75
Check · Review · Land · Live done — Landed and live in production.
quill/pixel-plays:quill/65-add-goalkeeper-dive-left-or-right-to-sav → main 2 commits fork
10 files
changed +770
−1
README.md +2 −1
| @@ -36,6 +36,7 @@ Concept by [abhi](https://x.com/awwbhi2). Game by [AmirMushich](https://x.com/Am | ||
| 36 | 36 | | [](ice-fishing/) | A 60-second round on a frozen lake: hold to let line out through the hole and watch the hook sink in the cut-away, a perch (1), trout (2) or pike (3) crossing the hook at its depth bites, then hold to reel and ease off to let the tension fall; full tension snaps the line and three snaps end the round (forged by the crew on the engine and shell above, not by the original creators) | Hold the canvas; hold space | |
| 37 | 37 | | [](archery/) | Ten arrows at a straw target across a meadow: drag back from the archer to draw (the drag length is power, its angle is aim), release to shoot; the wind changes every arrow and shows on the pennant and the grass, holding past three seconds makes the aim sway wider, and rings score 10 (gold), 8, 6, 4 and 2 (forged by the crew on the engine and shell above, not by the original creators) | Drag back, release; arrows + hold space | |
| 38 | 38 | | [](tram-line/) | A 120-second timetable over eight stops: hold to power, release to coast, brake so the front door lands on the yellow mark; within 0.5 m scores 3, within 2 m scores 1, overshooting the shelter scores 0 and strands the passengers, stopping between stops costs 1, and each second left at the last stop adds 1 (forged by the crew on the engine and shell above, not by the original creators) | Hold up or W (right half) to power, down or S (left half) to brake | |
| 39 | | [](goalkeeper/) | Ten penalties in a village football ground: each taker runs up and their hips and plant foot lean toward the side they will shoot (arms spread wide is down the middle), readable for a short tell window before the kick that shrinks with every penalty; tap the left third of the canvas to dive left, the right third to dive right and the middle third to stay up, and a dive to the right side before the ball arrives saves it, while diving before the tell window opens lets the taker switch side half the time; seven saves wins; the concept is the ordinary football penalty, with no particular game as its source (forged by the crew on the engine and shell above, not by the original creators) | Tap left, middle or right; Left and Right arrows dive, Up stays | | |
| 39 | 40 | |
| 40 | 41 | ## Run |
| 41 | 42 | Requires [](https://nodejs.org/); nothing to install. |
| @@ -55,7 +56,7 @@ On the forge, `forge.json` runs `bun serve.mjs` with health check `/`. State is | ||
| 55 | 56 | Every page carries Open Graph and Twitter tags (between `og:begin` and `og:end`), and `og/<id>.png` is a 1200x630 card: the game's own opening frame, scaled 2x on white; `og/home.png` is a grid of all of them. Because the games are static, `bun scripts/og.mjs` makes them without a browser: it runs each page's scripts in a `vm` against a stub DOM, captures the canvas pixels and writes the PNG (zlib only, no dependency) and the tags. Run it after adding a game or changing a scene; `serve.test.mjs` fails when a card or its tags are missing or stale. Scenes are rendered with a seeded `Math.random`, so the cards are byte-for-byte reproducible. Absolute URLs use `ORIGIN` at the top of the script (the forge production site), so change it there if the site moves. |
| 56 | 57 | |
| 57 | 58 | ## Add a game |
| 58 | 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 as a card with its `kind`, `input` and `accent`, searchable and filterable by kind (a static fallback list for no-JS is kept in sync by `serve.test.mjs`, so add the matching `<li>` to `index.html` too, with the same `data-kind`, `data-input`, `--accent` and tags line). Then run `bun scripts/og.mjs` to make its share card and tags. The page needs a `#mute` button. Put its background track at `<id>/<id>.mp3`, or set `music: false` on its `GAMES` entry when it has none (Stone Skip, Kite Hill, Paper Route, Harbour Crane, Pizza Toss, Sled Run, Pottery Wheel, Row Home, Orchard, Lighthouse, Market Scale, Window Washer, Domino Run, Campfire, Beekeeper, Bread Oven, Ice Fishing, Tightrope, Tram Line, Archery and Bell Tower 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`. | |
| 59 | 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 as a card with its `kind`, `input` and `accent`, searchable and filterable by kind (a static fallback list for no-JS is kept in sync by `serve.test.mjs`, so add the matching `<li>` to `index.html` too, with the same `data-kind`, `data-input`, `--accent` and tags line). Then run `bun scripts/og.mjs` to make its share card and tags. The page needs a `#mute` button. Put its background track at `<id>/<id>.mp3`, or set `music: false` on its `GAMES` entry when it has none (Stone Skip, Kite Hill, Paper Route, Harbour Crane, Pizza Toss, Sled Run, Pottery Wheel, Row Home, Orchard, Lighthouse, Market Scale, Window Washer, Domino Run, Campfire, Beekeeper, Bread Oven, Ice Fishing, Tightrope, Tram Line, Archery, Bell Tower and Goalkeeper 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`. | |
| 59 | 60 | |
| 60 | 61 | - `shell.js` provides the game menu, the Rules popup, the credits and the background music. |
| 61 | 62 | - `engine.js` provides the camera, depth buffer, colour ramps, shadows, edges, figures, trees and wind as globals. |
goalkeeper/index.html added +445
| @@ -0,0 +1,445 @@ | ||
| 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>Goalkeeper</title> | |
| 7 | <!-- og:begin --> | |
| 8 | <meta name="description" content="Read the taker and dive left or right to save ten penalties"> | |
| 9 | <link rel="canonical" href="https://pixel-plays--forge-trevato-apps.open-platform.sh/goalkeeper/"> | |
| 10 | <meta property="og:type" content="website"> | |
| 11 | <meta property="og:site_name" content="Pixel Plays"> | |
| 12 | <meta property="og:title" content="Goalkeeper"> | |
| 13 | <meta property="og:description" content="Read the taker and dive left or right to save ten penalties"> | |
| 14 | <meta property="og:url" content="https://pixel-plays--forge-trevato-apps.open-platform.sh/goalkeeper/"> | |
| 15 | <meta property="og:image" content="https://pixel-plays--forge-trevato-apps.open-platform.sh/og/goalkeeper.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="Goalkeeper: the opening scene of the game"> | |
| 20 | <meta name="twitter:card" content="summary_large_image"> | |
| 21 | <!-- og:end --> | |
| 22 | <style> | |
| 23 | :root { : #1c1b19; : #8b877f; : #fff; : #3f7a3a; } | |
| 24 | * { box-sizing: border-box; } | |
| 25 | html, body { height: 100%; margin: 0; background: var(); color: var(); } | |
| 26 | body { font: 13px/1.35 ui-monospace, "Cascadia Mono", Consolas, Menlo, monospace; overflow: hidden; } | |
| 27 | #stage { position: fixed; inset: 0; display: grid; place-items: center; } | |
| 28 | canvas { display: block; image-rendering: pixelated; touch-action: none; cursor: pointer; outline: none; user-select: none; -webkit-user-select: none; } | |
| 29 | .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; } | |
| 30 | #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; } | |
| 31 | #scores b { font-size: 16px; font-weight: 600; font-variant-numeric: tabular-nums; min-width: 5ch; } | |
| 32 | #scores span { color: var(); font-variant-numeric: tabular-nums; min-height: 1.35em; } | |
| 33 | #bottom { bottom: max(14px, env(safe-area-inset-bottom)); align-items: flex-end; } | |
| 34 | #notes { display: grid; gap: 2px; } | |
| 35 | #notes span { color: var(); min-height: 1.35em; } | |
| 36 | button { pointer-events: auto; font: inherit; color: var(); background: none; border: 0; padding: 10px; margin: -10px; cursor: pointer; } | |
| 37 | button:hover, button:focus-visible, button[aria-pressed="true"] { color: var(); } | |
| 38 | </style> | |
| 39 | </head> | |
| 40 | <body> | |
| 41 | <div id="stage"><canvas id="c" tabindex="0" aria-label="Goalkeeper. You are in goal for ten penalties. Watch the taker run up: their hips and plant foot lean toward the side they will shoot, arms spread wide means down the middle, and it shows only for a short moment before the kick. Tap or click the left third of the picture to dive left, the right third to dive right, the middle third to stay up; or press the Left and Right arrow keys to dive and Up to stay. Diving early lets the taker switch side. Seven saves wins."></canvas></div> | |
| 42 | <div id="scores"> | |
| 43 | <b id="score">0 saves</b><span id="meta"></span> | |
| 44 | </div> | |
| 45 | <div class="hud" id="bottom"> | |
| 46 | <div id="notes"><span id="status" role="status" aria-live="polite"></span></div> | |
| 47 | <button id="mute" type="button" aria-pressed="false">sound on</button> | |
| 48 | </div> | |
| 49 | <script src="../shell.js"></script> | |
| 50 | <script> | |
| 51 | PixelPlays.mount({ | |
| 52 | id: 'goalkeeper', | |
| 53 | rules: [ | |
| 54 | 'Ten penalties. Tap the left third of the picture to dive left, the right third to dive right, the middle third to stay up. Keyboard: Left and Right arrows dive, Up stays.', | |
| 55 | 'The taker leans toward the side they will shoot, for a moment before the kick: tilt is a side, arms spread wide is the middle.', | |
| 56 | 'Dive the right way before the ball arrives to save. Too early, and the taker may switch sides. Seven saves wins.', | |
| 57 | ], | |
| 58 | }); | |
| 59 | </script> | |
| 60 | <script>window.PP_SCENE = { originY: -30, shadowX: 1.3, shadowY: -.12 };</script> | |
| 61 | <script src="../engine.js"></script> | |
| 62 | <script src="shootout.js"></script> | |
| 63 | <script> | |
| 64 | 'use strict'; | |
| 65 | ||
| 66 | const K = window.Goalkeeper; | |
| 67 | const BEST_KEY = 'pixel-plays-goalkeeper-best'; | |
| 68 | const GW = 5.2, GH = 3, DEPTH = 2.4; // the goal: half-width, crossbar height, depth of the net behind the line | |
| 69 | const SPOT_B = 6.6, START_B = 11.6, PLANT_B = 7.4; // the penalty spot, where the taker starts and where they plant | |
| 70 | const SHOT_A = 3.7, LOW = .55, HIGH = 2.25; // where the ball meets the line: across for left or right, and the two heights | |
| 71 | const HALF_A = 16.5, HALF_B = 10.2, CENTRE_B = 5.6; // the pitch block | |
| 72 | const WALL_B = -4.4; | |
| 73 | const FIG = 1.2; // the people are drawn a fifth larger than the goal would allow, so the tell reads at 360 by 280 | |
| 74 | const DIVE_TIME = .2, TELL_EASE = .16; | |
| 75 | const PITCH = [[40, 74, 50], [52, 88, 52], [70, 106, 54], [88, 122, 56]]; | |
| 76 | const MOWN = [[60, 100, 56], [74, 118, 60], [92, 136, 64], [108, 150, 68]]; | |
| 77 | const CHALK = [238, 238, 228], POST = [244, 242, 232], NET = rgb(206, 212, 204), NET_FAR = rgb(176, 186, 176); | |
| 78 | const STONE = [[132, 124, 112], [156, 146, 130], [112, 106, 98], [170, 160, 142]]; | |
| 79 | const KEEPER = { shirt: [236, 196, 60], legs: [44, 50, 60], skin: [222, 170, 132], hair: [74, 50, 34], glove: [248, 246, 238] }; | |
| 80 | const TAKERS = [[200, 62, 52], [64, 118, 190], [236, 236, 226], [150, 70, 160], [232, 140, 50]]; | |
| 81 | const SHORTS = [[236, 232, 222], [236, 232, 222], [60, 66, 120], [236, 232, 222], [56, 60, 70]]; | |
| 82 | const SKIN = [[222, 170, 132], [176, 124, 92], [236, 190, 150], [150, 100, 72], [222, 170, 132]]; | |
| 83 | const HAIR = [[74, 50, 34], [36, 30, 28], [196, 150, 70], [60, 40, 30], [30, 28, 30]]; | |
| 84 | const BALL = [248, 246, 238]; | |
| 85 | // local pitch space: a runs across the goal (screen right is +), b away from the goal toward the camera, z up; the world axes sit 45 degrees off | |
| 86 | const toWorld = (a, b, z) => [(a + b) / R2, (b - a) / R2, z]; | |
| 87 | const toLocal = (x, y) => ({ a: (x - y) / R2, b: (x + y) / R2 }); | |
| 88 | const mix = (p, q, t) => [lerp(p[0], q[0], t), lerp(p[1], q[1], t), lerp(p[2], q[2], t)]; | |
| 89 | ||
| 90 | /* ---------- the pitch ---------- */ | |
| 91 | function chalkAt(a, b) { | |
| 92 | const w = .13, A = Math.abs(a); | |
| 93 | if (Math.abs(b) < w && A <= 7.4) return true; // goal line | |
| 94 | if (Math.abs(b - 3) < w && A <= 7.4) return true; // six-yard box | |
| 95 | if (Math.abs(A - 7.4) < w && b >= 0 && b <= 3) return true; | |
| 96 | if (Math.abs(b - 9) < w && A <= 11.1) return true; // the penalty area | |
| 97 | if (Math.abs(A - 11) < w && b >= 0 && b <= 9) return true; | |
| 98 | const d = Math.hypot(a, b - SPOT_B); | |
| 99 | if (Math.abs(d - 3.6) < w * 1.1 && b > 9) return true; // the arc | |
| 100 | return d < .24; // the spot | |
| 101 | } | |
| 102 | function groundColor(x, y, ix, iy) { | |
| 103 | const { a, b } = toLocal(x, y); | |
| 104 | const edge = Math.pow(Math.pow(Math.abs(a) / HALF_A, 4) + Math.pow(Math.abs(b - CENTRE_B) / HALF_B, 4), .25); | |
| 105 | const rough = edge + (noise(a * 1.3, b * 1.3) - .5) * .1; | |
| 106 | if (rough > 1.04) return 0; | |
| 107 | if (rough > .94 && hash(ix, iy) < (rough - .94) / .1) return 0; | |
| 108 | if (chalkAt(a, b) && rough < .96) return rgb(...CHALK); | |
| 109 | const band = Math.floor((b + 30) / 2.4) % 2, fine = hash(ix, iy); | |
| 110 | const t = noise(a * .9, b * .9) * .6 + (fine - .5) * .3 + band * .28 + (bayerAt(ix, iy) - .5) * .25; | |
| 111 | const ramp = band ? MOWN : PITCH; | |
| 112 | return rgb(...ramp[clamp(Math.floor(t * 3.4), 0, 3)]); | |
| 113 | } | |
| 114 | function bakeGoal() { | |
| 115 | const post = () => POST; | |
| 116 | for (const s of [-1, 1]) { | |
| 117 | capsule(toWorld(s * GW, 0, 0), toWorld(s * GW, 0, GH), .11, .11, post, 'solid'); | |
| 118 | capsule(toWorld(s * GW, 0, GH), toWorld(s * GW, -DEPTH, GH * .8), .06, .06, post, 'solid'); | |
| 119 | capsule(toWorld(s * GW, -DEPTH, GH * .8), toWorld(s * GW, -DEPTH, 0), .06, .06, post, 'solid'); | |
| 120 | capsule(toWorld(s * GW, -DEPTH, 0), toWorld(s * GW, 0, 0), .05, .05, post, 'solid'); | |
| 121 | } | |
| 122 | capsule(toWorld(-GW, 0, GH), toWorld(GW, 0, GH), .11, .11, post, 'solid'); | |
| 123 | capsule(toWorld(-GW, -DEPTH, GH * .8), toWorld(GW, -DEPTH, GH * .8), .06, .06, post, 'solid'); | |
| 124 | for (let k = 0; k <= 30; k++) { | |
| 125 | for (const s of [-1, 1]) castShadow(...toWorld(s * GW, 0, GH * k / 30), .12, 1, 0); | |
| 126 | castShadow(...toWorld(-GW + 2 * GW * k / 30, 0, GH), .12, 1, 0); | |
| 127 | } | |
| 128 | } | |
| 129 | function bakeWall() { | |
| 130 | // a low dry-stone wall behind the goal: courses of rough stones, each course offset by half a stone | |
| 131 | for (let row = 0; row < 3; row++) { | |
| 132 | for (let a = -17 + (row % 2) * .35; a <= 17; a += .7) { | |
| 133 | const jitter = hash(Math.round(a * 10), row * 7); | |
| 134 | sphere(...toWorld(a, WALL_B + (jitter - .5) * .15, .3 + row * .42), .34 + jitter * .06, STONE[Math.floor(jitter * 3.99)], 'rough'); | |
| 135 | } | |
| 136 | } | |
| 137 | for (let a = -17; a <= 17; a += .35) { castShadow(...toWorld(a, WALL_B, 1.2), .3, 1, .2); castShadow(...toWorld(a, WALL_B, .6), .3, 1, .2); } | |
| 138 | } | |
| 139 | const POLE = { a: 11.5, b: WALL_B - .2, top: 3.4 }; | |
| 140 | function bakePole() { | |
| 141 | capsule(toWorld(POLE.a, POLE.b, 0), toWorld(POLE.a, POLE.b, POLE.top), .08, .06, () => [128, 94, 62], 'solid'); | |
| 142 | sphere(...toWorld(POLE.a, POLE.b, POLE.top + .1), .12, [226, 206, 140], 'solid'); | |
| 143 | for (let k = 0; k <= 24; k++) castShadow(...toWorld(POLE.a, POLE.b, POLE.top * k / 24), .09, 1, 0); | |
| 144 | } | |
| 145 | function bake() { | |
| 146 | ground.fill(0); shadowMask.fill(0); staticShadow.fill(0); kinds.fill(0); | |
| 147 | zb.fill(-1e9); | |
| 148 | pix.fill(rgb(255, 255, 255)); | |
| 149 | for (let iy = 0; iy < H; iy++) for (let ix = 0; ix < W; ix++) { | |
| 150 | const g = unproject(ix + .5, iy + .5), i = iy * W + ix, c = groundColor(g.x, g.y, ix, iy); | |
| 151 | if (!c) continue; | |
| 152 | pix[i] = c; ground[i] = 1; | |
| 153 | zb[i] = (g.x + g.y) / R2 * COS; | |
| 154 | } | |
| 155 | groundZ.set(zb); | |
| 156 | bakeWall(); | |
| 157 | bakePole(); | |
| 158 | bakeGoal(); | |
| 159 | bakeOcclusion(); | |
| 160 | staticShadow.set(shadowMask); | |
| 161 | applyShadows(groundZ); | |
| 162 | bakedPix.set(pix); | |
| 163 | bakedZ.set(zb); | |
| 164 | } | |
| 165 | ||
| 166 | /* ---------- state ---------- */ | |
| 167 | const best0 = (() => { try { return Number(localStorage.getItem(BEST_KEY)) || 0; } catch (e) { return 0; } })(); | |
| 168 | const G = K.newRound(best0); | |
| 169 | let time = 0, status = '', overFor = 0, lastOver = false, arrivedAt = -9, flash = null; | |
| 170 | const nodes = { score: document.getElementById('score'), meta: document.getElementById('meta'), status: document.getElementById('status') }; | |
| 171 | ||
| 172 | /* ---------- people ---------- */ | |
| 173 | const add = (a, b, r0, r1, color) => dynamicAdd(a, b, r0, r1, () => color); | |
| 174 | // one figure in pitch space. o: roll (radians, + tilts toward screen right), shift, lift, crouch, stride (-1..1), hands [[la, z], [la, z]] (left, right on screen), | |
| 175 | // kick (0..1, the right foot swings through), plant (sideways offset of the left foot), pal, face (+1 toward the camera, -1 away) | |
| 176 | function person(base, o) { | |
| 177 | const bone = (p, q, r0, r1, c) => add(p, q, r0 * FIG, r1 * FIG, c); | |
| 178 | const pal = o.pal, roll = o.roll || 0, cs = Math.cos(roll), sn = Math.sin(roll), z0 = .1, crouch = o.crouch || 0, face = o.face; | |
| 179 | const at = (la, lb, z) => { | |
| 180 | const dz = z - z0; | |
| 181 | return toWorld(base.a + (o.shift || 0) + (la * cs + dz * sn) * FIG, base.b + lb * FIG, Math.max(.12, (z0 + dz * cs - la * sn) * FIG) + (o.lift || 0)); | |
| 182 | }; | |
| 183 | const hipZ = 1.3 - crouch * .5, stride = o.stride || 0, kick = o.kick || 0, plant = o.plant || 0; | |
| 184 | const hipL = at(-.17, 0, hipZ), hipR = at(.17, 0, hipZ); | |
| 185 | const footL = at(-.2 + plant, stride * .55 * face, .12 + Math.max(0, -stride) * .25); | |
| 186 | const footR = kick ? at(.2, -1.3 * kick * face, .12 + kick * .5) : at(.2, -stride * .55 * face, .12 + Math.max(0, stride) * .25); | |
| 187 | const kneeL = mix(hipL, footL, .5), kneeR = mix(hipR, footR, .5); | |
| 188 | kneeL[1] += .12 * face; kneeR[1] += .12 * face; | |
| 189 | bone(hipL, kneeL, .17, .13, pal.legs); bone(kneeL, footL, .13, .1, pal.legs); | |
| 190 | bone(hipR, kneeR, .17, .13, pal.legs); bone(kneeR, footR, .13, .1, pal.legs); | |
| 191 | bone(footL, [footL[0] + .12 * face * .7, footL[1] + .12 * face * .7, footL[2] - .03], .1, .09, [226, 222, 212]); | |
| 192 | bone(footR, [footR[0] + .12 * face * .7, footR[1] + .12 * face * .7, footR[2] - .03], .1, .09, [226, 222, 212]); | |
| 193 | const mid = at(0, 0, hipZ), neck = at(0, 0, 2.2 - crouch * .35), head = at(0, 0, 2.58 - crouch * .4); | |
| 194 | bone(mid, neck, .3, .32, pal.shirt); | |
| 195 | bone(neck, head, .1, .11, pal.skin); | |
| 196 | bone(head, head, .25, .25, pal.skin); | |
| 197 | const back = [head[0] - face * .06, head[1] - face * .06, head[2] + (face > 0 ? .13 : .07)]; | |
| 198 | if (face < 0) { back[0] += .13; back[1] += .13; } // seen from behind, the hair covers the head | |
| 199 | bone(back, back, .22, .22, pal.hair); | |
| 200 | const shZ = 2.1 - crouch * .35; | |
| 201 | o.hands.forEach(([la, z], k) => { | |
| 202 | const side = k ? 1 : -1, sh = at(side * .38, 0, shZ), hand = at(la, 0, z); | |
| 203 | const elbow = mix(sh, hand, .5); | |
| 204 | bone(sh, elbow, .12, .1, pal.shirt); | |
| 205 | bone(elbow, hand, .09, .08, pal.glove || pal.skin); | |
| 206 | }); | |
| 207 | } | |
| 208 | ||
| 209 | /* ---------- the match ---------- */ | |
| 210 | const easeOut = t => 1 - (1 - t) * (1 - t); | |
| 211 | function finalSide() { return K.finalShot(G.seed, G.n, G.dive); } | |
| 212 | function drawKeeper() { | |
| 213 | const pal = KEEPER, base = { a: 0, b: .35 }; | |
| 214 | let o = { pal, face: 1, crouch: .35, hands: [[-.85, 1.45], [.85, 1.45]], stride: .1 }; | |
| 215 | if (G.dive) { | |
| 216 | const shot = finalSide(), p = easeOut(clamp((G.t - G.dive.t) / DIVE_TIME, 0, 1)), s = G.dive.side, saved = G.last && G.last.n === G.n && G.last.saved; | |
| 217 | const high = shot.height === 1; | |
| 218 | if (s === 0) { | |
| 219 | o = { pal, face: 1, crouch: lerp(.35, high ? 0 : .7, p), lift: p * (high ? .45 : 0), stride: .1, hands: high ? [[-.5, lerp(1.45, 3.1, p)], [.5, lerp(1.45, 3.1, p)]] : [[-.7, lerp(1.45, .7, p)], [.7, lerp(1.45, .7, p)]] }; | |
| 220 | } else { | |
| 221 | const reach = saved ? shotA(shot) - s * 2.9 : s * 1.0; | |
| 222 | o = { | |
| 223 | pal, face: 1, roll: s * 1.35 * p, shift: reach * p, lift: p * ((saved ? high : shot.height === 1) ? 1.15 : .2), crouch: .2 * (1 - p), stride: 0, | |
| 224 | hands: [[-.5 + s * .15, lerp(1.45, 3.05, p)], [.5 + s * .15, lerp(1.45, 3.05, p)]], | |
| 225 | }; | |
| 226 | } | |
| 227 | } | |
| 228 | person(base, o); | |
| 229 | } | |
| 230 | const shotA = shot => shot.side * SHOT_A; | |
| 231 | function takerPos() { | |
| 232 | const u = clamp(G.t / K.KICK, 0, 1); | |
| 233 | return { a: 0, b: lerp(START_B, PLANT_B, 1 - (1 - u) * (1 - u) * .6 - u * .4 * (1 - u)) }; | |
| 234 | } | |
| 235 | function drawTaker() { | |
| 236 | const n = (G.n - 1) % TAKERS.length, pal = { shirt: TAKERS[n], legs: SHORTS[n], skin: SKIN[n], hair: HAIR[n] }; | |
| 237 | const t = G.t, run = t < K.KICK, stride = run ? Math.sin(t * 11) * .9 : 0; | |
| 238 | const side = G.phase === 'runup' || G.phase === 'result' || G.phase === 'over' ? (t >= K.tellOpens(G.n) ? finalSide().side : null) : null; | |
| 239 | const r = side === null ? 0 : easeOut(clamp((t - K.tellOpens(G.n)) / TELL_EASE, 0, 1)); | |
| 240 | const kickK = clamp((t - (K.KICK - .16)) / .2, 0, 1); | |
| 241 | const pump = run ? Math.sin(t * 11) * .25 : 0; | |
| 242 | const o = { pal, face: -1, stride, kick: kickK, hands: [[-.6, 1.25 + pump], [.6, 1.25 - pump]] }; | |
| 243 | if (side === 0) { | |
| 244 | o.crouch = .4 * r; o.hands = [[lerp(-.6, -1.55, r), lerp(1.25, 2.0, r)], [lerp(.6, 1.55, r), lerp(1.25, 2.0, r)]]; | |
| 245 | } else if (side) { | |
| 246 | o.roll = side * .5 * r; o.shift = side * .3 * r; o.plant = side * .55 * r; | |
| 247 | const far = side > 0 ? 0 : 1; // the arm on the side away from the shot flings wide | |
| 248 | o.hands = o.hands.map((h, k) => k === far ? [lerp(h[0], -side * 1.5, r), lerp(h[1], 2.05, r)] : [lerp(h[0], side * .4, r), lerp(h[1], 1.15, r)]); | |
| 249 | } | |
| 250 | person(takerPos(), o); | |
| 251 | } | |
| 252 | // where the ball is, in pitch space | |
| 253 | function ballAt() { | |
| 254 | const tz = shot => shot.height ? HIGH : LOW, spot = [0, SPOT_B, .3]; | |
| 255 | if (G.t < K.KICK && !(G.phase !== 'runup' && G.last && G.last.n === G.n)) return spot; | |
| 256 | const shot = G.last && G.last.n === G.n ? G.last.shot : finalSide(), ta = shotA(shot), z = tz(shot); | |
| 257 | if (G.phase === 'runup' && G.t < K.ARRIVE) { | |
| 258 | const u = clamp((G.t - K.KICK) / K.FLIGHT, 0, 1); | |
| 259 | return [lerp(0, ta, u), lerp(SPOT_B, 0, u), lerp(.3, z, u) + Math.sin(Math.PI * u) * .25]; | |
| 260 | } | |
| 261 | const tau = Math.min(1.2, Math.max(0, time - arrivedAt)); | |
| 262 | if (G.last && G.last.saved) return [ta + shot.side * 1.6 * tau, 1.8 * tau, Math.max(.3, z + 2.2 * tau - 7 * tau * tau)]; | |
| 263 | return [ta, -Math.min(DEPTH - .5, tau * 14), Math.max(.3, z - 9 * tau * tau)]; | |
| 264 | } | |
| 265 | function drawBall() { | |
| 266 | const [a, b, z] = ballAt(), p = toWorld(a, b, z); | |
| 267 | sphere(p[0], p[1], p[2], .3, BALL, 'solid'); | |
| 268 | if (z > .4) castShadow(...toWorld(a, b, z), .3, 1, 0); | |
| 269 | else castShadow(...toWorld(a, b, .3), .3, 1, 0); | |
| 270 | } | |
| 271 | ||
| 272 | /* ---------- the net and the flag: the only things that move on their own ---------- */ | |
| 273 | function drawNet(t) { | |
| 274 | const sway = STILL ? 0 : 1; | |
| 275 | const bulge = G.last && !G.last.saved && G.last.n === G.n ? Math.min(1, (time - arrivedAt) * 6) * Math.max(0, 1 - (time - arrivedAt) * .7) : 0; | |
| 276 | const ball = G.last && !G.last.saved && G.last.n === G.n ? shotA(G.last.shot) : 0; | |
| 277 | const bz = G.last && G.last.shot.height ? HIGH : LOW; | |
| 278 | const dent = (a, z) => bulge * 1.1 * Math.exp(-((a - ball) * (a - ball) + (z - bz) * (z - bz)) / 2.2); | |
| 279 | const wob = (a, z, k) => sway * Math.sin(t * 1.7 + a * .9 + z * 1.3 + k) * .07; | |
| 280 | const dot = (a, b, z, c) => plot(...toWorld(a, b, z), c); | |
| 281 | const hh = GH * .8; | |
| 282 | // back panel, a grid every .4 | |
| 283 | for (let a = -GW; a <= GW + 1e-6; a += .4) for (let z = 0; z <= hh; z += .12) dot(a, -DEPTH - wob(a, z, 0) - dent(a, z), z, NET_FAR); | |
| 284 | for (let z = 0; z <= hh + 1e-6; z += .4) for (let a = -GW; a <= GW; a += .12) dot(a, -DEPTH - wob(a, z, 0) - dent(a, z), z, NET_FAR); | |
| 285 | // the roof, from the crossbar slanting back | |
| 286 | for (let a = -GW; a <= GW + 1e-6; a += .4) for (let k = 0; k <= 1; k += .08) dot(a, -DEPTH * k - wob(a, k * 3, 1), lerp(GH, hh, k), NET); | |
| 287 | for (let k = 0; k <= 1.001; k += .18) for (let a = -GW; a <= GW; a += .12) dot(a, -DEPTH * k - wob(a, k * 3, 1), lerp(GH, hh, k), NET); | |
| 288 | // the two sides | |
| 289 | for (const s of [-1, 1]) { | |
| 290 | for (let k = 0; k <= 1.001; k += .18) for (let z = 0; z <= lerp(GH, hh, k); z += .12) dot(s * GW, -DEPTH * k - wob(s * GW, z, 2), z, NET); | |
| 291 | for (let z = 0; z <= GH; z += .4) for (let k = 0; k <= 1; k += .05) dot(s * GW, -DEPTH * k - wob(s * GW, z, 2), z * lerp(1, .8, k), NET); | |
| 292 | } | |
| 293 | } | |
| 294 | const FLAG = [[200, 62, 52], [240, 232, 210]]; | |
| 295 | function drawFlag(t) { | |
| 296 | const len = 3.2, n = 26; | |
| 297 | for (let k = 0; k <= n; k++) { | |
| 298 | const s = k / n, wave = STILL ? 0 : Math.sin(t * 5 - s * 6) * .22 * s, drop = .1 * s; | |
| 299 | const a = POLE.a + s * len, b = POLE.b + wave, z = POLE.top - drop, half = .55 * (1 - s * .55); | |
| 300 | const color = rgb(...FLAG[Math.floor(s * 5) % 2]); | |
| 301 | for (let dz = -half; dz <= half; dz += .06) plot(...toWorld(a, b, z + dz), color); | |
| 302 | if (k % 3 === 0) castShadow(...toWorld(a, b, z), .2, 1, .2); | |
| 303 | } | |
| 304 | } | |
| 305 | ||
| 306 | /* ---------- hud ---------- */ | |
| 307 | let shown = ''; | |
| 308 | function renderHud() { | |
| 309 | const n = Math.min(K.PENALTIES, G.n); | |
| 310 | const text = [G.saves, G.best, n, G.phase].join('|'); | |
| 311 | if (text === shown) return; | |
| 312 | shown = text; | |
| 313 | nodes.score.textContent = G.saves + (G.saves === 1 ? ' save' : ' saves'); | |
| 314 | nodes.meta.textContent = 'penalty ' + n + '/' + K.PENALTIES + (G.best ? ' · best ' + G.best : ''); | |
| 315 | } | |
| 316 | function setStatus(text) { | |
| 317 | if (text === status) return; | |
| 318 | status = text; | |
| 319 | nodes.status.textContent = text; | |
| 320 | } | |
| 321 | const REASONS = { 'wrong side': 'goal · wrong way', 'too late': 'goal · too late', 'no dive': 'goal · you stood still' }; | |
| 322 | function resultText(out) { | |
| 323 | const note = out.shot.switched ? ' · you dived early, the taker switched' : ''; | |
| 324 | return (out.saved ? 'saved!' : REASONS[out.reason]) + note; | |
| 325 | } | |
| 326 | function statusFor() { | |
| 327 | if (G.phase === 'over') { | |
| 328 | const line = K.won(G) ? 'you win: ' + G.saves + ' saves, the village will talk of this' : 'you lose: ' + G.saves + ' saves, seven wins'; | |
| 329 | return line + (G.record ? ' · new best' : '') + ' · tap to play again'; | |
| 330 | } | |
| 331 | if (flash && G.phase === 'result') return flash; | |
| 332 | if (G.phase === 'runup' && G.dive) return 'dived'; | |
| 333 | return G.t >= K.tellOpens(G.n) && G.phase === 'runup' ? 'now' : 'read the taker · tap left, middle or right'; | |
| 334 | } | |
| 335 | ||
| 336 | /* ---------- loop ---------- */ | |
| 337 | function update(dt) { | |
| 338 | time += dt; | |
| 339 | const out = K.tick(G, dt); | |
| 340 | if (out) { | |
| 341 | arrivedAt = time; | |
| 342 | flash = resultText(out); | |
| 343 | sound(out.saved ? 'save' : 'goal'); | |
| 344 | } | |
| 345 | if (G.phase === 'over') { | |
| 346 | overFor += dt; | |
| 347 | if (!lastOver) { | |
| 348 | lastOver = true; | |
| 349 | if (G.record) try { localStorage.setItem(BEST_KEY, String(G.best)); } catch (e) { /* private window: keep it for this visit */ } | |
| 350 | } | |
| 351 | } | |
| 352 | setStatus(statusFor()); | |
| 353 | renderHud(); | |
| 354 | } | |
| 355 | function draw() { | |
| 356 | pix.set(bakedPix); | |
| 357 | zb.set(bakedZ); | |
| 358 | pendingShadows.length = 0; | |
| 359 | const t = STILL ? 0 : Math.floor(time * WIND_FPS) / WIND_FPS; | |
| 360 | drawKeeper(); | |
| 361 | drawTaker(); | |
| 362 | drawBall(); | |
| 363 | drawFlag(t); | |
| 364 | flushShadows(); | |
| 365 | applyShadows(bakedZ); | |
| 366 | shadeEdges(); | |
| 367 | drawNet(t); | |
| 368 | ctx.putImageData(image, 0, 0); | |
| 369 | } | |
| 370 | ||
| 371 | /* ---------- input ---------- */ | |
| 372 | function again() { | |
| 373 | if (G.phase === 'over' && overFor > .8) { K.restart(G); overFor = 0; lastOver = false; flash = null; return true; } | |
| 374 | return G.phase === 'over'; | |
| 375 | } | |
| 376 | function jump(side) { | |
| 377 | ensureAudio(); | |
| 378 | if (again()) return; | |
| 379 | if (K.dive(G, side)) sound('dive'); | |
| 380 | } | |
| 381 | canvas.addEventListener('pointerdown', e => { | |
| 382 | canvas.focus({ preventScroll: true }); | |
| 383 | const r = canvas.getBoundingClientRect(), u = (e.clientX - r.left) / r.width; | |
| 384 | jump(u < .36 ? -1 : u > .64 ? 1 : 0); | |
| 385 | }); | |
| 386 | canvas.addEventListener('contextmenu', e => e.preventDefault()); | |
| 387 | canvas.addEventListener('keydown', e => { | |
| 388 | const side = e.key === 'ArrowLeft' ? -1 : e.key === 'ArrowRight' ? 1 : e.key === 'ArrowUp' ? 0 : null; | |
| 389 | if (side === null) { | |
| 390 | if (e.key !== ' ' && e.key !== 'Enter') return; | |
| 391 | e.preventDefault(); | |
| 392 | if (!e.repeat) { ensureAudio(); again(); } | |
| 393 | return; | |
| 394 | } | |
| 395 | e.preventDefault(); | |
| 396 | if (!e.repeat) jump(side); | |
| 397 | }); | |
| 398 | ||
| 399 | /* ---------- sound ---------- */ | |
| 400 | let audio = null, muted = false; | |
| 401 | function ensureAudio() { | |
| 402 | if (audio) { if (audio.state === 'suspended') audio.resume(); return; } | |
| 403 | const AC = window.AudioContext || window.webkitAudioContext; | |
| 404 | if (AC) audio = new AC(); | |
| 405 | } | |
| 406 | function tone(type, from, to, len, vol) { | |
| 407 | const now = audio.currentTime, gain = audio.createGain(), osc = audio.createOscillator(); | |
| 408 | osc.type = type; | |
| 409 | osc.frequency.setValueAtTime(from, now); | |
| 410 | osc.frequency.exponentialRampToValueAtTime(to, now + len); | |
| 411 | gain.gain.setValueAtTime(vol, now); | |
| 412 | gain.gain.exponentialRampToValueAtTime(.0005, now + len); | |
| 413 | osc.connect(gain).connect(audio.destination); | |
| 414 | osc.start(now); osc.stop(now + len + .01); | |
| 415 | } | |
| 416 | function sound(kind) { | |
| 417 | if (!audio || muted) return; | |
| 418 | if (kind === 'dive') tone('triangle', 300, 120, .12, .07); | |
| 419 | else if (kind === 'save') { tone('square', 160, 80, .1, .08); tone('triangle', 520, 780, .22, .07); } | |
| 420 | else if (kind === 'goal') tone('sawtooth', 240, 70, .35, .05); | |
| 421 | } | |
| 422 | const muteButton = document.getElementById('mute'); | |
| 423 | muteButton.addEventListener('click', () => { | |
| 424 | muted = !muted; | |
| 425 | muteButton.textContent = muted ? 'sound off' : 'sound on'; | |
| 426 | muteButton.setAttribute('aria-pressed', String(muted)); | |
| 427 | }); | |
| 428 | ||
| 429 | /* ---------- boot ---------- */ | |
| 430 | addEventListener('resize', fit); | |
| 431 | fit(); | |
| 432 | bake(); | |
| 433 | setStatus(statusFor()); | |
| 434 | renderHud(); | |
| 435 | let last = performance.now(); | |
| 436 | function frame(now) { | |
| 437 | update(Math.min(.04, (now - last) / 1000)); | |
| 438 | last = now; | |
| 439 | draw(); | |
| 440 | requestAnimationFrame(frame); | |
| 441 | } | |
| 442 | requestAnimationFrame(frame); | |
| 443 | </script> | |
| 444 | </body> | |
| 445 | </html> |
goalkeeper/page.test.mjs added +104
| @@ -0,0 +1,104 @@ | ||
| 1 | import { test, expect, setDefaultTimeout } from 'bun:test'; | |
| 2 | import vm from 'node:vm'; | |
| 3 | import { boot } from '../playtest.mjs'; | |
| 4 | setDefaultTimeout(60000); | |
| 5 | ||
| 6 | const KEY = 'pixel-plays-goalkeeper-best'; | |
| 7 | const X = { '-1': 40, 0: 180, 1: 320 }; | |
| 8 | const read = (g, code) => vm.runInContext(code, g.window); | |
| 9 | // the side the current taker will shoot, once any early-dive switch is settled (the player here never dives early) | |
| 10 | const shotSide = g => read(g, 'K.pickShot(G.seed, G.n).side'); | |
| 11 | // wait for the tell window of the current penalty to open | |
| 12 | const waitForTell = g => { for (let i = 0; i < 100 && read(g, 'K.lean(G)') === null; i++) g.step(1); }; | |
| 13 | // play one penalty reading the tell, tapping the way the taker leans | |
| 14 | function save(g) { | |
| 15 | waitForTell(g); | |
| 16 | g.tap(X[read(g, 'K.lean(G)')], 140); | |
| 17 | decided(g); | |
| 18 | } | |
| 19 | // run on to the moment the penalty is decided, and on past the result to the next taker | |
| 20 | const decided = g => { for (let i = 0; i < 200 && read(g, 'G.phase') === 'runup'; i++) g.step(1); }; | |
| 21 | const nextTaker = g => { for (let i = 0; i < 200 && read(g, 'G.phase') === 'result'; i++) g.step(1); }; | |
| 22 | ||
| 23 | test('goalkeeper: tapping the side the taker leans to saves, and the HUD counts it', () => { | |
| 24 | const g = boot('goalkeeper'); | |
| 25 | expect(g.text()).toContain('penalty 1/10'); | |
| 26 | expect(g.text()).toContain('0 saves'); | |
| 27 | save(g); | |
| 28 | expect(g.errors.map(e => e.message)).toEqual([]); | |
| 29 | expect(g.text()).toContain('1 save'); | |
| 30 | expect(g.text()).toContain('saved'); | |
| 31 | }); | |
| 32 | ||
| 33 | test('goalkeeper: the wrong side concedes, and standing still concedes', () => { | |
| 34 | const g = boot('goalkeeper'); | |
| 35 | waitForTell(g); | |
| 36 | const wrong = shotSide(g) === 1 ? -1 : 1; | |
| 37 | g.tap(X[wrong], 140); | |
| 38 | decided(g); | |
| 39 | expect(g.text()).toContain('wrong way'); | |
| 40 | expect(g.text()).toContain('0 saves'); | |
| 41 | nextTaker(g); | |
| 42 | expect(g.text()).toContain('penalty 2/10'); | |
| 43 | decided(g); | |
| 44 | expect(g.text()).toContain('you stood still'); | |
| 45 | }); | |
| 46 | ||
| 47 | test('goalkeeper: the keyboard dives and stays up', () => { | |
| 48 | const g = boot('goalkeeper'); | |
| 49 | waitForTell(g); | |
| 50 | const side = read(g, 'K.lean(G)'); | |
| 51 | g.press(side === -1 ? 'ArrowLeft' : side === 1 ? 'ArrowRight' : 'ArrowUp'); | |
| 52 | decided(g); | |
| 53 | expect(g.text()).toContain('saved'); | |
| 54 | }); | |
| 55 | ||
| 56 | test('goalkeeper: the tell is hidden until the window opens, and the picture shows the lean', () => { | |
| 57 | const g = boot('goalkeeper'); | |
| 58 | expect(read(g, 'K.lean(G)')).toBe(null); | |
| 59 | const before = g.picture(); | |
| 60 | waitForTell(g); | |
| 61 | expect(read(g, 'K.lean(G)')).not.toBe(null); | |
| 62 | g.run(.1); | |
| 63 | expect(g.picture()).not.toBe(before); | |
| 64 | }); | |
| 65 | ||
| 66 | test('goalkeeper: ten penalties end the round with a win or loss line, a saved best, and a tap starts again', () => { | |
| 67 | const g = boot('goalkeeper'); | |
| 68 | for (let i = 0; i < 10; i++) { save(g); nextTaker(g); } | |
| 69 | expect(g.errors.map(e => e.message)).toEqual([]); | |
| 70 | expect(g.text()).toContain('you win'); | |
| 71 | expect(g.text()).toContain('10 saves'); | |
| 72 | expect(g.text()).toContain('tap to play again'); | |
| 73 | expect(g.store[KEY]).toBe('10'); | |
| 74 | g.run(1); | |
| 75 | g.tap(180, 140); | |
| 76 | g.run(.3); | |
| 77 | expect(g.text()).toContain('penalty 1/10'); | |
| 78 | expect(g.text()).toContain('best 10'); | |
| 79 | const lose = boot('goalkeeper'); | |
| 80 | lose.run(10 * 4); | |
| 81 | expect(lose.text()).toContain('you lose'); | |
| 82 | }); | |
| 83 | ||
| 84 | test('goalkeeper: diving before the tell window opens can make the taker switch, and the status says so', () => { | |
| 85 | // a seed and penalty where an early dive switches the shot: found with the rules, then played on the page | |
| 86 | const K = require('./shootout.js'); | |
| 87 | let seed = null; | |
| 88 | for (let s = 1; s < 500 && seed === null; s++) if (K.finalShot(s, 1, { side: 0, t: 0 }).switched) seed = s; | |
| 89 | expect(seed).not.toBe(null); | |
| 90 | const g = boot('goalkeeper'); | |
| 91 | read(g, `K.restart(G, ${seed})`); | |
| 92 | expect(read(g, 'K.lean(G)')).toBe(null); | |
| 93 | g.tap(X[0], 140); // t is well before the window opens | |
| 94 | expect(read(g, 'G.dive.t < K.tellOpens(1)')).toBe(true); | |
| 95 | decided(g); | |
| 96 | expect(g.text()).toContain('you dived early, the taker switched'); | |
| 97 | // the same dive inside the window never switches | |
| 98 | const h = boot('goalkeeper'); | |
| 99 | read(h, `K.restart(G, ${seed})`); | |
| 100 | waitForTell(h); | |
| 101 | h.tap(X[0], 140); | |
| 102 | decided(h); | |
| 103 | expect(h.text()).not.toContain('taker switched'); | |
| 104 | }); |
goalkeeper/shootout.js added +91
| @@ -0,0 +1,91 @@ | ||
| 1 | 'use strict'; | |
| 2 | // Goalkeeper rules as plain data in, plain data out: ten penalties, each taker runs up and shoots left, centre or right (high or low) | |
| 3 | // from the seed. The taker's lean shows the side for a short tell window before the kick, and the window shortens every penalty. | |
| 4 | // A dive (side -1 left, 0 stay up, 1 right) saves when it picks the shot side before the ball arrives. A dive before the tell window | |
| 5 | // opens is a guess the taker can answer: half the time they switch to another side. Times are seconds since the taker began the run-up. | |
| 6 | (function (root) { | |
| 7 | const PENALTIES = 10, WIN_AT = 7; | |
| 8 | const KICK = 1.5, FLIGHT = .3, ARRIVE = KICK + FLIGHT; // the boot meets the ball at KICK, the ball reaches the line at ARRIVE | |
| 9 | const TELL_FIRST = 1, TELL_STEP = .06; // the window before the kick, in seconds, for penalty 1 and how much each penalty takes off | |
| 10 | const SWITCH_CHANCE = .5; | |
| 11 | const RESULT_HOLD = 1.3; // seconds the result stays up before the next taker | |
| 12 | const SIDES = [-1, 0, 1]; | |
| 13 | ||
| 14 | // a seeded roll in [0, 1) for (seed, penalty, salt): the same round is the same round every time | |
| 15 | function roll(seed, n, salt) { | |
| 16 | let h = (Math.imul(seed | 0, 374761393) + Math.imul(n, 668265263) + Math.imul(salt, 2246822519)) | 0; | |
| 17 | h = Math.imul(h ^ h >>> 13, 1274126177); | |
| 18 | h = Math.imul(h ^ h >>> 16, 2246822519); | |
| 19 | h ^= h >>> 15; | |
| 20 | return (h >>> 0) / 4294967296; | |
| 21 | } | |
| 22 | const tellWindow = n => Math.max(.3, TELL_FIRST - TELL_STEP * (n - 1)); | |
| 23 | const tellOpens = n => KICK - tellWindow(n); | |
| 24 | ||
| 25 | // the shot the taker has in mind for penalty n | |
| 26 | function pickShot(seed, n) { | |
| 27 | return { side: SIDES[Math.floor(roll(seed, n, 1) * 3)], height: roll(seed, n, 2) < .5 ? 0 : 1 }; // height: 0 low, 1 high | |
| 28 | } | |
| 29 | // the shot as it will be struck: a dive before the window opens lets the taker switch to one of the other two sides | |
| 30 | function finalShot(seed, n, dive) { | |
| 31 | const shot = pickShot(seed, n); | |
| 32 | if (!dive || dive.t >= tellOpens(n) || roll(seed, n, 3) >= SWITCH_CHANCE) return { ...shot, switched: false }; | |
| 33 | const others = SIDES.filter(s => s !== shot.side); | |
| 34 | return { ...shot, side: others[roll(seed, n, 4) < .5 ? 0 : 1], switched: true }; | |
| 35 | } | |
| 36 | // dive: null (the keeper never moved) or { side, t } | |
| 37 | function resolve(seed, n, dive) { | |
| 38 | const shot = finalShot(seed, n, dive); | |
| 39 | let reason = 'saved'; | |
| 40 | if (!dive) reason = 'no dive'; | |
| 41 | else if (dive.t >= ARRIVE) reason = 'too late'; | |
| 42 | else if (dive.side !== shot.side) reason = 'wrong side'; | |
| 43 | return { shot, saved: reason === 'saved', reason }; | |
| 44 | } | |
| 45 | ||
| 46 | function newRound(best = 0, seed = Math.floor(Math.random() * 2147483647)) { | |
| 47 | return { seed, n: 1, saves: 0, results: [], best, record: false, phase: 'runup', t: 0, hold: 0, dive: null, last: null }; | |
| 48 | } | |
| 49 | function restart(G, seed = Math.floor(Math.random() * 2147483647)) { | |
| 50 | Object.assign(G, newRound(G.best, seed)); | |
| 51 | } | |
| 52 | // the first dive of a penalty counts; later ones are ignored | |
| 53 | function dive(G, side) { | |
| 54 | if (G.phase !== 'runup' || G.dive || !SIDES.includes(side)) return false; | |
| 55 | G.dive = { side, t: G.t }; | |
| 56 | return true; | |
| 57 | } | |
| 58 | // what the taker's body shows now: the side they lean to once the window is open, otherwise null | |
| 59 | function lean(G) { | |
| 60 | if (G.phase !== 'runup' || G.t < tellOpens(G.n)) return null; | |
| 61 | return finalShot(G.seed, G.n, G.dive).side; | |
| 62 | } | |
| 63 | // advance time; returns the result when a penalty is decided on this tick | |
| 64 | function tick(G, dt) { | |
| 65 | if (G.phase === 'runup') { | |
| 66 | G.t += dt; | |
| 67 | if (G.t < ARRIVE) return null; | |
| 68 | const out = resolve(G.seed, G.n, G.dive); | |
| 69 | out.n = G.n; | |
| 70 | G.last = out; | |
| 71 | G.results.push(out.saved); | |
| 72 | if (out.saved) G.saves++; | |
| 73 | if (G.n >= PENALTIES) { | |
| 74 | G.phase = 'over'; | |
| 75 | G.record = G.saves > G.best; | |
| 76 | if (G.record) G.best = G.saves; | |
| 77 | } else { G.phase = 'result'; G.hold = 0; } | |
| 78 | return out; | |
| 79 | } | |
| 80 | if (G.phase === 'result') { | |
| 81 | G.hold += dt; | |
| 82 | if (G.hold >= RESULT_HOLD) { G.n++; G.phase = 'runup'; G.t = 0; G.dive = null; } | |
| 83 | } | |
| 84 | return null; | |
| 85 | } | |
| 86 | const won = G => G.saves >= WIN_AT; | |
| 87 | ||
| 88 | const api = { PENALTIES, WIN_AT, KICK, FLIGHT, ARRIVE, RESULT_HOLD, SWITCH_CHANCE, tellWindow, tellOpens, pickShot, finalShot, resolve, newRound, restart, dive, lean, tick, won }; | |
| 89 | if (typeof module !== 'undefined' && module.exports) module.exports = api; | |
| 90 | else root.Goalkeeper = api; | |
| 91 | })(typeof window !== 'undefined' ? window : globalThis); |
goalkeeper/shootout.test.mjs added +119
| @@ -0,0 +1,119 @@ | ||
| 1 | import { test, expect } from 'bun:test'; | |
| 2 | import { PENALTIES, WIN_AT, KICK, ARRIVE, RESULT_HOLD, tellWindow, tellOpens, pickShot, finalShot, resolve, newRound, restart, dive, lean, tick, won } from './shootout.js'; | |
| 3 | ||
| 4 | // a seed and penalty whose taker will not switch on an early dive, and one that will | |
| 5 | const find = (want, side) => { for (let s = 1; s < 500; s++) for (let n = 1; n <= 10; n++) { | |
| 6 | const early = finalShot(s, n, { side: 0, t: 0 }); | |
| 7 | if (early.switched === want && (side === undefined || pickShot(s, n).side === side)) return [s, n]; | |
| 8 | } }; | |
| 9 | const inWindow = n => tellOpens(n) + .05; | |
| 10 | ||
| 11 | test('the shot comes from the seed: same seed, same shots, and every side and height turns up', () => { | |
| 12 | expect(pickShot(4, 3)).toEqual(pickShot(4, 3)); | |
| 13 | const sides = new Set(), heights = new Set(); | |
| 14 | for (let s = 0; s < 40; s++) for (let n = 1; n <= 10; n++) { const p = pickShot(s, n); sides.add(p.side); heights.add(p.height); } | |
| 15 | expect([...sides].sort()).toEqual([-1, 0, 1]); | |
| 16 | expect([...heights].sort()).toEqual([0, 1]); | |
| 17 | }); | |
| 18 | ||
| 19 | test('a dive to the shot side inside the window saves', () => { | |
| 20 | for (let n = 1; n <= 10; n++) { | |
| 21 | const side = pickShot(9, n).side; | |
| 22 | expect(resolve(9, n, { side, t: inWindow(n) })).toMatchObject({ saved: true, reason: 'saved' }); | |
| 23 | expect(resolve(9, n, { side, t: ARRIVE - .01 }).saved).toBe(true); | |
| 24 | } | |
| 25 | }); | |
| 26 | ||
| 27 | test('a dive to the wrong side concedes', () => { | |
| 28 | const side = pickShot(9, 2).side, wrong = side === 1 ? -1 : 1; | |
| 29 | expect(resolve(9, 2, { side: wrong, t: inWindow(2) })).toMatchObject({ saved: false, reason: 'wrong side' }); | |
| 30 | }); | |
| 31 | ||
| 32 | test('a dive after the ball arrives concedes, and so does never moving', () => { | |
| 33 | const side = pickShot(9, 2).side; | |
| 34 | expect(resolve(9, 2, { side, t: ARRIVE })).toMatchObject({ saved: false, reason: 'too late' }); | |
| 35 | expect(resolve(9, 2, { side, t: ARRIVE + .5 }).saved).toBe(false); | |
| 36 | expect(resolve(9, 2, null)).toMatchObject({ saved: false, reason: 'no dive' }); | |
| 37 | }); | |
| 38 | ||
| 39 | test('staying up saves a shot down the middle', () => { | |
| 40 | const [s, n] = find(false, 0); | |
| 41 | expect(resolve(s, n, { side: 0, t: inWindow(n) }).saved).toBe(true); | |
| 42 | }); | |
| 43 | ||
| 44 | test('an early dive can switch the shot, about half the time, and a dive inside the window cannot', () => { | |
| 45 | const [s, n] = find(true); | |
| 46 | const early = finalShot(s, n, { side: 0, t: 0 }); | |
| 47 | expect(early.switched).toBe(true); | |
| 48 | expect(early.side).not.toBe(pickShot(s, n).side); | |
| 49 | expect(finalShot(s, n, { side: 0, t: inWindow(n) })).toMatchObject({ switched: false, side: pickShot(s, n).side }); | |
| 50 | expect(finalShot(s, n, null).switched).toBe(false); | |
| 51 | let switched = 0, total = 0; | |
| 52 | for (let seed = 0; seed < 200; seed++) for (let k = 1; k <= 10; k++) { total++; if (finalShot(seed, k, { side: 1, t: 0 }).switched) switched++; } | |
| 53 | expect(switched / total).toBeGreaterThan(.4); | |
| 54 | expect(switched / total).toBeLessThan(.6); | |
| 55 | }); | |
| 56 | ||
| 57 | test('the window shrinks with the penalty number and the first opens before the kick', () => { | |
| 58 | for (let n = 2; n <= 10; n++) expect(tellWindow(n)).toBeLessThan(tellWindow(n - 1)); | |
| 59 | expect(tellOpens(1)).toBeGreaterThan(0); | |
| 60 | expect(tellOpens(10)).toBeGreaterThan(tellOpens(1)); | |
| 61 | expect(tellOpens(10)).toBeLessThan(KICK); | |
| 62 | }); | |
| 63 | ||
| 64 | test('the taker shows the lean only once the window opens, and it is the side struck', () => { | |
| 65 | const G = newRound(0, 5); | |
| 66 | tick(G, tellOpens(1) - .05); | |
| 67 | expect(lean(G)).toBe(null); | |
| 68 | tick(G, .1); | |
| 69 | expect(lean(G)).toBe(pickShot(5, 1).side); | |
| 70 | }); | |
| 71 | ||
| 72 | test('the keeper dives once per penalty, and the state resolves it at the arrival', () => { | |
| 73 | const G = newRound(0, 5); | |
| 74 | tick(G, inWindow(1)); | |
| 75 | expect(dive(G, pickShot(5, 1).side)).toBe(true); | |
| 76 | expect(dive(G, 2)).toBe(false); | |
| 77 | expect(dive(G, -pickShot(5, 1).side || 1)).toBe(false); | |
| 78 | const out = tick(G, ARRIVE); | |
| 79 | expect(out).toMatchObject({ saved: true, n: 1 }); | |
| 80 | expect(G.saves).toBe(1); | |
| 81 | expect(G.phase).toBe('result'); | |
| 82 | expect(dive(G, 0)).toBe(false); | |
| 83 | tick(G, RESULT_HOLD + .01); | |
| 84 | expect(G).toMatchObject({ phase: 'runup', n: 2, t: 0, dive: null }); | |
| 85 | }); | |
| 86 | ||
| 87 | test('the tenth penalty ends the round, with a best kept and a win from seven saves', () => { | |
| 88 | const G = newRound(2, 8); | |
| 89 | for (let n = 1; n <= PENALTIES; n++) { | |
| 90 | expect(G.phase).toBe('runup'); | |
| 91 | tick(G, inWindow(n)); | |
| 92 | dive(G, pickShot(8, n).side); | |
| 93 | tick(G, ARRIVE); | |
| 94 | if (n < PENALTIES) tick(G, RESULT_HOLD + .01); | |
| 95 | } | |
| 96 | expect(G.phase).toBe('over'); | |
| 97 | expect(G.saves).toBe(10); | |
| 98 | expect(G.results.length).toBe(10); | |
| 99 | expect(won(G)).toBe(true); | |
| 100 | expect(G.best).toBe(10); | |
| 101 | expect(G.record).toBe(true); | |
| 102 | expect(tick(G, 5)).toBe(null); | |
| 103 | restart(G, 9); | |
| 104 | expect(G).toMatchObject({ phase: 'runup', n: 1, saves: 0, best: 10, seed: 9 }); | |
| 105 | }); | |
| 106 | ||
| 107 | test('six saves lose and seven win', () => { | |
| 108 | expect(WIN_AT).toBe(7); | |
| 109 | expect(won({ saves: 6 })).toBe(false); | |
| 110 | expect(won({ saves: 7 })).toBe(true); | |
| 111 | }); | |
| 112 | ||
| 113 | test('never diving concedes ten times', () => { | |
| 114 | const G = newRound(0, 3); | |
| 115 | for (let i = 0; i < 2000 && G.phase !== 'over'; i++) tick(G, .05); | |
| 116 | expect(G.phase).toBe('over'); | |
| 117 | expect(G.saves).toBe(0); | |
| 118 | expect(won(G)).toBe(false); | |
| 119 | }); |
index.html +1
| @@ -86,6 +86,7 @@ footer a::after { content: none; } | ||
| 86 | 86 | <li data-kind="Timing" data-input="Hold" style="--accent:#6fb3d2"><a href="ice-fishing/">Ice Fishing</a><span>Drop a line through the ice and reel each fish up before it snaps: hold to let out line, a fish at the hook depth bites, hold to reel but ease off as the tension rises</span><span>Game forged by the crew</span><span class="tags">Timing · Hold</span></li> |
| 87 | 87 | <li data-kind="Sport" data-input="Drag" style="--accent:#b33a3a"><a href="archery/">Archery</a><span>Ten arrows at a straw target across a meadow: drag back to draw, read the pennant for wind, gold scores 10</span><span>Game forged by the crew</span><span class="tags">Sport · Drag</span></li> |
| 88 | 88 | <li data-kind="Precision" data-input="Hold" style="--accent:#c9a227"><a href="tram-line/">Tram Line</a><span>Drive a tram along eight stops: brake so the door lands on the yellow mark, exact scores 3, overshoot scores nothing</span><span>Game forged by the crew</span><span class="tags">Precision · Hold</span></li> |
| 89 | <li data-kind="Sport" data-input="Tap" style="--accent:#3f7a3a"><a href="goalkeeper/">Goalkeeper</a><span>Read the taker and dive left or right to save ten penalties</span><span>Game forged by the crew</span><span class="tags">Sport · Tap</span></li> | |
| 89 | 90 | </ul> |
| 90 | 91 | <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> |
| 91 | 92 | </main> |
og/goalkeeper.png added
Binary file not shown.
og/home.png
Binary file not shown.
serve.test.mjs +7
| @@ -381,6 +381,13 @@ test('Beekeeper and its rules script are served without a track', async () => { | ||
| 381 | 381 | expect((await fetch(base + '/beekeeper/hive.js')).status).toBe(200); |
| 382 | 382 | }); |
| 383 | 383 | |
| 384 | test('Goalkeeper and its rules script are served without a track', async () => { | |
| 385 | const page = await fetch(base + '/goalkeeper/'); | |
| 386 | expect(page.status).toBe(200); | |
| 387 | expect(page.headers.get('content-type')).toBe('text/html; charset=utf-8'); | |
| 388 | expect((await fetch(base + '/goalkeeper/shootout.js')).status).toBe(200); | |
| 389 | }); | |
| 390 | ||
| 384 | 391 | test('Tram Line and its rules script are served without a track', async () => { |
| 385 | 392 | const page = await fetch(base + '/tram-line/'); |
| 386 | 393 | expect(page.status).toBe(200); |
shell.js +1
| @@ -30,6 +30,7 @@ window.PixelPlays = (() => { | ||
| 30 | 30 | { id: 'ice-fishing', title: 'Ice Fishing', blurb: 'Drop a line through the ice and reel each fish up before it snaps: hold to let out line, a fish at the hook depth bites, hold to reel but ease off as the tension rises', kind: 'Timing', input: 'Hold', accent: '#6fb3d2', music: false, forged: true }, |
| 31 | 31 | { id: 'archery', title: 'Archery', blurb: 'Ten arrows at a straw target across a meadow: drag back to draw, read the pennant for wind, gold scores 10', kind: 'Sport', input: 'Drag', accent: '#b33a3a', music: false, forged: true }, |
| 32 | 32 | { id: 'tram-line', title: 'Tram Line', blurb: 'Drive a tram along eight stops: brake so the door lands on the yellow mark, exact scores 3, overshoot scores nothing', kind: 'Precision', input: 'Hold', accent: '#c9a227', music: false, forged: true }, |
| 33 | { id: 'goalkeeper', title: 'Goalkeeper', blurb: 'Read the taker and dive left or right to save ten penalties', kind: 'Sport', input: 'Tap', accent: '#3f7a3a', music: false, forged: true }, | |
| 33 | 34 | ]; |
| 34 | 35 | const MUSIC_VOLUME = .4; |
| 35 | 36 | const CREDITS = [ |
new above
offline