REFACTOR: Split into modules (controller, models, and utils)
This commit is contained in:
0
controllers/__init__.py
Normal file
0
controllers/__init__.py
Normal file
61
controllers/image_coord_controller.py
Normal file
61
controllers/image_coord_controller.py
Normal file
@@ -0,0 +1,61 @@
|
||||
from utils.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))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
31
controllers/map_coord_controller.py
Normal file
31
controllers/map_coord_controller.py
Normal file
@@ -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.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))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user