|
|
@@ -0,0 +1,465 @@ |
| 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>Market Scale</title> |
| 7 |
<style> |
| 8 |
:root { --ink: #1c1b19; --muted: #8b877f; --paper: #fff; } |
| 9 |
* { box-sizing: border-box; } |
| 10 |
html, body { height: 100%; margin: 0; background: var(--paper); color: var(--ink); } |
| 11 |
body { font: 13px/1.35 ui-monospace, "Cascadia Mono", Consolas, Menlo, monospace; overflow: hidden; } |
| 12 |
#stage { position: fixed; inset: 0; display: grid; place-items: center; } |
| 13 |
canvas { display: block; image-rendering: pixelated; touch-action: none; cursor: pointer; outline: none; } |
| 14 |
.hud { position: fixed; left: max(16px, env(safe-area-inset-left)); right: max(16px, env(safe-area-inset-right)); display: flex; justify-content: space-between; gap: 16px; pointer-events: none; } |
| 15 |
#scores { position: fixed; top: max(14px, env(safe-area-inset-top)); left: max(16px, env(safe-area-inset-left)); display: grid; gap: 2px; pointer-events: none; } |
| 16 |
#scores b { font-size: 16px; font-weight: 600; font-variant-numeric: tabular-nums; min-width: 5ch; } |
| 17 |
#scores span { color: var(--muted); font-variant-numeric: tabular-nums; min-height: 1.35em; } |
| 18 |
#bottom { bottom: max(14px, env(safe-area-inset-bottom)); align-items: flex-end; } |
| 19 |
#notes { display: grid; gap: 2px; } |
| 20 |
#notes span { color: var(--muted); min-height: 1.35em; } |
| 21 |
button { pointer-events: auto; font: inherit; color: var(--muted); background: none; border: 0; padding: 10px; margin: -10px; cursor: pointer; } |
| 22 |
button:hover, button:focus-visible { color: var(--ink); } |
| 23 |
</style> |
| 24 |
</head> |
| 25 |
<body> |
| 26 |
<div id="stage"><canvas id="c" tabindex="0" aria-label="Market Scale. Drag fruit from the crate into the pan, or tap it to put it in, and tap a fruit in the pan to take it out. With the keyboard, arrows pick a fruit and enter moves it. Match the weight on the chalk slate, then tap the bell or press space to sell. Five customers, fifteen seconds each."></canvas></div> |
| 27 |
<div id="scores"><b id="score">0</b><span id="meta"></span></div> |
| 28 |
<div class="hud" id="bottom"> |
| 29 |
<div id="notes"><span id="status" role="status" aria-live="polite"></span><span>apple 150 · pear 200 · lemon 100 · melon 500 g</span></div> |
| 30 |
<button id="mute" type="button" aria-pressed="false">sound on</button> |
| 31 |
</div> |
| 32 |
<script src="../shell.js"></script> |
| 33 |
<script> |
| 34 |
PixelPlays.mount({ |
| 35 |
id: 'market-scale', |
| 36 |
rules: [ |
| 37 |
'Drag fruit into the pan, or tap it, to match the weight on the chalk slate; tap one in the pan to take it out.', |
| 38 |
'Arrows pick a fruit, enter moves it. Tap the bell or press space to sell.', |
| 39 |
'Exact scores 3, within 50 g scores 1, and a miss or the 15-second clock running out scores 0.', |
| 40 |
'Five customers make a round.', |
| 41 |
], |
| 42 |
}); |
| 43 |
</script> |
| 44 |
<script>window.PP_SCENE = { originY: 18, shadowX: 1.1, shadowY: -.3 };</script> |
| 45 |
<script src="../engine.js"></script> |
| 46 |
<script src="scale.js"></script> |
| 47 |
<script> |
| 48 |
'use strict'; |
| 49 |
|
| 50 |
const BEST_KEY = 'pixel-plays-market-scale-best'; |
| 51 |
const HALF = 11, SQ = Math.SQRT1_2, STALL = { lo: -8, hi: -1 }, PAN = { x: 1.5, y: -4.5, radius: 3, rest: 3 }; |
| 52 |
const SAND = [[196, 170, 128], [206, 182, 138], [216, 194, 150], [226, 206, 164]]; |
| 53 |
const WOOD = [168, 120, 78], WOOD_DARK = [112, 78, 54], STRAW = [226, 196, 108], BRASS = [232, 178, 62], CHAIN = [150, 128, 96]; |
| 54 |
const AWN_RED = [214, 72, 52], AWN_CREAM = [246, 236, 214]; |
| 55 |
const KIND_COLOR = { apple: [206, 48, 46], pear: [148, 178, 62], lemon: [248, 212, 54], melon: [66, 138, 72] }; |
| 56 |
const STEM = [92, 66, 44]; |
| 57 |
const SLATE = rgb(40, 56, 50), SLATE_LIGHT = rgb(52, 70, 62), CHALK = rgb(244, 240, 226), FRAME = rgb(...WOOD), FRAME_DARK = rgb(...WOOD_DARK); |
| 58 |
const FACE = rgb(250, 244, 226), BRASS_PX = rgb(...BRASS), BRASS_DARK = rgb(176, 124, 40), NEEDLE = rgb(176, 32, 32), TICK = rgb(60, 52, 44); |
| 59 |
const CRATE_AT = { x0: -5.3, x1: -1.1, y0: 2.8, y1: 9.9 }; |
| 60 |
const BELL = { x: 3, y: 1.5 }; |
| 61 |
const roofZ = (x, y) => 7.2 + .05 * (-(x + y) - 2); |
| 62 |
|
| 63 |
const uv = (du, dv) => [(du + dv) * SQ, (dv - du) * SQ]; |
| 64 |
|
| 65 |
|
| 66 |
function groundColor(x, y, ix, iy) { |
| 67 |
const edge = Math.pow(Math.pow(Math.abs(x) / HALF, 4) + Math.pow(Math.abs(y) / HALF, 4), .25); |
| 68 |
const rough = edge + (noise(x * 1.3, y * 1.3) - .5) * .1; |
| 69 |
if (rough > 1.04) return 0; |
| 70 |
if (rough > .94 && hash(ix, iy) < (rough - .94) / .1) return 0; |
| 71 |
const t = noise(x * .8, y * .8) * 1.1 + (hash(ix, iy) - .5) * .5 + (1 - edge) * .14 - .1; |
| 72 |
return rgb(...SAND[clamp(Math.floor(t * 4.2), 0, 3)]); |
| 73 |
} |
| 74 |
|
| 75 |
function post(x, y, top) { |
| 76 |
capsule([x, y, 0], [x, y, top], .22, .22, () => WOOD_DARK, 'rough'); |
| 77 |
castShadow(x, y, top, .35, 1, .2); |
| 78 |
} |
| 79 |
function awning() { |
| 80 |
const { lo, hi } = STALL; |
| 81 |
|
| 82 |
for (let k = -20; k <= 20; k++) { |
| 83 |
const w = k * .5; |
| 84 |
const y0 = Math.max(lo, lo - w), y1 = Math.min(hi, hi - w); |
| 85 |
if (y1 <= y0) continue; |
| 86 |
const a = [y0 + w, y0, 0], b = [y1 + w, y1, 0]; |
| 87 |
a[2] = roofZ(a[0], a[1]); b[2] = roofZ(b[0], b[1]); |
| 88 |
const color = Math.floor((k + 40) / 2) % 2 ? AWN_CREAM : AWN_RED; |
| 89 |
capsule(a, b, .34, .34, () => color, 'solid'); |
| 90 |
} |
| 91 |
|
| 92 |
for (const edge of [[hi, null], [null, hi]]) for (let t = lo; t <= hi; t += .7) { |
| 93 |
const x = edge[0] == null ? t : edge[0], y = edge[1] == null ? t : edge[1]; |
| 94 |
sphere(x, y, roofZ(x, y) - .3, .42, Math.floor((t - lo) / .7) % 2 ? AWN_CREAM : AWN_RED, 'solid'); |
| 95 |
} |
| 96 |
} |
| 97 |
function crate() { |
| 98 |
const c = CRATE_AT; |
| 99 |
box(c.x0, c.y0, 0, c.x1, c.y1, 1, WOOD, 3); |
| 100 |
box(c.x0 + .3, c.y0 + .3, 0, c.x1 - .3, c.y1 - .3, 1.12, STRAW, 5); |
| 101 |
castShadow((c.x0 + c.x1) / 2, (c.y0 + c.y1) / 2, 1, 3.4, 1, .3); |
| 102 |
} |
| 103 |
function bake() { |
| 104 |
zb.fill(-1e9); |
| 105 |
pix.fill(rgb(255, 255, 255)); |
| 106 |
for (let iy = 0; iy < H; iy++) for (let ix = 0; ix < W; ix++) { |
| 107 |
const g = unproject(ix + .5, iy + .5), i = iy * W + ix, c = groundColor(g.x, g.y, ix, iy); |
| 108 |
if (!c) continue; |
| 109 |
pix[i] = c; ground[i] = 1; |
| 110 |
zb[i] = (g.x + g.y) / R2 * COS; |
| 111 |
} |
| 112 |
groundZ.set(zb); |
| 113 |
const { lo, hi } = STALL; |
| 114 |
|
| 115 |
box(lo + .3, lo + .3, 0, hi - .3, lo + 1.5, 2, WOOD, 9); |
| 116 |
castShadow((lo + hi) / 2, lo + 1, 2, 3.4, 1, .3); |
| 117 |
post(lo, lo, roofZ(lo, lo) - .2); post(hi, lo, roofZ(hi, lo) - .2); |
| 118 |
post(lo, hi, roofZ(lo, hi) - .2); post(hi, hi, roofZ(hi, hi) - .2); |
| 119 |
awning(); |
| 120 |
|
| 121 |
capsule([hi, PAN.y, roofZ(hi, PAN.y) - .5], [PAN.x, PAN.y, roofZ(hi, PAN.y) - .5], .16, .16, () => WOOD_DARK, 'rough'); |
| 122 |
crate(); |
| 123 |
|
| 124 |
box(BELL.x - .8, BELL.y - .8, 0, BELL.x + .8, BELL.y + .8, 1.4, WOOD, 11); |
| 125 |
castShadow(BELL.x, BELL.y, 1.4, 1.2, 1, .2); |
| 126 |
bakeOcclusion(); |
| 127 |
staticShadow.set(shadowMask); |
| 128 |
applyShadows(groundZ); |
| 129 |
bakedPix.set(pix); |
| 130 |
bakedZ.set(zb); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
const best0 = (() => { try { return Number(localStorage.getItem(BEST_KEY)) || 0; } catch (e) { return 0; } })(); |
| 135 |
let G = MarketScale.newState(best0), time = 0, status = '', overT = 0, lastPhase = 'ready', ringT = 9, drag = null, lastTick = 0; |
| 136 |
const nodes = { score: document.getElementById('score'), meta: document.getElementById('meta'), status: document.getElementById('status') }; |
| 137 |
let shown = ''; |
| 138 |
function renderHud() { |
| 139 |
const text = [G.score, G.best, G.customer, Math.ceil(G.clock), G.total, G.phase].join('|'); |
| 140 |
if (text === shown) return; |
| 141 |
shown = text; |
| 142 |
nodes.score.textContent = String(G.score); |
| 143 |
const parts = []; |
| 144 |
if (G.best) parts.push('best ' + G.best); |
| 145 |
if (G.phase === 'serve' || G.phase === 'sold') parts.push('customer ' + (G.customer + 1) + '/' + MarketScale.CUSTOMERS); |
| 146 |
if (G.phase === 'serve') parts.push(Math.ceil(G.clock) + 's', 'pan ' + G.total + ' g'); |
| 147 |
nodes.meta.textContent = parts.join(' · '); |
| 148 |
} |
| 149 |
function setStatus(text) { |
| 150 |
if (text === status) return; |
| 151 |
status = text; |
| 152 |
nodes.status.textContent = text; |
| 153 |
} |
| 154 |
function statusFor() { |
| 155 |
if (G.phase === 'ready') return 'tap the bell or press space to open the stall'; |
| 156 |
if (G.phase === 'over') return G.score + ' of ' + MarketScale.CUSTOMERS * 3 + (G.record ? ' · new best' : '') + ' · tap to play again'; |
| 157 |
if (G.phase === 'sold') { |
| 158 |
const p = G.sale.points, how = G.sale.late ? 'too slow' : p === 3 ? 'exact' : p === 1 ? 'close, ' + Math.abs(G.sale.total - G.sale.target) + ' g off' : 'missed by ' + Math.abs(G.sale.total - G.sale.target) + ' g'; |
| 159 |
return how + ' · +' + p; |
| 160 |
} |
| 161 |
return 'want ' + G.target + ' g · pan ' + G.total + ' g · tap the bell to sell'; |
| 162 |
} |
| 163 |
function press() { |
| 164 |
ensureAudio(); |
| 165 |
if (G.phase === 'ready') { MarketScale.start(G); ringT = 0; sound('bell'); return; } |
| 166 |
if (G.phase === 'over') { if (overT > .8) { G = MarketScale.restart(G); lastPhase = 'ready'; } return; } |
| 167 |
if (MarketScale.sell(G)) { ringT = 0; sound('bell'); } |
| 168 |
} |
| 169 |
function move(i) { |
| 170 |
ensureAudio(); |
| 171 |
if (MarketScale.toggle(G, i)) sound('thud'); |
| 172 |
} |
| 173 |
function update(dt) { |
| 174 |
time += dt; |
| 175 |
ringT += dt; |
| 176 |
MarketScale.step(G, dt); |
| 177 |
if (G.phase !== lastPhase) { |
| 178 |
if (G.phase === 'sold') { ringT = 0; sound(G.sale.points ? 'coin' : 'miss'); } |
| 179 |
if (G.phase === 'over') { |
| 180 |
overT = 0; drag = null; |
| 181 |
if (G.record) try { localStorage.setItem(BEST_KEY, String(G.best)); } catch (e) { } |
| 182 |
} |
| 183 |
lastPhase = G.phase; |
| 184 |
} |
| 185 |
if (G.phase === 'over') overT += dt; |
| 186 |
const tick = Math.ceil(G.clock); |
| 187 |
if (G.phase === 'serve' && tick <= 3 && tick !== lastTick) sound('tick'); |
| 188 |
lastTick = tick; |
| 189 |
setStatus(statusFor()); |
| 190 |
renderHud(); |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
const slotAt = i => ({ x: CRATE_AT.x0 + 1.2 + 1.9 * Math.floor(i / 4), y: CRATE_AT.y0 + 1.1 + 1.7 * (i % 4) }); |
| 195 |
const RADIUS = { apple: .85, pear: .8, lemon: .7, melon: 1.3 }; |
| 196 |
const panSink = () => Math.round(MarketScale.sink(G.needle) * 12); |
| 197 |
const panZ = () => PAN.rest - panSink() / (S * COS); |
| 198 |
|
| 199 |
function panSpot(k, kind) { |
| 200 |
const z = panZ() + .25; |
| 201 |
if (k === 0) return { x: PAN.x, y: PAN.y, z: z + RADIUS[kind] }; |
| 202 |
if (k < 7) { |
| 203 |
const a = (k - 1) * Math.PI / 3 + .4, [dx, dy] = uv(Math.cos(a) * 2.1, Math.sin(a) * 2.1); |
| 204 |
return { x: PAN.x + dx, y: PAN.y + dy, z: z + RADIUS[kind] }; |
| 205 |
} |
| 206 |
return { x: PAN.x, y: PAN.y, z: z + RADIUS[kind] + 1.3 }; |
| 207 |
} |
| 208 |
|
| 209 |
function spotOf(i) { |
| 210 |
const kind = MarketScale.CRATE[i], k = G.pan.indexOf(i); |
| 211 |
if (k >= 0) return panSpot(k, kind); |
| 212 |
const slot = slotAt(i); |
| 213 |
return { x: slot.x, y: slot.y, z: 1.12 + RADIUS[kind] }; |
| 214 |
} |
| 215 |
const screenOf = i => { const p = spotOf(i); return project(p.x, p.y, p.z); }; |
| 216 |
const panScreen = () => project(PAN.x, PAN.y, panZ() + 1); |
| 217 |
const bellScreen = () => project(BELL.x, BELL.y, 2); |
| 218 |
|
| 219 |
|
| 220 |
function drawFruit(kind, x, y, z, shadow = true) { |
| 221 |
const r = RADIUS[kind], color = KIND_COLOR[kind]; |
| 222 |
if (kind === 'apple') { |
| 223 |
sphere(x, y, z, r, color, 'solid'); |
| 224 |
capsule([x, y, z + r * .8], [x, y, z + r * 1.25], .08, .08, () => STEM, 'solid'); |
| 225 |
} else if (kind === 'pear') { |
| 226 |
sphere(x, y, z - .1, r, color, 'solid'); |
| 227 |
sphere(x, y, z + r * .85, r * .6, color, 'solid'); |
| 228 |
capsule([x, y, z + r * 1.3], [x, y, z + r * 1.65], .07, .07, () => STEM, 'solid'); |
| 229 |
} else if (kind === 'lemon') { |
| 230 |
capsule([x - .22, y + .22, z], [x + .22, y - .22, z], r, r, () => color, 'solid'); |
| 231 |
sphere(x + .6, y - .6, z, .22, color, 'solid'); |
| 232 |
} else { |
| 233 |
sphere(x, y, z, r, color, 'rough'); |
| 234 |
sphere(x, y, z + r * .55, r * .5, [110, 180, 100], 'rough'); |
| 235 |
} |
| 236 |
if (shadow) castShadow(x, y, z, r * .8, 1, .2); |
| 237 |
} |
| 238 |
function drawPan() { |
| 239 |
const z = panZ(), top = PAN.rest + 4.2; |
| 240 |
|
| 241 |
for (const a of [.6, 2.7, 4.8]) { |
| 242 |
const [dx, dy] = uv(Math.cos(a) * PAN.radius, Math.sin(a) * PAN.radius * .9); |
| 243 |
capsule([PAN.x, PAN.y, top], [PAN.x + dx, PAN.y + dy, z + .1], .045, .045, () => CHAIN, 'solid'); |
| 244 |
} |
| 245 |
|
| 246 |
for (let t = -PAN.radius; t <= PAN.radius; t += .4) { |
| 247 |
const half = Math.sqrt(Math.max(0, PAN.radius * PAN.radius - t * t)), [ax, ay] = uv(-half, t), [bx, by] = uv(half, t); |
| 248 |
capsule([PAN.x + ax, PAN.y + ay, z - .15], [PAN.x + bx, PAN.y + by, z - .15], .24, .24, () => BRASS, 'solid'); |
| 249 |
} |
| 250 |
for (let k = 0; k < 28; k++) { |
| 251 |
const a = k / 28 * Math.PI * 2, [dx, dy] = uv(Math.cos(a) * PAN.radius, Math.sin(a) * PAN.radius); |
| 252 |
sphere(PAN.x + dx, PAN.y + dy, z + .05, .2, [244, 204, 100], 'solid'); |
| 253 |
} |
| 254 |
castShadow(PAN.x, PAN.y, z, PAN.radius * .9, 1, .3); |
| 255 |
} |
| 256 |
|
| 257 |
function drawFringe(t) { |
| 258 |
const { lo, hi } = STALL; |
| 259 |
let n = 0; |
| 260 |
for (const edge of [[hi, null], [null, hi]]) for (let s = lo; s <= hi; s += .7, n++) { |
| 261 |
const sway = STILL ? 0 : Math.round(Math.sin(t * 2.2 + n * .55) * 1.1); |
| 262 |
const [dx, dy] = uv(sway * .1, 0); |
| 263 |
const x = (edge[0] == null ? s : edge[0]) + dx, y = (edge[1] == null ? s : edge[1]) + dy; |
| 264 |
sphere(x, y, roofZ(x, y) - .9, .3, n % 2 ? AWN_CREAM : AWN_RED, 'solid'); |
| 265 |
sphere(x + dx * .5, y + dy * .5, roofZ(x, y) - 1.3, .22, n % 2 ? AWN_CREAM : AWN_RED, 'solid'); |
| 266 |
} |
| 267 |
} |
| 268 |
function drawBell() { |
| 269 |
const press = ringT < .25 ? .2 : 0; |
| 270 |
sphere(BELL.x, BELL.y, 1.4 + .2 - press, .8, BRASS, 'solid'); |
| 271 |
capsule([BELL.x - .8, BELL.y + .8, 1.4], [BELL.x + .8, BELL.y - .8, 1.4], .13, .13, () => [244, 204, 100], 'solid'); |
| 272 |
sphere(BELL.x, BELL.y, 1.4 + 1.05 - press, .2, [244, 204, 100], 'solid'); |
| 273 |
castShadow(BELL.x, BELL.y, 1.8, .8, 1, .2); |
| 274 |
} |
| 275 |
|
| 276 |
const GLYPHS = { |
| 277 |
0: '111101101101111', 1: '010110010010111', 2: '111001111100111', 3: '111001111001111', 4: '101101111001001', |
| 278 |
5: '111100111001111', 6: '111100111101111', 7: '111001001001001', 8: '111101111101111', 9: '111101111001111', |
| 279 |
'+': '000010111010000', '?': '111001010000010', |
| 280 |
}; |
| 281 |
function px(x, y, color) { |
| 282 |
if (x >= 0 && y >= 0 && x < W && y < H) pix[y * W + x] = color; |
| 283 |
} |
| 284 |
function rect(x0, y0, x1, y1, color) { |
| 285 |
for (let y = y0; y < y1; y++) for (let x = x0; x < x1; x++) px(x, y, color); |
| 286 |
} |
| 287 |
function text(str, left, top, scale, color) { |
| 288 |
let x = left; |
| 289 |
for (const ch of str) { |
| 290 |
const g = GLYPHS[ch] || ''; |
| 291 |
for (let k = 0; k < g.length; k++) if (g[k] === '1') rect(x + (k % 3) * scale, top + Math.floor(k / 3) * scale, x + (k % 3 + 1) * scale, top + (Math.floor(k / 3) + 1) * scale, color); |
| 292 |
x += 4 * scale; |
| 293 |
} |
| 294 |
} |
| 295 |
const textWidth = (str, scale) => str.length * 4 * scale - scale; |
| 296 |
function line(x0, y0, x1, y1, color, thick) { |
| 297 |
const n = Math.max(Math.abs(x1 - x0), Math.abs(y1 - y0), 1); |
| 298 |
for (let k = 0; k <= n; k++) { |
| 299 |
const x = Math.round(x0 + (x1 - x0) * k / n), y = Math.round(y0 + (y1 - y0) * k / n); |
| 300 |
for (let a = 0; a < thick; a++) for (let b = 0; b < thick; b++) px(x + a, y + b, color); |
| 301 |
} |
| 302 |
} |
| 303 |
const SLATE_BOX = { x: 84, y: 88, w: 78, h: 44 }; |
| 304 |
function drawSlate() { |
| 305 |
const { x, y, w, h } = SLATE_BOX; |
| 306 |
for (const lx of [x + 6, x + w - 8]) rect(lx, y + h + 3, lx + 3, y + h + 17, FRAME_DARK); |
| 307 |
rect(x - 3, y - 3, x + w + 3, y + h + 3, FRAME_DARK); |
| 308 |
rect(x - 2, y - 2, x + w + 2, y + h + 1, FRAME); |
| 309 |
for (let yy = y; yy < y + h; yy++) for (let xx = x; xx < x + w; xx++) px(xx, yy, hash(xx, yy) > .93 ? SLATE_LIGHT : SLATE); |
| 310 |
const body = G.phase === 'ready' || G.phase === 'over' ? '?' : String(G.target), size = body.length > 3 ? 3 : 4; |
| 311 |
text(body, x + Math.round((w - textWidth(body, size)) / 2), y + 5, size, CHALK); |
| 312 |
if (G.phase === 'sold') { |
| 313 |
const mark = '+' + G.sale.points; |
| 314 |
text(mark, x + Math.round((w - textWidth(mark, 2)) / 2), y + 29, 2, G.sale.points === 3 ? rgb(255, 214, 10) : CHALK); |
| 315 |
} else if (G.phase === 'serve') { |
| 316 |
|
| 317 |
const len = Math.round((w - 12) * G.clock / MarketScale.CLOCK); |
| 318 |
rect(x + 6, y + 33, x + 6 + len, y + 35, G.clock < 4 ? rgb(236, 120, 96) : CHALK); |
| 319 |
} |
| 320 |
} |
| 321 |
function drawDial() { |
| 322 |
const hub = project(PAN.x, PAN.y, PAN.rest + 4.2), cx = Math.round(hub.sx), cy = Math.round(hub.sy) - 12, R = 15; |
| 323 |
for (let yy = -R - 2; yy <= R + 2; yy++) for (let xx = -R - 2; xx <= R + 2; xx++) { |
| 324 |
const d = Math.hypot(xx + .5, yy + .5); |
| 325 |
if (d <= R) px(cx + xx, cy + yy, FACE); |
| 326 |
else if (d <= R + 1.5) px(cx + xx, cy + yy, BRASS_PX); |
| 327 |
else if (d <= R + 2.5) px(cx + xx, cy + yy, BRASS_DARK); |
| 328 |
} |
| 329 |
const pivotY = cy + 5; |
| 330 |
for (let k = 0; k <= 8; k++) { |
| 331 |
const a = (k / 8 * 2 - 1) * Math.PI / 3, s = Math.sin(a), c = Math.cos(a); |
| 332 |
line(Math.round(cx + s * 10), Math.round(pivotY - c * 10), Math.round(cx + s * 13), Math.round(pivotY - c * 13), TICK, 1); |
| 333 |
} |
| 334 |
const a = MarketScale.needleAngle(G.needle); |
| 335 |
line(cx, pivotY, Math.round(cx + Math.sin(a) * 11), Math.round(pivotY - Math.cos(a) * 11), NEEDLE, 2); |
| 336 |
rect(cx - 1, pivotY - 1, cx + 2, pivotY + 2, TICK); |
| 337 |
} |
| 338 |
function drawMarker(i) { |
| 339 |
const p = spotOf(i), top = project(p.x, p.y, p.z + RADIUS[MarketScale.CRATE[i]] + 1); |
| 340 |
drawArrow(top.sx, top.sy, time); |
| 341 |
} |
| 342 |
function draw() { |
| 343 |
pix.set(bakedPix); |
| 344 |
zb.set(bakedZ); |
| 345 |
const windT = STILL ? 0 : Math.floor(time * WIND_FPS) / WIND_FPS; |
| 346 |
drawPan(); |
| 347 |
drawBell(); |
| 348 |
drawFringe(windT); |
| 349 |
MarketScale.CRATE.forEach((kind, i) => { |
| 350 |
if (drag && drag.i === i) return; |
| 351 |
const p = spotOf(i); |
| 352 |
drawFruit(kind, p.x, p.y, p.z); |
| 353 |
}); |
| 354 |
applyShadows(bakedZ); |
| 355 |
shadeEdges(); |
| 356 |
drawSlate(); |
|
|
|
| 357 |
drawDial(); |
| 358 |
if (drag) { |
| 359 |
|
| 360 |
const kind = MarketScale.CRATE[drag.i], lift = 9, g = unproject(drag.sx, drag.sy - 26 + lift * COS * S); |
| 361 |
drawFruit(kind, g.x, g.y, lift, false); |
| 362 |
} |
| 363 |
if (G.kb && G.phase === 'serve') drawMarker(G.sel); |
| 364 |
if (G.phase === 'ready' || G.phase === 'over') { const b = bellScreen(); drawArrow(b.sx, b.sy - 10, time); } |
| 365 |
ctx.putImageData(image, 0, 0); |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
const at = e => { |
| 370 |
const box = canvas.getBoundingClientRect(); |
| 371 |
return { sx: (e.clientX - box.left) * W / box.width, sy: (e.clientY - box.top) * H / box.height }; |
| 372 |
}; |
| 373 |
const near = (p, q, r) => Math.hypot(p.sx - q.sx, p.sy - q.sy) <= r; |
| 374 |
function fruitAt(p) { |
| 375 |
let best = -1, bestD = 1e9; |
| 376 |
MarketScale.CRATE.forEach((kind, i) => { |
| 377 |
const s = screenOf(i), d = Math.hypot(p.sx - s.sx, p.sy - s.sy); |
| 378 |
if (d <= RADIUS[kind] * S + 5 && d < bestD) { best = i; bestD = d; } |
| 379 |
}); |
| 380 |
return best; |
| 381 |
} |
| 382 |
canvas.addEventListener('pointerdown', e => { |
| 383 |
canvas.focus({ preventScroll: true }); |
| 384 |
const p = at(e); |
| 385 |
if (G.phase !== 'serve' || near(p, bellScreen(), 16)) { press(); return; } |
| 386 |
const i = fruitAt(p); |
| 387 |
if (i < 0) return; |
| 388 |
ensureAudio(); |
| 389 |
canvas.setPointerCapture(e.pointerId); |
| 390 |
G.sel = i; |
| 391 |
if (G.pan.includes(i)) { move(i); return; } |
| 392 |
drag = { i, sx: p.sx, sy: p.sy, from: p, moved: false }; |
| 393 |
}); |
| 394 |
canvas.addEventListener('pointermove', e => { |
| 395 |
if (!drag) return; |
| 396 |
const p = at(e); |
| 397 |
drag.sx = p.sx; drag.sy = p.sy; |
| 398 |
if (!near(p, drag.from, 5)) drag.moved = true; |
| 399 |
}); |
| 400 |
function drop() { |
| 401 |
if (!drag) return; |
| 402 |
const d = drag, p = { sx: d.sx, sy: d.sy }; |
| 403 |
drag = null; |
| 404 |
|
| 405 |
if (!d.moved || near(p, panScreen(), 42)) move(d.i); |
| 406 |
} |
| 407 |
canvas.addEventListener('pointerup', drop); |
| 408 |
canvas.addEventListener('pointercancel', () => { drag = null; }); |
| 409 |
addEventListener('blur', () => { drag = null; }); |
| 410 |
canvas.addEventListener('keydown', e => { |
| 411 |
const by = { ArrowRight: 1, ArrowLeft: -1, ArrowDown: 4, ArrowUp: -4 }[e.key]; |
| 412 |
if (by) { e.preventDefault(); if (G.phase === 'serve') MarketScale.select(G, by); return; } |
| 413 |
if (e.key === ' ') { e.preventDefault(); if (!e.repeat) press(); return; } |
| 414 |
if (e.key === 'Enter') { e.preventDefault(); if (!e.repeat && G.phase === 'serve') { G.kb = true; move(G.sel); } else if (!e.repeat) press(); } |
| 415 |
}); |
| 416 |
|
| 417 |
|
| 418 |
let audio = null, muted = false; |
| 419 |
function ensureAudio() { |
| 420 |
if (audio) { if (audio.state === 'suspended') audio.resume(); return; } |
| 421 |
const AC = window.AudioContext || window.webkitAudioContext; |
| 422 |
if (AC) audio = new AC(); |
| 423 |
} |
| 424 |
function tone(type, from, to, len, vol) { |
| 425 |
const now = audio.currentTime, gain = audio.createGain(), osc = audio.createOscillator(); |
| 426 |
osc.type = type; |
| 427 |
osc.frequency.setValueAtTime(from, now); |
| 428 |
osc.frequency.exponentialRampToValueAtTime(to, now + len); |
| 429 |
gain.gain.setValueAtTime(vol, now); |
| 430 |
gain.gain.exponentialRampToValueAtTime(.0005, now + len); |
| 431 |
osc.connect(gain).connect(audio.destination); |
| 432 |
osc.start(now); osc.stop(now + len + .01); |
| 433 |
} |
| 434 |
function sound(kind) { |
| 435 |
if (!audio || muted) return; |
| 436 |
if (kind === 'bell') { tone('sine', 1320, 1300, .5, .18); tone('sine', 1980, 1960, .35, .06); } |
| 437 |
else if (kind === 'thud') tone('triangle', 220, 120, .1, .16); |
| 438 |
else if (kind === 'coin') { tone('square', 880, 880, .08, .05); setTimeout(() => { if (audio) tone('square', 1320, 1320, .16, .05); }, 80); } |
| 439 |
else if (kind === 'miss') tone('triangle', 260, 130, .3, .14); |
| 440 |
else if (kind === 'tick') tone('square', 600, 600, .05, .04); |
| 441 |
} |
| 442 |
const muteButton = document.getElementById('mute'); |
| 443 |
muteButton.addEventListener('click', () => { |
| 444 |
muted = !muted; |
| 445 |
muteButton.textContent = muted ? 'sound off' : 'sound on'; |
| 446 |
muteButton.setAttribute('aria-pressed', String(muted)); |
| 447 |
}); |
| 448 |
|
| 449 |
|
| 450 |
addEventListener('resize', fit); |
| 451 |
fit(); |
| 452 |
bake(); |
| 453 |
setStatus(statusFor()); |
| 454 |
renderHud(); |
| 455 |
let last = performance.now(); |
| 456 |
function frame(now) { |
| 457 |
update(Math.min(.04, (now - last) / 1000)); |
| 458 |
last = now; |
| 459 |
draw(); |
| 460 |
requestAnimationFrame(frame); |
| 461 |
} |
| 462 |
requestAnimationFrame(frame); |
| 463 |
</script> |
| 464 |
</body> |
| 465 |
</html> |
z = lift = 2.5 is below the dish (PAN.rest = 3), so the fruit is hidden while it's held over the pan, which is exactly where the player drops it. Also, unprojecting at sy + lift·COS·S and then drawing at z = lift lands the fruit back at sy, centred under the finger rather than lifted above it. Draw it above the pan's top and offset it up a fixed number of screen pixels.