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

38 lines
1.2 KiB
Python
Raw Normal View History

2023-03-25 15:41:32 +01:00
import json
from level.Level import Level
class LevelManager:
def __init__(self, level_dir: str):
self.level_dir = level_dir
self.levels = []
def load_from_config(self, config_file: str):
print('Loading levels from sprite sheet config file', config_file)
config = json.load(open(config_file))
for level_data in config:
csv = self.parse_csv(self.level_dir + '/' + level_data['file'])
level = Level(level_data['name'], level_data['theme'], level_data['abilities'], csv)
self.levels.append(level)
def parse_csv(self, file: str):
csv_array = []
with open(file) as csvfile:
for row in csvfile:
split_row = row.split(',')
filtered_row = list(map(lambda x: x.replace('\n', ''), split_row))
csv_array.append(filtered_row)
return csv_array
def get_levels_by_theme(self) -> dict[str, list[Level]]:
levels_by_theme = {}
for level in self.levels:
if level.theme not in levels_by_theme:
levels_by_theme[level.theme] = []
levels_by_theme[level.theme].append(level)
return levels_by_theme