-
Notifications
You must be signed in to change notification settings - Fork 47
/
init.lua
175 lines (148 loc) · 5.29 KB
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
--[[
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.
]]--
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
local Shapes = require(_NAME .. '.shapes')
local Spatialhash = require(_NAME .. '.spatialhash')
-- 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
local HC = {}
function HC:init(cell_size)
self:resetHash(cell_size)
end
function HC:hash() return self._hash end -- consistent interface with global HC instance
-- spatial hash management
function HC:resetHash(cell_size)
self._hash = common_local.instance(Spatialhash, cell_size or 100)
return self
end
function HC:register(shape)
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, ...)
self._hash:update(this, x1,y1,x2,y2, this:bbox())
return this
end
end
return shape
end
function HC:remove(shape)
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
end
return self
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)
end
function HC:circle(x,y,r)
return self:register(newCircleShape(x,y,r))
end
function HC:point(x,y)
return self:register(newPointShape(x,y))
end
-- collision detection
function HC:neighbors(shape)
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
end
function HC:raycast(x, y, dx, dy, range)
local dxr, dyr = dx * range, dy * range
local bbox = { x + dxr , y + dyr, x, y }
local candidates = self._hash:inSameCells(unpack(bbox))
for col in pairs(candidates) do
local rparams = col:intersectionsWithRay(x, y, dx, dy)
if #rparams > 0 then
for i, rparam in pairs(rparams) do
if rparam < 0 or rparam > range then
rawset(rparams, i, nil)
else
local hitx, hity = x + (rparam * dx), y + (rparam * dy)
rawset(rparams, i, { x = hitx, y = hity })
end
end
rawset(candidates, col, rparams)
else
rawset(candidates, col, nil)
end
end
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)
-- 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,
shapesAt = function(...) return instance:shapesAt(...) end,
hash = function() return instance.hash() end,
}, {__call = function(_, ...) return common_local.instance(HC, ...) end})