285 lines
15 KiB
JavaScript
285 lines
15 KiB
JavaScript
// BoggleDialogs.js — den pop-ups: a cream card in a teak frame with a
|
||
// three-colour 70s stripe. Each opener returns { close() } and swallows clicks
|
||
// beneath it.
|
||
//
|
||
// openSetup match setup (timer · rounds · board · shortest word · shared words)
|
||
// or solo setup (Practice / Daily Board)
|
||
// openPauseMenu resume · table settings · how to play · new · leave
|
||
// openBetween standings between rounds
|
||
// confirmBox yes / no
|
||
|
||
import { GAME_WIDTH, GAME_HEIGHT } from '../../config.js';
|
||
import { Button } from '../../ui/Button.js';
|
||
import { playSound, SFX } from '../../ui/Sounds.js';
|
||
import { DISPLAY_FONT, SERIF, HAND_FONT, BROWN_HEX, RED_HEX, PENCIL_HEX } from './BoggleArt.js';
|
||
import { TIMER_OPTIONS, ROUND_OPTIONS, UNTIMED, longDate } from './BoggleLogic.js';
|
||
|
||
const BTN = 0x5a3820, BTN_GO = 0xb3501f, BTN_ALT = 0x3f4a1c;
|
||
|
||
export function frame(scene, x, y, w, h, depth) {
|
||
const g = scene.add.graphics().setDepth(depth);
|
||
g.fillStyle(0x000000, 0.45).fillRoundedRect(x + 5, y + 10, w, h, 18);
|
||
g.fillStyle(0x6a3f1e, 1).fillRoundedRect(x, y, w, h, 18);
|
||
g.lineStyle(2, 0xe8c090, 0.55).strokeRoundedRect(x + 5, y + 5, w - 10, h - 10, 14);
|
||
g.fillStyle(0xf8f0da, 1).fillRoundedRect(x + 14, y + 14, w - 28, h - 28, 10);
|
||
const band = [0xd9a63a, 0xd0652a, 0xa33a1c];
|
||
band.forEach((c, i) => g.fillStyle(c, 1).fillRect(x + 14, y + 78 + i * 6, w - 28, 6));
|
||
return g;
|
||
}
|
||
|
||
export function blocker(scene, depth, alpha = 0) {
|
||
return scene.add.rectangle(GAME_WIDTH / 2, GAME_HEIGHT / 2, GAME_WIDTH, GAME_HEIGHT, 0x000000, alpha)
|
||
.setDepth(depth).setInteractive();
|
||
}
|
||
|
||
export function title(scene, x, y, text, depth, size = 34) {
|
||
return scene.add.text(x, y, text, { fontFamily: DISPLAY_FONT, fontSize: `${size}px`, color: BROWN_HEX, align: 'center' })
|
||
.setOrigin(0.5).setDepth(depth);
|
||
}
|
||
|
||
export function handle(objs, onClose) {
|
||
let closed = false;
|
||
return {
|
||
objs,
|
||
close() {
|
||
if (closed) return;
|
||
closed = true;
|
||
objs.forEach((o) => o?.destroy?.());
|
||
onClose?.();
|
||
},
|
||
};
|
||
}
|
||
|
||
export function button(scene, x, y, label, fn, depth, { w = 240, h = 52, size = 20, bg = BTN } = {}) {
|
||
return new Button(scene, x, y, label, fn, { width: w, height: h, fontSize: size, bg }).setDepth(depth);
|
||
}
|
||
|
||
// A labelled row of mutually exclusive chips. opts: [{ label, value }].
|
||
function chipRow(scene, objs, { x, y, w, label, opts, get, set, depth, chipW = null }) {
|
||
objs.push(scene.add.text(x, y, label, { fontFamily: SERIF, fontSize: '19px', color: PENCIL_HEX, fontStyle: 'italic' })
|
||
.setOrigin(0, 0.5).setDepth(depth + 1));
|
||
const x0 = x + 200;
|
||
const avail = w - 200;
|
||
const cw = chipW ?? Math.min(150, Math.floor((avail - (opts.length - 1) * 10) / opts.length));
|
||
const chips = opts.map((o, i) => {
|
||
const cx = x0 + i * (cw + 10) + cw / 2;
|
||
const g = scene.add.graphics().setDepth(depth + 1);
|
||
const t = scene.add.text(cx, y, o.label, { fontFamily: DISPLAY_FONT, fontSize: '19px', color: BROWN_HEX }).setOrigin(0.5).setDepth(depth + 2);
|
||
const hit = scene.add.rectangle(cx, y, cw, 42, 0xffffff, 0.001).setDepth(depth + 3).setInteractive({ useHandCursor: true });
|
||
hit.on('pointerdown', () => { set(o.value); playSound(scene, SFX.PIECE_CLICK); draw(); });
|
||
objs.push(g, t, hit);
|
||
return { o, g, t, cx };
|
||
});
|
||
const draw = () => {
|
||
for (const c of chips) {
|
||
const on = c.o.value === get();
|
||
c.g.clear();
|
||
c.g.fillStyle(on ? 0xc2551f : 0xffffff, on ? 1 : 0.55).fillRoundedRect(c.cx - cw / 2, y - 21, cw, 42, 21);
|
||
c.g.lineStyle(2, on ? 0x7a2a10 : 0xa08060, 0.9).strokeRoundedRect(c.cx - cw / 2, y - 21, cw, 42, 21);
|
||
c.t.setColor(on ? '#fff6e6' : BROWN_HEX);
|
||
}
|
||
};
|
||
draw();
|
||
return draw;
|
||
}
|
||
|
||
// ── setup ───────────────────────────────────────────────────────────────────
|
||
// solo: { mode, practice: {...rules, hints}, daily: { today, played, streak } }
|
||
export function openSetup(scene, { solo, rules, rivals = [], soloState = null, depth, onStart, onHelp, onLeave, onClose }) {
|
||
const r = { ...rules };
|
||
const st = soloState ? { mode: soloState.mode, hints: soloState.hints } : null;
|
||
const objs = [blocker(scene, depth - 1, 0.55)];
|
||
const w = 960, h = solo ? 720 : 700;
|
||
const x = GAME_WIDTH / 2 - w / 2, y = GAME_HEIGHT / 2 - h / 2;
|
||
const cx = x + w / 2;
|
||
objs.push(frame(scene, x, y, w, h, depth));
|
||
objs.push(title(scene, cx, y + 48, solo ? 'Boggle — Solo' : 'Boggle — Game Night', depth + 1));
|
||
let hRef = null;
|
||
|
||
const rowX = x + 80, rowW = w - 160;
|
||
const dynamic = [];
|
||
const clearDynamic = () => { dynamic.forEach((o) => o.destroy()); dynamic.length = 0; };
|
||
|
||
const buildRules = (y0, { timed = true, untimed = false, rounds = true, dup = true, hints = false }) => {
|
||
let yy = y0;
|
||
const timerOpts = TIMER_OPTIONS.map((t) => ({ label: t.label, value: t.ms }));
|
||
if (untimed) timerOpts.push({ label: 'Untimed', value: UNTIMED });
|
||
if (timed) { chipRow(scene, dynamic, { x: rowX, y: yy, w: rowW, label: 'Round length', opts: timerOpts, get: () => r.timerMs, set: (v) => { r.timerMs = v; }, depth }); yy += 66; }
|
||
if (rounds) { chipRow(scene, dynamic, { x: rowX, y: yy, w: rowW, label: 'Rounds', opts: ROUND_OPTIONS.map((n) => ({ label: `${n}`, value: n })), get: () => r.rounds, set: (v) => { r.rounds = v; }, depth, chipW: 110 }); yy += 66; }
|
||
const drawMin = [];
|
||
chipRow(scene, dynamic, {
|
||
x: rowX, y: yy, w: rowW, label: 'Board', depth, chipW: 220,
|
||
opts: [{ label: 'Classic 4 × 4', value: 4 }, { label: 'Big 5 × 5', value: 5 }],
|
||
get: () => r.size, set: (v) => { r.size = v; r.minLen = v === 5 ? 4 : 3; drawMin[0]?.(); },
|
||
});
|
||
yy += 66;
|
||
drawMin[0] = chipRow(scene, dynamic, {
|
||
x: rowX, y: yy, w: rowW, label: 'Shortest word', depth, chipW: 160,
|
||
opts: [{ label: '3 letters', value: 3 }, { label: '4 letters', value: 4 }],
|
||
get: () => r.minLen, set: (v) => { r.minLen = v; },
|
||
});
|
||
yy += 66;
|
||
if (dup) {
|
||
chipRow(scene, dynamic, {
|
||
x: rowX, y: yy, w: rowW, label: 'Shared words', depth, chipW: 220,
|
||
opts: [{ label: 'Cancel out', value: true }, { label: 'Still score', value: false }],
|
||
get: () => r.dupCancel, set: (v) => { r.dupCancel = v; },
|
||
});
|
||
yy += 66;
|
||
}
|
||
if (hints) {
|
||
chipRow(scene, dynamic, {
|
||
x: rowX, y: yy, w: rowW, label: 'Hints', depth, chipW: 160,
|
||
opts: [{ label: 'On', value: true }, { label: 'Off', value: false }],
|
||
get: () => st.hints, set: (v) => { st.hints = v; },
|
||
});
|
||
yy += 66;
|
||
}
|
||
return yy;
|
||
};
|
||
|
||
const startBtn = button(scene, cx - 250, y + h - 64, 'Start', () => {
|
||
if (solo && st.mode === 'daily' && soloState.daily.played) return;
|
||
hRef.close();
|
||
onStart(solo ? { mode: st.mode, rules: r, hints: st.hints } : { mode: 'match', rules: r });
|
||
}, depth + 2, { w: 250, h: 56, size: 22, bg: BTN_GO });
|
||
objs.push(startBtn);
|
||
objs.push(button(scene, cx + 10, y + h - 64, 'How to Play', () => onHelp?.(), depth + 2, { w: 220, h: 56, size: 20, bg: BTN_ALT }));
|
||
objs.push(button(scene, cx + 250, y + h - 64, 'Leave', () => { hRef.close(); onLeave?.(); }, depth + 2, { w: 200, h: 56, size: 20, bg: BTN }));
|
||
|
||
if (!solo) {
|
||
const names = rivals.map((p) => `${p.name} (skill ${p.skill ?? 3})`).join(' · ');
|
||
objs.push(scene.add.text(cx, y + 128, `Tonight at the table: ${names}`, {
|
||
fontFamily: SERIF, fontSize: '19px', fontStyle: 'italic', color: PENCIL_HEX, align: 'center', wordWrap: { width: w - 120 },
|
||
}).setOrigin(0.5).setDepth(depth + 1));
|
||
buildRules(y + 196, {});
|
||
objs.push(scene.add.text(cx, y + h - 128, 'Words found by two or more players are crossed off every sheet when "Cancel out" is on.', {
|
||
fontFamily: SERIF, fontSize: '16px', fontStyle: 'italic', color: PENCIL_HEX, align: 'center', wordWrap: { width: w - 140 },
|
||
}).setOrigin(0.5).setDepth(depth + 1));
|
||
} else {
|
||
const modeY = y + 140;
|
||
const drawSolo = () => {
|
||
clearDynamic();
|
||
if (st.mode === 'practice') {
|
||
r.rounds = 1;
|
||
buildRules(modeY + 76, { untimed: true, rounds: false, dup: false, hints: true });
|
||
const tip = scene.add.text(cx, y + h - 136, 'Practice goal: find a quarter of the board\'s common words. Untimed boards end when you press Finish.', {
|
||
fontFamily: SERIF, fontSize: '16px', fontStyle: 'italic', color: PENCIL_HEX, align: 'center', wordWrap: { width: w - 140 },
|
||
}).setOrigin(0.5).setDepth(depth + 1);
|
||
dynamic.push(tip);
|
||
startBtn.setEnabled(true).setLabel('Start');
|
||
} else {
|
||
const d = soloState.daily;
|
||
const lines = [
|
||
`${longDate(d.today)}`,
|
||
'Everyone gets the same board today — one attempt.',
|
||
'Classic 4 × 4 · 3 minutes · 3-letter words count',
|
||
'',
|
||
d.played ? `Today: ${d.played.score} pts · ${d.played.words} words · ${d.played.commonPct}% of common words` : 'Not played yet today.',
|
||
`Daily streak: ${d.streak}${d.bestStreak ? ` (best ${d.bestStreak})` : ''}`,
|
||
];
|
||
const t = scene.add.text(cx, modeY + 210, lines.join('\n'), {
|
||
fontFamily: SERIF, fontSize: '21px', color: '#2b2418', align: 'center', lineSpacing: 8,
|
||
}).setOrigin(0.5).setDepth(depth + 1);
|
||
dynamic.push(t);
|
||
if (d.played) startBtn.setEnabled(false).setLabel('Come back tomorrow');
|
||
else startBtn.setEnabled(true).setLabel("Play Today's Board");
|
||
}
|
||
};
|
||
chipRow(scene, objs, {
|
||
x: rowX, y: modeY, w: rowW, label: 'Mode', depth, chipW: 220,
|
||
opts: [{ label: 'Practice', value: 'practice' }, { label: 'Daily Board', value: 'daily' }],
|
||
get: () => st.mode, set: (v) => { st.mode = v; drawSolo(); },
|
||
});
|
||
drawSolo();
|
||
}
|
||
|
||
hRef = handle(objs, () => { clearDynamic(); onClose?.(); });
|
||
return hRef;
|
||
}
|
||
|
||
// ── pause menu ──────────────────────────────────────────────────────────────
|
||
const onOff = (v) => (v ? 'On' : 'Off');
|
||
|
||
export function openPauseMenu(scene, { settings, depth, midRound, onChange, onHelp, onNew, onLeave, onClose }) {
|
||
const objs = [blocker(scene, depth - 1, 0.5)];
|
||
const w = 560, h = 680;
|
||
const x = GAME_WIDTH / 2 - w / 2, y = GAME_HEIGHT / 2 - h / 2;
|
||
const cx = x + w / 2;
|
||
objs.push(frame(scene, x, y, w, h, depth));
|
||
objs.push(title(scene, cx, y + 48, midRound ? 'Paused' : 'Game Night', depth + 1));
|
||
let hRef = null;
|
||
const rows = [
|
||
['Resume', () => hRef.close(), BTN_GO],
|
||
[() => `Upright letters: ${onOff(settings.upright)}`, () => onChange('upright', !settings.upright)],
|
||
[() => `Release to submit (drag): ${onOff(settings.releaseSubmit)}`, () => onChange('releaseSubmit', !settings.releaseSubmit)],
|
||
[() => `Rival pencil sounds: ${onOff(settings.rivalSounds)}`, () => onChange('rivalSounds', !settings.rivalSounds)],
|
||
[() => `Last-ten-seconds tick: ${onOff(settings.tick)}`, () => onChange('tick', !settings.tick)],
|
||
['How to Play', () => onHelp?.(), BTN_ALT],
|
||
['New Game', () => { onNew(); hRef.close(); }, 0x6a4a1a],
|
||
['Leave the Den', () => { onLeave(); hRef.close(); }, 0x7a2a1a],
|
||
];
|
||
rows.forEach(([label, fn, bg], i) => {
|
||
const text = typeof label === 'function' ? label() : label;
|
||
const btn = new Button(scene, cx, y + 136 + i * 58, text, () => {
|
||
fn();
|
||
if (typeof label === 'function') btn.setLabel(label());
|
||
}, { width: 460, height: 48, fontSize: 18, bg: bg ?? BTN }).setDepth(depth + 2);
|
||
objs.push(btn);
|
||
});
|
||
objs.push(scene.add.text(cx, y + h - 46, 'Keys: type letters · Enter submit · Backspace · Esc clear / pause · Space shake', {
|
||
fontFamily: SERIF, fontSize: '14px', fontStyle: 'italic', color: '#5a4428', align: 'center', wordWrap: { width: w - 70 },
|
||
}).setOrigin(0.5).setDepth(depth + 1));
|
||
hRef = handle(objs, onClose);
|
||
return hRef;
|
||
}
|
||
|
||
// ── between rounds ──────────────────────────────────────────────────────────
|
||
export function openBetween(scene, { match, depth, onNext, onMenu }) {
|
||
const objs = [blocker(scene, depth - 1, 0.55)];
|
||
const n = match.players.length;
|
||
const rounds = match.history.length;
|
||
const w = 900, h = 330 + n * 58;
|
||
const x = GAME_WIDTH / 2 - w / 2, y = GAME_HEIGHT / 2 - h / 2;
|
||
const cx = x + w / 2;
|
||
objs.push(frame(scene, x, y, w, h, depth));
|
||
objs.push(title(scene, cx, y + 48, `After Round ${rounds} of ${match.rules.rounds}`, depth + 1));
|
||
const colX = (k) => x + 380 + k * 110;
|
||
for (let k = 0; k < rounds; k++) {
|
||
objs.push(scene.add.text(colX(k), y + 132, `R${k + 1}`, { fontFamily: DISPLAY_FONT, fontSize: '20px', color: PENCIL_HEX }).setOrigin(0.5).setDepth(depth + 1));
|
||
}
|
||
objs.push(scene.add.text(x + w - 90, y + 132, 'Total', { fontFamily: DISPLAY_FONT, fontSize: '20px', color: PENCIL_HEX }).setOrigin(0.5).setDepth(depth + 1));
|
||
const order = [...match.players].sort((a, b) => match.totals[b.id] - match.totals[a.id]);
|
||
order.forEach((p, i) => {
|
||
const yy = y + 182 + i * 58;
|
||
objs.push(scene.add.text(x + 70, yy, `${i + 1}. ${p.name}`, {
|
||
fontFamily: HAND_FONT, fontSize: '36px', color: p.isHuman ? RED_HEX : '#2b2418',
|
||
}).setOrigin(0, 0.5).setDepth(depth + 1));
|
||
match.history.forEach((hh, k) => {
|
||
objs.push(scene.add.text(colX(k), yy, String(hh.scores[p.id] ?? 0), { fontFamily: HAND_FONT, fontSize: '32px', color: '#2b2418' }).setOrigin(0.5).setDepth(depth + 1));
|
||
});
|
||
objs.push(scene.add.text(x + w - 90, yy, String(match.totals[p.id]), { fontFamily: DISPLAY_FONT, fontSize: '30px', color: BROWN_HEX }).setOrigin(0.5).setDepth(depth + 1));
|
||
});
|
||
let hRef = null;
|
||
objs.push(button(scene, cx - 140, y + h - 60, `Round ${rounds + 1}`, () => { hRef.close(); onNext(); }, depth + 2, { w: 250, h: 56, size: 22, bg: BTN_GO }));
|
||
objs.push(button(scene, cx + 140, y + h - 60, 'Menu', () => onMenu(), depth + 2, { w: 200, h: 56, size: 20 }));
|
||
hRef = handle(objs);
|
||
return hRef;
|
||
}
|
||
|
||
export function confirmBox(scene, { heading, text, yes = 'Yes', no = 'Cancel', depth, onYes, onNo }) {
|
||
const objs = [blocker(scene, depth - 1, 0.5)];
|
||
const w = 580, h = 290;
|
||
const x = GAME_WIDTH / 2 - w / 2, y = GAME_HEIGHT / 2 - h / 2;
|
||
objs.push(frame(scene, x, y, w, h, depth));
|
||
objs.push(title(scene, x + w / 2, y + 48, heading, depth + 1, 28));
|
||
objs.push(scene.add.text(x + w / 2, y + 150, text, {
|
||
fontFamily: SERIF, fontSize: '18px', color: '#2b2418', align: 'center', wordWrap: { width: w - 80 },
|
||
}).setOrigin(0.5).setDepth(depth + 1));
|
||
let hRef = null;
|
||
objs.push(button(scene, x + w / 2 - 130, y + h - 52, yes, () => { hRef.close(); onYes?.(); }, depth + 2, { w: 230, h: 48, size: 18, bg: 0x7a2a1a }));
|
||
objs.push(button(scene, x + w / 2 + 130, y + h - 52, no, () => { hRef.close(); onNo?.(); }, depth + 2, { w: 230, h: 48, size: 18 }));
|
||
hRef = handle(objs);
|
||
return hRef;
|
||
}
|