sep-pm-platformer/project/level/selection/LevelSelectionScreenManager.py

91 lines
3.7 KiB
Python
Raw Normal View History

2023-03-29 14:14:45 +02:00
from typing import Optional
from level.Level import Level
from level.LevelManager import LevelManager
from level.selection.ScreenManager import ScreenManager
from physics.SpriteManager import SpriteManager, DrawLayers
from physics.TickData import TickData
from sprite.PositionScale import PositionScale
from sprite.SpritesheetManager import SpritesheetManager
from sprite.StaticSprite import StaticSprite
from ui_elements.ClickEvent import ClickEvent
from ui_elements.KeyManager import KeyManager
from ui_elements.TextLabel import TextLabel
LABEL_COUNT = 10
OFFSET = (50, 20)
class LevelSelectionScreenManager(ScreenManager):
def __init__(self, sprite_manager: SpriteManager, spritesheet_manager: SpritesheetManager, main_loop,
level_manager: LevelManager):
super().__init__(sprite_manager, spritesheet_manager, main_loop)
self.level_manager = level_manager
self.levels_by_theme = self.level_manager.get_levels_by_theme()
self.themes = ['tutorial', 'cave', 'castle']
self.level_labels: list[TextLabel] = []
2023-03-29 14:14:45 +02:00
self.theme_label: Optional[TextLabel] = None
self.selected_theme = self.themes[0]
def select_next_theme(self, click: ClickEvent):
if click.is_click_down(ClickEvent.CLICK_LEFT):
index = (self.themes.index(self.selected_theme) + 1) % len(self.themes)
self.select_theme(self.themes[index])
def select_prev_theme(self, click):
if click.is_click_down(ClickEvent.CLICK_LEFT):
index = self.themes.index(self.selected_theme) - 1
if index < 0:
index = len(self.themes) - 1
self.select_theme(self.themes[index])
def select_level(self, click: ClickEvent, level: Level):
if click.is_click_down(ClickEvent.CLICK_LEFT):
self.main_loop.select_level(level)
def initialize(self):
arrow_left = StaticSprite(self.spritesheet_manager.get_sheet('ui_arrow'))
arrow_left.set_animation_state('left')
arrow_left.position_scale = PositionScale((OFFSET[0], OFFSET[1]), (3, 3))
arrow_left.add_click_listener(self.select_prev_theme)
self.add_element(DrawLayers.UI, arrow_left)
arrow_right = StaticSprite(self.spritesheet_manager.get_sheet('ui_arrow'))
arrow_right.set_animation_state('right')
arrow_right.position_scale = PositionScale((OFFSET[0] + 50, OFFSET[1]), (3, 3))
arrow_right.add_click_listener(self.select_next_theme)
self.add_element(DrawLayers.UI, arrow_right)
2023-03-29 14:14:45 +02:00
self.theme_label = TextLabel('', OFFSET[0] + 120, OFFSET[1] - 2, font_size=65)
self.theme_label.position_scale.scale = (0.5, 0.5)
self.add_element(DrawLayers.UI, self.theme_label)
for i in range(0, LABEL_COUNT):
label = TextLabel(str(i), OFFSET[0] + 30, OFFSET[1] + 60 + i * 50, font_size=65)
label.position_scale.scale = (0.5, 0.5)
self.level_labels.append(label)
self.add_element(DrawLayers.UI, label)
self.select_theme(self.selected_theme)
def select_theme(self, theme: str):
self.selected_theme = theme
2023-03-29 14:14:45 +02:00
self.theme_label.set_text(theme)
for label in self.level_labels:
label.set_text('')
label.click_listeners = []
for id, level in enumerate(self.levels_by_theme[self.selected_theme]):
self.level_labels[id].set_text(level.name)
self.level_labels[id].add_click_listener(lambda click: self.select_level(click, level))
def destroy(self):
super().destroy()
def tick(self, tick_data: TickData):
if tick_data.key_manager.is_keymap_down(KeyManager.KEY_ESCAPE):
self.main_loop.select_main_menu()