--[[
Author: Will Graham (wgraham@digipen.edu)

Copyright 2008-2009 DigiPen (USA) Corp.
]]--

--print("Functions.lua: Loading Functions...")

function PassSpawnController(controller)
	--print("GlobalSpawnController passed succesfully")
	GlobalSpawnController = controller
end

function SpawnTower(width, len, height, pos, scale, rot, mass, table, type)
	Start = Vector3D(pos.x - (width / 2) * scale.x + scale.x / 2, pos.y - (len / 2) * scale.y + scale.y / 2, pos.z);

	totalBlocks = 0
	for k = 0, height - 1 do
		for i = 0, width - 1 do
			for j = 0, len - 1 do
				pos = Vector3D(Start.x + i * (scale.x + 0.2), Start.y + j * (scale.y + 0.2), Start.z + k * (scale.z + 0.2))
				vel = Vector3D(0.0, 0.0, 0.0)
				totalBlocks = totalBlocks + 1
				table[totalBlocks] = GlobalSpawnController:SpawnBlock(pos, vel, scale, rot, mass, type)
			end
		end
	end
	return totalBlocks;
end

function SpawnTower_Info(Info)
	GlobalSpawnController:SpawnTower(
		Info.Width_,
		Info.Length_,
		Info.Height_,
		Info.Pos_,
		Info.Scale_,
		Vector3D(0.0, 0.0, 0.0),
		1.0,
		false,
		6)
end

function SpawnTowerInfo(Info, Insert, Sleep, Platform)
	Platform = Platform or false
	return GlobalSpawnController:SpawnTower(
		Info.Width_,
		Info.Length_,
		Info.Height_,
		Info.Pos_,
		Info.Scale_,
		Info.Rot_,
		Info.Mass_,
		Insert,
		Sleep,
		4,
		Platform)
end
function SpawnTowerPatternInfo(Info, Insert, Sleep, Platform)
	Platform = Platform or false
	return GlobalSpawnController:SpawnTowerPattern(
		Info.Width_,
		Info.Length_,
		Info.Height_,
		Info.Pos_,
		Info.Scale_,
		Info.Rot_,
		Info.Mass_,
		Insert,
		Sleep,
		4,
		Platform)
end
function SpawnPillarInfo(Info, Insert, Sleep, Platform)
	Platform = Platform or false
	return GlobalSpawnController:SpawnPillar(
		Info.Width_,
		Info.Length_,
		Info.Height_,
		Info.Pos_,
		Info.Scale_,
		Info.Rot_,
		Info.Mass_,
		Insert,
		Sleep,
		4,
		Platform)
end
	
--Object* SpawnTrigger(Vector3D pos, Vector3D scale, char* luafile, bool visual = false, bool file = true);
function SpawnTriggerInfo(Info, VisualFlag, FileFlag, Script)
	return GlobalSpawnController:SpawnTrigger(
		Info.Pos_,
		Info.Scale_,
		Script,
		VisualFlag,
		FileFlag,
		Info.Rot_)
end

function SpawnDoomWallInfo(Info)
	return GlobalSpawnController:SpawnDoomWall(
		Info.Pos_,
		Info.Scale_,
		Info.Rot_,
		Vector3D(0.0, 0.0, 0.0))
end

--Object* SpawnRock(Vector3D pos, Vector3D vel, float radius = 1.0f, float mass = 10.0f);
function SpawnRockInfo(Info, Velocity)
	return GlobalSpawnController:SpawnRock(
		Info.Pos_,
		Velocity,
		Info.Scale_.x / 2,
		200.0)
end

function SpawnPlatformInfo(Info)
	return GlobalSpawnController:SpawnPlatform(
		Info.Pos_,
		Info.Scale_,
		Info.Rot_)
end

function SpawnDeathBoundaryInfo(Info)
	return GlobalSpawnController:SpawnDeathBoundary(
		Info.Pos_,
		Info.Scale_,
		Info.Rot_)
end

