countinued working on level loading

main
Stephan Halder 2023-03-26 17:19:30 +02:00
parent bf0688408e
commit 4892c1a9ff
6 changed files with 80 additions and 10 deletions

View File

@ -22,32 +22,32 @@ class LevelElementSymbols:
} }
AIR = { AIR = {
'type': STATIC, 'type': STATIC,
'sprite_id': '_air' 'sprite_id': 'air'
} }
GATE = { GATE = {
'type':STATIC, 'type':STATIC,
'sprite_id': '_gate' 'sprite_id': 'gate'
} }
GOAL_DOOR = { GOAL_DOOR = {
'type': STATIC, 'type': STATIC,
'sprite_id': '_goal_door' 'sprite_id': 'goal_door'
} }
SMALL_SPIKE = { SMALL_SPIKE = {
'type': STATIC, 'type': STATIC,
'sprite_id': '_small_spike' 'sprite_id': 'small_spike'
} }
LEVER = { LEVER = {
'type': STATIC, 'type': STATIC,
'sprite_id': '_lever' 'sprite_id': 'lever'
} }
PLAYER = { PLAYER = {
'type': DYNAMIC, 'type': DYNAMIC,
'sprite_id': '_player' 'sprite_id': 'player'
} }
dict = { dict = {
'#': SOLID_BLOCK, '#': SOLID_BLOCK,
'+': SOLID_BLOCK, '+': SOLID_BLOCK_TOP,
'l': SOLID_BLOCK_RIGHT, 'l': SOLID_BLOCK_RIGHT,
'r': SOLID_BLOCK_LEFT, 'r': SOLID_BLOCK_LEFT,
'': AIR, '': AIR,
@ -60,3 +60,5 @@ class LevelElementSymbols:
BLOCKS_LIST = [SOLID_BLOCK, SOLID_BLOCK_RIGHT, SOLID_BLOCK_LEFT] BLOCKS_LIST = [SOLID_BLOCK, SOLID_BLOCK_RIGHT, SOLID_BLOCK_LEFT]
INTERACTABLE_LIST = [LEVER]

View File

@ -0,0 +1,14 @@
from level.elements.StaticLevelElement import StaticLevelElement
from sprite.Spritesheet import Spritesheet
class DoorElement(StaticLevelElement):
def __init__(self, spritesheet: Spritesheet, id : str, requires : list[str]):
super().__init__(spritesheet)
self.id = id
self.requires = requires
def openDoor(self):
pass
def closeDoor(self):
pass

View File

@ -0,0 +1,8 @@
from level.elements.DoorElement import DoorElement
from sprite.Spritesheet import Spritesheet
class GateElement(DoorElement):
def __int__(self, spritesheet: Spritesheet, id : str, requires : list[str]):
super().__int__(spritesheet, id, requires)

View File

@ -0,0 +1,7 @@
from level.elements.DoorElement import DoorElement
from sprite.Spritesheet import Spritesheet
class GoalDoorElement(DoorElement):
def __int__(self, spritesheet: Spritesheet, id : str, requires : list[str]):
super().__int__(spritesheet, id, requires)

View File

@ -1,9 +1,12 @@
from StaticLevelElement import StaticLevelElement from level.elements.StaticLevelElement import StaticLevelElement
from sprite.Spritesheet import Spritesheet from sprite.Spritesheet import Spritesheet
class InteractableElement(StaticLevelElement): class InteractableElement(StaticLevelElement):
def __init__(self, spritesheet: Spritesheet, block_id: str, requires: list[str]): def __init__(self, spritesheet: Spritesheet, block_id: str):
super().__init__(spritesheet) super().__init__(spritesheet)
self.block_id = block_id self.block_id = block_id
self.requires = requires self.element = []
def connect_element(self, element: StaticLevelElement):
self.element.append(element)

View File

@ -1,6 +1,9 @@
from level.Level import Level from level.Level import Level
from level.LevelElementSymbols import LevelElementSymbols from level.LevelElementSymbols import LevelElementSymbols
from level.elements.BlockElement import BlockElement 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 physics.SpriteManager import SpriteManager, DrawLayers
from sprite.SpritesheetManager import SpritesheetManager from sprite.SpritesheetManager import SpritesheetManager
@ -14,6 +17,8 @@ class LoadedLevel:
self.blocks = [] self.blocks = []
def load_level(self, level: Level): def load_level(self, level: Level):
requires_dict = {
}
for row_number, row in enumerate(level.tiles): for row_number, row in enumerate(level.tiles):
for tile_number, tile in enumerate(row): for tile_number, tile in enumerate(row):
position = (tile_number * BLOCK_SIZE[0], row_number * BLOCK_SIZE[1]) position = (tile_number * BLOCK_SIZE[0], row_number * BLOCK_SIZE[1])
@ -28,11 +33,42 @@ class LoadedLevel:
sprite.set_animation_state('1') sprite.set_animation_state('1')
sprite.position_scale.position = position 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'] # tile['id'], tile['requirements']
if sprite is not None: if sprite is not None:
self.blocks.append(sprite)
self.sprite_manager.add_ui_element(DrawLayers.OBJECTS, 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): def destroy_level(self):
for block in self.blocks: for block in self.blocks:
self.sprite_manager.remove_ui_element(block) self.sprite_manager.remove_ui_element(block)