#include "script/common.lua"

enabled = false
fade = 0

gCredits = 
{
	{
		title = "Art Vandals by",
		content = {"Tuxedo Labs"},
		pos = "upper left",
		t0 = 3,
		t1 = 10,
	},
	{
		title = "Äìëãíñ ùôòåñéí",
		content = {"Emil Bengtsson", "Olle Lundahl"},
		pos = "upper right",
		t0 = 11,
		t1 = 20,
	},
	{
		title = "Àôö èìôéîöòô",
		content = {"John Kearney"},
		pos = "upper left",
		t0 = 21,
		t1 = 30,
	},
	{
		title = "Íùëºîã",
		content = {"Douglas Holmquist"},
		pos = "upper right",
		t0 = 31,
		t1 = 40,
	},
	{
		title = "Gameplay programming",
		content = {"Olle Lundahl"},
		pos = "upper left",
		t0 = 41,
		t1 = 50,
	},
	{
		title = "Ñùîòåòèìöéï¼ óôòéîöã",
		content = {"Marcus Dawson"},
		pos = "upper right",
		t0 = 51,
		t1 = 60,
	},
	{
		title = "Additional programming",
		content = {"Jim Winberg", "Dennis Gustafsson"},
		pos = "upper left",
		t0 = 61,
		t1 = 70,
	},
	{
		title = "Additional artwork",
		content = {"Saber Interactive"},
		pos = "upper right",
		t0 = 71,
		t1 = 80,
	},
	{
		title = "Ïõòäñ¿î Âù",
		content = {"Kabi Jedhagen"},
		pos = "upper left",
		t0 = 81,
		t1 = 90,
	},
	{
		title = "Áïãçòèãôìð åãõ ëã ìçôù!",
		content = {""},
		pos = "littleup",
		t0 = 91,
		t1 = 100,
	},
	
}

gTime = 0
gMusicVol = 1

function init()
	vehicle = FindVehicle("epamonstertruck", true)
end

function tick(dt)
	local inVehicle = GetPlayerVehicle() == vehicle
	if inVehicle then
		enabled = true
	end
	
	if inVehicle then
		SetBool("hud.disable", true)
		gTime = gTime + dt
		if fade == 0 then
			SetValue("fade", 1.0, "easeout", 0.3)
		end
	else
		local dist = VecLength(VecSub(GetPlayerTransform().pos, GetVehicleTransform(vehicle).pos))
		dist = math.max(dist, 1.0)*0.1;
		gMusicVol = clamp(1.05/(dist*dist)-0.05, 0.0, 1.0)
		if fade == 1 then
			SetValue("fade", 0.0, "easein", 0.3)
		end
	end

	local vol = fade + gMusicVol*(1-fade)
	if vol > 0.01 then
		PlayMusic("MOD/music/ending.ogg")
	end
	SetFloat("game.music.volume", vol)
	SetFloat("game.music.lowpass", 1.0-fade)
end


function drawCredits()
	for i=1, #gCredits do
		local s = gCredits[i]
		if gTime > s.t0 and gTime < s.t1 then
			UiPush()
			UiAlign("left")
			local width = 250
			local height = 32 + #s.content*36
			if string.find(s.pos, "lower") then
				UiTranslate(0, UiHeight() - 250 - height)
			elseif string.find(s.pos, "upper") then
				UiTranslate(0, 250)
			elseif string.find(s.pos, "littleup") then
				UiTranslate(0, 350)
			else
				UiTranslate(0, UiMiddle() - height/2)
			end
			if string.find(s.pos, "left") then
				UiTranslate(300, 0)
			elseif string.find(s.pos, "right") then
				UiTranslate(UiWidth() - 300 - width, 0)
			else
				UiTranslate(UiCenter()-width/2, 0)
			end
			if string.find(s.pos, "centered") then
				UiTranslate(width/2, 0)
				UiAlign("center")
			end	
			if s.scale then
				UiScale(s.scale)
			end
			local alpha = 1.0
			local fadeTime = 1.0
			local t = gTime - s.t0
			local duration = s.t1 - s.t0
			if t < fadeTime then
				alpha = t/fadeTime
			elseif t > duration-fadeTime then
				alpha = (duration-t)/fadeTime
			end
			UiColor(1,1,1,alpha)
			UiTextOutline(0,0,0,alpha,0.2)
			UiFont("bold.ttf", 32)
			UiText(s.title)
			UiFont("regular.ttf", 40)
			UiTranslate(0, UiFontHeight())
			for j=1, #s.content do
				UiText(s.content[j])		
				UiTranslate(0, 36)
			end
			UiPop()
		end
	end
end


function draw()
	if enabled then
		UiPush()
			UiColorFilter(1,1,1,fade)
			drawCredits()
		UiPop()
		UiPush()
			UiColor(0,0,0)
			UiRect(UiWidth(), 80*fade)
			UiTranslate(0, UiHeight()-80*fade)
			UiRect(UiWidth(), 80)
		UiPop()
	end
end
