Added decel

This commit is contained in:
Logen Kain 2016-11-25 16:14:38 -07:00
parent b9393dda5f
commit d9c349b1c0

View File

@ -1,13 +1,28 @@
require "Tserial" require "Tserial"
function move(void) function move(accel, axis, direction)
-- with acceleration -- with acceleration
if accel and (direction == "right" or direction == "down")then
if player.current_speed < player.speed then if axis < player.speed then
player.current_speed = player.current_speed + (player.speed*player.accel) axis = axis + (player.speed*player.accel)
end end
return player.current_speed --note the NEGATIVE player.speed
elseif accel and (direction == "left" or direction == "up") then
if axis > -player.speed then
axis = axis - (player.speed*player.accel)
end
elseif not accel and (direction == "right" or direction == "down") then
if axis > 0 then
axis = axis - (player.speed*player.accel)
end
elseif not accel and (direction == "left" or direction == "up") then
if axis < 0 then
axis = axis + (player.speed*player.accel)
end
end
return axis
end end
@ -27,7 +42,11 @@ function love.load()
player.w = 25 player.w = 25
player.h = 25 player.h = 25
player.speed = 500 player.speed = 500
player.current_speed = 0 player.current_speed = {}
player.current_speed.left = 0
player.current_speed.right = 0
player.current_speed.up = 0
player.current_speed.down = 0
--Must be less than 1 --Must be less than 1
--if I understand correctly, the second number should be how many frames --if I understand correctly, the second number should be how many frames
--it takes to get to top speed --it takes to get to top speed
@ -57,29 +76,38 @@ function love.update(dt)
--Move character left or right --Move character left or right
if love.keyboard.isDown("right") then if love.keyboard.isDown("right") then
move() player.current_speed.right = move(true, player.current_speed.right, "right")
player.x = player.x + player.current_speed*dt
elseif love.keyboard.isDown("left") then elseif love.keyboard.isDown("left") then
move() player.current_speed.left = move(true, player.current_speed.left, "left")
player.x = player.x - player.current_speed*dt print (player.current_speed.left)
-- player.x = player.x - player.current_speed.left*dt
--Reset current speed if key is released
end end
if not love.keyboard.isDown("right") then
player.current_speed.right = move(false, player.current_speed.right, "right")
end
if not love.keyboard.isDown("left") then
player.current_speed.left = move(false, player.current_speed.left, "left")
end
player.x = player.x + (player.current_speed.right + player.current_speed.left)*dt
--Also able to move or up down at the same time as one of the above --Also able to move or up down at the same time as one of the above
if love.keyboard.isDown("up") then if love.keyboard.isDown("up") then
move() player.current_speed.up = move(true, player.current_speed.up, "up")
player.y = player.y - player.current_speed*dt
elseif love.keyboard.isDown ("down") then elseif love.keyboard.isDown ("down") then
move() player.current_speed.down = move(true, player.current_speed.down, "down")
player.y = player.y + player.current_speed*dt
end end
if not love.keyboard.isDown("right", "left", "up", "down") then if not love.keyboard.isDown("up") then
player.current_speed = 0 player.current_speed.up = move(false, player.current_speed.up, "up")
end
if not love.keyboard.isDown("down") then
player.current_speed.down = move(false, player.current_speed.down, "down")
end end
player.y = player.y + (player.current_speed.up + player.current_speed.down)*dt
end end
function love.draw() function love.draw()