debug = true
lfs = require "lfs"
Tee = require "tee"

function lengthdir_x(length, dir)
	return math.cos(dir)*length
end

function lengthdir_y(length, dir)
	return math.sin(dir)*length
end

function point_direction(x1, y1, x2, y2)
	return math.atan2((y2 - y1), (x2 - x1))
end

function degtorad(dir)
	return dir*math.pi/180
end

function get_files(dir)
	local filetable = {}
	for file in lfs.dir(dir) do
		local g = dir.."/"..file
		local attr = lfs.attributes(g)
		assert(type(attr) == "table")
		if attr.mode ~= "directory" then
			table.insert(filetable, #filetable+1, file)
		end
	end
	return filetable
end

function love.load()
	direction = 0
	twskins = Tee.load_all_skins()
	tee = {}
	math.randomseed(os.time())
	tee.skin = math.floor(math.random(#twskins))
	isthisloss = Tee.create(twskins[tee.skin], 320-48, 240-48)
	nothisispatrick = Tee.create(twskins[tee.skin], 320+48, 240+48)
end

function love.update(dt)
	if love.keyboard.isDown("a") then
		isthisloss.hspeed = -200
	elseif love.keyboard.isDown("d") then
		isthisloss.hspeed = 200
	else
		isthisloss.hspeed = 0
	end
	isthisloss:update(dt, true)
end

function love.draw()
	love.graphics.setColor(255, 255, 255)
	love.graphics.rectangle("fill", 0, 0, 640, 480)
	love.graphics.setColor(0, 0, 0)
	love.graphics.print(twskins[tee.skin], 32, 32)
	love.graphics.setColor(255, 255, 255)
	
	Tee.draw()
end

function love.keypressed(key, unicode)
	if key == "c" then
		tee.skin = tee.skin+1
		if tee.skin > #twskins then tee.skin = 1 end
		Tee.delete(isthisloss.id)
		isthisloss = Tee.create(twskins[tee.skin], 320-48, 240-48)
	end
end