summaryrefslogtreecommitdiff
path: root/src/engine/config/assets.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/engine/config/assets.ts')
-rw-r--r--src/engine/config/assets.ts42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/engine/config/assets.ts b/src/engine/config/assets.ts
new file mode 100644
index 0000000..173bab3
--- /dev/null
+++ b/src/engine/config/assets.ts
@@ -0,0 +1,42 @@
+import type { SpriteSpec } from "./sprites";
+import { SPRITE_SPECS } from "./sprites";
+
+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,
+ ),
+ ),
+ // TODO: Sound
+ ]);