from typing import Optional from pygame import Surface from physics.PhysicsElementsHandler import PhysicsElementsHandler from physics.TickData import TickData 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_ui_element(self, layer: str, sprite: UiElement): self.layers[layer].append(sprite) def remove_ui_element(self, sprite: UiElement): 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) 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 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 def collision_detected(self, sprite_a: Sprite, sprite_b: Sprite): pass 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