tamamo/src/tools/genmap.js

146 lines
3.6 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 Tileset = sh0rk.Tileset;\n`;
out += `const Trigger = sh0rk.Trigger;\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: 34,
height: 16,
name: "Rpg",
};
}
if (tset.source.includes("Cave")) {
tilesheet = {
width: 14,
height: 5,
name: "Cave",
}
}
});
out += `pub const tileset: Tileset = .${tilesheet.name};\n`;
out += `pub const ts_width = ${tilesheet.width};\n`;
out += `pub const ts_height = ${tilesheet.height};\n\n`;
const genMap = (tiles) => {
out += `pub const data = [${tiles.data.length}]u9{\n `;
tiles.data.forEach((tile, index) => {
tile -= 1;
out += `${tile},`;
if (index !== 0 && index % 10 === 0) {
out += "\n ";
} else {
out += " ";
}
});
out += "\n};\n\n";
};
const genColl = coll => {
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);
let comment = "";
if (obj.name !== "") {
comment = ` // ${obj.name}`;
}
out += ` Rect{.base = Point{.x = ${x}, .y = ${y}}, .width = ${width}, .height = ${height}},${comment}\n`;
});
out += "};\n\n";
};
const genAct = act => {
out += `pub const triggers = [_]Trigger{\n`;
act.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);
let direction = "";
let dialogue = "";
obj.properties.forEach(prop => {
if (prop.name === "direction") {
direction = prop.value;
}
if (prop.name === "dialogue") {
dialogue = prop.value;
}
});
if (direction === "" || dialogue === "") {
throw new Error("Missing direction or dialogue");
}
out += ` Trigger{.aura = Rect{.base = Point{.x = ${x}, .y = ${y}}, .width = ${width}, .height = ${height}}, .direction = Direction.${direction}, .dialogue = "${dialogue}"}, // ${obj.name}\n`;
});
out += "};\n\n";
};
map.layers.forEach(layer => {
if (layer.name == "map") {
genMap(layer);
}
if (layer.name == "coll") {
genColl(layer);
}
if (layer.name == "act") {
genAct(layer);
}
if (layer.name == "spc") {
layer.objects.forEach(obj => {
if (obj.name === "mara_start") {
out += `pub const start_point = Point{ .x = ${Math.round(obj.x)}, .y = ${Math.round(obj.y)} };\n\n`;
}
});
}
});
await Deno.writeTextFile(outFname, out);