2023-08-25 18:48:17 -04:00
|
|
|
import type { SpriteSpec } from './sprites';
|
|
|
|
import { SPRITE_SPECS } from './sprites';
|
2023-07-19 23:38:24 -04:00
|
|
|
|
|
|
|
export const IMAGES = new Map<string, HTMLImageElement>();
|
|
|
|
|
|
|
|
export const loadSpritesIntoImageElements = (
|
2023-08-25 18:48:17 -04:00
|
|
|
spriteSpecs: Partial<SpriteSpec>[]
|
2023-07-19 23:38:24 -04:00
|
|
|
): Promise<void>[] => {
|
|
|
|
const spritePromises: Promise<void>[] = [];
|
|
|
|
|
|
|
|
for (const spriteSpec of spriteSpecs) {
|
|
|
|
if (spriteSpec.sheet) {
|
|
|
|
const img = new Image();
|
|
|
|
img.src = spriteSpec.sheet;
|
|
|
|
IMAGES.set(spriteSpec.sheet, img);
|
|
|
|
|
|
|
|
spritePromises.push(
|
|
|
|
new Promise((resolve) => {
|
|
|
|
img.onload = () => resolve();
|
2023-08-25 18:48:17 -04:00
|
|
|
})
|
2023-07-19 23:38:24 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (spriteSpec.states) {
|
|
|
|
spritePromises.push(
|
2023-08-25 18:48:17 -04:00
|
|
|
...loadSpritesIntoImageElements(Array.from(spriteSpec.states.values()))
|
2023-07-19 23:38:24 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return spritePromises;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const loadAssets = () =>
|
|
|
|
Promise.all([
|
|
|
|
...loadSpritesIntoImageElements(
|
2023-08-12 15:49:16 -04:00
|
|
|
Array.from(SPRITE_SPECS.keys()).map(
|
2023-08-25 18:48:17 -04:00
|
|
|
(key) => SPRITE_SPECS.get(key) as SpriteSpec
|
|
|
|
)
|
|
|
|
)
|
2023-07-19 23:38:24 -04:00
|
|
|
// TODO: Sound
|
|
|
|
]);
|