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 --- player.lua | 53 ++++++++++++++++++++++------------------------------- 1 file changed, 22 insertions(+), 31 deletions(-) (limited to 'player.lua') diff --git a/player.lua b/player.lua index d2bc6d1..b3ce86f 100644 --- a/player.lua +++ b/player.lua @@ -3,6 +3,7 @@ -- Collision category: [2] -- WHOLE CODE HAS FLAG OF "need a cleanup" +require "animated" -- Metatable of `Player` -- nils initialized in constructor @@ -24,11 +25,6 @@ Player = { alive = true, punchcd = 0.25, punchdir = 0, -- a really bad thing - -- Animation - animations = require "animations", - current = nil, - frame = 1, - delay = 0.10, -- Movement inAir = true, salto = false, @@ -46,6 +42,8 @@ Player = { -- Sounds sfx = require "sounds" } +Player.__index = Player +setmetatable(Player, Animated) -- Constructor of `Player` function Player:new (game, world, x, y, name) @@ -153,21 +151,7 @@ function Player:update(dt) end end - -- # ANIMATIONS - -- Animation - self.delay = self.delay - dt - if self.delay < 0 then - self.delay = self.delay + Player.delay -- INITIAL from metatable - -- Thank you De Morgan! - if self.current.repeated or not (self.frame == self.current.frames) then - self.frame = (self.frame % self.current.frames) + 1 - elseif isDown(controlset, "right") or isDown(controlset, "left") then - -- If nonrepeatable animation is finished and player is walking - self:changeAnimation("walk") - elseif self.current == self.animations.damage then - self:changeAnimation("idle") - end - end + self:animate(dt) -- # DEATH -- We all die in the end. @@ -240,7 +224,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:changeAnimation("idle") + self:setAnimation("idle") end -- Remove jump self.jumpnumber = self.jumpnumber - 1 @@ -252,7 +236,7 @@ function Player:controlpressed(set, action, key) (self.current ~= self.animations.attack) and (self.current ~= self.animations.attack_up) and (self.current ~= self.animations.attack_down) then - self:changeAnimation("walk") + self:setAnimation("walk") end -- Punching @@ -262,19 +246,19 @@ function Player:controlpressed(set, action, key) if isDown(controlset, "up") then -- Punch up if self.current ~= self.animations.damage then - self:changeAnimation("attack_up") + self:setAnimation("attack_up") end self:hit("up") elseif isDown(controlset, "down") then -- Punch down if self.current ~= self.animations.damage then - self:changeAnimation("attack_down") + self:setAnimation("attack_down") end self:hit("down") else -- Punch horizontal if self.current ~= self.animations.damage then - self:changeAnimation("attack") + self:setAnimation("attack") end if f == 1 then self:hit("right") @@ -299,7 +283,7 @@ function Player:controlreleased(set, action, key) (isDown(controlset, "left") or isDown(controlset, "right")) and self.current == self.animations.walk then - self:changeAnimation("idle") + self:setAnimation("idle") end end @@ -359,10 +343,17 @@ end -- Change animation of `Player` -- idle, walk, attack, attack_up, attack_down, damage -function Player:changeAnimation(animation) - self.frame = 1 - self.delay = Player.delay -- INITIAL from metatable - self.current = self.animations[animation] +function Player:nextFrame() + local isDown = Controller.isDown + local controlset = self:getControlSet() + if self.current.repeated or not (self.frame == self.current.frames) then + self.frame = (self.frame % self.current.frames) + 1 + elseif isDown(controlset, "right") or isDown(controlset, "left") then + -- If nonrepeatable animation is finished and player is walking + self:setAnimation("walk") + elseif self.current == self.animations.damage then + self:setAnimation("idle") + end end -- Spawn `Effect` relative to `Player` @@ -425,7 +416,7 @@ function Player:damage(direction) local x,y = self.body:getLinearVelocity() self.body:setLinearVelocity(x,0) self.body:applyLinearImpulse((42+10*self.combo)*horizontal, (68+10*self.combo)*vertical + 15) - self:changeAnimation("damage") + self:setAnimation("damage") self.combo = math.min(27, self.combo + 1) self.punchcd = 0.08 + self.combo*0.006 self:playSound(2) -- 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. --- player.lua | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) (limited to 'player.lua') 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 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 --- player.lua | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) (limited to 'player.lua') diff --git a/player.lua b/player.lua index 66eb818..b6d4e3f 100644 --- a/player.lua +++ b/player.lua @@ -52,7 +52,6 @@ function Player:new (game, world, x, y, name) -- Meta local o = {} setmetatable(o, self) - self.__index = self -- Physics local group = -1-#game.Nauts o.body = love.physics.newBody(world, x, y, "dynamic") @@ -79,12 +78,6 @@ function Player:new (game, world, x, y, name) return o end --- Destructor of `Player` -function Player:delete() - -- body deletion is handled by world deletion - self.sprite = nil -end - -- Control set managment function Player:assignControlSet(set) self.controlset = set @@ -303,9 +296,7 @@ function Player: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 - love.graphics.setColor(255,255,255,255) - 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) + Animated.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 -- cgit v1.1