From e0da0e869c5f0639b792c1e435b773393126ec02 Mon Sep 17 00:00:00 2001 From: Aki Date: Fri, 20 Jan 2017 00:12:16 +0100 Subject: Moved animation outside of player --- animated.lua | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 animated.lua (limited to 'animated.lua') 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 -- cgit v1.1