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 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 From 4b2f70d89a53ab03361a51cf970cede2e53cb3f6 Mon Sep 17 00:00:00 2001 From: Aki Date: Fri, 20 Jan 2017 12:29:40 +0100 Subject: Clean-ups in Player-Animated relations --- animated.lua | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'animated.lua') diff --git a/animated.lua b/animated.lua index 236b68d..49fcbfa 100644 --- a/animated.lua +++ b/animated.lua @@ -11,6 +11,11 @@ Animated = { } Animated.__index = Animated +-- Cleans up reference to sprite on deletion. +function Animated:delete() + self.sprite = nil +end + -- Sets an Image as a sprite. function Animated:setSprite(image) self.sprite = image @@ -38,9 +43,12 @@ end -- Drawing self to LOVE2D buffer. function Animated:draw(...) - love.graphics.draw(self:getSprite(), self:getQuad(), ...) + local s, q = self:getSprite(), self:getQuad() + if s and q then + love.graphics.setColor(255,255,255,255) + love.graphics.draw(s, q, ...) + end end - -- Animation updating. function Animated:update(dt) self.delay = self.delay - dt -- cgit v1.1 From 29a527f4dbfeae138b71ce2dd8a82aa40560c522 Mon Sep 17 00:00:00 2001 From: Aki Date: Fri, 20 Jan 2017 13:09:03 +0100 Subject: Added behaviours for non-animated instances --- animated.lua | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) (limited to 'animated.lua') diff --git a/animated.lua b/animated.lua index 49fcbfa..de16195 100644 --- a/animated.lua +++ b/animated.lua @@ -25,6 +25,12 @@ function Animated:getSprite() return self.sprite end +-- Sets new animations list. +function Animated:setAnimationsList(t) + self.animations = t + self:setAnimation("default") +end + -- Sets current animation by table key. function Animated:setAnimation(animation) self.frame = 1 @@ -38,23 +44,29 @@ end -- Get frame quad for drawing. function Animated:getQuad() - return self.current[self.frame] + if self.animations and self.current then + return self.current[self.frame] + end end -- Drawing self to LOVE2D buffer. +-- If there is no Quad, it will draw entire sprite. function Animated:draw(...) local s, q = self:getSprite(), self:getQuad() - if s and q then + if s then love.graphics.setColor(255,255,255,255) - love.graphics.draw(s, q, ...) + if q then love.graphics.draw(s, q, ...) + else love.graphics.draw(s, ...) end end 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() + if self.animations and self.current then + self.delay = self.delay - dt + if self.delay < 0 then + self.delay = self.delay + Animated.delay -- INITIAL from metatable + self:nextFrame() + end end end -- Moving to the next frame. -- cgit v1.1 From ff957be7d38a3143aa89a798184a975af00998bc Mon Sep 17 00:00:00 2001 From: Aki Date: Fri, 20 Jan 2017 14:24:50 +0100 Subject: Animated platform test for Abyss small --- animated.lua | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'animated.lua') diff --git a/animated.lua b/animated.lua index de16195..160ee44 100644 --- a/animated.lua +++ b/animated.lua @@ -27,8 +27,10 @@ end -- Sets new animations list. function Animated:setAnimationsList(t) - self.animations = t - self:setAnimation("default") + if t then + self.animations = t + self:setAnimation("default") + end end -- Sets current animation by table key. -- cgit v1.1