feat(sprite): add hitbox loading from sprites areas
This commit is contained in:
parent
ae089b58a5
commit
28305faf42
5 changed files with 57 additions and 0 deletions
|
@ -127,6 +127,21 @@ function Sprite:getDimensions()
|
|||
return self.tileset:getDimensions()
|
||||
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 sprites using these functions
|
||||
|
||||
|
|
|
@ -119,6 +119,10 @@ function Animator:getDimensions()
|
|||
return self.sprite:getDimensions()
|
||||
end
|
||||
|
||||
function Animator:getAreas()
|
||||
return self.sprite:getAreas(self.currentAnimation, self.frame)
|
||||
end
|
||||
|
||||
-- CALLBACK FUNCTIONS
|
||||
-- Handle getting a calback from the animation system
|
||||
|
||||
|
|
|
@ -69,6 +69,9 @@ end
|
|||
|
||||
function Actor:draw()
|
||||
self:_drawVisual()
|
||||
if (self.def.drawHitboxes == true) then
|
||||
self:_drawHitboxes()
|
||||
end
|
||||
end
|
||||
|
||||
-- Callbacks
|
||||
|
|
|
@ -203,6 +203,17 @@ function Physics:_initMainHitbox()
|
|||
self.mainHitbox:advertiseAsMainHitbox()
|
||||
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)
|
||||
if (self.hitboxes[name] ~= nil) then
|
||||
core.debug:logWarn("PhysicalActor", "the hitbox " .. name .. " already exists")
|
||||
|
@ -250,4 +261,13 @@ function Physics:applyHitboxCollisions(name, filter)
|
|||
return cols, colNumber
|
||||
end
|
||||
|
||||
-- Debug
|
||||
|
||||
function Physics:_drawHitboxes()
|
||||
self.mainHitbox:draw()
|
||||
for name, value in pairs(self.hitboxes) do
|
||||
value:draw()
|
||||
end
|
||||
end
|
||||
|
||||
return Physics
|
|
@ -45,6 +45,11 @@ function Sprite:getAnimator()
|
|||
end
|
||||
|
||||
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)
|
||||
end
|
||||
|
||||
|
@ -55,6 +60,16 @@ end
|
|||
function Sprite:update(dt)
|
||||
if (self.spriteClone ~= nil) then
|
||||
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
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue