-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathAnimation.lua
65 lines (57 loc) · 2 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
local Class = require("lib.Class")
local Vector2 = require("lib.Vector2")
local Anim = Class:derive("Animation")
function Anim:new(xoffset, yoffset, w, h, frames, column_size, fps, loop)
self.fps = fps
if type(frames) == "table" then
self.frames = frames
else
self.frames = {}
for i = 1, frames do
self.frames[i] = i
end
end
self.column_size = column_size
self.start_offset = Vector2(xoffset, yoffset)
self.offset = Vector2()
self.size = Vector2(w, h)
--loop = false, playthrough once, otherwise, loop forever
self.loop = loop == nil or loop
self:reset()
end
function Anim:reset()
self.timer = 1 / self.fps
self.index = 1
self.done = false
self.offset.x = self.start_offset.x + (self.size.x * ((self.frames[self.index] - 1) % (self.column_size)))
self.offset.y = self.start_offset.y + (self.size.y * math.floor((self.frames[self.index] - 1) / self.column_size))
end
function Anim:set(quad)
quad:setViewport(self.offset.x, self.offset.y, self.size.x, self.size.y)
end
function Anim:update(dt, quad)
if #self.frames <= 1 then
self.done = true
return
elseif self.timer > 0 then
self.timer = self.timer - dt
if self.timer <= 0 then
self.timer = 1 / self.fps
self.index = self.index + 1
if self.index > #self.frames then
if self.loop then
self.index = 1
else
self.index = #self.frames
self.timer = 0
self.done = true
end
end
self.offset.x = self.start_offset.x + (self.size.x * ((self.frames[self.index] - 1) % (self.column_size)))
self.offset.y = self.start_offset.y + (self.size.y * math.floor((self.frames[self.index] - 1) / self.column_size))
-- print( self.index .. " " .. self.offset.x .. " " .. self.offset.y)
self:set(quad)
end
end
end
return Anim