feat(sprite): add hitbox loading from sprites areas

This commit is contained in:
Kazhnuz 2024-10-31 09:43:43 +01:00
parent ae089b58a5
commit 28305faf42
5 changed files with 57 additions and 0 deletions

View file

@ -127,6 +127,21 @@ function Sprite:getDimensions()
return self.tileset:getDimensions() return self.tileset:getDimensions()
end end
function Sprite:getAreas(animation, frame)
local animationData = self:getAnimationData(animation)
if (animationData.areas == nil) then
return nil
end
local frameList = nil
for i = 1, (frame+1), 1 do
local frameData = animationData.areas["frame" .. i]
if (frameData ~= nil) then
frameList = frameData
end
end
return frameList
end
-- DRAW FUNCTIONS -- DRAW FUNCTIONS
-- Draw sprites using these functions -- Draw sprites using these functions

View file

@ -119,6 +119,10 @@ function Animator:getDimensions()
return self.sprite:getDimensions() return self.sprite:getDimensions()
end end
function Animator:getAreas()
return self.sprite:getAreas(self.currentAnimation, self.frame)
end
-- CALLBACK FUNCTIONS -- CALLBACK FUNCTIONS
-- Handle getting a calback from the animation system -- Handle getting a calback from the animation system

View file

@ -69,6 +69,9 @@ end
function Actor:draw() function Actor:draw()
self:_drawVisual() self:_drawVisual()
if (self.def.drawHitboxes == true) then
self:_drawHitboxes()
end
end end
-- Callbacks -- Callbacks

View file

@ -203,6 +203,17 @@ function Physics:_initMainHitbox()
self.mainHitbox:advertiseAsMainHitbox() self.mainHitbox:advertiseAsMainHitbox()
end end
function Physics:setHitboxes(areas)
self:purgeHitbox()
for i, area in ipairs(areas) do
if (area.type == "main") then
self.mainHitbox:modify(area.box.position, area.box.dimensions)
else
self:addHitbox(area.type .. "" .. i, area.type, area.box, self.visual.scale, area.isSolid)
end
end
end
function Physics:addHitbox(name, type, data, scale, isSolid) function Physics:addHitbox(name, type, data, scale, isSolid)
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")
@ -250,4 +261,13 @@ function Physics:applyHitboxCollisions(name, filter)
return cols, colNumber return cols, colNumber
end end
-- Debug
function Physics:_drawHitboxes()
self.mainHitbox:draw()
for name, value in pairs(self.hitboxes) do
value:draw()
end
end
return Physics return Physics

View file

@ -45,6 +45,11 @@ function Sprite:getAnimator()
end end
function Sprite:changeAnimation(animation, restart) function Sprite:changeAnimation(animation, restart)
if (animation ~= self:getAnimator():getCurrentAnimation()) then
if (self.getHitboxes) then
self.owner:purgeHitbox()
end
end
self:getAnimator():changeAnimation(animation, restart) self:getAnimator():changeAnimation(animation, restart)
end end
@ -55,6 +60,16 @@ end
function Sprite:update(dt) function Sprite:update(dt)
if (self.spriteClone ~= nil) then if (self.spriteClone ~= nil) then
self.spriteClone:update(dt) self.spriteClone:update(dt)
self:sendHitboxes()
end
end
function Sprite:sendHitboxes()
if (self.getHitboxes) then
local areas = self:getAnimator():getAreas()
if (areas ~= nil) then
self.owner:setHitboxes(areas)
end
end end
end end