-
Notifications
You must be signed in to change notification settings - Fork 4
/
animation.lua
480 lines (413 loc) · 12.4 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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
--[[
The MIT License (MIT)
Copyright (c) 2013 Patrick Rabier
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.
]]
--
-- LOVE2D ANIMATION
--
-- @todo document
-- @todo error management
-- @todo the path in the animation file should be relative to the file's location
-- @todo more flexible events ?
-- @todo load an animation with an already loaded image
--
--
-- The animation class
-- Loads an animation file (containing a path to the image)
-- Check the animation file template for the format
--
-- @member filepath the path of the animation file
-- @member descriptor the object loaded from the file (describes the animation frames)
-- @member currentState the state the animation is in. Each state is a line in the image
-- @member currentFrame the frame (of the current state) the animation is displaying
-- @member tick the amount of time the current frame has been displayed
-- @member speedMultiplier a multiplier used to control the animation speed programatically
-- @member active whether the animation is running (or is paused)
-- @member texture the image loaded
-- @member x horizontal coordinate
-- @member y vertical coordinate
-- @member rotation rotation angle in radians
-- @member relativeOriginX the x origin as a multiplier of the sprite's width
-- (ex : 1 for right, 0 for left)
-- @member relativeOriginY the y origin as a multiplier of the sprite's height
-- @member visible boolean, whether the sprite is visible
--
LoveAnimation = {
-- Members
filepath = nil,
descriptor = nil,
currentState = nil,
currentFrame = 0,
tick = 0,
speedMultiplier = 1,
active = true,
texture = nil,
x = 0,
y = 0,
rotation = 0,
relativeOriginX = 0,
relativeOriginY = 0,
visible = true,
flipX = 1,
_stateEndCallbacks = {},
_stateStartCallbacks = {},
}
LoveAnimation.__index = LoveAnimation
-- Static
local __loadedDescriptors = {}
-- Local function
local check_descriptor_integrity = function(desc)
-- @TODO
end
--
-- @brief Creates a new animation
-- @param filepath the file describing the animation
-- @param imagePath [optional] an alternative image
-- @return the animation object
--
function LoveAnimation.new(filepath, imagePath)
local new_anim = {}
setmetatable(new_anim, LoveAnimation)
new_anim:init(filepath, imagePath)
return new_anim
end
function LoveAnimation:init(filepath, imagePath)
local desc = nil
if __loadedDescriptors[filepath] then
desc = __loadedDescriptors[filepath]
else
local chunk = love.filesystem.load(filepath)
desc = chunk()
check_descriptor_integrity(desc);
__loadedDescriptors[filepath] = desc;
end
self.filepath = filepath
self.descriptor = desc
self.texture = imagePath and love.graphics.newImage(imagePath) or love.graphics.newImage(desc.imageSrc)
self:resetAnimation()
end
--
-- @brief Clones the animation (avoids reloading)
-- @return the new animation object
--
function LoveAnimation:clone()
local new_anim = {}
setmetatable(new_anim, LoveAnimation)
new_anim.filepath = self.filepath
new_anim.descriptor = self.descriptor
new_anim.texture = self.texture
new_anim:resetAnimation()
return new_anim
end
--
-- @brief Updates the animation state and frame. Called in the update loop
-- @param dt the elapsed time in seconds (floating point)
--
function LoveAnimation:update(dt)
if self.active == false then
return -- paused
end
self.tick = self.tick + dt * self.speedMultiplier
local state_descriptor = self.descriptor.states[self.currentState]
if self.tick > state_descriptor.switchDelay then
-- switch to the next frame
self.currentFrame = self.currentFrame + 1
if self.currentFrame >= state_descriptor.frameCount then
-- last frame reached, set next state
if not state_descriptor.nextState then
self.currentFrame = state_descriptor.frameCount - 1
self.tick = 0
return
end
self.currentFrame = 0
--callbacks
if state_descriptor.nextState ~= self.currentState then
if self._stateEndCallbacks[self.currentState] then
self._stateEndCallbacks[self.currentState](self, self.currentState)
end
if self._stateStartCallbacks[state_descriptor.nextState] then
self._stateStartCallbacks[state_descriptor.nextState](self, state_descriptor.nextState)
end
end
self.currentState = state_descriptor.nextState
end
-- reset tick
self.tick = 0
end
end
--
-- @brief Draws the sprite on the screen. Called in the drawing loop
--
--
function LoveAnimation:draw(scaleX, scaleY)
if not self.visible then
return
end
local state_descriptor = self.descriptor.states[self.currentState]
local quad = nil
-- we save the quads for each frame to avoid recreating them everytime
if not state_descriptor.quads then
state_descriptor.quads = {}
end
if not state_descriptor.quads[self.currentFrame] then
-- the quad for the current frame has not been created
quad = love.graphics.newQuad(
(state_descriptor.offsetX or 0) + (self.currentFrame * state_descriptor.frameW),
state_descriptor.offsetY or 0,
state_descriptor.frameW,
state_descriptor.frameH,
self.texture:getWidth(),
self.texture:getHeight())
-- we save it
state_descriptor.quads[self.currentFrame] = quad
else
quad = state_descriptor.quads[self.currentFrame]
end
love.graphics.draw(self.texture,
quad,
self.x,
self.y,
self.rotation,
self.flipX * (scaleX or 1), -- negative scale to flip
scaleY or 1, -- scale
self.relativeOriginX * state_descriptor.frameW,
self.relativeOriginY * state_descriptor.frameH,
0,0)
end
--
-- @brief Sets the state of the animation
-- @param state (string) a state specified in the animation file that we want to switch to
-- e.g anim:setState("jump")
--
function LoveAnimation:setState(state)
local current = self.currentState
local next = state
if self.descriptor.states[next] then
if state ~= current then
if self._stateEndCallbacks[current] then
self._stateEndCallbacks[current](self, current)
end
if self._stateStartCallbacks[next] then
self._stateStartCallbacks[next](self, next)
end
end
self.currentState = state
self.tick = 0
self.currentFrame = 0
end
end
--
-- @brief Gets the state the animation is currently in
-- @return (string) the current state
--
function LoveAnimation:getCurrentState()
return self.currentState;
end
--
-- @brief Gets the list of all states
-- @return (array) the list of all possible states
--
function LoveAnimation:getAllStates()
local states = {}
for state in pairs(self.descriptor.states) do
table.insert(states, state)
end
return states
end
--
-- @brief Sets the frame of the animation state
-- @param f (integer) the frame number
--
function LoveAnimation:setCurrentFrame(f)
local state_descriptor = self.descriptor.states[self.currentState]
if f < state_descriptor.frameCount then
self.currentFrame = f
else
self.currentFrame = state_descriptor.frameCount - 1 -- Is that wise ?
end
end
--
-- @brief Allows to speed up the animation with a multiplier
-- @param sm (positive floating point) the multiplier
--
function LoveAnimation:setSpeedMultiplier(sm)
-- negative multiplier support ? backwards animations ? to think abt it
self.speedMultiplier = math.abs(sm)
end
--
-- @brief Returns the animation to it's inital state,frame,speed,rotation
--
--
function LoveAnimation:resetAnimation()
self.currentState = self.descriptor.defaultState
self.tick = 0
self.speedMultiplier = 1
self.active = true
self.currentFrame = 0
self.rotation = 0
end
local _CheckCollision = function(ax1,ay1,aw,ah, bx1,by1,bw,bh)
local ax2,ay2,bx2,by2 = ax1 + aw, ay1 + ah, bx1 + bw, by1 + bh
return ax1 < bx2 and ax2 > bx1 and ay1 < by2 and ay2 > by1
end
--
-- @brief Checks for a collision between the sprite quad and the specified rectangle
-- @return true if the two quads intersect, false otherwise
--
function LoveAnimation:intersects(x,y,width,height)
local h = height or 1
local w = width or 1
local state_descriptor = self.descriptor.states[self.currentState]
return _CheckCollision(
self.x - (self.relativeOriginX * state_descriptor.frameW),
self.y - (self.relativeOriginY * state_descriptor.frameH),
state_descriptor.frameW,
state_descriptor.frameH,
x,y,w,h
)
end
--
-- @brief Switches the pause
--
--
function LoveAnimation:togglePause()
self.active = not self.active
end
--
-- @brief Pauses the animation
--
--
function LoveAnimation:pause()
self.active = false
end
--
-- @brief Unpauses the animation
--
--
function LoveAnimation:unpause()
self.active = true
end
--
-- @brief Get the sprite's current position and size
--
function LoveAnimation:getGeometry()
return {
x = self.x,
y = self.y,
width = self:getFrameWidth(),
height = self:getFrameHeight()
}
end
--
-- @brief Sets the sprite's current position
-- @param x the x coordinate of the sprite
-- @param y the y coordinate of the sprite
--
function LoveAnimation:setPosition(x, y)
self.x = x
self.y = y
end
--
-- @brief Rotates the sprite to the specified angle
-- @param r rotation angle (according to the love drawing functions)
--
function LoveAnimation:setRotation(r)
self.rotation = r
end
--
-- @brief Sets the sprite as visible or invisible
-- @param v (bool) true if visible, false if invisible
--
function LoveAnimation:setVisibility(v)
self.visible = v
end
--
-- @brief Sets the origin of the sprite (default is top left corner)
-- @param ox the x coordinate
-- @param oy the y coordinate
--
-- The coordinates are a percentage relative to the sprite.
-- e.g : setRelativeOrigin(0,0) is the top left corner
-- e.g : setRelativeOrigin(0.5,0.5) is the center of the sprite
--
function LoveAnimation:setRelativeOrigin(ox, oy)
-- rename to setAnchorPoint (perhaps clearer)
self.relativeOriginX = ox
self.relativeOriginY = oy
end
--
-- @brief Flips the sprite horizontally
--
-- attention : the flips is dependant on the origin
--
function LoveAnimation:toggleHorizontalFlip()
self.flipX = -1 * self.flipX
end
--
-- @brief Flips the sprite horizontally according to bool
-- @param bool (bool) true if the sprite should be flipped horizontally
--
-- attention : the flips is dependant on the origin
--
function LoveAnimation:setHorizontalFlip(bool)
self.flipX = bool and -1 or 1
end
--
-- @brief Gets the width of the current frame
-- @return the current width
--
function LoveAnimation:getFrameWidth()
local state_descriptor = self.descriptor.states[self.currentState]
return state_descriptor.frameW
end
--
-- @brief Gets the height of the current frame
-- @return the current height
--
function LoveAnimation:getFrameHeight()
local state_descriptor = self.descriptor.states[self.currentState]
return state_descriptor.frameH
end
--
-- @brief Gets width & height of the current frame
-- @return current dimension
--
function LoveAnimation:getFrameDimension()
local state_descriptor = self.descriptor.states[self.currentState]
return state_descriptor.frameW, state_descriptor.frameH
end
--
-- EVENTS
--
--
-- @brief Adds a listener to the animation. Called when the last frame of a state is reached
-- @param state (string) The name of the state
-- @param callback (function) The callback function, recieves the LoveAnimation object as param
-- Set the callback to nil to stop receiving events
--
function LoveAnimation:onStateEnd(state, callback)
self._stateEndCallbacks[state] = callback
end
--
-- @brief Adds a listener to the animation. Called when the state starts
-- @param state (string) The name of the state
-- @param callback (function) The callback function, recieves the LoveAnimation object as param
-- Set the callback to nil to stop receiving events
--
function LoveAnimation:onStateStart(state, callback)
self._stateStartCallbacks[state] = callback
end