Fix #59: add HC.shapesAt(x,y)

This commit is contained in:
Matthias Richter 2018-10-24 13:31:31 +02:00
parent 94bf0ff4ac
commit f0aa1bf3d8
2 changed files with 28 additions and 1 deletions

View File

@ -217,7 +217,7 @@ You can iterate over the shapes using ``pairs`` (see example).
.. function:: HC.neighbors(shape)
:param Shape shape: Query shape.
:returns: Table of neighboring shapes, where the keys of the table are the shape.
:returns: Table of neighboring shapes, where the keys of the table are the shapes.
Get other shapes in that are close to ``shape``.
The table is a *set*, meaning that the shapes are stored in *keys* of the table.
@ -238,6 +238,22 @@ You can iterate over the shapes using ``pairs`` (see example).
end
end
.. function:: HC.shapesAt(x, y)
:param numbers x,y: Point to query.
:returns: Table of shapes at the point, where the keys of the table are the shapes.
Get shapes that contain the point (x,y).
The table is a *set*, meaning that the shapes are stored in *keys* of the table.
You can iterate over the shapes using ``pairs`` (see example).
**Example**::
local shapes = HC.shapesAt(love.mouse.getPosition)
for s in pairs(shapes) do
game.selectUnit(s)
end
.. function:: HC.hash()

View File

@ -118,6 +118,16 @@ function HC:collisions(shape)
return candidates
end
function HC:shapesAt(x, y)
local candidates = {}
for c in pairs(self._hash:cellAt(x, y)) do
if c:contains(x, y) then
rawset(candidates, c, c)
end
end
return candidates
end
-- the class and the instance
HC = common_local.class('HardonCollider', HC)
local instance = common_local.instance(HC)
@ -136,5 +146,6 @@ return setmetatable({
neighbors = function(...) return instance:neighbors(...) end,
collisions = function(...) return instance:collisions(...) end,
shapesAt = function(...) return instance:shapesAt(...) end,
hash = function() return instance.hash() end,
}, {__call = function(_, ...) return common_local.instance(HC, ...) end})