From cc48afb0a20d01c8f4098c195239f2ca51fac495 Mon Sep 17 00:00:00 2001 From: Aki Date: Fri, 20 Jan 2017 12:09:54 +0100 Subject: Animated class changes. --- animated.lua | 43 ++++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) (limited to 'animated.lua') diff --git a/animated.lua b/animated.lua index c698872..236b68d 100644 --- a/animated.lua +++ b/animated.lua @@ -1,42 +1,59 @@ -- `Animated` --- Abstract class for animated entities. +-- Abstract class for drawable animated entities. -- Metatable Animated = { - animations = require "animations", - current--[[animations.idle]], + animations--[[table with animations]], + current--[[animations.default]], + sprite--[[love.graphics.newImage()]], frame = 1, - delay = .1 + delay = .1, } Animated.__index = Animated -Animated.current = Animated.animations.idle --- setAnimation(self, animation) +-- Sets an Image as a sprite. +function Animated:setSprite(image) + self.sprite = image +end +-- Returns current sprite Image. +function Animated:getSprite() + return self.sprite +end + +-- Sets current animation by table key. function Animated:setAnimation(animation) self.frame = 1 self.delay = Animated.delay -- INITIAL from metatable self.current = self.animations[animation] end - --- getAnimation(self) +-- Returns current animation table. function Animated:getAnimation() return self.current end --- animate(self, dt) -function Animated:animate(dt) +-- Get frame quad for drawing. +function Animated:getQuad() + return self.current[self.frame] +end + +-- Drawing self to LOVE2D buffer. +function Animated:draw(...) + love.graphics.draw(self:getSprite(), self:getQuad(), ...) +end + +-- Animation updating. +function Animated:update(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) +-- Moving to the next frame. 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") + self:setAnimation("default") end end \ No newline at end of file -- cgit v1.1