import { PartType, PlanetType } from "./protocol.ts";
import tex_earth from "./assets/earth.svg";
import tex_hearty from "./assets/hearty.svg";
import tex_missing from "./assets/missing.svg";
let planet_textures: Map<PlanetType, HTMLImageElement> = new Map();
function planet_texture_url(type: PlanetType): string {
if (type == PlanetType.Earth) {
return tex_earth;
}
return tex_missing;
}
export async function planet_texture(
type: PlanetType,
): Promise<HTMLImageElement> {
return new Promise((resolve, reject) => {
if (planet_textures.has(type)) {
resolve(planet_textures.get(type)!);
} else {
let img = new Image();
img.onload = () => {
planet_textures.set(type, img);
resolve(img);
};
img.onerror = reject;
img.src = planet_texture_url(type);
}
});
}
let part_textures: Map<PartType, HTMLImageElement> = new Map();
function part_texture_url(type: PartType): string {
if (type == PartType.Hearty) {
return tex_hearty;
}
return tex_missing;
}
export async function part_texture(type: PartType): Promise<HTMLImageElement> {
return new Promise((resolve, reject) => {
if (part_textures.has(type)) {
resolve(part_textures.get(type)!);
} else {
let img = new Image();
img.onload = () => {
part_textures.set(type, img);
resolve(img);
};
img.onerror = reject;
img.src = part_texture_url(type);
}
});
}