Advertisement
Guest User

:O

a guest
Nov 23rd, 2014
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.18 KB | None | 0 0
  1. import numpy as np
  2.  
  3. class MapChunk(object):
  4.  
  5.     def __init__(self, chunkVal):
  6.         """
  7.         chunkVal = 4digit int 1010 minimum value 7474 maximum value
  8.         """
  9.         try:
  10.             self.tileArray=np.load(str(chunkVal/100)+"."+str(chunkVal%100)+".npy")
  11.         except IOError as errorInfo:
  12.             print("IOError: ", errorInfo)
  13.             self.tileArray=np.zeros([64,64])
  14.            
  15.     def getTileArray(self):
  16.         return self.tileArray
  17.     def getTile(self,x,y):
  18.         return self.tileArray[x,y]
  19.        
  20.     def setTileArray(self, tileArray):
  21.         self.tileArray=tileArray
  22.     def setTile(self,x,y,val):
  23.         self.tileArray[x,y]=val
  24.        
  25. class Map(object):
  26.  
  27.     def __init__(self, offsetX, offsetY): # Offset 0 to 61 (61 limit bc 3x3 grid)
  28.         self.mapArray=np.empty(shape=(3,3), dtype=MapChunk) # Create chunk map (64x64)
  29.         self.currentX=offsetX
  30.         self.currentY=offsetY
  31.        
  32.         if offsetX<0: # out of bounds management
  33.             offsetX=0
  34.             print "Map OffsetX < 0, setting to 0. Map.__init__"
  35.         if offsetX>61:
  36.             offsetX=61
  37.             print "Map OffsetX > 61, setting to 61. Map.__init__"
  38.         if offsetY<0:
  39.             offsetY=0
  40.             print "Map OffsetY < 0, setting to 0. Map.__init__"
  41.         if offsetY>61:
  42.             offsetY=61
  43.             print "Map OffsetY > 61, setting to 61. Map.__init__"
  44.         for x in range(3): # Fill with data from file
  45.             for y in range(3):
  46.                 chunkVal=1010+100*(offsetX+x)+offsetY+y
  47.                 self.mapArray[x,y]=MapChunk(chunkVal)
  48.                 # print x, " ", y, " ",chunkVal
  49.    
  50.     def getMap(self): # very much doubt you'd want to use this but psshhhhhh y-knot
  51.         return self.mapArray
  52.     def getMapChunkExact(self, x, y):
  53.         if x>=self.currentX and x<=self.currentX+2: # Check value is in mem
  54.             if y>=self.currentY and y<=self.currentY+2:
  55.                 return self.mapArray[x-self.currentX,y-self.currentY]
  56.         print "Offset out of bounds in Map.getMapChunkExact"
  57.     def getMapChunkByOffset(self, offsetX, offsetY): # range 0-2
  58.         if offsetX>=0 and offsetX<=2: # Check value is in mem
  59.             if offsetY>=0 and offsetY<=2:
  60.                 return self.mapArray[self.currentX+offsetX,self.currentY+offsetY]
  61.         print "Offset out of bounds in Map.getMapChunkByOffset"
  62.                
  63. x=Map(0,0)
  64. x.getMapChunkByOffset(2,2)
  65.  
  66. """tileArray=np.zeros([64,64])
  67. for i in range(10,74):
  68.     for j in range(10,74):
  69.         np.save(str(i)+"."+str(j),tileArray)"""
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement