sonic-radiance/sonic-radiance.love/scenes/overworld/charsetmanager.lua

72 lines
1.9 KiB
Lua
Raw Permalink Normal View History

2020-08-01 18:54:12 +02:00
local Charset = Object:extend()
local folder = "assets/sprites/charset/"
local animation = {1, 2, 3, 2}
local directionList = {"down", "right", "up", "left"}
function Charset:new(scene)
self.char = {}
self.list = {}
for i=1, 2 do
for j=1, 4 do
local id = ((i-1)*4) + j
self.char[id] = self:addChar(i, j)
end
end
self.currentFrame = 0
end
function Charset:update(dt)
self.currentFrame = ((self.currentFrame + (dt*5)) % 4)
end
function Charset:addChar(ii, jj)
local charx, chary = (jj-1)*(24*3), (ii-1)*(32*4)
local char = {}
for i=1, 4 do
animatedDirection = {}
local running = {}
for j=1, 3 do
local x, y = charx + ((j-1)*24), (chary + (i-1)*32)
--print(x, y)
running[j] = love.graphics.newQuad(x, y, 24, 32, 24*12, 32*8)
end
local direction = directionList[i]
char[direction] = running
end
return char
end
function Charset:addTexture(charsetName)
self.list[charsetName] = love.graphics.newImage(folder .. charsetName .. ".png")
end
function Charset:getRunningFrame(charID, direction)
local char = self.char[charID]
local animatedDirection = char[direction]
local fakeFrame = math.min(math.floor(self.currentFrame) + 1, 4)
local trueFrame = animation[fakeFrame]
return animatedDirection[trueFrame]
end
function Charset:getStandingFrame(charID, direction)
local char = self.char[charID]
local animatedDirection = char[direction]
return animatedDirection[2]
end
function Charset:draw(charsetName, charID, direction, x, y)
local drawable = self.list[charsetName]
local quad = self:getRunningFrame(charID, direction)
love.graphics.draw(drawable, quad, math.floor(x), math.floor(y), 0, 1, 1, 4, 16)
end
function Charset:drawStanding(charsetName, charID, direction, x, y)
local drawable = self.list[charsetName]
local quad = self:getStandingFrame(charID, direction)
2020-08-01 20:49:06 +02:00
love.graphics.draw(drawable, quad, math.floor(x), math.floor(y), 0, 1, 1, 4, 16)
2020-08-01 18:54:12 +02:00
end
return Charset