// CatanEndScreen.js — the end of a game. // // 1. Reveal: every hidden Victory Point card flips face-up over its owner's // board, so the final scores make sense. // 2. Scoreboard: settlements, cities, Longest Road, Largest Army, VP cards and // any island bonus per player, the winner's row gilded. // 3. Two charts: the dice (how often each total came up vs the odds) and // production (what each player collected, plus robber/steal/trade counts). import * as Phaser from 'phaser'; import { Button } from '../../ui/Button.js'; import { createOpponentPortrait, createPlayerPortrait } from '../../ui/Portrait.js'; import { RESOURCE_INFO, RESOURCE_TYPES, PLAYER_COLORS } from './CatanBoard.js'; import * as L from './CatanLogic.js'; const PX = 960, PY = 540, PW = 1260, PH = 840; export async function showCatanEndScreen(scene, { depth, onPlayAgain, onLeave, revealAt }) { const gs = scene.gs; const winner = gs.winner; const isHuman = winner === 0; // ── 1. reveal hidden VP cards ───────────────────────────────────────────── const reveals = []; for (let seat = 0; seat < gs.playerCount; seat++) { const n = gs.players[seat].vpCards ?? 0; if (!n) continue; const at = revealAt(seat); for (let i = 0; i < n; i++) { const c = scene.makeCardSprite({ kind: 'devback' }).setPosition(at.x + i * 26, at.y).setDepth(depth + 2).setScale(0.2).setAlpha(0); reveals.push(c); scene.tweens.add({ targets: c, scale: 1.1, alpha: 1, duration: 260, delay: 300 * reveals.length, ease: 'Back.Out' }); scene.time.delayedCall(300 * reveals.length + 420, () => { scene.tweens.add({ targets: c, scaleX: 0, duration: 120, onComplete: () => { c.paint({ kind: 'dev', type: 'vp' }); scene.tweens.add({ targets: c, scaleX: 1.1, duration: 120 }); }, }); }); } const label = scene.add.text(at.x, at.y - 70, `+${n} VP`, { fontFamily: 'Righteous', fontSize: '30px', color: '#ffd34d', stroke: '#1a0d04', strokeThickness: 5, }).setOrigin(0.5).setDepth(depth + 3).setAlpha(0); reveals.push(label); scene.tweens.add({ targets: label, alpha: 1, y: at.y - 90, duration: 400, delay: 300 * reveals.length + 300 }); } if (reveals.length) await new Promise((r) => scene.time.delayedCall(300 * reveals.length + 1300, r)); reveals.forEach((o) => scene.tweens.add({ targets: o, alpha: 0, duration: 300, onComplete: () => o.destroy() })); // portraits are DOM elements over the canvas: tuck the table's away scene.opponentPortraits.forEach((p) => p?.hide?.()); scene.humanPortrait?.hide?.(); // ── 2. panel + scoreboard ───────────────────────────────────────────────── const objs = []; const add = (o) => { objs.push(o); return o; }; add(scene.add.rectangle(PX, PY, 1920, 1080, 0x000000, 0.55).setInteractive().setDepth(depth)); const g = add(scene.add.graphics().setDepth(depth + 1)); g.fillStyle(0x000000, 0.45); g.fillRoundedRect(PX - PW / 2 + 6, PY - PH / 2 + 10, PW, PH, 18); g.fillStyle(0x24160b, 0.98); g.fillRoundedRect(PX - PW / 2, PY - PH / 2, PW, PH, 18); g.lineStyle(3, 0xc9a24a, 1); g.strokeRoundedRect(PX - PW / 2, PY - PH / 2, PW, PH, 18); g.lineStyle(1, 0xc9a24a, 0.45); g.strokeRoundedRect(PX - PW / 2 + 8, PY - PH / 2 + 8, PW - 16, PH - 16, 13); const top = PY - PH / 2; add(scene.add.text(PX + 70, top + 52, isHuman ? 'Victory!' : `${L.playerName(gs, winner)} wins`, { fontFamily: 'Righteous', fontSize: '46px', color: isHuman ? '#ffd34d' : '#f2ead8', }).setOrigin(0.5).setDepth(depth + 2)); const rolled = gs.stats?.rolls?.reduce((a, b) => a + b, 0) ?? 0; add(scene.add.text(PX + 70, top + 96, `Reached ${L.winVpFor(gs)} victory points after ${rolled} rolls`, { fontFamily: '"Julius Sans One"', fontSize: '18px', color: '#cbb994', }).setOrigin(0.5).setDepth(depth + 2)); // winner portrait const wx = PX - PW / 2 + 130, wy = top + 150; const ring = add(scene.add.graphics().setDepth(depth + 2)); ring.fillStyle(0x111111, 1); ring.fillCircle(wx, wy, 86); ring.lineStyle(5, PLAYER_COLORS[gs.players[winner].colorIndex].hex, 1); ring.strokeCircle(wx, wy, 86); let portrait = null; if (isHuman) portrait = createPlayerPortrait(scene, wx, wy, 80, depth + 3, 'CatanGame'); else { portrait = createOpponentPortrait(scene, scene.opponents[winner - 1], wx, wy, 80, depth + 3, { playIntro: false }); portrait?.playEmotion?.('happy'); } // fireworks const fw = scene.add.particles(PX, PY, 'catanParticle', { speed: { min: 80, max: 480 }, lifespan: 1400, scale: { start: 1.2, end: 0 }, alpha: { start: 1, end: 0 }, quantity: 3, frequency: 35, tint: [0xffd700, 0xff6644, 0xffffff, 0x44aaff, 0xff44aa, 0x88ff44], angle: { min: 0, max: 360 }, emitZone: { type: 'random', source: new Phaser.Geom.Rectangle(-PW / 2, -PH / 2, PW, PH) }, }).setDepth(depth + 8); scene.time.delayedCall(3200, () => { fw.stop(); scene.time.delayedCall(1400, () => fw.destroy()); }); const cols = [ { k: 'name', label: 'Player', x: 0, w: 230, align: 0 }, { k: 'settle', label: 'Settlements', x: 250 }, { k: 'city', label: 'Cities', x: 360 }, { k: 'lr', label: 'Long Road', x: 460 }, { k: 'la', label: 'Army', x: 560 }, { k: 'vp', label: 'VP cards', x: 650 }, { k: 'bonus', label: 'Island', x: 745 }, { k: 'total', label: 'Total', x: 840 }, ]; const tx = PX - PW / 2 + 260, ty = top + 140, rowH = 44; cols.forEach((c) => add(scene.add.text(tx + c.x + (c.align === 0 ? 0 : 30), ty, c.label, { fontFamily: '"Julius Sans One"', fontSize: '14px', color: '#cbb994', }).setOrigin(c.align === 0 ? 0 : 0.5, 0.5).setDepth(depth + 2))); const order = [...Array(gs.playerCount).keys()].sort((a, b) => L.victoryPoints(gs, b) - L.victoryPoints(gs, a)); order.forEach((seat, i) => { const p = gs.players[seat]; const y = ty + 34 + i * rowH; const col = PLAYER_COLORS[p.colorIndex]; const rg = add(scene.add.graphics().setDepth(depth + 1.5)); if (seat === winner) { rg.fillStyle(0xc9a24a, 0.28); rg.fillRoundedRect(tx - 14, y - rowH / 2 + 3, 920, rowH - 6, 8); rg.lineStyle(2, 0xffd34d, 0.9); rg.strokeRoundedRect(tx - 14, y - rowH / 2 + 3, 920, rowH - 6, 8); } else { rg.fillStyle(0xffffff, i % 2 ? 0.03 : 0.06); rg.fillRoundedRect(tx - 14, y - rowH / 2 + 3, 920, rowH - 6, 8); } rg.fillStyle(col.hex, 1); rg.fillCircle(tx, y, 8); const total = L.victoryPoints(gs, seat); const base = p.settlements.length + p.cities.length * 2 + (p.vpCards ?? 0) + (gs.longestRoad.owner === seat ? 2 : 0) + (gs.largestArmy.owner === seat ? 2 : 0); const vals = { name: L.playerName(gs, seat), settle: p.settlements.length, city: `${p.cities.length} (${p.cities.length * 2})`, lr: gs.longestRoad.owner === seat ? '2' : '—', la: gs.largestArmy.owner === seat ? '2' : '—', vp: p.vpCards || '—', bonus: total - base || '—', total, }; cols.forEach((c) => add(scene.add.text(tx + c.x + (c.align === 0 ? 18 : 30), y, String(vals[c.k]), { fontFamily: c.k === 'total' || c.k === 'name' ? 'Righteous' : '"Julius Sans One"', fontSize: c.k === 'total' ? '24px' : '18px', color: c.k === 'total' ? (seat === winner ? '#ffd34d' : '#f2ead8') : '#f2ead8', }).setOrigin(c.align === 0 ? 0 : 0.5, 0.5).setDepth(depth + 2))); }); // ── 3. charts ───────────────────────────────────────────────────────────── const chartTop = ty + 34 + 4 * rowH + 10; const chart = { x: PX - PW / 2 + 60, y: chartTop + 40, w: PW - 120, h: PY + PH / 2 - 96 - (chartTop + 40) }; const chartObjs = []; const clearChart = () => { chartObjs.forEach((o) => o.destroy()); chartObjs.length = 0; }; const cadd = (o) => { chartObjs.push(o); return o; }; const drawDice = () => { clearChart(); const rolls = gs.stats?.rolls ?? Array(13).fill(0); const total = rolls.reduce((a, b) => a + b, 0); const ways = (n) => 6 - Math.abs(7 - n); let max = 1; for (let n = 2; n <= 12; n++) max = Math.max(max, rolls[n], total * ways(n) / 36); const cg = cadd(scene.add.graphics().setDepth(depth + 2)); const bw = 46, pitch = chart.w / 11, base = chart.y + chart.h - 26, hMax = chart.h - 60; cg.lineStyle(1.5, 0xcbb994, 0.6); cg.lineBetween(chart.x, base, chart.x + chart.w, base); for (let n = 2; n <= 12; n++) { const x = chart.x + pitch * (n - 2) + pitch / 2; const h = (rolls[n] / max) * hMax; const hot = n === 6 || n === 8; cg.fillStyle(n === 7 ? 0x8f9a96 : (hot ? 0xd0553f : 0xe6cf98), 1); cg.fillRoundedRect(x - bw / 2, base - h, bw, h, { tl: 5, tr: 5, bl: 0, br: 0 }); const eh = (total * ways(n) / 36 / max) * hMax; cg.lineStyle(3, 0x7fd0ff, 0.9); cg.lineBetween(x - bw / 2 - 6, base - eh, x + bw / 2 + 6, base - eh); cadd(scene.add.text(x, base + 6, String(n), { fontFamily: 'Righteous', fontSize: '18px', color: hot ? '#ff9b8a' : '#f2ead8' }).setOrigin(0.5, 0).setDepth(depth + 2)); if (rolls[n]) cadd(scene.add.text(x, base - h - 4, String(rolls[n]), { fontFamily: 'Righteous', fontSize: '16px', color: '#f2ead8' }).setOrigin(0.5, 1).setDepth(depth + 2)); } cadd(scene.add.text(chart.x + chart.w, chart.y - 6, '▬ expected by the odds', { fontFamily: '"Julius Sans One"', fontSize: '14px', color: '#7fd0ff', }).setOrigin(1, 0).setDepth(depth + 2)); }; const drawProduction = () => { clearChart(); const st = gs.stats; if (!st) return; const n = gs.playerCount; const rowGap = Math.min(70, (chart.h - 10) / n); let max = 1; for (let s = 0; s < n; s++) max = Math.max(max, RESOURCE_TYPES.reduce((t, r) => t + st.produced[s][r], 0)); const barX = chart.x + 200, barW = chart.w - 200 - 360; const cg = cadd(scene.add.graphics().setDepth(depth + 2)); for (let s = 0; s < n; s++) { const y = chart.y + 14 + s * rowGap; const col = PLAYER_COLORS[gs.players[s].colorIndex]; cg.fillStyle(col.hex, 1); cg.fillCircle(chart.x + 8, y + 12, 7); cadd(scene.add.text(chart.x + 22, y + 12, L.playerName(gs, s), { fontFamily: 'Righteous', fontSize: '18px', color: '#f2ead8' }).setOrigin(0, 0.5).setDepth(depth + 2)); let x = barX; let tot = 0; for (const r of RESOURCE_TYPES) { const v = st.produced[s][r]; tot += v; const w = (v / max) * barW; if (w <= 0) continue; cg.fillStyle(RESOURCE_INFO[r].swatch, 1); cg.fillRect(x, y, w, 24); if (w > 22) cadd(scene.add.text(x + w / 2, y + 12, String(v), { fontFamily: 'Righteous', fontSize: '13px', color: '#1a0d04' }).setOrigin(0.5).setDepth(depth + 2)); x += w; } cadd(scene.add.text(x + 8, y + 12, `${tot}`, { fontFamily: 'Righteous', fontSize: '18px', color: '#ffe7a8' }).setOrigin(0, 0.5).setDepth(depth + 2)); const extra = [ `robber blocked ${st.blocked[s]}`, `stole ${st.stolen[s]}`, `lost ${st.lostToSteal[s] + st.discarded[s]}`, `trades ${st.trades[s]}`, ].join(' · '); cadd(scene.add.text(chart.x + chart.w, y + 12, extra, { fontFamily: '"Julius Sans One"', fontSize: '13px', color: '#cbb994' }).setOrigin(1, 0.5).setDepth(depth + 2)); } // legend let lx = barX; for (const r of RESOURCE_TYPES) { cg.fillStyle(RESOURCE_INFO[r].swatch, 1); cg.fillRect(lx, chart.y - 18, 12, 12); const t = cadd(scene.add.text(lx + 16, chart.y - 12, RESOURCE_INFO[r].label, { fontFamily: '"Julius Sans One"', fontSize: '13px', color: '#cbb994' }).setOrigin(0, 0.5).setDepth(depth + 2)); lx += 22 + t.width + 14; } }; const tabY = chartTop + 6; const tabs = {}; const select = (k) => { for (const [key, b] of Object.entries(tabs)) b.setAlpha(key === k ? 1 : 0.55); if (k === 'dice') drawDice(); else drawProduction(); }; tabs.dice = add(new Button(scene, PX - 110, tabY, 'Dice', () => select('dice'), { width: 200, height: 40, fontSize: 18 }).setDepth(depth + 2)); tabs.prod = add(new Button(scene, PX + 110, tabY, 'Production', () => select('prod'), { width: 200, height: 40, fontSize: 18 }).setDepth(depth + 2)); select('dice'); const by = PY + PH / 2 - 44; const cleanup = () => { clearChart(); objs.forEach((o) => o.destroy()); portrait?.destroy?.(); }; add(new Button(scene, PX - 120, by, 'Play Again', () => { cleanup(); onPlayAgain(); }, { width: 210, fontSize: 22 }).setDepth(depth + 2)); add(new Button(scene, PX + 120, by, 'Leave', () => { cleanup(); onLeave(); }, { variant: 'ghost', width: 210, fontSize: 22 }).setDepth(depth + 2)); }