import { SPRITE_SIZE_PX } from "/shared/protocol/constants.js"; export const UTILITY_IMAGES = [ { name: "player-body-male", url: "/assets/characters/player-body-male.png" }, { name: "player-body-female", url: "/assets/characters/player-body-female.png" }, { name: "night-shade", url: shadeDataUrl("#000000") }, { name: "chunk-edge", url: shadeDataUrl("#e23d3d") } ]; export function playImagesFromCatalog (catalog) { return [...(catalog?.images ?? []), ...UTILITY_IMAGES]; } export async function loadSpriteCatalog () { const response = await fetch("/assets/catalog.json"); if (!response.ok) { throw new Error("Failed to load sprite catalog."); } return response.json(); } export function applySpriteSheets (images, catalog) { for (const entry of catalog?.images ?? []) { const sprite = images[entry.name]; if (!sprite) { continue; } sprite._animationSpeed = entry.animationSpeed ?? 0; const frames = entry.subImgTotal || 1; if (frames > 1) { sprite.addAnimationCycle( "idle", Array.from({ length: frames }, (_, index) => index), true ); } } } export function shadeDataUrl (fill = "#000000") { const canvas = document.createElement("canvas"); canvas.width = SPRITE_SIZE_PX; canvas.height = SPRITE_SIZE_PX; const context = canvas.getContext("2d"); context.fillStyle = fill; context.fillRect(0, 0, SPRITE_SIZE_PX, SPRITE_SIZE_PX); return canvas.toDataURL("image/png"); } export function useNearestNeighbour (scene, images) { const gl = scene.glContext.gl; for (const sprite of Object.values(images)) { const texture = scene.glContext.getTexture(sprite.g_image); gl.bindTexture(gl.TEXTURE_2D, texture); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); } }