REFACTOR to be more modular

This commit is contained in:
2025-04-20 16:43:55 +02:00
parent 00274e429d
commit c170c4c049
6 changed files with 100 additions and 84 deletions

View File

@@ -1,18 +1,39 @@
# Description: This file contains the CoordinateManager class which is responsible for converting coordinates to pixels and vice versa.
import math
import pygame
from utils.utils import mapped_value
from utils.json_object import JSONObject
class MapCoordController:
def __init__(self, screen_width: int, screen_height: int, map_min_x: int, map_max_x, map_min_y, map_max_y):
def __init__(self, screen_width: int, screen_height, json_file: str, map_image_path: str):
self.screen_width = screen_width
self.screen_height = screen_height
self.map_min_x = map_min_x
self.map_max_x = map_max_x
self.map_min_y = map_min_y
self.map_max_y = map_max_y
self.map_width = map_max_x - map_min_x
self.map_height = map_max_y - map_min_y
# too much for one class, refactor
self.map_image = pygame.image.load(map_image_path)
self.image_width = self.map_image.get_width()
self.image_height = self.map_image.get_height()
# Load map data from json file
self.json_object = self._load_json(json_file)
# Load map data from json object
self.map_min_x = self.json_object.get("pos_x")
self.map_min_y = self.json_object.get("pos_y")
self.scale = self.json_object.get("scale")
self.map_max_x = self.map_min_x + (self.image_width * self.scale)
self.map_max_y = self.map_min_y - (self.image_height * self.scale)
self.map_width = self.map_max_x - self.map_min_x
self.map_height = self.map_max_y - self.map_min_y
def _load_json(self, path: str):
try:
return JSONObject(path)
except FileNotFoundError:
raise NotImplementedError(f"Map not implemented.")
def update_screen_size(self, screen_width: int, screen_height: int):
self.screen_width = screen_width