forked from kikito/anim8
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanim8.lua
More file actions
259 lines (214 loc) · 7.65 KB
/
anim8.lua
File metadata and controls
259 lines (214 loc) · 7.65 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
-- anim8 v1.1.1 - 2012-06
-- Copyright (c) 2011 Enrique García Cota
-- 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 Grid = {}
local _frames = {}
local function assertPositiveInteger(value, name)
if type(value) ~= 'number' then error(("%s should be a number, was %q"):format(name, tostring(value))) end
if value < 1 then error(("%s should be a positive number, was %d"):format(name, value)) end
if value ~= math.floor(value) then error(("%s should be an integer, was %d"):format(name, value)) end
end
local function createFrame(self, x, y)
local fw, fh = self.frameWidth, self.frameHeight
return love.graphics.newQuad(
self.left + (x-1) * fw + x * self.border,
self.top + (y-1) * fh + y * self.border,
fw,
fh,
self.imageWidth,
self.imageHeight
)
end
local function getGridKey(...)
return table.concat( {...} ,'-' )
end
local function getOrCreateFrame(self, x, y)
if x < 1 or x > self.width or y < 1 or y > self.height then
error(("There is no frame for x=%d, y=%d"):format(x, y))
end
local key = self._key
_frames[key] = _frames[key] or {}
_frames[key][x] = _frames[key][x] or {}
_frames[key][x][y] = _frames[key][x][y] or createFrame(self, x, y)
return _frames[key][x][y]
end
local function parseInterval(str)
str = str:gsub(' ', '')
local min, max = str:match("^(%d+)-(%d+)$")
if not min then
min = str:match("^%d+$")
max = min
end
assert(min and max, ("Could not parse interval from %q"):format(str))
min, max = tonumber(min), tonumber(max)
local step = min <= max and 1 or -1
return min, max, step
end
local function parseIntervals(str)
local left, right = str:match("(.+),(.+)")
assert(left and right, ("Could not parse intervals from %q"):format(str))
local minx, maxx, stepx = parseInterval(left)
local miny, maxy, stepy = parseInterval(right)
return minx, maxx, stepx, miny, maxy, stepy
end
local function parseFrames(self, args, result, position)
local current = args[position]
local kind = type(current)
if kind == 'number' then
result[#result + 1] = getOrCreateFrame(self, current, args[position + 1])
return position + 2
elseif kind == 'string' then
local minx, maxx, stepx, miny, maxy, stepy = parseIntervals(current)
for y = miny, maxy, stepy do
for x = minx, maxx, stepx do
result[#result+1] = getOrCreateFrame(self,x,y)
end
end
return position + 1
else
error(("Invalid type: %q (%s)"):format(kind, tostring(args[position])))
end
end
function Grid:getFrames(...)
local args = {...}
local length = #args
local result = {}
local position = 1
while position <= length do
position = parseFrames(self, args, result, position)
end
return result
end
local Gridmt = {
__index = Grid,
__call = Grid.getFrames
}
local function newGrid(frameWidth, frameHeight, imageWidth, imageHeight, left, top, border)
assertPositiveInteger(frameWidth, "frameWidth")
assertPositiveInteger(frameHeight, "frameHeight")
assertPositiveInteger(imageWidth, "imageWidth")
assertPositiveInteger(imageHeight, "imageHeight")
left = left or 0
top = top or 0
border = border or 0
local key = getGridKey(frameWidth, frameHeight, imageWidth, imageHeight, left, top, border)
local grid = setmetatable(
{ frameWidth = frameWidth,
frameHeight = frameHeight,
imageWidth = imageWidth,
imageHeight = imageHeight,
left = left,
top = top,
border = border,
width = math.floor(imageWidth/frameWidth),
height = math.floor(imageHeight/frameHeight),
_key = key
},
Gridmt
)
return grid
end
-----------------------------------------------------------
local Animation = {}
local function cloneArray(arr)
local result = {}
for i=1,#arr do result[i] = arr[i] end
return result
end
local function parseDelays(delays)
local parsedDelays = {}
local tk,min,max,step
for k,v in pairs(delays) do
tk = type(k)
if tk == "number"
then parsedDelays[k] = v
elseif tk == "string" then
min, max, step = parseInterval(k)
for i = min,max,step do parsedDelays[i] = v end
else
error(("Unexpected delay key: [%s]. Expected a number or a string"):format(tostring(k)))
end
end
return parsedDelays
end
local function repeatValue(value, times)
local result = {}
for i=1,times do result[i] = value end
return result
end
local function createDelays(frames, defaultDelay, delays)
local maxFrames = #frames
local result = repeatValue(defaultDelay, maxFrames)
for i,v in pairs(parseDelays(delays)) do
if i > maxFrames then
error(("The delay value %d is too high; there are only %d frames"):format(i, maxFrames))
end
result[i] = v
end
return result
end
local animationModes = {
loop = function(self) self.position = 1 end,
once = function(self)
self.position = #self.frames
self.status = "finished"
end,
bounce = function(self)
self.direction = self.direction * -1
self.position = self.position + self.direction + self.direction
end
}
local Animationmt = { __index = Animation }
local function newAnimation(mode, frames, defaultDelay, delays)
delays = delays or {}
assert(animationModes[mode], ("%q is not a valid mode"):format(tostring(mode)))
assert(type(defaultDelay) == 'number' and defaultDelay > 0, "defaultDelay must be a positive number" )
assert(type(delays) == 'table', "delays must be a table or nil")
return setmetatable({
mode = mode,
frames = cloneArray(frames),
endSequence = animationModes[mode],
delays = createDelays(frames, defaultDelay, delays),
timer = 0,
position = 1,
direction = 1,
status = "playing"
},
Animationmt
)
end
function Animation:clone()
return newAnimation(self.mode, self.frames, 1, self.delays)
end
function Animation:update(dt)
if self.status ~= "playing" then return end
self.timer = self.timer + dt
while self.timer > self.delays[self.position] do
self.timer = self.timer - self.delays[self.position]
self.position = self.position + self.direction
if self.position < 1 or self.position > #self.frames then
self:endSequence()
end
end
end
function Animation:pause()
self.status = "paused"
end
function Animation:resume()
self.status = "playing"
end
function Animation:gotoFrame(position)
self.position = position
end
function Animation:draw(image, x, y, r, sx, sy, ox, oy)
local frame = self.frames[self.position]
love.graphics.drawq(image, frame, x, y, r, sx, sy, ox, oy)
end
-----------------------------------------------------------
local anim8 = {
newGrid = newGrid,
newAnimation = newAnimation
}
return anim8