2023-03-26 09:51:11 +02:00
|
|
|
from pygame import Surface
|
|
|
|
|
|
|
|
from physics.PhysicsElementsHandler import PhysicsElementsHandler
|
|
|
|
from sprite.PositionScale import PositionScale
|
|
|
|
from sprite.Sprite import Sprite
|
|
|
|
from ui_elements.UiElement import UiElement
|
|
|
|
|
|
|
|
|
|
|
|
class DrawLayers:
|
|
|
|
BACKGROUND = 'background'
|
|
|
|
LEVEL = 'level'
|
|
|
|
OBJECTS = 'objects'
|
|
|
|
PLAYER = 'player'
|
|
|
|
UI = 'ui'
|
|
|
|
|
|
|
|
DRAW_ORDER = [BACKGROUND, LEVEL, OBJECTS, PLAYER, UI]
|
|
|
|
|
|
|
|
|
|
|
|
class SpriteManager:
|
|
|
|
def __init__(self):
|
|
|
|
self.physics_handler = PhysicsElementsHandler()
|
2023-03-26 10:40:17 +02:00
|
|
|
self.physics_handler.add_collision_callback(self.collision_detected)
|
2023-03-26 09:51:11 +02:00
|
|
|
|
|
|
|
self.layers: dict[str, list[UiElement]] = {}
|
|
|
|
for layer in DrawLayers.DRAW_ORDER:
|
|
|
|
self.layers[layer] = []
|
|
|
|
|
|
|
|
def add_sprite(self, layer: str, sprite: UiElement):
|
|
|
|
self.layers[layer].append(sprite)
|
|
|
|
|
|
|
|
def remove_sprite(self, sprite: UiElement):
|
|
|
|
for layer in self.layers:
|
|
|
|
if sprite in self.layers[layer]:
|
|
|
|
self.layers[layer].remove(sprite)
|
|
|
|
|
|
|
|
def tick(self, dt: float):
|
2023-03-26 10:05:23 +02:00
|
|
|
self.physics_handler.tick(dt, self.layers)
|
2023-03-26 09:51:11 +02:00
|
|
|
|
|
|
|
def draw(self, screen: Surface, screen_transform: PositionScale):
|
|
|
|
for layer in DrawLayers.DRAW_ORDER:
|
|
|
|
for sprite in self.layers[layer]:
|
|
|
|
sprite.draw(screen, screen_transform)
|
2023-03-26 10:40:17 +02:00
|
|
|
|
|
|
|
def collision_detected(self, sprite_a: Sprite, sprite_b: Sprite):
|
|
|
|
# print(f"Collision detected between {sprite_a} and {sprite_b}")
|
|
|
|
pass
|