Public: anyone can read this; the crew can change it.

Migrated from github.com/amirmushichge/pixel-plays

Add Espresso Bar: pour each coffee to the line as cups slide past under the spout #71

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

quill/pixel-plays:quill/61-add-espresso-bar-pour-each-coffee-to-the → main 6 commits fork

  • Merged. quill/61-add-espresso-bar-pour-each-coffee-to-the was merged into main as 50e7872.

Review

Sign in to review this change.
9 files changed +585 −2
README.md +3 −2
@@ -37,6 +37,7 @@ Concept by [abhi](https://x.com/awwbhi2). Game by [AmirMushich](https://x.com/Am
3737 | [Ice Fishing](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 |
3838 | [Archery](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 |
3939 | [Tram Line](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 |
40 | [Espresso Bar](espresso-bar/) | Twenty cups slide past under an espresso spout, each with a painted fill line and an order card (espresso a short line, latte a tall one): hold to pour, and the stream keeps falling for a quarter second after you let go; within 3% of the line scores 3, within 10% scores 1, over the brim spills and costs a life, and a cup that passes untouched costs a life; cups come faster as the round goes, three lives end it (forged by the crew on the engine and shell above, not by the original creators) | Hold space, or press and hold the canvas |
4041 | [Goalkeeper](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 |
4142
4243 ## Run
@@ -57,7 +58,7 @@ On the forge, `forge.json` runs `bun serve.mjs` with health check `/`. State is
5758 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.
5859
5960 ## Add a game
60 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, Goalkeeper and Wood Split 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`.
61 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, Espresso Bar, Goalkeeper and Wood Split 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`.
6162
6263 - `shell.js` provides the game menu, the Rules popup, the credits and the background music.
6364 - `engine.js` provides the camera, depth buffer, colour ramps, shadows, edges, figures, trees and wind as globals.
@@ -73,7 +74,7 @@ Create `<id>/index.html`, load `../shell.js`, call `PixelPlays.mount({ id, rules
7374 - Depth-buffer edges: darker where an object sits in front, a warm rim on upper-left edges.
7475 - Large flat surfaces use closely spaced tones; wide steps make the dither read as a checkerboard.
7576 - Characters stay sharp: flat bands with no dither, a dark silhouette on their own pixels, patterns at least 3px wide.
76 - Ambient motion only where the scene names it: grass and leaves sway in whole pixels at 8 fps, and Tram Line's waiting passengers shift their weight; all of it stops under `prefers-reduced-motion`.
77 - Ambient motion only where the scene names it: grass and leaves sway in whole pixels at 8 fps, Tram Line's waiting passengers shift their weight, and Espresso Bar's machine steam and awning fringe move; all of it stops under `prefers-reduced-motion`.
7778
7879 ## License
7980 The code is under the [MIT License](LICENSE). The music is not: see [MUSIC.md](MUSIC.md).
espresso-bar/index.html added +348
@@ -0,0 +1,348 @@
1 <!doctype html>
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>Espresso Bar</title>
7 <!-- og:begin -->
8 <meta name="description" content="Pour twenty coffees to the line as cups slide past: hold to pour, let go early, over the brim costs a life">
9 <link rel="canonical" href="https://pixel-plays--forge-trevato-apps.open-platform.sh/espresso-bar/">
10 <meta property="og:type" content="website">
11 <meta property="og:site_name" content="Pixel Plays">
12 <meta property="og:title" content="Espresso Bar">
13 <meta property="og:description" content="Pour twenty coffees to the line as cups slide past: hold to pour, let go early, over the brim costs a life">
14 <meta property="og:url" content="https://pixel-plays--forge-trevato-apps.open-platform.sh/espresso-bar/">
15 <meta property="og:image" content="https://pixel-plays--forge-trevato-apps.open-platform.sh/og/espresso-bar.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="Espresso Bar: the opening scene of the game">
20 <meta name="twitter:card" content="summary_large_image">
21 <!-- og:end -->
22 <style>
23 :root { --ink: #1c1b19; --muted: #8b877f; --paper: #fff; --accent: #6f4a33; }
24 * { box-sizing: border-box; }
25 html, body { height: 100%; margin: 0; background: var(--paper); color: var(--ink); }
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; }
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 #bottom { bottom: max(14px, env(safe-area-inset-bottom)); align-items: flex-end; }
32 .row { display: flex; align-items: center; gap: 8px; color: var(--accent); }
33 .row b { font-size: 16px; font-weight: 600; min-width: 1ch; font-variant-numeric: tabular-nums; }
34 .row span { color: var(--muted); }
35 #status { color: var(--muted); min-height: 1.35em; }
36 button { pointer-events: auto; font: inherit; color: var(--muted); background: none; border: 0; padding: 10px; margin: -10px; cursor: pointer; }
37 button:hover, button:focus-visible { color: var(--ink); }
38 </style>
39 </head>
40 <body>
41 <div id="stage"><canvas id="c" tabindex="0" aria-label="A street café counter with an espresso machine, a short belt of glass cups sliding past under its spout, and a striped awning. Each cup has a painted fill line and a small order card: espresso has a short line, latte a tall one. Hold space, or press and hold anywhere on the canvas, to pour; the stream keeps falling for a moment after you let go, so let go early. Within 3 percent of the line scores 3, within 10 percent scores 1, over the brim spills and costs a life, and a cup that passes untouched costs a life. Twenty cups or three lives."></canvas></div>
42 <div id="scores">
43 <div class="row"><b id="score">0</b><span>score</span></div>
44 <div class="row"><b id="cup">1</b><span>cup of 20</span></div>
45 <div class="row"><b id="lives">3</b><span>lives</span></div>
46 <div class="row"><b id="best">0</b><span>best</span></div>
47 </div>
48 <div class="hud" id="bottom">
49 <div id="status" role="status" aria-live="polite"></div>
50 <button id="mute" type="button" aria-pressed="false">sound on</button>
51 </div>
52 <script src="../shell.js"></script>
53 <script>
54 PixelPlays.mount({
55 id: 'espresso-bar',
56 rules: [
57 'Hold space, or press and hold the canvas, to pour. The stream keeps falling a moment after you let go, so let go early.',
58 'Within 3% of the painted line scores 3, within 10% scores 1.',
59 'Over the brim, or a cup passing untouched, costs a life. Twenty cups or three lives.',
60 ],
61 });
62 </script>
63 <script>window.PP_SCENE = { originY: 30, shadowX: 1.3, shadowY: -.12 };</script>
64 <script src="../engine.js"></script>
65 <script src="pour.js"></script>
66 <script>
67 'use strict';
68
69 const BEST_KEY = 'pixel-plays-espresso-bar-best';
70 const BELT_Y = 1.4, CUP_BASE = .08, CUP_H = 3.4, CUP_R = 1.05, NOZZLE_Z = 4.25;
71 const COUNTER = { x0: -9.5, x1: 9.5, y0: -2.8, y1: 3.2, z0: -1.5 }, BELT = { y0: .3, y1: 2.5 };
72 const WOOD = [176, 132, 92], WOOD_DARK = [124, 88, 60], SLATE = [74, 74, 84], STEEL = [150, 154, 164], CREAM = [240, 228, 204];
73 const BROWN = [111, 74, 51], COFFEE = [92, 52, 28], GLASS = [196, 214, 226], MACHINE = [214, 74, 58], BRASS = [214, 168, 70];
74 const LINE_RED = rgb(214, 40, 36), INK_PX = rgb(28, 27, 25), PAPER_PX = rgb(255, 255, 255);
75 const AWNING_Y0 = -3.2, AWNING_Y1 = 3.9, AWNING_Z0 = 11.4, AWNING_Z1 = 9.9, STRIPE = 1.9;
76
77 /* ---------- state ---------- */
78 const best0 = (() => { try { return Number(localStorage.getItem(BEST_KEY)) || 0; } catch (e) { return 0; } })();
79 let R = Pour.newRound(best0);
80 const G = { time: 0, status: '', noteUntil: 0, leaving: [] };
81 const nodes = {};
82 for (const id of ['score', 'cup', 'lives', 'best', 'status']) nodes[id] = document.getElementById(id);
83 function renderHud() {
84 nodes.score.textContent = R.score;
85 nodes.cup.textContent = Math.min(R.cup + 1, Pour.CUPS);
86 nodes.lives.textContent = R.lives;
87 nodes.best.textContent = Math.max(R.best, R.score);
88 }
89 function setStatus(text) {
90 if (text === G.status) return;
91 G.status = text;
92 nodes.status.textContent = text;
93 }
94 function note(text) { setStatus(text); G.noteUntil = G.time + 2.2; }
95 const HINT = 'hold space or press to pour · let go early, the stream keeps falling';
96
97 /* ---------- baked scene: counter, belt, machine and awning ---------- */
98 const mod = (a, n) => ((a % n) + n) % n;
99 function counterColor(x, y) {
100 if (y > BELT.y0 - .12 && y < BELT.y0 + .08 || y > BELT.y1 - .08 && y < BELT.y1 + .12) return rgb(...STEEL);
101 if (y >= BELT.y0 && y <= BELT.y1) return (mod(x, 1.1) < .1 ? rgb(60, 60, 70) : rgb(...SLATE));
102 const plank = Math.floor((y + 4) / .7), seam = mod(y + 4, .7) < .07;
103 const t = .94 + hash(plank, Math.floor(x / 3.1 + plank)) * .1 - (seam ? .12 : 0);
104 return rgb(WOOD[0] * t, WOOD[1] * t, WOOD[2] * t);
105 }
106 function bakeMachine() {
107 const mx0 = -3.4, mx1 = 3.4, my0 = -2.4, my1 = -.4, top = 5.8;
108 box(mx0, my0, 0, mx1, my1, top, MACHINE, 3);
109 box(mx0 - .2, my0 - .1, top, mx1 + .2, my1 + .15, top + .3, STEEL, 5); // steel cap
110 box(mx0 + .3, my1, .4, mx1 - .3, my1 + .2, 1.3, STEEL, 7); // drip tray
111 box(-1, my1, 4, 1, .9, 5.1, STEEL, 9); // group head
112 capsule([0, .9, 4.65], [0, BELT_Y, 4.65], .2, .2, () => STEEL, 'solid'); // spout arm
113 capsule([0, BELT_Y, 4.65], [0, BELT_Y, NOZZLE_Z], .24, .14, () => STEEL, 'solid'); // nozzle
114 sphere(-2.2, my1 + .05, 3.6, .5, CREAM, 'gloss'); // pressure gauge
115 sphere(2.2, my1 + .05, 3.6, .5, BRASS, 'gloss');
116 for (const x of [-2.2, -1.5, 1.5]) box(x - .2, my1, 2.2, x + .2, my1 + .15, 2.6, CREAM, 11);
117 // a cup rail and a pair of saucers on the back of the counter
118 for (const x of [5.2, 6.4, 7.6]) capsule([x, -1.4, 0], [x, -1.4, .5], .5, .5, () => CREAM, 'solid');
119 box(-8.6, -2.4, 0, -6.2, -.8, .9, WOOD_DARK, 13); // bean crate
120 for (let k = 0; k < 14; k++) sphere(-8.4 + hash(k, 2) * 2, -2.2 + hash(2, k) * 1.4, .95, .22, [70, 44, 30], 'rough');
121 }
122 function bakeAwning() {
123 for (const x of [COUNTER.x0 + .3, COUNTER.x1 - .3]) {
124 capsule([x, 3.5, COUNTER.z0], [x, 3.5, AWNING_Z1], .14, .14, () => STEEL, 'solid');
125 capsule([x, -3, 0], [x, -3, AWNING_Z0], .14, .14, () => STEEL, 'solid');
126 }
127 const stripes = Math.ceil((COUNTER.x1 - COUNTER.x0) / STRIPE);
128 for (let k = 0; k < stripes; k++) {
129 const x0 = COUNTER.x0 + k * STRIPE, x1 = Math.min(COUNTER.x1, x0 + STRIPE), col = k % 2 ? CREAM : BROWN;
130 for (let y = AWNING_Y0; y < AWNING_Y1; y += .5) {
131 const z = lerp(AWNING_Z0, AWNING_Z1, (y - AWNING_Y0) / (AWNING_Y1 - AWNING_Y0));
132 box(x0, y, z, x1, y + .55, z + .12, col, k + 40);
133 }
134 }
135 }
136 function bake() {
137 zb.fill(-1e9);
138 pix.fill(PAPER_PX);
139 groundZ.fill(-1e9);
140 box(COUNTER.x0, COUNTER.y0, COUNTER.z0, COUNTER.x1, COUNTER.y1, 0, WOOD_DARK, 17);
141 for (let iy = 0; iy < H; iy++) for (let ix = 0; ix < W; ix++) {
142 const g = unproject(ix + .5, iy + .5), i = iy * W + ix;
143 if (g.x < COUNTER.x0 || g.x > COUNTER.x1 || g.y < COUNTER.y0 || g.y > COUNTER.y1 || zb[i] < -1e8) continue;
144 pix[i] = counterColor(g.x, g.y);
145 ground[i] = 1;
146 groundZ[i] = zb[i];
147 }
148 bakeMachine();
149 for (let x = -3.6; x <= 3.6; x += .6) for (let z = .6; z <= 5.8; z += 1.2) castShadow(x, -2.4, z, .6, 1, 0);
150 for (let y = -2.4; y <= -.4; y += .5) for (let z = .6; z <= 5.8; z += 1.2) castShadow(3.4, y, z, .6, 1, 0);
151 bakeOcclusion();
152 applyShadows(groundZ);
153 bakeAwning();
154 bakedPix.set(pix);
155 bakedZ.set(zb);
156 }
157
158 /* ---------- drawing ---------- */
159 const DIGITS = {
160 E: ['111', '100', '111', '100', '111'], S: ['111', '100', '111', '001', '111'], P: ['111', '101', '111', '100', '100'],
161 L: ['100', '100', '100', '100', '111'], A: ['010', '101', '111', '101', '101'], T: ['111', '010', '010', '010', '010'],
162 };
163 const CARD_TEXT = { espresso: 'ESP', latte: 'LAT' };
164 function px(ix, iy, color) { if (ix >= 0 && iy >= 0 && ix < W && iy < H) pix[iy * W + ix] = color; }
165 // a small order card: the drink, and a bar as long as its painted line is tall
166 function card(x, kind, line) {
167 const p = project(x, BELT_Y, 0), left = Math.round(p.sx) - 9, top = Math.round(p.sy) + 11;
168 for (let dy = 0; dy < 11; dy++) for (let dx = 0; dx < 19; dx++) {
169 px(left + dx, top + dy, dx === 0 || dy === 0 || dx === 18 || dy === 10 ? INK_PX : PAPER_PX);
170 }
171 [...CARD_TEXT[kind]].forEach((ch, n) => DIGITS[ch].forEach((row, dy) => [...row].forEach((c, dx) => { if (c === '1') px(left + 3 + n * 4 + dx, top + 2 + dy, INK_PX); })));
172 const bar = Math.round(line * 24);
173 for (let dx = 0; dx < bar; dx++) for (let dy = 0; dy < 2; dy++) px(left + 3 + dx, top + 8 + dy, LINE_RED);
174 }
175 const shadeOf = (cosT, sinT) => clamp(2 + Math.round((-cosT * .6 + sinT * .6) * 2.2), 0, 4);
176 // a glass cup: coffee up to the fill level, a red painted line where the order wants it
177 function cup(x, fill, line, spilled) {
178 const top = CUP_BASE + CUP_H, fillZ = CUP_BASE + CUP_H * fill, lineZ = CUP_BASE + CUP_H * line;
179 const glass = rampOf(GLASS), coffee = rampOf(COFFEE);
180 for (let z = CUP_BASE; z <= top; z += .045) {
181 const r = lerp(CUP_R * .86, CUP_R, (z - CUP_BASE) / CUP_H), liquid = z <= fillZ || spilled;
182 for (let a = -.9; a <= 2.5; a += .09) {
183 const c = Math.cos(a), s = Math.sin(a);
184 plot(x + c * r, BELT_Y + s * r, z, z < CUP_BASE + .12 ? rgb(...STEEL) : (liquid ? coffee : glass)[shadeOf(c, s)]);
185 }
186 }
187 for (const dz of [0, .05]) for (let a = -.9; a <= 2.5; a += .05) plot(x + Math.cos(a) * (CUP_R + .03), BELT_Y + Math.sin(a) * (CUP_R + .03), lineZ + dz, LINE_RED);
188 for (let a = 0; a < 6.3; a += .1) plot(x + Math.cos(a) * CUP_R, BELT_Y + Math.sin(a) * CUP_R, top + .02, rgb(...CREAM)); // rim
189 for (let r = 0; r < CUP_R - .1; r += .09) for (let a = 0; a < 6.3; a += .12) {
190 plot(x + Math.cos(a) * r, BELT_Y + Math.sin(a) * r, top - .02, fill >= .96 || spilled ? coffee[3] : rgb(214, 226, 234)); // the surface at the brim
191 }
192 if (spilled) for (let r = CUP_R; r < CUP_R + 1; r += .09) for (let a = 0; a < 6.3; a += .12) plot(x + Math.cos(a) * r, BELT_Y + Math.sin(a) * r * .8, CUP_BASE + .01, coffee[1]);
193 for (let z = 0; z <= top; z += .6) castShadow(x, BELT_Y, z, .5, 1, 0);
194 }
195 function stream(target) {
196 const col = rampOf(COFFEE);
197 capsule([0, BELT_Y, NOZZLE_Z - .1], [0, BELT_Y, target], .14, .14, () => COFFEE, 'solid');
198 for (let z = target; z < NOZZLE_Z; z += .5) castShadow(0, BELT_Y, z, .15, 1, 0);
199 for (let k = 0; k < 6; k++) plot(Math.cos(k) * .5, BELT_Y + Math.sin(k) * .4, target + .02, col[1]);
200 }
201 function steam(t) {
202 const time = STILL ? 0 : t;
203 for (let k = 0; k < 6; k++) {
204 const ph = (time * .25 + k / 6) % 1;
205 sphere(2.4 + ph * 1.2, -1.4 + Math.sin(ph * 6 + k) * .3, 6.5 + ph * 3, .28 + ph * .4, [226 - ph * 20, 226 - ph * 20, 230 - ph * 18], 'rough');
206 }
207 }
208 // the awning fringe: tabs under the front edge that shift a pixel at 8 fps
209 function fringe(t) {
210 const step = STILL ? 0 : Math.floor(t * 8), stripes = Math.ceil((COUNTER.x1 - COUNTER.x0) / STRIPE);
211 for (let k = 0; k < stripes; k++) {
212 const x0 = COUNTER.x0 + k * STRIPE, x1 = Math.min(COUNTER.x1, x0 + STRIPE), col = k % 2 ? CREAM : BROWN;
213 const sway = (STILL ? 0 : Math.sin(step * .9 + k * 1.7)) * .12;
214 box(x0 + .05, AWNING_Y1 + sway, AWNING_Z1 - .7, x1 - .05, AWNING_Y1 + .12 + sway, AWNING_Z1 + .12, col, k + 60);
215 }
216 }
217 const cupX = k => R.x - k * Pour.SPACING;
218 function draw() {
219 pix.set(bakedPix);
220 zb.set(bakedZ);
221 steam(G.time);
222 const waiting = [];
223 for (let k = 3; k >= 0; k--) {
224 const n = R.cup + k, x = cupX(k);
225 if (n >= Pour.CUPS || x < COUNTER.x0 + 1.2) continue;
226 const fill = k === 0 ? R.fill : 0;
227 cup(x, fill, Pour.lineOf(n), k === 0 && R.spilled);
228 waiting.push([x, Pour.kindOf(n), Pour.lineOf(n)]);
229 }
230 for (const c of G.leaving) cup(c.x, c.fill, c.line, c.spilled);
231 const under = Math.abs(R.x) <= Pour.WINDOW && !R.done;
232 if (R.flowing && R.phase === 'play') stream(under ? CUP_BASE + CUP_H * Math.min(R.fill, 1) : CUP_BASE);
233 applyShadows(groundZ);
234 fringe(G.time);
235 shadeEdges();
236 for (const [x, kind, line] of waiting) card(x, kind, line);
237 ctx.putImageData(image, 0, 0);
238 }
239
240 /* ---------- game ---------- */
241 const hold = { key: false, pointers: new Set() };
242 const holding = () => hold.key || hold.pointers.size > 0;
243 function act(events) {
244 for (const e of events) {
245 if (e.type === 'cup' || e.type === 'miss' || e.type === 'spill') {
246 if (R.cup !== e.index) G.leaving.push({ x: R.x + Pour.SPACING, fill: e.fill, line: e.line, spilled: false, speed: Pour.speedOf(e.index) });
247 if (e.type === 'spill') { note('over the brim: spilled, a life lost'); sound('buzz'); }
248 else if (e.type === 'miss') { note('a cup went by empty: a life lost'); sound('buzz'); }
249 else {
250 const pct = Math.round((e.fill - e.line) * 100);
251 note(e.points === 3 ? 'on the line: +3' : e.points ? `${Math.abs(pct)}% ${pct < 0 ? 'under' : 'over'} the line: +1` : `${Math.abs(pct)}% ${pct < 0 ? 'under' : 'over'}: nothing`);
252 sound(e.points === 3 ? 'chime' : e.points ? 'thud' : 'buzz');
253 }
254 }
255 if (e.type === 'over') {
256 if (R.record) try { localStorage.setItem(BEST_KEY, String(R.best)); } catch (err) { /* private window: keep it for this visit */ }
257 const why = e.reason === 'cups' ? 'twenty cups poured' : 'out of lives';
258 setStatus(`${why} · ${R.score}${R.record ? ' · new best' : ` · best ${R.best}`} · tap or press space to pour again`);
259 }
260 }
261 renderHud();
262 }
263 function restart() {
264 const next = Pour.again(R);
265 if (!next) return false;
266 R = next; G.leaving = [];
267 note(HINT);
268 renderHud();
269 return true;
270 }
271 function update(dt) {
272 G.time += dt;
273 act(Pour.advance(R, dt, holding()));
274 for (const c of G.leaving) c.x += c.speed * Math.min(dt, .1);
275 G.leaving = G.leaving.filter(c => c.x < COUNTER.x1 + 1.5);
276 if (R.phase === 'play' && G.time >= G.noteUntil) {
277 setStatus(`cup ${R.cup + 1} of ${Pour.CUPS} · ${Pour.kindOf(R.cup)}, pour to the line`);
278 }
279 }
280
281 /* ---------- input ---------- */
282 canvas.addEventListener('keydown', e => {
283 if (e.key !== ' ') return;
284 e.preventDefault();
285 ensureAudio();
286 if (e.repeat) return;
287 if (restart()) return;
288 hold.key = true;
289 });
290 canvas.addEventListener('keyup', e => { if (e.key === ' ') hold.key = false; });
291 canvas.addEventListener('blur', () => { hold.key = false; hold.pointers.clear(); });
292 canvas.addEventListener('pointerdown', e => {
293 ensureAudio();
294 canvas.focus({ preventScroll: true });
295 if (restart()) return;
296 hold.pointers.add(e.pointerId);
297 try { canvas.setPointerCapture(e.pointerId); } catch (err) { /* synthetic pointers have nothing to capture */ }
298 });
299 for (const type of ['pointerup', 'pointercancel']) canvas.addEventListener(type, e => hold.pointers.delete(e.pointerId));
300
301 /* ---------- sound ---------- */
302 let audio = null, muted = false;
303 function ensureAudio() {
304 if (audio) { if (audio.state === 'suspended') audio.resume(); return; }
305 const AC = window.AudioContext || window.webkitAudioContext;
306 if (AC) audio = new AC();
307 }
308 function sound(kind) {
309 if (!audio || muted) return;
310 const now = audio.currentTime, gain = audio.createGain(), osc = audio.createOscillator();
311 const spec = {
312 thud: [180, 90, .12, .08, 'sine'],
313 chime: [660, 990, .3, .07, 'sine'],
314 buzz: [150, 70, .3, .06, 'sawtooth'],
315 }[kind];
316 osc.type = spec[4];
317 osc.frequency.setValueAtTime(spec[0], now);
318 osc.frequency.exponentialRampToValueAtTime(spec[1], now + spec[2]);
319 gain.gain.setValueAtTime(spec[3], now);
320 gain.gain.exponentialRampToValueAtTime(.0005, now + spec[2]);
321 osc.connect(gain).connect(audio.destination);
322 osc.start(now); osc.stop(now + spec[2] + .01);
323 }
324 const muteButton = document.getElementById('mute');
325 muteButton.addEventListener('click', () => {
326 muted = !muted;
327 muteButton.textContent = muted ? 'sound off' : 'sound on';
328 muteButton.setAttribute('aria-pressed', String(muted));
329 });
330
331 /* ---------- fit and boot ---------- */
332 addEventListener('resize', fit);
333 fit();
334 bake();
335 note(HINT);
336 renderHud();
337 canvas.focus({ preventScroll: true });
338 let last = performance.now();
339 function frame(now) {
340 update(Math.min(.04, (now - last) / 1000));
341 last = now;
342 draw();
343 requestAnimationFrame(frame);
344 }
345 requestAnimationFrame(frame);
346 </script>
347 </body>
348 </html>
espresso-bar/pour.js added +87
@@ -0,0 +1,87 @@
1 'use strict';
2 // The espresso bar rules: cups slide under a spout one at a time, the player holds to pour, and the stream keeps
3 // falling for a moment after release. Fill is a fraction of the cup's height (1 is the brim); a cup's position is in
4 // world units with the spout at 0. Plain numbers in, plain numbers out, no DOM. The page draws what this decides.
5 (function (root) {
6 const CUPS = 20, LIVES = 3;
7 const RATE = .55; // cup heights poured per second while the stream runs
8 const DELAY = .25; // seconds the stream keeps falling after release
9 const WINDOW = 1; // a cup takes coffee while its centre is within this of the spout
10 const SPACING = 4.4; // distance between neighbouring cups on the belt
11 const BASE_SPEED = 1, SPEED_UP = .02; // belt speed in units per second: cup n runs BASE_SPEED * (1 + SPEED_UP * n)
12 const LINES = { espresso: .3, latte: .6 }; // the painted fill line, as a fraction of the cup
13 const ORDERS = ['espresso', 'latte', 'espresso', 'espresso', 'latte'];
14 const EXACT = .03, NEAR = .1; // within EXACT of the line scores 3, within NEAR scores 1
15 const SCORES = { exact: 3, near: 1, far: 0 };
16 const START_X = -3, RESTART_AFTER = .6, EPS = 1e-9;
17
18 const finite = v => (Number.isFinite(v) ? v : 0);
19 const kindOf = n => ORDERS[((n % ORDERS.length) + ORDERS.length) % ORDERS.length];
20 const lineOf = n => LINES[kindOf(n)];
21 const speedOf = n => BASE_SPEED * (1 + SPEED_UP * Math.max(0, finite(n)));
22 // points for a cup filled to this fraction when its line is `line`
23 function scoreFor(fill, line) {
24 if (!Number.isFinite(fill) || !Number.isFinite(line)) return SCORES.far;
25 const off = Math.abs(fill - line);
26 return off <= EXACT + EPS ? SCORES.exact : off <= NEAR + EPS ? SCORES.near : SCORES.far;
27 }
28
29 function newRound(best = 0) {
30 return {
31 phase: 'play', timer: 0, score: 0, cup: 0, x: START_X, fill: 0, done: false, spilled: false, tail: 0, flowing: false,
32 lives: LIVES, results: [], best: Math.max(0, finite(best)), record: false, reason: '',
33 };
34 }
35 const under = s => Math.abs(s.x) <= WINDOW + EPS;
36
37 function finish(s, reason, events) {
38 s.phase = 'over'; s.reason = reason; s.timer = 0; s.flowing = false;
39 if (s.score > s.best) { s.best = s.score; s.record = true; }
40 events.push({ type: 'over', reason, score: s.score, lives: s.lives, record: s.record });
41 }
42 function settle(s, result, events) {
43 s.results.push(result);
44 s.score += result.points;
45 s.lives -= result.lost;
46 s.done = true;
47 events.push({ type: result.spilled ? 'spill' : result.missed ? 'miss' : 'cup', ...result });
48 if (s.lives <= 0) finish(s, 'lives', events);
49 }
50 // move the belt and the stream over dt; `hold` is whether the player is pouring
51 function advance(s, dt, hold) {
52 const events = [];
53 dt = Math.max(0, Math.min(.1, finite(dt)));
54 if (s.phase !== 'play') { s.timer += dt; return events; }
55 s.flowing = !!hold || s.tail > 0;
56 s.tail = hold ? DELAY : Math.max(0, s.tail - dt);
57 if (s.flowing && !s.done && under(s)) {
58 s.fill += RATE * dt;
59 if (s.fill > 1 + EPS) {
60 s.fill = 1; s.spilled = true;
61 settle(s, { index: s.cup, kind: kindOf(s.cup), line: lineOf(s.cup), fill: 1, points: 0, lost: 1, spilled: true, missed: false }, events);
62 if (s.phase !== 'play') return events;
63 }
64 }
65 s.x += speedOf(s.cup) * dt;
66 if (s.x > WINDOW) {
67 if (!s.done) {
68 const missed = s.fill <= 0, line = lineOf(s.cup);
69 settle(s, { index: s.cup, kind: kindOf(s.cup), line, fill: s.fill, points: missed ? 0 : scoreFor(s.fill, line), lost: missed ? 1 : 0, spilled: false, missed }, events);
70 if (s.phase !== 'play') return events;
71 }
72 s.cup++; s.x -= SPACING; s.fill = 0; s.done = false; s.spilled = false;
73 if (s.cup >= CUPS) finish(s, 'cups', events);
74 }
75 return events;
76 }
77 // a press after the round has been over for a moment starts the next one; otherwise null
78 function again(s) {
79 return s.phase === 'over' && s.timer > RESTART_AFTER ? newRound(s.best) : null;
80 }
81
82 const api = {
83 CUPS, LIVES, RATE, DELAY, WINDOW, SPACING, BASE_SPEED, SPEED_UP, LINES, EXACT, NEAR,
84 kindOf, lineOf, speedOf, scoreFor, newRound, advance, again,
85 };
86 if (typeof module !== 'undefined') module.exports = api; else root.Pour = api;
87 })(typeof window !== 'undefined' ? window : globalThis);
espresso-bar/pour.test.mjs added +138
@@ -0,0 +1,138 @@
1 import { test, expect } from 'bun:test';
2 import { CUPS, LIVES, RATE, DELAY, WINDOW, SPACING, EXACT, NEAR, kindOf, lineOf, speedOf, scoreFor, newRound, advance, again } from './pour.js';
3
4 const DT = 1 / 60;
5 const run = (s, seconds, hold) => { const out = []; for (let t = 0; t < seconds - 1e-9; t += DT) out.push(...advance(s, DT, hold)); return out; };
6 // a cup sitting under the spout, with the belt held still by setting x back each step
7 const still = (s, seconds, hold) => { const out = []; for (let t = 0; t < seconds - 1e-9; t += DT) { s.x = 0; out.push(...advance(s, DT, hold)); } return out; };
8 const settleWith = (fill, n = 0) => {
9 const s = newRound(); s.cup = n; s.x = WINDOW - .001; s.fill = fill;
10 return { s, events: run(s, .05) };
11 };
12
13 test('holding fills the cup at the stated rate, only while it is under the spout', () => {
14 const s = newRound();
15 run(s, 1, true);
16 expect(s.fill).toBe(0); // the first cup has not arrived yet
17 const t = newRound();
18 still(t, .5, true);
19 expect(t.fill).toBeCloseTo(RATE * .5, 1);
20 });
21
22 test('the stream keeps adding after release for the stated delay, then stops', () => {
23 const s = newRound();
24 still(s, .3, true);
25 const released = s.fill;
26 still(s, DELAY * 1.5, false);
27 expect(s.fill - released).toBeGreaterThan(RATE * DELAY * .9);
28 expect(s.fill - released).toBeLessThanOrEqual(RATE * DELAY + RATE * DT + 1e-6);
29 const settled = s.fill;
30 still(s, 1, false);
31 expect(s.fill).toBe(settled);
32 });
33
34 test('fill scoring: within 3% of the line is 3, within 10% is 1, further is 0', () => {
35 expect(scoreFor(.3, .3)).toBe(3);
36 expect(scoreFor(.3 + EXACT, .3)).toBe(3);
37 expect(scoreFor(.3 - .029, .3)).toBe(3);
38 expect(scoreFor(.3 + .05, .3)).toBe(1);
39 expect(scoreFor(.3 + NEAR, .3)).toBe(1);
40 expect(scoreFor(.3 - .09, .3)).toBe(1);
41 expect(scoreFor(.3 + .11, .3)).toBe(0);
42 expect(scoreFor(NaN, .3)).toBe(0);
43 const { s, events } = settleWith(.31);
44 expect(events[0]).toMatchObject({ type: 'cup', index: 0, points: 3, lost: 0 });
45 expect(s.score).toBe(3);
46 expect(s.cup).toBe(1);
47 expect(settleWith(.2).events[0]).toMatchObject({ type: 'cup', points: 1 });
48 expect(settleWith(.1).events[0]).toMatchObject({ type: 'cup', points: 0, lost: 0 });
49 });
50
51 test('a latte has a taller line than an espresso', () => {
52 expect(kindOf(0)).toBe('espresso');
53 expect(kindOf(1)).toBe('latte');
54 expect(lineOf(1)).toBeGreaterThan(lineOf(0));
55 const { events } = settleWith(lineOf(1), 1);
56 expect(events[0]).toMatchObject({ kind: 'latte', points: 3 });
57 });
58
59 test('pouring over the brim spills the cup and costs a life', () => {
60 const s = newRound();
61 const events = still(s, 3, true);
62 const spill = events.find(e => e.type === 'spill');
63 expect(spill).toMatchObject({ index: 0, points: 0, lost: 1, spilled: true });
64 expect(s.lives).toBe(LIVES - 1);
65 expect(s.fill).toBe(1);
66 expect(s.score).toBe(0);
67 // a spilled cup is settled once: leaving the spout costs nothing more
68 const before = s.lives;
69 run(s, 4, false);
70 expect(s.lives).toBe(before);
71 expect(s.cup).toBeGreaterThanOrEqual(1);
72 });
73
74 test('a cup that passes the spout untouched costs a life', () => {
75 const { s, events } = settleWith(0);
76 expect(events[0]).toMatchObject({ type: 'miss', index: 0, points: 0, lost: 1, missed: true });
77 expect(s.lives).toBe(LIVES - 1);
78 });
79
80 test('a poured cup far from its line scores nothing but keeps the life', () => {
81 const { s } = settleWith(.02);
82 expect(s.lives).toBe(LIVES);
83 });
84
85 test('the cup speed rises with the cup number', () => {
86 expect(speedOf(5)).toBeGreaterThan(speedOf(0));
87 expect(speedOf(19)).toBeGreaterThan(speedOf(10));
88 // a faster cup spends less time under the spout
89 const time = n => 2 * WINDOW / speedOf(n);
90 expect(time(19)).toBeLessThan(time(0));
91 // and the round's own belt moves that fast
92 const s = newRound(); s.cup = 19; s.x = 0; advance(s, DT, false);
93 expect(s.x).toBeCloseTo(speedOf(19) * DT, 6);
94 });
95
96 test('the third lost life ends the round at once', () => {
97 const s = newRound();
98 const events = run(s, 60, false);
99 expect(s.lives).toBe(0);
100 expect(s.phase).toBe('over');
101 expect(s.reason).toBe('lives');
102 expect(events.filter(e => e.type === 'miss').length).toBe(LIVES);
103 expect(events.at(-1)).toMatchObject({ type: 'over', reason: 'lives' });
104 expect(s.cup).toBe(LIVES - 1);
105 });
106
107 test('twenty cups end the round, and the best is kept', () => {
108 const s = newRound(5);
109 for (let n = 0; n < CUPS; n++) {
110 s.x = WINDOW - .001; s.fill = lineOf(s.cup);
111 run(s, .05);
112 }
113 expect(s.phase).toBe('over');
114 expect(s.reason).toBe('cups');
115 expect(s.score).toBe(CUPS * 3);
116 expect(s.lives).toBe(LIVES);
117 expect(s.best).toBe(60);
118 expect(s.record).toBe(true);
119 });
120
121 test('a press after the round has been over a moment starts the next, keeping the best', () => {
122 const s = newRound();
123 while (s.phase === 'play') advance(s, DT, false);
124 expect(again(s)).toBe(null);
125 run(s, 1, false);
126 const next = again(s);
127 expect(next).toMatchObject({ phase: 'play', score: 0, cup: 0, lives: LIVES });
128 expect(next.best).toBe(s.best);
129 expect(again(next)).toBe(null);
130 });
131
132 test('the belt keeps cups a gap apart, and bad numbers do no harm', () => {
133 expect(SPACING).toBeGreaterThan(WINDOW * 2);
134 const s = newRound();
135 expect(advance(s, NaN, true)).toEqual([]);
136 expect(advance(s, -1, undefined)).toEqual([]);
137 expect(s.x).toBe(-3);
138 });
index.html +1
@@ -87,6 +87,7 @@ footer a::after { content: none; }
8787 <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>
8888 <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>
8989 <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>
90 <li data-kind="Precision" data-input="Hold" style="--accent:#6f4a33"><a href="espresso-bar/">Espresso Bar</a><span>Pour twenty coffees to the line as cups slide past: hold to pour, let go early, over the brim costs a life</span><span>Game forged by the crew</span><span class="tags">Precision · Hold</span></li>
9091 <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>
9192 </ul>
9293 <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>
og/espresso-bar.png added
Binary file not shown.
og/home.png
Binary file not shown.
serve.test.mjs +7
@@ -401,3 +401,10 @@ test('Tram Line and its rules script are served without a track', async () => {
401401 expect(page.headers.get('content-type')).toBe('text/html; charset=utf-8');
402402 expect((await fetch(base + '/tram-line/line.js')).status).toBe(200);
403403 });
404
405 test('Espresso Bar and its rules script are served without a track', async () => {
406 const page = await fetch(base + '/espresso-bar/');
407 expect(page.status).toBe(200);
408 expect(page.headers.get('content-type')).toBe('text/html; charset=utf-8');
409 expect((await fetch(base + '/espresso-bar/pour.js')).status).toBe(200);
410 });
shell.js +1
@@ -31,6 +31,7 @@ window.PixelPlays = (() => {
3131 { 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 },
3232 { 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 },
3333 { 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 },
34 { id: 'espresso-bar', title: 'Espresso Bar', blurb: 'Pour twenty coffees to the line as cups slide past: hold to pour, let go early, over the brim costs a life', kind: 'Precision', input: 'Hold', accent: '#6f4a33', music: false, forged: true },
3435 { 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 },
3536 ];
3637 const MUSIC_VOLUME = .4;

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs