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

48 lines
1.8 KiB
Python

from physics import ConstantsParser
class Level:
def __init__(self, name: str, theme: str, abilities: list[str], csv_grid: list[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[list[str]]):
tiles = []
for line_number, line in enumerate(csv_grid):
if line_number <= ConstantsParser.CONFIG.level_size[1]:
tiles.append([])
for item_number, item in enumerate(line):
if item_number <= ConstantsParser.CONFIG.level_size[0]:
tiles[line_number].append({'name': item})
else:
print('Level is too wide:', item_number, '>', ConstantsParser.CONFIG.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(';')
elif split_item[0] == 'emitter_state':
tile[split_item[0]] = split_item[1].split(';')
else:
raise ValueError('Incorrect attribute name: ' + split_item[0])
tiles[x][y] = tile
return tiles