function ColorTowards2(Button, r, g, b, a, speed)

	local ColorSpeed = speed

	if Button.R < r then
		Button:SetR(Button.R + ColorSpeed)
		if Button.R > r then
			Button:SetR(r)
		end
	elseif Button.R > r then
		Button:SetR(Button.R - ColorSpeed)
		if Button.R < r then
			Button:SetR(r)
		end
	end
	
	if Button.G < g then
		Button:SetG(Button.G + ColorSpeed)
		if Button.G > g then
			Button:SetG(g)
		end
	elseif Button.G > g then
		Button:SetG(Button.G - ColorSpeed)
		if Button.G < g then
			Button:SetG(g)
		end
	end
	
	if Button.B < b then
		Button:SetB(Button.B + ColorSpeed)
		if Button.B > b then
			Button:SetB(b)
		end
	elseif Button.B > b then
		Button:SetB(Button.B - ColorSpeed)
		if Button.B < b then
			Button:SetB(b)
		end
	end
	
	if Button.A < a then
		Button:SetA(Button.A + ColorSpeed)
		if Button.A > a then
			Button:SetA(a)
		end
	elseif Button.A > a then
		Button:SetA(Button.A - ColorSpeed)
		if Button.A < a then
			Button:SetA(a)
		end
	end
end

function MoveTowards(Button, x, y, xspeed, yspeed)
	if Button.X > x then
		Button:SetX(Button.X - xspeed)
		if Button.X < x then
			Button:SetX(x);
		end
	else
		Button:SetX(Button.X + xspeed)
		if Button.X > x then
			Button:SetX(x);
		end
	end
	
	if Button.Y > y then
		Button:SetY(Button.Y - yspeed)
		if Button.Y < y then
			Button:SetY(y);
		end
	else
		Button:SetY(Button.Y + yspeed)
		if Button.Y > y then
			Button:SetY(y);
		end
	end
end

function ResizeScreen(width, height)
	wDiff = width - ScreenWidth
	hDiff = height - ScreenHeight + BarHeight
	
	ScreenWidth = width
	ScreenHeight = height
	FadeBackground:SetWidth(width)
	FadeBackground:SetHeight(height)
	
	BarHeight = CalculateBarHeight(width, height)
	BarWidth = ScreenWidth;
	
	hDiff = hDiff - BarHeight;
	
	CinematicBar1:SetWidth(BarWidth);
	CinematicBar1:SetHeight(BarHeight);
	CinematicBar2:SetWidth(BarWidth);
	CinematicBar2:SetHeight(BarHeight + 10);
	CinematicBar2:SetY(CinematicBar2.Y + hDiff)
	
	LevelTitleWidth = ScreenWidth / 2
	LevelTitleHeight = LevelTitleWidth / LevelTitleRatio
	LevelTitleX = ScreenWidth / 2 - LevelTitleWidth / 2
	--LevelTitleX = 0
	--LevelTitleX = ScreenWidth - LevelTitleWidth
	--LevelTitleY = ScreenHeight - LevelTitleHeight - (BarHeight - LevelTitleHeight) / 2
	LevelTitleY = ScreenHeight / 2 - LevelTitleHeight / 2
	--LevelTitleY = 0
	
	for i = 1, 4 do
		LevelTitles[i]:SetWidth(LevelTitleWidth)
		LevelTitles[i]:SetHeight(LevelTitleHeight)
		LevelTitles[i]:SetX(LevelTitleX)
		LevelTitles[i]:SetY(LevelTitleY)
	end
	
	
	LoadingRatio = 256.0 / 128.0
	LoadingWidth = ScreenWidth / 8
	LoadingHeight = LoadingWidth / LoadingRatio
	LoadingX = 0
	LoadingY = ScreenHeight - LoadingHeight
	LoadingScreen:SetX(LoadingX)
	LoadingScreen:SetY(LoadingY)
	LoadingScreen:SetWidth(LoadingWidth)
	LoadingScreen:SetHeight(LoadingHeight)
	
end

function ChangeLevel(num)
	GameplayState:ChangeLevel(num)
end

function ToggleDebugInfo()
	if GameEngine.DebugInfo == true then
		GameEngine.DebugInfo = false
	else
		GameEngine.DebugInfo = true
	end
end

function ToggleDevKeys()
	if GameEngine.DevKeys == true then
		GameEngine.DevKeys = false
	else
		GameEngine.DevKeys = true
	end
end

function SpawnBlockDeleterInfo(info, vel, deleteTime)
	GlobalSpawnController:SpawnBlockDeleter(info.Pos_, info.Scale_, info.Rot_, vel, deleteTime);
end

function CalculateBarHeight(Width, Height)
	barHeight = Height / 8
	return barHeight
end

function InsertObjectRange(Towers, index1, index2)
	for i = index1, index2 do
		Towers[i]:InsertIntoWorld();
	end
end



--print("Functions.lua: Functions Loaded.")
--print("")