add character movement acceleration/deceleration

This commit is contained in:
Pascal Batty 2019-04-28 19:28:55 +02:00
parent 3ecd48d11d
commit 6850f2af25

View file

@ -159,18 +159,44 @@ do -- Player
isFlipped=false isFlipped=false
} }
local maxSp=2
local accel=0.3
local decel=0.5
function Player:direct() function Player:direct()
if btn(2) then if btn(2) then
self.vx = -2 self:goLeft()
self.isFlipped = true
elseif btn(3) then elseif btn(3) then
self.vx = 2 self:goRight()
self.isFlipped = false else self:stop()
else self.vx = 0
end end
if btnp(4) and self.vy == 0 then self.vy = j end if btnp(4) and self.vy == 0 then self.vy = j end
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() function Player:update()
self:resolve() self:resolve()
self.x = self.x + self.vx self.x = self.x + self.vx