helpers.ts 876 B

123456789101112131415161718192021222324
  1. import { readFileSync } from 'node:fs';
  2. import { resolve } from 'node:path';
  3. import { parseMaterials, type MaterialRegistry } from '../src/shared/materials';
  4. import { TerrainBitmap } from '../src/shared/terrain/bitmap';
  5. export const ASSETS = resolve(import.meta.dirname, '../assets');
  6. export function realMaterials(): MaterialRegistry {
  7. return parseMaterials(JSON.parse(readFileSync(resolve(ASSETS, 'materials/materials.json'), 'utf8')));
  8. }
  9. /** Builds a bitmap from ASCII art: '.' = empty, digits = material id, '#' = plate (255). */
  10. export function bitmapFrom(rows: string[]): TerrainBitmap {
  11. const h = rows.length;
  12. const w = rows[0].length;
  13. const bm = new TerrainBitmap(w, h);
  14. rows.forEach((row, y) => {
  15. for (let x = 0; x < w; x++) {
  16. const c = row[x];
  17. bm.data[y * w + x] = c === '.' ? 0 : c === '#' ? 255 : Number(c);
  18. }
  19. });
  20. return bm;
  21. }