From 4892c1a9ff998b8a554bca6f78a5f3b444331da8 Mon Sep 17 00:00:00 2001 From: Stephan Halder <2121472@stud.hs-mannheim.de> Date: Sun, 26 Mar 2023 17:19:30 +0200 Subject: [PATCH] countinued working on level loading --- project/level/LevelElementSymbols.py | 16 +++++---- project/level/elements/DoorElement.py | 14 ++++++++ project/level/elements/GateElement.py | 8 +++++ project/level/elements/GoalDoorElement.py | 7 ++++ project/level/elements/InteractableElement.py | 9 +++-- project/level/elements/LoadedLevel.py | 36 +++++++++++++++++++ 6 files changed, 80 insertions(+), 10 deletions(-) create mode 100644 project/level/elements/DoorElement.py create mode 100644 project/level/elements/GateElement.py create mode 100644 project/level/elements/GoalDoorElement.py diff --git a/project/level/LevelElementSymbols.py b/project/level/LevelElementSymbols.py index 901b542..b5ad9c0 100644 --- a/project/level/LevelElementSymbols.py +++ b/project/level/LevelElementSymbols.py @@ -22,32 +22,32 @@ class LevelElementSymbols: } AIR = { 'type': STATIC, - 'sprite_id': '_air' + 'sprite_id': 'air' } GATE = { 'type':STATIC, - 'sprite_id': '_gate' + 'sprite_id': 'gate' } GOAL_DOOR = { 'type': STATIC, - 'sprite_id': '_goal_door' + 'sprite_id': 'goal_door' } SMALL_SPIKE = { 'type': STATIC, - 'sprite_id': '_small_spike' + 'sprite_id': 'small_spike' } LEVER = { 'type': STATIC, - 'sprite_id': '_lever' + 'sprite_id': 'lever' } PLAYER = { 'type': DYNAMIC, - 'sprite_id': '_player' + 'sprite_id': 'player' } dict = { '#': SOLID_BLOCK, - '+': SOLID_BLOCK, + '+': SOLID_BLOCK_TOP, 'l': SOLID_BLOCK_RIGHT, 'r': SOLID_BLOCK_LEFT, '': AIR, @@ -60,3 +60,5 @@ class LevelElementSymbols: BLOCKS_LIST = [SOLID_BLOCK, SOLID_BLOCK_RIGHT, SOLID_BLOCK_LEFT] + INTERACTABLE_LIST = [LEVER] + diff --git a/project/level/elements/DoorElement.py b/project/level/elements/DoorElement.py new file mode 100644 index 0000000..8ed9286 --- /dev/null +++ b/project/level/elements/DoorElement.py @@ -0,0 +1,14 @@ +from level.elements.StaticLevelElement import StaticLevelElement +from sprite.Spritesheet import Spritesheet + + +class DoorElement(StaticLevelElement): + def __init__(self, spritesheet: Spritesheet, id : str, requires : list[str]): + super().__init__(spritesheet) + self.id = id + self.requires = requires + + def openDoor(self): + pass + def closeDoor(self): + pass \ No newline at end of file diff --git a/project/level/elements/GateElement.py b/project/level/elements/GateElement.py new file mode 100644 index 0000000..d3c21bd --- /dev/null +++ b/project/level/elements/GateElement.py @@ -0,0 +1,8 @@ +from level.elements.DoorElement import DoorElement +from sprite.Spritesheet import Spritesheet + + +class GateElement(DoorElement): + + def __int__(self, spritesheet: Spritesheet, id : str, requires : list[str]): + super().__int__(spritesheet, id, requires) \ No newline at end of file diff --git a/project/level/elements/GoalDoorElement.py b/project/level/elements/GoalDoorElement.py new file mode 100644 index 0000000..f76775b --- /dev/null +++ b/project/level/elements/GoalDoorElement.py @@ -0,0 +1,7 @@ +from level.elements.DoorElement import DoorElement +from sprite.Spritesheet import Spritesheet + + +class GoalDoorElement(DoorElement): + def __int__(self, spritesheet: Spritesheet, id : str, requires : list[str]): + super().__int__(spritesheet, id, requires) \ No newline at end of file diff --git a/project/level/elements/InteractableElement.py b/project/level/elements/InteractableElement.py index 5d6fc5f..36153c0 100644 --- a/project/level/elements/InteractableElement.py +++ b/project/level/elements/InteractableElement.py @@ -1,9 +1,12 @@ -from StaticLevelElement import StaticLevelElement +from level.elements.StaticLevelElement import StaticLevelElement from sprite.Spritesheet import Spritesheet class InteractableElement(StaticLevelElement): - def __init__(self, spritesheet: Spritesheet, block_id: str, requires: list[str]): + def __init__(self, spritesheet: Spritesheet, block_id: str): super().__init__(spritesheet) self.block_id = block_id - self.requires = requires + self.element = [] + + def connect_element(self, element: StaticLevelElement): + self.element.append(element) \ No newline at end of file diff --git a/project/level/elements/LoadedLevel.py b/project/level/elements/LoadedLevel.py index e42351b..14046f2 100644 --- a/project/level/elements/LoadedLevel.py +++ b/project/level/elements/LoadedLevel.py @@ -1,6 +1,9 @@ from level.Level import Level from level.LevelElementSymbols import LevelElementSymbols from level.elements.BlockElement import BlockElement +from level.elements.GateElement import GateElement +from level.elements.GoalDoorElement import GoalDoorElement +from level.elements.InteractableElement import InteractableElement from physics.SpriteManager import SpriteManager, DrawLayers from sprite.SpritesheetManager import SpritesheetManager @@ -14,6 +17,8 @@ class LoadedLevel: self.blocks = [] def load_level(self, level: Level): + requires_dict = { + } for row_number, row in enumerate(level.tiles): for tile_number, tile in enumerate(row): position = (tile_number * BLOCK_SIZE[0], row_number * BLOCK_SIZE[1]) @@ -28,11 +33,42 @@ class LoadedLevel: sprite.set_animation_state('1') sprite.position_scale.position = position + elif tile_element == LevelElementSymbols.GOAL_DOOR: + spritesheet = self.spritesheet_manager.get_sheet(tile_element['sprite_id']) + sprite = GoalDoorElement(spritesheet, tile['id'], tile['requires']) + sprite.set_animation_state('closed') + sprite.position_scale.position = position + for required in tile['requires']: + requires_dict[required] = requires_dict.get(required, []).append(sprite) + + elif tile_element == LevelElementSymbols.GATE: + spritesheet = self.spritesheet_manager.get_sheet(tile_element['sprite_id']) + sprite = GateElement(spritesheet, tile['id'], tile['requires']) + sprite.set_animation_state('closed') + sprite.position_scale.position = position + for required in tile['requires']: + requires_dict[required] = requires_dict.get(required, []).append(sprite) + + elif tile_element in LevelElementSymbols.INTERACTABLE_LIST: + spritesheet = self.spritesheet_manager.get_sheet(tile_element['sprite_id']) + sprite = InteractableElement(spritesheet, tile['id']) + sprite.set_animation_state('on') + sprite.position_scale.position = position + + # tile['id'], tile['requirements'] if sprite is not None: + self.blocks.append(sprite) self.sprite_manager.add_ui_element(DrawLayers.OBJECTS, sprite) + for block in self.blocks: + if isinstance(block, InteractableElement): + for requirement in requires_dict: + block.connect_element(requirement) + + + def destroy_level(self): for block in self.blocks: self.sprite_manager.remove_ui_element(block)