From a1a130ae32f0dc95dd9ca9f3916d2c68d1645880 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sat, 15 May 2021 15:53:40 +0200 Subject: [PATCH] improvement: parse and validate hitboxes --- .../modules/world/actors/mixins/physics.lua | 12 +++++----- .../modules/world/actors/utils/hitbox2D.lua | 11 +++++---- .../modules/world/actors/utils/hitbox3D.lua | 15 +++++++----- sonic-radiance.love/birb/structures/box.lua | 1 + .../birb/structures/hitbox.lua | 1 + sonic-radiance.love/birb/structures/rect.lua | 1 + sonic-radiance.love/birb/utils/datas.lua | 23 +++++++++++++++++++ 7 files changed, 48 insertions(+), 16 deletions(-) create mode 100644 sonic-radiance.love/birb/structures/box.lua create mode 100644 sonic-radiance.love/birb/structures/hitbox.lua create mode 100644 sonic-radiance.love/birb/structures/rect.lua diff --git a/sonic-radiance.love/birb/modules/world/actors/mixins/physics.lua b/sonic-radiance.love/birb/modules/world/actors/mixins/physics.lua index 6cd7029..8b6a143 100644 --- a/sonic-radiance.love/birb/modules/world/actors/mixins/physics.lua +++ b/sonic-radiance.love/birb/modules/world/actors/mixins/physics.lua @@ -1,4 +1,5 @@ PhysicalActor = Object:extend() +local hitboxStructure = require "birb.structures.hitbox" -- PHYSICS FUNCTIONS -- Raw implementation of everything common in physics @@ -176,23 +177,22 @@ end function PhysicalActor:addHitboxFromFrameData(framedata, animationID, frameID, hitboxID) local sx, sy = self.sprite:getScalling() - local type = framedata[1] - local box = framedata[2] - local isSolid = framedata[3] or false + local hitbox = utils.table.parse(framedata, hitboxStructure, 1) local anim = animationID or "null" local frame = frameID or 0 local id = hitboxID or 0 - if (type == "main") then - self.mainHitbox:setFromData(box, sx, sy) + if (hitbox.type == "main") then + self.mainHitbox:setFromData(hitbox.box, sx, sy) else local hitboxName = anim .. frame .. type .. id - self:addHitbox(hitboxName, type, box, sx, sy, isSolid) + self:addHitbox(hitboxName, hitbox.type, hitbox.box, sx, sy, hitbox.isSolid) return hitboxName end end function PhysicalActor:addHitbox(name, type, data, sx, sy, isSolid) + isSolid = (isSolid == true) if (self.hitboxes[name] ~= nil) then core.debug:logWarn("PhysicalActor", "the hitbox " .. name .. " already exists") else diff --git a/sonic-radiance.love/birb/modules/world/actors/utils/hitbox2D.lua b/sonic-radiance.love/birb/modules/world/actors/utils/hitbox2D.lua index 2452029..cd9571a 100644 --- a/sonic-radiance.love/birb/modules/world/actors/utils/hitbox2D.lua +++ b/sonic-radiance.love/birb/modules/world/actors/utils/hitbox2D.lua @@ -23,6 +23,7 @@ ]] local Hitbox2D = Object:extend() +local rectStructure = require "birb.structures.rect" -- INIT FUNCTIONS -- Initialise the actor and its base functions @@ -55,10 +56,12 @@ function Hitbox2D:modify(ox, oy, w, h) end function Hitbox2D:setFromData(data, sx, sy) - self.ox = data[1] - self.oy = data[2] - self.w = data[3] - self.h = data[4] + local rect = utils.table.parse(data, rectStructure) + + self.ox = rect.x + self.oy = rect.y + self.w = rect.w + self.h = rect.h self:applyScale(sx, sy) end diff --git a/sonic-radiance.love/birb/modules/world/actors/utils/hitbox3D.lua b/sonic-radiance.love/birb/modules/world/actors/utils/hitbox3D.lua index e49761b..0158742 100644 --- a/sonic-radiance.love/birb/modules/world/actors/utils/hitbox3D.lua +++ b/sonic-radiance.love/birb/modules/world/actors/utils/hitbox3D.lua @@ -23,6 +23,7 @@ ]] local Hitbox3D = Object:extend() +local boxStructure = require "birb.structures.box" -- INIT FUNCTIONS -- Initialise the actor and its base functions @@ -57,12 +58,14 @@ function Hitbox3D:modify(ox, oy, oz, w, h, d) end function Hitbox3D:setFromData(data, sx, sy) - self.ox = data[1] - self.oy = data[2] - self.oz = data[3] - self.w = data[4] - self.h = data[5] - self.d = data[6] + local box = utils.table.parse(data, boxStructure) + + self.ox = box.x + self.oy = box.y + self.oz = box.z + self.w = box.w + self.h = box.h + self.d = box.d self:applyScale(sx, sy) end diff --git a/sonic-radiance.love/birb/structures/box.lua b/sonic-radiance.love/birb/structures/box.lua new file mode 100644 index 0000000..572ae5d --- /dev/null +++ b/sonic-radiance.love/birb/structures/box.lua @@ -0,0 +1 @@ +return {"x", "y", "z", "w", "h", "d"} \ No newline at end of file diff --git a/sonic-radiance.love/birb/structures/hitbox.lua b/sonic-radiance.love/birb/structures/hitbox.lua new file mode 100644 index 0000000..21aad41 --- /dev/null +++ b/sonic-radiance.love/birb/structures/hitbox.lua @@ -0,0 +1 @@ +return {"type", "box", "isSolid"} \ No newline at end of file diff --git a/sonic-radiance.love/birb/structures/rect.lua b/sonic-radiance.love/birb/structures/rect.lua new file mode 100644 index 0000000..85032be --- /dev/null +++ b/sonic-radiance.love/birb/structures/rect.lua @@ -0,0 +1 @@ +return {"x", "y", "w", "h"} \ No newline at end of file diff --git a/sonic-radiance.love/birb/utils/datas.lua b/sonic-radiance.love/birb/utils/datas.lua index 1b7bf8e..ad99d82 100644 --- a/sonic-radiance.love/birb/utils/datas.lua +++ b/sonic-radiance.love/birb/utils/datas.lua @@ -1,3 +1,26 @@ +-- loveutils.datas : simple functions for data manipulation. + +--[[ + Copyright © 2021 Kazhnuz + + Permission is hereby granted, free of charge, to any person obtaining a copy of + this software and associated documentation files (the "Software"), to deal in + the Software without restriction, including without limitation the rights to + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of + the Software, and to permit persons to whom the Software is furnished to do so, + subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all + copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER + IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +]] + local DataUtils = {} local DATADIR = "datas"