First real map with real tiles

This commit is contained in:
2024-02-27 15:09:51 -08:00
parent 9ef88fb34d
commit d823220e90
9 changed files with 3756 additions and 2198 deletions

View File

@ -6,9 +6,6 @@ TEMPLATE = """
// generated code! be nice
#include "sys/sys.h"
sys_i32 {{map_name}}_player_start_x = {{player_start.0}};
sys_i32 {{map_name}}_player_start_y = {{player_start.1}};
sys_maptile {{map_name}}_data[{{width * height}}] = { {{ tiles|join(",") }} };
sys_map {{map_name}} = {
.tiles={{map_name}}_data,
@ -18,13 +15,12 @@ sys_map {{map_name}} = {
""".lstrip()
def main(map_name, fname_ldtk, fname_c):
width, height, tiles, player_start = load_mapdata(fname_ldtk)
width, height, tiles = load_mapdata(fname_ldtk)
with open(fname_c, "wt") as output:
output.write(
shared.templates.from_string(TEMPLATE).render(
map_name=map_name,
player_start=player_start,
tiles=tiles,
width=width,
height=height,
@ -34,29 +30,27 @@ def main(map_name, fname_ldtk, fname_c):
def load_mapdata(fname_ldtk):
sparse_tiles = {}
player_start_xy = None
entities = []
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"]:
w = layer["__cWid"]
h = layer["__cHei"]
if layer["__identifier"] == "vague":
if layer["__identifier"] == "conceptual":
# for right now I use the vague layer to assign tiles
for x, y, ix in annot_xy(layer["intGridCsv"], w, h):
if ix == 0:
continue
for tile in layer["autoLayerTiles"]:
x, y = tile["px"]
x //= 8
y //= 8
ix = tile["t"]
sparse_tiles[level_x + x, level_y + y] = ix
if layer["__identifier"] == "entities":
for e in layer["entityInstances"]:
x, y = e["__grid"]
player_start_xy = (level_x + x, level_y + y)
raise NotImplementedError()
assert player_start_xy is not None, "player start not found"
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)"
@ -70,11 +64,9 @@ def load_mapdata(fname_ldtk):
if k in sparse_tiles:
dense_tiles.append(sparse_tiles[k])
else:
dense_tiles.append(0)
return width, height, dense_tiles, player_start_xy
dense_tiles.append(255)
return width, height, dense_tiles
def annot_xy(lst, w, h):