refactor: improve rendering process
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user