refactor: improve rendering process

This commit is contained in:
2025-10-18 15:10:13 +02:00
parent 6805e69509
commit 2b20582b87
7 changed files with 114 additions and 48 deletions

View File

@@ -1,13 +1,42 @@
import importlib
import pkgutil
from enum import StrEnum
from typing import Optional, Self
import pygame
import src.objects
from src.camera import Camera
class TrackObjectType(StrEnum):
Road = "road"
Unknown = "unknown"
class TrackObject:
def __init__(
self,
type: TrackObjectType,
) -> None:
self.type: TrackObjectType = type
REGISTRY = {}
type: TrackObjectType = TrackObjectType.Unknown
@staticmethod
def init():
package = src.objects
for _, modname, _ in pkgutil.walk_packages(
package.__path__, package.__name__ + "."
):
importlib.import_module(modname)
def __init_subclass__(cls, **kwargs) -> None:
super().__init_subclass__(**kwargs)
TrackObject.REGISTRY[cls.type] = cls
@classmethod
def load(cls, data: dict) -> Self:
obj_type: Optional[TrackObjectType] = data.get("type")
if obj_type not in cls.REGISTRY:
raise ValueError(f"Unknown object tyoe: {obj_type}")
return cls.REGISTRY[obj_type].load(data)
def render(self, surf: pygame.Surface, camera: Camera):
pass