add character movement acceleration/deceleration

master
Pascal Batty 2019-04-28 19:28:55 +02:00
parent 3ecd48d11d
commit 6850f2af25
1 changed files with 31 additions and 5 deletions

View File

@ -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()