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

44 lines
1.6 KiB
Python
Raw Normal View History

2023-03-26 17:14:02 +02:00
import random
2023-03-26 11:21:22 +02:00
from level.Level import Level
2023-03-27 11:46:03 +02:00
from level.elements.ButtonInputLevelElement import ButtonInputLevelElement
from level.elements.SimpleBlockLevelElement import SimpleBlockLevelElement
from physics import ConstantsParser
2023-03-26 11:21:22 +02:00
from physics.SpriteManager import SpriteManager, DrawLayers
from sprite.SpritesheetManager import SpritesheetManager
2023-03-27 11:46:03 +02:00
TILES = {
'#': SimpleBlockLevelElement,
'L': ButtonInputLevelElement, # TODO
'P': ButtonInputLevelElement
}
2023-03-26 15:14:06 +02:00
2023-03-26 11:21:22 +02:00
class LoadedLevel:
def __init__(self, sprite_manager: SpriteManager, spritesheet_manager: SpritesheetManager):
self.sprite_manager = sprite_manager
self.spritesheet_manager = spritesheet_manager
self.blocks = []
2023-03-26 11:21:22 +02:00
def load_level(self, level: Level):
for row_number, row in enumerate(level.tiles):
for tile_number, tile in enumerate(row):
2023-03-27 11:46:03 +02:00
position = (tile_number * ConstantsParser.CONFIG.block_size[0],
row_number * ConstantsParser.CONFIG.block_size[1])
tile['position'] = position
name = tile['name']
if name in TILES:
element = TILES[name](tile, self)
self.blocks.append(element)
self.sprite_manager.add_ui_element(DrawLayers.OBJECTS, element)
element.load(self.sprite_manager, self.spritesheet_manager, level)
elif not name == '':
print(f"Unknown tile: {name}")
2023-03-26 17:19:30 +02:00
def destroy_level(self):
2023-03-26 15:14:06 +02:00
for block in self.blocks:
self.sprite_manager.remove_ui_element(block)
del self.blocks