48 lines
1.1 KiB
TypeScript
Raw Normal View History

2024-03-01 22:04:57 -07:00
import { type SpriteSpec, SPRITE_SPECS } from ".";
2024-03-01 18:56:58 -07:00
2024-03-02 04:02:20 -07:00
export const FONT = new FontFace("scientifica", "url(/fonts/scientifica.ttf)");
FONT.load().then((font) => {
document.fonts.add(font);
});
2024-03-01 18:56:58 -07:00
export const IMAGES = new Map<string, HTMLImageElement>();
export const loadSpritesIntoImageElements = (
spriteSpecs: Partial<SpriteSpec>[],
): 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();
}),
);
}
if (spriteSpec.states) {
spritePromises.push(
...loadSpritesIntoImageElements(Array.from(spriteSpec.states.values())),
);
}
}
return spritePromises;
};
export const loadAssets = () =>
Promise.all([
...loadSpritesIntoImageElements(
Array.from(SPRITE_SPECS.keys()).map(
(key) => SPRITE_SPECS.get(key) as SpriteSpec,
),
),
2024-03-02 04:02:20 -07:00
FONT.load(),
2024-03-01 18:56:58 -07:00
// TODO: Sound
]);