From 2ede81dc8d77926d8e247e8adf092dea3b080a79 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sat, 10 Apr 2021 12:24:56 +0200 Subject: [PATCH] improvement: refactor charset manager --- .../scenes/overworld/actors/parent.lua | 2 +- .../scenes/overworld/charsetmanager.lua | 46 ++++++++++--------- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/sonic-radiance.love/scenes/overworld/actors/parent.lua b/sonic-radiance.love/scenes/overworld/actors/parent.lua index f808288..fe4c4a8 100644 --- a/sonic-radiance.love/scenes/overworld/actors/parent.lua +++ b/sonic-radiance.love/scenes/overworld/actors/parent.lua @@ -48,7 +48,7 @@ function Parent:drawCharset(charset, charId) if (not self.isTurning) then if (self:isMoving() and (not self.cantWalk)) then - self.charsetManager:draw(self.charset, self.charId, self.charDir, x, y - z, self.isFast) + self.charsetManager:drawMoving(self.charset, self.charId, self.charDir, x, y - z, self.isFast) else self.charsetManager:drawStanding(self.charset, self.charId, self.charDir, x, y - z, self.isFast) end diff --git a/sonic-radiance.love/scenes/overworld/charsetmanager.lua b/sonic-radiance.love/scenes/overworld/charsetmanager.lua index 1a532d6..ca12011 100644 --- a/sonic-radiance.love/scenes/overworld/charsetmanager.lua +++ b/sonic-radiance.love/scenes/overworld/charsetmanager.lua @@ -8,6 +8,8 @@ local CHARWIDTH = 38 local CHARHEIGHT = 48 local FAST_BOOST = 2.5 +local FRAME_STANDING = 2 + function Charset:new(scene) self.char = {} self.list = {} @@ -57,43 +59,45 @@ function Charset:addTexture(charsetName) self.list[charsetName] = love.graphics.newImage(folder .. charsetName .. ".png") end -function Charset:getRunningFrame(charID, direction, isFast) +-- FRAME FUNCTIONS +-- Easily get frame + +function Charset:getCurrentFrame(isFast) local frame = self.currentFrame if (isFast == true) then frame = self.fastFrame end + return math.min(math.floor(frame) + 1, 4) +end + +function Charset:getQuad(frame, charID, direction) local char = self.char[charID] local animatedDirection = char[direction] - local fakeFrame = math.min(math.floor(frame) + 1, 4) - local trueFrame = animation[fakeFrame] + local trueFrame = animation[frame] return animatedDirection[trueFrame] end -function Charset:getStandingFrame(charID, direction) - local char = self.char[charID] - local animatedDirection = char[direction] - return animatedDirection[2] +-- DRAW FUNCTIONS +-- Draw the charset in various situations +function Charset:draw(charsetName, charID, direction, frame, x, y) + local drawable = self:getTexture(charsetName) + local quad = self:getQuad(frame, charID, direction) + love.graphics.draw(drawable, quad, math.floor(x), math.floor(y), 0, 1, 1, 11, 32) end -function Charset:draw(charsetName, charID, direction, x, y, isFast) - local drawable = self:getTexture(charsetName) - local quad = self:getRunningFrame(charID, direction, isFast) - love.graphics.draw(drawable, quad, math.floor(x), math.floor(y), 0, 1, 1, 11, 32) +function Charset:drawMoving(charsetName, charID, direction, x, y, isFast) + local frame = self:getCurrentFrame(isFast) + self:draw(charsetName, charID, direction, frame, x, y) end function Charset:drawStanding(charsetName, charID, direction, x, y) - local drawable = self:getTexture(charsetName) - local quad = self:getStandingFrame(charID, direction) - love.graphics.draw(drawable, quad, math.floor(x), math.floor(y), 0, 1, 1, 11, 32) + self:draw(charsetName, charID, direction, FRAME_STANDING, x, y) end -function Charset:drawTurning(charsetName, charID, x, y, isFast) - local frame = self.currentFrame - if (isFast == true) then - frame = self.fastFrame - end - local dir = math.min(math.floor(frame) + 1, 4) - return self:drawStanding(charsetName, charID, directionList[dir], x, y) +function Charset:drawTurning(charsetName, charID, x, y, isFast, frame) + local frame = frame or FRAME_STANDING + local dir = self:getCurrentFrame(isFast) + return self:draw(charsetName, charID, directionList[dir], frame, x, y) end return Charset