-
Notifications
You must be signed in to change notification settings - Fork 0
/
animation.lua
executable file
·43 lines (37 loc) · 1.3 KB
/
animation.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
local self = {}
-- replay is for play an animation not just once
function self.create(sprite, numFrames, xOrigin, yOrigin, w, h, fps)
local animation = {}
animation.sprite = sprite
animation.numFrames = numFrames
animation.w = w
animation.h = h
animation.fps = fps
animation.timer = 0
animation.currentFrame = 1
animation.quads = {}
for iFrame = 1, numFrames do
local quad = love.graphics.newQuad(xOrigin + (iFrame-1)*w, yOrigin, w, h, sprite:getDimensions())
table.insert(animation.quads, quad)
end
return animation
end
function self.load()
end
function self.update(animation, dt)
animation.timer = animation.timer + dt
if (animation.timer >= 1/animation.fps) then
animation.timer = 0
animation.currentFrame = animation.currentFrame + 1
if (animation.currentFrame > animation.numFrames) and animation.replay == "yes" then
animation.currentFrame = 1
elseif (animation.currentFrame > animation.numFrames) and animation.replay ~= "yes" then
animation.currentFrame = animation.numFrames
end
end
end
function self.draw(animation, x, y)
love.graphics.setColor(1, 1, 1)
love.graphics.draw(animation.sprite, animation.quads[animation.currentFrame], x, y, 0, 1, 1, animation.w/2, animation.h/2)
end
return self