|
|
@@ -0,0 +1,570 @@ |
| 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>Tea Party</title> |
| 7 |
|
| 8 |
<meta name="description" content="Remember every guest's order and pour it right as the table grows"> |
| 9 |
<link rel="canonical" href="https://pixel-plays--forge-trevato-apps.open-platform.sh/tea-party/"> |
| 10 |
<meta property="og:type" content="website"> |
| 11 |
<meta property="og:site_name" content="Pixel Plays"> |
| 12 |
<meta property="og:title" content="Tea Party"> |
| 13 |
<meta property="og:description" content="Remember every guest's order and pour it right as the table grows"> |
| 14 |
<meta property="og:url" content="https://pixel-plays--forge-trevato-apps.open-platform.sh/tea-party/"> |
| 15 |
<meta property="og:image" content="https://pixel-plays--forge-trevato-apps.open-platform.sh/og/tea-party.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="Tea Party: the opening scene of the game"> |
| 20 |
<meta name="twitter:card" content="summary_large_image"> |
| 21 |
|
| 22 |
<style> |
| 23 |
:root { --ink: #1c1b19; --muted: #6b675f; --paper: #fff; --accent: #c98aa0; } |
| 24 |
* { box-sizing: border-box; } |
| 25 |
html, body { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent; } |
| 26 |
html, body { height: 100%; margin: 0; background: var(--paper); color: var(--ink); } |
| 27 |
body { font: 13px/1.35 ui-monospace, "Cascadia Mono", Consolas, Menlo, monospace; overflow: hidden; } |
| 28 |
#stage { position: fixed; inset: 0; display: grid; place-items: center; } |
| 29 |
canvas { display: block; image-rendering: pixelated; touch-action: none; cursor: pointer; outline: none; } |
| 30 |
canvas:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; } |
| 31 |
.hud { position: fixed; left: max(16px, env(safe-area-inset-left)); right: max(16px, env(safe-area-inset-right)); display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; pointer-events: none; } |
| 32 |
#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; } |
| 33 |
#bottom { bottom: max(20px, env(safe-area-inset-bottom)); } |
| 34 |
.line { display: flex; flex-wrap: wrap; gap: 0 12px; } |
| 35 |
.row { display: flex; align-items: center; gap: 8px; color: var(--accent); } |
| 36 |
.row b { font-size: 16px; font-weight: 600; min-width: 1ch; font-variant-numeric: tabular-nums; } |
| 37 |
.row span { color: var(--muted); } |
| 38 |
#status { color: var(--muted); min-height: 2.7em; max-width: 60ch; } |
| 39 |
button { pointer-events: auto; font: inherit; color: var(--muted); background: none; border: 0; padding: 13px 10px; margin: -13px -10px; min-height: 44px; min-width: 44px; cursor: pointer; } |
| 40 |
button:hover, button:focus-visible { color: var(--ink); } |
| 41 |
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; } |
| 42 |
noscript p { position: fixed; inset: 40% 16px auto; margin: 0; text-align: center; color: var(--muted); } |
| 43 |
</style> |
| 44 |
</head> |
| 45 |
<body> |
| 46 |
<div id="stage"><canvas id="c" tabindex="0" aria-label="Tea Party: a garden table with up to five guests, a teapot, a coffee pot, a milk jug, a sugar bowl and a cake stand. Each guest's order shows in a speech bubble in turn: drink, milk, sugars and cake. Then serve from memory: tap a guest, then the pot, milk, sugar and cake, and tap the guest again to serve. With a keyboard, Tab or the arrow keys move and Enter chooses. Three wrong parts in a round end the party."></canvas></div> |
| 47 |
<noscript><p>Tea Party needs JavaScript to play.</p></noscript> |
| 48 |
<div id="scores"> |
| 49 |
<div class="line"> |
| 50 |
<div class="row"><b id="round">1</b><span id="of">of 4 rounds</span></div> |
| 51 |
<div class="row"><b id="guests">2</b><span>guests</span></div> |
| 52 |
<div class="row"><b id="score">0</b><span>points</span></div> |
| 53 |
</div> |
| 54 |
<div class="line"> |
| 55 |
<div class="row"><b id="wrong">0</b><span id="ofwrong">wrong parts</span></div> |
| 56 |
<div class="row"><b id="perfect">0</b><span>perfect</span></div> |
| 57 |
<div class="row"><b id="best">0</b><span>best</span></div> |
| 58 |
</div> |
| 59 |
<div class="row"><span id="cup"></span></div> |
| 60 |
</div> |
| 61 |
<div class="hud" id="bottom"> |
| 62 |
<div id="status" role="status" aria-live="polite"></div> |
| 63 |
<button id="mute" type="button" aria-pressed="false">sound on</button> |
| 64 |
</div> |
| 65 |
<script src="../shell.js"></script> |
| 66 |
<script> |
| 67 |
PixelPlays.mount({ |
| 68 |
id: 'tea-party', |
| 69 |
rules: [ |
| 70 |
'Listen: each guest shows an order in turn, tea or coffee, milk or none, 0 to 3 sugars and a cake. Remember them all.', |
| 71 |
'Serve: tap a guest, then the pot, milk, sugar (each tap adds one) and cake (each tap changes it); tap the guest again to serve. Keys: Tab or arrows, Enter.', |
| 72 |
'Each right part scores 10 and a perfect cup 20 more. Three wrong parts in a round end the party.', |
| 73 |
'Serve rounds of 2, 3, 4 and 5 guests to win.', |
| 74 |
], |
| 75 |
}); |
| 76 |
</script> |
| 77 |
<script>window.PP_SCENE = { originY: 10, shadowX: 1.3, shadowY: -.12 };</script> |
| 78 |
<script src="../engine.js"></script> |
| 79 |
<script src="table.js"></script> |
| 80 |
<script> |
| 81 |
'use strict'; |
| 82 |
|
| 83 |
const M = window.TeaParty; |
| 84 |
const BEST_KEY = 'pixel-plays-tea-party-best'; |
| 85 |
const LINE = rgb(40, 36, 48), WHITE = rgb(255, 255, 255), PINK = rgb(201, 138, 160), GREEN = rgb(86, 160, 96), RED = rgb(208, 87, 60), GOLD = rgb(250, 206, 36); |
| 86 |
const PAVING = [[150, 146, 150], [164, 160, 162], [178, 174, 172], [192, 188, 184]]; |
| 87 |
const CLOTH_A = [206, 140, 164], CLOTH_B = [246, 236, 232], VALANCE = [186, 112, 140]; |
| 88 |
const SKIN = [[222, 170, 132], [176, 124, 92], [236, 190, 150], [150, 100, 72], [200, 148, 112]]; |
| 89 |
const HAIR = [[74, 50, 34], [36, 30, 28], [196, 150, 70], [60, 40, 30], [150, 150, 156]]; |
| 90 |
|
| 91 |
const GUESTS = [ |
| 92 |
{ name: 'in the top hat', shirt: [64, 118, 190], stripe: [240, 232, 210], legs: [60, 60, 70], hair: HAIR[1], skin: SKIN[1] }, |
| 93 |
{ name: 'in the boater', shirt: [86, 160, 96], stripe: [240, 232, 210], legs: [52, 66, 96], hair: HAIR[2], skin: SKIN[2] }, |
| 94 |
{ name: 'in the sun hat', shirt: [236, 196, 60], stripe: [250, 244, 226], legs: [70, 80, 100], hair: HAIR[3], skin: SKIN[3] }, |
| 95 |
{ name: 'in the bobble hat', shirt: [150, 80, 168], stripe: [240, 232, 210], legs: [44, 50, 60], hair: HAIR[0], skin: SKIN[0] }, |
| 96 |
{ name: 'in the party hat', shirt: [208, 87, 60], stripe: [240, 232, 210], legs: [52, 66, 96], hair: HAIR[4], skin: SKIN[4] }, |
| 97 |
]; |
| 98 |
const SEATS = { 2: [225, 315], 3: [180, 270, 0], 4: [180, 235, 305, 0], 5: [180, 225, 270, 315, 0] }; |
| 99 |
const SEAT_A = 6.8, SEAT_B = 4.6, TABLE_R = 4.2, TOP = 1.5; |
| 100 |
const STATIONS = ['tea', 'coffee', 'milk', 'sugar', 'cake']; |
| 101 |
const STATION_A = { tea: -3.1, coffee: -1.55, milk: 0, sugar: 1.55, cake: 3.1 }, STATION_B = .5; |
| 102 |
const STATION_NAME = { tea: 'the teapot', coffee: 'the coffee pot', milk: 'the milk jug', sugar: 'the sugar bowl', cake: 'the cake stand' }; |
| 103 |
|
| 104 |
const toWorld = (a, b, z) => [(a + b) / R2, (b - a) / R2, z]; |
| 105 |
const toLocal = (x, y) => ({ a: (x - y) / R2, b: (x + y) / R2 }); |
| 106 |
|
| 107 |
|
| 108 |
const nodes = {}; |
| 109 |
for (const k of ['round', 'of', 'guests', 'score', 'wrong', 'ofwrong', 'perfect', 'best', 'cup', 'status']) nodes[k] = document.getElementById(k); |
| 110 |
const shown = {}; |
| 111 |
function setText(key, text) { |
| 112 |
if (shown[key] === text) return; |
| 113 |
shown[key] = text; |
| 114 |
nodes[key].textContent = text; |
| 115 |
} |
| 116 |
const best0 = (() => { try { return Number(localStorage.getItem(BEST_KEY)) || 0; } catch (e) { return 0; } })(); |
| 117 |
let P = M.start(Math.floor(Math.random() * 4294967296)); |
| 118 |
|
| 119 |
|
| 120 |
const G = { time: 0, sel: null, cup: blankCup(), results: {}, focus: 0, keyed: false, dirty: true, wind: -1, overAt: 0, best: best0, carry: '' }; |
| 121 |
function blankCup() { return { drink: null, milk: null, sugars: null, cake: null }; } |
| 122 |
const article = c => (c === 'eclair' ? 'an ' : 'a ') + c; |
| 123 |
const sugarText = n => n === 0 ? 'no sugar' : n + (n === 1 ? ' sugar' : ' sugars'); |
| 124 |
const orderText = o => `${o.drink}, ${o.milk === 'milk' ? 'with milk' : 'no milk'}, ${sugarText(o.sugars)}, ${article(o.cake)}`; |
| 125 |
function cupText(c) { |
| 126 |
const bits = [c.drink || 'drink?', c.milk ? (c.milk === 'milk' ? 'milk' : 'no milk') : 'milk?', c.sugars === null ? 'sugar?' : sugarText(c.sugars), c.cake || 'cake?']; |
| 127 |
return bits.join(' · '); |
| 128 |
} |
| 129 |
const guestName = i => `guest ${i + 1} ${GUESTS[i].name}`; |
| 130 |
const cupDone = c => M.PARTS.every(p => c[p] !== null); |
| 131 |
|
| 132 |
function renderHud() { |
| 133 |
setText('round', String(P.round)); |
| 134 |
setText('of', `of ${M.LAST_ROUND} rounds`); |
| 135 |
setText('guests', String(P.guests)); |
| 136 |
setText('score', String(P.score)); |
| 137 |
setText('wrong', String(P.wrong)); |
| 138 |
setText('ofwrong', `of ${M.MAX_WRONG} wrong parts`); |
| 139 |
setText('perfect', String(P.perfect)); |
| 140 |
setText('best', String(G.best)); |
| 141 |
setText('cup', G.sel === null ? '' : `cup for guest ${G.sel + 1}: ${cupText(G.cup)}`); |
| 142 |
} |
| 143 |
function say(text) { setText('status', text); } |
| 144 |
|
| 145 |
|
| 146 |
function seatOf(i) { |
| 147 |
const deg = SEATS[P.guests][i], a = SEAT_A * Math.cos(deg * Math.PI / 180), b = SEAT_B * Math.sin(deg * Math.PI / 180); |
| 148 |
const w = toWorld(a, b, 0); |
| 149 |
return { a, b, x: w[0], y: w[1] }; |
| 150 |
} |
| 151 |
function stationAt(id) { const w = toWorld(STATION_A[id], STATION_B, TOP); return { x: w[0], y: w[1] }; } |
| 152 |
|
| 153 |
function pointOf(t) { |
| 154 |
if (typeof t === 'number') { const s = seatOf(t), p = project(s.x, s.y, 1.7); return [Math.round(p.sx), Math.round(p.sy)]; } |
| 155 |
const s = stationAt(t), p = project(s.x, s.y, TOP + .7); |
| 156 |
return [Math.round(p.sx), Math.round(p.sy)]; |
| 157 |
} |
| 158 |
function targetAt(sx, sy) { |
| 159 |
let best = null, bestD = 1e9; |
| 160 |
for (const id of STATIONS) { |
| 161 |
const [x, y] = pointOf(id), dx = Math.abs(sx - x), dy = sy - y; |
| 162 |
if (dx <= 8 && dy >= -13 && dy <= 12 && dx + Math.abs(dy) < bestD) { best = id; bestD = dx + Math.abs(dy); } |
| 163 |
} |
| 164 |
if (best) return best; |
| 165 |
for (let i = 0; i < P.guests; i++) { |
| 166 |
const [x, y] = pointOf(i), dx = Math.abs(sx - x), dy = sy - y; |
| 167 |
if (dx <= 13 && dy >= -26 && dy <= 24 && dx + Math.abs(dy) < bestD) { best = i; bestD = dx + Math.abs(dy); } |
| 168 |
} |
| 169 |
return best; |
| 170 |
} |
| 171 |
const ring = () => [...Array(P.guests).keys(), ...STATIONS]; |
| 172 |
|
| 173 |
|
| 174 |
function groundColor(x, y, ix, iy, i) { |
| 175 |
const { a, b } = toLocal(x, y); |
| 176 |
const edge = Math.pow(Math.pow(Math.abs(a) / 15, 4) + Math.pow(Math.abs(b) / 7.6, 4), .25); |
| 177 |
const rough = edge + (noise(a * 1.3, b * 1.3) - .5) * .1; |
| 178 |
if (rough > 1.04) return 0; |
| 179 |
if (rough > .94 && hash(ix, iy) < (rough - .94) / .1) return 0; |
| 180 |
if (Math.pow(a / 9.5, 2) + Math.pow(b / 5.8, 2) > 1 || rough > .86) { |
| 181 |
kinds[i] = KIND_GRASS; |
| 182 |
const t = noise(a * .9, b * .9) * 1.15 + (hash(ix, iy) - .5) * .55 - .08; |
| 183 |
return rgb(...GRASS[clamp(Math.floor(t * 4.2), 0, 3)]); |
| 184 |
} |
| 185 |
const joint = Math.abs(((a + 30) % 1.6) - .8) > .72 || Math.abs(((b + 30) % 1.6) - .8) > .72; |
| 186 |
const t = noise(a * .7, b * .7) * .7 + (hash(ix, iy) - .5) * .3 + (bayerAt(ix, iy) - .5) * .2 - (joint ? .45 : 0); |
| 187 |
return rgb(...PAVING[clamp(Math.floor(t * 3.6), 0, 3)]); |
| 188 |
} |
| 189 |
const clothZ = new Float32Array(W * H).fill(-1e9); |
| 190 |
function paintCloth(x, y, z, color) { |
| 191 |
const p = project(x, y, z), ix = p.sx | 0, iy = p.sy | 0; |
| 192 |
if (ix < 0 || iy < 0 || ix >= W || iy >= H) return; |
| 193 |
const i = iy * W + ix; |
| 194 |
if (p.d <= zb[i]) return; |
| 195 |
zb[i] = p.d; pix[i] = color; clothZ[i] = p.d; |
| 196 |
} |
| 197 |
function bakeTable() { |
| 198 |
const tone = (a, b, lit) => { |
| 199 |
const check = (Math.floor((a + 20) / 1.15) + Math.floor((b + 20) / 1.15)) % 2 ? CLOTH_A : CLOTH_B; |
| 200 |
const k = lit + (hash(Math.floor(a * 9), Math.floor(b * 9)) - .5) * .08; |
| 201 |
return rgb(check[0] * k, check[1] * k, check[2] * k); |
| 202 |
}; |
| 203 |
for (let a = -TABLE_R; a <= TABLE_R; a += .055) for (let b = -TABLE_R; b <= TABLE_R; b += .055) { |
| 204 |
if (a * a + b * b > TABLE_R * TABLE_R) continue; |
| 205 |
const w = toWorld(a, b, TOP); |
| 206 |
paintCloth(w[0], w[1], TOP, tone(a, b, 1)); |
| 207 |
} |
| 208 |
|
| 209 |
for (let ang = 0; ang < Math.PI * 2; ang += .006) { |
| 210 |
const a = TABLE_R * Math.cos(ang), b = TABLE_R * Math.sin(ang), scallop = .45 + .22 * Math.abs(Math.sin(ang * 14)); |
| 211 |
for (let z = TOP - .02; z > TOP - scallop; z -= .05) { |
| 212 |
const w = toWorld(a, b, z), k = .8 - .2 * Math.max(0, Math.cos(ang)) * (b > -.5 ? 1 : .3); |
| 213 |
paintCloth(w[0], w[1], z, rgb(VALANCE[0] * k, VALANCE[1] * k, VALANCE[2] * k)); |
| 214 |
} |
| 215 |
} |
| 216 |
const post = toWorld(0, 0, 0); |
| 217 |
capsule([post[0], post[1], 0], [post[0], post[1], 1.0], .32, .26, () => [150, 120, 96], 'solid'); |
| 218 |
const foot = toWorld(0, 0, .08); |
| 219 |
capsule([foot[0], foot[1], .05], [foot[0], foot[1], .12], 1.1, 1.0, () => [150, 120, 96], 'solid'); |
| 220 |
for (let a = -TABLE_R; a <= TABLE_R; a += .9) for (let b = -TABLE_R; b <= TABLE_R; b += .9) { |
| 221 |
if (a * a + b * b < TABLE_R * TABLE_R * 1.1) { const w = toWorld(a, b, TOP); castShadow(w[0], w[1], TOP, .9, 1, .2); } |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
function clothShadow(x, y, h, r) { |
| 226 |
const p = project(x + SHADOW_X * h, y + SHADOW_Y * h, TOP), rx = r * S, ry = rx * SIN + 1; |
| 227 |
for (let iy = Math.max(0, Math.floor(p.sy - ry)); iy <= Math.min(H - 1, Math.ceil(p.sy + ry)); iy++) { |
| 228 |
for (let ix = Math.max(0, Math.floor(p.sx - rx)); ix <= Math.min(W - 1, Math.ceil(p.sx + rx)); ix++) { |
| 229 |
const dx = (ix + .5 - p.sx) / rx, dy = (iy + .5 - p.sy) / ry, i = iy * W + ix; |
| 230 |
if (dx * dx + dy * dy < 1 && clothZ[i] > -1e8 && zb[i] === clothZ[i]) tint(i, .6, .56, .74); |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
const TEAL = [96, 160, 170], CREAM = [244, 236, 224], COPPER = [196, 120, 70], DARK = [72, 52, 44], STEEL = [200, 204, 214]; |
| 235 |
function bakeItems() { |
| 236 |
const solid = (a, b, r0, r1, c) => capsule(a, b, r0, r1, () => c, 'solid'); |
| 237 |
const at = (id, dx, dz) => { const w = toWorld(STATION_A[id] + dx, STATION_B, TOP + dz); return w; }; |
| 238 |
|
| 239 |
let c = at('tea', 0, .5); |
| 240 |
sphere(c[0], c[1], c[2], .52, TEAL, 'gloss'); |
| 241 |
const lid = at('tea', 0, 1.05); sphere(lid[0], lid[1], lid[2], .22, CREAM, 'gloss'); |
| 242 |
const knob = at('tea', 0, 1.25); sphere(knob[0], knob[1], knob[2], .08, [201, 138, 160], 'solid'); |
| 243 |
solid(at('tea', -.4, .4), at('tea', -.85, .95), .13, .07, TEAL); |
| 244 |
solid(at('tea', .45, .95), at('tea', .8, .6), .08, .08, TEAL); solid(at('tea', .8, .6), at('tea', .45, .25), .08, .08, TEAL); |
| 245 |
|
| 246 |
solid(at('coffee', 0, .05), at('coffee', 0, 1.45), .42, .3, COPPER); |
| 247 |
const top = at('coffee', 0, 1.6); sphere(top[0], top[1], top[2], .2, DARK, 'gloss'); |
| 248 |
solid(at('coffee', -.3, 1.25), at('coffee', -.62, 1.62), .1, .06, COPPER); |
| 249 |
solid(at('coffee', .4, 1.25), at('coffee', .52, .45), .07, .07, DARK); |
| 250 |
|
| 251 |
solid(at('milk', 0, .05), at('milk', 0, .8), .3, .26, [236, 238, 244]); |
| 252 |
solid(at('milk', 0, .4), at('milk', 0, .5), .31, .31, [96, 132, 190]); |
| 253 |
solid(at('milk', -.25, .75), at('milk', -.45, .88), .08, .05, [236, 238, 244]); |
| 254 |
solid(at('milk', .28, .7), at('milk', .42, .4), .06, .06, [236, 238, 244]); |
| 255 |
|
| 256 |
c = at('sugar', 0, .38); sphere(c[0], c[1], c[2], .42, [244, 214, 222], 'gloss'); |
| 257 |
const lid2 = at('sugar', 0, .78); sphere(lid2[0], lid2[1], lid2[2], .22, [250, 232, 238], 'gloss'); |
| 258 |
|
| 259 |
solid(at('cake', 0, .03), at('cake', 0, .1), .5, .5, STEEL); |
| 260 |
solid(at('cake', 0, .1), at('cake', 0, .72), .1, .1, STEEL); |
| 261 |
solid(at('cake', 0, .72), at('cake', 0, .8), .62, .62, STEEL); |
| 262 |
for (const id of STATIONS) { const w = at(id, 0, 0); for (const h of [.4, .8, 1.2]) clothShadow(w[0], w[1], h, .45); } |
| 263 |
} |
| 264 |
function bake() { |
| 265 |
ground.fill(0); shadowMask.fill(0); staticShadow.fill(0); kinds.fill(0); |
| 266 |
zb.fill(-1e9); |
| 267 |
pix.fill(PAPER); |
| 268 |
for (let iy = 0; iy < H; iy++) for (let ix = 0; ix < W; ix++) { |
| 269 |
const g = unproject(ix + .5, iy + .5), i = iy * W + ix, c = groundColor(g.x, g.y, ix, iy, i); |
| 270 |
if (!c) continue; |
| 271 |
pix[i] = c; ground[i] = 1; |
| 272 |
zb[i] = (g.x + g.y) / R2 * COS; |
| 273 |
} |
| 274 |
growGrass(); |
| 275 |
groundZ.set(zb); |
| 276 |
bakeTable(); |
| 277 |
bakeItems(); |
| 278 |
const tree = toWorld(-12, -4.8, 0); |
| 279 |
growTree(tree[0], tree[1], .9, 260, 5); |
| 280 |
castShadow(tree[0], tree[1], 1.3, 1.1, 1, .4); |
| 281 |
bakeOcclusion(); |
| 282 |
staticShadow.set(shadowMask); |
| 283 |
applyShadows(groundZ); |
| 284 |
bakedPix.set(pix); |
| 285 |
bakedZ.set(zb); |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
function hat(k, x, y, base, yaw) { |
| 290 |
const z = base + 2.72, fx = Math.cos(yaw), fy = Math.sin(yaw); |
| 291 |
if (k === 0) { |
| 292 |
dynamicAdd([x, y, z + .06], [x, y, z + .06], .4, .4, () => [40, 36, 44]); |
| 293 |
dynamicAdd([x, y, z + .1], [x, y, z + .72], .26, .26, () => [40, 36, 44]); |
| 294 |
dynamicAdd([x, y, z + .14], [x, y, z + .24], .28, .28, () => [190, 50, 50]); |
| 295 |
} else if (k === 1) { |
| 296 |
dynamicAdd([x, y, z + .05], [x, y, z + .05], .46, .46, () => [236, 214, 150]); |
| 297 |
dynamicAdd([x, y, z + .1], [x, y, z + .38], .28, .28, () => [236, 214, 150]); |
| 298 |
dynamicAdd([x, y, z + .13], [x, y, z + .2], .29, .29, () => [60, 90, 170]); |
| 299 |
} else if (k === 2) { |
| 300 |
dynamicAdd([x, y, z + .05], [x, y, z + .05], .62, .62, () => [240, 220, 160]); |
| 301 |
dynamicAdd([x, y, z + .1], [x, y, z + .26], .26, .26, () => [240, 220, 160]); |
| 302 |
dynamicAdd([x - fy * .24, y + fx * .24, z + .3], [x - fy * .24, y + fx * .24, z + .3], .13, .13, () => [230, 90, 130]); |
| 303 |
} else if (k === 3) { |
| 304 |
dynamicAdd([x, y, z + .02], [x, y, z + .02], .29, .29, () => [90, 130, 200]); |
| 305 |
dynamicAdd([x, y, z + .3], [x, y, z + .3], .12, .12, () => [250, 244, 226]); |
| 306 |
} else { |
| 307 |
dynamicAdd([x, y, z + .04], [x, y, z + .8], .26, .04, () => [220, 80, 150]); |
| 308 |
dynamicAdd([x, y, z + .84], [x, y, z + .84], .08, .08, () => [250, 206, 36]); |
| 309 |
} |
| 310 |
} |
| 311 |
function drawGuest(i) { |
| 312 |
const s = seatOf(i), yaw = Math.atan2(-s.y, -s.x), pal = GUESTS[i]; |
| 313 |
const talking = P.phase === 'order' && P.status === 'playing' && P.shown === i + 1; |
| 314 |
const picked = G.sel === i; |
| 315 |
figure(s.x, s.y, yaw, { armR: talking ? .9 : .1, armL: picked ? .5 : .12, stance: .1, crouch: .05 }, pal, dynamicAdd); |
| 316 |
hat(i, s.x, s.y, 0, yaw); |
| 317 |
} |
| 318 |
function drawCake() { |
| 319 |
const w = toWorld(STATION_A.cake, STATION_B, TOP + .8), x = w[0], y = w[1], c = G.cup.cake, z = w[2]; |
| 320 |
const add = (a, b, r0, r1, col) => dynamicAdd(a, b, r0, r1, () => col); |
| 321 |
if (c === 'scone') { add([x, y, z + .02], [x, y, z + .06], .46, .46, [214, 160, 84]); add([x, y, z + .24], [x, y, z + .24], .34, .34, [226, 176, 96]); } |
| 322 |
else if (c === 'sponge') { |
| 323 |
add([x, y, z + .02], [x, y, z + .2], .42, .42, [244, 214, 120]); add([x, y, z + .22], [x, y, z + .3], .43, .43, [250, 244, 230]); |
| 324 |
add([x, y, z + .32], [x, y, z + .5], .42, .42, [244, 214, 120]); add([x, y, z + .52], [x, y, z + .56], .43, .43, [226, 130, 160]); |
| 325 |
} else if (c === 'tart') { add([x, y, z + .02], [x, y, z + .16], .5, .46, [226, 176, 90]); add([x, y, z + .18], [x, y, z + .2], .38, .38, [238, 120, 60]); add([x, y, z + .3], [x, y, z + .3], .12, .12, [200, 40, 50]); } |
| 326 |
else if (c === 'eclair') { const a = toWorld(STATION_A.cake - .4, STATION_B, z + .22), b = toWorld(STATION_A.cake + .4, STATION_B, z + .22); add(a, b, .22, .22, [250, 240, 214]); add([a[0], a[1], a[2] + .14], [b[0], b[1], b[2] + .14], .2, .2, [100, 62, 44]); } |
| 327 |
else add([x, y, z + .05], [x, y, z + .05], .45, .45, [184, 204, 220]); |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
function rect(x, y, w, h, c) { |
| 332 |
for (let iy = Math.max(0, y); iy < Math.min(H, y + h); iy++) for (let ix = Math.max(0, x); ix < Math.min(W, x + w); ix++) pix[iy * W + ix] = c; |
| 333 |
} |
| 334 |
function frameRect(x, y, w, h, c) { rect(x, y, w, 1, c); rect(x, y + h - 1, w, 1, c); rect(x, y, 1, h, c); rect(x + w - 1, y, 1, h, c); } |
| 335 |
|
| 336 |
function bitmap(rows, x, y, pal) { |
| 337 |
rows.forEach((row, r) => { for (let c = 0; c < row.length; c++) { const ch = row[c]; if (ch !== '.') rect(x + c, y + r, 1, 1, ch === '#' ? LINE : pal[ch]); } }); |
| 338 |
} |
| 339 |
const ICON_W = 9, ICON_H = 8; |
| 340 |
const TEA_ICON = ['....#....', '..#####..', '##aaaaa##', '#aaaaaaa#', '#aaaaaaa#', '.#aaaaa#.', '..#####..']; |
| 341 |
const COFFEE_ICON = ['...###...', '..#bbb#..', '..#aaa##.', '..#aaa#.#', '..#aaa#.#', '..#aaa##.', '..#aaa#..', '..#####..']; |
| 342 |
const MILK_ICON = ['..####...', '.#wwww#..', '.#wwww##.', '.#wwww#.#', '.#wwww#.#', '.#wwww##.', '..####...']; |
| 343 |
const CROSS_ICON = ['#.......#', '.#.....#.', '..#...#..', '...#.#...', '..#...#..', '.#.....#.', '#.......#']; |
| 344 |
const CAKE_ICONS = { |
| 345 |
scone: ['.........', '...###...', '..#ttt#..', '.#ttttt#.', '.#ttttt#.', '.#######.'], |
| 346 |
sponge: ['..#####..', '.#ppppp#.', '.#ccccc#.', '.#sssss#.', '.#ccccc#.', '.#sssss#.', '..#####..'], |
| 347 |
tart: ['....r....', '...rrr...', '.#######.', '#fffrfff#', '.#fffff#.', '..#####..'], |
| 348 |
eclair: ['.........', '.#######.', '#kkkkkkk#', '#kkkkkkk#', '#ccccccc#', '.#######.'], |
| 349 |
}; |
| 350 |
const ICON_PAL = { a: [214, 150, 70], b: [120, 80, 60], w: [250, 250, 250], t: [226, 176, 96], p: [226, 130, 160], c: [250, 244, 230], s: [244, 214, 120], f: [226, 176, 90], r: [200, 40, 50], k: [100, 62, 44] }; |
| 351 |
const palOf = {}; for (const k in ICON_PAL) palOf[k] = rgb(...ICON_PAL[k]); |
| 352 |
function drawIcon(part, value, x, y) { |
| 353 |
if (part === 'drink') { |
| 354 |
const pal = { ...palOf, a: value === 'tea' ? rgb(214, 150, 70) : rgb(90, 56, 44), b: rgb(120, 80, 60) }; |
| 355 |
bitmap(value === 'tea' ? TEA_ICON : COFFEE_ICON, x, y, pal); |
| 356 |
} else if (part === 'milk') bitmap(value === 'milk' ? MILK_ICON : CROSS_ICON, x, y, palOf); |
| 357 |
else if (part === 'sugars') { |
| 358 |
if (value === 0) { bitmap(CROSS_ICON.map(r => r.slice(1, 8)).slice(1, 6), x + 1, y + 1, palOf); return; } |
| 359 |
const cube = (cx, cy) => { rect(cx, cy, 4, 4, LINE); rect(cx + 1, cy + 1, 2, 2, WHITE); }; |
| 360 |
if (value === 1) cube(x + 2, y + 3); |
| 361 |
else if (value === 2) { cube(x, y + 3); cube(x + 5, y + 3); } |
| 362 |
else { cube(x, y + 4); cube(x + 5, y + 4); cube(x + 2, y); } |
| 363 |
} else if (part === 'cake') bitmap(CAKE_ICONS[value], x, y + (value === 'sponge' ? 0 : 1), palOf); |
| 364 |
} |
| 365 |
function drawBubble(o, cx, bottom) { |
| 366 |
const w = 4 * 12 + 4, h = ICON_H + 8, x = clamp(Math.round(cx - w / 2), 2, W - w - 2), y = bottom - h - 4; |
| 367 |
rect(x, y, w, h, LINE); rect(x + 1, y + 1, w - 2, h - 2, WHITE); |
| 368 |
const tx = clamp(Math.round(cx), x + 4, x + w - 5); |
| 369 |
rect(tx - 2, y + h, 5, 1, LINE); rect(tx - 1, y + h + 1, 3, 1, LINE); rect(tx, y + h + 2, 1, 1, LINE); |
| 370 |
rect(tx - 1, y + h - 1, 3, 1, WHITE); rect(tx, y + h, 1, 1, WHITE); |
| 371 |
M.PARTS.forEach((p, k) => drawIcon(p, o[p], x + 4 + k * 12, y + 4)); |
| 372 |
} |
| 373 |
|
| 374 |
function drawPips(r, cx, y) { |
| 375 |
for (let k = 0; k < 4; k++) rect(Math.round(cx) - 7 + k * 4, y, 3, 3, r.perfect ? GOLD : k < r.matched ? GREEN : RED); |
| 376 |
} |
| 377 |
function drawMarker(cx, y, c) { rect(cx - 2, y, 5, 1, c); rect(cx - 1, y + 1, 3, 1, c); rect(cx, y + 2, 1, 1, c); } |
| 378 |
function drawOverlay() { |
| 379 |
const talking = P.status === 'playing' && P.phase === 'order'; |
| 380 |
for (let i = 0; i < P.guests; i++) { |
| 381 |
const [cx, cy] = pointOf(i), top = cy - 26; |
| 382 |
if (talking && P.shown === i + 1) drawBubble(P.orders[i], cx, top); |
| 383 |
else if (G.results[i]) drawPips(G.results[i], cx, top - 2); |
| 384 |
else if (G.sel === i) { drawMarker(cx, top - 4, LINE); drawMarker(cx, top - 5, PINK); } |
| 385 |
} |
| 386 |
if (G.sel !== null) { |
| 387 |
|
| 388 |
for (const id of STATIONS) { |
| 389 |
const [cx, cy] = pointOf(id), x = cx - 4, y = cy - 28; |
| 390 |
if (id === 'tea' || id === 'coffee') { if (G.cup.drink === id) { drawMarker(cx, y + 6, LINE); drawMarker(cx, y + 5, PINK); } } |
| 391 |
else if (id === 'milk' && G.cup.milk) drawIcon('milk', G.cup.milk, x, y); |
| 392 |
else if (id === 'sugar' && G.cup.sugars !== null) drawIcon('sugars', G.cup.sugars, x, y); |
| 393 |
} |
| 394 |
} |
| 395 |
if (G.keyed) { |
| 396 |
const f = G.focus, [cx, cy] = pointOf(f), box = typeof f === 'number' ? [26, 52, cy - 26] : [18, 26, cy - 13]; |
| 397 |
frameRect(cx - box[0] / 2 - 1, box[2] - 1, box[0] + 2, box[1] + 2, WHITE); |
| 398 |
frameRect(cx - box[0] / 2, box[2], box[0], box[1], LINE); |
| 399 |
} |
| 400 |
} |
| 401 |
function draw() { |
| 402 |
pix.set(bakedPix); |
| 403 |
zb.set(bakedZ); |
| 404 |
pendingShadows.length = 0; |
| 405 |
const t = STILL ? 0 : Math.floor(G.time * WIND_FPS) / WIND_FPS; |
| 406 |
for (let i = 0; i < P.guests; i++) drawGuest(i); |
| 407 |
drawCake(); |
| 408 |
drawLeaves(t); |
| 409 |
drawTufts(t); |
| 410 |
flushShadows(); |
| 411 |
applyShadows(bakedZ); |
| 412 |
shadeEdges(); |
| 413 |
drawOverlay(); |
| 414 |
ctx.putImageData(image, 0, 0); |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
function saveBest() { |
| 419 |
if (P.score <= G.best) return; |
| 420 |
G.best = P.score; |
| 421 |
try { localStorage.setItem(BEST_KEY, String(G.best)); } catch (e) { } |
| 422 |
} |
| 423 |
const SERVE_HELP = 'Everyone has ordered. Tap a guest, then the pots, milk, sugar and cake, then the guest again to serve.'; |
| 424 |
function announce(text) { say(G.carry ? `${G.carry} ${text}` : text); G.carry = ''; } |
| 425 |
function update(dt) { |
| 426 |
G.time += dt; |
| 427 |
if (P.status === 'playing' && P.phase === 'order') { |
| 428 |
const before = P, r = M.tick(P, dt); |
| 429 |
if (r.ok) { |
| 430 |
P = r.state; |
| 431 |
G.dirty = true; |
| 432 |
if (before.phase === 'order' && P.phase === 'serve') announce(SERVE_HELP); |
| 433 |
else if (P.shown !== before.shown) announce(`${P.shown === 1 ? P.line + '. ' : ''}Guest ${P.shown} orders ${orderText(P.orders[P.shown - 1])}.`); |
| 434 |
} |
| 435 |
} |
| 436 |
const wind = STILL ? 0 : Math.floor(G.time * WIND_FPS); |
| 437 |
if (wind !== G.wind) { G.wind = wind; G.dirty = true; } |
| 438 |
renderHud(); |
| 439 |
} |
| 440 |
function begin() { |
| 441 |
announce(`${P.line}. Guest 1 orders ${orderText(P.orders[0])}.`); |
| 442 |
} |
| 443 |
function restart() { |
| 444 |
P = M.start(Math.floor(Math.random() * 4294967296)); |
| 445 |
G.sel = null; G.cup = blankCup(); G.results = {}; G.focus = 0; G.dirty = true; |
| 446 |
begin(); |
| 447 |
} |
| 448 |
function serveGuest() { |
| 449 |
const g = G.sel, before = P, r = M.serve(P, g, G.cup); |
| 450 |
if (!r.ok) { say(`Cannot serve: ${r.why}.`); sound(150, .12, 'square'); return; } |
| 451 |
P = r.state; |
| 452 |
const hit = P.last; |
| 453 |
G.dirty = true; |
| 454 |
const text = `Guest ${g + 1}: ${hit.matched} of 4 right${hit.perfect ? ', a perfect cup' : ''}.`; |
| 455 |
sound(hit.perfect ? 784 : hit.matched >= 3 ? 560 : 180, .14, hit.matched >= 3 ? 'triangle' : 'sawtooth'); |
| 456 |
G.sel = null; G.cup = blankCup(); |
| 457 |
if (P.round !== before.round && P.status === 'playing') { |
| 458 |
G.results = {}; |
| 459 |
G.carry = text; |
| 460 |
announce(`${P.line}. Guest 1 orders ${orderText(P.orders[0])}.`); |
| 461 |
G.focus = 0; |
| 462 |
} else { |
| 463 |
G.results[g] = hit; |
| 464 |
say(P.status === 'playing' ? text : `${text} ${P.line}. Tap or press Enter to play again.`); |
| 465 |
} |
| 466 |
if (P.status !== 'playing') { G.overAt = G.time; saveBest(); sound(P.status === 'won' ? 880 : 110, .4, P.status === 'won' ? 'triangle' : 'sawtooth', .15); } |
| 467 |
} |
| 468 |
|
| 469 |
function choose(t) { |
| 470 |
ensureAudio(); |
| 471 |
G.dirty = true; |
| 472 |
if (P.status !== 'playing') { if (G.time - G.overAt > .6) restart(); return; } |
| 473 |
if (P.phase === 'order') { say('Still listening: wait for every order before serving.'); return; } |
| 474 |
if (typeof t === 'number') { |
| 475 |
if (P.served.includes(t)) { say(`${guestName(t)} is already served.`); return; } |
| 476 |
if (G.sel !== t) { G.sel = t; G.cup = blankCup(); say(`Serving ${guestName(t)}. Choose a pot, milk, sugar and cake, then choose the guest again.`); sound(400, .06, 'triangle'); return; } |
| 477 |
if (!cupDone(G.cup)) { say(`The cup is not ready: ${cupText(G.cup)}.`); sound(150, .1, 'square'); return; } |
| 478 |
serveGuest(); |
| 479 |
return; |
| 480 |
} |
| 481 |
if (G.sel === null) { say('Choose a guest first.'); return; } |
| 482 |
const c = G.cup; |
| 483 |
if (t === 'tea' || t === 'coffee') c.drink = t; |
| 484 |
else if (t === 'milk') c.milk = c.milk === 'milk' ? 'none' : 'milk'; |
| 485 |
else if (t === 'sugar') c.sugars = c.sugars === null ? 1 : (c.sugars + 1) % (M.SUGARS.length); |
| 486 |
else c.cake = M.CAKES[(c.cake === null ? 0 : M.CAKES.indexOf(c.cake) + 1) % M.CAKES.length]; |
| 487 |
sound(300 + 40 * STATIONS.indexOf(t), .06, 'triangle'); |
| 488 |
say(`${cupText(c)}.`); |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
const point = e => { const r = canvas.getBoundingClientRect(); return [(e.clientX - r.left) / r.width * W, (e.clientY - r.top) / r.height * H]; }; |
| 493 |
canvas.addEventListener('pointerdown', e => { |
| 494 |
canvas.focus({ preventScroll: true }); |
| 495 |
G.keyed = false; |
| 496 |
const [sx, sy] = point(e), t = targetAt(sx, sy); |
| 497 |
if (t === null) { ensureAudio(); if (P.status !== 'playing') choose(0); else say(P.phase === 'order' ? 'Listen to each guest in turn.' : 'Tap a guest, then the pots, milk, sugar and cake.'); G.dirty = true; return; } |
| 498 |
G.focus = t; |
| 499 |
choose(t); |
| 500 |
}); |
| 501 |
canvas.addEventListener('contextmenu', e => e.preventDefault()); |
| 502 |
canvas.addEventListener('keydown', e => { |
| 503 |
if (e.ctrlKey || e.metaKey || e.altKey) return; |
| 504 |
const list = ring(); |
| 505 |
if (G.focus !== 0 && !list.includes(G.focus)) G.focus = 0; |
| 506 |
const at = list.indexOf(G.focus); |
| 507 |
let step = 0; |
| 508 |
if (e.key === 'ArrowRight' || e.key === 'ArrowDown') step = 1; |
| 509 |
else if (e.key === 'ArrowLeft' || e.key === 'ArrowUp') step = -1; |
| 510 |
else if (e.key === 'Tab') { |
| 511 |
step = e.shiftKey ? -1 : 1; |
| 512 |
if (G.keyed && (at + step < 0 || at + step >= list.length)) { G.keyed = false; G.dirty = true; return; } |
| 513 |
} else if (e.key !== 'Enter' && e.key !== ' ') return; |
| 514 |
e.preventDefault(); |
| 515 |
G.dirty = true; |
| 516 |
if (step) { |
| 517 |
G.focus = list[(at + step + list.length) % list.length]; |
| 518 |
G.keyed = true; |
| 519 |
const f = G.focus; |
| 520 |
say(typeof f === 'number' ? `${guestName(f)}${P.served.includes(f) ? ', served' : ''}` : STATION_NAME[f]); |
| 521 |
return; |
| 522 |
} |
| 523 |
if (e.repeat) return; |
| 524 |
G.keyed = true; |
| 525 |
choose(G.focus); |
| 526 |
}); |
| 527 |
|
| 528 |
|
| 529 |
let audio = null, muted = false; |
| 530 |
function ensureAudio() { |
| 531 |
if (audio) { if (audio.state === 'suspended') audio.resume(); return; } |
| 532 |
const AC = window.AudioContext || window.webkitAudioContext; |
| 533 |
if (AC) audio = new AC(); |
| 534 |
} |
| 535 |
function sound(freq, life, type, delay = 0) { |
| 536 |
if (!audio || muted) return; |
| 537 |
const now = audio.currentTime + delay, gain = audio.createGain(), osc = audio.createOscillator(); |
| 538 |
osc.type = type; |
| 539 |
osc.frequency.setValueAtTime(freq, now); |
| 540 |
gain.gain.setValueAtTime(.0001, now); |
| 541 |
gain.gain.exponentialRampToValueAtTime(.06, now + .01); |
| 542 |
gain.gain.exponentialRampToValueAtTime(.0005, now + life); |
| 543 |
osc.connect(gain).connect(audio.destination); |
| 544 |
osc.start(now); osc.stop(now + life + .02); |
| 545 |
} |
| 546 |
const muteButton = document.getElementById('mute'); |
| 547 |
muteButton.addEventListener('click', () => { |
| 548 |
muted = !muted; |
| 549 |
muteButton.textContent = muted ? 'sound off' : 'sound on'; |
| 550 |
muteButton.setAttribute('aria-pressed', String(muted)); |
| 551 |
}); |
| 552 |
|
| 553 |
|
| 554 |
addEventListener('resize', fit); |
| 555 |
fit(); |
| 556 |
bake(); |
| 557 |
renderHud(); |
| 558 |
begin(); |
| 559 |
canvas.focus({ preventScroll: true }); |
| 560 |
let last = performance.now(); |
| 561 |
function frame(now) { |
| 562 |
update(Math.min(.04, (now - last) / 1000)); |
| 563 |
last = now; |
| 564 |
if (G.dirty) { G.dirty = false; draw(); } |
| 565 |
requestAnimationFrame(frame); |
| 566 |
} |
| 567 |
requestAnimationFrame(frame); |
| 568 |
</script> |
| 569 |
</body> |
| 570 |
</html> |