This repository has been archived by the owner on Feb 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
anims.lua
46 lines (38 loc) · 1.45 KB
/
anims.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
function animationSet(image, frameCount)
local animSet = {}
animSet.image = image
animSet.frameWidth = image:getWidth()/frameCount
animSet.frames = {}
for i = 1, frameCount do
table.insert(animSet.frames, love.graphics.newQuad((i-1)*animSet.frameWidth, 0, animSet.frameWidth, image:getHeight(), image:getWidth(), image:getHeight()))
end
animSet.animations = {}
animSet.update = function(self, dt)
end
animSet.setAnimation = function(self, name, startIndex)
self.animations[name].index = startIndex or 1
self.animations[name].time = 0
self.currentAnimation = name
end
animSet.update = function(self, dt)
self.animations[self.currentAnimation].time = self.animations[self.currentAnimation].time + dt
end
animSet.getCurrentFrame = function(self)
local anim = self.animations[self.currentAnimation]
local frame = anim.from + math.floor(anim.time * anim.speed) % (anim.to - anim.from)
if anim.to == anim.from then frame = anim.from end
return frame
end
animSet.draw = function(self, ...)
local anim = self.animations[self.currentAnimation]
love.graphics.draw(animSet.image, animSet.frames[self:getCurrentFrame()], ...)
end
return animSet
end
function animation(from, to, speed)
local anim = {}
anim.from = from
anim.to = to
anim.speed = speed
return anim
end