-
Notifications
You must be signed in to change notification settings - Fork 0
/
MapGen.py
470 lines (378 loc) · 17.4 KB
/
MapGen.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
import VectorMaps, json, sys
from os import system, path
#Used instead of print() and input() for purposes of accessibility to screenreaders
def dual_print(string):
system("title "+string)
print(string + '\n')
def dual_input(string):
system("title "+string)
return input(string)
#Utility functions
def get_commands():
command = dual_input('Please enter a command: ')
return command
def validate_position(position, bound):
if position not in range(bound + 1):
return False
else:
return True
def export(data):
with open('data.json', 'w') as outfile:
json.dump(data, outfile)
outfile.close()
#Commands that can be used in tile edit mode
def list_fields(tile): #Lists fields within a tile
if tile.fields == []:
dual_print('There are no fields in this tile.')
else:
dual_print('There are ' + str(len(tile.fields)) + ' fields. In order of addition: ')
for f in tile.fields:
dual_print(f.name + ',')
return
def create_field(tile):
field_name = str(dual_input('Enter a name for the new field: '))
for f in tile.fields:
if f.name == field_name:
dual_print('Error: Field named ' + field_name + ' already exists in this tile. Exiting field creation.')
return
while True:
try:
anchor_x = dual_input('Enter the x-position of the field\'s anchor (upper left corner): ')
anchor_x = int(anchor_x)
except:
dual_print('Error: ' + str(anchor_x) + ' is not a number.')
continue
else:
if validate_position(anchor_x, tile.width):
break
else:
dual_print('Error: ' + str(anchor_x) + ' is out of bounds.')
while True:
try:
anchor_y = dual_input('Enter the y-position of the field\'s anchor (upper left corner): ')
anchor_y = int(anchor_y)
except:
dual_print('Error: ' + str(anchor_y) + ' is not a number.')
continue
else:
if validate_position(anchor_y, tile.height):
break
else:
dual_print('Error: ' + str(anchor_y) + ' is out of bounds.')
while True:
try:
field_width = dual_input('Enter the width of the field: ')
field_width = int(field_width)
except:
dual_print('Error: ' + str(field_width) + ' is not a number.')
continue
else:
if validate_position(field_width + anchor_x, tile.width):
break
else:
dual_print('Error: the edge of the field is out of bounds.')
while True:
try:
field_height = dual_input('Enter the height of the field: ')
field_height = int(field_height)
except:
dual_print('Error: ' + str(field_height) + ' is not a number.')
continue
else:
if validate_position(field_height + anchor_y, tile.height):
break
else:
dual_print('Error: the edge of the field is out of bounds. Try shortening it.')
while True:
field_clips = dual_input('Can entities pass through this field? Y/n: ')
if field_clips.lower() == 'y':
field_clips = True
break
elif field_clips.lower() == 'n':
field_clips = False
break
else:
dual_print('Invalid input.')
dual_print('Creating field...')
tile.fields.append(VectorMaps.Field([field_width, field_height],[anchor_x, anchor_y], name=field_name, clipping=field_clips))
dual_print('Done. Returning to tile edit mode.')
return tile
def edit_field(tile):
found = False
field_name = str(dual_input('Enter the name of the field you wish to edit. '))
for f in tile.fields:
if f.name == field_name:
dual_print('Now editing field "' + field_name + '" in tile ' + str(tile.pos) + '.')
found = True
active_field_index = tile.fields.index(f)
if not found:
dual_print('Field "' + field_name + '" was not found. Returning to tile edit mode.')
return
while 1:
option = str(dual_input('Enter the name of the variable you want to edit. Options are: \'anchorX\', \'anchorY\', \'width\', \'height\', and \'name\'. To exit, use \'save\' to save changes and exit or \'cancel\' to revert changes and exit: ')).lower().strip()
if option == 'anchorx':
dual_print('The anchor\'s current coordinates are ' + str(tile.fields[active_field_index].anchor) + '.')
try:
new_x = int(dual_input('Enter the new value for the anchor\'s position on the X axis: '))
except:
dual_print('Error: Not a Number')
continue
else:
if validate_position(new_x + tile.fields[active_field_index].dimensions[0], tile.width):
tile.fields[active_field_index].anchor[0] = new_x
dual_print('Done. The anchor\'s coordinates are now ' + str(tile.fields[active_field_index].anchor) + '.')
else:
dual_print('Error: the edge of the field is now out of bounds. Cancelling action.')
elif option == 'anchory':
try:
new_y = int(dual_input('Enter the new value for the anchor\'s position on the Y axis: '))
except:
dual_print('Error: Not a Number')
continue
else:
if validate_position(new_y + tile.fields[active_field_index].dimensions[1], tile.height):
tile.fields[active_field_index].anchor[1] = new_y
dual_print('Done. The anchor\'s coordinates are now ' + str(tile.fields[active_field_index].anchor) + '.')
else:
dual_print('Error: the edge of the field is now out of bounds. Cancelling action.')
elif option == 'name':
try:
new_name = str(dual_input('Enter the new name for the field: '))
except:
dual_print('Something went wrong. Please report this error.')
continue
else:
tile.fields[active_field_index].name = new_name
dual_print('New name has been set.')
elif option == 'width':
try:
new_width = int(dual_input('Enter the new value for the field\'s width: '))
except:
dual_print('Error: Not a Number')
continue
else:
if validate_position(new_width + tile.fields[active_field_index].anchor[0], tile.width):
tile.fields[active_field_index].dimensions[0] = new_width
dual_print('Done. The field\'s dimensions are now' + str(tile.fields[active_field_index].dimensions) + '.')
else:
dual_print('Error: the edge of the field is now out of bounds. Cancelling action.')
elif option == 'height':
try:
new_height = int(dual_input('Enter the new value for the field\'s height: '))
except:
dual_print('Error: Not a Number')
continue
else:
if validate_position(new_height + tile.fields[active_field_index].anchor[1], tile.height):
tile.fields[active_field_index].dimensions[1] = new_height
dual_print('Done. The field\'s dimensions are now' + str(tile.fields[active_field_index].dimensions) + '.')
else:
dual_print('Error: the edge of the field is now out of bounds. Cancelling action.')
elif option == 'clipping':
field_clips = dual_input('Can entities pass through this field? Y/n: ')
if field_clips.lower() == 'y':
tile.fields[active_field_index].clipping = False
dual_print('Clipping set to False')
break
elif field_clips.lower() == 'n':
tile.fields[active_field_index].clipping = True
dual_print('Clipping set to True')
break
else:
dual_print('Invalid input. Cancelling action.')
elif option == 'cancel':
dual_print('Cancelling all changes. Exiting to tile edit mode.')
return
elif option == 'save':
dual_print('Saving all changes. Exiting to tile edit mode. Note that to save to a file you must export.')
return tile
def delete_field(tile):
found = False
field_name = str(dual_input('Enter the name of the field you wish to delete. '))
for f in tile.fields:
if f.name == field_name:
dual_print('Deleting field...')
found = True
tile.fields.remove(f)
dual_print('Field deleted. Returning to tile edit mode.')
return tile
if not found:
dual_print('Field "' + field_name + '" was not found. Returning to tile edit mode.')
return
def view_field(tile):
found = False
field_name = str(dual_input('Enter the name of the field you wish to view. '))
for f in tile.fields:
if f.name == field_name:
dual_print('Now viewing field "' + field_name + '" in tile ' + str(tile.pos) + '.')
dual_print('anchor: ' + str(f.anchor) + '\ndimensions: ' + str(f.dimensions) + '\nclips: ' + str(f.clipping) )
found = True
return
if not found:
dual_print('Field "' + field_name + '" was not found. Returning to tile edit mode.')
return
def parse_command(command, tile):
command_dict = {
'field' : {'list' : list_fields, 'new' : create_field, 'delete' : delete_field, 'edit' : edit_field, 'view' : view_field},
'goto' : [],
'help' : ['field', 'goto'],
'save' : [],
'exit' : []
}
commands = command.split(' ')
try:
options = command_dict[commands[0]]
except:
dual_print('Error, invalid command.')
else:
if commands[0] == 'field':
try:
if commands[1] in options:
tile = options[commands[1]](tile)
return tile
else:
dual_print('Error, invalid argument')
except:
dual_print('You need to provide an argument. Try \'list\', \'new\', \'delete\', \'edit\', or \'view\'.')
#raise
elif commands[0] == 'help':
try:
if commands[1] not in options:
raise Error
else:
dual_print('Extended help menu is coming soon')
dual_print('Valid arguments are \'field\', \'help\', \'goto\', \'save\', \'exit\'.')
except:
dual_print('Valid commands and arguments are \'field\', \'help\', \'goto\', \'save\', \'exit\'.')
#raise
elif commands[0] == 'save':
return 'save'
elif commands[0] == 'exit':
dual_print('Exiting program.')
sys.exit()
elif commands[0] == 'goto':
while 1:
try:
new_x = int(dual_input('Enter the X-position of the tile you wish to view: '))
except:
dual_print('Error: Not a Number.')
continue
else:
if validate_position(new_x, map.width):
break
else:
dual_print('Error: That position is out of bounds.')
continue
while 1:
try:
new_y = int(dual_input('Enter the Y-position of the tile you wish to view: '))
except:
dual_print('Error: Not a Number.')
continue
else:
if validate_position(new_y, map.height):
break
else:
dual_print('Error: That position is out of bounds.')
continue
return((new_x, new_y))
def setup():
while 1:
try:
MAP_NAME = str(dual_input('Please name the map: '))
except:
print("Invalid name.")
continue
else:
break
while 1:
try:
TILE_WIDTH = int(dual_input('What should the width of each tile be?: '))
except:
print("Error: Not a Number")
continue
else:
break
while 1:
try:
TILE_HEIGHT = int(dual_input('What should the height of each tile be?: '))
except:
print("Error: Not a Number")
continue
else:
break
while 1:
try:
MAP_WIDTH = int(dual_input('How wide should the map be (in tiles)?: '))
except:
print("Error: Not a Number")
continue
else:
break
while 1:
try:
MAP_HEIGHT = int(dual_input('How tall should the map be (in tiles)?: '))
except:
print("Error: Not a Number")
continue
else:
break
map = VectorMaps.Map(MAP_WIDTH, MAP_HEIGHT, name=MAP_NAME)
dual_print('Map object created. Propagating...')
map.propagate(tile_width=TILE_WIDTH, tile_height=TILE_HEIGHT)
dual_print('Done. Now entering tile edit mode.')
return map
def edit_tile(pos):
dual_print('Now editing tile' + str(pos) + '. Type \'help\' for a list of commands.')
while 1:
command = get_commands()
parsed_data = parse_command(command, map.tiles[(pos[0], pos[1])])
if type(parsed_data) == type(map.tiles[(0, 0)]):
map.tiles[(pos[0], pos[1])] = parsed_data
elif type(parsed_data) == type(()):
edit_tile(parsed_data)
break
elif parsed_data == 'exit':
dual_print('Closing program.')
sys.exit()
elif parsed_data == 'save':
dual_print('Saving...')
data = {'CONSTANTS' : {'MAP_NAME' : map.name, 'MAP_HEIGHT' : map.height, 'MAP_WIDTH' : map.width, 'TILE_HEIGHT' : map.tiles[(0, 0)].height, 'TILE_WIDTH' : map.tiles[(0, 0)].width},
'TILES' : {}}
for key, tile in map.tiles.items():
json_tile = {'FIELDS' : []}
for field in tile.fields:
json_field = {'NAME' : field.name, 'DIMENSIONS' : field.dimensions, 'ANCHOR' : field.anchor, 'CLIPPING' : field.clipping}
json_tile['FIELDS'].append(json_field)
data['TILES'][str(key)] = json_tile
export(data)
dual_print('Saved!')
if __name__ == '__main__':
dual_print('Welcome to the VectorMaps Editor.')
while 1:
choice = str(dual_input('To create a new map, enter \'new\'. To load a map, enter \'load\'. To exit, enter \'exit\'.'))
if choice == 'new':
map = setup()
edit_tile((0,0))
break
elif choice == 'load':
fname = str(dual_input('What is the name of the file you want to open?'))
if path.isfile(fname):
with open('data.json', 'r') as infile:
data = json.load(infile)
infile.close()
map = VectorMaps.Map(data['CONSTANTS']['MAP_WIDTH'], data['CONSTANTS']['MAP_HEIGHT'], name=data['CONSTANTS']['MAP_NAME'])
for i in data['TILES']:
coords = i.strip('(').strip(')').split(',')
key = (int(coords[0]), int(coords[1]))
map.tiles[key] = VectorMaps.Tile(key, data['CONSTANTS']['TILE_WIDTH'], data['CONSTANTS']['TILE_HEIGHT'])
for j in data['TILES'][i]['FIELDS']:
map.tiles[key].fields.append(VectorMaps.Field(j['DIMENSIONS'], j['ANCHOR'], clipping=j['CLIPPING'], name=j['NAME']))
edit_tile((0,0))
break
elif choice == 'exit':
dual_print('Exiting program.')
sys.exit()
else:
dual_print('Invalid input.')
continue