xonecas.ioDon't click! Sean Caetano Martin Sign in
tiny-adventure / web / js / threat.js 68 lines4.2 kB Raw
1import { drawText } from './text.js';
2import { PAD } from './chrome.js';
3import { T } from './theme.js';
4import { nature, traitName } from './traits.js';
5
6const ROW = 22;
7
8export const oddsWord = (t) => (t.odds >= 85 ? (t.wounds < 35 ? 'Easy' : 'Costly') : t.odds >= 60 ? 'Likely' : t.odds >= 35 ? 'Risky' : 'Deadly');
9export const threatHeight = (t) => 338 + t.enemies.length * ROW;
10const list = (xs) => xs.map(traitName).join(', ');
11const foeAbilities = (foes) => [...new Set(foes.flatMap((e) => e.abilities.map((a) => a.name)))].join(', ');
12
13export function createThreat(layer, m, f) {
14  return layer.add('threat', {
15    z: 4,
16    modal: true,
17    draw(ctx, w, h, wd) {
18      const { threat, poi, faction, hurt } = m.threat;
19      f.panel(ctx, 0, 0, w, h, 'surface', T.muted);
20      drawText(ctx, poi.name, PAD, 12, { h: 28, size: 22, display: true, maxW: w - PAD * 2 - 60, fit: true });
21      drawText(ctx, `Lv ${poi.level}`, w - PAD, 12, { h: 28, size: 15, weight: 'bold', align: 'right', color: T.muted });
22      drawText(ctx, threat.band ? `${faction ? `${faction.name} ` : ''}${poi.kind}` : `${poi.kind}${faction ? `, held by ${faction.name}` : ''}`, PAD, 40, { h: 18, size: 13, color: T.muted, maxW: w - PAD * 2 });
23      f.rule(ctx, PAD, 62, w - PAD * 2);
24      let y = 70;
25      for (const e of threat.enemies) {
26        drawText(ctx, e.name, PAD, y, { h: ROW, size: 14, maxW: w - PAD * 2 - 110 });
27        drawText(ctx, `Lv ${e.level}`, w - PAD - 64, y, { h: ROW, size: 13, color: T.muted, align: 'right' });
28        drawText(ctx, `${e.stats.maxHp} hp`, w - PAD, y, { h: ROW, size: 13, color: T.muted, align: 'right' });
29        y += ROW;
30      }
31      const n = nature(threat.enemies);
32      const e = threat.edge;
33      const line = (label, text, color) => {
34        drawText(ctx, label, PAD, y, { h: 20, size: 12, color: T.faint });
35        drawText(ctx, text || 'nothing', PAD + 78, y, { h: 20, size: 13.5, color: text ? color : T.faint, maxW: w - PAD * 2 - 78, fit: true });
36        y += 20;
37      };
38      y += 4;
39      line('Abilities', foeAbilities(threat.enemies), T.fg);
40      line('Traits', list(n.traits), T.fg);
41      line('Weak to', list(n.weak), T.fg);
42      line('Resists', list(n.resist), T.muted);
43      line('Your edge', e.hits.join(', ') + (e.blunted.length ? `${e.hits.length ? '. ' : ''}Resisted: ${e.blunted.join(', ')}` : ''), T.fg);
44      f.rule(ctx, PAD, y + 6, w - PAD * 2);
45      y += 16;
46      if (threat.barred) {
47        drawText(ctx, 'Sealed', PAD, y, { h: 28, size: 24, display: true, color: T.muted });
48        drawText(ctx, threat.needs?.length ? `Recover the ${threat.needs.join(', the ')} first.` : 'Finish the other main steps before you face this foe.', PAD, y + 34, { h: 18, size: 13, color: T.muted, maxW: w - PAD * 2, fit: true });
49      } else {
50        const bad = threat.odds < 60;
51        drawText(ctx, oddsWord(threat), PAD, y, { h: 28, size: 24, display: true, color: bad ? T.danger : T.fg });
52        drawText(ctx, `${threat.odds}% to win`, w - PAD, y, { h: 28, size: 14, align: 'right', color: bad ? T.danger : T.muted });
53        f.bar(ctx, PAD, y + 32, w - PAD * 2, 4, threat.odds / 100, bad ? T.danger : T.muted);
54        drawText(ctx, `Expect to lose about ${threat.wounds}% of the party's health.`, PAD, y + 42, { h: 18, size: 13.5, maxW: w - PAD * 2, fit: true });
55        const tip = hurt && bad ? 'Your party is wounded. Rest at a camp or drink a potion first.' : bad ? 'Prepare: gear that hits their weakness swings the odds.' : 'Wounds carry over until you rest.';
56        drawText(ctx, tip, PAD, y + 62, { h: 18, size: 13, color: T.muted, maxW: w - PAD * 2, fit: true });
57      }
58      const by = h - PAD - 38;
59      const bw = (w - PAD * 2 - 20) / 3;
60      const items = threat.barred ? [['threat-back', 'Back off', 'Esc', m.threatBack]] : [['threat-fight', 'Fight', '⏎', m.threatFight], ['threat-prepare', 'Prepare', 'G', m.prepare], ['threat-back', 'Back off', 'Esc', m.threatBack]];
61      items.forEach(([name, label, key, fn], i) => {
62        const x = threat.barred ? w - PAD - bw : PAD + i * (bw + 10);
63        f.button(ctx, x, by, bw, 38, label, { hover: wd.hovered(name), primary: name === 'threat-fight', key, size: 15 });
64        wd.hit(x, by, bw, 38, { name, key: name, onClick: fn });
65      });
66    },
67  });
68}