HC/init.lua

152 lines
4.7 KiB
Lua
Raw Normal View History

2011-01-13 13:38:36 +00:00
--[[
Copyright (c) 2011 Matthias Richter
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
Except as contained in this notice, the name(s) of the above copyright holders
shall not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
]]--
2013-07-21 10:51:35 +00:00
local _NAME, common_local = ..., common
if not (type(common) == 'table' and common.class and common.instance) then
assert(common_class ~= false, 'No class commons specification available.')
require(_NAME .. '.class')
end
2011-01-18 22:33:11 +00:00
local Shapes = require(_NAME .. '.shapes')
local Spatialhash = require(_NAME .. '.spatialhash')
2013-07-21 10:51:35 +00:00
-- reset global table `common' (required by class commons)
if common_local ~= common then
common_local, common = common, common_local
end
local newPolygonShape = Shapes.newPolygonShape
local newCircleShape = Shapes.newCircleShape
local newPointShape = Shapes.newPointShape
2011-01-13 13:38:36 +00:00
local HC = {}
function HC:init(cell_size)
2018-04-08 12:35:15 +00:00
self:resetHash(cell_size)
2011-01-13 13:38:36 +00:00
end
2018-04-08 12:35:15 +00:00
function HC:hash() return self._hash end -- consistent interface with global HC instance
-- spatial hash management
function HC:resetHash(cell_size)
2018-04-08 12:35:15 +00:00
self._hash = common_local.instance(Spatialhash, cell_size or 100)
return self
2011-01-13 13:38:36 +00:00
end
function HC:register(shape)
2018-04-08 12:35:15 +00:00
self._hash:register(shape, shape:bbox())
-- keep track of where/how big the shape is
for _, f in ipairs({'move', 'rotate', 'scale'}) do
local old_function = shape[f]
shape[f] = function(this, ...)
local x1,y1,x2,y2 = this:bbox()
old_function(this, ...)
2018-04-08 12:35:15 +00:00
self._hash:update(this, x1,y1,x2,y2, this:bbox())
return this
2012-07-05 15:30:13 +00:00
end
end
2011-01-13 13:38:36 +00:00
return shape
end
function HC:remove(shape)
2018-04-08 12:35:15 +00:00
self._hash:remove(shape, shape:bbox())
for _, f in ipairs({'move', 'rotate', 'scale'}) do
shape[f] = function()
error(f.."() called on a removed shape")
end
2011-01-13 13:38:36 +00:00
end
return self
2011-01-13 13:38:36 +00:00
end
-- shape constructors
function HC:polygon(...)
return self:register(newPolygonShape(...))
end
function HC:rectangle(x,y,w,h)
return self:polygon(x,y, x+w,y, x+w,y+h, x,y+h)
2011-01-18 14:04:27 +00:00
end
function HC:circle(x,y,r)
return self:register(newCircleShape(x,y,r))
2011-01-18 14:04:27 +00:00
end
function HC:point(x,y)
return self:register(newPointShape(x,y))
2011-01-18 14:04:27 +00:00
end
-- collision detection
function HC:neighbors(shape)
2018-04-08 12:35:15 +00:00
local neighbors = self._hash:inSameCells(shape:bbox())
rawset(neighbors, shape, nil)
return neighbors
end
function HC:collisions(shape)
local candidates = self:neighbors(shape)
for other in pairs(candidates) do
local collides, dx, dy = shape:collidesWith(other)
if collides then
rawset(candidates, other, {dx,dy, x=dx, y=dy})
else
rawset(candidates, other, nil)
end
end
return candidates
2011-01-18 14:04:27 +00:00
end
2018-10-24 11:31:31 +00:00
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)
-- the module
return setmetatable({
new = function(...) return common_local.instance(HC, ...) end,
resetHash = function(...) return instance:resetHash(...) end,
register = function(...) return instance:register(...) end,
remove = function(...) return instance:remove(...) end,
polygon = function(...) return instance:polygon(...) end,
rectangle = function(...) return instance:rectangle(...) end,
circle = function(...) return instance:circle(...) end,
point = function(...) return instance:point(...) end,
neighbors = function(...) return instance:neighbors(...) end,
collisions = function(...) return instance:collisions(...) end,
2018-10-24 11:31:31 +00:00
shapesAt = function(...) return instance:shapesAt(...) end,
2018-04-08 12:35:15 +00:00
hash = function() return instance.hash() end,
}, {__call = function(_, ...) return common_local.instance(HC, ...) end})