add typing with custom types

This commit is contained in:
Acereca 2022-06-06 14:49:15 +02:00
parent 390ba86c64
commit c56fc0e873
7 changed files with 144 additions and 18 deletions

17
Math.py Normal file
View File

@ -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)

View File

@ -1,16 +1,23 @@
import pygame import pygame
import numpy as np import numpy as np
import Math
import Types
tile_size = 64 # MAT_CART = 1 / (0.5 * 0.25 + 0.5 * 0.25) * np.array([[0.25, -0.25], [0.5, 0.5]])
MAT_ISO = np.array([[0.5, 0.25], [-0.5, 0.25]])
class MapTile(pygame.sprite.Sprite): class MapTile(pygame.sprite.Sprite):
size = 64
image = pygame.image.load("tileset/placeholder.png")
def __init__(self, x: int, y: int): def __init__(self, x: int, y: int):
super(MapTile, self).__init__() super(MapTile, self).__init__()
self.image = pygame.image.load("tileset/placeholder.png") self.image = MapTile.image
self.rect = self.image.get_rect() # self.rect = self.image.get_rect()
self.rect.center = (x, y) self.rect = pygame.Rect(
x - MapTile.size / 2, y - MapTile.size / 2, MapTile.size, MapTile.size
)
# self.rect.center = (x, y)
class IsoMapTile(MapTile): class IsoMapTile(MapTile):
@ -19,13 +26,34 @@ class IsoMapTile(MapTile):
u: int, u: int,
v: int, v: int,
elevation: int = 0, 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 # vec = np.array([u, v]) @ MAT_ISO * tile_size + origin
super(IsoMapTile, self).__init__(vec[0], vec[1] + tile_size / 8 * elevation) 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.u = u
self.v = v self.v = v
self.elevation = elevation self.elevation = elevation
def update(self, dx: int, dy: int): def update(self, dx: int, dy: int):
self.rect.move_ip(-dx, -dy) 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

7
Types.py Normal file
View File

@ -0,0 +1,7 @@
import typing
import numpy as np
import numpy.typing as npt
IsoCoord = npt.NDArray[np.int32]
CartCoors = npt.NDArray

74
main.py
View File

@ -1,12 +1,14 @@
import pygame import pygame
import pygame_gui as pgg import pygame_gui as pgg
import numpy as np import numpy as np
import Tiles import Tiles, Math
import logging as log import logging as log
screen_size = np.array([16 / 9 / 2, 1]) * 1440 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"): 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): for u, v, e, t in zip(us, vs, es, ts):
tile = getattr(Tiles, t)(u, v, e, origin) tile = getattr(Tiles, t)(u, v, e, origin)
map_tiles.add(tile) map_tiles.add(tile)
map_tiles_by_uv[(int(u), int(v))] = tile
print(f"loaded {len(map_tiles)} tiles") 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(): def main():
global origin
pygame.init() pygame.init()
# pygame.font.init() # pygame.font.init()
# font = pygame.font.SysFont("Raleway", 12) # font = pygame.font.SysFont("Raleway", 12)
@ -51,6 +55,7 @@ def main():
clock = pygame.time.Clock() clock = pygame.time.Clock()
gui_manager = pgg.UIManager(screen_size) gui_manager = pgg.UIManager(screen_size)
gui_manager.get_theme().load_theme("theme.json")
save_btn = pgg.elements.UIButton( save_btn = pgg.elements.UIButton(
relative_rect=pygame.Rect((0, 0), (100, 20)), relative_rect=pygame.Rect((0, 0), (100, 20)),
text="Save", text="Save",
@ -61,11 +66,41 @@ def main():
text="Load", text="Load",
manager=gui_manager, 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_text = font.render("test", True, (0, 0, 0))
map_tiles = pygame.sprite.RenderPlain() 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 xix in range(10):
# for yix in range(10): # for yix in range(10):
# tile = Tiles.IsoMapTile(xix, yix, 0, origin) # tile = Tiles.IsoMapTile(xix, yix, 0, origin)
@ -74,16 +109,35 @@ def main():
gameit = 0 gameit = 0
is_running = True is_running = True
while is_running: 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 # events
# mouse_x, mouse_y = pygame.mouse.get_pos()
# if pygame.mouse.get_pressed()[1]: ## mouse
# map_tiles.update(mouse_prev_x - mouse_x, mouse_prev_y - mouse_y) mouse_x, mouse_y = pygame.mouse.get_pos()
# mouse_prev_x = mouse_x if pygame.mouse.get_pressed()[1]:
# mouse_prev_y = mouse_y 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(): for evt in pygame.event.get():
if evt.type == pygame.QUIT: if evt.type == pygame.QUIT:
is_running = False is_running = False
@ -103,7 +157,7 @@ def main():
# Drawing # Drawing
screen.blit(bg, (0, 0)) screen.blit(bg, (0, 0))
map_tiles.draw(screen) map_tiles.draw(screen)
overlay_tiles.draw(screen)
gui_manager.draw_ui(screen) gui_manager.draw_ui(screen)
pygame.display.flip() pygame.display.flip()

BIN
map.npz Normal file

Binary file not shown.

20
theme.json Normal file
View File

@ -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"
}
}
}

BIN
tileset/cursor.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 344 B