All files / renderer/village assetMap.ts

91.89% Statements 68/74
88.88% Branches 16/18
100% Functions 5/5
91.89% Lines 68/74

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118                              27x       27x 27x 3x 27x 3x 27x 3x 27x 3x 27x 3x 27x 3x 27x 3x 27x 3x 27x 3x 27x           27x 27x   4x 4x 4x 2x 4x 2x 4x       4x 4x         1x 18x 18x         1x 2x 2x             1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x               1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x  
import type { ZoneId } from "../../shared/zones";
 
/**
 * Tier 2 asset map. Resolves the bundled GLB URL for each zone building and
 * character kind. Uses `new URL(..., import.meta.url)` so Vite picks the
 * files up at build time and emits them into the renderer asset pipeline;
 * at runtime this yields a file:// or blob: URL that `useGLTF` can load
 * without touching the network.
 *
 * Swapping placeholder GLBs for real Kenney GLBs is a matter of replacing
 * the files under ../assets/models/ - no code change required.
 */
 
export type CharacterKind = "mayor" | "villager";
 
function zoneUrl(id: ZoneId): string {
  // We intentionally list each zone explicitly rather than build the path
  // with string concatenation. Vite's asset graph only picks up `new URL(...)`
  // calls whose argument is a literal string, so we cannot template this.
  switch (id) {
    case "office":
      return new URL("../assets/models/zone-office.glb", import.meta.url).href;
    case "library":
      return new URL("../assets/models/zone-library.glb", import.meta.url).href;
    case "mine":
      return new URL("../assets/models/zone-mine.glb", import.meta.url).href;
    case "forest":
      return new URL("../assets/models/zone-forest.glb", import.meta.url).href;
    case "farm":
      return new URL("../assets/models/zone-farm.glb", import.meta.url).href;
    case "nether":
      return new URL("../assets/models/zone-nether.glb", import.meta.url).href;
    case "signpost":
      return new URL("../assets/models/zone-signpost.glb", import.meta.url).href;
    case "spawner":
      return new URL("../assets/models/zone-spawner.glb", import.meta.url).href;
    case "tavern":
      return new URL("../assets/models/zone-tavern.glb", import.meta.url).href;
    default: {
      // Exhaustiveness check - if ZoneId ever gains a new variant, TS errors
      // here and forces us to add the matching asset.
      const _exhaustive: never = id;
      throw new Error(`no zone model for id: ${String(_exhaustive)}`);
    }
  }
}
 
function characterUrl(kind: CharacterKind): string {
  switch (kind) {
    case "mayor":
      return new URL("../assets/models/character-mayor.glb", import.meta.url).href;
    case "villager":
      return new URL("../assets/models/character-villager.glb", import.meta.url).href;
    default: {
      const _exhaustive: never = kind;
      throw new Error(`no character model for kind: ${String(_exhaustive)}`);
    }
  }
}
 
/**
 * Public: fetch the GLB URL for a zone building.
 */
export function zoneModel(id: ZoneId): string {
  return zoneUrl(id);
}
 
/**
 * Public: fetch the GLB URL for a character.
 */
export function characterModel(kind: CharacterKind): string {
  return characterUrl(kind);
}
 
/**
 * The full list of bundled model URLs. Consumed by useGLTF.preload at app
 * startup so the first scene render doesn't stutter while loading every
 * building at once.
 */
export function allModelUrls(): string[] {
  const zoneIds: ZoneId[] = [
    "office",
    "library",
    "mine",
    "forest",
    "farm",
    "nether",
    "signpost",
    "spawner",
    "tavern"
  ];
  const characterKinds: CharacterKind[] = ["mayor", "villager"];
  return [...zoneIds.map(zoneUrl), ...characterKinds.map(characterUrl)];
}
 
/**
 * Stable string keys used purely for asset-mapping unit tests. These are the
 * canonical filenames; we compare the .href suffix against them to avoid
 * depending on the runtime URL base (which differs between dev, test and
 * prod bundlers).
 */
export const ZONE_MODEL_BASENAMES: Record<ZoneId, string> = {
  office: "zone-office.glb",
  library: "zone-library.glb",
  mine: "zone-mine.glb",
  forest: "zone-forest.glb",
  farm: "zone-farm.glb",
  nether: "zone-nether.glb",
  signpost: "zone-signpost.glb",
  spawner: "zone-spawner.glb",
  tavern: "zone-tavern.glb"
};
 
export const CHARACTER_MODEL_BASENAMES: Record<CharacterKind, string> = {
  mayor: "character-mayor.glb",
  villager: "character-villager.glb"
};