import { resolveGameAssets } from '../data/assetManifest.js'; // Lazy per-game asset loading. PreloadScene only loads shared assets at // startup; each game's own art (see data/assetManifest.js) is loaded here, // from GameRoomScene, the first time the user enters that game. function isLoaded(scene, d) { if (d.type === 'audio') return scene.cache.audio.exists(d.key); if (d.type === 'json') return scene.cache.json.exists(d.key); return scene.textures.exists(d.key); } export function missingGameAssets(scene, slug) { return resolveGameAssets(scene, slug).filter((d) => !isLoaded(scene, d)); } // Queue and load every missing asset for `slug` on the scene's loader. Call // from create() only (the loader is idle there). Resolves once the load pass // completes; a failed file logs a warning and the game's usual // textures.exists fallback applies. export function ensureGameAssets(scene, slug, { onProgress } = {}) { const missing = missingGameAssets(scene, slug); if (missing.length === 0) return Promise.resolve(false); for (const d of missing) { if (d.type === 'spritesheet') { scene.load.spritesheet(d.key, d.path, { frameWidth: d.frameWidth, frameHeight: d.frameHeight }); } else if (d.type === 'audio') { scene.load.audio(d.key, d.path); } else if (d.type === 'json') { scene.load.json(d.key, d.path); } else { scene.load.image(d.key, d.path); } } return new Promise((resolve) => { const onError = (file) => console.warn(`[assets] failed to load ${file.key} (${file.src})`); if (onProgress) scene.load.on('progress', onProgress); scene.load.on('loaderror', onError); scene.load.once('complete', () => { if (onProgress) scene.load.off('progress', onProgress); scene.load.off('loaderror', onError); resolve(true); }); scene.load.start(); }); }