From 21880f5d084f04f0403301bac53dc9587eea198d Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 9 Apr 2018 20:27:54 +0200 Subject: Restructured Hero a little bit more --- not/Hero.lua | 85 ++++++++++++++++++++++++++++++------------------------------ 1 file changed, 43 insertions(+), 42 deletions(-) diff --git a/not/Hero.lua b/not/Hero.lua index 38eb749..7c818ee 100644 --- a/not/Hero.lua +++ b/not/Hero.lua @@ -176,6 +176,37 @@ function Hero:update (dt) end 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 + +--- Damps linear velocity every frame by applying minor force to body. +function Hero:dampVelocity (dt) + if not self:isWalking() then + local face + local x, y = self:getLinearVelocity() + if x < -12 then + face = 1 + elseif x > 12 then + face = -1 + else + face = 0 + end + self:applyForce(40*face,0) + if not self.inAir then + self:applyForce(80*face,0) + end + end +end + --- Called each time Hero starts walking. -- Is not called when direction of walking is changed. function Hero:onWalkingStarted () @@ -223,36 +254,6 @@ function Hero:onJumpStopped () self.jumpTimer = Hero.JUMP_TIMER end ---- Damps linear velocity every frame by applying minor force to body. -function Hero:dampVelocity (dt) - if not self:isWalking() then - local face - local x, y = self:getLinearVelocity() - if x < -12 then - face = 1 - elseif x > 12 then - face = -1 - else - face = 0 - end - self:applyForce(40*face,0) - if not self.inAir then - self:applyForce(80*face,0) - end - end -end - --- TODO: comment them and place them somewhere properly -function Hero:getAngle () - return self.angle -end -function Hero:getHorizontalMirror () - return self.facing -end -function Hero:getOffset () - return 12,15 -end - function Hero:draw (debug) if not self.isAlive then return end Hero.__super.draw(self, debug) @@ -321,6 +322,18 @@ function Hero:land () self:createEffect("land") end +function Hero:getAngle () + return self.angle +end + +function Hero:getHorizontalMirror () + return self.facing +end + +function Hero:getOffset () + return 12,15 +end + function Hero:isJumping () return false end @@ -337,18 +350,6 @@ 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`. -- cgit v1.1