xonecas.ioDon't click! Sean Caetano Martin Sign in
tiny-adventure / web / js / journal.js 83 lines3.0 kB Raw
1import { drawText, wrap, lineHeight } from './text.js';
2import { chrome, PAD } from './chrome.js';
3import { T } from './theme.js';
4
5const INK = T.fg;
6
7function blocks(c, hints, pois) {
8  const out = [];
9  const head = (t, size = 15) => out.push({ t, size, weight: 'bold', color: INK, gap: 8 });
10  const next = c.main.steps.find((s) => !s.done);
11  const steps = (q) => q.steps.forEach((s) => {
12    const lv = pois.find((p) => p.id === s.poi)?.level;
13    const now = s === next;
14    out.push({ t: `${now ? 'Next: ' : ''}${s.text}${lv && !s.done ? ` (Lv ${lv})` : ''}`, size: 13, weight: now ? 'bold' : undefined, color: s.done ? T.faint : now ? T.accent : INK, indent: 20, box: s.done ? 2 : 1 });
15  });
16  head(c.main.name, 17);
17  steps(c.main);
18  for (const q of c.sideQuests) {
19    head(q.name);
20    steps(q);
21  }
22  head('Factions');
23  c.factions.forEach((x) => out.push({ t: `${x.name} (${x.theme}, power ${x.power})`, size: 13, color: T.muted, indent: 8 }));
24  head('Rumours');
25  hints.forEach((s) => out.push({ t: `${s.kind[0].toUpperCase()}${s.kind.slice(1)}: ${s.hint}`, size: 13, color: T.muted, indent: 8 }));
26    return out;
27}
28
29export function createJournal(layer, m, f) {
30  return layer.add('panel-journal', {
31    z: 3,
32    wheel(dy) {
33      m.scrollJournal(dy > 0 ? 44 : -44);
34    },
35    draw(ctx, w, h, wd) {
36      chrome(ctx, w, h, 'Quest Journal', wd, f, m);
37      const top = 62;
38      const bottom = h - PAD + 4;
39      const width = w - PAD * 2 - 12;
40      const lines = [];
41      for (const b of blocks(m.state.campaign, m.state.map.spawns, m.state.map.pois)) {
42        const ls = wrap(ctx, b.t, b.size, b.weight, width - (b.indent || 0));
43        if (b.gap) lines.push({ gap: b.gap });
44        ls.forEach((t, i) => lines.push({ t, b, first: i === 0 }));
45      }
46      const total = lines.reduce((n, l) => n + (l.gap ?? lineHeight(l.b.size)), 0);
47      m.jMax = Math.max(0, total - (bottom - top));
48      m.jScroll = Math.min(m.jScroll, m.jMax);
49      ctx.save();
50      ctx.beginPath();
51      ctx.rect(PAD - 4, top, w - PAD * 2 + 8, bottom - top);
52      ctx.clip();
53      let y = top - m.jScroll;
54      for (const l of lines) {
55        if (l.gap) {
56          y += l.gap;
57          continue;
58        }
59        const { b } = l;
60        const lh = lineHeight(b.size);
61        if (l.first && b.box) {
62          const by = y + (lh - 11) / 2;
63          ctx.strokeStyle = INK;
64          ctx.lineWidth = 1.5;
65          ctx.strokeRect(PAD + 2, by, 11, 11);
66          if (b.box === 2) {
67            ctx.beginPath();
68            ctx.moveTo(PAD + 4, by + 6);
69            ctx.lineTo(PAD + 7, by + 10);
70            ctx.lineTo(PAD + 14, by - 2);
71            ctx.strokeStyle = T.heal;
72            ctx.lineWidth = 2.5;
73            ctx.stroke();
74          }
75        }
76        drawText(ctx, l.t, PAD + (b.indent || 0), y, { size: b.size, weight: b.weight, color: b.color, shadow: null });
77        y += lh;
78      }
79      ctx.restore();
80      f.scrollbar(ctx, w - PAD - 2, top, bottom - top, m.jScroll, bottom - top, total);
81    },
82  });
83}