Facebook
From CsWork, 1 Year ago, written in Python.
Embed
Download Paste or View Raw
Hits: 404
  1.  
  2.         class ItemGrid(object):
  3.                 def __init__(self, width, height):
  4.                         self.grid = {}
  5.                         self.gridWidth = width
  6.                         self.gridHeight = height
  7.                         self.gridSize = width * height
  8.                         self.Clear()
  9.  
  10.                 def __del__(self):
  11.                         self.grid = {}
  12.                         self.gridWidth = 0
  13.                         self.gridHeight = 0
  14.                         self.gridSize = 0
  15.  
  16.                 def Clear(self):
  17.                         for pos in range(self.gridSize):
  18.                                 self.grid[pos] = False
  19.  
  20.                 def IsEmpty(self, pos, width, height):
  21.                         row = pos / self.gridWidth
  22.  
  23.                         if row + height > self.gridHeight:
  24.                                 return False
  25.  
  26.                         if pos + width > row * self.gridWidth + self.gridWidth:
  27.                                 return False
  28.  
  29.                         for y in range(height):
  30.                                 start = pos + (y * self.gridWidth)
  31.                                 if self.grid[start] == True:
  32.                                         return False
  33.  
  34.                                 x = 1
  35.                                 while x < width:
  36.                                         x =+ 1
  37.                                         if self.grid[start + x] == True:
  38.                                                 return False
  39.  
  40.                         return True
  41.  
  42.                 def FindBlank(self, width, height):
  43.                         if width > self.gridWidth or height > self.gridHeight:
  44.                                 return -1
  45.  
  46.                         for row in range(self.gridHeight):
  47.                                 for col in range(self.gridWidth):
  48.                                         index = row * self.gridWidth + col
  49.                                         if self.IsEmpty(index, width, height):
  50.                                                 return index
  51.  
  52.                         return -1
  53.  
  54.                 def Put(self, pos, width, height):
  55.                         if not self.IsEmpty(pos, width, height):
  56.                                 return False
  57.  
  58.                         for y in range(height):
  59.                                 start = pos + (y * self.gridWidth)
  60.                                 self.grid[start] = True
  61.  
  62.                                 x = 1
  63.                                 while x < width:
  64.                                         x += 1
  65.                                         self.grid[start + x] = True
  66.  
  67.                         return True
  68.  
  69. #       C++'da (grid.h) item slotun python versiyonu.
  70.  
  71. #       Ilk Yukleme
  72.         self.itemGrid = self.ItemGrid(4, 4) #4x4= 16Slot
  73.  
  74. #       Memory Destroy
  75.         self.itemGrid = {}
  76.  
  77. #       Slot Temizleme
  78.         if self.itemGrid:
  79.                 self.itemGrid.Clear()
  80.  
  81. #       Itemin boyutunu alalim.
  82.         itemVnum = 189
  83.         itemCount = 1
  84.  
  85.         item.SelectItem(itemVnum)
  86.         itemIcon = item.GetIconImage()
  87.         (width, height) = item.GetItemSize() #Genislik, Yukseklik
  88.  
  89.         pos = self.itemGrid.FindBlank(width, height) #Bos Slot Bul!
  90.         if pos == -1:
  91.                 break
  92.         self.itemGrid.Put(pos, width, height) #Slota Ekle!