| 1 | import { readdirSync } from "node:fs"; |
| 2 | import { extname, join } from "node:path"; |
| 3 | import { BLOCK_DEFINITIONS, GROUND_DEFINITIONS } from "../../../shared/definitions/tiles.js"; |
| 4 | import { ITEM_DEFINITIONS } from "../../../shared/definitions/items.js"; |
| 5 | import { MOB_DEFINITIONS } from "../../../shared/definitions/mobs.js"; |
| 6 | import { |
| 7 | ORE_BEARING_MATERIALS, |
| 8 | oreBoulderState, |
| 9 | materialTintColor |
| 10 | } from "../../../shared/definitions/materialTiers.js"; |
| 11 | import { |
| 12 | bloomImageName, |
| 13 | equipBloomImageName, |
| 14 | equipImageName, |
| 15 | isCompanionSpriteStem, |
| 16 | parseSpriteStem, |
| 17 | spriteImageName |
| 18 | } from "../../../shared/definitions/tileSprites.js"; |
| 19 | |
| 20 | const CHARACTER_DEFINITIONS = Object.freeze({ |
| 21 | "player-body-male": Object.freeze({ key: "player-body-male", id: "player-body-male" }), |
| 22 | "player-body-female": Object.freeze({ key: "player-body-female", id: "player-body-female" }) |
| 23 | }); |
| 24 | |
| 25 | export function buildSpriteCatalog (assetRoot) |
| 26 | { |
| 27 | const grounds = scanLayer(join(assetRoot, "grounds"), GROUND_DEFINITIONS, "ground", "grounds"); |
| 28 | const blocks = scanLayer(join(assetRoot, "blocks"), BLOCK_DEFINITIONS, "block", "blocks"); |
| 29 | const items = scanLayer(join(assetRoot, "items"), itemScanDefs(), "item", "items"); |
| 30 | const mobs = scanMobLayers(assetRoot); |
| 31 | const characters = scanLayer(join(assetRoot, "characters"), CHARACTER_DEFINITIONS, "character", "characters"); |
| 32 | const fx = scanFx(join(assetRoot, "fx")); |
| 33 | return { |
| 34 | grounds: grounds.types, |
| 35 | blocks: blocks.types, |
| 36 | items: items.types, |
| 37 | mobs: mobs.types, |
| 38 | characters: characters.types, |
| 39 | fx: fx.types, |
| 40 | images: [ |
| 41 | ...grounds.images, |
| 42 | ...blocks.images, |
| 43 | ...items.images, |
| 44 | ...mobs.images, |
| 45 | ...characters.images, |
| 46 | ...fx.images |
| 47 | ] |
| 48 | }; |
| 49 | } |
| 50 | |
| 51 | function itemScanDefs () |
| 52 | { |
| 53 | const defs = {}; |
| 54 | for (const def of Object.values(ITEM_DEFINITIONS)) |
| 55 | { |
| 56 | const folderKey = def.iconKey && def.iconKey !== def.key ? def.iconKey : def.key; |
| 57 | if (!folderKey || defs[folderKey]) |
| 58 | { continue; } |
| 59 | defs[folderKey] = { |
| 60 | key: folderKey, |
| 61 | id: def.itemId, |
| 62 | frames: def.frames ?? 1, |
| 63 | framesPerRow: def.framesPerRow ?? 1, |
| 64 | animationSpeed: def.animationSpeed ?? 0 |
| 65 | }; |
| 66 | } |
| 67 | return defs; |
| 68 | } |
| 69 | |
| 70 | function mobScanDefs () |
| 71 | { |
| 72 | const defs = {}; |
| 73 | for (const def of Object.values(MOB_DEFINITIONS)) |
| 74 | { |
| 75 | if (def.usePlayerBody) |
| 76 | { continue; } |
| 77 | const key = def.key ?? def.mobTypeId; |
| 78 | defs[key] = { |
| 79 | key, |
| 80 | id: def.mobTypeId, |
| 81 | frames: def.frames ?? 1, |
| 82 | framesPerRow: def.framesPerRow ?? 1, |
| 83 | animationSpeed: def.animationSpeed ?? 0, |
| 84 | assetFolder: def.assetFolder ?? "mobs" |
| 85 | }; |
| 86 | } |
| 87 | return defs; |
| 88 | } |
| 89 | |
| 90 | function scanMobLayers (assetRoot) |
| 91 | { |
| 92 | const byFolder = new Map(); |
| 93 | for (const def of Object.values(mobScanDefs())) |
| 94 | { |
| 95 | const folder = def.assetFolder ?? "mobs"; |
| 96 | if (!byFolder.has(folder)) |
| 97 | { byFolder.set(folder, {}); } |
| 98 | byFolder.get(folder)[def.key] = def; |
| 99 | } |
| 100 | const types = {}; |
| 101 | const images = []; |
| 102 | for (const [folder, defs] of byFolder) |
| 103 | { |
| 104 | const scanned = scanLayer(join(assetRoot, folder), defs, "mob", folder); |
| 105 | Object.assign(types, scanned.types); |
| 106 | images.push(...scanned.images); |
| 107 | } |
| 108 | return { types, images }; |
| 109 | } |
| 110 | |
| 111 | function scanFx (fxRoot) |
| 112 | { |
| 113 | const types = {}; |
| 114 | const images = []; |
| 115 | let folders; |
| 116 | try |
| 117 | { folders = readdirSync(fxRoot, { withFileTypes: true }).filter((entry) => entry.isDirectory()); } |
| 118 | catch (error) |
| 119 | { |
| 120 | if (error.code === "ENOENT") |
| 121 | { return { types, images }; } |
| 122 | throw error; |
| 123 | } |
| 124 | for (const entry of folders) |
| 125 | { |
| 126 | const key = entry.name; |
| 127 | const defs = { |
| 128 | [key]: { key, id: key, frames: 1, framesPerRow: 1, animationSpeed: 0 } |
| 129 | }; |
| 130 | const scanned = scanLayer(fxRoot, defs, "fx", "fx"); |
| 131 | Object.assign(types, scanned.types); |
| 132 | images.push(...scanned.images); |
| 133 | } |
| 134 | return { types, images }; |
| 135 | } |
| 136 | |
| 137 | function scanLayer (layerDir, definitions, layer, urlFolder) |
| 138 | { |
| 139 | const types = {}; |
| 140 | const images = []; |
| 141 | for (const def of Object.values(definitions)) |
| 142 | { |
| 143 | const folder = join(layerDir, def.key); |
| 144 | const { states: fileStates, blooms, equips, equipBlooms } = listVisualStates(folder); |
| 145 | if (fileStates.length === 0) |
| 146 | { throw new Error(`No PNG states in ${folder}`); } |
| 147 | |
| 148 | const frames = Math.max(1, def.frames ?? 1); |
| 149 | const framesPerRow = Math.max(1, def.framesPerRow ?? 1); |
| 150 | const animationSpeed = Math.max(0, def.animationSpeed ?? 0); |
| 151 | let states = [...fileStates]; |
| 152 | const imagesForType = []; |
| 153 | |
| 154 | if (layer === "block" && def.key === "boulder") |
| 155 | { |
| 156 | states = states.filter((state) => state !== "boulder-ore"); |
| 157 | if (!states.includes("boulder")) |
| 158 | { states.unshift("boulder"); } |
| 159 | for (const material of ORE_BEARING_MATERIALS) |
| 160 | { |
| 161 | const state = oreBoulderState(material); |
| 162 | if (!states.includes(state)) |
| 163 | { states.push(state); } |
| 164 | const tint = materialTintColor(material, { ore: true }); |
| 165 | imagesForType.push({ |
| 166 | name: spriteImageName(layer, def.key, state), |
| 167 | url: `/assets/${urlFolder}/${def.key}/boulder-ore.png`, |
| 168 | subImgTotal: frames, |
| 169 | perRow: framesPerRow, |
| 170 | loop: animationSpeed > 0, |
| 171 | animationSpeed, |
| 172 | tint: { r: tint.r, g: tint.g, b: tint.b } |
| 173 | }); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | types[def.key] = { |
| 178 | key: def.key, |
| 179 | typeId: def.id, |
| 180 | states, |
| 181 | defaultState: def.key === "boulder" && states.includes("boulder") |
| 182 | ? "boulder" |
| 183 | : states[0], |
| 184 | blooms, |
| 185 | equips, |
| 186 | equipBlooms, |
| 187 | frames, |
| 188 | framesPerRow, |
| 189 | animationSpeed |
| 190 | }; |
| 191 | for (const state of states) |
| 192 | { |
| 193 | if (imagesForType.some((entry) => entry.name === spriteImageName(layer, def.key, state))) |
| 194 | { continue; } |
| 195 | const sheet = { |
| 196 | subImgTotal: frames, |
| 197 | perRow: framesPerRow, |
| 198 | loop: animationSpeed > 0, |
| 199 | animationSpeed |
| 200 | }; |
| 201 | images.push({ |
| 202 | name: spriteImageName(layer, def.key, state), |
| 203 | url: `/assets/${urlFolder}/${def.key}/${state}.png`, |
| 204 | ...sheet |
| 205 | }); |
| 206 | if (blooms[state]) |
| 207 | { |
| 208 | images.push({ |
| 209 | name: bloomImageName(layer, def.key, state), |
| 210 | url: `/assets/${urlFolder}/${def.key}/${state}-bloom.png`, |
| 211 | ...sheet |
| 212 | }); |
| 213 | } |
| 214 | if (equips[state]) |
| 215 | { |
| 216 | images.push({ |
| 217 | name: equipImageName(layer, def.key, state), |
| 218 | url: `/assets/${urlFolder}/${def.key}/${state}-equip.png`, |
| 219 | ...sheet |
| 220 | }); |
| 221 | } |
| 222 | if (equipBlooms[state]) |
| 223 | { |
| 224 | images.push({ |
| 225 | name: equipBloomImageName(layer, def.key, state), |
| 226 | url: `/assets/${urlFolder}/${def.key}/${state}-equip-bloom.png`, |
| 227 | ...sheet |
| 228 | }); |
| 229 | } |
| 230 | } |
| 231 | images.push(...imagesForType); |
| 232 | } |
| 233 | return { types, images }; |
| 234 | } |
| 235 | |
| 236 | function listVisualStates (folder) |
| 237 | { |
| 238 | const stems = listPngStems(folder); |
| 239 | const blooms = {}; |
| 240 | const equips = {}; |
| 241 | const equipBlooms = {}; |
| 242 | const states = []; |
| 243 | for (const stem of stems) |
| 244 | { |
| 245 | const parsed = parseSpriteStem(stem); |
| 246 | if (parsed.companion) |
| 247 | { |
| 248 | if (!parsed.base) |
| 249 | { continue; } |
| 250 | if (parsed.equip && parsed.bloom) |
| 251 | { equipBlooms[parsed.base] = true; } |
| 252 | else if (parsed.equip) |
| 253 | { equips[parsed.base] = true; } |
| 254 | else if (parsed.bloom) |
| 255 | { blooms[parsed.base] = true; } |
| 256 | continue; |
| 257 | } |
| 258 | if (isCompanionSpriteStem(stem)) |
| 259 | { continue; } |
| 260 | states.push(stem); |
| 261 | } |
| 262 | const filterMap = (map) => |
| 263 | { |
| 264 | const next = {}; |
| 265 | for (const state of states) |
| 266 | { |
| 267 | if (map[state]) |
| 268 | { next[state] = true; } |
| 269 | } |
| 270 | return next; |
| 271 | }; |
| 272 | return { |
| 273 | states, |
| 274 | blooms: filterMap(blooms), |
| 275 | equips: filterMap(equips), |
| 276 | equipBlooms: filterMap(equipBlooms) |
| 277 | }; |
| 278 | } |
| 279 | |
| 280 | function listPngStems (folder) |
| 281 | { |
| 282 | let entries; |
| 283 | try |
| 284 | { entries = readdirSync(folder, { withFileTypes: true }); } |
| 285 | catch (error) |
| 286 | { |
| 287 | if (error.code === "ENOENT") |
| 288 | { return []; } |
| 289 | throw error; |
| 290 | } |
| 291 | |
| 292 | return entries |
| 293 | .filter((entry) => entry.isFile() && extname(entry.name).toLowerCase() === ".png") |
| 294 | .map((entry) => entry.name.slice(0, -extname(entry.name).length)) |
| 295 | .sort((a, b) => a.localeCompare(b, "en", { numeric: true, sensitivity: "base" })); |
| 296 | } |