From a3b6bcd4995257e872777162050bcec41b372086 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sun, 23 Jun 2019 10:19:01 +0200 Subject: [PATCH] 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. --- examples/gameplay/plateform/actors/player.lua | 12 ++++----- gamecore/modules/world/actors/actor2D.lua | 27 +++++++++++++++++++ gamecore/modules/world/actors/baseactor.lua | 4 +++ 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/examples/gameplay/plateform/actors/player.lua b/examples/gameplay/plateform/actors/player.lua index 80eb06b..0c93e62 100644 --- a/examples/gameplay/plateform/actors/player.lua +++ b/examples/gameplay/plateform/actors/player.lua @@ -9,6 +9,8 @@ function Player:new(world, x, y, id) self.isPunching = false self.direction = 1 + + self.punchName = "" end function Player:updateStart(dt) @@ -31,18 +33,14 @@ function Player:updateStart(dt) if self.keys["A"].isDown then self.isPunching = true - if (self.direction == 1) then - self:addHitbox("punch", "punch", 16, 6, 12, 12, false) - else - self:addHitbox("punch", "punch", -12, 6, 12, 12, false) - end + self.punchName = self:addHitboxFromFrameData({"punch", 16, 6, 12, 12, false}) else self.isPunching = false - self:removeHitbox("punch") + self:removeHitbox(self.punchName) end if (self.isPunching) then - self:checkHitboxCollisions("punch") + self:checkHitboxCollisions(self.punchName) end if self.keys["start"].isPressed then diff --git a/gamecore/modules/world/actors/actor2D.lua b/gamecore/modules/world/actors/actor2D.lua index 0ffeffd..268af8a 100644 --- a/gamecore/modules/world/actors/actor2D.lua +++ b/gamecore/modules/world/actors/actor2D.lua @@ -187,6 +187,33 @@ function Actor2D:initHitboxes(w, h) self.hitboxes = {} 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() self.mainHitbox = Hitbox(self, self.type, 0, 0, self.w, self.h, self.isSolid) self.world:registerBody(self.mainHitbox) diff --git a/gamecore/modules/world/actors/baseactor.lua b/gamecore/modules/world/actors/baseactor.lua index 5512449..ac4ce59 100644 --- a/gamecore/modules/world/actors/baseactor.lua +++ b/gamecore/modules/world/actors/baseactor.lua @@ -236,6 +236,10 @@ function BaseActor:setSpriteScallingY(sy) self.sprite.sy = sy end +function BaseActor:getSpriteScalling() + return self.sprite.sx, self.sprite.sy +end + function BaseActor:drawSprite(x, y, r, sx, sy, ox, oy, kx, ky) if (self.sprite.name ~= nil) then local x = x + self.sprite.ox