From 6d61fb66d72bb668d71479d3ed5f40b51041639e Mon Sep 17 00:00:00 2001 From: Aki Date: Fri, 29 Sep 2017 13:17:54 +0200 Subject: Moved update part of jumping from Player to Hero isJumping is now a method added debugging information on player jumps --- not/Hero.lua | 19 ++++++++++++++++++- not/Player.lua | 20 ++++---------------- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/not/Hero.lua b/not/Hero.lua index 2e1f1f0..b3edfbf 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -43,7 +43,6 @@ function Hero:new (config, x, y, world) self.salto = false self.smoke = false self.isAlive = true - self.isJumping = false self.spawntimer = 2 self.punchCooldown = 0 -- TODO: Pass loaded portrait from menu to Hero. @@ -140,6 +139,13 @@ function Hero:update (dt) self:setLinearVelocity(38*self.facing, 0) end end + + -- Jumping. + if self:isJumping() and self.jumpTimer > 0 and (self.jumpCounter == 0 or self.jumpCounter == 1) then + local x = self:getLinearVelocity() + self:setLinearVelocity(x,-160) + self.jumpTimer = self.jumpTimer - dt + end end --- Damps linear velocity every frame by applying minor force to body. @@ -175,6 +181,13 @@ end function Hero:draw (debug) if not self.isAlive then return end Hero.__super.draw(self, debug) + if debug then + local x, y = self:getPosition() + love.graphics.setColor(255, 50, 50) + love.graphics.setFont(Font) + local msg = string.format("%d %s %s", self.jumpCounter, tostring(self.jumpTimer > 0), tostring(self:isJumping())) + love.graphics.print(msg, x + 10, y) + end end -- TODO: Hero@drawTag's printf is not readable. @@ -233,6 +246,10 @@ function Hero:land () self:createEffect("land") end +function Hero:isJumping () + return false +end + function Hero:isWalking () return self:isWalkingLeft() or self:isWalkingRight() end diff --git a/not/Player.lua b/not/Player.lua index 0b56e6d..3d15f31 100644 --- a/not/Player.lua +++ b/not/Player.lua @@ -25,16 +25,8 @@ function Player:isControlDown (control) return Controller.isDown(self:getControllerSet(), control) end --- Update of `Player`. -function Player:update (dt) - Player.__super.update(self, dt) -- TODO: It would be probably a good idea to add return to update functions to terminate if something goes badly in parent's update. - if self.body:isDestroyed() then return end - local x, y = self:getLinearVelocity() - -- Jumping. - if self.isJumping and self.jumpTimer > 0 then - self:setLinearVelocity(x,-160) - self.jumpTimer = self.jumpTimer - dt - end +function Player:isJumping () + return self:isControlDown("jump") end function Player:isWalkingLeft () @@ -51,10 +43,9 @@ function Player:controlpressed (set, action, key) self.smoke = false -- TODO: temporary -- Jumping if action == "jump" then + self.jumpCounter = self.jumpCounter - 1 if self.jumpCounter > 0 then - -- General jump logics - self.isJumping = true - --self:playSound(6) + -- self:playSound(6) -- Spawn proper effect if not self.inAir then self:createEffect("jump") @@ -71,8 +62,6 @@ function Player:controlpressed (set, action, key) (self.current == self.animations.attack_down) then self:setAnimation("default") end - -- Remove jump - self.jumpCounter = self.jumpCounter - 1 end end @@ -118,7 +107,6 @@ function Player:controlreleased (set, action, key) if set ~= self:getControllerSet() then return end -- Jumping if action == "jump" then - self.isJumping = false self.jumpTimer = Hero.jumpTimer -- take initial from metatable end -- Walking -- cgit v1.1