sep-pm-platformer/project/level/elements/LoadedLevel.py

42 lines
1.5 KiB
Python

import random
from level.Level import Level
from level.LevelElementSymbols import LevelElementSymbols
from level.elements.BlockElement import BlockElement
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):
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
# tile['id'], tile['requirements']
if sprite is not None:
self.sprite_manager.add_ui_element(DrawLayers.OBJECTS, sprite)
def destroy_level(self):
for block in self.blocks:
self.sprite_manager.remove_ui_element(block)
del self.blocks