fix(actor2D): let hitboxes handle themselves

This commit is contained in:
Kazhnuz 2019-06-27 20:46:21 +02:00
parent b7344a8973
commit 9f66df8537
2 changed files with 12 additions and 5 deletions

View file

@ -39,7 +39,7 @@ end
function Actor2D:destroy()
self.world:removeActor(self)
self.world:removeBody(self.mainHitbox)
self.mainHitbox:destroy()
self.isDestroyed = true
end
@ -262,7 +262,6 @@ end
function Actor2D:initMainHitbox()
self.mainHitbox = Hitbox(self, self.type, 0, 0, self.w, self.h, self.isSolid)
self.world:registerBody(self.mainHitbox)
end
function Actor2D:addHitbox(name, type, ox, oy, w, h, isSolid)
@ -271,7 +270,6 @@ function Actor2D:addHitbox(name, type, ox, oy, w, h, isSolid)
else
local hitbox = Hitbox(self, type, ox, oy, w, h, isSolid)
self.hitboxes[name] = hitbox
self.world:registerBody(self.hitboxes[name])
return hitbox
end
end
@ -307,14 +305,14 @@ end
function Actor2D:removeHitbox(name)
if (self.hitboxes[name] ~= nil) then
self.world:removeBody(self.hitboxes[name])
self.hitboxes[name]:destroy()
self.hitboxes[name] = nil
end
end
function Actor2D:purgeHitbox()
for k,v in pairs(self.hitboxes) do
self.world:removeBody(v)
v:destroy()
end
self.hitboxes = {}
end

View file

@ -40,6 +40,7 @@ function Hitbox2D:new(owner, type, ox, oy, w, h, isSolid)
self.isSolid = isSolid
self:setDebugColor(0,0,0)
self:register()
end
function Hitbox2D:modify(ox, oy, w, h)
@ -57,6 +58,14 @@ function Hitbox2D:setDebugColor(r,g,b)
self.debug.b = b
end
function Hitbox2D:register()
self.world:registerBody(self)
end
function Hitbox2D:destroy()
self.world:removeBody(self)
end
-- COORDINATE FUNCTIONS
-- Handle Hitbox position