-
Notifications
You must be signed in to change notification settings - Fork 0
/
Animation.lua
38 lines (34 loc) · 1.22 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
-- Animation base class:
-- takes a list of frames and returns the current frame on 'draw'
-- 'frames' can be a 1-indexed array of any type
-- 'frameTime' is the time in seconds between frames
return Class {
init = function(self, frames, frameTime)
self.loop = true
self.frames = frames
self.frameTime = frameTime
self:reset()
end,
-- reset animation to startFrame or first frame if no startFrame is supplied
reset = function(self, startFrame, loop)
if loop == false then self.loop = false else self.loop = true end
self.timer = self.frameTime
self.currentFrame = (startFrame and startFrame >= 1) and startFrame or 1
end,
update = function(self, dt)
self.timer = self.timer - dt
if self.timer <= 0 then
self.timer = self.timer + self.frameTime
if self.currentFrame < #self.frames then
self.currentFrame = self.currentFrame + 1
else
if self.loop then self.currentFrame = 1 end
return true
end
end
return false
end,
draw = function(self)
return self.frames[self.currentFrame]
end
}