-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimation.lua
198 lines (145 loc) · 3.77 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
--[[
MIT License
Copyright (c) 2021 Thales Maia de Moraes
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
]]
local floor = math.floor
local anm = {}
anm.__index = anm
function anm.loadFolder(folder, callback)
local images = {}
local files = love.filesystem.getDirectoryItems(folder)
for _, filename in ipairs(files) do
local file, extension = filename:match("(.*)%.(.*)")
local result = callback(#files, file, extension)
if result then
images[result] = love.graphics.newImage(folder.."/"..file.."."..extension)
end
end
return images
end
function anm:update(dt)
if self.playing then
local dir = self.rewind and -1 or 1
self.time = self.time + self.speed * dt
if self.time >= 1 then
self.frame = self.frame + dir
self.time = self.time - 1
end
local startFrame = self.rewind and 1 or self.range
local endFrame = self.rewind and self.range or 1
if self.frame < 1 or self.frame > self.range then
self.playing = self.loop
self.frame = self.loop and endFrame or startFrame
if self.onFinish then
self:onFinish()
end
end
end
return self
end
function anm:draw(...)
love.graphics.draw(self.images[self.frame], ...)
return self
end
function anm:stop()
self.playing = false
self.time = 0
self.frame = self.rewind and self.range or 1
return self
end
function anm:pause()
self.playing = false
return self
end
function anm:play()
self.playing = true
local startFrame = self.rewind and self.range or 1
local endFrame = self.rewind and 1 or self.range
if self.frame == endFrame then
self.frame = startFrame
end
return self
end
function anm:getFrame()
return self.frame
end
function anm:getSpeed()
return self.speed
end
function anm:getRange()
return self.range
end
function anm:getImage(frame)
return self.images[frame or self.frame]
end
function anm:getAllImages()
return self.frames
end
function anm:isLooping()
return self.loop
end
function anm:isRewinding()
return self.rewind
end
function anm:isPlaying()
return self.playing
end
function anm:setFrame(frame)
self.frame = frame
self.time = 0
return self
end
function anm:setSpeed(speed)
self.speed = speed
return self
end
function anm:setRange(range)
self.range = range
return self
end
function anm:setImage(image, frame)
self.images[frame or self.frame] = image
return self
end
function anm:setAllImages(images)
self.images = images
self.range = #images
return self
end
function anm:setLoop(loop)
self.loop = loop
return self
end
function anm:setRewind(rewind)
self.rewind = rewind
return self
end
function anm.__call(_, imageList, speed, range)
local new = {
images = imageList,
speed = speed or 30,
frame = 1,
time = 0,
playing = false,
rewind = false,
loop = false,
range = range or #imageList
}
return setmetatable(new, anm)
end
return setmetatable({}, anm)