From 2f8b8c87fb2062313afea7b625ccb20d1bea95b9 Mon Sep 17 00:00:00 2001 From: Maksymilian Jopek Date: Tue, 24 May 2022 18:25:43 +0200 Subject: Initial commit --- src/Images.ts | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/Images.ts (limited to 'src/Images.ts') diff --git a/src/Images.ts b/src/Images.ts new file mode 100644 index 0000000..f0e1ba0 --- /dev/null +++ b/src/Images.ts @@ -0,0 +1,61 @@ +// const URLS = [ +// "/src/imgs/bg2.png", +// "/src/imgs/topbar.png", +// ]; +import { alphabet } from "./consts"; +import { toKebabCase } from "./helpers"; + +export const IMGS = { + bg: {} as HTMLImageElement, + topbar: {} as HTMLImageElement, + playerUp: {} as HTMLImageElement, + playerLeft: {} as HTMLImageElement, + playerRight: {} as HTMLImageElement, + bigCircleDown: {} as HTMLImageElement, + bigCircleDownToRight1: {} as HTMLImageElement, + bigCircleDownToRight2: {} as HTMLImageElement, + bigCircleDownToRight3: {} as HTMLImageElement, + bigCircleRight: {} as HTMLImageElement, + bigCircleRightToUp1: {} as HTMLImageElement, + bigCircleRightToUp2: {} as HTMLImageElement, + bigCircleRightToUp3: {} as HTMLImageElement, + bigCircleUp: {} as HTMLImageElement, + bigCircleUpToLeft1: {} as HTMLImageElement, + bigCircleUpToLeft2: {} as HTMLImageElement, + bigCircleUpToLeft3: {} as HTMLImageElement, + bigCircleLeft: {} as HTMLImageElement, + bigCircleLeftToDown1: {} as HTMLImageElement, + bigCircleLeftToDown2: {} as HTMLImageElement, + bigCircleLeftToDown3: {} as HTMLImageElement, + font: { + white: {} as any, + yellow: {} as any, + ' ': {} as HTMLImageElement, + }, + selector: {} as HTMLImageElement, + empty: {} as HTMLImageElement, +}; +// window.IMGS = IMGS; +export default async function loadAllImages() { + for (const key in IMGS) { + if (["empty", "font"].includes(key)) continue; + IMGS[key as keyof typeof IMGS] = await asyncImageLoader("/src/imgs/" + toKebabCase(key) + ".png") as any + } + for (const color of ['yellow', 'white']) { + for (const char of [...alphabet]) { + (((IMGS.font as any)[color] as any)[char]) = await asyncImageLoader("/src/imgs/font/" + color + "/" + encodeURIComponent(char) + ".png") as any + } + } + IMGS.font[' '] = await asyncImageLoader("/src/imgs/font/ .png") + IMGS.font.yellow[' '] = await asyncImageLoader("/src/imgs/font/ .png") + IMGS.font.white[' '] = await asyncImageLoader("/src/imgs/font/ .png") +} +async function asyncImageLoader(url: string): Promise { + return new Promise((resolve, reject) => { + const image = new Image() + image.src = url + image.onload = () => resolve(image) + image.onerror = () => { console.log("img did not load", url); reject() } + }) +} + -- cgit v1.3.1