fertig-classic-games/src/games/azul/AzulHud.js

313 lines
16 KiB
JavaScript

// AzulHud.js — the workshop's furniture that isn't tiles:
// • board headers (name, rolling score, first-player star) + bonus badges
// • the tile tracker (bottom-left): where every colour is
// • the work-order panel (bottom-centre): whose turn, what to do, the move
// preview and up to three dock buttons
// • the scoreboard (bottom-right): round, standings, wall variant
// • the order book (right): the last few log lines
import { Button } from '../../ui/Button.js';
import { COLORS, TILE_NAME, WALL_SIZE, ROW_BONUS, COLUMN_BONUS, COLOR_BONUS } from './AzulData.js';
import { bonusStatus } from './AzulLogic.js';
import {
TRACKER, ACTION, SCORES, NOTEPAD, DOCK, boardOf, bonusBadgeAt,
} from './AzulLayout.js';
import { SERIF, DISPLAY, COBALT, INK, SHADOW, tileKey, tileDisplay } from './AzulArt.js';
const CREAM = '#f6efdc';
export class AzulHud {
constructor(scene, { depth, playerCount, names, tooltip }) {
this.scene = scene;
this.D = depth;
this.n = playerCount;
this.names = names;
this.tooltip = tooltip;
this.shown = Array(playerCount).fill(0); // displayed (rolling) scores
this.scoreTweens = [];
this.buildPanels();
this.buildHeaders();
this.buildTracker();
this.buildAction();
this.buildScores();
this.buildNotepad();
}
add(o) { return o; }
buildPanels() {
const s = this.scene;
const img = (key, r) => s.add.image(r.x - SHADOW, r.y - SHADOW, key).setOrigin(0).setDepth(this.D.panel);
this.trackerPanel = img('azul-panel-tracker', TRACKER);
img('azul-panel-action', ACTION);
img('azul-panel-scores', SCORES);
s.add.image(NOTEPAD.x - SHADOW, NOTEPAD.y - SHADOW, 'azul-notepad').setOrigin(0).setDepth(this.D.panel);
}
// ── board headers ───────────────────────────────────────────────────────────
buildHeaders() {
const s = this.scene;
this.headers = [];
for (let seat = 0; seat < this.n; seat++) {
const { rect, m } = boardOf(seat, this.n);
const big = m.size === 'big';
const nameX = rect.x + m.pad + (big ? 40 : 30); // clears the hanging portrait
const hy = rect.y + (m.headerH - (big ? 8 : 6)) / 2;
const name = s.add.text(nameX, hy, this.names[seat] + (seat === 0 ? ' (you)' : ''), {
fontFamily: DISPLAY, fontSize: big ? '22px' : '17px', color: CREAM,
}).setOrigin(0, 0.5).setDepth(this.D.label);
const score = s.add.text(rect.x + rect.w - m.pad - 4, hy, '0', {
fontFamily: 'Righteous', fontSize: big ? '28px' : '22px', color: '#ffe08a', stroke: '#0e2442', strokeThickness: 3,
}).setOrigin(1, 0.5).setDepth(this.D.label);
const star = s.add.text(score.x - (big ? 60 : 46), hy, '①', {
fontFamily: SERIF, fontSize: big ? '22px' : '18px', color: '#ffffff',
}).setOrigin(1, 0.5).setDepth(this.D.label).setVisible(false);
// Active-turn frame (drawn just outside the board).
const glow = s.add.graphics().setDepth(this.D.boardGlow);
glow.lineStyle(big ? 5 : 4, 0xffd36b, 1).strokeRoundedRect(rect.x - 4, rect.y - 4, rect.w + 8, rect.h + 8, big ? 14 : 11);
glow.setAlpha(0);
// Bonus badge.
const bb = bonusBadgeAt(rect, m);
const badge = s.add.text(bb.x + bb.w - 2, bb.y + bb.h / 2, '', {
fontFamily: SERIF, fontSize: big ? '14px' : '11px', color: '#1d4f8f', fontStyle: 'bold', align: 'right', lineSpacing: -2,
}).setOrigin(1, 0.5).setDepth(this.D.label);
const bz = s.add.zone(bb.x + bb.w / 2, bb.y + bb.h / 2, bb.w, bb.h).setInteractive().setDepth(this.D.zone + 1);
this.tooltip.attachTo(bz, () => this.bonusTooltip(seat));
this.headers.push({ name, score, star, glow, badge, bz, big });
}
}
setActive(seat) {
this.headers.forEach((h, i) => {
const on = i === seat;
this.scene.tweens.killTweensOf(h.glow);
if (on) {
h.glow.setAlpha(1);
this.scene.tweens.add({ targets: h.glow, alpha: 0.45, duration: 900, yoyo: true, repeat: -1, ease: 'Sine.easeInOut' });
} else h.glow.setAlpha(0);
});
}
setStartPlayer(seat, hasMarker) {
this.headers.forEach((h, i) => h.star.setVisible(!!hasMarker && i === seat));
}
// Score text: snap (no animation) or roll from what's shown now.
setScore(seat, value, { roll = false, ms = 500 } = {}) {
const h = this.headers[seat];
this.scoreTweens[seat]?.stop?.();
if (!roll) { this.shown[seat] = value; h.score.setText(`${value}`); this.refreshScores(); return; }
const from = this.shown[seat];
const o = { v: from };
this.scoreTweens[seat] = this.scene.tweens.add({
targets: o, v: value, duration: ms, ease: 'Cubic.easeOut',
onUpdate: () => { this.shown[seat] = Math.round(o.v); h.score.setText(`${this.shown[seat]}`); },
onComplete: () => { this.shown[seat] = value; h.score.setText(`${value}`); this.refreshScores(); },
});
this.scene.tweens.add({ targets: h.score, scale: { from: 1.25, to: 1 }, duration: 380, ease: 'Back.easeOut' });
}
setBonuses(gs, show) {
this.gs = gs;
this.headers.forEach((h, seat) => {
if (!show) { h.badge.setText(''); return; }
const b = bonusStatus(gs.players[seat]);
const near = b.cols.filter((n) => n === WALL_SIZE - 1).length + Object.values(b.colors).filter((n) => n === WALL_SIZE - 1).length;
const txt = b.locked || near ? `+${b.locked}${near ? ` (${near}★)` : ''}` : '';
h.badge.setText(txt ? (h.big ? `Bonus ${txt}` : txt) : '');
});
}
bonusTooltip(seat) {
const gs = this.gs;
if (!gs) return { title: 'Bonuses', lines: [] };
const b = bonusStatus(gs.players[seat]);
const lines = [];
const row = (label, arr, bonus) => {
const parts = arr.map((n, i) => `${label[0]}${i + 1}:${n}`).join(' ');
return { text: `${label} (+${bonus} each): ${parts}` };
};
lines.push(row('Rows', b.rows, ROW_BONUS));
lines.push(row('Columns', b.cols, COLUMN_BONUS));
lines.push({ text: `Colours (+${COLOR_BONUS} each): ${COLORS.map((c) => `${TILE_NAME[c]} ${b.colors[c]}`).join(' · ')}` });
lines.push({ text: `Locked in: +${b.locked}. A ★ is a set with 4 of 5 — one tile from a bonus.`, color: '#ffe08a' });
return { title: `${this.names[seat]} — end-game bonuses`, lines };
}
// ── tile tracker ────────────────────────────────────────────────────────────
buildTracker() {
const s = this.scene;
const x = TRACKER.x, y = TRACKER.y;
this.trk = { objs: [] };
const head = s.add.text(x + TRACKER.w / 2, y + 30, 'Tile Tracker', { fontFamily: DISPLAY, fontSize: '20px', color: COBALT }).setOrigin(0.5).setDepth(this.D.label);
const cols = [['Bag', 132], ['Lid', 182], ['Table', 232], ['Walls', 280]];
const colHeads = cols.map(([t, cx]) => s.add.text(x + cx, y + 62, t, { fontFamily: SERIF, fontSize: '12px', color: '#6b5226', fontStyle: 'italic' }).setOrigin(0.5).setDepth(this.D.label));
this.trk.rows = COLORS.map((c, i) => {
const ry = y + 96 + i * 50;
const icon = s.add.image(x + 46, ry, tileKey(c, false)).setDisplaySize(tileDisplay(32), tileDisplay(32)).setDepth(this.D.label);
const name = s.add.text(x + 70, ry, TILE_NAME[c], { fontFamily: SERIF, fontSize: '13px', color: INK }).setOrigin(0, 0.5).setDepth(this.D.label);
const nums = cols.map(([, cx]) => s.add.text(x + cx, ry, '0', { fontFamily: 'Righteous', fontSize: '18px', color: INK }).setOrigin(0.5).setDepth(this.D.label));
const zone = s.add.zone(x + TRACKER.w / 2, ry, TRACKER.w - 28, 44).setInteractive().setDepth(this.D.zone);
this.tooltip.attachTo(zone, () => this.trackerTooltip(c));
return { c, icon, name, nums, zone };
});
this.trk.foot = s.add.text(x + TRACKER.w / 2, y + TRACKER.h - 30, '', { fontFamily: SERIF, fontSize: '12px', fontStyle: 'italic', color: '#6b5226', align: 'center' }).setOrigin(0.5).setDepth(this.D.label);
this.trk.hidden = s.add.text(x + TRACKER.w / 2, y + TRACKER.h / 2, 'Tracker off\n(Menu → Tile tracker)', { fontFamily: SERIF, fontSize: '15px', fontStyle: 'italic', color: '#6b5226', align: 'center' }).setOrigin(0.5).setDepth(this.D.label).setVisible(false);
this.trk.objs = [head, ...colHeads, ...this.trk.rows.flatMap((r) => [r.icon, r.name, ...r.nums]), this.trk.foot];
}
setTracker(census, visible) {
this.census = census;
this.trk.objs.forEach((o) => o.setVisible(visible));
this.trk.hidden.setVisible(!visible);
this.trk.rows.forEach((r) => r.zone.input && (r.zone.input.enabled = visible));
if (!visible) return;
let unseen = 0;
this.trk.rows.forEach((r) => {
const k = census[r.c];
const vals = [k.bag, k.lid, k.table, k.walls];
r.nums.forEach((t, i) => t.setText(`${vals[i]}`));
// Bag low → colour the count.
r.nums[0].setColor(k.bag <= 2 ? '#a7311d' : INK);
unseen += k.bag;
});
this.trk.foot.setText(`${unseen} in the bag · lid refills it when empty`);
}
trackerTooltip(c) {
const k = this.census?.[c];
if (!k) return { title: TILE_NAME[c], lines: [] };
return {
title: `${TILE_NAME[c]} tiles (20)`,
lines: [
{ text: `In the bag: ${k.bag}` },
{ text: `In the lid (discarded): ${k.lid}` },
{ text: `On plates / centre: ${k.table}` },
{ text: `On pattern lines: ${k.lines}` },
{ text: `On floor lines: ${k.floors}` },
{ text: `Set on walls: ${k.walls}` },
],
};
}
// ── work-order panel ──────────────────────────────────────────────────────────
buildAction() {
const s = this.scene;
const cx = ACTION.x + ACTION.w / 2;
this.act = {};
this.act.title = s.add.text(cx, ACTION.y + 40, '', { fontFamily: DISPLAY, fontSize: '26px', color: COBALT }).setOrigin(0.5).setDepth(this.D.label);
this.act.body = s.add.text(cx, ACTION.y + 100, '', {
fontFamily: SERIF, fontSize: '17px', color: INK, align: 'center', lineSpacing: 6, wordWrap: { width: ACTION.w - 70 },
}).setOrigin(0.5, 0.5).setDepth(this.D.label);
this.act.previewBg = s.add.graphics().setDepth(this.D.label - 1);
this.act.preview = s.add.text(cx, ACTION.y + 214, '', {
fontFamily: SERIF, fontSize: '16px', color: '#1d3f6e', align: 'center', lineSpacing: 5, wordWrap: { width: ACTION.w - 80 },
}).setOrigin(0.5).setDepth(this.D.label);
this.dock = DOCK.map((r) => {
const b = new Button(s, r.x, r.y, '', () => b._fn?.(), { width: r.w, height: r.h, fontSize: 17, bg: 0x1d4f8f }).setDepth(this.D.label + 2);
b.setVisible(false);
return b;
});
}
setAction(title, body, tone = 'plain') {
this.act.title.setText(title);
this.act.title.setColor(tone === 'you' ? COBALT : tone === 'warn' ? '#a7311d' : '#6b5226');
this.act.body.setText(body);
}
setPreview(text, tone = 'plain') {
const g = this.act.previewBg;
g.clear();
this.act.preview.setText(text ?? '');
if (!text) return;
const b = this.act.preview.getBounds();
const fill = tone === 'bad' ? 0xf2d2c4 : tone === 'good' ? 0xd8e6d0 : 0xe6ecf4;
g.fillStyle(fill, 1).fillRoundedRect(b.x - 16, b.y - 10, b.width + 32, b.height + 20, 8);
g.lineStyle(1.5, tone === 'bad' ? 0xa7311d : tone === 'good' ? 0x3c7a3a : 0x1d4f8f, 0.7).strokeRoundedRect(b.x - 16, b.y - 10, b.width + 32, b.height + 20, 8);
}
// buttons: [{ label, fn, bg }] — up to three, left to right.
setDock(buttons = []) {
this.dock.forEach((b, i) => {
const spec = buttons[i];
if (!spec) { b.setVisible(false); b.disableInteractive(); b._fn = null; return; }
b._fn = spec.fn;
b.setLabel(spec.label);
b.options.bg = spec.bg ?? 0x1d4f8f;
b._drawBg(b.options.bg, 1);
b.setVisible(true);
b.setEnabled(spec.enabled !== false);
});
}
// ── scoreboard ───────────────────────────────────────────────────────────────
buildScores() {
const s = this.scene;
const cx = SCORES.x + SCORES.w / 2;
this.sc = {};
this.sc.round = s.add.text(cx, SCORES.y + 38, '', { fontFamily: DISPLAY, fontSize: '26px', color: COBALT }).setOrigin(0.5).setDepth(this.D.label);
this.sc.sub = s.add.text(cx, SCORES.y + 70, '', { fontFamily: SERIF, fontSize: '13px', fontStyle: 'italic', color: '#6b5226' }).setOrigin(0.5).setDepth(this.D.label);
this.sc.rows = Array.from({ length: this.n }, (_, i) => {
const y = SCORES.y + 116 + i * 54;
const bg = s.add.graphics().setDepth(this.D.label - 1);
const rank = s.add.text(SCORES.x + 40, y, '', { fontFamily: 'Righteous', fontSize: '18px', color: '#6b5226' }).setOrigin(0.5).setDepth(this.D.label);
const name = s.add.text(SCORES.x + 62, y - 8, '', { fontFamily: SERIF, fontSize: '17px', color: INK }).setOrigin(0, 0.5).setDepth(this.D.label);
const sub = s.add.text(SCORES.x + 62, y + 13, '', { fontFamily: SERIF, fontSize: '12px', fontStyle: 'italic', color: '#6b5226' }).setOrigin(0, 0.5).setDepth(this.D.label);
const pts = s.add.text(SCORES.x + SCORES.w - 34, y, '', { fontFamily: 'Righteous', fontSize: '24px', color: COBALT }).setOrigin(1, 0.5).setDepth(this.D.label);
return { bg, rank, name, sub, pts, y };
});
this.sc.foot = s.add.text(cx, SCORES.y + SCORES.h - 30, '', { fontFamily: SERIF, fontSize: '13px', fontStyle: 'italic', color: '#6b5226', align: 'center' }).setOrigin(0.5).setDepth(this.D.label);
}
setRound(gs, { label } = {}) {
this.gs = gs;
this.sc.round.setText(label ?? (gs.phase === 'gameOver' ? 'Final' : `Round ${gs.round}`));
const plates = gs.factories.filter((f) => f.length).length;
this.sc.sub.setText(gs.phase === 'offer' ? `${plates} plate${plates === 1 ? '' : 's'} left · ${gs.center.length} in the centre` : gs.phase === 'scoring' ? 'tiling the walls' : '');
this.sc.foot.setText(gs.variant === 'free' ? 'Free wall — place tiles anywhere legal' : 'Printed wall');
this.refreshScores();
}
refreshScores() {
const gs = this.gs;
if (!gs) return;
const order = gs.players.map((p) => p.seat).sort((a, b) => this.shown[b] - this.shown[a] || a - b);
order.forEach((seat, i) => {
const r = this.sc.rows[i];
const p = gs.players[seat];
const b = bonusStatus(p);
r.rank.setText(`${i + 1}`);
r.name.setText(seat === 0 ? `${p.name} (you)` : p.name);
r.sub.setText(`${b.rows.filter((n) => n === WALL_SIZE).length ? 'row done · ' : ''}${b.rows.reduce((a, c) => a + c, 0)} on wall${b.locked ? ` · +${b.locked} locked` : ''}`);
r.pts.setText(`${this.shown[seat]}`);
r.bg.clear();
const cur = gs.phase === 'offer' && gs.current === seat;
r.bg.fillStyle(cur ? 0xffe7a8 : seat === 0 ? 0xe6ecf4 : 0xffffff, cur ? 0.9 : 0.45)
.fillRoundedRect(SCORES.x + 22, r.y - 24, SCORES.w - 44, 48, 6);
});
}
// ── order book ───────────────────────────────────────────────────────────────
buildNotepad() {
const s = this.scene;
s.add.text(NOTEPAD.x + 20, NOTEPAD.y + 13, 'Order Book', { fontFamily: DISPLAY, fontSize: '15px', color: '#f3dcc0' }).setOrigin(0, 0.5).setDepth(this.D.label);
this.np = s.add.text(NOTEPAD.x + 54, NOTEPAD.y + 46, '', {
fontFamily: SERIF, fontSize: '14px', fontStyle: 'italic', color: '#1d3f6e', lineSpacing: 10.5, wordWrap: { width: NOTEPAD.w - 74 },
fixedWidth: NOTEPAD.w - 74,
}).setDepth(this.D.label);
const maskG = s.make.graphics({ x: 0, y: 0, add: false });
maskG.fillStyle(0xffffff).fillRect(NOTEPAD.x + 6, NOTEPAD.y + 30, NOTEPAD.w - 12, NOTEPAD.h - 34);
this.np.setMask(maskG.createGeometryMask());
s.events.once('shutdown', () => maskG.destroy());
}
setLog(log) {
// Newest at the bottom: show as many recent lines as fit.
const lines = log.slice(-12);
this.np.setText(lines.join('\n'));
const maxH = NOTEPAD.h - 56;
while (this.np.height > maxH && lines.length > 1) { lines.shift(); this.np.setText(lines.join('\n')); }
}
}