From 9f66df8537b1e9943d9b2baa2ffee4867700c458 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Thu, 27 Jun 2019 20:46:21 +0200 Subject: [PATCH] fix(actor2D): let hitboxes handle themselves --- gamecore/modules/world/actors/actor2D.lua | 8 +++----- gamecore/modules/world/actors/utils/hitbox2D.lua | 9 +++++++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/gamecore/modules/world/actors/actor2D.lua b/gamecore/modules/world/actors/actor2D.lua index 75d2e2c..e1970a6 100644 --- a/gamecore/modules/world/actors/actor2D.lua +++ b/gamecore/modules/world/actors/actor2D.lua @@ -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 diff --git a/gamecore/modules/world/actors/utils/hitbox2D.lua b/gamecore/modules/world/actors/utils/hitbox2D.lua index f20c8ff..a29a7a5 100644 --- a/gamecore/modules/world/actors/utils/hitbox2D.lua +++ b/gamecore/modules/world/actors/utils/hitbox2D.lua @@ -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