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

45 lines
1.6 KiB
Python

LEVEL_SIZE = (71, 40)
class Level:
def __init__(self, name: str, theme: str, abilities: list[str], csv_grid: list[str]):
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)
def parse_csv_to_2darray(self, csv_grid: list[str]):
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