tiles.test.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { describe, expect, it } from "vitest";
  2. import type { MapDef } from "@nengine/core";
  3. import { TILE_COLORS, UNKNOWN_TILE_COLOR, tileAssetId, tileColor } from "../src/tiles.js";
  4. function map(tiles?: Record<string, string>): MapDef {
  5. return {
  6. id: "farm",
  7. width: 2,
  8. height: 2,
  9. tilesetRef: "tiles",
  10. spawn: { x: 0, y: 0 },
  11. layers: [{ name: "ground", data: [["grass", "water"], ["grass", "tilled"]] }],
  12. ...(tiles !== undefined ? { tiles } : {}),
  13. };
  14. }
  15. describe("tile palette", () => {
  16. it("maps known tile ids to their colors", () => {
  17. expect(tileColor("grass")).toBe(TILE_COLORS.grass);
  18. expect(tileColor("tilled")).toBe(TILE_COLORS.tilled);
  19. expect(tileColor("water")).toBe(TILE_COLORS.water);
  20. expect(TILE_COLORS.grass).toBe(0x7cb342);
  21. expect(TILE_COLORS.tilled).toBe(0x8d6e63);
  22. expect(TILE_COLORS.water).toBe(0x4fc3f7);
  23. });
  24. it("falls back to a neutral color for unknown tile ids", () => {
  25. expect(tileColor("void")).toBe(UNKNOWN_TILE_COLOR);
  26. expect(tileColor("")).toBe(UNKNOWN_TILE_COLOR);
  27. });
  28. });
  29. describe("tileAssetId (D31 map tiles mapping)", () => {
  30. it("resolves a mapped tile id to its image asset id", () => {
  31. expect(tileAssetId(map({ grass: "grass-1" }), "grass")).toBe("grass-1");
  32. expect(tileAssetId(map({ grass: "grass-1" }), "water")).toBeUndefined();
  33. });
  34. it("returns undefined when the map has no tiles mapping (palette fallback)", () => {
  35. expect(tileAssetId(map(), "grass")).toBeUndefined();
  36. });
  37. });