xonecas.ioDon't click! Sean Caetano Martin Sign in
tiny-adventure / web / js / inventory.js 132 lines7.5 kB Raw
1import { drawText } from './text.js';
2import { chrome, PAD } from './chrome.js';
3import { T, rarityColor } from './theme.js';
4import { total, maxHp, bonusText, compareText } from './stats.js';
5import { traitText, traitName, heroTraits } from './traits.js';
6import { abilityName } from './abilities.js';
7import { oddsWord } from './threat.js';
8
9export const SLOTS = ['weapon', 'armor', 'helm', 'trinket'];
10const STATS = [['attack', 'Attack'], ['defense', 'Defense'], ['magic', 'Magic'], ['speed', 'Speed'], ['crit', 'Crit %']];
11const CELL = { w: 112, h: 44, gap: 6 };
12
13const describe = (it) => `${it.name}  ·  ${bonusText(it.bonus) || 'no bonus'}${it.trait ? `  ·  ${traitText(it.trait)}` : ''}`;
14
15function focusRing(ctx, x, y, w, h) {
16  ctx.strokeStyle = T.accent;
17  ctx.lineWidth = 2;
18  ctx.strokeRect(x + 1, y + 1, w - 2, h - 2);
19}
20
21export function createInventory(layer, m, f) {
22  return layer.add('panel-inv', {
23    z: 3,
24    wheel(dy) {
25      m.packRow = Math.max(0, Math.min(m.packMax || 0, m.packRow + (dy > 0 ? 1 : -1)));
26      this.invalidate();
27    },
28    draw(ctx, w, h, wd) {
29      chrome(ctx, w, h, 'Inventory', wd, f, m);
30      const s = m.state;
31      const u = m.unit();
32      const two = w >= 560;
33      const inner = w - PAD * 2;
34      const tabW = (inner - 18) / 4;
35      s.party.units.forEach((x, i) => {
36        const tx = PAD + i * (tabW + 6);
37        const sel = x.id === u.id;
38        f.button(ctx, tx, 60, tabW, 26, x.name, { hover: wd.hovered(`tab-${i}`), focus: sel, size: 13 });
39        if (sel) ctx.fillStyle = T.accent, ctx.fillRect(tx + 6, 84, tabW - 12, 2);
40        wd.hit(tx, 60, tabW, 26, { name: `tab-${i}`, key: `tab-${i}`, onClick: () => m.select(x.id) });
41      });
42      const leftW = two ? 250 : inner;
43      const topY = 98;
44      drawText(ctx, `${u.name}, level ${u.level} ${u.race ? u.race + ' ' : ''}${u.class}`, PAD, topY, { h: 20, size: 15, weight: 'bold', maxW: leftW, fit: true });
45      drawText(ctx, `${u.stats.hp} / ${maxHp(u)} hp`, PAD, topY + 20, { h: 18, size: 13, color: u.stats.hp * 2 < maxHp(u) ? T.danger : T.muted });
46      const tips = {};
47      const sw = (leftW - 6) / 2;
48      const gearY = topY + 42;
49      SLOTS.forEach((slot, i) => {
50        const x = PAD + (i % 2) * (sw + 6);
51        const y = gearY + Math.floor(i / 2) * 46;
52        const it = u.gear?.[slot];
53        const key = `slot-${i}`;
54        f.panel(ctx, x, y, sw, 40, 'raised', wd.hovered(key) ? T.muted : T.line);
55        drawText(ctx, slot, x + 8, y + 3, { h: 15, size: 12, color: T.faint, maxW: sw - 12 });
56        drawText(ctx, it ? it.name : 'empty', x + 8, y + 18, { h: 18, size: 13, color: it ? rarityColor(it.rarity) : T.faint, maxW: sw - 12, fit: true });
57        tips[key] = it ? `${describe(it)}  ·  click to unequip` : `${slot}: empty`;
58        if (it) wd.hit(x, y, sw, 40, { key, onClick: () => m.unequip(u.id, it.id) });
59        if (m.cursorOn && m.cursor === i) focusRing(ctx, x, y, sw, 40);
60      });
61      const statY = gearY + 96;
62      STATS.forEach(([k, label], i) => {
63        const x = PAD + (i % 3) * (leftW / 3);
64        const y = statY + Math.floor(i / 3) * 36;
65        drawText(ctx, label, x, y, { h: 13, size: 12, color: T.faint });
66        drawText(ctx, String(total(u, k)), x, y + 13, { h: 18, size: 16, weight: 'bold' });
67      });
68      const abY = statY + 76;
69      drawText(ctx, 'Abilities', PAD, abY, { h: 13, size: 12, color: T.faint });
70      drawText(ctx, [...new Set([...u.abilities.map((a) => a.id), ...Object.values(u.gear || {}).flatMap((it) => it?.abilities || [])])].map(abilityName).join(', '), PAD, abY + 13, { h: 18, size: 13, color: T.muted, maxW: leftW, fit: true });
71      const traits = heroTraits(u);
72      drawText(ctx, 'Traits', PAD, abY + 34, { h: 13, size: 12, color: T.faint });
73      drawText(ctx, traits.length ? traits.map(traitName).join(', ') : 'None yet. Gear with a trait adds one.', PAD, abY + 47, { h: 18, size: 13, color: traits.length ? T.fg : T.faint, maxW: leftW, fit: true });
74
75      const px = two ? PAD + leftW + 18 : PAD;
76      const py = two ? topY : abY + 72;
77      const pw = two ? w - PAD - px : inner;
78      const inv = s.party.inventory;
79      drawText(ctx, `Pack  ${inv.length}`, px, py, { h: 20, size: 15, weight: 'bold' });
80      if (inv.length) drawText(ctx, `compared with ${u.name}'s gear`, px + pw, py, { h: 20, size: 12, color: T.faint, align: 'right' });
81      const gy = py + 22;
82      const th = s.threat;
83      const prep = m.preparing && th;
84      const footH = prep ? 66 : 40;
85      const cols = Math.max(1, Math.floor((pw + CELL.gap) / (CELL.w + CELL.gap)));
86      const cw = (pw - (cols - 1) * CELL.gap) / cols;
87      const rows = Math.max(1, Math.floor((h - PAD - footH - gy + CELL.gap) / (CELL.h + CELL.gap)));
88      const totalRows = Math.ceil(inv.length / cols);
89      m.packMax = Math.max(0, totalRows - rows);
90      m.packCols = cols;
91      m.packRows = rows;
92      m.packRow = Math.min(m.packRow, m.packMax);
93      if (!inv.length) drawText(ctx, 'Empty. Loot drops from fights and chests on the board.', px, gy, { h: 20, size: 13, color: T.faint, maxW: pw, fit: true });
94      for (let r = 0; r < rows; r++) {
95        for (let c = 0; c < cols; c++) {
96          const idx = (m.packRow + r) * cols + c;
97          const it = inv[idx];
98          if (!it) continue;
99          const x = px + c * (cw + CELL.gap);
100          const y = gy + r * (CELL.h + CELL.gap);
101          const key = `item-${idx}`;
102          f.panel(ctx, x, y, cw, CELL.h, 'raised', wd.hovered(key) ? T.muted : T.line);
103          drawText(ctx, it.name, x + 7, y + 4, { h: 19, size: 12.5, color: rarityColor(it.rarity), maxW: cw - 12, fit: true });
104          const worn = u.gear?.[it.slot];
105          drawText(ctx, `${compareText(it, worn)}${it.trait ? `, ${traitName(it.trait)}` : ''}`, x + 7, y + 23, { h: 16, size: 11.5, color: T.muted, maxW: cw - 12, fit: true });
106          tips[key] = `${describe(it)}  ·  ${worn ? `vs ${worn.name}: ${compareText(it, worn)}` : `${it.slot} slot is empty`}  ·  click to equip`;
107          wd.hit(x, y, cw, CELL.h, { key, name: key, onClick: () => m.equip(u.id, it.id) });
108          if (m.cursorOn && m.cursor === 4 + idx) focusRing(ctx, x, y, cw, CELL.h);
109        }
110      }
111      f.scrollbar(ctx, px + pw + 4, gy, rows * (CELL.h + CELL.gap) - CELL.gap, m.packRow, rows, totalRows);
112      const key = wd.hoverKey() ?? (m.cursorOn ? (m.cursor < 4 ? `slot-${m.cursor}` : `item-${m.cursor - 4}`) : null);
113      const by = h - PAD - 28;
114      const bw = 104;
115      const pots = s.party.potions;
116      const full = u.stats.hp >= maxHp(u);
117      if (prep) {
118        f.button(ctx, w - PAD - bw, by, bw, 28, 'To the fight', { hover: wd.hovered('prepare-back'), primary: true, key: 'Esc', size: 12.5 });
119        wd.hit(w - PAD - bw, by, bw, 28, { name: 'prepare-back', key: 'prepare-back', onClick: () => m.resume() });
120      }
121      const dx = prep ? w - PAD - bw * 2 - 8 : w - PAD - bw;
122      f.button(ctx, dx, by, bw, 28, `Potion  ${pots}`, { hover: wd.hovered('drink'), disabled: !pots || full, key: 'P', size: 12.5 });
123      wd.hit(dx, by, bw, 28, { name: 'drink', key: 'drink', onClick: () => m.drink(u.id) });
124      const hint = tips[key] || (pots && !full ? `P drinks a potion: ${u.name} heals half their health.` : 'Arrows move, Enter equips, Tab switches hero, Esc closes.');
125      if (prep) {
126        const bad = th.odds < 60;
127        drawText(ctx, `${oddsWord(th)}: ${th.odds}% to win, about ${th.wounds}% of your health lost`, PAD, by - 26, { h: 20, size: 14, weight: 'bold', color: bad ? T.danger : T.fg, maxW: w - PAD * 2, fit: true });
128      }
129      if (dx - PAD > 240) drawText(ctx, hint, PAD, by, { h: 28, size: 13, color: T.muted, maxW: dx - PAD - 10, fit: true });
130    },
131  });
132}