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"}
|
|
|
|
|
2020-08-02 21:55:37 +02:00
|
|
|
local CHARWIDTH = 38
|
|
|
|
local CHARHEIGHT = 48
|
2021-04-10 11:59:32 +02:00
|
|
|
local FAST_BOOST = 2.5
|
2020-08-02 21:55:37 +02:00
|
|
|
|
2021-04-10 12:24:56 +02:00
|
|
|
local FRAME_STANDING = 2
|
|
|
|
|
2020-08-01 18:54:12 +02:00
|
|
|
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
|
2021-04-10 11:59:32 +02:00
|
|
|
self.fastFrame = 0
|
2020-08-01 18:54:12 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function Charset:update(dt)
|
2021-04-02 19:33:56 +02:00
|
|
|
if (core.screen:isActive()) then
|
|
|
|
self.currentFrame = ((self.currentFrame + (dt*5)) % 4)
|
2021-04-10 11:59:32 +02:00
|
|
|
self.fastFrame = ((self.fastFrame + (dt*5*FAST_BOOST)) % 4)
|
2021-04-02 19:33:56 +02:00
|
|
|
end
|
2020-08-01 18:54:12 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function Charset:addChar(ii, jj)
|
2020-08-02 21:55:37 +02:00
|
|
|
local charx, chary = (jj-1)*(CHARWIDTH*3), (ii-1)*(CHARHEIGHT*4)
|
2020-08-01 18:54:12 +02:00
|
|
|
local char = {}
|
|
|
|
for i=1, 4 do
|
2020-09-13 16:57:06 +02:00
|
|
|
local animatedDirection = {}
|
2020-08-01 18:54:12 +02:00
|
|
|
local running = {}
|
|
|
|
for j=1, 3 do
|
2020-08-02 21:55:37 +02:00
|
|
|
local x, y = charx + ((j-1)*CHARWIDTH), (chary + (i-1)*CHARHEIGHT)
|
2020-08-01 18:54:12 +02:00
|
|
|
--print(x, y)
|
2020-08-02 21:55:37 +02:00
|
|
|
running[j] = love.graphics.newQuad(x, y, CHARWIDTH, CHARHEIGHT, CHARWIDTH*12, CHARHEIGHT*8)
|
2020-08-01 18:54:12 +02:00
|
|
|
end
|
|
|
|
local direction = directionList[i]
|
|
|
|
char[direction] = running
|
|
|
|
end
|
|
|
|
return char
|
|
|
|
end
|
|
|
|
|
2021-03-21 15:56:35 +01:00
|
|
|
function Charset:getTexture(charsetName)
|
|
|
|
if (self.list[charsetName] == nil) then
|
|
|
|
self:addTexture(charsetName)
|
|
|
|
end
|
|
|
|
return self.list[charsetName]
|
|
|
|
end
|
|
|
|
|
2020-08-01 18:54:12 +02:00
|
|
|
function Charset:addTexture(charsetName)
|
|
|
|
self.list[charsetName] = love.graphics.newImage(folder .. charsetName .. ".png")
|
|
|
|
end
|
|
|
|
|
2021-04-10 12:24:56 +02:00
|
|
|
-- FRAME FUNCTIONS
|
|
|
|
-- Easily get frame
|
|
|
|
|
|
|
|
function Charset:getCurrentFrame(isFast)
|
2021-04-10 11:59:32 +02:00
|
|
|
local frame = self.currentFrame
|
|
|
|
if (isFast == true) then
|
|
|
|
frame = self.fastFrame
|
|
|
|
end
|
2021-04-10 12:24:56 +02:00
|
|
|
return math.min(math.floor(frame) + 1, 4)
|
2020-08-01 18:54:12 +02:00
|
|
|
end
|
|
|
|
|
2021-04-10 12:24:56 +02:00
|
|
|
function Charset:getQuad(frame, charID, direction)
|
2020-08-01 18:54:12 +02:00
|
|
|
local char = self.char[charID]
|
|
|
|
local animatedDirection = char[direction]
|
2021-04-10 12:24:56 +02:00
|
|
|
local trueFrame = animation[frame]
|
|
|
|
return animatedDirection[trueFrame]
|
2020-08-01 18:54:12 +02:00
|
|
|
end
|
|
|
|
|
2021-04-10 12:24:56 +02:00
|
|
|
-- DRAW FUNCTIONS
|
|
|
|
-- Draw the charset in various situations
|
|
|
|
function Charset:draw(charsetName, charID, direction, frame, x, y)
|
2021-03-21 15:56:35 +01:00
|
|
|
local drawable = self:getTexture(charsetName)
|
2021-04-10 12:24:56 +02:00
|
|
|
local quad = self:getQuad(frame, charID, direction)
|
2020-08-02 21:55:37 +02:00
|
|
|
love.graphics.draw(drawable, quad, math.floor(x), math.floor(y), 0, 1, 1, 11, 32)
|
2020-08-01 18:54:12 +02:00
|
|
|
end
|
|
|
|
|
2021-04-10 12:24:56 +02:00
|
|
|
function Charset:drawMoving(charsetName, charID, direction, x, y, isFast)
|
|
|
|
local frame = self:getCurrentFrame(isFast)
|
|
|
|
self:draw(charsetName, charID, direction, frame, x, y)
|
|
|
|
end
|
|
|
|
|
2020-08-01 18:54:12 +02:00
|
|
|
function Charset:drawStanding(charsetName, charID, direction, x, y)
|
2021-04-10 12:24:56 +02:00
|
|
|
self:draw(charsetName, charID, direction, FRAME_STANDING, x, y)
|
2020-08-01 18:54:12 +02:00
|
|
|
end
|
|
|
|
|
2021-04-10 12:24:56 +02:00
|
|
|
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)
|
2021-03-22 21:04:55 +01:00
|
|
|
end
|
|
|
|
|
2020-08-01 18:54:12 +02:00
|
|
|
return Charset
|