NPCs 1: tools to integrate properties
This commit is contained in:
@@ -17,15 +17,37 @@ sys_map map_{{map_name}} = {
|
||||
void map_{{map_name}}_{{entity_type}}_create(sys_i32 x, sys_i32 y);
|
||||
{% endfor %}
|
||||
|
||||
{% for entity_name, field_name, c_type, render_mode in entity_fields %}
|
||||
{% if render_mode == "scalar" %}
|
||||
void map_{{map_name}}_{{entity_name}}_set_{{field_name}}({{c_type}} value);
|
||||
{% elif render_mode == "array" %}
|
||||
void map_{{map_name}}_{{entity_name}}_add_{{field_name}}({{c_type}} value);
|
||||
{% else %}
|
||||
wtf; // {{ render_mode }}
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
|
||||
void map_{{map_name}}_create_entities() {
|
||||
{% for entity in entities %}
|
||||
map_{{map_name}}_{{entity.type}}_create({{entity.x}}, {{entity.y}});
|
||||
|
||||
{% for field in entity.fields %}
|
||||
{% if field.renderMode == "scalar" %}
|
||||
map_{{map_name}}_{{entity.type}}_set_{{field.name}}({{field.value|safe}});
|
||||
{% elif field.renderMode == "array" %}
|
||||
{% for s in field.value %}
|
||||
map_{{map_name}}_{{entity.type}}_add_{{field.name}}({{s|safe}});
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
|
||||
{% endfor %}
|
||||
}
|
||||
""".lstrip()
|
||||
|
||||
def main(map_name, fname_ldtk, fname_c):
|
||||
width, height, tiles, entities = load_mapdata(fname_ldtk)
|
||||
width, height, tiles, entities, entity_types, entity_fields = load_mapdata(fname_ldtk)
|
||||
print(entity_fields)
|
||||
|
||||
with open(fname_c, "wt") as output:
|
||||
output.write(
|
||||
@@ -34,8 +56,9 @@ def main(map_name, fname_ldtk, fname_c):
|
||||
tiles=tiles,
|
||||
width=width,
|
||||
height=height,
|
||||
entity_types=sorted(set(i["type"] for i in entities)),
|
||||
entities=entities,
|
||||
entity_types=entity_types,
|
||||
entity_fields=entity_fields,
|
||||
)
|
||||
)
|
||||
|
||||
@@ -43,6 +66,8 @@ def main(map_name, fname_ldtk, fname_c):
|
||||
def load_mapdata(fname_ldtk):
|
||||
sparse_tiles = {}
|
||||
entities = []
|
||||
entity_types = set()
|
||||
entity_fields = set()
|
||||
|
||||
with open(fname_ldtk, "rt") as f:
|
||||
data = json.load(f)
|
||||
@@ -62,11 +87,27 @@ def load_mapdata(fname_ldtk):
|
||||
if layer["__identifier"] == "entities":
|
||||
for e in layer["entityInstances"]:
|
||||
# TODO: Other fields?
|
||||
entities.append({
|
||||
entity = {
|
||||
"type": e["__identifier"],
|
||||
"x": e["__worldX"] // 8,
|
||||
"y": e["__worldY"] // 8,
|
||||
})
|
||||
"fields": []
|
||||
}
|
||||
for f in e["fieldInstances"]:
|
||||
field = {
|
||||
"name": f["__identifier"],
|
||||
}
|
||||
field["value"], field["renderMode"], rendered_type = format_field_value(f["__type"], f["__value"])
|
||||
entity["fields"].append(field)
|
||||
entity_fields.add((
|
||||
entity["type"],
|
||||
field["name"],
|
||||
rendered_type,
|
||||
field["renderMode"]
|
||||
))
|
||||
|
||||
entities.append(entity)
|
||||
entity_types.add(entity["type"])
|
||||
|
||||
x_min = 0
|
||||
y_min = 0
|
||||
@@ -83,7 +124,20 @@ def load_mapdata(fname_ldtk):
|
||||
else:
|
||||
dense_tiles.append(255)
|
||||
|
||||
return width, height, dense_tiles, entities
|
||||
return width, height, dense_tiles, entities, entity_types, entity_fields
|
||||
|
||||
|
||||
def format_field_value(ty, val):
|
||||
if ty == "Int":
|
||||
return str(val), "scalar", "sys_i32"
|
||||
elif ty == "String":
|
||||
if val is None:
|
||||
return "NULL", "scalar", "const char*"
|
||||
return json.dumps(val), "scalar" # this is close enough to being right in C
|
||||
elif ty == "Array<Int>":
|
||||
return [format_field_value("Int", i)[0] for i in val], "array", "sys_i32"
|
||||
elif ty == "Array<String>":
|
||||
return [format_field_value("String", i)[0] for i in val], "array", "const char*"
|
||||
|
||||
|
||||
def annot_xy(lst, w, h):
|
||||
|
Reference in New Issue
Block a user