Skip to content

Commit 16f0d48

Browse files
committed
Normalize character asset keys and preload playable assets
1 parent e9f3518 commit 16f0d48

3 files changed

Lines changed: 79 additions & 5 deletions

File tree

src/assets/assetLoading.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,10 @@ export function loadJsonOnce(scene, key, url) {
5959
if (!scene.cache.json.exists(key)) scene.load.json(key, url);
6060
}
6161

62-
const IMAGE_EXTENSIONS = new Set(["jpg", "jpeg", "png", "webp", "svg"]);
63-
const ATLAS_EXTENSIONS = new Set(["png", "webp"]);
62+
const IMAGE_EXTENSIONS = new Set(["jpg", "jpeg", "png", "webp", "svg", "gif"]);
63+
const ATLAS_EXTENSIONS = new Set(["png", "webp", "jpg", "jpeg", "svg", "gif"]);
64+
65+
import { characters } from "../characters/CharacterRegistry.js";
6466

6567
/**
6668
* @param {string} name
@@ -82,6 +84,7 @@ function resolveAssetName(name, fallbackExtension, allowedExtensions) {
8284
if (ext && allowedExtensions.has(ext)) {
8385
return { file: name, stem: name.slice(0, -ext.length - 1) };
8486
}
87+
// If no extension, or unrecognised extension, assume the requested fallback
8588
return { file: `${name}.${fallbackExtension}`, stem: name };
8689
}
8790

@@ -244,6 +247,21 @@ export function collectChapterAssetKeys(manager, chapter) {
244247
keys.add(bg);
245248
for (const a of cfg.assets ?? []) keys.add(a);
246249
}
250+
251+
// Auto-include playable characters, as they can appear in any scene
252+
for (const id of characters.playableIds()) {
253+
const conf = characters.get(id);
254+
if (conf?.spriteKey) keys.add(conf.spriteKey);
255+
if (conf?.animationSet) {
256+
for (const dir of Object.values(conf.animationSet)) {
257+
if (dir.still) keys.add(dir.still);
258+
if (dir.idle) keys.add(dir.idle);
259+
if (dir.walk) keys.add(dir.walk);
260+
if (dir.reach) keys.add(dir.reach);
261+
}
262+
}
263+
}
264+
247265
return keys;
248266
}
249267

src/characters/CharacterRegistry.js

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,45 @@
2323
* `animationSet` + scales/origins — a full sprite-set swap) that overrides the
2424
* base look. See {@link CharacterRegistry#render} and ADR 0006.
2525
*/
26+
const PREFIXES = ["character_", "bg_", "sprite_", "object_"];
27+
28+
/**
29+
* @param {string | undefined} key
30+
* @returns {string | undefined}
31+
*/
32+
function prefixKey(key) {
33+
if (!key || typeof key !== "string") return key;
34+
if (PREFIXES.some((p) => key.startsWith(p))) return key;
35+
return `character_${key}`;
36+
}
37+
38+
/**
39+
* @param {CharacterConfig} config
40+
* @returns {CharacterConfig}
41+
*/
42+
function normalizeConfig(config) {
43+
const copy = { ...config };
44+
if (copy.spriteKey) copy.spriteKey = prefixKey(copy.spriteKey);
45+
if (copy.animationSet) {
46+
copy.animationSet = { ...copy.animationSet };
47+
for (const [dir, set] of Object.entries(copy.animationSet)) {
48+
/** @type {any} */ (copy.animationSet)[dir] = { ...set };
49+
for (const [anim, key] of Object.entries(set)) {
50+
if (typeof key === "string") {
51+
/** @type {any} */ (copy.animationSet)[dir][anim] = prefixKey(key);
52+
}
53+
}
54+
}
55+
}
56+
if (copy.outfits) {
57+
copy.outfits = { ...copy.outfits };
58+
for (const [name, outfit] of Object.entries(copy.outfits)) {
59+
copy.outfits[name] = normalizeConfig(outfit);
60+
}
61+
}
62+
return copy;
63+
}
64+
2665
export class CharacterRegistry {
2766
constructor() {
2867
/** @type {Map<string, CharacterConfig>} */
@@ -37,8 +76,9 @@ export class CharacterRegistry {
3776
* @param {string} id @param {CharacterConfig} config
3877
*/
3978
register(id, config) {
40-
this._chars.set(id, config);
41-
if (config.playable && this._defaultPlayer === null) this._defaultPlayer = id;
79+
const normalized = normalizeConfig(config);
80+
this._chars.set(id, normalized);
81+
if (normalized.playable && this._defaultPlayer === null) this._defaultPlayer = id;
4282
return this;
4383
}
4484

src/scene/AdventureScene.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,23 @@ export class AdventureScene extends Phaser.Scene {
128128

129129
collectAssetKeys() {
130130
const cfg = this.sceneConfig;
131-
return [...Object.values(cfg.backgroundsByChapter ?? {}), ...(cfg.assets ?? [])];
131+
const keys = [...Object.values(cfg.backgroundsByChapter ?? {}), ...(cfg.assets ?? [])];
132+
133+
// Auto-include playable characters
134+
for (const id of characters.playableIds()) {
135+
const conf = characters.get(id);
136+
if (conf?.spriteKey) keys.push(conf.spriteKey);
137+
if (conf?.animationSet) {
138+
for (const dir of Object.values(conf.animationSet)) {
139+
if (dir.still) keys.push(dir.still);
140+
if (dir.idle) keys.push(dir.idle);
141+
if (dir.walk) keys.push(dir.walk);
142+
if (dir.reach) keys.push(dir.reach);
143+
}
144+
}
145+
}
146+
147+
return keys;
132148
}
133149

134150
preload() {

0 commit comments

Comments
 (0)