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

View file

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