-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_tileset.py
180 lines (137 loc) · 5.69 KB
/
generate_tileset.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/python3
from PIL import Image
import json, math, os, time
config_ref = {}
sheet_ref = {}
def add_directory_to_refs(directory, starting_index: int = 0):
filenames = files_of(directory, ".png")
file_index = starting_index
for filename in filenames:
if filename not in sheet_ref:
sheet_ref[filename] = file_index
config_ref[os.path.splitext(os.path.basename(filename))[0]] = file_index
file_index += 1
def draw_spritesheet(directory, tile_size):
tiles = []
directory_path = "gfx/" + directory + "/"
files = files_of(directory_path, ".png")
for current_file in files :
try:
with Image.open(os.path.normpath(current_file)) as im :
tiles.append(im.getdata())
except:
# print(current_file + " is not a valid image")
pass
max_tiles_per_row = 16.0
spritesheet_width = 0
spritesheet_height = 0
tile_width = tile_size
tile_height = tile_size
if len(tiles) > max_tiles_per_row:
spritesheet_width = tile_width * max_tiles_per_row
required_rows = math.ceil(len(tiles) / max_tiles_per_row)
spritesheet_height = tile_height * required_rows
else:
spritesheet_width = tile_width * len(tiles)
spritesheet_height = tile_height
spritesheet = Image.new("RGBA", (int(spritesheet_width), int(spritesheet_height)))
for current_frame in tiles :
top = tile_height * math.floor((tiles.index(current_frame)) / max_tiles_per_row)
left = tile_width * (tiles.index(current_frame) % max_tiles_per_row)
bottom = top + tile_height
right = left + tile_width
box = (left, top, right, bottom)
box = [int(i) for i in box]
cut_frame = current_frame.crop((0, 0, tile_width,tile_height))
spritesheet.paste(cut_frame, box)
spritesheet.save("pasteldays/" + directory + ".png", "PNG")
def change_config_value(value_key: str, json_data: dict) -> dict:
updated_json = json_data
if value_key in json_data:
sprite_value_type = type(json_data[value_key])
if sprite_value_type is str:
sprite_value = config_ref[json_data[value_key]]
updated_json[value_key] = int(sprite_value)
elif sprite_value_type is list:
sprite_value_list_type = type(json_data[value_key][0])
multi_tile_index = 0
if sprite_value_list_type is str:
for multi_tile_sprite_value in json_data[value_key]:
multi_tile_sprite_value_string = config_ref[multi_tile_sprite_value]
updated_json[value_key][multi_tile_index] = int(multi_tile_sprite_value_string)
multi_tile_index += 1
elif sprite_value_list_type is dict:
for sprite_value_dict in json_data[value_key]:
sprite_value_dict_sprite_value = sprite_value_dict["sprite"]
sprite_value_string = config_ref[sprite_value_dict_sprite_value]
updated_json[value_key][multi_tile_index]["sprite"] = int(sprite_value_string)
multi_tile_index += 1
if "additional_tiles" in json_data:
additional_tile_index = 0
for additional_tile in json_data["additional_tiles"]:
if value_key in additional_tile:
sprite_value_type = type(additional_tile[value_key])
if sprite_value_type is str:
additional_sprite_value = config_ref[additional_tile[value_key]]
updated_json["additional_tiles"][additional_tile_index][value_key] = int(additional_sprite_value)
elif sprite_value_type is list:
sprite_value_list_type = type(additional_tile[value_key][0])
if sprite_value_list_type is str:
multi_additional_tile_index = 0
for multi_tile_sprite_value in additional_tile[value_key]:
multi_additional_sprite_value = config_ref[multi_tile_sprite_value]
updated_json["additional_tiles"][additional_tile_index][value_key][multi_additional_tile_index] = int(multi_additional_sprite_value)
multi_additional_tile_index += 1
additional_tile_index += 1
return updated_json
def files_of(directory, extension) -> list:
filenames = []
for filename in os.listdir(directory):
if filename.endswith(extension):
filenames.append(directory + filename)
if os.path.isdir(directory + filename):
holder = files_of(directory + filename + "/", extension)
for nested_file in holder:
filenames.append(nested_file)
filenames.sort()
return filenames
def fill_item_config(directory) -> list:
directory_path = "gfx/" + directory + "/"
directory_json = []
json_files = files_of(directory_path, ".json")
for filename in json_files:
with open(filename, 'r') as f:
file_json = json.load(f)
file_json = change_config_value("fg", file_json)
file_json = change_config_value("bg", file_json)
directory_json.append(file_json)
f.close()
return directory_json
def generate_refs():
# tiles
add_directory_to_refs("gfx/tiles/")
next_starting_index = math.ceil(len(sheet_ref.keys()) / 16) * 16
previous_keys = len(sheet_ref.keys())
# large
add_directory_to_refs("gfx/large/", next_starting_index)
keys_added = len(sheet_ref.keys()) - previous_keys
# next_starting_index = round(keys_added / 16) * 16
def generate_spritesheets():
draw_spritesheet("tiles", 10)
draw_spritesheet("large", 20)
def generate_tile_config():
tile_config = {}
with open('templates/empty_tile_config.json', 'r') as f:
tile_config = json.load(f)
f.close()
tile_config["tiles-new"][0]["tiles"] = fill_item_config("tiles")
tile_config["tiles-new"][1]["tiles"] = fill_item_config("large")
with open("pasteldays/tile_config.json", "w") as outfile:
json.dump(tile_config, outfile, indent=2)
outfile.close()
def main():
generate_refs()
generate_spritesheets()
generate_tile_config()
if __name__ == "__main__":
main()