started implementing level creation logic

main
Stephan Halder 2023-03-26 14:12:46 +02:00
parent 65c8a17543
commit a3a09f2c11
6 changed files with 92 additions and 16 deletions

View File

@ -19,7 +19,7 @@ class Level:
tiles.append([]) tiles.append([])
for item_number, item in enumerate(line): for item_number, item in enumerate(line):
if item_number <= LEVEL_SIZE[0]: if item_number <= LEVEL_SIZE[0]:
tiles[line_number].append({'tile': item}) tiles[line_number].append({'name': item})
else: else:
print('Level is too wide:', item_number, '>', LEVEL_SIZE[0]) print('Level is too wide:', item_number, '>', LEVEL_SIZE[0])
break break

View File

@ -6,9 +6,51 @@ DYNAMIC = 'dynamic'
class LevelElementSymbols: class LevelElementSymbols:
SOLID_BLOCK = { SOLID_BLOCK = {
'symbol': '#', 'symbol': '#',
'type': STATIC 'type': STATIC,
'sprite_id': '_block'
}
SOLID_BLOCK_LEFT = {
'type': STATIC,
'sprite_id': '_block_left'
}
SOLID_BLOCK_RIGHT = {
'type': STATIC,
'sprite_id': '_block_right'
} }
AIR = { AIR = {
'symbol': '', 'type': STATIC,
'type': STATIC 'sprite_id': '_air'
} }
GATE = {
'type':STATIC,
'sprite_id': '_gate'
}
GOAL_DOOR = {
'type':STATIC,
'sprite_id': '_goal_door'
}
SMALL_SPIKE = {
'type': STATIC,
'sprite_id': '_small_spike'
}
LEVER = {
'type':STATIC,
'sprite_id': '_lever'
}
PLAYER = {
'type': DYNAMIC,
'sprite_id': '_player'
}
dict = {
'#': SOLID_BLOCK,
'l': SOLID_BLOCK_RIGHT,
'r': SOLID_BLOCK_LEFT,
'': AIR,
'g': GATE,
'd': GOAL_DOOR,
'x': SMALL_SPIKE,
'h': LEVER,
'P': PLAYER
}

View File

@ -1,5 +1,7 @@
from level.elements.LevelElement import LevelElement from level.elements.LevelElement import LevelElement
from sprite.StaticSprite import StaticSprite
class BlockElement(LevelElement): class BlockElement(LevelElement):
pass def __init__(self, x_position : int, y_position : int, sprite : StaticSprite):
super().__init__(x_position, y_position, sprite)

View File

@ -0,0 +1,9 @@
from LevelElement import LevelElement
from sprite.StaticSprite import StaticSprite
class InteractableElement(LevelElement):
def __init__(self, x_position : int, y_position : int, sprite : StaticSprite, block_id : str, requires : list[str]):
super().__init__(x_position, y_position, sprite)
self.block_id = block_id
self.requires = requires

View File

@ -1,2 +1,9 @@
from sprite.StaticSprite import StaticSprite
class LevelElement: class LevelElement:
pass def __init__(self,x_position : int, y_position : int, sprite : StaticSprite):
self.x_position = x_position
self.y_position = y_position
self.sprite = sprite

View File

@ -1,23 +1,39 @@
from level.Level import Level from level.Level import Level
from level.LevelElementSymbols import LevelElementSymbols
from level.elements.BlockElement import BlockElement
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
from sprite.StaticSprite import StaticSprite from sprite.StaticSprite import StaticSprite
BLOCK_SIZE = (16, 16) BLOCK_SIZE = (12, 12)
class LoadedLevel: class LoadedLevel:
def __init__(self, sprite_manager: SpriteManager, spritesheet_manager: SpritesheetManager): def __init__(self, sprite_manager: SpriteManager, spritesheet_manager: SpritesheetManager):
self.sprite_manager = sprite_manager self.sprite_manager = sprite_manager
self.spritesheet_manager = spritesheet_manager self.spritesheet_manager = spritesheet_manager
self.blocks = []
def load_level(self, level: Level): def load_level(self, level: Level):
for row in level.tiles: for row_number, row in enumerate(level.tiles):
for tile in row: self.blocks.append([])
pass for tile_number, tile in enumerate(row):
x_position = row_number * BLOCK_SIZE[0]
y_position = tile_number * BLOCK_SIZE[1]
if LevelElementSymbols.dict[tile] != LevelElementSymbols.PLAYER and LevelElementSymbols.dict[tile] != LevelElementSymbols.AIR:
sprite = StaticSprite(self.spritesheet_manager.get_sheet(level.theme + LevelElementSymbols.dict[tile]['sprite_id']))
sprite.position_scale.position = (x_position, y_position)
sprite.set_animation_state('1')
self.sprite_manager.add_ui_element(DrawLayers.OBJECTS, sprite)
if(tile['id'] is None):
self.blocks[row_number].append(BlockElement(x_position, y_position, sprite))
else:
self.blocks[row_number].append(InteractableElement(x_position, y_position, sprite,tile['id'], tile['requirements']))
sprite = StaticSprite(self.spritesheet_manager.get_sheet('test_1'))
sprite.position_scale.position = (1, 1)
sprite.set_animation_state('idle')
self.sprite_manager.add_ui_element(DrawLayers.OBJECTS, None)
def destroy_level(self):
del self.blocks