Guest User

Untitled

a guest
Oct 17th, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.23 KB | None | 0 0
  1.     def check_tiles(self):
  2.         """March tiles together"""
  3.         #Loop through chunks
  4.         for c, chunko in enumerate(self.chunks):
  5.             #Loop through tiles in chunk
  6.             for y, row in enumerate(chunko.chunk):
  7.                 for x, tile in enumerate(row):
  8.                     self.check_tile(x, y, c)
  9.             chunko.draw(self.rendersurface)
  10.  
  11.     def check_tile(self, x, y, chunkindex):
  12.         """Checks for tiles"""
  13.         #Get the current chunk to make changes to
  14.         currentchunk = self.chunks[chunkindex]
  15.         if currentchunk.chunk[y][x].tag == "grass":
  16.             #Get the tiles neighbors
  17.             tile_neighbors = self.get_neighbors(x, y, chunkindex)
  18.             #Values for every neighbor tile, listed by indexes
  19.             neighbor_values = {0: 1, 1: 8, 2: 2, 3: 4}
  20.             #Setup a mask value for the grass sprite
  21.             maskindex = 0
  22.             for neighbor_index, neighbor_tile in enumerate(tile_neighbors):
  23.                 if neighbor_tile.tag in ("dirt", "grass"):
  24.                     maskindex += neighbor_values[neighbor_index]
  25.  
  26.             #Add the mask to the tile
  27.             if maskindex:
  28.                 currentchunk.chunk[y][x].mask = [self.grass[maskindex]]
  29.                 currentchunk.chunk[y][x].draw(currentchunk.surface)
  30.  
  31.     def get_neighbors(self, xpos, ypos, chunkindex):
  32.         """Gets E, W, S and N neighbors of a tile in a specified chunk"""
  33.         #Gets chunks that the neighbors can theoriticaly be
  34.         chunks = self.get_neighbor_chunks(chunkindex)
  35.  
  36.         #Loop thorugh a 3x3 square around the tile being checked
  37.         for yy in range(ypos - 1, ypos + 2):
  38.             for xx in range(xpos - 1, xpos + 2):
  39.                 #Skip the corner coordinates
  40.                 if yy != ypos and xx != xpos:
  41.                     continue
  42.                 #Skip also the tile beeing checked
  43.                 if yy == ypos and xx == xpos:
  44.                     continue
  45.  
  46.                 #Get a chunk index and a x index for neighbor tile
  47.                 chunkindex, xindex = self.get_correct_chunk_index(xx)
  48.                 yield chunks[chunkindex].chunk[yy][xindex]
  49.  
  50.     def get_correct_chunk_index(self, xpos):
  51.         """Gets a correct chunk to take the tile from for neighbors
  52.            Returns (chunkindex, correct_x_index)"""
  53.         #If the xpos is to the left chunk
  54.         if xpos < 0:
  55.             return (0, -1)
  56.         #If the xpos is to the right chunk
  57.         if xpos >= len(self.chunks[0].chunk[0]):
  58.             return (2, 0)
  59.         #If the xpos doesn't cross chunk borders
  60.         return (1, xpos)
  61.  
  62.     def get_neighbor_chunks(self, chunkindex):
  63.         """Get neighbor chunks for a chunk at chunkindex"""
  64.         #Create a empty set of chunks
  65.         chunks = [0, 0, 0]
  66.         #Set the middlechunk to current chunk
  67.         chunks[1] = self.chunks[chunkindex]
  68.         #Get the chunk to the left
  69.         chunks[0] = self.chunks[chunkindex - 1]
  70.         #Try to get the chunk to the right:
  71.         try:
  72.             chunks[2] = self.chunks[chunkindex + 1]
  73.         except IndexError:
  74.             #The current chunk is in the last slot of the chunks
  75.             #so get the first chunk
  76.             chunks[2] = self.chunks[0]
  77.         return chunks
Advertisement
Add Comment
Please, Sign In to add comment