import random 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 BLOCK_SIZE = (12, 12) class LoadedLevel: def __init__(self, sprite_manager: SpriteManager, spritesheet_manager: SpritesheetManager): self.sprite_manager = sprite_manager self.spritesheet_manager = spritesheet_manager 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]) tile_element = LevelElementSymbols.dict[tile['name']] sprite = None if tile_element in LevelElementSymbols.BLOCKS_LIST: spritesheet = self.spritesheet_manager.get_sheet(level.theme + tile_element['sprite_id']) sprite = BlockElement(spritesheet) sprite.set_animation_state(str(random.randint(0, 3))) 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) del self.blocks