summaryrefslogtreecommitdiffhomepage
path: root/animated.lua
blob: c69887223a3a764ef4357bd5ce0e0f30469d85ce (plain)
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
-- `Animated`
-- Abstract class for animated entities.

-- Metatable
Animated = {
	animations = require "animations",
	current--[[animations.idle]], 
	frame = 1,
	delay = .1
}
Animated.__index = Animated
Animated.current = Animated.animations.idle

-- setAnimation(self, animation)
function Animated:setAnimation(animation)
	self.frame = 1
	self.delay = Animated.delay -- INITIAL from metatable
	self.current = self.animations[animation]
end

-- getAnimation(self)
function Animated:getAnimation()
	return self.current
end

-- animate(self, dt)
function Animated:animate(dt)
	self.delay = self.delay - dt
	if self.delay < 0 then
		self.delay = self.delay + Animated.delay -- INITIAL from metatable
		self:nextFrame()
	end
end

-- nextFrame(self)
function Animated:nextFrame()
	if self.current.repeated or not (self.frame == self.current.frames) then
		self.frame = (self.frame % self.current.frames) + 1
	else
		self:setAnimation("idle")
	end
end