// BackgammonHud.js — everything around the case: the two café-board plates // (portraits, names, pip counts), the chalk slate (match score, cube, race bar, // status), the waiter's-pad move ledger, the button stack and speech bubbles. import { Button } from '../../ui/Button.js'; import { createOpponentPortrait, createPlayerPortrait } from '../../ui/Portrait.js'; import { OPP_PLATE, HUMAN_PLATE, OPP_PARTS, HUMAN_PARTS, SLATE, LEDGER, MENU_BTN, BTN, BUTTONS, } from './BackgammonLayout.js'; import { DISPLAY_FONT, SERIF, HAND_FONT, PRINT_FONT, INK } from './BackgammonArt.js'; const CHALK = '#ecebe4'; const CHALK_DIM = '#a9b0aa'; export class Hud { constructor(scene, depth, { names, skill, opponent, onButton, onMenu }) { this.scene = scene; this.depth = depth; this.names = names; // { white, black } this.skill = skill; this.opponent = opponent; this.onButton = onButton; this.onMenu = onMenu; this.buttons = {}; this.bubble = null; } build() { const s = this.scene, D = this.depth; // plates s.add.image(OPP_PLATE.x - 16, OPP_PLATE.y - 16, 'bg-plate').setOrigin(0).setDepth(D); s.add.image(HUMAN_PLATE.x - 16, HUMAN_PLATE.y - 16, 'bg-plate').setOrigin(0).setDepth(D); this.rings = { white: s.add.graphics().setDepth(D + 1), black: s.add.graphics().setDepth(D + 1) }; const op = OPP_PARTS.portrait, hp = HUMAN_PARTS.portrait; this.portraits = { black: createOpponentPortrait(s, this.opponent ?? { id: 'bot', spriteIndex: 0 }, op.x, op.y, op.r, D + 2), white: createPlayerPortrait(s, hp.x, hp.y, hp.r, D + 2, 'Backgammon'), }; const nm = (n) => (n.length > 16 ? n.slice(0, 15) + '…' : n); s.add.text(OPP_PARTS.name.x, OPP_PARTS.name.y, nm(this.names.black), { fontFamily: DISPLAY_FONT, fontSize: '24px', color: '#22303c' }).setOrigin(0.5).setDepth(D + 1); s.add.text(OPP_PARTS.skill.x, OPP_PARTS.skill.y, '●'.repeat(this.skill) + '○'.repeat(5 - this.skill), { fontFamily: SERIF, fontSize: '14px', color: '#2f6ea6' }).setOrigin(0.5).setDepth(D + 1); s.add.text(HUMAN_PARTS.name.x, HUMAN_PARTS.name.y, nm(this.names.white), { fontFamily: DISPLAY_FONT, fontSize: '24px', color: '#22303c' }).setOrigin(0.5).setDepth(D + 1); this.pipText = { black: s.add.text(OPP_PARTS.pips.x, OPP_PARTS.pips.y, '', { fontFamily: PRINT_FONT, fontSize: '16px', color: '#5a4a32' }).setOrigin(0.5).setDepth(D + 1), white: s.add.text(HUMAN_PARTS.pips.x, HUMAN_PARTS.pips.y, '', { fontFamily: PRINT_FONT, fontSize: '16px', color: '#5a4a32' }).setOrigin(0.5).setDepth(D + 1), }; this.chk = { black: s.add.image(OPP_PARTS.pips.x - 92, OPP_PARTS.pips.y, 'bg-chk-black').setScale(0.3).setDepth(D + 1), white: s.add.image(HUMAN_PARTS.pips.x - 92, HUMAN_PARTS.pips.y, 'bg-chk-white').setScale(0.3).setDepth(D + 1), }; // slate s.add.image(SLATE.x - 16, SLATE.y - 16, 'bg-slate').setOrigin(0).setDepth(D); const sx = SLATE.x + SLATE.w / 2; this.slate = { head: s.add.text(sx, SLATE.y + 40, '', { fontFamily: HAND_FONT, fontSize: '26px', color: CHALK }).setOrigin(0.5).setDepth(D + 1), score: s.add.text(sx, SLATE.y + 74, '', { fontFamily: HAND_FONT, fontSize: '22px', color: CHALK, align: 'center' }).setOrigin(0.5).setDepth(D + 1), cube: s.add.text(sx, SLATE.y + 106, '', { fontFamily: HAND_FONT, fontSize: '19px', color: CHALK_DIM }).setOrigin(0.5).setDepth(D + 1), race: s.add.graphics().setDepth(D + 1), raceText: s.add.text(sx, SLATE.y + 158, '', { fontFamily: HAND_FONT, fontSize: '17px', color: CHALK_DIM }).setOrigin(0.5).setDepth(D + 1), status: s.add.text(sx, SLATE.y + 206, '', { fontFamily: HAND_FONT, fontSize: '25px', color: '#ffe9a8', align: 'center', wordWrap: { width: SLATE.w - 40 } }).setOrigin(0.5).setDepth(D + 1), sub: s.add.text(sx, SLATE.y + 254, '', { fontFamily: SERIF, fontSize: '14px', fontStyle: 'italic', color: CHALK_DIM, align: 'center', wordWrap: { width: SLATE.w - 44 } }).setOrigin(0.5).setDepth(D + 1), }; // ledger s.add.image(LEDGER.x - 14, LEDGER.y - 14, 'bg-ledger').setOrigin(0).setDepth(D); this.ledgerLines = []; for (let i = 0; i < 11; i++) { this.ledgerLines.push(s.add.text(LEDGER.x + 10, LEDGER.y + 62 + i * 26, '', { fontFamily: HAND_FONT, fontSize: '17px', color: '#22303c', }).setOrigin(0, 0.5).setDepth(D + 1)); } // buttons const label = { double: 'Double (D)', hint: 'Hint (H)', undo: 'Undo (Z)', done: 'Done (Space)' }; const bg = { double: 0x7a2420, hint: 0x2f5577, undo: 0x6a4a1a, done: 0x2f6e3a }; BUTTONS.forEach((k, i) => { this.buttons[k] = new Button(s, BTN.x, BTN.y0 + i * BTN.gap, label[k], () => this.onButton(k), { width: BTN.w, height: BTN.h, fontSize: 17, bg: bg[k], }).setDepth(D + 3); this.buttons[k].setEnabled(false); }); this.menuBtn = new Button(s, MENU_BTN.x, MENU_BTN.y, 'Menu (Esc)', () => this.onMenu(), { width: MENU_BTN.w, height: MENU_BTN.h, fontSize: 17, bg: 0x22445f, }).setDepth(D + 3); return this; } setButton(k, enabled, labelText) { const b = this.buttons[k]; if (!b) return; b.setEnabled(!!enabled); if (labelText) b.setLabel(labelText); } setActive(color) { for (const c of ['white', 'black']) { const g = this.rings[c]; g.clear(); if (c !== color) continue; const P = c === 'white' ? HUMAN_PLATE : OPP_PLATE; const pp = c === 'white' ? HUMAN_PARTS.portrait : OPP_PARTS.portrait; g.lineStyle(5, 0xf5c542, 1).strokeRoundedRect(P.x - 4, P.y - 4, P.w + 8, P.h + 8, 16); g.lineStyle(4, 0xf5c542, 1).strokeCircle(pp.x, pp.y, pp.r + 6); } } setPips(w, b, show = true) { this.pipText.white.setText(show ? `${w} pips` : ''); this.pipText.black.setText(show ? `${b} pips` : ''); this.chk.white.setVisible(show); this.chk.black.setVisible(show); // race bar on the slate: who leads and by how much const g = this.slate.race; g.clear(); const x0 = SLATE.x + 30, x1 = SLATE.x + SLATE.w - 30, y = SLATE.y + 136; g.lineStyle(2, 0xecebe4, 0.5).lineBetween(x0, y, x1, y); if (!show) { this.slate.raceText.setText(''); return; } const diff = b - w; // > 0: you lead const k = Math.max(-1, Math.min(1, diff / 60)); const mid = (x0 + x1) / 2; g.lineStyle(6, diff >= 0 ? 0xbfe3a8 : 0xf0a18e, 0.9).lineBetween(mid, y, mid + k * (x1 - x0) / 2, y); g.fillStyle(0xecebe4, 1).fillCircle(mid, y, 3); this.slate.raceText.setText(diff === 0 ? 'Race: dead even' : diff > 0 ? `Race: you lead by ${diff}` : `Race: ${this.names.black} leads by ${-diff}`); } setMatch(match, cube, crawford, cubeEnabled) { const sl = this.slate; if (match.length > 0) { sl.head.setText(`Match to ${match.length}`); sl.score.setText(`You ${match.score.white} · ${this.short(this.names.black)} ${match.score.black}`); } else { sl.head.setText('Single game'); sl.score.setText(match.games.length ? `Tonight: You ${match.score.white} · ${this.short(this.names.black)} ${match.score.black}` : 'Bear off all fifteen'); } let c; if (crawford) c = 'Crawford game — no doubling'; else if (!cubeEnabled) c = 'Doubling cube off'; else if (cube.value === 1) c = 'Cube in the middle'; else c = `Cube at ${cube.value} · ${cube.owner === 'white' ? 'yours' : `${this.short(this.names.black)}'s`}`; sl.cube.setText(c); } short(n) { return n.length > 9 ? n.slice(0, 8) + '…' : n; } setStatus(main, sub = '') { this.slate.status.setText(main); this.slate.sub.setText(sub); } setLedger(log) { const lines = log.slice(-this.ledgerLines.length); this.ledgerLines.forEach((t, i) => { const e = lines[i]; if (!e) { t.setText(''); return; } const who = e.player === 'white' ? 'You' : this.short(this.names.black); const dice = e.dice ? ` ${e.dice[0]}-${e.dice[1]}` : ''; t.setText(`${who}${dice} ${e.text}`).setColor(INK[e.player] ?? '#22303c'); // keep long lines inside the pad t.setFontSize(t.text.length > 24 ? 14 : 17); }); } say(text, dur = 2400) { const s = this.scene; this.bubble?.destroy(); const b = OPP_PARTS.bubble; const t = s.add.text(b.x + 16, b.y + b.h / 2, text, { fontFamily: SERIF, fontSize: '16px', fontStyle: 'italic', color: '#22303c', wordWrap: { width: b.w - 28 }, }).setOrigin(0, 0.5); const w = b.w, h = Math.max(42, t.height + 20); t.setPosition(b.x + w / 2, b.y + 8 + h / 2).setOrigin(0.5).setAlign('center'); const g = s.add.graphics(); const y0 = b.y + 8; g.fillStyle(0x000000, 0.3).fillRoundedRect(b.x + 3, y0 + 4, w, h, 12); g.fillStyle(0xfbf8ef, 1).fillRoundedRect(b.x, y0, w, h, 12); g.lineStyle(2, 0x7a2420, 1).strokeRoundedRect(b.x, y0, w, h, 12); // tail pointing up at the portrait g.fillStyle(0xfbf8ef, 1).fillTriangle(b.x + w / 2 - 10, y0 + 1, b.x + w / 2 + 10, y0 + 1, b.x + w / 2, y0 - 14); g.lineStyle(2, 0x7a2420, 1).lineBetween(b.x + w / 2 - 10, y0, b.x + w / 2, y0 - 14).lineBetween(b.x + w / 2 + 10, y0, b.x + w / 2, y0 - 14); const c = s.add.container(0, 0, [g, t]).setDepth(this.depth + 6).setAlpha(0); this.bubble = c; s.tweens.add({ targets: c, alpha: 1, duration: 160 }); s.tweens.add({ targets: c, alpha: 0, delay: dur, duration: 300, onComplete: () => { c.destroy(); if (this.bubble === c) this.bubble = null; } }); } emotion(color, e) { this.portraits[color]?.playEmotion?.(e); } hidePortraits() { Object.values(this.portraits).forEach((p) => p?.setVideoVisible?.(false)); } showPortraits() { Object.values(this.portraits).forEach((p) => p?.setVideoVisible?.(true)); } }