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

78 lines
3.3 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
from level.LevelElementSymbols import LevelElementSymbols
from level.elements.BlockElement import BlockElement
2023-03-26 17:19:30 +02:00
from level.elements.GateElement import GateElement
from level.elements.GoalDoorElement import GoalDoorElement
from level.elements.InteractableElement import InteractableElement
2023-03-26 11:21:22 +02:00
from physics.SpriteManager import SpriteManager, DrawLayers
from sprite.SpritesheetManager import SpritesheetManager
BLOCK_SIZE = (12, 12)
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):
2023-03-26 17:19:30 +02:00
requires_dict = {
}
for row_number, row in enumerate(level.tiles):
for tile_number, tile in enumerate(row):
2023-03-26 15:14:06 +02:00
position = (tile_number * BLOCK_SIZE[0], row_number * BLOCK_SIZE[1])
2023-03-26 15:14:06 +02:00
tile_element = LevelElementSymbols.dict[tile['name']]
2023-03-26 15:14:06 +02:00
sprite = None
2023-03-26 15:14:06 +02:00
if tile_element in LevelElementSymbols.BLOCKS_LIST:
spritesheet = self.spritesheet_manager.get_sheet(level.theme + tile_element['sprite_id'])
sprite = BlockElement(spritesheet)
2023-03-26 17:14:02 +02:00
sprite.set_animation_state(str(random.randint(0, 3)))
2023-03-26 15:14:06 +02:00
sprite.position_scale.position = position
2023-03-26 17:19:30 +02:00
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
2023-03-26 15:14:06 +02:00
# tile['id'], tile['requirements']
2023-03-26 11:21:22 +02:00
2023-03-26 15:14:06 +02:00
if sprite is not None:
2023-03-26 17:19:30 +02:00
self.blocks.append(sprite)
2023-03-26 15:14:06 +02:00
self.sprite_manager.add_ui_element(DrawLayers.OBJECTS, sprite)
2023-03-26 11:21:22 +02:00
2023-03-26 17:19:30 +02:00
for block in self.blocks:
if isinstance(block, InteractableElement):
for requirement in requires_dict:
block.connect_element(requirement)
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