Added feature for colliding sprites messaging the main level logic

main
Skyball2000 2023-03-29 15:40:15 +02:00
parent af29b1e009
commit e9e36c1d50
18 changed files with 48 additions and 31 deletions

View File

@ -1,5 +1,6 @@
import uuid import uuid
from abc import abstractmethod from abc import abstractmethod
from typing import Optional
from level.Level import Level from level.Level import Level
from physics.SpriteManager import SpriteManager from physics.SpriteManager import SpriteManager
@ -7,9 +8,10 @@ from sprite.SpritesheetManager import SpritesheetManager
class LevelElement: class LevelElement:
def __init__(self, tile: dict, loaded_level): def __init__(self, tile: dict, loaded_level, level_screen_manager: 'LevelScreenManager'):
self.tile = tile self.tile = tile
self.loaded_level = loaded_level self.loaded_level = loaded_level
self.level_screen_manager: 'LevelScreenManager' = level_screen_manager
if 'id' in tile: if 'id' in tile:
self.id = tile['id'] self.id = tile['id']

View File

@ -52,6 +52,7 @@ class LoadedLevel:
self.spritesheet_manager = spritesheet_manager self.spritesheet_manager = spritesheet_manager
self.blocks = [] self.blocks = []
self.elements_by_id: dict[str, LevelElement] = {} self.elements_by_id: dict[str, LevelElement] = {}
self.level_screen_manager = None
def load_level(self, level: Level): def load_level(self, level: Level):
for row_number, row in enumerate(level.tiles): for row_number, row in enumerate(level.tiles):
@ -67,7 +68,7 @@ class LoadedLevel:
layer = DrawLayers.OBJECTS layer = DrawLayers.OBJECTS
if name in TILES: if name in TILES:
element = TILES[name](tile, self) element = TILES[name](tile, self, self.level_screen_manager)
elif name == 'C': elif name == 'C':
ghost_character = PlayerSprite(self.spritesheet_manager.get_sheet("ghost_character")) ghost_character = PlayerSprite(self.spritesheet_manager.get_sheet("ghost_character"))

View File

@ -5,8 +5,8 @@ from sprite.SpritesheetManager import SpritesheetManager
class BouncingBallLevelElement(PushableLevelElement): class BouncingBallLevelElement(PushableLevelElement):
def __init__(self, tile: dict, loaded_level): def __init__(self, tile: dict, loaded_level, level_screen_manager: 'LevelScreenManager'):
super().__init__(tile, loaded_level) super().__init__(tile, loaded_level, level_screen_manager)
self.bounce_factor = -1 self.bounce_factor = -1

View File

@ -5,6 +5,6 @@ from sprite.DynamicSprite import DynamicSprite
class DynamicLevelElement(DynamicSprite, LevelElement, ABC): class DynamicLevelElement(DynamicSprite, LevelElement, ABC):
def __init__(self, tile: dict, loaded_level): def __init__(self, tile: dict, loaded_level, level_screen_manager: 'LevelScreenManager'):
DynamicSprite.__init__(self) DynamicSprite.__init__(self)
LevelElement.__init__(self, tile, loaded_level) LevelElement.__init__(self, tile, loaded_level, level_screen_manager)

View File

