From 8dce8f967825e6aed3ba765e5d11d7d90b76b8f5 Mon Sep 17 00:00:00 2001 From: Fenysha <126053988+TeshariEnjoer@users.noreply.github.com> Date: Fri, 9 Aug 2024 06:48:58 +0300 Subject: [PATCH] Abstract map continue --- code/__DEFINES/laplas_defines/abstract_map.dm | 2 +- .../abstract_overmap/SSabstract_overmap.dm | 3 +- laplas/tools/abstract_map/camera.py | 40 +++++++++++++++ laplas/tools/abstract_map/main.py | 14 ++++-- laplas/tools/abstract_map/objects/object.py | 48 ++++++++++++++++++ laplas/tools/abstract_map/overmap.py | 50 +++++++++++-------- 6 files changed, 131 insertions(+), 26 deletions(-) create mode 100644 laplas/tools/abstract_map/camera.py create mode 100644 laplas/tools/abstract_map/objects/object.py diff --git a/code/__DEFINES/laplas_defines/abstract_map.dm b/code/__DEFINES/laplas_defines/abstract_map.dm index 0311764..65f3a04 100644 --- a/code/__DEFINES/laplas_defines/abstract_map.dm +++ b/code/__DEFINES/laplas_defines/abstract_map.dm @@ -1,7 +1,7 @@ #define MAP_SERVER_STATUS_DISABLED 0 #define MAP_SERVER_STATUS_BOOTING 1 #define MAP_SERVER_STATUS_INGAME 2 -#define DEFAULT_ABSTRACT_MAP_URL "http://localhost:5000/" +#define DEFAULT_ABSTRACT_MAP_URL "http://127.0.0.1:5000/" // diff --git a/laplas/code/modules/abstract_overmap/SSabstract_overmap.dm b/laplas/code/modules/abstract_overmap/SSabstract_overmap.dm index a960991..f39e237 100644 --- a/laplas/code/modules/abstract_overmap/SSabstract_overmap.dm +++ b/laplas/code/modules/abstract_overmap/SSabstract_overmap.dm @@ -35,6 +35,7 @@ SUBSYSTEM_DEF(abstract_overmap) CRASH("[name] failed to install new secure key!") init_map() + /datum/controller/subsystem/abstract_overmap/proc/init_map() . = "" . += "key = [http_key]," @@ -43,10 +44,10 @@ SUBSYSTEM_DEF(abstract_overmap) . += "generation_type [generation_type]," . += "max_planets [max_planets]," . += "max_outposts [max_outposts]," - var/response = ABSTRACT_MAP_REQUEST(ABSTRACT_MAP_INIT, args) + /datum/controller/subsystem/abstract_overmap/proc/reset_key() var/response = ABSTRACT_MAP_REQUEST(ABSTRACT_MAP_RESET_KEY, http_key) if(response == AM_RESPONSE_SUCESS) diff --git a/laplas/tools/abstract_map/camera.py b/laplas/tools/abstract_map/camera.py new file mode 100644 index 0000000..f285afb --- /dev/null +++ b/laplas/tools/abstract_map/camera.py @@ -0,0 +1,40 @@ +import pygame + +class camera: + def __init__(self, screen, surface, new_x, new_y) -> None: + self.width = 800 + self.height = 600 + self.dragging = False + self.last_mouse_x = 0 + self.last_mouse_y = 0 + + self.x = new_x + self.y = new_y + + self.screen = screen + self.render_surface = surface + self.camera_rect = pygame.Rect(self.x, self.y, self.width, self.height) + + def process(self): + for event in pygame.event.get(): + if(event.type == pygame.MOUSEBUTTONDOWN): + if(event.button == 1): + self.dragging = True + self.last_mouse_x, self.last_mouse_y = event.pos + + if(event.type == pygame.MOUSEBUTTONUP): + if(event.button == 1): # Левая кнопка мыши + self.dragging = False + + if(event.type == pygame.MOUSEMOTION): + if(self.dragging): + mouse_x, mouse_y = event.pos + dx = self.last_mouse_x - mouse_x + dy = self.last_mouse_y - mouse_y + self.x += dx + self.y += dy + self.last_mouse_x, self.last_mouse_y = self.x, self.y + + self.camera_rect.topleft = (self.x, self.y) + self.screen.blit(self.render_surface, (0, 0), self.camera_rect) + pygame.display.flip() diff --git a/laplas/tools/abstract_map/main.py b/laplas/tools/abstract_map/main.py index df7b822..f669d67 100644 --- a/laplas/tools/abstract_map/main.py +++ b/laplas/tools/abstract_map/main.py @@ -12,6 +12,10 @@ acess_key = None map = None +process = False + +if __name__ == '__main__': + app.run(debug=True, port= 5000) def check_acess(key): if(key == acess_key): @@ -29,8 +33,9 @@ def unpack_data(args): return result # Sets up a new acess key for map args = new_key -@app.route('/set_key', methods=['GET']) +@app.route('/set_key', methods=['GET', 'POST']) def set_key(args): + print(f"Request method: {args}") if(acess_key): return "ABSTRACT MAP ERROR: ATTEMPT REPLACE EXISTED ACESS KEY" @@ -69,8 +74,11 @@ def init_map(args): if(not check_acess(key)): return ACESS_DANIED + global map map = overmap(map_size_x, map_size_x) + global process + process = True @app.route('/create_obj', methods=['GET']) def create_obj(id: str, args): @@ -80,5 +88,5 @@ def create_obj(id: str, args): def move_obj(id, new_position): pass -if __name__ == '__main__': - app.run(debug=True) +while(process): + map.process() diff --git a/laplas/tools/abstract_map/objects/object.py b/laplas/tools/abstract_map/objects/object.py new file mode 100644 index 0000000..6c78866 --- /dev/null +++ b/laplas/tools/abstract_map/objects/object.py @@ -0,0 +1,48 @@ +import pygame + +class object: + DM_datum = "datum/overmap" + + def __init__(self, x, y, width, height, icon = ""): + self.iconpath = self.texture_path + icon + # Draw parametrs + self.original_texute = pygame.image.load(self.iconpath).convert_alpha() + self.texute = self.original_texute + self.rect = self.image.get_rect(center=(x, y)) + self.rect.width = width + self.rect.height = height + + # Physical parametr + + # Our current velocity + self.velocity = pygame.Vector2(0, 0) + # A temporary acceleration that we get + self.acceleration = pygame.Vector2(0, 0) + + self.angle = 0 + self.rotation_speed = 0 + + + # Applied new temporary acceleration + def apply_force(self, force: pygame.Vector2): + self.acceleration += force + + def apply_thrust(self, thrust): + direction = pygame.Vector2(1, 0).rotate(self.angle) + self.apply_force(direction * thrust) + + def apply_brake(self, brake_force): + direction = pygame.Vector2(1, 0).rotate(self.angle) + self.apply_force(-direction * brake_force) + + def update(self): + self.velocity += self.acceleration + self.rect.center += pygame.Vector2(self.velocity.x, self.velocity.y) + self.acceleration = pygame.Vector2(0, 0) + + self.angle %= 360 + self.image = pygame.transform.rotate(self.original_texute, -self.angle) + self.rect = self.image.get_rect(center=self.rect.center) + + def draw(self, screen): + pygame.draw.rect(screen, self.rect) diff --git a/laplas/tools/abstract_map/overmap.py b/laplas/tools/abstract_map/overmap.py index 70f3553..0eede1e 100644 --- a/laplas/tools/abstract_map/overmap.py +++ b/laplas/tools/abstract_map/overmap.py @@ -1,48 +1,56 @@ import pygame +from objects import object +import camera +camera_width, camera_height = 800, 600 class overmap: - process = False + pygame.init() def __init__(self, size_x: int, size_y: int, visual: bool) -> None: + # A map full size self.size_x = size_x self.size_y = size_y + + self.all_object = list() # Format "id" = obj - self.all_object = dict() self.all_ships = dict() + # Format "id" = obj self.all_planets = dict() - - if(visual): - self.init_window() - + self.screen = pygame.display.set_mode((camera_width, camera_height)) + self.create_map() + self.create_camera() + pygame.display.set_caption("Large Map with Coordinate System") ## Overmap functions - def init_window(): - pygame.init() + # Actually creates a non physical map, ans setups a cordinates system + def create_map(self): + self.map_holder = pygame.Surface((self.size_x, self.size_y)) + self.level_surface.fill((25, 25, 25)) - width, height = 800, 600 - screen = pygame.Surface((width, height)) - screen.fill(1, 1, 1) - pygame.draw.rect(screen, (0, 0, 0), pygame.Rect(100, 100, 200, 200), 1) - pygame.draw.circle(screen, (255, 0, 0), (400, 300), 50) - - def create_camera(): - pass + def create_camera(self): + camera_x, camera_y = 0, 0 + self.camera = camera(self.map_holder, self.screen, camera_x, camera_y) ## Function for manipulate with objects - def create_object(): + def create_object(self, obj, args): pass - def remove_object(): + def remove_object(self): pass - def adjust_speed(): + def adjust_speed(self): pass - while process: - pass + def process(self): + self.camera.process() + + if(not self.all_object.__len__()): + return + for obj in self.all_object: + obj.process()