| 1 | import { SPRITE_SIZE_PX } from "/shared/protocol/constants.js"; |
| 2 | import { applyMagentaTintToImageData } from "/shared/render/tint.js"; |
| 3 | |
| 4 | export const UTILITY_IMAGES = [ |
| 5 | { name: "night-shade", url: shadeDataUrl("#000000") }, |
| 6 | { name: "chunk-edge", url: shadeDataUrl("#e23d3d") }, |
| 7 | { name: "spawn-range", url: shadeDataUrl("rgba(72, 180, 210, 0.32)") }, |
| 8 | { name: "wander-range", url: shadeDataUrl("rgba(210, 170, 72, 0.22)") }, |
| 9 | { name: "spell-target-tile", url: shadeDataUrl("rgba(110, 220, 255, 0.62)") }, |
| 10 | { name: "spell-target-invalid", url: shadeDataUrl("rgba(255, 72, 72, 0.48)") }, |
| 11 | { name: "spell-target-entity", url: outlineDataUrl("rgba(110, 220, 255, 0.95)") }, |
| 12 | { name: "spell-target-entity-invalid", url: outlineDataUrl("rgba(255, 72, 72, 0.9)") } |
| 13 | ]; |
| 14 | |
| 15 | export function playImagesFromCatalog (catalog) |
| 16 | { |
| 17 | return [...(catalog?.images ?? []), ...UTILITY_IMAGES]; |
| 18 | } |
| 19 | |
| 20 | export async function loadSpriteCatalog () |
| 21 | { |
| 22 | const response = await fetch("/assets/catalog.json"); |
| 23 | if (!response.ok) |
| 24 | { throw new Error("Failed to load sprite catalog."); } |
| 25 | return response.json(); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Load catalog images without aborting the whole client when one file is missing. |
| 30 | * Failed entries are logged and omitted from `game.images`. |
| 31 | */ |
| 32 | export async function preloadCatalogImages (game, catalog, { onProgress } = {}) |
| 33 | { |
| 34 | const images = playImagesFromCatalog(catalog); |
| 35 | const total = images.length; |
| 36 | let loaded = 0; |
| 37 | const failed = []; |
| 38 | const report = (name) => |
| 39 | { |
| 40 | loaded += 1; |
| 41 | onProgress?.({ loaded, total, name }); |
| 42 | }; |
| 43 | await Promise.all(images.map((imageData) => loadOneImage(game, imageData).then((sprite) => |
| 44 | { |
| 45 | report(imageData.name); |
| 46 | return sprite; |
| 47 | }).catch((error) => |
| 48 | { |
| 49 | console.error(error.message ?? error); |
| 50 | failed.push({ name: imageData.name, url: imageData.url }); |
| 51 | report(imageData.name); |
| 52 | }))); |
| 53 | if (failed.length) |
| 54 | { console.warn(`Skipped ${failed.length} missing/broken sprite(s).`, failed.map((entry) => entry.name)); } |
| 55 | return failed; |
| 56 | } |
| 57 | |
| 58 | function loadOneImage (game, imageData) |
| 59 | { |
| 60 | return new Promise((resolve, reject) => |
| 61 | { |
| 62 | const img = new Image(); |
| 63 | img.onload = () => |
| 64 | { |
| 65 | const source = imageData.tint |
| 66 | ? tintedHtmlImage(img, imageData.tint) |
| 67 | : img; |
| 68 | const finalImage = new image( |
| 69 | source, |
| 70 | imageData.subImgTotal, |
| 71 | imageData.perRow, |
| 72 | imageData.loop, |
| 73 | imageData.chain |
| 74 | ); |
| 75 | finalImage.name = imageData.name; |
| 76 | game.images[imageData.name] = finalImage; |
| 77 | resolve(finalImage); |
| 78 | }; |
| 79 | img.onerror = () => |
| 80 | { |
| 81 | reject(new Error(`Failed to load image '${imageData.name}' from ${imageData.url}`)); |
| 82 | }; |
| 83 | img.src = imageData.url; |
| 84 | }); |
| 85 | } |
| 86 | |
| 87 | function tintedHtmlImage (sourceImg, tintRgb) |
| 88 | { |
| 89 | const canvas = document.createElement("canvas"); |
| 90 | canvas.width = sourceImg.naturalWidth || sourceImg.width; |
| 91 | canvas.height = sourceImg.naturalHeight || sourceImg.height; |
| 92 | const ctx = canvas.getContext("2d"); |
| 93 | ctx.imageSmoothingEnabled = false; |
| 94 | ctx.drawImage(sourceImg, 0, 0); |
| 95 | const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height); |
| 96 | applyMagentaTintToImageData(imageData, tintRgb); |
| 97 | ctx.putImageData(imageData, 0, 0); |
| 98 | return canvas; |
| 99 | } |
| 100 | |
| 101 | export function applySpriteSheets (images, catalog) |
| 102 | { |
| 103 | for (const entry of catalog?.images ?? []) |
| 104 | { |
| 105 | const sprite = images[entry.name]; |
| 106 | if (!sprite) |
| 107 | { continue; } |
| 108 | sprite._animationSpeed = entry.animationSpeed ?? 0; |
| 109 | const frames = entry.subImgTotal || 1; |
| 110 | if (frames > 1) |
| 111 | { |
| 112 | sprite.addAnimationCycle( |
| 113 | "idle", |
| 114 | Array.from({ length: frames }, (_, index) => index), |
| 115 | true |
| 116 | ); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | export function shadeDataUrl (fill = "#000000") |
| 122 | { |
| 123 | const canvas = document.createElement("canvas"); |
| 124 | canvas.width = SPRITE_SIZE_PX; |
| 125 | canvas.height = SPRITE_SIZE_PX; |
| 126 | const context = canvas.getContext("2d"); |
| 127 | context.fillStyle = fill; |
| 128 | context.fillRect(0, 0, SPRITE_SIZE_PX, SPRITE_SIZE_PX); |
| 129 | return canvas.toDataURL("image/png"); |
| 130 | } |
| 131 | |
| 132 | /** Hollow square for entity spell targeting (box around a mob / item). */ |
| 133 | export function outlineDataUrl (stroke = "rgba(110, 220, 255, 0.95)", lineWidth = 3) |
| 134 | { |
| 135 | const canvas = document.createElement("canvas"); |
| 136 | canvas.width = SPRITE_SIZE_PX; |
| 137 | canvas.height = SPRITE_SIZE_PX; |
| 138 | const context = canvas.getContext("2d"); |
| 139 | context.strokeStyle = stroke; |
| 140 | context.lineWidth = lineWidth; |
| 141 | const inset = lineWidth / 2; |
| 142 | context.strokeRect(inset, inset, SPRITE_SIZE_PX - lineWidth, SPRITE_SIZE_PX - lineWidth); |
| 143 | return canvas.toDataURL("image/png"); |
| 144 | } |
| 145 | |
| 146 | function setGlNearest (gl, texture) |
| 147 | { |
| 148 | if (!gl || !texture) |
| 149 | { return; } |
| 150 | gl.bindTexture(gl.TEXTURE_2D, texture); |
| 151 | gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); |
| 152 | gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); |
| 153 | } |
| 154 | |
| 155 | /** Framework defaults to LINEAR; patch uploads so pixel art stays crisp when scaled. */ |
| 156 | export function patchNearestNeighbourTextures (scene) |
| 157 | { |
| 158 | const glContext = scene?.glContext; |
| 159 | if (!glContext || glContext._nearestPatched) |
| 160 | { return; } |
| 161 | const original = glContext.getTexture.bind(glContext); |
| 162 | glContext.getTexture = (htmlImage) => |
| 163 | { |
| 164 | const texture = original(htmlImage); |
| 165 | if (texture) |
| 166 | { setGlNearest(glContext.gl, texture); } |
| 167 | return texture; |
| 168 | }; |
| 169 | glContext._nearestPatched = true; |
| 170 | } |
| 171 | |
| 172 | /** Canvas atlases and dynamic sprites — call after invalidate/reupload. */ |
| 173 | export function setNearestFilter (scene, source) |
| 174 | { |
| 175 | if (!source) |
| 176 | { return; } |
| 177 | patchNearestNeighbourTextures(scene); |
| 178 | const glContext = scene?.glContext; |
| 179 | if (!glContext) |
| 180 | { return; } |
| 181 | const htmlImage = source.g_image ?? source; |
| 182 | const texture = glContext.getTexture(htmlImage); |
| 183 | if (texture) |
| 184 | { setGlNearest(glContext.gl, texture); } |
| 185 | } |
| 186 | |
| 187 | export function applyNearestToAllTextures (scene, images) |
| 188 | { |
| 189 | patchNearestNeighbourTextures(scene); |
| 190 | const glContext = scene?.glContext; |
| 191 | if (!glContext) |
| 192 | { return; } |
| 193 | for (const sprite of Object.values(images ?? {})) |
| 194 | { |
| 195 | const htmlImage = sprite?.g_image ?? sprite; |
| 196 | if (htmlImage) |
| 197 | { glContext.getTexture(htmlImage); } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | export function useNearestNeighbour (scene, images) |
| 202 | { |
| 203 | applyNearestToAllTextures(scene, images); |
| 204 | } |