| 1 | import { ItemId } from "/shared/definitions/items.js"; |
| 2 | import { SPRITE_SIZE_PX } from "/shared/protocol/constants.js"; |
| 3 | |
| 4 | const bottleCache = new Map(); |
| 5 | const fillCache = new Map(); |
| 6 | const urlCache = new Map(); |
| 7 | |
| 8 | function loadImage (src) |
| 9 | { |
| 10 | return new Promise((resolve, reject) => |
| 11 | { |
| 12 | const img = new Image(); |
| 13 | img.onload = () => resolve(img); |
| 14 | img.onerror = () => reject(new Error(`Failed to load ${src}`)); |
| 15 | img.src = src; |
| 16 | }); |
| 17 | } |
| 18 | |
| 19 | async function getBottle () |
| 20 | { |
| 21 | if (!bottleCache.has("bottle")) |
| 22 | { bottleCache.set("bottle", loadImage("/assets/items/glass-bottle/glass-bottle.png")); } |
| 23 | return bottleCache.get("bottle"); |
| 24 | } |
| 25 | |
| 26 | async function getFill (level) |
| 27 | { |
| 28 | const key = Math.max(1, Math.min(5, level | 0 || 4)); |
| 29 | if (!fillCache.has(key)) |
| 30 | { fillCache.set(key, loadImage(`/assets/items/potion/fill-${key}.png`)); } |
| 31 | return fillCache.get(key); |
| 32 | } |
| 33 | |
| 34 | function colorKey (color) |
| 35 | { |
| 36 | if (!color) |
| 37 | { return "0,0,0"; } |
| 38 | const r = Math.round((color.r ?? 0) * 255); |
| 39 | const g = Math.round((color.g ?? 0) * 255); |
| 40 | const b = Math.round((color.b ?? 0) * 255); |
| 41 | return `${r},${g},${b}`; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Build a data-URL icon: clear bottle + fill mask tinted to potion color. |
| 46 | * UI-only; world ground items use the same bake then lit darkness shader. |
| 47 | */ |
| 48 | export async function potionIconDataUrl (instance) |
| 49 | { |
| 50 | const fillLevel = Math.max(1, Math.min(5, instance?.fillLevel | 0 || 4)); |
| 51 | const key = `${fillLevel}|${colorKey(instance?.color)}`; |
| 52 | if (urlCache.has(key)) |
| 53 | { return urlCache.get(key); } |
| 54 | |
| 55 | const [bottle, fill] = await Promise.all([getBottle(), getFill(fillLevel)]); |
| 56 | const canvas = document.createElement("canvas"); |
| 57 | canvas.width = SPRITE_SIZE_PX; |
| 58 | canvas.height = SPRITE_SIZE_PX; |
| 59 | const ctx = canvas.getContext("2d"); |
| 60 | ctx.clearRect(0, 0, canvas.width, canvas.height); |
| 61 | |
| 62 | ctx.drawImage(fill, 0, 0); |
| 63 | const image = ctx.getImageData(0, 0, canvas.width, canvas.height); |
| 64 | const data = image.data; |
| 65 | const r = Math.round((instance?.color?.r ?? 0.4) * 255); |
| 66 | const g = Math.round((instance?.color?.g ?? 0.4) * 255); |
| 67 | const b = Math.round((instance?.color?.b ?? 0.7) * 255); |
| 68 | for (let i = 0; i < data.length; i += 4) |
| 69 | { |
| 70 | if (data[i + 3] < 8) |
| 71 | { continue; } |
| 72 | const a = data[i + 3] / 255; |
| 73 | data[i] = r; |
| 74 | data[i + 1] = g; |
| 75 | data[i + 2] = b; |
| 76 | data[i + 3] = Math.round(220 * a); |
| 77 | } |
| 78 | ctx.putImageData(image, 0, 0); |
| 79 | ctx.drawImage(bottle, 0, 0); |
| 80 | |
| 81 | const url = canvas.toDataURL("image/png"); |
| 82 | urlCache.set(key, url); |
| 83 | return url; |
| 84 | } |
| 85 | |
| 86 | export function isPotionStack (stack) |
| 87 | { |
| 88 | return stack?.itemId === ItemId.potion && stack.instance?.kind === "potion"; |
| 89 | } |
| 90 | |
| 91 | /** Sync helper: set img.src to recolored potion or fall back to asset path. */ |
| 92 | export function applyItemIcon (img, stack, fallbackPath) |
| 93 | { |
| 94 | if (!img) |
| 95 | { return; } |
| 96 | if (!isPotionStack(stack)) |
| 97 | { |
| 98 | img.src = fallbackPath; |
| 99 | return; |
| 100 | } |
| 101 | img.src = fallbackPath; |
| 102 | potionIconDataUrl(stack.instance).then((url) => |
| 103 | { |
| 104 | img.src = url; |
| 105 | }).catch(() => |
| 106 | { |
| 107 | img.src = fallbackPath; |
| 108 | }); |
| 109 | } |