Froobtris/src/textureStore.ts
Alexander Bass 9b528ab2e8 init
2023-07-28 14:01:50 -04:00

34 lines
1 KiB
TypeScript

import { SpriteSlicer } from "./lib/spriteSlicer";
import { loadImage } from "./lib/util";
export type Image = HTMLImageElement;
export class TextureStore {
readonly tile: Array<Image>;
readonly numbers: Array<Image>;
readonly playfieldBg: Image;
readonly scoreboardBg: Image;
// Private constructor which is only called by the `new()` method because constructors can not be async.
// Using `await TextureStore.new()` is a somewhat clean workaround.
private constructor(
img: Array<Image>,
numbers: Array<Image>,
tile: Array<Image>
) {
[this.playfieldBg, this.scoreboardBg] = img;
this.tile = tile;
this.numbers = numbers;
}
static async new(): Promise<TextureStore> {
const playfield = await loadImage("./playfield.png");
const colors = SpriteSlicer.slice(await loadImage("./tiles.png"), [16, 16]);
const numbers = SpriteSlicer.slice(
await loadImage("./numbers.png"),
[6, 8]
);
const score = await loadImage("./side.png");
return new TextureStore([playfield, score], numbers, colors);
}
}