improvement: parse and validate hitboxes

This commit is contained in:
Kazhnuz 2021-05-15 15:53:40 +02:00
parent 792738c5cc
commit a1a130ae32
7 changed files with 48 additions and 16 deletions

View file

@ -1,4 +1,5 @@
PhysicalActor = Object:extend() PhysicalActor = Object:extend()
local hitboxStructure = require "birb.structures.hitbox"
-- PHYSICS FUNCTIONS -- PHYSICS FUNCTIONS
-- Raw implementation of everything common in physics -- Raw implementation of everything common in physics
@ -176,23 +177,22 @@ end
function PhysicalActor:addHitboxFromFrameData(framedata, animationID, frameID, hitboxID) function PhysicalActor:addHitboxFromFrameData(framedata, animationID, frameID, hitboxID)
local sx, sy = self.sprite:getScalling() local sx, sy = self.sprite:getScalling()
local type = framedata[1] local hitbox = utils.table.parse(framedata, hitboxStructure, 1)
local box = framedata[2]
local isSolid = framedata[3] or false
local anim = animationID or "null" local anim = animationID or "null"
local frame = frameID or 0 local frame = frameID or 0
local id = hitboxID or 0 local id = hitboxID or 0
if (type == "main") then if (hitbox.type == "main") then
self.mainHitbox:setFromData(box, sx, sy) self.mainHitbox:setFromData(hitbox.box, sx, sy)
else else
local hitboxName = anim .. frame .. type .. id 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 return hitboxName
end end
end end
function PhysicalActor:addHitbox(name, type, data, sx, sy, isSolid) function PhysicalActor:addHitbox(name, type, data, sx, sy, isSolid)
isSolid = (isSolid == true)
if (self.hitboxes[name] ~= nil) then if (self.hitboxes[name] ~= nil) then
core.debug:logWarn("PhysicalActor", "the hitbox " .. name .. " already exists") core.debug:logWarn("PhysicalActor", "the hitbox " .. name .. " already exists")
else else

View file

@ -23,6 +23,7 @@
]] ]]
local Hitbox2D = Object:extend() local Hitbox2D = Object:extend()
local rectStructure = require "birb.structures.rect"
-- INIT FUNCTIONS -- INIT FUNCTIONS
-- Initialise the actor and its base functions -- Initialise the actor and its base functions
@ -55,10 +56,12 @@ function Hitbox2D:modify(ox, oy, w, h)
end end
function Hitbox2D:setFromData(data, sx, sy) function Hitbox2D:setFromData(data, sx, sy)
self.ox = data[1] local rect = utils.table.parse(data, rectStructure)
self.oy = data[2]
self.w = data[3] self.ox = rect.x
self.h = data[4] self.oy = rect.y
self.w = rect.w
self.h = rect.h
self:applyScale(sx, sy) self:applyScale(sx, sy)
end end

View file

@ -23,6 +23,7 @@
]] ]]
local Hitbox3D = Object:extend() local Hitbox3D = Object:extend()
local boxStructure = require "birb.structures.box"
-- INIT FUNCTIONS -- INIT FUNCTIONS
-- Initialise the actor and its base functions -- Initialise the actor and its base functions
@ -57,12 +58,14 @@ function Hitbox3D:modify(ox, oy, oz, w, h, d)
end end
function Hitbox3D:setFromData(data, sx, sy) function Hitbox3D:setFromData(data, sx, sy)
self.ox = data[1] local box = utils.table.parse(data, boxStructure)
self.oy = data[2]
self.oz = data[3] self.ox = box.x
self.w = data[4] self.oy = box.y
self.h = data[5] self.oz = box.z
self.d = data[6] self.w = box.w
self.h = box.h
self.d = box.d
self:applyScale(sx, sy) self:applyScale(sx, sy)
end end

View file

@ -0,0 +1 @@
return {"x", "y", "z", "w", "h", "d"}

View file

@ -0,0 +1 @@
return {"type", "box", "isSolid"}

View file

@ -0,0 +1 @@
return {"x", "y", "w", "h"}

View file

@ -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 DataUtils = {}
local DATADIR = "datas" local DATADIR = "datas"