~starkingdoms/starkingdoms

ref: 321e32fc5c5439af1c396ff486d9be99d70cd36e starkingdoms/starkingdoms-client/src/textures.ts -rw-r--r-- 1.4 KiB
321e32fc — core update hook to provide commands for invalid formatting 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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);
    }
  });
}