improvement: refactor charset manager

This commit is contained in:
Kazhnuz 2021-04-10 12:24:56 +02:00
parent fc1eaa0460
commit 2ede81dc8d
2 changed files with 26 additions and 22 deletions

View file

@ -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

View file

@ -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