require "Tserial"

--Detect when player hits screen edges
function colDetect()
	-- Is it above 0? Awesome, we're probably on screen
	if player.x >= 0 then
		-- Is it below the screen width minus the plaers width?
		if player.x >= (screen.w - player.w) then
			--No? Guess we better stop our guy from going off screen
			--by setting his position to the screen edge
			player.x = screen.w - player.w
		end
	-- Oh, we weren't above or equal to 0? 
	-- Obviously it can't be above the max if it is 0 or less
	-- so let's just set it to 0
	else player.x = 0
	
	end

	-- Same as above except for y
	if player.y >= 0 then
		if player.y >= (screen.h - player.h) then
			player.y = screen.h - player.h
		end
	else
		player.y = 0
	end
	return player.x, player.y
end
--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
		if accel and (direction == "right" or direction == "down")then
			if axis < player.speed then
				axis = axis + (player.speed*player.accel)
			end
		--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

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 and grab the screen width/height for colision detection
	screen = {}
	screen.w = love.graphics.getWidth()
	screen.h = love.graphics.getHeight()
	
	-- Create a table to hold all the player data
	player = {}
	-- Player x/y position
	player.x = 250
	player.y = 250
	-- player height/width
	player.w = 25
	player.h = 25
	
	--Player top speed
	player.speed = 500

	--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)
  --quit  
	if key == "q" then
      love.event.quit()
   end

	-- save
	if key == "s" then
		love.filesystem.write(saveFile, Tserial.pack(player, false, true))
	end

	-- Load
	if key == "l" then
		if save_exists then
			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
		else 
			player.kill_engines = false
		end
	end

end

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: " .. 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 ".. math.floor(player.current_speed.left) .. " TOTAL :" .. 
										 math.floor(player.current_speed.x))
	end

	--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
	
	-- 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")
		end
		if not love.keyboard.isDown("down") then
			player.current_speed.down = move(false, player.current_speed.down, "down")
		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
	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
	
	--Make sure player doesn't go off screen
	colDetect()
end

function love.draw()
	--draw the rectangle. Fill/line for the type, 
	--(x,y, width, height)
	love.graphics.rectangle("line", player.x, player.y, player.w, player.h)
end