-
Notifications
You must be signed in to change notification settings - Fork 2
/
tilesetdef-generator.py
executable file
·93 lines (74 loc) · 2.38 KB
/
tilesetdef-generator.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
#!/usr/bin/python3
import os, sys
import xml.etree.ElementTree as ET
def main():
tileset_w = 0
tileset_h = 0
start_index = 0
end_index = 1
image_path_prefix = "images/tilesets/"
# returns a formated line defining a tile
def getTile(index, w, h, off_x, off_y):
tiles_per_row = tileset_w / w
true_index = index - start_index
left_x = (true_index % tiles_per_row) * w
top_y = (true_index / tiles_per_row) * h
offset_x = (map_w / 2) - off_x
offset_y = h - (map_h /2) - off_y
return "tile=%d,%d,%d,%d,%d,%d,%d\n" % (index,left_x,top_y,w,h,offset_x,offset_y)
# load the TMX file
tiled_file = ""
for opt in sys.argv:
if (opt == sys.argv[0]):
continue
if (os.path.isfile(opt)):
tiled_file = opt
break
if (tiled_file == ""):
print("No Tiled TMX file specified")
return
parser = ET.parse(tiled_file)
root = parser.getroot()
# get the overall tilesize
map_w = int(root.get('tilewidth'))
map_h = int(root.get('tileheight'))
# get the tileset filename
map_name = "output"
for prop in root.find('properties').findall('property'):
if (prop.get('name') == "tileset"):
map_name = os.path.basename(os.path.splitext(prop.get('value'))[0])
map_name += ".txt"
# create the tilesetdef file
map_file = open(map_name,'w')
# add the tileset filename
map_file.write("# %s\n\n" % (map_name))
# add each tile
for tileset in root.findall('tileset'):
tile_w = int(tileset.get('tilewidth'))
tile_h = int(tileset.get('tileheight'))
tileset_name = tileset.get('name')
tileset_w = 0
tileset_h = 0
image = tileset.find('image')
tileset_image = image_path_prefix + os.path.split(image.get('source'))[1]
tileset_w = int(image.get('width'))
tileset_h = int(image.get('height'))
tileoffset_x = 0
tileoffset_y = 0
tileoffset = tileset.find('tileoffset')
if (tileoffset != None):
tileoffset_x = int(tileoffset.get('x'))
tileoffset_y = int(tileoffset.get('y'))
start_index = end_index
# TODO use tilecount?
end_index += (tileset_w/tile_w) * (tileset_h/tile_h)
if (tileset_name != "collision" and tileset_name != "set_rules"):
map_file.write("# " + tileset_name + "\n")
map_file.write("[tileset]\n")
map_file.write("img=%s\n\n" % (tileset_image))
for i in range(int(start_index), int(end_index)):
map_file.write(getTile(i, tile_w, tile_h, tileoffset_x, tileoffset_y))
map_file.write("\n\n")
map_file.close()
if __name__ == "__main__":
main()