From d9c349b1c09cdb8dc2a1304b099adc1f5b8646c0 Mon Sep 17 00:00:00 2001 From: Logen Kain Date: Fri, 25 Nov 2016 16:14:38 -0700 Subject: [PATCH] Added decel --- love/tommy/main.lua | 68 ++++++++++++++++++++++++++++++++------------- 1 file changed, 48 insertions(+), 20 deletions(-) diff --git a/love/tommy/main.lua b/love/tommy/main.lua index c030d3b..bd8092a 100644 --- a/love/tommy/main.lua +++ b/love/tommy/main.lua @@ -1,13 +1,28 @@ require "Tserial" -function move(void) +function move(accel, axis, direction) -- with acceleration - - if player.current_speed < player.speed then - player.current_speed = player.current_speed + (player.speed*player.accel) + if accel and (direction == "right" or direction == "down")then + if axis < player.speed then + axis = axis + (player.speed*player.accel) 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 @@ -27,7 +42,11 @@ function love.load() player.w = 25 player.h = 25 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 --if I understand correctly, the second number should be how many frames --it takes to get to top speed @@ -57,29 +76,38 @@ function love.update(dt) --Move character left or right if love.keyboard.isDown("right") then - move() - player.x = player.x + player.current_speed*dt + player.current_speed.right = move(true, player.current_speed.right, "right") elseif love.keyboard.isDown("left") then - move() - player.x = player.x - player.current_speed*dt - - --Reset current speed if key is released + player.current_speed.left = move(true, player.current_speed.left, "left") + print (player.current_speed.left) +-- player.x = player.x - player.current_speed.left*dt 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 if love.keyboard.isDown("up") then - move() - player.y = player.y - player.current_speed*dt + player.current_speed.up = move(true, player.current_speed.up, "up") elseif love.keyboard.isDown ("down") then - move() - player.y = player.y + player.current_speed*dt - end - - if not love.keyboard.isDown("right", "left", "up", "down") then - player.current_speed = 0 + player.current_speed.down = move(true, player.current_speed.down, "down") end + if not love.keyboard.isDown("up") then + 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 + + player.y = player.y + (player.current_speed.up + player.current_speed.down)*dt end function love.draw()