-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanimation.lua
67 lines (52 loc) · 1.43 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
playdate.graphics.animation = playdate.graphics.animation or {}
local module = {}
playdate.graphics.animation.loop = module
local meta = {}
meta.__index = meta
module.__index = meta
function module.new(delay, imageTable, shouldLoop)
local animation = setmetatable({}, meta)
animation.startFrame = 1
animation.endFrame = 1
animation.frame = 1
animation.step = 1
animation.pause = false
animation._startTime = playdate.getCurrentTimeMilliseconds()
animation.delay = delay or 100
animation._imageTable = imageTable
animation.shouldLoop = shouldLoop
if imageTable then
animation.endFrame = imageTable:getLength()
end
return animation
end
function meta:image(it)
return self._imageTable
end
function meta:setImageTable(it)
self._imageTable = it
end
function meta:isValid()
if self.shouldLoop then
return true
end
if self.frame > self.endFrame then
return false
end
return true
end
function meta:draw(x, y, flip)
if not self.pause then
local elapsedTime = playdate.getCurrentTimeMilliseconds() - self._startTime
self.frame = self.startFrame + math.floor(elapsedTime / self.delay) * self.step
if self.frame > self.endFrame then
if self.shouldLoop then
self.frame = self.startFrame
self._startTime = playdate.getCurrentTimeMilliseconds()
else
self.frame = self.endFrame
end
end
end
self._imageTable:drawImage(self.frame, x, y, flip)
end