1import { drawText, wrap, lineHeight } from './text.js'; 2import { PAD } from './chrome.js'; 3import { T } from './theme.js'; 4 5const fmtTime = (s) => (s >= 3600 ? `${Math.floor(s / 3600)}h ${String(Math.floor((s % 3600) / 60)).padStart(2, '0')}m` : `${Math.floor(s / 60)}m ${String(Math.floor(s % 60)).padStart(2, '0')}s`); 6const fmtNum = (n) => (n >= 10000 ? `${Math.round(n / 1000)}k` : n >= 1000 ? `${(n / 1000).toFixed(1)}k` : String(n)); 7const names = (list) => (list.length < 2 ? list.join('') : `${list.slice(0, -1).join(', ')} and ${list.at(-1)}`); 8 9function paragraph(ctx, text, y, w, o) { 10 const lines = wrap(ctx, text, o.size, o.weight, w - PAD * 2, o.display); 11 lines.forEach((t, i) => drawText(ctx, t, o.align === 'left' ? PAD : w / 2, y + i * lineHeight(o.size), { align: 'center', ...o })); 12 return y + lines.length * lineHeight(o.size); 13} 14 15function buttons(ctx, w, y, labels, wd, f) { 16 const inner = w - PAD * 2; 17 const bw = (inner - (labels.length - 1) * 10) / labels.length; 18 labels.forEach(([label, fn, key], i) => { 19 const x = PAD + i * (bw + 10); 20 const name = `button-${i}`; 21 f.button(ctx, x, y, bw, 40, label, { hover: wd.hovered(name), primary: i === 0, size: 15, key: bw > 130 ? key : '' }); 22 wd.hit(x, y, bw, 40, { name, key: name, onClick: fn }); 23 }); 24 return y + 40; 25} 26 27export const started = (s) => s.clock.day > 1 || s.clock.time > 0 || s.summary.battles > 0; 28 29function title(ctx, w, h, wd, f, m) { 30 const { state: s, onStart, onNew } = m.screenData; 31 const c = s.campaign; 32 drawText(ctx, 'Tiny Adventure', w / 2, 22, { h: 48, size: 42, display: true, align: 'center', maxW: w - PAD * 2, fit: true }); 33 let y = paragraph(ctx, 'A seeded tabletop campaign. Your board is yours alone and saves as you play.', 74, w, { size: 13, color: T.muted }); 34 f.rule(ctx, PAD, y + 10, w - PAD * 2); 35 y = paragraph(ctx, c.main.name, y + 22, w, { size: 22, display: true, color: T.accent }); 36 y = paragraph(ctx, `Seed ${s.seed}. ${names(s.party.units.map((u) => `${u.name} the ${u.class}`))}.`, y + 6, w, { size: 13.5, color: T.muted }); 37 y += 10; 38 for (const line of [ 39 'Click the board to walk. Step up to a marked place to size up its defenders and your odds, then fight or back off.', 40 'Every hex walked spends daylight. After dusk, make camp to heal and greet a new day.', 41 'A hero at 0 hp falls and sits out until a potion or a night at camp brings them back. If the whole party falls, the campaign ends.', 42 ]) y = paragraph(ctx, line, y + 6, w, { size: 13.5, align: 'left', color: T.fg }); 43 const by = h - PAD - 40 - 28; 44 buttons(ctx, w, by, [[started(s) ? `Continue, day ${s.clock.day}` : 'Begin', onStart, '⏎'], ['New campaign', onNew, 'N']], wd, f); 45 paragraph(ctx, 'I inventory, J journal, T trader, C camp, P potion, M sound, Q and E rotate', by + 50, w, { size: 12, color: T.faint }); 46} 47 48function tiles(ctx, w, y, list, f) { 49 const cols = 4; 50 const gap = 6; 51 const tw = (w - PAD * 2 - gap * (cols - 1)) / cols; 52 const th = 46; 53 list.forEach(([value, label], i) => { 54 const x = PAD + (i % cols) * (tw + gap); 55 const ty = y + Math.floor(i / cols) * (th + gap); 56 f.panel(ctx, x, ty, tw, th, 'raised', null); 57 drawText(ctx, value, x + tw / 2, ty + 4, { h: 24, size: 19, weight: 'bold', align: 'center', maxW: tw - 8, fit: true }); 58 drawText(ctx, label, x + tw / 2, ty + 26, { h: 16, size: 11.5, color: T.muted, align: 'center', maxW: tw - 8, fit: true }); 59 }); 60 return y + Math.ceil(list.length / cols) * (th + gap); 61} 62 63function end(ctx, w, h, wd, f, m) { 64 const { run, own, copied, onShare, onSeed, onNew } = m.screenData; 65 const sum = run.summary; 66 const won = run.phase === 'victory'; 67 drawText(ctx, won ? `Victory on day ${run.day}` : `Fell on day ${run.day}`, w / 2, 18, { h: 50, size: 40, display: true, align: 'center', color: won ? T.accent : T.danger, maxW: w - PAD * 2, fit: true }); 68 const who = names(run.party.map((u) => u.name)); 69 let y = paragraph(ctx, won ? `${who} slew ${run.boss}. ${run.mission} is complete.` : `${who} fell on the road to ${run.boss}. ${run.mission} is left undone.`, 72, w, { size: 14, color: T.muted }); 70 y = tiles(ctx, w, y + 12, [ 71 [String(sum.battles), 'battles'], 72 [String(sum.kills), 'foes slain'], 73 [`${run.keys}/${run.keysOf}`, 'keys'], 74 [fmtNum(sum.hexes), 'hexes walked'], 75 [fmtNum(sum.dealt), 'damage dealt'], 76 [fmtNum(sum.taken), 'damage taken'], 77 [String(sum.best), 'best hit'], 78 [String(sum.crits), 'critical hits'], 79 [String(sum.fallen), 'heroes fallen'], 80 [String(sum.potions), 'potions drunk'], 81 [String(sum.camps), 'nights camped'], 82 [fmtNum(run.gold), 'gold'], 83 ], f); 84 y = paragraph(ctx, run.party.map((u) => `${u.name}, level ${u.level} ${u.class}.`).join(' '), y + 6, w, { size: 13, color: T.muted }); 85 paragraph(ctx, `Seed ${run.seed}, ${fmtTime(sum.seconds)} at the table`, y + 2, w, { size: 12, color: T.faint }); 86 const list = own ? [['New game', onNew, '⏎'], ['Play this seed', onSeed, ''], [copied ? 'Link copied' : 'Copy link', onShare, '']] : [['Play this seed', onSeed, '⏎'], ['New game', onNew, '']]; 87 buttons(ctx, w, h - PAD - 40, list, wd, f); 88} 89 90export function createScreens(layer, m, f) { 91 const scrim = layer.add('scrim', { 92 z: 5, 93 modal: true, 94 lowres: true, 95 essential: false, 96 draw(ctx, w, h) { 97 ctx.fillStyle = m.screen === 'end' ? '#0c0a08d0' : '#0c0a0860'; 98 ctx.fillRect(0, 0, w, h); 99 }, 100 }); 101 const card = layer.add('card', { 102 z: 6, 103 draw(ctx, w, h, wd) { 104 f.panel(ctx, 0, 0, w, h, 'surface'); 105 (m.screen === 'end' ? end : title)(ctx, w, h, wd, f, m); 106 }, 107 }); 108 return { scrim, card }; 109}