xonecas.ioDon't click! Sean Caetano Martin Sign in
tiny-adventure / web / js / hud.js 226 lines8.8 kB Raw
1import { bearing } from './compass.js';
2import { drawText, wrap } from './text.js';
3import { maxHp, total } from './stats.js';
4import { T } from './theme.js';
5import { LIP } from './layout.js';
6
7const LOG_COLORS = { head: T.fg, crit: T.accent, kill: T.danger, reward: T.heal, hint: T.muted };
8const LOG_SIZE = 12.5;
9
10export function logRows(ctx, log, width) {
11  const rows = [];
12  for (const l of log.slice(-8)) wrap(ctx, l.text, LOG_SIZE, l.cls === 'head' ? 'bold' : 'normal', width).forEach((t) => rows.push({ t, cls: l.cls }));
13  return rows;
14}
15
16function potion(ctx, x, y, color) {
17  ctx.fillStyle = color;
18  ctx.beginPath();
19  ctx.arc(x, y + 2, 5, 0, Math.PI * 2);
20  ctx.fill();
21  ctx.fillRect(x - 2, y - 7, 4, 5);
22}
23
24function dial(ctx, x, y, clock) {
25  const span = clock.dayLength + clock.nightLength;
26  const night = clock.time >= clock.dayLength;
27  const w = 34;
28  ctx.fillStyle = T.raised;
29  ctx.fillRect(x, y - 2, w * (clock.dayLength / span), 4);
30  ctx.fillStyle = T.bg;
31  ctx.fillRect(x + w * (clock.dayLength / span), y - 2, w * (clock.nightLength / span), 4);
32  const px = x + w * (clock.time / span);
33  ctx.fillStyle = night ? T.fg : T.accent;
34  ctx.beginPath();
35  ctx.arc(px, y, 4, 0, Math.PI * 2);
36  ctx.fill();
37  if (night) {
38    ctx.fillStyle = T.surface;
39    ctx.beginPath();
40    ctx.arc(px + 1.8, y - 1.2, 3.2, 0, Math.PI * 2);
41    ctx.fill();
42  }
43}
44
45function keyIcon(ctx, x, y, color) {
46  ctx.strokeStyle = color;
47  ctx.lineWidth = 2;
48  ctx.beginPath();
49  ctx.arc(x - 3, y, 3.5, 0, Math.PI * 2);
50  ctx.moveTo(x + 0.5, y);
51  ctx.lineTo(x + 8, y);
52  ctx.moveTo(x + 6, y);
53  ctx.lineTo(x + 6, y + 3.5);
54  ctx.stroke();
55}
56
57function coin(ctx, x, y) {
58  ctx.fillStyle = T.muted;
59  ctx.beginPath();
60  ctx.arc(x, y, 5, 0, Math.PI * 2);
61  ctx.fill();
62}
63
64export function createHud(layer, m, f) {
65  const topbar = layer.add('topbar', {
66    z: 2,
67    draw(ctx, w, h) {
68      f.panel(ctx, 0, 0, w, h, 'surface');
69      const s = m.state;
70      const next = s.campaign.main.steps.find((q) => !q.done);
71      const poi = next && s.map.pois.find((q) => q.id === next.poi);
72      const text = m.toast || (next ? `${next.text}${poi ? `  ยท  ${bearing(s.party, poi)}` : ''}` : 'The deed is done.');
73      ctx.fillStyle = T.accent;
74      ctx.beginPath();
75      ctx.moveTo(14, h / 2 - 5);
76      ctx.lineTo(19, h / 2);
77      ctx.lineTo(14, h / 2 + 5);
78      ctx.lineTo(9, h / 2);
79      ctx.fill();
80      const purse = 112;
81      const want = s.campaign.main.steps.filter((q) => q.slots.item).length;
82      const keys = want ? 62 : 0;
83      const time = 104 + keys;
84      const night = s.clock.time >= s.clock.dayLength;
85      drawText(ctx, text, 28, 0, { h, size: 14, color: m.toast ? T.accent : T.fg, maxW: w - 28 - purse - time - 8, fit: true });
86      drawText(ctx, `Day ${s.clock.day}`, w - purse - time + 6, 0, { h, size: 14, weight: 'bold', color: night ? T.accent : T.fg });
87      dial(ctx, w - purse - keys - 44, h / 2, s.clock);
88      if (want) {
89        const kx = w - purse - keys + 2;
90        keyIcon(ctx, kx + 4, h / 2, s.party.keys.length === want ? T.accent : T.muted);
91        drawText(ctx, `${s.party.keys.length}/${want}`, kx + 16, 0, { h, size: 14, weight: 'bold', color: s.party.keys.length === want ? T.accent : T.fg });
92      }
93      coin(ctx, w - purse + 8, h / 2);
94      drawText(ctx, String(s.party.gold), w - purse + 18, 0, { h, size: 14, weight: 'bold' });
95      potion(ctx, w - 40, h / 2, T.muted);
96      drawText(ctx, String(s.party.potions), w - 30, 0, { h, size: 14, weight: 'bold', color: s.party.potions ? T.fg : T.faint });
97    },
98  });
99
100  const log = layer.add('log', {
101    z: 2,
102    passive: true,
103    draw(ctx, w, h) {
104      ctx.globalAlpha = 0.88;
105      f.panel(ctx, 0, 0, w, h, 'bg', null);
106      ctx.globalAlpha = 1;
107      const n = Math.floor((h - 12) / 17);
108      logRows(ctx, m.log, w - 16).slice(-n).forEach((l, i) => drawText(ctx, l.t, 8, 6 + i * 17, { h: 17, size: LOG_SIZE, color: LOG_COLORS[l.cls] || T.muted, weight: l.cls === 'head' ? 'bold' : 'normal' }));
109    },
110  });
111
112  const bottom = layer.add('bottom', {
113    z: 1,
114    draw(ctx, w, h) {
115      ctx.fillStyle = T.bg;
116      ctx.fillRect(0, LIP, w, h - LIP);
117      ctx.fillStyle = T.line;
118      ctx.fillRect(0, LIP, w, 1);
119    },
120  });
121
122  const portraits = layer.add('portraits', {
123    z: 2,
124    draw(ctx, w, h, wd) {
125      const units = m.state.party.units;
126      const gap = 8;
127      const cw = (w - gap * 3) / 4;
128      const small = cw < 120;
129      const top = small ? 8 : 14;
130      const d = small ? 38 : 56;
131      wd.spots = [];
132      units.forEach((u, i) => {
133        const x = i * (cw + gap);
134        const max = maxHp(u);
135        const cur = Math.max(0, m.hp ? m.hp.get(u.id) ?? u.stats.hp : u.stats.hp);
136        const sel = u.id === m.selected;
137        const hov = wd.hovered(`portrait-${i}`);
138        const low = cur / max < 0.35;
139        f.panel(ctx, x, top, cw, h - top, sel ? 'raised' : 'surface', sel ? T.accent : hov ? T.muted : T.line);
140        wd.spots.push({ id: u.id, cx: x + 6 + d / 2, cy: d / 2, d, down: !cur, rim: sel ? T.accent : low ? T.danger : hov ? T.muted : T.line });
141        const tx = x + 6 + d + 6;
142        const tw = x + cw - 6 - tx;
143        drawText(ctx, u.name, tx, top + 3, { h: small ? 15 : 18, size: small ? 11.5 : 14, weight: 'bold', maxW: tw, fit: true, color: cur ? T.fg : T.faint });
144        drawText(ctx, `Lv ${u.level} ${u.class}`, tx, top + (small ? 18 : 21), { h: small ? 13 : 16, size: small ? 9.5 : 11.5, color: T.muted, maxW: tw, fit: true });
145        if (!small) drawText(ctx, `${total(u, 'attack')} atk  ${total(u, 'defense')} def  ${total(u, 'magic')} mag`, x + 8, d + 1, { h: 16, size: 11, color: T.faint, maxW: cw - 16, fit: true });
146        const bh = small ? 12 : 14;
147        const by = h - 10 - bh;
148        const bw = cw - 16;
149        f.bar(ctx, x + 8, by, bw, bh, cur / max, low ? T.danger : T.muted);
150        const split = x + 8 + bw * Math.min(1, cur / max);
151        [[x, split - x, low ? T.fg : T.bg], [split, x + cw - split, T.fg]].forEach(([cx, cw2, color]) => {
152          ctx.save();
153          ctx.beginPath();
154          ctx.rect(cx, by, cw2, bh);
155          ctx.clip();
156          drawText(ctx, cur ? `${cur}/${max}` : 'Fallen', x + cw / 2, by, { h: bh, size: small ? 10 : 11, align: 'center', weight: 'bold', color });
157          ctx.restore();
158        });
159        f.bar(ctx, x + 8, h - 7, bw, 2, Math.min(1, u.xp / (40 * u.level)), T.faint);
160        wd.hit(x, 0, cw, h, { name: `portrait-${i}`, key: `portrait-${i}`, onClick: () => m.select(u.id) });
161      });
162    },
163  });
164
165  const menu = layer.add('menu', {
166    z: 2,
167    draw(ctx, w, h, wd) {
168      const s = m.state;
169      const items = [['btn-inv', 'Inventory', 'I', 'inv'], ['btn-journal', 'Journal', 'J', 'journal']];
170      if (s.shop) items.push(['btn-shop', 'Trader', 'T', 'shop']);
171      if (s.clock.time >= s.clock.dayLength && !m.combat && !m.threat && !m.camp) items.push(['btn-camp', 'Make camp', 'C', null]);
172      const grid = h > 60;
173      const gap = 6;
174      const cols = grid ? 2 : items.length;
175      const rows = grid ? 2 : 1;
176      const bw = (w - gap * (cols - 1)) / cols;
177      const bh = (h - gap * (rows - 1)) / rows;
178      items.forEach(([name, label, key, panel], i) => {
179        const x = (i % cols) * (bw + gap);
180        const y = Math.floor(i / cols) * (bh + gap);
181        f.button(ctx, x, y, bw, bh, label, { hover: wd.hovered(name), focus: panel && m.panel === panel, primary: !panel, key: grid ? key : '', size: 13.5 });
182        wd.hit(x, y, bw, bh, { name, key: name, onClick: () => (panel ? m.togglePanel(panel) : m.rest()) });
183      });
184    },
185  });
186
187  const banner = layer.add('combat-banner', {
188    z: 2,
189    passive: true,
190    draw(ctx, w, h) {
191      f.panel(ctx, 0, 0, w, h, 'surface', T.danger);
192      drawText(ctx, m.combatTitle || 'Fight', w / 2, 0, { h, size: 17, display: true, align: 'center', maxW: w - 16, fit: true });
193    },
194  });
195
196  const camp = layer.add('camp-banner', {
197    z: 2,
198    passive: true,
199    draw(ctx, w, h) {
200      const { day, k } = m.camp;
201      const dawn = k > 0.85;
202      f.panel(ctx, 0, 0, w, h, 'surface', dawn ? T.accent : T.line);
203      drawText(ctx, dawn ? `Dawn of day ${day + 1}` : `Night ${day} passes by the fire`, w / 2, 6, { h: 24, size: 16, display: true, align: 'center', color: dawn ? T.accent : T.fg, maxW: w - 16, fit: true });
204      const x = 18;
205      const bw = w - 36;
206      const y = h - 16;
207      ctx.fillStyle = T.bg;
208      ctx.fillRect(x, y - 2, bw, 4);
209      ctx.fillStyle = T.faint;
210      ctx.fillRect(x, y - 2, bw * k, 4);
211      const px = x + bw * k;
212      ctx.fillStyle = dawn ? T.accent : T.fg;
213      ctx.beginPath();
214      ctx.arc(px, y, 5, 0, Math.PI * 2);
215      ctx.fill();
216      if (!dawn) {
217        ctx.fillStyle = T.surface;
218        ctx.beginPath();
219        ctx.arc(px + 2.2, y - 1.5, 4, 0, Math.PI * 2);
220        ctx.fill();
221      }
222    },
223  });
224
225  return { topbar, log, bottom, portraits, menu, 'combat-banner': banner, 'camp-banner': camp };
226}