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

42 lines
1.2 KiB
Python
Raw Normal View History

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()
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)
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)