diff --git a/AndorsTrail/create_worlds.bat b/AndorsTrail/create_worlds.bat new file mode 100644 index 000000000..a612488b2 --- /dev/null +++ b/AndorsTrail/create_worlds.bat @@ -0,0 +1,7 @@ +@echo off +echo Creating folder for worlds +mkdir %~dp0\res\xml\worlds +echo Creating worlds +python %~dp0\tools\create_worlds.py %~dp0\res\xml\ +echo Done! +# pause \ No newline at end of file diff --git a/AndorsTrail/tools/create_worlds.py b/AndorsTrail/tools/create_worlds.py new file mode 100644 index 000000000..591a8996a --- /dev/null +++ b/AndorsTrail/tools/create_worlds.py @@ -0,0 +1,52 @@ +import json +import sys +from pathlib import Path +from xml.etree import ElementTree as etree + + +def convert(xml_path: Path, world_folder: Path): + with open(xml_path, 'r') as xml_f: + xml = etree.fromstring(xml_f.read()) + + maps_folder = world_folder.parent + + for segment in xml.findall('segment'): + segment_id = segment.get('id') + world = {} + + maps = [] + for xml_map in segment.findall('map'): + x = int(xml_map.get('x')) + y = int(xml_map.get('y')) + id = xml_map.get('id') + with open(Path(maps_folder, f'{id}.tmx'), 'r') as map_f: + map_tmx = etree.fromstring(map_f.read()) + + tilewidth = int(map_tmx.get('tilewidth')) + tileheight = int(map_tmx.get('tileheight')) + width = int(map_tmx.get('width')) + height = int(map_tmx.get('height')) + + map = { + 'fileName': f'../{id}.tmx', + "height": width * tilewidth, + "width": height * tileheight, + 'x': x * tilewidth, + 'y': y * tileheight, + } + maps.append(map) + + world['maps'] = maps + world["onlyShowAdjacentMaps"] = False + world["type"] = "world" + with open(Path(world_folder, f'{segment_id}.world'), 'w') as f: + json.dump(world, f, indent=4) + + +if __name__ == '__main__': + if sys.argv: + folder = Path(sys.argv[1]) + else: + folder = Path('./res\\xml\\') + convert(Path(folder, 'worldmap.xml'), + Path(folder, 'worlds'))