annotate anim8.lua @ 48:eeb43b94f625 default tip

merge
author Makdaam
date Sun, 16 Dec 2012 19:50:44 +0100
parents 716ff40413d2
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
1 -- anim8 v1.1.1 - 2012-06
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
2 -- Copyright (c) 2011 Enrique GarcĂ­a Cota
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
3 -- 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:
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
4 -- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
5 -- 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.
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
6
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
7 local Grid = {}
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
8
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
9 local _frames = {}
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
10
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
11 local function assertPositiveInteger(value, name)
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
12 if type(value) ~= 'number' then error(("%s should be a number, was %q"):format(name, tostring(value))) end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
13 if value < 1 then error(("%s should be a positive number, was %d"):format(name, value)) end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
14 if value ~= math.floor(value) then error(("%s should be an integer, was %d"):format(name, value)) end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
15 end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
16
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
17 local function createFrame(self, x, y)
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
18 local fw, fh = self.frameWidth, self.frameHeight
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
19 return love.graphics.newQuad(
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
20 self.left + (x-1) * fw + x * self.border,
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
21 self.top + (y-1) * fh + y * self.border,
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
22 fw,
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
23 fh,
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
24 self.imageWidth,
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
25 self.imageHeight
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
26 )
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
27 end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
28
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
29 local function getGridKey(...)
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
30 return table.concat( {...} ,'-' )
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
31 end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
32
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
33
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
34 local function getOrCreateFrame(self, x, y)
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
35 if x < 1 or x > self.width or y < 1 or y > self.height then
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
36 error(("There is no frame for x=%d, y=%d"):format(x, y))
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
37 end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
38 local key = self._key
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
39 _frames[key] = _frames[key] or {}
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
40 _frames[key][x] = _frames[key][x] or {}
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
41 _frames[key][x][y] = _frames[key][x][y] or createFrame(self, x, y)
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
42 return _frames[key][x][y]
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
43 end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
44
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
45 local function parseInterval(str)
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
46 str = str:gsub(' ', '')
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
47 local min, max = str:match("^(%d+)-(%d+)$")
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
48 if not min then
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
49 min = str:match("^%d+$")
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
50 max = min
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
51 end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
52 assert(min and max, ("Could not parse interval from %q"):format(str))
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
53 min, max = tonumber(min), tonumber(max)
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
54 local step = min <= max and 1 or -1
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
55 return min, max, step
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
56 end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
57
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
58 local function parseIntervals(str)
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
59 local left, right = str:match("(.+),(.+)")
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
60 assert(left and right, ("Could not parse intervals from %q"):format(str))
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
61 local minx, maxx, stepx = parseInterval(left)
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
62 local miny, maxy, stepy = parseInterval(right)
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
63 return minx, maxx, stepx, miny, maxy, stepy
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
64 end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
65
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
66 local function parseFrames(self, args, result, position)
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
67 local current = args[position]
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
68 local kind = type(current)
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
69
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
70 if kind == 'number' then
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
71
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
72 result[#result + 1] = getOrCreateFrame(self, current, args[position + 1])
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
73 return position + 2
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
74
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
75 elseif kind == 'string' then
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
76
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
77 local minx, maxx, stepx, miny, maxy, stepy = parseIntervals(current)
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
78 for y = miny, maxy, stepy do
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
79 for x = minx, maxx, stepx do
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
80 result[#result+1] = getOrCreateFrame(self,x,y)
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
81 end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
82 end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
83
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
84 return position + 1
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
85
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
86 else
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
87
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
88 error(("Invalid type: %q (%s)"):format(kind, tostring(args[position])))
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
89
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
90 end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
91 end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
92
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
93 function Grid:getFrames(...)
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
94 local args = {...}
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
95 local length = #args
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
96 local result = {}
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
97 local position = 1
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
98
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
99 while position <= length do
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
100 position = parseFrames(self, args, result, position)
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
101 end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
102
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
103 return result
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
104 end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
105
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
106 local Gridmt = {
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
107 __index = Grid,
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
108 __call = Grid.getFrames
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
109 }
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
110
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
111 local function newGrid(frameWidth, frameHeight, imageWidth, imageHeight, left, top, border)
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
112 assertPositiveInteger(frameWidth, "frameWidth")
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
113 assertPositiveInteger(frameHeight, "frameHeight")
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
114 assertPositiveInteger(imageWidth, "imageWidth")
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
115 assertPositiveInteger(imageHeight, "imageHeight")
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
116
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
117 left = left or 0
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
118 top = top or 0
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
119 border = border or 0
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
120
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
121 local key = getGridKey(frameWidth, frameHeight, imageWidth, imageHeight, left, top, border)
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
122
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
123 local grid = setmetatable(
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
124 { frameWidth = frameWidth,
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
125 frameHeight = frameHeight,
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
126 imageWidth = imageWidth,
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
127 imageHeight = imageHeight,
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
128 left = left,
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
129 top = top,
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
130 border = border,
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
131 width = math.floor(imageWidth/frameWidth),
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
132 height = math.floor(imageHeight/frameHeight),
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
133 _key = key
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
134 },
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
135 Gridmt
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
136 )
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
137 return grid
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
138 end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
139
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
140 -----------------------------------------------------------
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
141
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
142 local Animation = {}
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
143
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
144 local function cloneArray(arr)
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
145 local result = {}
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
146 for i=1,#arr do result[i] = arr[i] end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
147 return result
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
148 end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
149
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
150 local function parseDelays(delays)
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
151 local parsedDelays = {}
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
152 local tk,min,max,step
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
153 for k,v in pairs(delays) do
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
154 tk = type(k)
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
155 if tk == "number"
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
156 then parsedDelays[k] = v
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
157 elseif tk == "string" then
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
158 min, max, step = parseInterval(k)
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
159 for i = min,max,step do parsedDelays[i] = v end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
160 else
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
161 error(("Unexpected delay key: [%s]. Expected a number or a string"):format(tostring(k)))
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
162 end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
163 end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
164 return parsedDelays
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
165 end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
166
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
167 local function repeatValue(value, times)
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
168 local result = {}
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
169 for i=1,times do result[i] = value end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
170 return result
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
171 end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
172
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
173 local function createDelays(frames, defaultDelay, delays)
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
174 local maxFrames = #frames
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
175 local result = repeatValue(defaultDelay, maxFrames)
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
176 for i,v in pairs(parseDelays(delays)) do
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
177 if i > maxFrames then
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
178 error(("The delay value %d is too high; there are only %d frames"):format(i, maxFrames))
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
179 end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
180 result[i] = v
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
181 end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
182 return result
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
183 end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
184
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
185 local animationModes = {
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
186 loop = function(self) self.position = 1 end,
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
187 once = function(self)
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
188 self.position = #self.frames
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
189 self.status = "finished"
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
190 end,
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
191 bounce = function(self)
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
192 self.direction = self.direction * -1
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
193 self.position = self.position + self.direction + self.direction
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
194 end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
195 }
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
196
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
197 local Animationmt = { __index = Animation }
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
198
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
199 local function newAnimation(mode, frames, defaultDelay, delays)
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
200 delays = delays or {}
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
201 assert(animationModes[mode], ("%q is not a valid mode"):format(tostring(mode)))
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
202 assert(type(defaultDelay) == 'number' and defaultDelay > 0, "defaultDelay must be a positive number" )
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
203 assert(type(delays) == 'table', "delays must be a table or nil")
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
204 return setmetatable({
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
205 mode = mode,
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
206 frames = cloneArray(frames),
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
207 endSequence = animationModes[mode],
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
208 delays = createDelays(frames, defaultDelay, delays),
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
209 timer = 0,
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
210 position = 1,
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
211 direction = 1,
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
212 status = "playing"
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
213 },
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
214 Animationmt
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
215 )
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
216 end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
217
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
218 function Animation:clone()
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
219 return newAnimation(self.mode, self.frames, 1, self.delays)
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
220 end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
221
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
222 function Animation:update(dt)
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
223 if self.status ~= "playing" then return end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
224
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
225 self.timer = self.timer + dt
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
226
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
227 while self.timer > self.delays[self.position] do
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
228 self.timer = self.timer - self.delays[self.position]
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
229 self.position = self.position + self.direction
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
230 if self.position < 1 or self.position > #self.frames then
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
231 self:endSequence()
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
232 end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
233 end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
234 end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
235
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
236 function Animation:pause()
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
237 self.status = "paused"
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
238 end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
239
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
240 function Animation:resume()
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
241 self.status = "playing"
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
242 end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
243
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
244 function Animation:gotoFrame(position)
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
245 self.position = position
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
246 end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
247
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
248 function Animation:draw(image, x, y, r, sx, sy, ox, oy)
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
249 local frame = self.frames[self.position]
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
250 love.graphics.drawq(image, frame, x, y, r, sx, sy, ox, oy)
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
251 end
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
252
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
253 -----------------------------------------------------------
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
254
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
255 local anim8 = {
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
256 newGrid = newGrid,
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
257 newAnimation = newAnimation
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
258 }
716ff40413d2 initial commit
Radomir Dopieralski <sheep-devel@sheep.art.pl>
parents:
diff changeset
259 return anim8