Advertisement
Prexxo

Untitled

Mar 13th, 2023
680
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.11 KB | None | 0 0
  1. local function WorldToGridPos(self, WorldPos)
  2.     local CellSize = self.CellSize
  3.     local RoundedHalfCellSize = self.RoundedHalfCellSize
  4.     local X = WorldPos.X + RoundedHalfCellSize.X
  5.     local Y = (WorldPos.Z or WorldPos.Y) + RoundedHalfCellSize.Y
  6.    
  7.     return Vector2.new(
  8.         math.floor( X / CellSize.X),
  9.         math.floor( Y / CellSize.Y)
  10.     )
  11. end
  12.  
  13. local function CreateCell(self, CellPos)
  14.     local Pos_X, Pos_Y = CellPos.X, CellPos.Y
  15.     local Cells = self.Cells
  16.     local Cell_X = Cells[Pos_X]
  17.    
  18.     if not Cell_X then
  19.         Cell_X = {}
  20.         Cells[Pos_X] = Cell_X  
  21.     end
  22.    
  23.     local Cell_Y = Cell_X[Pos_Y]
  24.    
  25.     if not Cell_Y then
  26.         Cell_Y = {}
  27.        
  28.         Cell_X[Pos_Y] = Cell_Y
  29.     end
  30.    
  31.     return Cell_Y
  32. end
  33.  
  34. local function GetCell(self, CellPos)
  35.     local Cell_X = self.Cells[CellPos.X]
  36.    
  37.     return Cell_X and Cell_X[CellPos.Y]
  38. end
  39.  
  40.  
  41. local function MoveObjectToCell(self, Object, CellPos, ObjectCacheName)
  42.     local NewCell = self:CreateCell(CellPos)
  43.     local ObjectToCell = self.ObjectToCell
  44.     local OldCell = ObjectToCell[Object]
  45.     local ObjectCache = ObjectCacheName == nil and NewCell or NewCell[ObjectCacheName]
  46.    
  47.     if not ObjectCache then
  48.         ObjectCache = {}
  49.        
  50.         NewCell[ObjectCacheName] = ObjectCacheName
  51.     end
  52.    
  53.     if OldCell then
  54.         if OldCell[Object] then
  55.             --stored in dictionary
  56.             OldCell[Object] = nil
  57.         else
  58.             --stored in an array
  59.             table.remove(OldCell, table.find(OldCell, Object))
  60.         end
  61.     end
  62.    
  63.     table.insert(ObjectCache, Object)
  64.     ObjectToCell[Object] = ObjectCache
  65. end
  66.  
  67. local function RegisterObject(self, Object, WorldPos, CacheName)
  68.     local CellPos = self:WorldToGridPos(WorldPos)
  69.    
  70.     MoveObjectToCell(self, Object, CellPos, CacheName)
  71.    
  72.     return CellPos
  73. end
  74.  
  75. local Module = {}
  76.  
  77. function Module.new(CellSize)
  78.     local Grid = {
  79.         CellSize = CellSize,
  80.         RoundedHalfCellSize = Vector2.new(math.floor(CellSize.X/2), math.floor(CellSize.Y/2)),
  81.        
  82.         Cells = {}, --[X][Z] = {}
  83.         ObjectToCell = {}, --[Object] = Cell
  84.        
  85.         WorldToGridPos = WorldToGridPos,
  86.         GetCell = GetCell,
  87.         RegisterObject = RegisterObject,
  88.         MoveObjectToCell = MoveObjectToCell,
  89.         CreateCell = CreateCell,
  90.        
  91.     }
  92.    
  93.     return Grid
  94. end
  95.  
  96. return Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement