From c56fc0e873e1465c3000034f57269736b74a7cdd Mon Sep 17 00:00:00 2001 From: Acereca Date: Mon, 6 Jun 2022 14:49:15 +0200 Subject: [PATCH] add typing with custom types --- Math.py | 17 +++++++++++ Tiles.py | 44 ++++++++++++++++++++++----- Types.py | 7 +++++ main.py | 74 +++++++++++++++++++++++++++++++++++++++------ map.npz | Bin 0 -> 16166 bytes theme.json | 20 ++++++++++++ tileset/cursor.png | Bin 0 -> 344 bytes 7 files changed, 144 insertions(+), 18 deletions(-) create mode 100644 Math.py create mode 100644 Types.py create mode 100644 map.npz create mode 100644 theme.json create mode 100644 tileset/cursor.png diff --git a/Math.py b/Math.py new file mode 100644 index 0000000..4ead43a --- /dev/null +++ b/Math.py @@ -0,0 +1,17 @@ +import numpy as np +import Types as t + +MAT_ISO = np.array([[0.5, 0.25], [-0.5, 0.25]]) +MAT_CART = np.linalg.inv(MAT_ISO) + + +def from_iso( + uv: t.IsoCoord, scale: float = 1, origin: t.CartCoors = np.array([0, 0]) +) -> t.CartCoors: + return (uv @ MAT_ISO) * scale + origin + + +def from_cart( + xy: t.CartCoors, scale: float = 1, origin: t.CartCoors = np.array([0, 0]) +) -> t.IsoCoord: + return (((xy - origin) / scale) @ MAT_CART + np.array([0.5, 0.5])).astype(np.int32) diff --git a/Tiles.py b/Tiles.py index 449920a..72dae75 100644 --- a/Tiles.py +++ b/Tiles.py @@ -1,16 +1,23 @@ import pygame import numpy as np +import Math +import Types -tile_size = 64 -MAT_ISO = np.array([[0.5, 0.25], [-0.5, 0.25]]) +# MAT_CART = 1 / (0.5 * 0.25 + 0.5 * 0.25) * np.array([[0.25, -0.25], [0.5, 0.5]]) class MapTile(pygame.sprite.Sprite): + size = 64 + image = pygame.image.load("tileset/placeholder.png") + def __init__(self, x: int, y: int): super(MapTile, self).__init__() - self.image = pygame.image.load("tileset/placeholder.png") - self.rect = self.image.get_rect() - self.rect.center = (x, y) + self.image = MapTile.image + # self.rect = self.image.get_rect() + self.rect = pygame.Rect( + x - MapTile.size / 2, y - MapTile.size / 2, MapTile.size, MapTile.size + ) + # self.rect.center = (x, y) class IsoMapTile(MapTile): @@ -19,13 +26,34 @@ class IsoMapTile(MapTile): u: int, v: int, elevation: int = 0, - origin: np.ndarray = np.array([100, 100]), + origin: np.ndarray = np.array([0, 0]), ): - vec = np.array([u, v]) @ MAT_ISO * tile_size + origin - super(IsoMapTile, self).__init__(vec[0], vec[1] + tile_size / 8 * elevation) + # vec = np.array([u, v]) @ MAT_ISO * tile_size + origin + vec = Math.from_iso(np.array([u, v], np.int32), MapTile.size, origin) + super(IsoMapTile, self).__init__(vec[0], vec[1] + MapTile.size / 8 * elevation) self.u = u self.v = v self.elevation = elevation def update(self, dx: int, dy: int): self.rect.move_ip(-dx, -dy) + + def move(self, uv: Types.IsoCoord, origin: Types.CartCoors): + vec = Math.from_iso(uv, MapTile.size, origin) + self.rect.center = (vec[0], vec[1] + MapTile.size / 8 * self.elevation) + + +class CursorTile(IsoMapTile): + image = pygame.image.load("tileset/cursor.png") + + def __init__( + self, + uv: Types.IsoCoord, + elevation: int = 0, + origin: Types.CartCoors = np.array([0, 0]), + ): + super(CursorTile, self).__init__( + u=uv[0], v=uv[1], elevation=elevation, origin=origin + ) + + self.image = CursorTile.image diff --git a/Types.py b/Types.py new file mode 100644 index 0000000..06fe88b --- /dev/null +++ b/Types.py @@ -0,0 +1,7 @@ +import typing +import numpy as np +import numpy.typing as npt + + +IsoCoord = npt.NDArray[np.int32] +CartCoors = npt.NDArray diff --git a/main.py b/main.py index 49f9b81..28f3d51 100755 --- a/main.py +++ b/main.py @@ -1,12 +1,14 @@ import pygame import pygame_gui as pgg import numpy as np -import Tiles +import Tiles, Math import logging as log screen_size = np.array([16 / 9 / 2, 1]) * 1440 -origin = np.array([screen_size[0] / 2, Tiles.tile_size]) +origin = np.array([screen_size[0] / 2, Tiles.MapTile.size], np.int32) + +map_tiles_by_uv = {} def load_map(map_file="map.npz"): @@ -21,6 +23,7 @@ def load_map(map_file="map.npz"): for u, v, e, t in zip(us, vs, es, ts): tile = getattr(Tiles, t)(u, v, e, origin) map_tiles.add(tile) + map_tiles_by_uv[(int(u), int(v))] = tile print(f"loaded {len(map_tiles)} tiles") @@ -42,6 +45,7 @@ def save_map(map_tiles: pygame.sprite.RenderPlain, map_file: str = "map.npz"): def main(): + global origin pygame.init() # pygame.font.init() # font = pygame.font.SysFont("Raleway", 12) @@ -51,6 +55,7 @@ def main(): clock = pygame.time.Clock() gui_manager = pgg.UIManager(screen_size) + gui_manager.get_theme().load_theme("theme.json") save_btn = pgg.elements.UIButton( relative_rect=pygame.Rect((0, 0), (100, 20)), text="Save", @@ -61,11 +66,41 @@ def main(): text="Load", manager=gui_manager, ) + bl_corner = pygame.Rect((0, 0), (150, 20)) + bl_corner.bottomleft = (0, 0) + location_label = pgg.elements.UILabel( + relative_rect=bl_corner, + text="", + manager=gui_manager, + anchors={ + "left": "left", + "right": "left", + "top": "bottom", + "bottom": "bottom", + }, + ) + br_corner = pygame.Rect((0, 0), (50, 20)) + br_corner.bottomright = (0, 0) + perf_label = pgg.elements.UILabel( + relative_rect=br_corner, + text="", + manager=gui_manager, + anchors={ + "left": "right", + "right": "right", + "top": "bottom", + "bottom": "bottom", + }, + ) - # mouse_prev_x, mouse_prev_y = pygame.mouse.get_pos() + mouse_prev_x, mouse_prev_y = pygame.mouse.get_pos() + uv_prev = np.array([0, 0], np.int32) # map_text = font.render("test", True, (0, 0, 0)) map_tiles = pygame.sprite.RenderPlain() + overlay_tiles = pygame.sprite.RenderPlain() + overlay_tile = Tiles.CursorTile(np.array([0, 0])) + overlay_tiles.add(overlay_tile) # for xix in range(10): # for yix in range(10): # tile = Tiles.IsoMapTile(xix, yix, 0, origin) @@ -74,16 +109,35 @@ def main(): gameit = 0 is_running = True while is_running: - time_delta = clock.tick(60) / 1000 + time_delta = clock.tick(30) / 1000 + perf_label.set_text(f"{1/time_delta:.0f}fps") # events - # mouse_x, mouse_y = pygame.mouse.get_pos() - # if pygame.mouse.get_pressed()[1]: - # map_tiles.update(mouse_prev_x - mouse_x, mouse_prev_y - mouse_y) + ## mouse + mouse_x, mouse_y = pygame.mouse.get_pos() - # mouse_prev_x = mouse_x - # mouse_prev_y = mouse_y + if pygame.mouse.get_pressed()[1]: + map_tiles.update(mouse_prev_x - mouse_x, mouse_prev_y - mouse_y) + origin = ( + origin[0] - mouse_prev_x + mouse_x, + origin[1] - mouse_prev_y + mouse_y, + ) + + uv: IsoCoord = Math.from_cart( + np.array([mouse_x, mouse_y]), Tiles.MapTile.size, origin + ) + + location_label.set_text(f"{uv[0]}x{uv[1]} / {mouse_x:.0f}x{mouse_y:.0f}") + + if np.all(uv_prev != uv): + overlay_tile.move(uv, origin) + uv_prev = uv + + mouse_prev_x = mouse_x + mouse_prev_y = mouse_y + + ## keys for evt in pygame.event.get(): if evt.type == pygame.QUIT: is_running = False @@ -103,7 +157,7 @@ def main(): # Drawing screen.blit(bg, (0, 0)) map_tiles.draw(screen) - + overlay_tiles.draw(screen) gui_manager.draw_ui(screen) pygame.display.flip() diff --git a/map.npz b/map.npz new file mode 100644 index 0000000000000000000000000000000000000000..332137dbfe15f6e673290347340752bc1104bfa0 GIT binary patch literal 16166 zcmeI3F-yZh6vtm|tx^#?bP!R9E>S5`>mnlBNsv;>UCUn)I}^qf2L`JzY90?dj6<(w;88 zDDCOernIL^uSk3U*LEzfz4zzYXk53)B(7B)syI^XUaeQP&eZx)_f_2wnpBBk7!E&oQGGo z3ZqHH(}z5lfThV&W^q_NmSdI+mO6{eGOULGhyStu5Px|7;Q5R958l7XKahVR|3v{KY~AkKY~AkKY~AkKY~AwmOt7hV+2cfdst6CI3`}5 z8Xfb-)#hQX>fo4oq{W8zgc(J?nOBx`K!(qu|pN%H5Fb#YIeeF9h` Bc}M^N literal 0 HcmV?d00001 diff --git a/theme.json b/theme.json new file mode 100644 index 0000000..f99e622 --- /dev/null +++ b/theme.json @@ -0,0 +1,20 @@ +{ + "label": { + "colours": { + "dark_bg": "#25292e", + "normal_text": "#c5cbd8", + "text_shadow": "#505050" + }, + "font": { + "name": "montserrat", + "size": "12", + "bold": "0", + "italic": "0" + }, + "misc": { + "text_shadow": "1", + "text_shadow_size": "1", + "text_shadow_offset": "0,0" + } + } +} diff --git a/tileset/cursor.png b/tileset/cursor.png new file mode 100644 index 0000000000000000000000000000000000000000..16173125b462469a08e18cfd3f2151f1cfa22813 GIT binary patch literal 344 zcmeAS@N?(olHy`uVBq!ia0vp^4j|0I1|(Ny7TyC=jKx9jP7LeL$-D%zg*;sxLn`LH zy?Ix#$w0#8Vgl=}Mb{EUuQliE%GGev8MSKyI_jBA=!&v1QXWe{EgMglt8bb*vdTPtOm}OnniVS+e%4MRu-Nqphh5gchZ$je j<^SAbUj_^m7)aQzFL>sp$>%=drywCuS3j3^P6