2024-02-27 03:51:26 +00:00
|
|
|
import sys
|
|
|
|
import json
|
|
|
|
import shared
|
|
|
|
|
|
|
|
TEMPLATE = """
|
|
|
|
// generated code! be nice
|
|
|
|
#include "sys/sys.h"
|
|
|
|
|
2024-02-28 00:00:57 +00:00
|
|
|
sys_maptile map_{{map_name}}_data[{{width * height}}] = { {{ tiles|join(",") }} };
|
|
|
|
sys_map map_{{map_name}} = {
|
|
|
|
.tiles=map_{{map_name}}_data,
|
2024-02-27 03:51:26 +00:00
|
|
|
.width={{width}},
|
|
|
|
.height={{height}},
|
|
|
|
};
|
|
|
|
""".lstrip()
|
|
|
|
|
|
|
|
def main(map_name, fname_ldtk, fname_c):
|
2024-02-27 23:09:51 +00:00
|
|
|
width, height, tiles = load_mapdata(fname_ldtk)
|
2024-02-27 03:51:26 +00:00
|
|
|
|
|
|
|
with open(fname_c, "wt") as output:
|
|
|
|
output.write(
|
|
|
|
shared.templates.from_string(TEMPLATE).render(
|
|
|
|
map_name=map_name,
|
|
|
|
tiles=tiles,
|
|
|
|
width=width,
|
|
|
|
height=height,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def load_mapdata(fname_ldtk):
|
|
|
|
sparse_tiles = {}
|
2024-02-27 23:09:51 +00:00
|
|
|
entities = []
|
|
|
|
|
2024-02-27 03:51:26 +00:00
|
|
|
with open(fname_ldtk, "rt") as f:
|
|
|
|
data = json.load(f)
|
|
|
|
for level in data["levels"]:
|
|
|
|
level_x = level["worldX"] // 8
|
|
|
|
level_y = level["worldY"] // 8
|
|
|
|
for layer in level["layerInstances"]:
|
2024-02-27 23:09:51 +00:00
|
|
|
if layer["__identifier"] == "conceptual":
|
2024-02-27 03:51:26 +00:00
|
|
|
# for right now I use the vague layer to assign tiles
|
2024-02-27 23:09:51 +00:00
|
|
|
for tile in layer["autoLayerTiles"]:
|
|
|
|
x, y = tile["px"]
|
|
|
|
x //= 8
|
|
|
|
y //= 8
|
|
|
|
ix = tile["t"]
|
2024-02-27 03:51:26 +00:00
|
|
|
sparse_tiles[level_x + x, level_y + y] = ix
|
|
|
|
|
|
|
|
if layer["__identifier"] == "entities":
|
|
|
|
for e in layer["entityInstances"]:
|
2024-02-27 23:09:51 +00:00
|
|
|
raise NotImplementedError()
|
2024-02-27 03:51:26 +00:00
|
|
|
|
|
|
|
x_min = 0
|
|
|
|
y_min = 0
|
|
|
|
assert not any(x for (x, _) in sparse_tiles if x < x_min), "level can't be left of (0, 0)"
|
|
|
|
assert not any(y for (_, y) in sparse_tiles if y < y_min), "level can't be up from (0, 0)"
|
|
|
|
width = max(x for (x, _) in sparse_tiles) + 1
|
|
|
|
height = max(y for (_, y) in sparse_tiles) + 1
|
|
|
|
dense_tiles = []
|
|
|
|
for y in range(height):
|
|
|
|
for x in range(width):
|
|
|
|
k = (x, y)
|
|
|
|
if k in sparse_tiles:
|
|
|
|
dense_tiles.append(sparse_tiles[k])
|
|
|
|
else:
|
2024-02-27 23:09:51 +00:00
|
|
|
dense_tiles.append(255)
|
2024-02-27 03:51:26 +00:00
|
|
|
|
2024-02-27 23:09:51 +00:00
|
|
|
return width, height, dense_tiles
|
2024-02-27 03:51:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
def annot_xy(lst, w, h):
|
|
|
|
assert len(lst) == w * h
|
|
|
|
for y in range(h):
|
|
|
|
for x in range(w):
|
|
|
|
yield x, y, lst[y * w + x]
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
assert len(sys.argv) == 4, \
|
|
|
|
"there must be three args (map name, src ldtk, out c)"
|
|
|
|
main(sys.argv[1], sys.argv[2], sys.argv[3])
|