From 6850f2af2572f7011c4f68953bdafe304f4cf842 Mon Sep 17 00:00:00 2001 From: Pascal Batty Date: Sun, 28 Apr 2019 19:28:55 +0200 Subject: [PATCH] add character movement acceleration/deceleration --- right_coin.lua | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/right_coin.lua b/right_coin.lua index 94d6dd2..b6e3fe0 100644 --- a/right_coin.lua +++ b/right_coin.lua @@ -158,18 +158,44 @@ do -- Player vx=0,vy=0, isFlipped=false } + + local maxSp=2 + local accel=0.3 + local decel=0.5 function Player:direct() if btn(2) then - self.vx = -2 - self.isFlipped = true + self:goLeft() elseif btn(3) then - self.vx = 2 - self.isFlipped = false - else self.vx = 0 + self:goRight() + else self:stop() end if btnp(4) and self.vy == 0 then self.vy = j end end + + function Player:goLeft() + if self.vx > 0 then self.vx=0 end + self.vx=self.vx - accel + if self.vx < -maxSp then self.vx=-maxSp end + self.isFlipped = true + end + + function Player:goRight() + if self.vx < 0 then self.vx=0 end + self.vx=self.vx + accel + if self.vx > maxSp then self.vx=maxSp end + self.isFlipped = false + end + + function Player:stop() + if self.vx < 0 then + self.vx= self.vx + decel + if self.vx > 0 then self.vx=0 end + elseif self.vx > 0 then + self.vx= self.vx - decel + if self.vx < 0 then self.vx=0 end + end + end function Player:update() self:resolve()