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

52 lines
2.0 KiB
Python
Raw Normal View History

2023-03-27 11:46:03 +02:00
from physics import ConstantsParser
2023-03-25 15:41:32 +01:00
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):
2023-03-27 11:46:03 +02:00
if line_number <= ConstantsParser.CONFIG.level_size[1]:
2023-03-25 15:41:32 +01:00
tiles.append([])
for item_number, item in enumerate(line):
2023-03-27 11:46:03 +02:00
if item_number <= ConstantsParser.CONFIG.level_size[0]:
tiles[line_number].append({'name': item})
2023-03-25 15:41:32 +01:00
else:
2023-03-27 11:46:03 +02:00
print('Level is too wide:', item_number, '>', ConstantsParser.CONFIG.level_size[0])
2023-03-25 15:41:32 +01:00
break
elif line[0] != '':
x = self.excel_column_name_to_number(line[0])
y = int(line[1]) - 1
2023-03-27 12:51:44 +02:00
tile = tiles[y][x]
2023-03-25 15:41:32 +01:00
for i in range(2, len(line)):
if line[i] == '':
2023-03-27 12:51:44 +02:00
continue
2023-03-25 15:41:32 +01:00
split_item = line[i].split('=')
if split_item[0] in ['id', 'emitter_state', 'debug', 'active_state']:
2023-03-25 15:41:32 +01:00
tile[split_item[0]] = split_item[1]
elif split_item[0] in ['requires', 'requires_and', 'requires_or', 'requires_xor']:
2023-03-25 15:41:32 +01:00
tile[split_item[0]] = split_item[1].split(';')
else:
raise ValueError('Incorrect attribute name: ' + split_item[0])
2023-03-27 12:51:44 +02:00
tiles[y][x] = tile
2023-03-25 15:41:32 +01:00
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