@ -5,8 +5,8 @@ from sprite.SpritesheetManager import SpritesheetManager
class PushableBoxLevelElement(PushableLevelElement): class PushableBoxLevelElement(PushableLevelElement):
def __init__(self, tile: dict, loaded_level): def __init__(self, tile: dict, loaded_level, level_screen_manager: 'LevelScreenManager'):
super().__init__(tile, loaded_level) super().__init__(tile, loaded_level, level_screen_manager)
if 'size' in self.tile: if 'size' in self.tile:
self.position_scale.scale = (float(self.tile['size']), self.position_scale.scale = (float(self.tile['size']),

View File

@ -6,8 +6,8 @@ from physics.TickData import TickData
class PushableLevelElement(DynamicLevelElement, ABC): class PushableLevelElement(DynamicLevelElement, ABC):
def __init__(self, tile: dict, loaded_level): def __init__(self, tile: dict, loaded_level, level_screen_manager: 'LevelScreenManager'):
super().__init__(tile, loaded_level) super().__init__(tile, loaded_level, level_screen_manager)
if 'weight' in self.tile: if 'weight' in self.tile:
self.weight = float(self.tile['weight']) self.weight = float(self.tile['weight'])

View File

@ -8,8 +8,8 @@ from sprite.SpritesheetManager import SpritesheetManager
class ButtonInputLevelElement(InputLevelElement): class ButtonInputLevelElement(InputLevelElement):
def __init__(self, tile: dict, level: Level): def __init__(self, tile: dict, level: Level, level_screen_manager: 'LevelScreenManager'):
super().__init__(tile, level) super().__init__(tile, level, level_screen_manager)
self.is_currently_stood_on = False self.is_currently_stood_on = False
self.is_collider = False self.is_collider = False

View File

@ -5,8 +5,8 @@ from sprite.SpritesheetManager import SpritesheetManager
class GateReceiverLevelElement(ReceiverLevelElement): class GateReceiverLevelElement(ReceiverLevelElement):
def __init__(self, tile: dict, level: Level): def __init__(self, tile: dict, level: Level, level_screen_manager: 'LevelScreenManager'):
super().__init__(tile, level) super().__init__(tile, level, level_screen_manager)
def load(self, sprite_manager: SpriteManager, spritesheet_manager: SpritesheetManager, level: Level): def load(self, sprite_manager: SpriteManager, spritesheet_manager: SpritesheetManager, level: Level):
self.spritesheet = spritesheet_manager.get_sheet('gate') self.spritesheet = spritesheet_manager.get_sheet('gate')

View File

@ -5,8 +5,8 @@ from sprite.SpritesheetManager import SpritesheetManager
class GoalDoorReceiverLevelElement(ReceiverLevelElement): class GoalDoorReceiverLevelElement(ReceiverLevelElement):
def __init__(self, tile: dict, level: Level): def __init__(self, tile: dict, level: Level, level_screen_manager: 'LevelScreenManager'):
super().__init__(tile, level) super().__init__(tile, level, level_screen_manager)
def load(self, sprite_manager: SpriteManager, spritesheet_manager: SpritesheetManager, level: Level): def load(self, sprite_manager: SpriteManager, spritesheet_manager: SpritesheetManager, level: Level):
self.spritesheet = spritesheet_manager.get_sheet('goal_door') self.spritesheet = spritesheet_manager.get_sheet('goal_door')

View File

@ -5,8 +5,8 @@ from level.elements.static.StaticLevelElement import StaticLevelElement
class InputLevelElement(StaticLevelElement, ABC): class InputLevelElement(StaticLevelElement, ABC):
def __init__(self, tile: dict, loaded_level): def __init__(self, tile: dict, loaded_level, level_screen_manager: 'LevelScreenManager'):
super().__init__(tile, loaded_level) super().__init__(tile, loaded_level, level_screen_manager)
if 'emitter_state' in tile: if 'emitter_state' in tile:
self.emitter_state = tile['emitter_state'] == 'true' self.emitter_state = tile['emitter_state'] == 'true'

View File

@ -8,8 +8,8 @@ from sprite.SpritesheetManager import SpritesheetManager
class LeverInputLevelElement(InputLevelElement): class LeverInputLevelElement(InputLevelElement):
def __init__(self, tile: dict, level: Level): def __init__(self, tile: dict, level: Level, level_screen_manager: 'LevelScreenManager'):
super().__init__(tile, level) super().__init__(tile, level, level_screen_manager)
self.is_currently_stood_on = False self.is_currently_stood_on = False
self.is_collider = False self.is_collider = False

View File

@ -5,8 +5,8 @@ from physics.TickData import TickData
class ReceiverLevelElement(StaticLevelElement, ABC): class ReceiverLevelElement(StaticLevelElement, ABC):
def __init__(self, tile: dict, loaded_level): def __init__(self, tile: dict, loaded_level, level_screen_manager: 'LevelScreenManager'):
super().__init__(tile, loaded_level) super().__init__(tile, loaded_level, level_screen_manager)
self.requires = { self.requires = {
} }

View File

@ -17,8 +17,8 @@ BLOCK_TYPES = {
class SimpleBlockLevelElement(StaticLevelElement): class SimpleBlockLevelElement(StaticLevelElement):
def __init__(self, tile: dict, loaded_level): def __init__(self, tile: dict, loaded_level, level_screen_manager: 'LevelScreenManager'):
super().__init__(tile, loaded_level) super().__init__(tile, loaded_level, level_screen_manager)
def load(self, sprite_manager: SpriteManager, spritesheet_manager: SpritesheetManager, level: Level): def load(self, sprite_manager: SpriteManager, spritesheet_manager: SpritesheetManager, level: Level):
if self.tile['name'] in BLOCK_TYPES: if self.tile['name'] in BLOCK_TYPES:

View File

@ -1,6 +1,7 @@
from level.Level import Level from level.Level import Level
from level.elements.static.StaticLevelElement import StaticLevelElement from level.elements.static.StaticLevelElement import StaticLevelElement
from physics.SpriteManager import SpriteManager from physics.SpriteManager import SpriteManager
from physics.TickData import TickData
from sprite.SpritesheetManager import SpritesheetManager from sprite.SpritesheetManager import SpritesheetManager
BLOCK_TYPES = { BLOCK_TYPES = {
@ -12,12 +13,18 @@ BLOCK_TYPES = {
class SpikeLevelElement(StaticLevelElement): class SpikeLevelElement(StaticLevelElement):
def __init__(self, tile: dict, loaded_level): def __init__(self, tile: dict, loaded_level, level_screen_manager: 'LevelScreenManager'):
super().__init__(tile, loaded_level) super().__init__(tile, loaded_level, level_screen_manager)
def load(self, sprite_manager: SpriteManager, spritesheet_manager: SpritesheetManager, level: Level): def load(self, sprite_manager: SpriteManager, spritesheet_manager: SpritesheetManager, level: Level):
block_type = BLOCK_TYPES[self.tile['name']] block_type = BLOCK_TYPES[self.tile['name']]
self.spritesheet = spritesheet_manager.get_sheet(block_type) self.spritesheet = spritesheet_manager.get_sheet(block_type)
self.set_animation_state('1') self.set_animation_state('1')
self.position_scale.position = self.tile['position'] self.position_scale.position = self.tile['position']
def tick(self, tick_data: TickData):
super().tick(tick_data)
for collision in self.get_collides_with():
if collision.secondary_sprite.id == 'player':
self.level_screen_manager.player_death()

View File

@ -5,6 +5,6 @@ from sprite.StaticSprite import StaticSprite
class StaticLevelElement(StaticSprite, LevelElement, ABC): class StaticLevelElement(StaticSprite, LevelElement, ABC):
def __init__(self, tile: dict, loaded_level): def __init__(self, tile: dict, loaded_level, level_screen_manager: 'LevelScreenManager'):
StaticSprite.__init__(self) StaticSprite.__init__(self)
LevelElement.__init__(self, tile, loaded_level) LevelElement.__init__(self, tile, loaded_level, level_screen_manager)

View File

@ -9,8 +9,8 @@ BLOCK_TYPES = {
class VacuumCleanerElement(StaticLevelElement): class VacuumCleanerElement(StaticLevelElement):
def __init__(self, tile: dict, loaded_level): def __init__(self, tile: dict, loaded_level, level_screen_manager: 'LevelScreenManager'):
super().__init__(tile, loaded_level) super().__init__(tile, loaded_level, level_screen_manager)
def load(self, sprite_manager: SpriteManager, spritesheet_manager: SpritesheetManager, level: Level): def load(self, sprite_manager: SpriteManager, spritesheet_manager: SpritesheetManager, level: Level):
block_type = BLOCK_TYPES[self.tile['name']] block_type = BLOCK_TYPES[self.tile['name']]

View File

@ -16,6 +16,7 @@ class LevelScreenManager(ScreenManager):
self.level = level self.level = level
self.loaded_level = LoadedLevel(sprite_manager, spritesheet_manager) self.loaded_level = LoadedLevel(sprite_manager, spritesheet_manager)
self.loaded_level.level_screen_manager = self
self.loaded_level.load_level(self.level) self.loaded_level.load_level(self.level)
def initialize(self): def initialize(self):
@ -28,3 +29,6 @@ class LevelScreenManager(ScreenManager):
def tick(self, tick_data: TickData): def tick(self, tick_data: TickData):
if tick_data.key_manager.is_keymap_down(KeyManager.KEY_ESCAPE): if tick_data.key_manager.is_keymap_down(KeyManager.KEY_ESCAPE):
self.main_loop.select_level_selection(self.level.theme) self.main_loop.select_level_selection(self.level.theme)
def player_death(self):
self.main_loop.player_death(self.level)

View File

@ -98,6 +98,9 @@ class MainLoop:
if self.screen_manager is not None and isinstance(self.screen_manager, LevelSelectionScreenManager): if self.screen_manager is not None and isinstance(self.screen_manager, LevelSelectionScreenManager):
self.screen_manager.select_theme(theme) self.screen_manager.select_theme(theme)
def player_death(self, level: Level):
self.select_level(level)
def select_main_menu(self): def select_main_menu(self):
self.set_game_state(self.GAME_STATE_MENU) self.set_game_state(self.GAME_STATE_MENU)