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 = self.excel_column_name_to_number(line[0]) y = int(line[1]) - 1 tile = tiles[y][x] for i in range(2, len(line)): if line[i] == '': continue split_item = line[i].split('=') if split_item[0] in ['id', 'emitter_state', 'debug', 'active_state', 'size', 'weight']: tile[split_item[0]] = split_item[1] elif split_item[0] in ['requires', 'requires_and', 'requires_or', 'requires_xor']: tile[split_item[0]] = split_item[1].split(';') else: raise ValueError('Incorrect attribute name: ' + split_item[0]) tiles[y][x] = tile return tiles @staticmethod def excel_column_name_to_number(name: str): number = 0 for i, c in enumerate(name): number += (ord(c.upper()) - ord('A') + 1) * (26 ** (len(name) - i - 1)) return number - 1