sep-pm-platformer/project/level/Level.py

45 lines
1.6 KiB
Python
Raw Normal View History

2023-03-25 15:41:32 +01:00
LEVEL_SIZE = (71, 40)
class Level:
2023-03-26 10:40:17 +02:00
def __init__(self, name: str, theme: str, abilities: list[str], csv_grid: list[list[str]]):
2023-03-25 15:41:32 +01:00
self.name = name
self.theme = theme
self.abilities = abilities
self.tiles = self.parse_csv_to_2darray(csv_grid)
def __str__(self):
return 'name: ' + self.name + ', theme: ' + self.theme + ', allowed abilities: ' + str(self.abilities)
2023-03-26 10:40:17 +02:00
def parse_csv_to_2darray(self, csv_grid: list[list[str]]):
2023-03-25 15:41:32 +01:00
tiles = []
for line_number, line in enumerate(csv_grid):
if line_number <= LEVEL_SIZE[1]:
tiles.append([])
for item_number, item in enumerate(line):
if item_number <= LEVEL_SIZE[0]:
tiles[line_number].append({'tile': item})
else:
print('Level is too wide:', item_number, '>', LEVEL_SIZE[0])
break
elif line[0] != '':
x = int(line[0])
y = int(line[1])
tile = tiles[x][y]
for i in range(2, len(line)):
if line[i] == '':
break
split_item = line[i].split('=')
if split_item[0] == 'id':
tile[split_item[0]] = split_item[1]
elif split_item[0] == 'requires':
tile[split_item[0]] = split_item[1].split(';')
else:
raise ValueError('Incorrect attribute name: ' + split_item[0])
tiles[x][y] = tile
return tiles