add typing with custom types
This commit is contained in:
parent
390ba86c64
commit
c56fc0e873
|
@ -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)
|
44
Tiles.py
44
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
|
||||
|
|
|
@ -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
74
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()
|
||||
|
||||
|
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
}
|
Binary file not shown.
After Width: | Height: | Size: 344 B |
Loading…
Reference in New Issue