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

67 lines
2.1 KiB
Python
Raw Normal View History

from typing import Optional
2023-03-26 09:51:11 +02:00
from pygame import Surface
from physics.PhysicsElementsHandler import PhysicsElementsHandler
from physics.TickData import TickData
2023-03-26 09:51:11 +02:00
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_ui_element(self, layer: str, sprite: UiElement):
2023-03-26 09:51:11 +02:00
self.layers[layer].append(sprite)
def remove_ui_element(self, sprite: UiElement):
2023-03-26 09:51:11 +02:00
for layer in self.layers:
if sprite in self.layers[layer]:
self.layers[layer].remove(sprite)
def tick(self, tick_data: TickData):
self.physics_handler.tick(tick_data, 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 find_elements_at_position(self, position: tuple[float, float]) -> list[UiElement]:
elements = []
for layer in DrawLayers.DRAW_ORDER:
for sprite in self.layers[layer]:
bounding_box = sprite.get_bounding_box()
if bounding_box is not None and bounding_box.contains_point(position):
elements.append(sprite)
return elements
2023-03-26 10:40:17 +02:00
def collision_detected(self, sprite_a: Sprite, sprite_b: Sprite):
pass
2023-03-26 17:01:28 +02:00
def find_sprite_by_uuid(self, uuid: str) -> Optional[UiElement]:
for layer in DrawLayers.DRAW_ORDER:
for sprite in self.layers[layer]:
if sprite.uuid == uuid:
return sprite
return None