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"
--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)
-- with acceleration
@ -25,39 +32,60 @@ function move(accel, axis, direction)
return axis
end
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")
saveFile = "test.lua"
-- Check if Save file exists
save_exists = love.filesystem.exists(saveFile)
-- Create a table to hold all the player data
player = {}
-- Player x/y position
player.x = 100
player.y = 100
-- player height/width
player.w = 25
player.h = 25
--Player top speed
player.speed = 500
player.x = 100
player.y = 100
player.w = 25
player.h = 25
player.speed = 500
player.current_speed = {}
player.current_speed.left = 0
player.current_speed.right = 0
player.current_speed.up = 0
player.current_speed.down = 0
player.current_speed.x = 0
player.current_speed.y = 0
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
--create a table to hold all the speed data
--Yay multi-dimensional arrays!
player.current_speed = {}
--Left right speed values of the player
player.current_speed.left = 0
player.current_speed.right = 0
--Actual x speed
player.current_speed.x = 0
-- Up down speed values of the player
player.current_speed.up = 0
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
-- 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)
if key == "q" then
--quit
if key == "q" then
love.event.quit()
end
@ -72,7 +100,8 @@ function love.keyreleased(key)
player = Tserial.unpack( love.filesystem.read( saveFile ) )
end
end
-- Engine kill
if key == "z" then
if player.kill_engines == false then
player.kill_engines = true
@ -87,35 +116,44 @@ function love.update(dt)
--Move character left or right
if love.keyboard.isDown("right") then
--All directional key presses restart the engines
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")
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
player.kill_engines = false
player.current_speed.left = move(true, player.current_speed.left, "left")
print ("LEFT ".. player.current_speed.left)
-- player.x = player.x - player.current_speed.left*dt
print ("LEFT ".. math.floor(player.current_speed.left) .. " TOTAL :" ..
math.floor(player.current_speed.x))
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
--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
player.kill_engines = false
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
player.killengines = false
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
player.y = player.y + (player.current_speed.up + player.current_speed.down)*dt
-- Decel
-- We check our kill engine flags to see if we even bother with this
if not player.kill_engines then
if not love.keyboard.isDown("up") then
player.current_speed.up = move(false, player.current_speed.up, "up")
@ -132,8 +170,17 @@ 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