fertig-classic-games/src/games/checkers/CheckersHud.js

226 lines
12 KiB
JavaScript

// CheckersHud.js — the furniture around the barrel: a walnut plaque per
// player (portrait, name, skill, a note line, pieces / kings / clock, the
// match tally, their colour), captured-checker trays, the status card, the
// move sheet, the buttons, the opponent's speech bubble and Gus's ribbon.
import { Button } from '../../ui/Button.js';
import { createOpponentPortrait, createPlayerPortrait } from '../../ui/Portrait.js';
import {
PLATES, PLATE, plateParts, TRAYS, BUBBLE, STATUS, LOG, LOG_ROWS, BUTTONS, BTN, RIBBON,
} from './CheckersLayout.js';
import { DISPLAY_FONT, SERIF, INK } from './CheckersArt.js';
import { counts, rulesOf, other } from './CheckersLogic.js';
const short = (s, n = 14) => (s.length > n ? s.slice(0, n - 1) + '…' : s);
const clock = (ms) => { const s = Math.floor(ms / 1000); return `${Math.floor(s / 60)}:${String(s % 60).padStart(2, '0')}`; };
const half = (x) => (x % 1 ? `${Math.floor(x) || ''}½` : String(x));
export class Hud {
constructor(scene, depth, { names, skill, opponent, onButton }) {
this.scene = scene;
this.depth = depth;
this.names = names; // { top, bottom }
this.skill = skill;
this.opponent = opponent;
this.onButton = onButton;
this.plates = {};
this.buttons = {};
this.bubble = null;
this.trayObjs = { top: [], bottom: [] };
this.symbols = false;
}
build() {
const s = this.scene, D = this.depth;
for (const side of ['top', 'bottom']) {
const P = PLATES[side], parts = plateParts(side);
const glow = s.add.image(P.x - 40, P.y - 40, 'ck-plate-glow').setOrigin(0).setDepth(D - 1).setAlpha(0);
s.add.image(P.x - 14, P.y - 14, 'ck-plate').setOrigin(0).setDepth(D);
const pp = parts.portrait;
const ring = s.add.circle(pp.x, pp.y, pp.r + 5, 0xd8b060, 1).setDepth(D + 1);
const portrait = side === 'bottom'
? createPlayerPortrait(s, pp.x, pp.y, pp.r, D + 2, 'CheckersGame')
: createOpponentPortrait(s, this.opponent ?? { id: 'bot', spriteIndex: 0 }, pp.x, pp.y, pp.r, D + 2);
s.add.text(parts.name.x, parts.name.y, short(this.names[side], side === 'top' ? 13 : 16), {
fontFamily: DISPLAY_FONT, fontSize: '22px', color: '#f8ead0',
}).setOrigin(0, 0.5).setDepth(D + 1);
if (side === 'top') {
const k = this.skill;
s.add.text(parts.skill.x, parts.skill.y, '●'.repeat(k) + '○'.repeat(5 - k), { fontFamily: SERIF, fontSize: '13px', color: '#e8c46a' })
.setOrigin(1, 0.5).setDepth(D + 1);
}
const note = s.add.text(parts.note.x, parts.note.y, '', { fontFamily: SERIF, fontSize: '17px', fontStyle: 'italic', color: '#e0caa0' })
.setOrigin(0, 0.5).setDepth(D + 1);
const line = s.add.text(parts.line.x, parts.line.y, '', { fontFamily: SERIF, fontSize: '15px', color: '#d8c4a0' })
.setOrigin(0, 0.5).setDepth(D + 1);
const chip = s.add.image(parts.color.x, parts.color.y, 'ck-man-red').setScale(0.2).setDepth(D + 1).setVisible(false);
const chipText = s.add.text(parts.color.x + 20, parts.color.y, '', { fontFamily: SERIF, fontSize: '14px', color: '#d8c4a0' })
.setOrigin(0, 0.5).setDepth(D + 1);
const tally = s.add.text(parts.tally.x, parts.tally.y, '', { fontFamily: DISPLAY_FONT, fontSize: '16px', color: '#f0d890' })
.setOrigin(1, 0.5).setDepth(D + 1);
this.plates[side] = { glow, ring, portrait, note, line, chip, chipText, tally, active: false };
}
// status card
s.add.image(STATUS.x - 14, STATUS.y - 14, 'ck-card-status').setOrigin(0).setDepth(D);
this.status = s.add.text(STATUS.x + STATUS.w / 2, STATUS.y + 66, '', {
fontFamily: DISPLAY_FONT, fontSize: '27px', color: '#3a1a0c', align: 'center', wordWrap: { width: STATUS.w - 50 },
}).setOrigin(0.5).setDepth(D + 1);
this.sub = s.add.text(STATUS.x + STATUS.w / 2, STATUS.y + 136, '', {
fontFamily: SERIF, fontSize: '16px', color: '#3a2a1a', align: 'center', wordWrap: { width: STATUS.w - 56 }, lineSpacing: 3,
}).setOrigin(0.5).setDepth(D + 1);
// move sheet
s.add.image(LOG.x - 14, LOG.y - 14, 'ck-card-log').setOrigin(0).setDepth(D);
this.logTitle = s.add.text(LOG.x + 24, LOG.y + 34, 'MOVE SHEET', { fontFamily: DISPLAY_FONT, fontSize: '16px', color: '#5a2a12' }).setOrigin(0, 0.5).setDepth(D + 1);
this.logSub = s.add.text(LOG.x + LOG.w - 24, LOG.y + 34, '', { fontFamily: SERIF, fontSize: '14px', fontStyle: 'italic', color: '#5a2a12' }).setOrigin(1, 0.5).setDepth(D + 1);
const colA = LOG.x + 66 + (LOG.w - 66) / 4, colB = LOG.x + 66 + ((LOG.w - 66) * 3) / 4;
this.colHead = [
s.add.text(colA, LOG.y + 62, '', { fontFamily: DISPLAY_FONT, fontSize: '14px', color: '#3a1a0c' }).setOrigin(0.5).setDepth(D + 1),
s.add.text(colB, LOG.y + 62, '', { fontFamily: DISPLAY_FONT, fontSize: '14px', color: '#3a1a0c' }).setOrigin(0.5).setDepth(D + 1),
];
this.rows = [];
for (let i = 0; i < LOG_ROWS; i++) {
const y = LOG.y + 96 + i * 31;
this.rows.push({
n: s.add.text(LOG.x + 50, y, '', { fontFamily: SERIF, fontSize: '15px', color: '#7a5a3a' }).setOrigin(1, 0.5).setDepth(D + 1),
a: s.add.text(colA, y, '', { fontFamily: SERIF, fontSize: '17px', color: '#2a1a10' }).setOrigin(0.5).setDepth(D + 1),
b: s.add.text(colB, y, '', { fontFamily: SERIF, fontSize: '17px', color: '#2a1a10' }).setOrigin(0.5).setDepth(D + 1),
});
}
// buttons
const label = { undo: 'Undo (Z)', hint: 'Hint (H)', draw: 'Offer Draw', resign: 'Resign', flip: 'Flip (F)', menu: 'Menu (Esc)' };
const bg = { undo: 0x6a4a1a, hint: 0x2a4a6a, draw: 0x4a4a4a, resign: 0x7a1a14, flip: 0x4a2c16, menu: 0x3a2212 };
for (const [k, b] of Object.entries(BUTTONS)) {
this.buttons[k] = new Button(s, b.x, b.y, label[k], () => this.onButton(k), { width: BTN.w, height: BTN.h, fontSize: 15, bg: bg[k] }).setDepth(D + 3);
if (!['menu', 'flip'].includes(k)) this.buttons[k].setEnabled(false);
}
// Gus's ribbon
s.add.image(RIBBON.x - 12, RIBBON.y - 12, 'ck-ribbon').setOrigin(0).setDepth(D);
this.gus = s.add.text(RIBBON.x + 84, RIBBON.y + RIBBON.h / 2, '', {
fontFamily: SERIF, fontSize: '19px', fontStyle: 'italic', color: '#f3e4c0', wordWrap: { width: RIBBON.w - 110 },
}).setOrigin(0, 0.5).setDepth(D + 1);
return this;
}
setSymbols(on) { this.symbols = !!on; }
/** gs, ctx: { humanColor, thinking, clocks: {human, ai}, match: {human, ai} | null, captured: {top, bottom} } */
refresh(gs, ctx) {
const c = counts(gs.board);
const sides = { bottom: ctx.humanColor, top: other(ctx.humanColor) };
for (const side of ['top', 'bottom']) {
const pl = this.plates[side], col = sides[side];
const n = col === 'black' ? c.black : c.red, k = col === 'black' ? c.blackKings : c.redKings;
const active = gs.phase === 'playing' && gs.turn === col;
if (active !== pl.active) {
pl.active = active;
this.scene.tweens.killTweensOf(pl.glow);
if (active) {
pl.glow.setAlpha(0.9);
this.scene.tweens.add({ targets: pl.glow, alpha: { from: 0.95, to: 0.45 }, duration: 900, yoyo: true, repeat: -1, ease: 'Sine.InOut' });
} else pl.glow.setAlpha(0);
}
let note;
if (gs.phase !== 'playing') note = '';
else if (active) note = side === 'bottom' ? 'Your move' : (ctx.thinking ? 'Thinking…' : 'To play');
else note = side === 'bottom' ? 'Waiting' : 'Watching the board';
pl.note.setText(note).setColor(active ? '#ffe0a0' : '#c8b490');
const ms = side === 'bottom' ? ctx.clocks.human : ctx.clocks.ai;
pl.line.setText(`${n} on the board${k ? ` · ${k} king${k === 1 ? '' : 's'}` : ''} ⏱ ${clock(ms)}`);
pl.chip.setTexture(`ck-man-${col}${this.symbols ? '-s' : ''}`).setVisible(true);
pl.chipText.setText(col === 'black' ? 'Black' : 'Red');
const m = ctx.match;
pl.tally.setText(m && m.games > 1 ? `Match ${half(side === 'bottom' ? m.human : m.ai) || '0'}` : '');
pl.ring.setFillStyle(side === 'bottom' ? 0xd8b060 : (col === 'black' ? 0x5a5868 : 0xc0302a));
}
this.setTrays(ctx.captured);
this.setLog(gs);
}
setTrays(captured) {
const s = this.scene;
for (const side of ['top', 'bottom']) {
this.trayObjs[side].forEach((o) => o.destroy());
this.trayObjs[side] = [];
const T = TRAYS[side];
const list = captured[side] ?? [];
// stacks of up to four, side by side
list.forEach((p, i) => {
const stack = Math.floor(i / 4), h = i % 4;
const x = T.x + 24 + stack * 46, y = T.y + T.h / 2 + 6 - h * 6;
const tex = `ck-man-${p > 0 ? 'black' : 'red'}${this.symbols ? '-s' : ''}`;
const im = s.add.image(x, y, tex).setScale(0.22).setDepth(this.depth + 1 + i * 0.01);
this.trayObjs[side].push(im);
});
}
}
trayTarget(side, n) {
const T = TRAYS[side];
const stack = Math.floor(n / 4), h = n % 4;
return { x: T.x + 24 + stack * 46, y: T.y + T.h / 2 + 6 - h * 6 };
}
setLog(gs) {
const rules = rulesOf(gs.variant);
const first = rules.first;
const name = (c) => (c === 'black' ? 'BLACK' : 'RED');
this.colHead[0].setText(name(first)).setColor(first === 'black' ? '#1d1c22' : INK.red.text);
this.colHead[1].setText(name(other(first))).setColor(first === 'black' ? INK.red.text : '#1d1c22');
this.logSub.setText(gs.ballot ? `${rules.short} · ${gs.ballot.name}` : rules.short);
const pairs = [];
for (const h of gs.history) {
if (h.color === first || !pairs.length) pairs.push({ a: null, b: null });
const last = pairs[pairs.length - 1];
if (h.color === first) last.a = h; else last.b = h;
}
const start = Math.max(0, pairs.length - LOG_ROWS);
const lastH = gs.history[gs.history.length - 1];
this.rows.forEach((r, i) => {
const p = pairs[start + i];
if (!p) { r.n.setText(''); r.a.setText(''); r.b.setText(''); return; }
r.n.setText(`${start + i + 1}.`);
for (const [t, h] of [[r.a, p.a], [r.b, p.b]]) {
if (!h) { t.setText(''); continue; }
t.setText(h.n + (h.crown ? ' ♔' : '')).setColor(h.ballot ? '#8a7a62' : h === lastH ? '#a0221c' : '#2a1a10')
.setFontStyle(h.ballot ? 'italic' : h === lastH ? 'bold' : 'normal');
}
});
}
setStatus(main, sub = '') { this.status.setText(main); this.sub.setText(sub); }
setButton(k, on) { this.buttons[k]?.setEnabled(!!on); }
gusSays(text, { hold = 7000 } = {}) {
if (!text) return;
const t = this.gus;
this.scene.tweens.killTweensOf(t);
t.setText(`Gus: “${text}”`).setAlpha(0);
this.scene.tweens.add({ targets: t, alpha: 1, duration: 220 });
this.scene.tweens.add({ targets: t, alpha: 0.55, delay: hold, duration: 800 });
}
say(text, dur = 2600) {
if (!text) return;
const s = this.scene;
this.bubble?.destroy();
const b = BUBBLE;
const t = s.add.text(b.x + b.w / 2, b.y + b.h / 2, text, {
fontFamily: SERIF, fontSize: '17px', fontStyle: 'italic', color: '#2a1a10', align: 'center', wordWrap: { width: b.w - 36 },
}).setOrigin(0.5);
const g = s.add.graphics();
g.fillStyle(0x000000, 0.3).fillRoundedRect(b.x + 3, b.y + 4, b.w, b.h, 12);
g.fillStyle(0xf3ead2, 1).fillRoundedRect(b.x, b.y, b.w, b.h, 12);
g.lineStyle(2, 0x8a5a2a, 1).strokeRoundedRect(b.x, b.y, b.w, b.h, 12);
const tx = plateParts('top').portrait.x + 40;
g.fillStyle(0xf3ead2, 1).fillTriangle(tx - 10, b.y + 1, tx + 10, b.y + 1, tx, b.y - 14);
const cont = s.add.container(0, 0, [g, t]).setDepth(this.depth + 8).setAlpha(0);
this.bubble = cont;
s.tweens.add({ targets: cont, alpha: 1, duration: 160 });
s.tweens.add({ targets: cont, alpha: 0, delay: dur, duration: 300, onComplete: () => { cont.destroy(); if (this.bubble === cont) this.bubble = null; } });
}
emotion(e) { this.plates.top?.portrait?.playEmotion?.(e); }
hidePortraits() { Object.values(this.plates).forEach((p) => p.portrait?.setVideoVisible?.(false)); }
showPortraits() { Object.values(this.plates).forEach((p) => p.portrait?.setVideoVisible?.(true)); }
}