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

54 lines
2.0 KiB
Python

from level.Level import Level
from level.elements.ButtonInputLevelElement import ButtonInputLevelElement
from level.elements.SimpleBlockLevelElement import SimpleBlockLevelElement
from level.elements.UnknownTileLevelElement import UnknownTileLevelElement
from physics import ConstantsParser
from physics.SpriteManager import SpriteManager, DrawLayers
from sprite.SpritesheetManager import SpritesheetManager
TILES = {
'#': SimpleBlockLevelElement,
'+': SimpleBlockLevelElement,
'-': SimpleBlockLevelElement,
'l': SimpleBlockLevelElement,
'r': SimpleBlockLevelElement,
'~': SimpleBlockLevelElement,
'|': SimpleBlockLevelElement,
'L': ButtonInputLevelElement, # TODO
'P': ButtonInputLevelElement
}
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 * ConstantsParser.CONFIG.block_size[0],
row_number * ConstantsParser.CONFIG.block_size[1])
tile['position'] = position
name = tile['name']
element = None
if name in TILES:
element = TILES[name](tile, self)
elif not name == '':
print(f"Unknown tile at {position}: {name}")
element = UnknownTileLevelElement(tile, self)
if element:
self.blocks.append(element)
self.sprite_manager.add_ui_element(DrawLayers.OBJECTS, element)
element.load(self.sprite_manager, self.spritesheet_manager, level)
def destroy_level(self):
for block in self.blocks:
self.sprite_manager.remove_ui_element(block)
del self.blocks