From bcebb0b6253f4d24699ce7025fe1e25e4de6538d Mon Sep 17 00:00:00 2001 From: ThisBirchWood Date: Sat, 15 Mar 2025 22:32:26 +0100 Subject: [PATCH] ADD: Map coordinate controller, and image pixel controller --- image_coord_controller.py | 61 +++++++++++++++++++++++++++++++++++++++ map_coord_controller.py | 31 ++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 image_coord_controller.py create mode 100644 map_coord_controller.py diff --git a/image_coord_controller.py b/image_coord_controller.py new file mode 100644 index 0000000..a96d0fd --- /dev/null +++ b/image_coord_controller.py @@ -0,0 +1,61 @@ +from utils import mapped_value +import math + +class ImageCoordController: + def __init__(self, image_width, image_height, screen_width, screen_height, ingame_zero_x, ingame_zero_y): + self.image_width = image_width + self.image_height = image_height + self.ingame_zero_x = ingame_zero_x + self.ingame_zero_y = ingame_zero_y + + self.screen_width = screen_width + self.screen_height = screen_height + + self.screen_middle_x = screen_width / 2 + self.screen_middle_y = screen_height / 2 + + self.scaler = 1.0 + self.rotation_degrees = 0 + + def scale(self, scaler): + self.scaler = scaler + + def rotate(self, degree): + ## validate for multiples of 90 degrees and convert to 0-360 + if degree % 90 != 0: + raise ValueError("Degree v must be a multiple of 90") + self.rotation_degrees = degree % 360 + + def top_left_screen(self): + if self.rotation_degrees == 0: + x = self.screen_middle_x - (self.ingame_zero_x * self.scaler) + y = self.screen_middle_y - (self.ingame_zero_y * self.scaler) + elif self.rotation_degrees == 270: + x = self.screen_middle_x - ((self.image_width - self.ingame_zero_y) * self.scaler) + y = self.screen_middle_y - (self.ingame_zero_x * self.scaler) + elif self.rotation_degrees == 180: + x = self.screen_middle_x - ((self.image_width - self.ingame_zero_x) * self.scaler) + y = self.screen_middle_y - ((self.image_height - self.ingame_zero_y) * self.scaler) + elif self.rotation_degrees == 90: + x = self.screen_middle_x - (self.ingame_zero_y * self.scaler) + y = self.screen_middle_y - ((self.image_height - self.ingame_zero_x) * self.scaler) + return x, y + + + + + + +''' +ingame zero maps to 0,0,0 in game, and needs to map to middle of pygame window + +''' +if __name__ == "__main__": + image_coord_controller = ImageCoordController(1024, 1024, 640, 360) + image_coord_controller.scale(4) + print(image_coord_controller.image_to_screen(0,0)) + + + + + diff --git a/map_coord_controller.py b/map_coord_controller.py new file mode 100644 index 0000000..0298188 --- /dev/null +++ b/map_coord_controller.py @@ -0,0 +1,31 @@ +# Description: This file contains the CoordinateManager class which is responsible for converting coordinates to pixels and vice versa. + +import math +from utils import mapped_value + +class MapCoordController: + def __init__(self, screen_width: int, screen_height: int, map_min_x: int, map_max_x, map_min_y, map_max_y): + 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 + + def screen_to_map(self, x, y): + mapped_x = mapped_value(x, 0, self.screen_width, self.map_min_x, self.map_max_x) + mapped_y = mapped_value(y, 0, self.screen_height, self.map_min_y, self.map_max_y) + return mapped_x, mapped_y + + def map_to_screen(self, x, y): + mapped_x = mapped_value(x, self.map_min_x, self.map_max_x, 0, self.screen_width) + mapped_y = mapped_value(y, self.map_min_y, self.map_max_y, 0, self.screen_height) + return mapped_x, mapped_y + +if __name__ == "__main__": + map_coord_controller = MapCoordController(700, 700, -3000, 3000, -3000, 3000) + print(map_coord_controller.map_to_screen(0,0)) + + \ No newline at end of file