feat(world): add a frame hitbox data structure to process

It'll allow us to create a way to process everyframe of an animation and 
change hitbox according to the animation.

It should especially simplify the creation of battle system.
This commit is contained in:
Kazhnuz 2019-06-23 10:19:01 +02:00
parent f88c7f49e4
commit a3b6bcd499
3 changed files with 36 additions and 7 deletions

View file

@ -9,6 +9,8 @@ function Player:new(world, x, y, id)
self.isPunching = false self.isPunching = false
self.direction = 1 self.direction = 1
self.punchName = ""
end end
function Player:updateStart(dt) function Player:updateStart(dt)
@ -31,18 +33,14 @@ function Player:updateStart(dt)
if self.keys["A"].isDown then if self.keys["A"].isDown then
self.isPunching = true self.isPunching = true
if (self.direction == 1) then self.punchName = self:addHitboxFromFrameData({"punch", 16, 6, 12, 12, false})
self:addHitbox("punch", "punch", 16, 6, 12, 12, false)
else
self:addHitbox("punch", "punch", -12, 6, 12, 12, false)
end
else else
self.isPunching = false self.isPunching = false
self:removeHitbox("punch") self:removeHitbox(self.punchName)
end end
if (self.isPunching) then if (self.isPunching) then
self:checkHitboxCollisions("punch") self:checkHitboxCollisions(self.punchName)
end end
if self.keys["start"].isPressed then if self.keys["start"].isPressed then

View file

@ -187,6 +187,33 @@ function Actor2D:initHitboxes(w, h)
self.hitboxes = {} self.hitboxes = {}
end end
function Actor2D:addHitboxFromFrameData(framedata, animationID, frameID, hitboxID)
local sx, sy = self:getSpriteScalling()
local type = framedata[1]
local ox = framedata[2]
local oy = framedata[3]
local w = framedata[4]
local h = framedata[5]
local isSolid = framedata[6] or false
local anim = animationID or "null"
local frame = frameID or 0
local id = hitboxID or 0
if (sx < 0) then
ox = self.w - ox - w
end
if (sy < 0) then
oy = self.h - oy - h
end
if (type == "main") then
self.mainHitbox:modify(ox, oy, w, h)
else
local hitboxName = anim .. frame .. type .. id
self:addHitbox(hitboxName, type, ox, oy, w, h, isSolid)
return hitboxName
end
end
function Actor2D:initMainHitbox() function Actor2D:initMainHitbox()
self.mainHitbox = Hitbox(self, self.type, 0, 0, self.w, self.h, self.isSolid) self.mainHitbox = Hitbox(self, self.type, 0, 0, self.w, self.h, self.isSolid)
self.world:registerBody(self.mainHitbox) self.world:registerBody(self.mainHitbox)

View file

@ -236,6 +236,10 @@ function BaseActor:setSpriteScallingY(sy)
self.sprite.sy = sy self.sprite.sy = sy
end end
function BaseActor:getSpriteScalling()
return self.sprite.sx, self.sprite.sy
end
function BaseActor:drawSprite(x, y, r, sx, sy, ox, oy, kx, ky) function BaseActor:drawSprite(x, y, r, sx, sy, ox, oy, kx, ky)
if (self.sprite.name ~= nil) then if (self.sprite.name ~= nil) then
local x = x + self.sprite.ox local x = x + self.sprite.ox