diff options
Diffstat (limited to 'animated.lua')
-rw-r--r-- | animated.lua | 42 |
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 |