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 ++++++++++++++++++++++++++++++------------- animations.lua | 4 ++-- player.lua | 23 +++++++++++++---------- 3 files changed, 45 insertions(+), 25 deletions(-) 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 diff --git a/animations.lua b/animations.lua index 58c4b88..881da49 100644 --- a/animations.lua +++ b/animations.lua @@ -1,8 +1,8 @@ -- Animations spritesheet array for `Player` -- Basic spritesheet size is 376x26. Each frame is 24x24 and has 1px border around it. --- From the left: idle (walk0), walk1, walk2, walk3, attack0, attack1, attack3, attack_up0, attack_up1, attack_up2, attack_down0, attack_down1, attack_down2, damage0, damage1 +-- From the left: default (walk0), walk1, walk2, walk3, attack0, attack1, attack3, attack_up0, attack_up1, attack_up2, attack_down0, attack_down1, attack_down2, damage0, damage1 local animations = { - idle = { + default = { [1] = love.graphics.newQuad( 1, 1, 24,24, 376,26), frames = 1, repeated = true diff --git a/player.lua b/player.lua index b3ce86f..66eb818 100644 --- a/player.lua +++ b/player.lua @@ -40,7 +40,9 @@ Player = { portrait_sheet = require "nautsicons", portrait_box = love.graphics.newQuad( 0, 15, 32,32, 80,130), -- Sounds - sfx = require "sounds" + sfx = require "sounds", + -- Animations table + animations = require "animations" } Player.__index = Player setmetatable(Player, Animated) @@ -63,11 +65,11 @@ function Player:new (game, world, x, y, name) o.body:setFixedRotation(true) -- Misc o.name = name or "empty" - o.sprite = newImage("assets/nauts/"..o.name..".png") + o:setSprite(newImage("assets/nauts/"..o.name..".png")) o.world = game o.punchcd = 0 -- Animation - o.current = o.animations.idle + o.current = o.animations.default o:createEffect("respawn") -- Portrait load for first object created if self.portrait_sprite == nil then @@ -108,7 +110,7 @@ function Player:update(dt) end -- Salto - if self.salto and (self.current == self.animations.walk or self.current == self.animations.idle) then + if self.salto and (self.current == self.animations.walk or self.current == self.animations.default) then self.rotate = (self.rotate + 17 * dt * self.facing) % 360 elseif self.rotate ~= 0 then self.rotate = 0 @@ -151,7 +153,7 @@ function Player:update(dt) end end - self:animate(dt) + Animated.update(self, dt) -- # DEATH -- We all die in the end. @@ -224,7 +226,7 @@ function Player:controlpressed(set, action, key) if (self.current == self.animations.attack) or (self.current == self.animations.attack_up) or (self.current == self.animations.attack_down) then - self:setAnimation("idle") + self:setAnimation("default") end -- Remove jump self.jumpnumber = self.jumpnumber - 1 @@ -283,7 +285,7 @@ function Player:controlreleased(set, action, key) (isDown(controlset, "left") or isDown(controlset, "right")) and self.current == self.animations.walk then - self:setAnimation("idle") + self:setAnimation("default") end end @@ -302,7 +304,8 @@ function Player:draw(offset_x, offset_y, scale, debug) local draw_y = (math.floor(y) + offset_y) * scale -- sprite draw love.graphics.setColor(255,255,255,255) - love.graphics.draw(self.sprite, self.current[self.frame], draw_x, draw_y, self.rotate, self.facing*scale, 1*scale, 12, 15) + Animated.draw(self, draw_x, draw_y, self.rotate, self.facing*scale, 1*scale, 12, 15) + --love.graphics.draw(self:getSprite(), self:getQuad(), draw_x, draw_y, self.rotate, self.facing*scale, 1*scale, 12, 15) -- debug draw if debug then for _,fixture in pairs(self.body:getFixtureList()) do @@ -342,7 +345,7 @@ function Player:drawHUD(x,y,scale,elevation) end -- Change animation of `Player` --- idle, walk, attack, attack_up, attack_down, damage +-- default, walk, attack, attack_up, attack_down, damage function Player:nextFrame() local isDown = Controller.isDown local controlset = self:getControlSet() @@ -352,7 +355,7 @@ function Player:nextFrame() -- If nonrepeatable animation is finished and player is walking self:setAnimation("walk") elseif self.current == self.animations.damage then - self:setAnimation("idle") + self:setAnimation("default") end end -- cgit v1.1