summaryrefslogtreecommitdiffhomepage
path: root/animated.lua
diff options
context:
space:
mode:
authorAki <nthirtyone@gmail.com>2017-01-20 00:12:16 +0100
committerAki <nthirtyone@gmail.com>2017-01-20 00:12:16 +0100
commite0da0e869c5f0639b792c1e435b773393126ec02 (patch)
treeafc016a37dbef2a9aba6683135105651f1382134 /animated.lua
parentae1547a22e7aa08c954133dc7320bc70077d1f9f (diff)
downloadroflnauts-e0da0e869c5f0639b792c1e435b773393126ec02.zip
roflnauts-e0da0e869c5f0639b792c1e435b773393126ec02.tar.gz
roflnauts-e0da0e869c5f0639b792c1e435b773393126ec02.tar.bz2
Moved animation outside of player
Diffstat (limited to 'animated.lua')
-rw-r--r--animated.lua42
1 files changed, 42 insertions, 0 deletions
diff --git a/animated.lua b/animated.lua
new file mode 100644
index 0000000..c698872
--- /dev/null
+++ b/animated.lua
@@ -0,0 +1,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 \ No newline at end of file