1import { drawText } from './text.js'; 2import { chrome, PAD } from './chrome.js'; 3import { T, rarityColor } from './theme.js'; 4import { bonusText, compareText } from './stats.js'; 5import { traitName } from './traits.js'; 6 7const ROW = 44; 8const sellPrice = (it) => Math.max(1, Math.floor(it.value / 2)); 9 10function list(ctx, f, wd, x, y, w, h, title, items, price, verb, onPick, afford, scroll, unit) { 11 drawText(ctx, title, x, y, { h: 20, size: 15, weight: 'bold' }); 12 const rows = Math.max(1, Math.floor((h - 26) / (ROW + 4))); 13 const start = Math.min(scroll, Math.max(0, items.length - rows)); 14 if (!items.length) drawText(ctx, verb === 'buy' ? 'Sold out.' : 'Nothing to sell.', x, y + 26, { h: 20, size: 13, color: T.faint }); 15 items.slice(start, start + rows).forEach((it, i) => { 16 const ry = y + 26 + i * (ROW + 4); 17 const key = `${verb}-${start + i}`; 18 const ok = afford(it); 19 f.panel(ctx, x, ry, w, ROW, 'raised', wd.hovered(key) && ok ? T.muted : T.line); 20 drawText(ctx, it.name, x + 8, ry + 4, { h: 19, size: 13, color: ok ? rarityColor(it.rarity) : T.faint, maxW: w - 80, fit: true }); 21 drawText(ctx, `${it.slot} ${unit ? compareText(it, unit.gear?.[it.slot]) : bonusText(it.bonus)}${it.trait ? `, ${traitName(it.trait)}` : ''}`, x + 8, ry + 23, { h: 16, size: 11.5, color: T.faint, maxW: w - 80, fit: true }); 22 drawText(ctx, `${price(it)} gold`, x + w - 8, ry, { h: ROW, size: 13, weight: 'bold', align: 'right', color: ok ? T.fg : T.faint }); 23 wd.hit(x, ry, w, ROW, { key, name: key, onClick: () => ok && onPick(it) }); 24 }); 25 f.scrollbar(ctx, x + w + 4, y + 26, rows * (ROW + 4) - 4, start, rows, items.length); 26 return items.length - rows; 27} 28 29export function createShop(layer, m, f) { 30 return layer.add('panel-shop', { 31 z: 3, 32 wheel(dy) { 33 m.shopScroll = Math.max(0, Math.min(m.shopMax || 0, (m.shopScroll || 0) + (dy > 0 ? 1 : -1))); 34 this.invalidate(); 35 }, 36 draw(ctx, w, h, wd) { 37 const s = m.state; 38 const shop = s.shop; 39 chrome(ctx, w, h, `Trader at ${shop.name[0].toUpperCase()}${shop.name.slice(1)}`, wd, f, m); 40 const bw = Math.min(170, (w - PAD * 2 - 10) / 2); 41 const can = s.party.gold >= shop.potionPrice; 42 f.button(ctx, PAD, 62, bw, 34, `Potion, ${shop.potionPrice} gold`, { hover: wd.hovered('buy-potion'), disabled: !can, size: 14 }); 43 wd.hit(PAD, 62, bw, 34, { name: 'buy-potion', key: 'buy-potion', onClick: () => can && m.buy('potion') }); 44 drawText(ctx, `${s.party.potions} carried, ${s.party.gold} gold`, w - PAD, 62, { h: 34, size: 13, align: 'right', color: T.muted }); 45 f.rule(ctx, PAD, 110, w - PAD * 2); 46 const two = w >= 520; 47 const top = 122; 48 const colW = two ? (w - PAD * 2 - 16) / 2 : w - PAD * 2; 49 const bottom = h - PAD; 50 const shopH = two ? bottom - top : Math.min(26 + shop.stock.length * (ROW + 4), 26 + 2 * (ROW + 4)); 51 list(ctx, f, wd, PAD, top, colW, shopH, `For sale, vs ${m.unit().name}`, shop.stock, (it) => it.value, 'buy', (it) => m.buy(it.id), (it) => s.party.gold >= it.value, 0, m.unit()); 52 const sellable = s.party.inventory.filter((it) => !it.key); 53 const sx = two ? PAD + colW + 16 : PAD; 54 const sy = two ? top : top + shopH + 10; 55 m.shopMax = Math.max(0, list(ctx, f, wd, sx, sy, colW, bottom - sy, 'Sell from pack', sellable, sellPrice, 'sell', (it) => m.sell(it.id), () => true, m.shopScroll || 0)); 56 }, 57 }); 58}