add loading button and functionality
This commit is contained in:
parent
14aad68cb7
commit
390ba86c64
50
main.py
50
main.py
|
@ -2,20 +2,43 @@ import pygame
|
||||||
import pygame_gui as pgg
|
import pygame_gui as pgg
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import Tiles
|
import Tiles
|
||||||
|
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.tile_size])
|
||||||
|
|
||||||
|
|
||||||
def load_map(map_file="map.dnd"):
|
def load_map(map_file="map.npz"):
|
||||||
for u, v, elevation, tile in lines:
|
print("started loading tiles")
|
||||||
getattr(Tiles, tile)(u, v, elevation)
|
data = np.load(map_file)
|
||||||
|
us = data["us"]
|
||||||
|
vs = data["vs"]
|
||||||
|
es = data["es"]
|
||||||
|
ts = data["ts"]
|
||||||
|
|
||||||
|
map_tiles = pygame.sprite.RenderPlain()
|
||||||
|
for u, v, e, t in zip(us, vs, es, ts):
|
||||||
|
tile = getattr(Tiles, t)(u, v, e, origin)
|
||||||
|
map_tiles.add(tile)
|
||||||
|
|
||||||
|
print(f"loaded {len(map_tiles)} tiles")
|
||||||
|
|
||||||
|
return map_tiles
|
||||||
|
|
||||||
|
|
||||||
def save_map(map_tiles: pygame.sprite.RenderPlain, map_file: str = "map.dnd"):
|
def save_map(map_tiles: pygame.sprite.RenderPlain, map_file: str = "map.npz"):
|
||||||
|
us = np.array([])
|
||||||
|
vs = np.array([])
|
||||||
|
es = np.array([])
|
||||||
|
ts = np.array([])
|
||||||
for tile in map_tiles.sprites():
|
for tile in map_tiles.sprites():
|
||||||
(tile.u, tile.v, tile.elevation, type(tile))
|
us = np.append(us, tile.u)
|
||||||
|
vs = np.append(vs, tile.v)
|
||||||
|
es = np.append(es, tile.elevation)
|
||||||
|
ts = np.append(ts, tile.__class__.__name__)
|
||||||
|
|
||||||
|
np.savez(file=map_file, us=us, vs=vs, es=es, ts=ts)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
@ -33,15 +56,20 @@ def main():
|
||||||
text="Save",
|
text="Save",
|
||||||
manager=gui_manager,
|
manager=gui_manager,
|
||||||
)
|
)
|
||||||
|
load_btn = pgg.elements.UIButton(
|
||||||
|
relative_rect=pygame.Rect((0, 20), (100, 20)),
|
||||||
|
text="Load",
|
||||||
|
manager=gui_manager,
|
||||||
|
)
|
||||||
|
|
||||||
# mouse_prev_x, mouse_prev_y = pygame.mouse.get_pos()
|
# mouse_prev_x, mouse_prev_y = pygame.mouse.get_pos()
|
||||||
|
|
||||||
# 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()
|
||||||
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)
|
||||||
map_tiles.add(tile)
|
# map_tiles.add(tile)
|
||||||
|
|
||||||
gameit = 0
|
gameit = 0
|
||||||
is_running = True
|
is_running = True
|
||||||
|
@ -66,7 +94,9 @@ def main():
|
||||||
if evt.type == pgg.UI_BUTTON_PRESSED:
|
if evt.type == pgg.UI_BUTTON_PRESSED:
|
||||||
if evt.ui_element == save_btn:
|
if evt.ui_element == save_btn:
|
||||||
save_map(map_tiles)
|
save_map(map_tiles)
|
||||||
gui_manager.process_events(evt)
|
if evt.ui_element == load_btn:
|
||||||
|
map_tiles = load_map()
|
||||||
|
gui_manager.process_events(evt)
|
||||||
|
|
||||||
gui_manager.update(time_delta)
|
gui_manager.update(time_delta)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue