added save/load funcs to tilemap

This commit is contained in:
StochasticMouse 2024-07-17 18:01:42 +02:00
parent 135ffef6af
commit e57ded9b8d
2 changed files with 18 additions and 4 deletions

View File

@ -38,7 +38,7 @@ class Game:
#'stone' : load_images('tiles/stone/') #'stone' : load_images('tiles/stone/')
'iso' : load_images('tiles/iso/') 'iso' : load_images('tiles/iso/')
} }
self.tilemap = Tilemap(self, (64,32), 'map_1.csv') self.tilemap = Tilemap(self, (64,32), 'map_2.csv')
self.camera = Camera(self) self.camera = Camera(self)
while self.status == 'iso_game': while self.status == 'iso_game':
delta = np.array([0.0,0.0]) delta = np.array([0.0,0.0])
@ -104,6 +104,10 @@ class Game:
delta += np.array([0,-5]) delta += np.array([0,-5])
if keys[pygame.K_DOWN]: if keys[pygame.K_DOWN]:
delta += np.array([0,5]) delta += np.array([0,5])
if keys[pygame.K_o]:
self.tilemap.save('map_2.csv')
if keys[pygame.K_l]:
self.tilemap.load_map('map_2.csv')
if mouse_butt[0]: #on left click if mouse_butt[0]: #on left click
click = True click = True
if mouse_butt[2]: #on right click if mouse_butt[2]: #on right click

View File

@ -10,7 +10,7 @@ class Tilemap:
def __init__(self, game, tile_size, map_path = None): def __init__(self, game, tile_size, map_path = None):
self.game = game self.game = game
self.tile_size = np.array(tile_size) self.tile_size = np.array(tile_size)
self.offgrid_tiles = [] #self.offgrid_tiles = []
if map_path: if map_path:
self.load_map(map_path) self.load_map(map_path)
@ -27,6 +27,16 @@ class Tilemap:
if np.any(self.tilemap.shape <= pos): if np.any(self.tilemap.shape <= pos):
self.extend_tilemap(pos) self.extend_tilemap(pos)
self.tilemap[*pos] = tile self.tilemap[*pos] = tile
def save(self, map_path):
inverse_tile_dict = {TILE_DICT[i]:i for i in range(len(TILE_DICT))}
tile_arr = np.empty_like(self.tilemap,dtype=np.int8)
for row in range(self.tilemap.shape[0]):
for col in range(self.tilemap.shape[1]):
tile = self.tilemap[row,col]
tile_arr[row,col] = inverse_tile_dict[(tile['type'], tile['variant'], tile['walkable'])]
tile_arr = np.transpose(tile_arr)
np.savetxt(BASE_MAP_PATH + map_path, tile_arr, delimiter=',', fmt='%.1d')
def load_map(self, map_path): def load_map(self, map_path):
#print(map_path) #print(map_path)
@ -41,8 +51,8 @@ class Tilemap:
for y in range(self.tilemap.shape[1]): for y in range(self.tilemap.shape[1]):
tile = self.tilemap[x,y] tile = self.tilemap[x,y]
surface.blit(self.game.assets[tile['type']][tile['variant']],(to_isometric_pixel(np.array((x,y)))*self.tile_size)-offset) surface.blit(self.game.assets[tile['type']][tile['variant']],(to_isometric_pixel(np.array((x,y)))*self.tile_size)-offset)
for tile in self.offgrid_tiles: #for tile in self.offgrid_tiles:
surface.blit(self.game.assets[tile['type']][tile['variant']],(to_isometric_pixel(tile['pos'])*self.tile_size)-offset) #this tile is written offgrid so no mult by tile_size # surface.blit(self.game.assets[tile['type']][tile['variant']],(to_isometric_pixel(tile['pos'])*self.tile_size)-offset) #this tile is written offgrid so no mult by tile_size
def tiles_around(self,pos): def tiles_around(self,pos):
tiles = [] tiles = []
tile_loc = np.array(pos // np.array((self.tile_size[0],self.tile_size[0])),dtype=int) #gotta use this and not just self.tile_size to normalize wrt the change of variables for the isometric view tile_loc = np.array(pos // np.array((self.tile_size[0],self.tile_size[0])),dtype=int) #gotta use this and not just self.tile_size to normalize wrt the change of variables for the isometric view