diff options
Diffstat (limited to 'not/Hero.lua')
-rw-r--r-- | not/Hero.lua | 86 |
1 files changed, 82 insertions, 4 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`. |