tamamo/src/tools/genmap.js

83 lines
1.8 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 ./src/tools/genmap.js ${mapFile}, DO NOT EDIT\n\n`;
out += `const sh0rk = @import("../sh0rk.zig");\n`;
out += `const Direction = sh0rk.Direction;\n`;
out += `const Point = sh0rk.Point;\n`;
out += `const Rect = sh0rk.Rect;\n\n`;
out += `pub const width = ${map.width};\n`;
out += `pub const height = ${map.height};\n\n`;
let tiles = map.layers[0];
let tilesheet = {
width: 10,
height: 5,
};
map.tilesets.forEach(tset => {
if (tset.source.includes("kenney")) {
tilesheet = {
width: 17,
height: 8,
};
}
if (tset.source.includes("Cave")) {
tilesheet = {
width: 10,
height: 5,
}
}
});
out += `pub const ts_width = ${tilesheet.width};\n`;
out += `pub const ts_height = ${tilesheet.height};\n\n`;
out += `pub const data = [${tiles.data.length}]u8{\n `;
tiles.data.forEach((tile, index) => {
tile -= 1;
out += `${tile},`;
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 = [_]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);