improvement: refactor charset manager
This commit is contained in:
parent
fc1eaa0460
commit
2ede81dc8d
2 changed files with 26 additions and 22 deletions
|
@ -48,7 +48,7 @@ function Parent:drawCharset(charset, charId)
|
||||||
|
|
||||||
if (not self.isTurning) then
|
if (not self.isTurning) then
|
||||||
if (self:isMoving() and (not self.cantWalk)) 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
|
else
|
||||||
self.charsetManager:drawStanding(self.charset, self.charId, self.charDir, x, y - z, self.isFast)
|
self.charsetManager:drawStanding(self.charset, self.charId, self.charDir, x, y - z, self.isFast)
|
||||||
end
|
end
|
||||||
|
|
|
@ -8,6 +8,8 @@ local CHARWIDTH = 38
|
||||||
local CHARHEIGHT = 48
|
local CHARHEIGHT = 48
|
||||||
local FAST_BOOST = 2.5
|
local FAST_BOOST = 2.5
|
||||||
|
|
||||||
|
local FRAME_STANDING = 2
|
||||||
|
|
||||||
function Charset:new(scene)
|
function Charset:new(scene)
|
||||||
self.char = {}
|
self.char = {}
|
||||||
self.list = {}
|
self.list = {}
|
||||||
|
@ -57,43 +59,45 @@ function Charset:addTexture(charsetName)
|
||||||
self.list[charsetName] = love.graphics.newImage(folder .. charsetName .. ".png")
|
self.list[charsetName] = love.graphics.newImage(folder .. charsetName .. ".png")
|
||||||
end
|
end
|
||||||
|
|
||||||
function Charset:getRunningFrame(charID, direction, isFast)
|
-- FRAME FUNCTIONS
|
||||||
|
-- Easily get frame
|
||||||
|
|
||||||
|
function Charset:getCurrentFrame(isFast)
|
||||||
local frame = self.currentFrame
|
local frame = self.currentFrame
|
||||||
if (isFast == true) then
|
if (isFast == true) then
|
||||||
frame = self.fastFrame
|
frame = self.fastFrame
|
||||||
end
|
end
|
||||||
|
return math.min(math.floor(frame) + 1, 4)
|
||||||
|
end
|
||||||
|
|
||||||
|
function Charset:getQuad(frame, charID, direction)
|
||||||
local char = self.char[charID]
|
local char = self.char[charID]
|
||||||
local animatedDirection = char[direction]
|
local animatedDirection = char[direction]
|
||||||
local fakeFrame = math.min(math.floor(frame) + 1, 4)
|
local trueFrame = animation[frame]
|
||||||
local trueFrame = animation[fakeFrame]
|
|
||||||
return animatedDirection[trueFrame]
|
return animatedDirection[trueFrame]
|
||||||
end
|
end
|
||||||
|
|
||||||
function Charset:getStandingFrame(charID, direction)
|
-- DRAW FUNCTIONS
|
||||||
local char = self.char[charID]
|
-- Draw the charset in various situations
|
||||||
local animatedDirection = char[direction]
|
function Charset:draw(charsetName, charID, direction, frame, x, y)
|
||||||
return animatedDirection[2]
|
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
|
end
|
||||||
|
|
||||||
function Charset:draw(charsetName, charID, direction, x, y, isFast)
|
function Charset:drawMoving(charsetName, charID, direction, x, y, isFast)
|
||||||
local drawable = self:getTexture(charsetName)
|
local frame = self:getCurrentFrame(isFast)
|
||||||
local quad = self:getRunningFrame(charID, direction, isFast)
|
self:draw(charsetName, charID, direction, frame, x, y)
|
||||||
love.graphics.draw(drawable, quad, math.floor(x), math.floor(y), 0, 1, 1, 11, 32)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function Charset:drawStanding(charsetName, charID, direction, x, y)
|
function Charset:drawStanding(charsetName, charID, direction, x, y)
|
||||||
local drawable = self:getTexture(charsetName)
|
self:draw(charsetName, charID, direction, FRAME_STANDING, x, y)
|
||||||
local quad = self:getStandingFrame(charID, direction)
|
|
||||||
love.graphics.draw(drawable, quad, math.floor(x), math.floor(y), 0, 1, 1, 11, 32)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function Charset:drawTurning(charsetName, charID, x, y, isFast)
|
function Charset:drawTurning(charsetName, charID, x, y, isFast, frame)
|
||||||
local frame = self.currentFrame
|
local frame = frame or FRAME_STANDING
|
||||||
if (isFast == true) then
|
local dir = self:getCurrentFrame(isFast)
|
||||||
frame = self.fastFrame
|
return self:draw(charsetName, charID, directionList[dir], frame, x, y)
|
||||||
end
|
|
||||||
local dir = math.min(math.floor(frame) + 1, 4)
|
|
||||||
return self:drawStanding(charsetName, charID, directionList[dir], x, y)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return Charset
|
return Charset
|
||||||
|
|
Loading…
Reference in a new issue