2023-03-25 15:41:32 +01:00
|
|
|
import json
|
2023-03-29 16:08:12 +02:00
|
|
|
from typing import Optional
|
2023-03-25 15:41:32 +01:00
|
|
|
|
|
|
|
from level.Level import Level
|
2023-03-29 16:08:12 +02:00
|
|
|
from physics import ConstantsParser
|
2023-03-25 15:41:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
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
|
2023-03-29 10:45:48 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2023-03-29 16:08:12 +02:00
|
|
|
def find_next_level(self, level: Level) -> Optional[Level]:
|
|
|
|
"""
|
|
|
|
Find the next element in the theme using THEME. If the last element from a theme is reached, the first element
|
|
|
|
of the next theme is returned. If the last THEME is reached, return None.
|
|
|
|
:param level: The level to find the next level from
|
|
|
|
:return: The next level or None
|
|
|
|
"""
|
|
|
|
levels_by_theme = self.get_levels_by_theme()
|
|
|
|
theme_index = ConstantsParser.CONFIG.themes.index(level.theme)
|
|
|
|
level_index = levels_by_theme[level.theme].index(level)
|
|
|
|
|
|
|
|
if level_index + 1 < len(levels_by_theme[level.theme]):
|
|
|
|
return levels_by_theme[level.theme][level_index + 1]
|
|
|
|
elif theme_index + 1 < len(ConstantsParser.CONFIG.themes):
|
|
|
|
return levels_by_theme[ConstantsParser.CONFIG.themes[theme_index + 1]][0]
|
|
|
|
else:
|
|
|
|
return None
|