Tommy: Added tons of comments; added total speed check

This commit is contained in:
Logen Kain 2016-11-25 17:13:59 -07:00
parent 117d58ad61
commit fe2e540963

View File

@ -1,5 +1,12 @@
require "Tserial" require "Tserial"
--Create a function to deal with acceleration
--accel is just a TRUE/False flag that tells us if we want to accel or decel
--axis is directional speed, shoudl probably change its name
--direction is direction, up/down/left/right
--
--left and down return negative values
--up and right return positive numbers... so standard grid
function move(accel, axis, direction) function move(accel, axis, direction)
-- with acceleration -- with acceleration
@ -25,39 +32,60 @@ function move(accel, axis, direction)
return axis return axis
end end
function love.load() function love.load()
--This should probably go in the config file once I learn how those work
--Name of the program Determines things like what folder it saves in
love.filesystem.setIdentity("tommy") love.filesystem.setIdentity("tommy")
saveFile = "test.lua" saveFile = "test.lua"
-- Check if Save file exists -- Check if Save file exists
save_exists = love.filesystem.exists(saveFile) save_exists = love.filesystem.exists(saveFile)
-- Create a table to hold all the player data
player = {} player = {}
-- Player x/y position
player.x = 100
player.y = 100
-- player height/width
player.w = 25
player.h = 25
player.x = 100 --Player top speed
player.y = 100 player.speed = 500
player.w = 25
player.h = 25 --create a table to hold all the speed data
player.speed = 500 --Yay multi-dimensional arrays!
player.current_speed = {} player.current_speed = {}
player.current_speed.left = 0
player.current_speed.right = 0 --Left right speed values of the player
player.current_speed.up = 0 player.current_speed.left = 0
player.current_speed.down = 0 player.current_speed.right = 0
player.current_speed.x = 0
player.current_speed.y = 0 --Actual x speed
player.kill_engines = false player.current_speed.x = 0
--Must be less than 1
--if I understand correctly, the second number should be how many frames -- Up down speed values of the player
--it takes to get to top speed player.current_speed.up = 0
player.accel = 1/120 player.current_speed.down = 0
--Actual y speed
player.current_speed.y = 0
-- Kill engines flag to disable deceleration
player.kill_engines = false
--Must be less than 1
--if I understand correctly, the second number should be how many frames
--it takes to get to top speed
player.accel = 1/120
end end
-- I choose to use this function for keys I don't want to repeat
-- For example, using the keyboard.isDown() function for saving
-- means that if I hold the key down, it's constantly writing data
-- No good for the hdd
function love.keyreleased(key) function love.keyreleased(key)
if key == "q" then --quit
if key == "q" then
love.event.quit() love.event.quit()
end end
@ -73,6 +101,7 @@ function love.keyreleased(key)
end end
end end
-- Engine kill
if key == "z" then if key == "z" then
if player.kill_engines == false then if player.kill_engines == false then
player.kill_engines = true player.kill_engines = true
@ -87,35 +116,44 @@ 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
--All directional key presses restart the engines
player.kill_engines = false player.kill_engines = false
--We use my move function to deal with acceleration
--we also track left, right, up, down speed seperately
player.current_speed.right = move(true, player.current_speed.right, "right") player.current_speed.right = move(true, player.current_speed.right, "right")
print ("RIGHT ".. player.current_speed.right) print ("RIGHT: " .. math.floor(player.current_speed.right) .. " Total: " ..
math.floor(player.current_speed.x))
elseif love.keyboard.isDown("left") then elseif love.keyboard.isDown("left") then
player.kill_engines = false player.kill_engines = false
player.current_speed.left = move(true, player.current_speed.left, "left") player.current_speed.left = move(true, player.current_speed.left, "left")
print ("LEFT ".. player.current_speed.left) print ("LEFT ".. math.floor(player.current_speed.left) .. " TOTAL :" ..
-- player.x = player.x - player.current_speed.left*dt math.floor(player.current_speed.x))
end 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
--i.e. I only used elseif to prevent left/right from working at the same time
--as well as up/down
--I may change this now that acceleration and deceleration are working properly
if love.keyboard.isDown("up") then if love.keyboard.isDown("up") then
player.kill_engines = false player.kill_engines = false
player.current_speed.up = move(true, player.current_speed.up, "up") player.current_speed.up = move(true, player.current_speed.up, "up")
print ("UP ".. math.floor(player.current_speed.up) .. " Total:" ..
math.floor(player.current_speed.y))
elseif love.keyboard.isDown ("down") then elseif love.keyboard.isDown ("down") then
player.killengines = false player.killengines = false
player.current_speed.down = move(true, player.current_speed.down, "down") player.current_speed.down = move(true, player.current_speed.down, "down")
print ("DOWN ".. math.floor(player.current_speed.down) .. " Total: "..
math.floor(player.current_speed.y))
end end
player.y = player.y + (player.current_speed.up + player.current_speed.down)*dt
-- Decel -- Decel
-- We check our kill engine flags to see if we even bother with this
if not player.kill_engines then if not player.kill_engines then
if not love.keyboard.isDown("up") then if not love.keyboard.isDown("up") then
player.current_speed.up = move(false, player.current_speed.up, "up") player.current_speed.up = move(false, player.current_speed.up, "up")
@ -133,7 +171,16 @@ function love.update(dt)
end end
-- Really just created these so I can see total speed stats
player.current_speed.x = (player.current_speed.right + player.current_speed.left)
player.current_speed.y = (player.current_speed.up + player.current_speed.down)
--Remember how we have our left/down speed as a negative number?
--It makes it simple to simply add the two x's and the two y's together
--to get a total speed for each and then reassign the player.x and player.y
--corrs to their new position for love.draw to redraw them
player.x = player.x + (player.current_speed.x)*dt
player.y = player.y + (player.current_speed.y)*dt
end end