-
Notifications
You must be signed in to change notification settings - Fork 5
/
collider.lua
78 lines (64 loc) · 1.83 KB
/
collider.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
-- a Collider object, wrapping shape, body, and fixtue
local set_funcs, lp, lg, COLLIDER_TYPES = unpack(
require((...):gsub('collider', '') .. '/utils'))
local Collider = {}
Collider.__index = Collider
function Collider.new(world, collider_type, ...)
print("Collider.new is deprecated and may be removed in a later version. use world:newCollider instead")
return world:newCollider(collider_type, {...})
end
function Collider:draw_type()
if self.collider_type == 'Edge' or self.collider_type == 'Chain' then
return 'line'
end
return self.collider_type:lower()
end
function Collider:__draw__()
self._draw_type = self._draw_type or self:draw_type()
local args
if self._draw_type == 'line' then
args = {self:getSpatialIdentity()}
else
args = {'line', self:getSpatialIdentity()}
end
love.graphics[self:draw_type()](unpack(args))
end
function Collider:setDrawOrder(num)
self._draw_order = num
self._world._draw_order_changed = true
end
function Collider:getDrawOrder()
return self._draw_order
end
function Collider:draw()
self:__draw__()
end
function Collider:destroy()
self._world:_remove(self)
self.fixture:setUserData(nil)
self.fixture:destroy()
self.body:destroy()
end
function Collider:getSpatialIdentity()
if self.collider_type == 'Circle' then
return self:getX(), self:getY(), self:getRadius()
else
return self:getWorldPoints(self:getPoints())
end
end
function Collider:collider_contacts()
local contacts = self:getContacts()
local colliders = {}
for i, contact in ipairs(contacts) do
if contact:isTouching() then
local f1, f2 = contact:getFixtures()
if f1 == self.fixture then
colliders[#colliders+1] = f2:getUserData()
else
colliders[#colliders+1] = f1:getUserData()
end
end
end
return colliders
end
return Collider