tamamo/src/tools/genmap.js

57 lines
1.4 KiB
JavaScript

if (Deno.args.length !== 2) {
console.log("Usage: ./genmap.js <path-to-map-file> <output-fname>");
Deno.exit(1);
}
const mapFile = Deno.args[0];
const outFname = Deno.args[1];
var out = "";
const data = await Deno.readTextFile(mapFile);
const map = JSON.parse(data);
out += `// Generated by genmap.js, DO NOT EDIT\n\n`;
out += `const sh0rk = @import("../sh0rk.zig");\n`;
out += `const Point = sh0rk.Point;\n`;
out += `const Rect = sh0rk.Rect;\n`;
out += `const Tile = sh0rk.Tile;\n\n`;
let tiles = map.layers[0];
out += `pub const data = [${tiles.data.length}]Tile{\n `;
tiles.data.forEach((tile, index) => {
tile -= 1;
const tileY = Math.trunc(tile / tiles.height);
const tileX = tile % tiles.width;
out += `Tile{.x = ${tileX}, .y = ${tileY}},`;
if (index !== 0 && index % 10 === 0) {
out += "\n ";
} else {
out += " ";
}
});
out += "\n};\n\n";
if (map.layers.length >= 2) {
let coll = map.layers[1];
out += `pub const coll = [${coll.objects.length}]Rect{\n`;
coll.objects.forEach(obj => {
const x = Math.round(obj.x);
const y = Math.round(obj.y);
const width = Math.round(obj.width);
const height = Math.round(obj.height);
out += ` Rect{.base = Point{.x = ${x}, .y = ${y}}, .width = ${width}, .height = ${height}},\n`;
});
out += "};\n\n";
}
await Deno.writeTextFile(outFname, out);