From bd22a09dc862ac5480426bfd5e0311a97b6cfa77 Mon Sep 17 00:00:00 2001 From: Aki Date: Wed, 1 Mar 2017 21:31:11 +0100 Subject: Rename: Animated -> Sprite Rename: getSprite, setSprite -> getImage, setImage --- animated.lua | 81 ---------------------------------------------------------- decoration.lua | 6 ++--- platform.lua | 8 +++--- player.lua | 10 ++++---- sprite.lua | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 93 insertions(+), 93 deletions(-) delete mode 100644 animated.lua create mode 100644 sprite.lua diff --git a/animated.lua b/animated.lua deleted file mode 100644 index 160ee44..0000000 --- a/animated.lua +++ /dev/null @@ -1,81 +0,0 @@ --- `Animated` --- Abstract class for drawable animated entities. - --- Metatable -Animated = { - animations--[[table with animations]], - current--[[animations.default]], - sprite--[[love.graphics.newImage()]], - frame = 1, - delay = .1, -} -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 -end --- Returns current sprite Image. -function Animated:getSprite() - return self.sprite -end - --- Sets new animations list. -function Animated:setAnimationsList(t) - if t then - self.animations = t - self:setAnimation("default") - end -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 --- Returns current animation table. -function Animated:getAnimation() - return self.current -end - --- Get frame quad for drawing. -function Animated:getQuad() - 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 then - love.graphics.setColor(255,255,255,255) - if q then love.graphics.draw(s, q, ...) - else love.graphics.draw(s, ...) end - end -end --- Animation updating. -function Animated:update(dt) - 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. -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("default") - end -end \ No newline at end of file diff --git a/decoration.lua b/decoration.lua index 3ca6f76..ffb007d 100644 --- a/decoration.lua +++ b/decoration.lua @@ -5,11 +5,11 @@ Decoration = { y = 0 } Decoration.__index = Decoration -setmetatable(Decoration, Animated) +setmetatable(Decoration, Sprite) function Decoration:new(x, y, sprite) local o = {} setmetatable(o, self) - o.sprite = love.graphics.newImage(sprite) + o:setImage(love.graphics.newImage(sprite)) o:setPosition(x,y) return o end @@ -29,5 +29,5 @@ function Decoration:draw(offset_x, offset_y, scale) local draw_x = (math.floor(x) + offset_x) * scale local draw_y = (math.floor(y) + offset_y) * scale -- draw - Animated.draw(self, draw_x, draw_y, 0, scale, scale) + Sprite.draw(self, draw_x, draw_y, 0, scale, scale) end \ No newline at end of file diff --git a/platform.lua b/platform.lua index 908cf73..9d75e66 100644 --- a/platform.lua +++ b/platform.lua @@ -3,7 +3,7 @@ -- Collision category: [1] -- WHOLE CODE HAS FLAG OF "need a cleanup" -require "animated" +require "sprite" -- Metatable of `Platform` -- nils initialized in constructor @@ -14,7 +14,7 @@ Platform = { world = nil, } Platform.__index = Platform -setmetatable(Platform, Animated) +setmetatable(Platform, Sprite) -- Constructor of `Platform` function Platform:new (game, world, x, y, shape, sprite, animations) @@ -39,7 +39,7 @@ function Platform:new (game, world, x, y, shape, sprite, animations) end end -- END HERE - o:setSprite(love.graphics.newImage(sprite)) + o:setImage(love.graphics.newImage(sprite)) o:setAnimationsList(animations) o.world = game return o @@ -62,7 +62,7 @@ function Platform:draw (offset_x, offset_y, scale, debug) local draw_x = (math.floor(x) + offset_x) * scale local draw_y = (math.floor(y) + offset_y) * scale -- sprite draw - Animated.draw(self, draw_x, draw_y, 0, scale, scale) + Sprite.draw(self, draw_x, draw_y, 0, scale, scale) -- debug draw if debug then love.graphics.setColor(255, 69, 0, 140) diff --git a/player.lua b/player.lua index 775dd60..a5b4bd2 100644 --- a/player.lua +++ b/player.lua @@ -3,7 +3,7 @@ -- Collision category: [2] -- WHOLE CODE HAS FLAG OF "need a cleanup" -require "animated" +require "sprite" -- Metatable of `Player` -- nils initialized in constructor @@ -45,7 +45,7 @@ Player = { animations = require "animations" } Player.__index = Player -setmetatable(Player, Animated) +setmetatable(Player, Sprite) -- Constructor of `Player` function Player:new (game, world, x, y, name) @@ -64,7 +64,7 @@ function Player:new (game, world, x, y, name) o.body:setFixedRotation(true) -- Misc o.name = name or "empty" - o:setSprite(newImage("assets/nauts/"..o.name..".png")) + o:setImage(newImage("assets/nauts/"..o.name..".png")) o.world = game o.punchcd = 0 -- Animation @@ -146,7 +146,7 @@ function Player:update(dt) end end - Animated.update(self, dt) + Sprite.update(self, dt) -- # DEATH -- We all die in the end. @@ -299,7 +299,7 @@ function Player:draw(offset_x, offset_y, scale, debug) local draw_y = (approx(y) + offset_y) * scale local draw_x = (math.floor(x) + offset_x) * scale -- sprite draw - Animated.draw(self, draw_x, draw_y, self.rotate, self.facing*scale, scale, 12, 15) + Sprite.draw(self, draw_x, draw_y, self.rotate, self.facing*scale, scale, 12, 15) -- debug draw if debug then for _,fixture in pairs(self.body:getFixtureList()) do diff --git a/sprite.lua b/sprite.lua new file mode 100644 index 0000000..1cc46f7 --- /dev/null +++ b/sprite.lua @@ -0,0 +1,81 @@ +-- `Sprite` +-- Abstract class for drawable animated entities. + +-- Metatable +Sprite = { + animations--[[table with animations]], + current--[[animations.default]], + image--[[love.graphics.newImage()]], + frame = 1, + delay = .1, +} +Sprite.__index = Sprite + +-- Cleans up reference to image on deletion. +function Sprite:delete() + self.image = nil +end + +-- Sets an Image as a image. +function Sprite:setImage(image) + self.image = image +end +-- Returns current image Image. +function Sprite:getImage() + return self.image +end + +-- Sets new animations list. +function Sprite:setAnimationsList(t) + if t then + self.animations = t + self:setAnimation("default") + end +end + +-- Sets current animation by table key. +function Sprite:setAnimation(animation) + self.frame = 1 + self.delay = Sprite.delay -- INITIAL from metatable + self.current = self.animations[animation] +end +-- Returns current animation table. +function Sprite:getAnimation() + return self.current +end + +-- Get frame quad for drawing. +function Sprite:getQuad() + 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 image. +function Sprite:draw(...) + local s, q = self:getImage(), self:getQuad() + if s then + love.graphics.setColor(255,255,255,255) + if q then love.graphics.draw(s, q, ...) + else love.graphics.draw(s, ...) end + end +end +-- Animation updating. +function Sprite:update(dt) + if self.animations and self.current then + self.delay = self.delay - dt + if self.delay < 0 then + self.delay = self.delay + Sprite.delay -- INITIAL from metatable + self:nextFrame() + end + end +end +-- Moving to the next frame. +function Sprite:nextFrame() + if self.current.repeated or not (self.frame == self.current.frames) then + self.frame = (self.frame % self.current.frames) + 1 + else + self:setAnimation("default") + end +end \ No newline at end of file -- cgit v1.1