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