42 lines
938 B
TypeScript
42 lines
938 B
TypeScript
class Assets {
|
|
#images: Record<string, HTMLImageElement>;
|
|
|
|
constructor() {
|
|
this.#images = {};
|
|
}
|
|
|
|
isLoaded(): boolean {
|
|
// you could use this, if so inclined, to check if a certain
|
|
// list of assets had been loaded prior to game start
|
|
//
|
|
// (to do so, you would call getImage() for each desired asset
|
|
// and then wait for isLoaded to return true)
|
|
for (let filename in this.#images) {
|
|
if (!this.#images[filename].complete) {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
getImage(filename: string): HTMLImageElement {
|
|
let element: HTMLImageElement;
|
|
if (this.#images[filename]) {
|
|
element = this.#images[filename];
|
|
} else {
|
|
element = document.createElement("img");
|
|
element.src = filename;
|
|
this.#images[filename] = element;
|
|
}
|
|
return element
|
|
}
|
|
}
|
|
|
|
let active: Assets = new Assets();
|
|
|
|
export function getAssets(): Assets {
|
|
return active;
|
|
}
|
|
|