From b0715476306c804236eafb54820f095ef0d9e79f Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Thu, 26 Nov 2020 20:12:05 +0100 Subject: [PATCH] chore: let the hitbox handle the scaling --- birb/modules/world/actors/actor2D.lua | 16 +++++----------- birb/modules/world/actors/actor3D.lua | 16 +++++----------- birb/modules/world/actors/utils/hitbox2D.lua | 17 ++++++++++++++--- birb/modules/world/actors/utils/hitbox3D.lua | 16 +++++++++++++--- 4 files changed, 37 insertions(+), 28 deletions(-) diff --git a/birb/modules/world/actors/actor2D.lua b/birb/modules/world/actors/actor2D.lua index 924b61f..ca334eb 100644 --- a/birb/modules/world/actors/actor2D.lua +++ b/birb/modules/world/actors/actor2D.lua @@ -153,32 +153,26 @@ function Actor2D:addHitboxFromFrameData(framedata, animationID, frameID, hitboxI local anim = animationID or "null" local frame = frameID or 0 local id = hitboxID or 0 - if (sx < 0) then - ox = self.w - ox - w - end - if (sy < 0) then - oy = self.h - oy - h - end if (type == "main") then - self.mainHitbox:setFromData({ox, oy, w, h}) + self.mainHitbox:setFromData({ox, oy, w, h}, sx, sy) else local hitboxName = anim .. frame .. type .. id - self:addHitbox(hitboxName, type, {ox, oy, w, h}, isSolid) + self:addHitbox(hitboxName, type, {ox, oy, w, h}, sx, sy, isSolid) return hitboxName end end 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}, 0, 0, self.isSolid) self.mainHitbox:advertiseAsMainHitbox() end -function Actor2D:addHitbox(name, type, data, isSolid) +function Actor2D:addHitbox(name, type, data, sx, sy, isSolid) if (self.hitboxes[name] ~= nil) then core.debug:logWarn("actor2D", "the hitbox " .. name .. " already exists") else - local hitbox = Hitbox(self, type, data, isSolid) + local hitbox = Hitbox(self, type, data, sx, sy, isSolid) self.hitboxes[name] = hitbox return hitbox end diff --git a/birb/modules/world/actors/actor3D.lua b/birb/modules/world/actors/actor3D.lua index 67b1286..70fe8f8 100644 --- a/birb/modules/world/actors/actor3D.lua +++ b/birb/modules/world/actors/actor3D.lua @@ -177,32 +177,26 @@ function Actor3D:addHitboxFromFrameData(framedata, animationID, frameID, hitboxI local anim = animationID or "null" local frame = frameID or 0 local id = hitboxID or 0 - if (sx < 0) then - ox = self.w - ox - w - end - if (sy < 0) then - oz = self.d - oz - d - end if (type == "main") then - self.mainHitbox:setFromData({ox, oy, oz, w, h, d}) + self.mainHitbox:setFromData({ox, oy, oz, w, h, d}, sx, sy) else local hitboxName = anim .. frame .. type .. id - self:addHitbox(hitboxName, type, {ox, oy, oz, w, h, d}, isSolid) + self:addHitbox(hitboxName, type, {ox, oy, oz, w, h, d}, sx, sy, isSolid) return hitboxName end end function Actor3D:initMainHitbox() - self.mainHitbox = Hitbox(self, self.type, {0, 0, 0, self.w, self.h, self.d}, self.isSolid) + self.mainHitbox = Hitbox(self, self.type, {0, 0, 0, self.w, self.h, self.d}, 0, 0, self.isSolid) self.mainHitbox:advertiseAsMainHitbox() end -function Actor3D:addHitbox(name, type, data, isSolid) +function Actor3D:addHitbox(name, type, data, sx, sy, isSolid) if (self.hitboxes[name] ~= nil) then core.debug:logWarn("actor3D", "the hitbox " .. name .. " already exists") else - local hitbox = Hitbox(self, type, data, isSolid) + local hitbox = Hitbox(self, type, data, sx, sy, isSolid) self.hitboxes[name] = hitbox return hitbox end diff --git a/birb/modules/world/actors/utils/hitbox2D.lua b/birb/modules/world/actors/utils/hitbox2D.lua index 64ab4ee..3b7625e 100644 --- a/birb/modules/world/actors/utils/hitbox2D.lua +++ b/birb/modules/world/actors/utils/hitbox2D.lua @@ -27,12 +27,12 @@ local Hitbox2D = Object:extend() -- INIT FUNCTIONS -- Initialise the actor and its base functions -function Hitbox2D:new(owner, type, data, isSolid) +function Hitbox2D:new(owner, type, data, sx, sy, isSolid) self.owner = owner self.world = owner.world self.type = type - self:setFromData(data) + self:setFromData(data, sx, sy) self.x, self.y = self:getPosition() self.isSolid = isSolid @@ -42,11 +42,22 @@ function Hitbox2D:new(owner, type, data, isSolid) self:register() end -function Hitbox2D:setFromData(data) +function Hitbox2D:setFromData(data, sx, sy) + local sx = sx or 0 + local sy = sy or 0 + self.ox = data[1] self.oy = data[2] self.w = data[3] self.h = data[4] + + if (sx < 0) then + self.ox = self.owner.w - self.ox - self.w + end + + if (sy < 0) then + self.oy = self.owner.h - self.oy - self.h + end end function Hitbox2D:advertiseAsMainHitbox() diff --git a/birb/modules/world/actors/utils/hitbox3D.lua b/birb/modules/world/actors/utils/hitbox3D.lua index 390b16a..77a0ab8 100644 --- a/birb/modules/world/actors/utils/hitbox3D.lua +++ b/birb/modules/world/actors/utils/hitbox3D.lua @@ -27,12 +27,12 @@ local Hitbox3D = Object:extend() -- INIT FUNCTIONS -- Initialise the actor and its base functions -function Hitbox3D:new(owner, type, data, isSolid) +function Hitbox3D:new(owner, type, data, sx, sy, isSolid) self.owner = owner self.world = owner.world self.type = type - self:setFromData(data) + self:setFromData(data, sx, sy) self.x, self.y, self.z = self:getPosition() self.isSolid = isSolid @@ -42,13 +42,23 @@ function Hitbox3D:new(owner, type, data, isSolid) self:register() end -function Hitbox3D:setFromData(data) +function Hitbox3D:setFromData(data, sx, sy) + local sx = sx or 0 + local sy = sy or 0 + self.ox = data[1] self.oy = data[2] self.oz = data[3] self.w = data[4] self.h = data[5] self.d = data[6] + + if (sx < 0) then + self.ox = self.owner.w - self.ox - self.w + end + if (sy < 0) then + self.oz = self.owner.d - self.oz - self.d + end end function Hitbox3D:advertiseAsMainHitbox()