From e4993f39afdf75afefdcf2d215cee1add475ee4d Mon Sep 17 00:00:00 2001 From: Skyball2000 Date: Wed, 29 Mar 2023 13:50:48 +0200 Subject: [PATCH] Added playable character loading --- project/data/levels/levels.json | 2 +- project/level/elements/LoadedLevel.py | 8 ++++++ project/level/selection/LevelScreenManager.py | 16 +++++++++++- .../selection/LevelSelectionScreenManager.py | 2 -- project/level/selection/ScreenManager.py | 4 +++ project/main.py | 26 ++++++++++++------- 6 files changed, 45 insertions(+), 13 deletions(-) diff --git a/project/data/levels/levels.json b/project/data/levels/levels.json index ae5684c..c99a025 100644 --- a/project/data/levels/levels.json +++ b/project/data/levels/levels.json @@ -1,7 +1,7 @@ [ { "name": "0-tutorial", - "theme": "cave", + "theme": "tutorial", "abilities": [ ], "file": "0-tutorial.csv" diff --git a/project/level/elements/LoadedLevel.py b/project/level/elements/LoadedLevel.py index 198280a..a019263 100644 --- a/project/level/elements/LoadedLevel.py +++ b/project/level/elements/LoadedLevel.py @@ -13,6 +13,8 @@ from level.elements.static.SimpleBlockLevelElement import SimpleBlockLevelElemen from level.elements.static.UnknownTileLevelElement import UnknownTileLevelElement from physics import ConstantsParser from physics.SpriteManager import SpriteManager, DrawLayers +from physics.sprites.PlayerSprite import PlayerSprite +from sprite.PositionScale import PositionScale from sprite.SpritesheetManager import SpritesheetManager TILES = { @@ -62,6 +64,12 @@ class LoadedLevel: if name in TILES: element = TILES[name](tile, self) + + elif name == 'C': + ghost_character = PlayerSprite(self.spritesheet_manager.get_sheet("ghost_character")) + ghost_character.position_scale = PositionScale((position[0], position[1]), (1, 1)) + self.sprite_manager.add_ui_element(DrawLayers.OBJECTS, ghost_character) + elif not name == '': print(f"Unknown tile at {position}: {name}") element = UnknownTileLevelElement(tile, self) diff --git a/project/level/selection/LevelScreenManager.py b/project/level/selection/LevelScreenManager.py index f86d1ae..d7ba526 100644 --- a/project/level/selection/LevelScreenManager.py +++ b/project/level/selection/LevelScreenManager.py @@ -1,16 +1,30 @@ from abc import abstractmethod +from level.Level import Level +from level.elements.LoadedLevel import LoadedLevel from level.selection.ScreenManager import ScreenManager from physics.SpriteManager import SpriteManager +from physics.TickData import TickData from sprite.SpritesheetManager import SpritesheetManager +from ui_elements.KeyManager import KeyManager class LevelScreenManager(ScreenManager): - def __init__(self, sprite_manager: SpriteManager, spritesheet_manager: SpritesheetManager, main_loop): + def __init__(self, sprite_manager: SpriteManager, spritesheet_manager: SpritesheetManager, main_loop, + level: Level): super().__init__(sprite_manager, spritesheet_manager, main_loop) + self.level = level + self.loaded_level = LoadedLevel(sprite_manager, spritesheet_manager) + self.loaded_level.load_level(self.level) + def initialize(self): pass def destroy(self): super().destroy() + self.loaded_level.destroy_level() + + def tick(self, tick_data: TickData): + if tick_data.key_manager.is_keymap_down(KeyManager.KEY_ESCAPE): + self.main_loop.select_level_selection() diff --git a/project/level/selection/LevelSelectionScreenManager.py b/project/level/selection/LevelSelectionScreenManager.py index b789be6..9f966b7 100644 --- a/project/level/selection/LevelSelectionScreenManager.py +++ b/project/level/selection/LevelSelectionScreenManager.py @@ -1,5 +1,3 @@ -from abc import abstractmethod - from level.Level import Level from level.LevelManager import LevelManager from level.selection.ScreenManager import ScreenManager diff --git a/project/level/selection/ScreenManager.py b/project/level/selection/ScreenManager.py index 29c9322..8bc1ce6 100644 --- a/project/level/selection/ScreenManager.py +++ b/project/level/selection/ScreenManager.py @@ -1,6 +1,7 @@ from abc import abstractmethod from physics.SpriteManager import SpriteManager +from physics.TickData import TickData from sprite.SpritesheetManager import SpritesheetManager from ui_elements.UiElement import UiElement @@ -24,3 +25,6 @@ class ScreenManager: def destroy(self): for element in self.elements: self.sprite_manager.remove_ui_element(element) + + def tick(self, tick_data: TickData): + pass diff --git a/project/main.py b/project/main.py index 404ebf2..8097e6f 100644 --- a/project/main.py +++ b/project/main.py @@ -71,6 +71,8 @@ class MainLoop: self.game_state = self.GAME_STATE_MENU self.set_game_state(self.GAME_STATE_LEVEL_SELECTION) + self.level: Optional[Level] = None + def update_position_scale(self, position_scale: PositionScale): self.screen_transform = position_scale self.window_size = ( @@ -83,9 +85,13 @@ class MainLoop: self.screen = pygame.display.set_mode((self.window_size[0], self.window_size[1])) def select_level(self, level: Level): - print(level.name) + print('Loading level', level.name) + self.level = level self.set_game_state(self.GAME_STATE_LEVEL) + def select_level_selection(self): + self.set_game_state(self.GAME_STATE_LEVEL_SELECTION) + def set_game_state(self, game_state: str): self.game_state = game_state @@ -98,7 +104,7 @@ class MainLoop: ) elif self.game_state == self.GAME_STATE_LEVEL: self.screen_manager = LevelScreenManager( - self.sprite_manager, self.spritesheet_manager, self + self.sprite_manager, self.spritesheet_manager, self, self.level ) elif self.game_state == self.GAME_STATE_LEVEL_SELECTION: self.screen_manager = LevelSelectionScreenManager( @@ -130,13 +136,15 @@ class MainLoop: quit() self.screen.fill((0, 0, 0)) - self.sprite_manager.tick( - TickData(apply_frame_rate(1, self.frame_rate), - pygame_events, - self.key_manager, - click_events, - self.screen_transform) - ) + + tick_data = TickData(apply_frame_rate(1, self.frame_rate), + pygame_events, + self.key_manager, + click_events, + self.screen_transform) + + self.screen_manager.tick(tick_data) + self.sprite_manager.tick(tick_data) self.sprite_manager.draw(self.screen, self.screen_transform) pygame.display.update()