-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimate.lua
90 lines (72 loc) · 2.05 KB
/
animate.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
-- animate.lua : helpers for animating
local animate = {}
animate.timer = 'animateTimer'
globals.timer.track(animate.timer)
globals.timer.start(animate.timer)
local function parseOptions(options)
if not options then
options = {}
end
options.speed = options.speed or 1
options.timer = options.timer or animate.timer
options.factor = options.factor or 10
options.frames = options.frames or 2
if options.loop == nil then
options.loop = true
end
return options
end
local function getFrame(animatable)
-- if we're not animating anymore, just return the last frame
if animatable.animating then
local frame =
(math.floor(animatable.options.speed
* globals.timer.getTime(animatable.options.timer))
% animatable.options.frames ) + 1
-- if at the last frame of the loop set not animating anymore if
-- looping is disabled
if animatable.options.loop == false
and frame == animatable.options.frames then
animatable.animating = false
end
return frame
else
return animatable.options.frames
end
end
function animate:new(options)
local obj = {
animating = true,
options = parseOptions(options),
}
self.__index = self
return setmetatable(obj, self)
end
function animate.reset()
globals.timer.reset(animate.timer)
end
function animate:jump(drawable, x, y)
local frame = getFrame(self)
if frame == 1 then
love.graphics.draw(drawable, x, y - self.options.factor)
elseif frame == 2 then
love.graphics.draw(drawable, x, y)
end
end
function animate:blinkText(text)
local frame = getFrame(self)
if frame == 1 then
return ''
elseif frame == 2 then
return text
end
end
function animate:teletype(text)
self.options.frames = string.len(text) + 1
local frame = getFrame(self)
return string.sub(text, 1, frame - 1)
end
function animate:replay()
self.animating = true
end
return animate