diff options
-rw-r--r-- | not/Hero.lua | 86 | ||||
-rw-r--r-- | not/Player.lua | 65 |
2 files changed, 91 insertions, 60 deletions
diff --git a/not/Hero.lua b/not/Hero.lua index 2b5c529..7ef7b4e 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -43,8 +43,6 @@ function Hero:new (config, x, y, world) self.salto = false self.smoke = false self.isAlive = true - self.isWalking = false - self.isJumping = false self.spawntimer = 2 self.punchCooldown = 0 -- TODO: Pass loaded portrait from menu to Hero. @@ -124,6 +122,13 @@ function Hero:update (dt) end end + if self:isWalkingLeft() then + self:walk(-1) + end + if self:isWalkingRight() then + self:walk(1) + end + -- Stop vertical local currentAnimation = self:getAnimation() if self.frame < currentAnimation.frames then @@ -134,11 +139,49 @@ function Hero:update (dt) self:setLinearVelocity(38*self.facing, 0) end end + + -- Jumping. + if self:isJumping() and self.jumpTimer > 0 then + if not self._jumpevent then + self._jumpevent = true + self:onJump() + end + if self.jumpCounter == 0 or self.jumpCounter == 1 then + local x = self:getLinearVelocity() + self:setLinearVelocity(x,-160) + self.jumpTimer = self.jumpTimer - dt + end + else + self._jumpevent = false + end +end + +function Hero:onJump () + -- Start salto if last jump + if self.jumpCounter == 1 then + self.salto = true + end + self.jumpCounter = self.jumpCounter - 1 + if self.jumpCounter > 0 then + -- self:playSound(6) + -- Spawn proper effect + if not self.inAir then + self:createEffect("jump") + else + self:createEffect("doublejump") + end + -- Animation clear + if (self.current == self.animations.attack) or + (self.current == self.animations.attack_up) or + (self.current == self.animations.attack_down) then + self:setAnimation("default") + end + end end --- Damps linear velocity every frame by applying minor force to body. function Hero:dampVelocity (dt) - if not self.isWalking then + if not self:isWalking() then local face local x, y = self:getLinearVelocity() if x < -12 then @@ -169,6 +212,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. @@ -199,7 +249,7 @@ end function Hero:goToNextFrame () if self.current.repeated or not (self.frame == self.current.frames) then self.frame = (self.frame % self.current.frames) + 1 - elseif self.isWalking then + elseif self:isWalking() then self:setAnimation("walk") elseif self.current == self.animations.damage then self:setAnimation("default") @@ -227,6 +277,34 @@ function Hero:land () self:createEffect("land") end +function Hero:isJumping () + return false +end + +function Hero:isWalking () + return self:isWalkingLeft() or self:isWalkingRight() +end + +function Hero:isWalkingLeft () + return false +end + +function Hero:isWalkingRight () + return false +end + +function Hero:walk (face) + local x, y = self:getLinearVelocity() + self.facing = face + self:applyForce(250 * face, 0) + if x > self.MAX_VELOCITY then + self:applyForce(-250, 0) + end + if x < -self.MAX_VELOCITY then + self:applyForce(250, 0) + end +end + -- Creates temporary fixture for hero's body that acts as sensor. -- direction: ("left", "right", "up", "down") -- Sensor fixture is deleted after time set in UserData[1]; deleted by `not.Hero.update`. diff --git a/not/Player.lua b/not/Player.lua index b0dac75..27ace75 100644 --- a/not/Player.lua +++ b/not/Player.lua @@ -25,70 +25,25 @@ 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 - -- Walking. - if self:isControlDown("left") then - self.facing = -1 - self:applyForce(-250, 0) - -- Controlled speed limit - if x < -self.MAX_VELOCITY then - self:applyForce(250, 0) - end - end - if self:isControlDown("right") then - self.facing = 1 - self:applyForce(250, 0) - -- Controlled speed limit - if x > self.MAX_VELOCITY then - self:applyForce(-250, 0) - end - end +function Player:isWalkingLeft () + return self:isControlDown("left") +end + +function Player:isWalkingRight () + return self:isControlDown("right") end -- Controller callbacks. function Player:controlpressed (set, action, key) if set ~= self:getControllerSet() then return end self.smoke = false -- TODO: temporary - -- Jumping - if action == "jump" then - if self.jumpCounter > 0 then - -- General jump logics - self.isJumping = true - --self:playSound(6) - -- Spawn proper effect - if not self.inAir then - self:createEffect("jump") - else - self:createEffect("doublejump") - end - -- Start salto if last jump - if self.jumpCounter == 1 then - self.salto = true - end - -- Animation clear - if (self.current == self.animations.attack) or - (self.current == self.animations.attack_up) or - (self.current == self.animations.attack_down) then - self:setAnimation("default") - end - -- Remove jump - self.jumpCounter = self.jumpCounter - 1 - end - end -- Walking if (action == "left" or action == "right") then - self.isWalking = true if (self.current ~= self.animations.attack) and (self.current ~= self.animations.attack_up) and (self.current ~= self.animations.attack_down) then @@ -129,13 +84,11 @@ 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 if (action == "left" or action == "right") then if not (self:isControlDown("left") or self:isControlDown("right")) then - self.isWalking = false if self.current == self.animations.walk then self:setAnimation("default") end |