var gTime = 4096;
var mainData;
var music;

var init = CreateTrigger(function()
{
	mainData = GameLoadData("main");
	if(mainData == null)
	{
		mainData = {};
	}
	PreloadTexture("SkyCubeMap.jpg");
	PreloadTexture("SkyCubeMap2.jpg");
	PreloadTexture("SkyCubeMap3.jpg");
	music = SoundPlayMusic("Above Cloudsdale.ogg", true);
	SoundSetVolume(music, 1);
	EnvironmentApplySkyboxTexture("SkyCubeMap.jpg", 0);
	EnvironmentApplyGlobalLight("Day", 0);
	CameraApplyFree("F", 0);
	RegionAttachCharacter("pieAround", "pinkie");
});
AddTriggerInitialize(init);

function lighting()
{
	while(true)
	{
		EnvironmentApplySkyboxTexture("SkyCubeMap2.jpg", gTime);
		EnvironmentApplyGlobalLight("Dusk", gTime);
		Wait(gTime+1);
		EnvironmentApplySkyboxTexture("SkyCubeMap3.jpg", gTime);
		EnvironmentApplyGlobalLight("Night", gTime);
		Wait(gTime+1);
		EnvironmentApplySkyboxTexture("SkyCubeMap.jpg", gTime*2);
		EnvironmentApplyGlobalLight("Dawn", gTime);
		Wait(gTime+1);
		EnvironmentApplyGlobalLight("Day", gTime);
		Wait(gTime+1);
	}
}


AddEventElapsed(0, intro);
AddEventElapsed(0, lighting);

function intro()
{
	math.srand(int(os.now()));
	DebugStartPerfTracking();
	UIShowUI("timer", false);
	SoundSetVolume(music, 0.3);
	CharacterEnableMove("player", false);
	CharacterShow("player", false);
	CharacterOrder("pinkie", "JUMPMOVE", Vec3(90, 0, 150), 12);
	CameraApplyFixed("Intro", 0);
	Wait(8);
	CharacterPlayMorphAnimation("pinkie", "talk");
	UIDisplayConversation("Intro/PP01");
	CharacterStopMorphAnimation("pinkie", "talk");
	
	SoundSetVolume(music, 1.0);
	CharacterEnableMove("player", true);
	CharacterShow("player", true);
	CameraApplyFree("F", 0);
	hide();
}

var hidePos = [Vec3(-700, 0, -400), Vec3(-400, 0, -850), Vec3(200, 0, -1000), Vec3(700, 0, -650), Vec3(950, 0, -50),
	Vec3(-250,0,350), Vec3(450,0,450), Vec3(-700,0,50), Vec3(-1350,0,500), Vec3(-1200,0,50)];

var half_sec = 1 / 8.0;

var g_timerHelp = 0;
var g_timerGame = 0;
var g_startTime = 0;
var g_level = 0;


function getMissionTime()
{
	return (30 - (g_level / 3) * 5)*16;
}

function hide()
{
	var r = math.rand() % len(hidePos);
	trigPieFind.turn = false;
	CharacterPlayMorphAnimation("pinkie", "talk");
	UIShowUI("meterbar", false);
	UIDisplayConversation("Hide/PP01");
	UIDisplayConversation("Hide/PP02");
	UIShowUI("meterbar", true);
	CharacterStopMorphAnimation("pinkie", "talk");
	WipeHorizontal(0.5, 0, half_sec);
	Wait(8);
	CharacterOrder("pinkie", "JUMPMOVE", hidePos[r], 30);
	CharacterEnableMove("player", false);
	Wait(16 * 3);
	CharacterRelocate("pinkie", hidePos[r]);
	UICountdown(5);
	Wait(16 * 5);
	WipeHorizontal(0.5, 1, -half_sec);
	CharacterEnableMove("player", true);
	
	g_timerHelp = TimerCreate(getMissionTime() - 16*7, function()
	{
		CharacterOrder("pinkie", "JUMP", Vec3(0, 0, 0));
		CharacterRotate("player", fromToAngle(CharacterGetPosition("player"), CharacterGetPosition("pinkie")), 4);
		if(math.rand() % 2 == 0)
		{
			UIDisplayConversation("Help/PP01");
		}
		else
		{
			UIDisplayConversation("Help/PP02");
		}
	});
	g_startTime = GameGetMissionTime();
	g_timerGame = TimerCreate(getMissionTime(), fail);
	TimerShow(g_timerGame, true);
	UIShowUI("timer", true);
	trigPieFind.turn = true;
}

function fail()
{
	trigPieFind.turn = false;
	TimerClear(g_timerHelp);
	TimerClear(g_timerGame);
	UIShowUI("timer", false);
	CharacterOrder("pinkie", "JUMPMOVE", CharacterGetPosition("player"));
	CharacterPlayMorphAnimation("pinkie", "talk");
	UIShowUI("meterbar", false);
	UIDisplayConversation("Fail/PP01");
	showScore();
}

function showScore()
{
	/*var dlg = UICreateDlg("결과", 500, 400);
	DlgAddStatic(dlg, "레벨:  " + (g_level+1), 50, 48, 400, 32);
	DlgAddStatic(dlg, "점수:  " + (GameGetScore()), 50, 80, 400, 32);
	DlgAddStatic(dlg, "플레이 시간:  " + (GameGetMissionTime() / 16) + "초", 50, 112, 400, 32);*/
	var dlg = DebugCreatePerfDlg();
	UIDisplayConversation("Fail/PP02");
	UIShowUI("meterbar", true);
	CharacterStopMorphAnimation("pinkie", "talk");
	var res = UIShowSelectDlg(["Dlg/Retry", "Dlg/Cancel"]);
	if(res == 0)
	{
		g_level = 0;
		GameSetScore(0);
		//GameSetMissionTime(0);
		UIDestroyDlg(dlg);
		DebugStartPerfTracking();
		hide();
	}
	else
	{
		GameQuit();
		UIDestroyDlg(dlg);
	}
}

function fromToAngle(from, to)
{
	var d = to.Sub(from);
	return math.atan2(d.z, d.x);
}

var trigPieFind = CreateTrigger(function()
{
	this.turn = false;
	UIShowUI("meterbar", false);
	UIShowUI("timer", false);
	TimerClear(g_timerHelp);
	TimerClear(g_timerGame);
	
	var remainTime = getMissionTime() - (GameGetMissionTime() - g_startTime);
	GameSetScore(GameGetScore() + remainTime * 100 / 16 * ((g_level+3)/3));
	
	SoundSetVolume(music, 0.3);
	CharacterEnableMove("player", false);
	CharacterRotate("pinkie", fromToAngle(CharacterGetPosition("pinkie"), CharacterGetPosition("player")), 4);
	CharacterOrder("pinkie", "JUMP", Vec3(0, 0, 0));
	CharacterPlayMorphAnimation("pinkie", "talk");
	if(math.rand() % 2 == 0)
	{
		UIDisplayConversation("Hide/PP03");
	}
	else
	{
		UIDisplayConversation("Misc/PP01");
	}
	CharacterStopMorphAnimation("pinkie", "talk");
	CharacterEnableMove("player", true);
	SoundSetVolume(music, 1);
	UIShowUI("meterbar", true);
	g_level = g_level + 1;
	if(g_level >= 15)
	{
		showScore();
	}
	else
	{
		this.turn = true;
		hide();
	}
});
AddTriggerEnter("player", "pieAround", true, trigPieFind);

