chore: refactor hitbox adding
This commit is contained in:
parent
f30fc6346d
commit
b2623cdb6a
3 changed files with 49 additions and 72 deletions
|
@ -42,12 +42,16 @@ local Hitbox = require("birb.modules.world.actors.utils.hitbox2D")
|
|||
|
||||
function Actor2D:new(world, type, x, y, w, h, isSolid)
|
||||
self:init(world, type)
|
||||
self:initPhysics(x, y, 0, w, h, 0, isSolid)
|
||||
self:initPhysics(Hitbox, x, y, 0, w, h, 0, isSolid)
|
||||
self:initTimers()
|
||||
self:initSprite()
|
||||
self:initKeys()
|
||||
end
|
||||
|
||||
function Actor2D:packForHitbox()
|
||||
return {0, 0, self.w, self.h}
|
||||
end
|
||||
|
||||
function Actor2D:destroy()
|
||||
self.world:removeActor(self)
|
||||
self.mainHitbox:destroy()
|
||||
|
@ -141,39 +145,6 @@ end
|
|||
-- HITBOXES FUNCTIONS
|
||||
-- Functions related to actor hitboxes
|
||||
|
||||
function Actor2D: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 anim = animationID or "null"
|
||||
local frame = frameID or 0
|
||||
local id = hitboxID or 0
|
||||
|
||||
if (type == "main") then
|
||||
self.mainHitbox:setFromData(box, sx, sy)
|
||||
else
|
||||
local hitboxName = anim .. frame .. type .. id
|
||||
self:addHitbox(hitboxName, type, box, sx, sy, isSolid)
|
||||
return hitboxName
|
||||
end
|
||||
end
|
||||
|
||||
function Actor2D:initMainHitbox()
|
||||
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, 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, sx, sy, isSolid)
|
||||
self.hitboxes[name] = hitbox
|
||||
return hitbox
|
||||
end
|
||||
end
|
||||
|
||||
function Actor2D:checkHitboxCollisions(name, filter)
|
||||
self:checkHitboxCollisionsAtPoint(name, self.x, self.y, filter)
|
||||
end
|
||||
|
|
|
@ -44,7 +44,7 @@ local Boxes = require(cwd .. "utils.boxes")
|
|||
|
||||
function Actor3D:new(world, type, x, y, z, w, h, d, isSolid)
|
||||
self:init(world, type)
|
||||
self:initPhysics(x, y, z, w, h, d, isSolid)
|
||||
self:initPhysics(Hitbox, x, y, z, w, h, d, isSolid)
|
||||
self:initTimers()
|
||||
self:initSprite()
|
||||
self.world:registerShape(self)
|
||||
|
@ -63,6 +63,11 @@ function Actor3D:destroy()
|
|||
self.isDestroyed = true
|
||||
end
|
||||
|
||||
function Actor3D:packForHitbox()
|
||||
return {0, 0, 0, self.w, self.h, self.d}
|
||||
end
|
||||
|
||||
|
||||
-- PHYSICS FUNCTIONS
|
||||
-- Handle movement and collisions.
|
||||
|
||||
|
@ -163,39 +168,6 @@ end
|
|||
-- HITBOXES FUNCTIONS
|
||||
-- Functions related to actor hitboxes
|
||||
|
||||
function Actor3D: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 anim = animationID or "null"
|
||||
local frame = frameID or 0
|
||||
local id = hitboxID or 0
|
||||
|
||||
if (type == "main") then
|
||||
self.mainHitbox:setFromData(box, sx, sy)
|
||||
else
|
||||
local hitboxName = anim .. frame .. type .. id
|
||||
self:addHitbox(hitboxName, type, box, 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}, 0, 0, self.isSolid)
|
||||
self.mainHitbox:advertiseAsMainHitbox()
|
||||
end
|
||||
|
||||
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, sx, sy, isSolid)
|
||||
self.hitboxes[name] = hitbox
|
||||
return hitbox
|
||||
end
|
||||
end
|
||||
|
||||
function Actor3D:checkHitboxCollisions(name, filter)
|
||||
self:checkHitboxCollisionsAtPoint(name, self.x, self.y, self.z, filter)
|
||||
end
|
||||
|
|
|
@ -3,7 +3,7 @@ PhysicalActor = Object:extend()
|
|||
-- PHYSICS FUNCTIONS
|
||||
-- Raw implementation of everything common in physics
|
||||
|
||||
function PhysicalActor:initPhysics(x, y, z, w, h, d, isSolid)
|
||||
function PhysicalActor:initPhysics(hitboxObj, x, y, z, w, h, d, isSolid)
|
||||
self:setCoordinate(x, y, z)
|
||||
|
||||
self.isSolid = isSolid or false
|
||||
|
@ -21,7 +21,7 @@ function PhysicalActor:initPhysics(x, y, z, w, h, d, isSolid)
|
|||
self.zfrc = 0
|
||||
|
||||
self:initGravity()
|
||||
self:initHitboxes()
|
||||
self:initHitboxes(hitboxObj)
|
||||
|
||||
self:setBounceFactor()
|
||||
self:setFilter()
|
||||
|
@ -135,7 +135,8 @@ end
|
|||
-- HITBOX FUNCTIONS
|
||||
-- All functions to handle hitboxes
|
||||
|
||||
function PhysicalActor:initHitboxes()
|
||||
function PhysicalActor:initHitboxes(hitboxObj)
|
||||
self.Hitbox = hitboxObj
|
||||
self:initMainHitbox()
|
||||
|
||||
self.hitboxes = {}
|
||||
|
@ -174,6 +175,39 @@ function PhysicalActor:getHitboxList(animation, frame)
|
|||
end
|
||||
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 anim = animationID or "null"
|
||||
local frame = frameID or 0
|
||||
local id = hitboxID or 0
|
||||
|
||||
if (type == "main") then
|
||||
self.mainHitbox:setFromData(box, sx, sy)
|
||||
else
|
||||
local hitboxName = anim .. frame .. type .. id
|
||||
self:addHitbox(hitboxName, type, box, sx, sy, isSolid)
|
||||
return hitboxName
|
||||
end
|
||||
end
|
||||
|
||||
function PhysicalActor:initMainHitbox()
|
||||
self.mainHitbox = self.Hitbox(self, self.type, self:packForHitbox(), 0, 0, self.isSolid)
|
||||
self.mainHitbox:advertiseAsMainHitbox()
|
||||
end
|
||||
|
||||
function PhysicalActor:addHitbox(name, type, data, sx, sy, isSolid)
|
||||
if (self.hitboxes[name] ~= nil) then
|
||||
core.debug:logWarn("PhysicalActor", "the hitbox " .. name .. " already exists")
|
||||
else
|
||||
local hitbox = self.Hitbox(self, type, data, sx, sy, isSolid)
|
||||
self.hitboxes[name] = hitbox
|
||||
return hitbox
|
||||
end
|
||||
end
|
||||
|
||||
function PhysicalActor:updateHitboxes()
|
||||
if (self.hitboxList ~= nil) then
|
||||
self:purgeHitbox()
|
||||
|
|
Loading…
Reference in a new issue