This commit is contained in:
Matthias Richter 2018-04-08 14:35:15 +02:00
parent d64a0daaea
commit 7cac6db548
2 changed files with 13 additions and 13 deletions

View File

@ -248,6 +248,8 @@ You can iterate over the shapes using ``pairs`` (see example).
end end
.. attribute:: HC.hash .. function:: HC.hash()
Reference to the :class:`SpatialHash` instance. :returns: :class:`SpatialHash`.
Get a reference to the :class:`SpatialHash` instance.

View File

@ -43,21 +43,19 @@ local newPointShape = Shapes.newPointShape
local HC = {} local HC = {}
function HC:init(cell_size) function HC:init(cell_size)
self.hash = common_local.instance(Spatialhash, cell_size or 100) self:resetHash(cell_size)
end end
function HC:hash() return self._hash end -- consistent interface with global HC instance
-- spatial hash management -- spatial hash management
function HC:resetHash(cell_size) function HC:resetHash(cell_size)
local hash = self.hash self._hash = common_local.instance(Spatialhash, cell_size or 100)
self.hash = common_local.instance(Spatialhash, cell_size or 100)
for shape in pairs(hash:shapes()) do
self.hash:register(shape, shape:bbox())
end
return self return self
end end
function HC:register(shape) function HC:register(shape)
self.hash:register(shape, shape:bbox()) self._hash:register(shape, shape:bbox())
-- keep track of where/how big the shape is -- keep track of where/how big the shape is
for _, f in ipairs({'move', 'rotate', 'scale'}) do for _, f in ipairs({'move', 'rotate', 'scale'}) do
@ -65,7 +63,7 @@ function HC:register(shape)
shape[f] = function(this, ...) shape[f] = function(this, ...)
local x1,y1,x2,y2 = this:bbox() local x1,y1,x2,y2 = this:bbox()
old_function(this, ...) old_function(this, ...)
self.hash:update(this, x1,y1,x2,y2, this:bbox()) self._hash:update(this, x1,y1,x2,y2, this:bbox())
return this return this
end end
end end
@ -74,7 +72,7 @@ function HC:register(shape)
end end
function HC:remove(shape) function HC:remove(shape)
self.hash:remove(shape, shape:bbox()) self._hash:remove(shape, shape:bbox())
for _, f in ipairs({'move', 'rotate', 'scale'}) do for _, f in ipairs({'move', 'rotate', 'scale'}) do
shape[f] = function() shape[f] = function()
error(f.."() called on a removed shape") error(f.."() called on a removed shape")
@ -102,7 +100,7 @@ end
-- collision detection -- collision detection
function HC:neighbors(shape) function HC:neighbors(shape)
local neighbors = self.hash:inSameCells(shape:bbox()) local neighbors = self._hash:inSameCells(shape:bbox())
rawset(neighbors, shape, nil) rawset(neighbors, shape, nil)
return neighbors return neighbors
end end
@ -138,5 +136,5 @@ return setmetatable({
neighbors = function(...) return instance:neighbors(...) end, neighbors = function(...) return instance:neighbors(...) end,
collisions = function(...) return instance:collisions(...) end, collisions = function(...) return instance:collisions(...) end,
hash = function() return instance.hash end, hash = function() return instance.hash() end,
}, {__call = function(_, ...) return common_local.instance(HC, ...) end}) }, {__call = function(_, ...) return common_local.instance(HC, ...) end})