-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerate.py
108 lines (93 loc) · 4.16 KB
/
generate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import json
import sys
from typing import Any
# Run command:
# python ./generate.py ./entities.json ./map_template.json
class MapConfigGenerator:
# Data loaded from json
entities_data: dict[str, Any] = {}
map_data: dict[str, Any] = {}
# Loaded values
entities: dict[int, str] = {} # entities.json/tiles[first].{id, type}
entities_count: int = 0 # entities.json/tilecount
firstgid: int = 0 # map.json/tilesets.firstgid
level_array: list[int] = [] # map.json/layers.[layer where name is Map].data
level_height: int = 0 # map.json/layers.[layer where name is Map].height
level_width: int = 0 # map.json/layers.[layer where name is Map].width
level_name: str = "" # map.json/properties.level_name
level_floor_texture: str = "" # map.json/properties.floor_texture
level_ceiling_texture: str = "" # map.json/properties.ceiling_texture
level_ceiling_height: int = 1 # map.json/properties.ceiling_height
def __init__(self, entities_file, input_map_file):
# Read entities data
with open(entities_file, "r") as ef:
self.entities_data = json.load(ef)
# Read map data
with open(input_map_file, "r") as imf:
self.map_data = json.load(imf)
def prepare_values(self):
# Get firstgid
for tileset in self.map_data["tilesets"]:
if tileset["source"] == "entities.json":
self.firstgid = tileset["firstgid"]
# Get all entities
for entity in self.entities_data["tiles"]:
self.entities[entity["id"] + self.firstgid] = entity["type"]
self.entities_count = len(self.entities)
# Get data from right layer
map_layer: dict[int, str] = {}
for layer in self.map_data["layers"]:
if layer["name"] == "Map":
map_layer = layer
# Since in tiled 0 is "untouched" and in game its "empty",
# just make any empty zones and untouched ones 0
data_array: list[str] = []
for item in map_layer["data"]:
if item <= 0:
data_array.append("empty")
else:
data_array.append(f"{self.entities[item]}")
self.level_array = (
str(data_array).strip(r"[]").replace(" ", "").replace("'", "")
)
self.level_height = map_layer["height"]
self.level_width = map_layer["width"]
# Get properties
for prop in self.map_data["properties"]:
if prop["name"] == "level_name":
self.level_name = str(prop["value"]).replace(",. ", "")
elif prop["name"] == "ceiling_height":
self.level_ceiling_height = prop["value"]
elif prop["name"] == "ceiling_texture":
self.level_ceiling_texture = prop["value"]
elif prop["name"] == "floor_texture":
self.level_floor_texture = prop["value"]
print(self.level_name)
def generate_level_config(self):
# Set map output file name
# NOTE: This will override any similarly named level!
level_file = f"{self.level_name}.cfg"
with open(level_file, "w+") as f:
f.write(f"name={self.level_name}\n")
f.write(f"height={self.level_height}\n")
f.write(f"width={self.level_width}\n")
f.write(f"data={self.level_array}\n")
f.write(f"ceilingheight={self.level_ceiling_height}\n")
f.write(f"floortexture={self.level_floor_texture}\n")
f.write(f"ceilingtexture={self.level_ceiling_texture}\n")
f.write(f"entitiestotal={self.entities_count}\n")
for entity in self.entities:
f.write(f"{entity}={self.entities[entity]}\n")
def print_odin_entity_creation_switch(self):
print(
"Copy the following switch-case ladder to AddEntityToScene if you modified entities!"
)
for entity in self.entities:
print(f'case "{self.entities[entity]}":')
def run(self):
self.prepare_values()
self.generate_level_config()
self.print_odin_entity_creation_switch()
if __name__ == "__main__":
mcg = MapConfigGenerator(sys.argv[1], sys.argv[2])
mcg.run()