sep-pm-platformer/project/physics/SpriteManager.py

47 lines
1.4 KiB
Python

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()
self.physics_handler.add_collision_callback(self.collision_detected)
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):
self.physics_handler.tick(dt, self.layers)
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)
def collision_detected(self, sprite_a: Sprite, sprite_b: Sprite):
# print(f"Collision detected between {sprite_a} and {sprite_b}")
pass