changed idea, an array is better due to isometric nature of game, added extend and add_tile to Tilemap, now can add/remove tiles via level_editor

This commit is contained in:
StochasticMouse 2024-07-17 12:24:04 +02:00
parent 47d54f43c3
commit 135ffef6af
12 changed files with 29 additions and 12 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 301 B

After

Width:  |  Height:  |  Size: 163 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 320 B

After

Width:  |  Height:  |  Size: 301 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 320 B

After

Width:  |  Height:  |  Size: 320 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 323 B

After

Width:  |  Height:  |  Size: 320 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 315 B

After

Width:  |  Height:  |  Size: 323 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 324 B

After

Width:  |  Height:  |  Size: 315 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 328 B

After

Width:  |  Height:  |  Size: 324 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 320 B

After

Width:  |  Height:  |  Size: 328 B

BIN
data/images/tiles/iso/8.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 320 B

View File

@ -116,8 +116,10 @@ class Game:
mpos = np.array(pygame.mouse.get_pos()) / RENDER_SCALE mpos = np.array(pygame.mouse.get_pos()) / RENDER_SCALE
pos = np.array(to_cartesian(mpos + self.camera.scroll,self.tilemap.tile_size),dtype=int) pos = np.array(to_cartesian(mpos + self.camera.scroll,self.tilemap.tile_size),dtype=int)
if click: if click and np.all(pos >= 0):
self.tilemap.tilemap[*pos] = {'type' : TILE_DICT[self.tile_sel][0], 'variant' : TILE_DICT[self.tile_sel][1], 'pos': pos, 'walkable': TILE_DICT[self.tile_sel][2]} self.tilemap.add_tile(pos, {'type' : TILE_DICT[self.tile_sel][0], 'variant' : TILE_DICT[self.tile_sel][1], 'walkable': TILE_DICT[self.tile_sel][2]})
if r_click and np.all(pos >= 0):
self.tilemap.add_tile(pos, {'type' : TILE_DICT[0][0], 'variant' : TILE_DICT[0][1], 'walkable': TILE_DICT[0][2]})
current_tile_img = self.assets[TILE_DICT[self.tile_sel][0]][TILE_DICT[self.tile_sel][1]].copy() current_tile_img = self.assets[TILE_DICT[self.tile_sel][0]][TILE_DICT[self.tile_sel][1]].copy()
current_tile_img.set_alpha(100) current_tile_img.set_alpha(100)

View File

@ -13,8 +13,23 @@ class Tilemap:
self.offgrid_tiles = [] self.offgrid_tiles = []
if map_path: if map_path:
self.load_map(map_path) self.load_map(map_path)
def extend_tilemap(self, pos):
#extend tilemap such that pos becomes an accessible tile
#by default adds the air block TILE_DICT[0]
if self.tilemap.shape[0] <= pos[0]:
self.tilemap = np.concatenate([self.tilemap, np.full(np.array((pos[0] - self.tilemap.shape[0] + 1, self.tilemap.shape[1])), {'type' : TILE_DICT[0][0], 'variant' : TILE_DICT[0][1], 'walkable': TILE_DICT[0][2]})], axis = 0)
if self.tilemap.shape[1] <= pos[1]:
self.tilemap = np.concatenate([self.tilemap, np.full(np.array((self.tilemap.shape[0], pos[1] - self.tilemap.shape[1] + 1)), {'type' : TILE_DICT[0][0], 'variant' : TILE_DICT[0][1], 'walkable': TILE_DICT[0][2]})], axis = 1)
def add_tile(self, pos, tile):
#by design if np.any(pos) < 0 it loops to the other side without errors
if np.any(self.tilemap.shape <= pos):
self.extend_tilemap(pos)
self.tilemap[*pos] = tile
def load_map(self, map_path): def load_map(self, map_path):
print(map_path) #print(map_path)
tile_arr = np.transpose(np.loadtxt(BASE_MAP_PATH + map_path, delimiter=',', dtype=np.int8)) tile_arr = np.transpose(np.loadtxt(BASE_MAP_PATH + map_path, delimiter=',', dtype=np.int8))
self.tilemap = np.empty_like(tile_arr, dtype = object) self.tilemap = np.empty_like(tile_arr, dtype = object)
for row in range(tile_arr.shape[0]): for row in range(tile_arr.shape[0]):
@ -25,7 +40,7 @@ class Tilemap:
for x in range(self.tilemap.shape[0]): for x in range(self.tilemap.shape[0]):
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(tile['pos'])*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):

View File

@ -2,13 +2,13 @@ import pygame
import os import os
import numpy as np import numpy as np
TILE_DICT = { TILE_DICT = {
0 : ('iso',6,False), 0 : ('iso',0,False),
1 : ('iso',0,True), 1 : ('iso',1,True),
2 : ('iso',1,False), 2 : ('iso',2,False),
3 : ('iso',2,False), 3 : ('iso',3,False),
4 : ('iso',3,False), 4 : ('iso',4,False),
5 : ('iso',4,False), 5 : ('iso',5,False),
6 : ('iso',5,False) 6 : ('iso',6,False)
} }
BASE_IMG_PATH = 'data/images/' BASE_IMG_PATH = 'data/images/'
def load_image(path): def load_image(path):