Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def check_tiles(self):
- """March tiles together"""
- #Loop through chunks
- for c, chunko in enumerate(self.chunks):
- #Loop through tiles in chunk
- for y, row in enumerate(chunko.chunk):
- for x, tile in enumerate(row):
- self.check_tile(x, y, c)
- chunko.draw(self.rendersurface)
- def check_tile(self, x, y, chunkindex):
- """Checks for tiles"""
- #Get the current chunk to make changes to
- currentchunk = self.chunks[chunkindex]
- if currentchunk.chunk[y][x].tag == "grass":
- #Get the tiles neighbors
- tile_neighbors = self.get_neighbors(x, y, chunkindex)
- #Values for every neighbor tile, listed by indexes
- neighbor_values = {0: 1, 1: 8, 2: 2, 3: 4}
- #Setup a mask value for the grass sprite
- maskindex = 0
- for neighbor_index, neighbor_tile in enumerate(tile_neighbors):
- if neighbor_tile.tag in ("dirt", "grass"):
- maskindex += neighbor_values[neighbor_index]
- #Add the mask to the tile
- if maskindex:
- currentchunk.chunk[y][x].mask = [self.grass[maskindex]]
- currentchunk.chunk[y][x].draw(currentchunk.surface)
- def get_neighbors(self, xpos, ypos, chunkindex):
- """Gets E, W, S and N neighbors of a tile in a specified chunk"""
- #Gets chunks that the neighbors can theoriticaly be
- chunks = self.get_neighbor_chunks(chunkindex)
- #Loop thorugh a 3x3 square around the tile being checked
- for yy in range(ypos - 1, ypos + 2):
- for xx in range(xpos - 1, xpos + 2):
- #Skip the corner coordinates
- if yy != ypos and xx != xpos:
- continue
- #Skip also the tile beeing checked
- if yy == ypos and xx == xpos:
- continue
- #Get a chunk index and a x index for neighbor tile
- chunkindex, xindex = self.get_correct_chunk_index(xx)
- yield chunks[chunkindex].chunk[yy][xindex]
- def get_correct_chunk_index(self, xpos):
- """Gets a correct chunk to take the tile from for neighbors
- Returns (chunkindex, correct_x_index)"""
- #If the xpos is to the left chunk
- if xpos < 0:
- return (0, -1)
- #If the xpos is to the right chunk
- if xpos >= len(self.chunks[0].chunk[0]):
- return (2, 0)
- #If the xpos doesn't cross chunk borders
- return (1, xpos)
- def get_neighbor_chunks(self, chunkindex):
- """Get neighbor chunks for a chunk at chunkindex"""
- #Create a empty set of chunks
- chunks = [0, 0, 0]
- #Set the middlechunk to current chunk
- chunks[1] = self.chunks[chunkindex]
- #Get the chunk to the left
- chunks[0] = self.chunks[chunkindex - 1]
- #Try to get the chunk to the right:
- try:
- chunks[2] = self.chunks[chunkindex + 1]
- except IndexError:
- #The current chunk is in the last slot of the chunks
- #so get the first chunk
- chunks[2] = self.chunks[0]
- return chunks
Advertisement
Add Comment
Please, Sign In to add comment