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