-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity_map_editor.py
executable file
·122 lines (100 loc) · 4.79 KB
/
entity_map_editor.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
#!/usr/bin/env python3
import os
from src.change_mode import ChangeMode
from src.map_editor import MapEditor
from src.references_file import Entity
from src.references_file import Tile
class EntityMapEditor(MapEditor):
def __init__(self):
super().__init__()
self.entity_map = dict()
self.path = str()
self.tile_map = None
self.modes = ['Points', 'Lines', 'Fill']
os.system('cls||clear')
while 1:
try:
tile_map_path = self.select_tile_map()
self.tile_map = self.load_tile_map(tile_map_path)
self.entity_map, self.path = self.load_entity_map(tile_map_path)
os.system('cls||clear')
if self.tile_map: break
except ChangeMode: continue
except AssertionError as error:
print(f'Unexpected {error}.')
exit()
except Exception as error:
print(f'Unexpected {error}. Try again \n')
continue
self.main()
def main(self):
os.system('cls||clear')
while 1:
try:
print("Type 'exit' to save and close the program \n")
print(self.dict_to_matrix(self.entity_map, self.tile_map), ' \n')
if self.mode == 0: self.edit_by_points()
elif self.mode == 1: self.edit_by_lines()
elif self.mode == 2: self.edit_by_fill()
else: AssertionError("invalid option")
except ChangeMode: continue
except Exception as error:
print(f"Unexpected {error}. Try again \n")
continue
def edit_by_lines(self):
axis = self.ask_question('Which axis of the map you want to modify? (x | y)')
if axis != 'x' and axis != 'y': raise AssertionError("invalid option")
line = int(self.ask_question('Wich row/column do you want to modify? (starting at 0)'))
start = int(self.ask_question('From which position do you want to modify? (inclusive)'))
end = int(self.ask_question('Until which position do you want to modify? (inclusive)'))
print(f'Select the entity type to replace it with:')
entities = self.get_list_of_elements(Entity)
entities.append(f"Remove ({len(entities)+1})")
entity = int(self.ask_question(self.list_to_str(entities)))
if entity == len(entities):
for pos in range(start, end + 1):
if axis == 'x': self.entity_map.pop((line, pos), None)
elif axis == 'y': self.entity_map.pop((pos, line), None)
else:
if axis == 'x':
for pos in range(start, end + 1):
self.tile_map.get_element((line, pos)) # Validate the position.
self.entity_map[(line, pos)] = Entity(entity)
elif axis == 'y':
for pos in range(start, end + 1):
self.tile_map.get_element((pos, line)) # Validate the position.
self.entity_map[(pos, line)] = Entity(entity)
os.system('cls||clear')
def edit_by_points(self):
row = int(self.ask_question('In which row do you want to add an entity?'))
column = int(self.ask_question('In which column do you want to add an entity?'))
self.tile_map.get_element((row,column)) # Validate the position.
print(f'Select the entity type to replace it with:')
entities = self.get_list_of_elements(Entity)
entities.append(f"Remove ({len(entities)+1})")
entity = int(self.ask_question(self.list_to_str(entities)))
if entity == len(entities): self.entity_map.pop((row, column), None)
else: self.entity_map[(row, column)] = Entity(entity)
os.system('cls||clear')
def edit_by_fill(self):
print(f'Select the tile type to fill:')
tile = Tile(int(self.ask_question(self.list_to_str(self.get_list_of_elements(Tile)))))
print(f'Select the entity type to replace it with:')
entities = self.get_list_of_elements(Entity)
entities.append(f"Remove ({len(entities)+1})")
entity = int(self.ask_question(self.list_to_str(entities)))
for row in range(self.tile_map.length()[0]):
for column in range (self.tile_map.length()[1]):
element = self.tile_map.get_element((row,column))
if element == tile:
if entity == len(entities):
self.entity_map.pop((row,column), None)
else:
self.entity_map[(row,column)] = Entity(entity)
def save(self):
if self.entity_map:
self.save_map(self.entity_map, self.path)
else:
self.delete_file(self.path)
exit()
EntityMapEditor()