#include "./functions.txt"
let dirCurrent = GetCurrentScriptDirectory();

let typeEnd = 0;
let END_FAILED = 1;
let END_SUCCESS = 2;
let END_TIMEOUT = 3;

@Initialize
{
	LoadCommonDataAreaA2("UPGRADE",GetCurrentScriptDirectory ~ "data/UPGRADE");

	InitFrame();
	TScore();
	TGraze();
	TBossLife();
	TBossTimer();
	TDifficulty();
	TCurrentFps();
	TReplayFps();

	SetItemRenderPriorityI(30);
}

@MainLoop
{
	yield;
}

@Event
{
	alternative(GetEventType())
	case(EV_START_BOSS_STEP)
		{
		let objScene = GetEnemyBossSceneObjectID();
		typeEnd = END_SUCCESS;		
		}
	case(EV_PLAYER_SHOOTDOWN)
		{
		if(typeEnd == END_SUCCESS)
			{
			typeEnd = END_FAILED;
			}
		}
	case(EV_TIMEOUT)
		{
		typeEnd = END_TIMEOUT;
		}
	case(EV_END_BOSS_STEP)
		{
		if(typeEnd == 0)
			{
			typeEnd = END_FAILED;
			}
		let objScene = GetEnemyBossSceneObjectID();
		if(ObjEnemyBossScene_GetInfo(objScene,INFO_IS_SPELL) == true && (typeEnd == END_SUCCESS ||(ObjEnemyBossScene_GetInfo(objScene,INFO_IS_DURABLE_SPELL) && typeEnd == END_TIMEOUT)))
			{
			let score = ObjEnemyBossScene_GetInfo(objScene, INFO_SPELL_SCORE);
			TGainSpell(score);
			}
		if(ObjEnemyBossScene_GetInfo(objScene,INFO_IS_SPELL) == true)
			{
			let path = dirCurrent ~ "Default_System_MagicCircle.txt";
			let id = LoadScript(path);
			StartScript(id);	
			}
		}
}

//----------------------------------------------------
//Initialization
//----------------------------------------------------
function InitFrame()
{
	let path = GetCurrentScriptDirectory() ~ "img/Default_SystemBackground.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, path);
	Obj_SetRenderPriority(obj, 0);
	ObjSprite2D_SetSourceRect(obj, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
	ObjSprite2D_SetDestRect(obj, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
	SetStgFrame(121,21,518,458,20,80);
}

//----------------------------------------------------
//Player Score
//----------------------------------------------------
task TScore()
{
	//Score
	let objScore = ObjText_Create();
	ObjText_SetText(objScore, "Score");
	ObjText_SetFontType(objScore,"Century Gothic");
	ObjText_SetFontSize(objScore, 22);
	ObjText_SetFontBold(objScore, true);
	ObjText_SetFontColorTop(objScore, 255, 255, 255);
	ObjText_SetFontColorBottom(objScore, 255, 255, 255);
	ObjText_SetFontBorderType(objScore, BORDER_FULL);
	ObjText_SetFontBorderColor(objScore,128, 128, 128);
	ObjText_SetFontBorderWidth(objScore, 2);
	Obj_SetRenderPriority(objScore, 0.01);
	ObjRender_SetPosition(objScore, GetScreenWidth-95,51,0);
	ObjRender_SetScaleXYZ(objScore,1.125,1,0);

	let pathDigit = GetCurrentScriptDirectory() ~ "img/Default_SystemDigit.png";
	let count = 9;

	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, pathDigit);
	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
	Obj_SetRenderPriority(obj, 0.1);
	ObjRender_SetY(obj, 32);

	while(true)
	{
		let score = GetScore();
		score = min(score, 99999999);
		let listNum = DigitToArray(score, count);

		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. count)
		{
			let num = truncate(listNum[iObj]);
			ObjRender_SetX(obj, GetScreenWidth - 115 + iObj * 110/9);
			ObjSpriteList2D_SetSourceRect(obj, num * 36, 0, (num + 1) * 36, 32);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 14, 21);
			ObjSpriteList2D_AddVertex(obj);
		}
		yield;
	}
}

//----------------------------------------------------
//Graze
//----------------------------------------------------
task TGraze()
{
	//Graze
	let objGraze = ObjText_Create();
	ObjText_SetText(objGraze, "Graze");
	ObjText_SetFontType(objGraze,"Century Gothic");
	ObjText_SetFontSize(objGraze, 22);
	ObjText_SetFontBold(objGraze, true);
	ObjText_SetFontColorTop(objGraze, 255, 255, 255);
	ObjText_SetFontColorBottom(objGraze, 255, 255, 255);
	ObjText_SetFontBorderType(objGraze, BORDER_FULL);
	ObjText_SetFontBorderColor(objGraze,128, 128, 128);
	ObjText_SetFontBorderWidth(objGraze, 2);
	Obj_SetRenderPriority(objGraze, 0.01);
	ObjRender_SetPosition(objGraze, GetScreenWidth-95,99,0);
	ObjRender_SetScaleXYZ(objGraze,1.125,1,0);

	let pathDigit = GetCurrentScriptDirectory() ~ "img/Default_SystemDigit.png";
	let count = 5;

	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, pathDigit);
	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
	Obj_SetRenderPriority(obj, 0.1);
	ObjRender_SetY(obj, 80);

	while(true)
		{
		let graze = GetGraze();
		graze = min(graze, 99999);
		let listNum = DigitToArray(graze, count);

		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. count)
			{
			let num = truncate(listNum[iObj]);
			ObjRender_SetX(obj, GetScreenWidth-95+ iObj * 14);
			ObjSpriteList2D_SetSourceRect(obj, num * 36, 0, (num + 1) * 36, 32);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 14, 21);
			ObjSpriteList2D_AddVertex(obj);
			}
		yield;
		}
}

//----------------------------------------------------
//Boss Health
//----------------------------------------------------
task TBossLife
{
	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, GetCurrentScriptDirectory() ~ "img/Default_System.png");
	Obj_SetRenderPriority(obj, 0.7);

	let spell = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(spell, GetCurrentScriptDirectory() ~ "img/RSS_Item.png");
	Obj_SetRenderPriority(spell, 0.7);

	let lastRemStep = -1;
	let lifeRateRender = 0;

	let objScene = ID_INVALID;

	let alpha = 0;
	loop
		{
		objScene = GetEnemyBossSceneObjectID();
		ObjSpriteList2D_ClearVertexCount(obj);
		ObjSpriteList2D_ClearVertexCount(spell);
		if(objScene != ID_INVALID)
			{
			RenderLife();
			}
		if(GetPlayerY < GetStgFrameHeight/4)
			{
			if(alpha > 20)	
				{
				alpha -=255/10;
				}
			else
				{
				alpha = 20;
				}
			}
		else
			{
			if(alpha < 255)	
				{
				alpha +=255/10;
				}
			else
				{
				alpha = 255;
				}
			}
		ObjRender_SetAlpha(obj,alpha);
		ObjRender_SetAlpha(spell,alpha);
		yield;
		}


	function RenderLife()
		{
		let countRemStep = ObjEnemyBossScene_GetInfo(objScene, INFO_REMAIN_STEP_COUNT);
		if(lastRemStep != countRemStep)
			{
			lifeRateRender = 0;
			}
		let lifeTotalMax = ObjEnemyBossScene_GetInfo(objScene, INFO_ACTIVE_STEP_TOTAL_MAX_LIFE);
		let lifeTotal = ObjEnemyBossScene_GetInfo(objScene, INFO_ACTIVE_STEP_TOTAL_LIFE);
		let lifeMax = ObjEnemyBossScene_GetInfo(objScene, INFO_CURRENT_LIFE_MAX);	
		let life = ObjEnemyBossScene_GetInfo(objScene, INFO_CURRENT_LIFE);
		let lifeRate = (min(life / lifeMax, lifeRateRender)+(lifeTotal-life)/lifeMax)*(lifeMax/lifeTotalMax);	
		ObjSpriteList2D_SetDestRect(obj, 16, 9.5, 16 + 318 * lifeRate, 13.5);
		ObjSpriteList2D_SetSourceRect(obj, 1, 1, 127, 11);
		ObjSpriteList2D_AddVertex(obj);
		ObjSpriteList2D_SetSourceRect(obj, 132, 1, 137, 11);
		let listLifeDiv = [0] ~ ObjEnemyBossScene_GetInfo(objScene, INFO_ACTIVE_STEP_LIFE_RATE_LIST);

		ascent(iDiv in 0 .. length(listLifeDiv))
			{
			let rate = listLifeDiv[iDiv];
			let x = 16 + 318 * (1-rate);
			ObjSpriteList2D_SetDestRect(obj, x-1, 5.5, x + 1, 15.5);
			ObjSpriteList2D_AddVertex(obj);
			}
		ObjSpriteList2D_SetSourceRect(obj, 1, 1, 127, 11);
		lifeRateRender += 0.01;
		lifeRateRender = min(lifeRateRender, 1);
		lastRemStep = countRemStep;

		ObjSpriteList2D_SetSourceRect(spell, 96, 0, 128, 32);
		ascent(iStep in 0 .. countRemStep)
			{
			let remStepRate = 13;
			ObjSpriteList2D_AddVertex(spell);
			ObjSpriteList2D_SetDestRect(spell, 26 + (iStep-1) * remStepRate + 2, 29,26 + (iStep) * remStepRate, 29+remStepRate);
			ObjSpriteList2D_AddVertex(spell);
			ObjRender_SetColor(spell,128,255,128);
			}
		}
}

//----------------------------------------------------
//Spellcard Timer
//----------------------------------------------------
task TBossTimer
{
	let pathDigit = GetCurrentScriptDirectory() ~ "img/Default_SystemDigit.png";

	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, pathDigit);
	Obj_SetRenderPriority(obj, 0.75);
	ObjRender_SetY(obj, 0);
	let alpha = 255;

	let objScene = ID_INVALID;
	let switch = false;

	loop
		{
		objScene = GetEnemyBossSceneObjectID();
		ObjSpriteList2D_ClearVertexCount(obj);
		if(objScene != ID_INVALID)
			{
			RenderTimer;
			}
		if(GetPlayerY < GetStgFrameHeight/4)
			{
			if(alpha > 20)	
				{
				alpha -=255/10;
				}
			else
				{
				alpha = 20;
				}
			}
		else
			{
			if(alpha < 255)	
				{
				alpha +=255/10;
				}
			else
				{
				alpha = 255;
				}
			}
		ObjRender_SetAlpha(obj,alpha);
		yield;
		}

	function RenderTimer
		{
		let timer = ObjEnemyBossScene_GetInfo(objScene, INFO_TIMERF)*100/60;
		timer = min(timer, 9999);
		let listNum = DigitToArray(timer, 4);

		if(timer > 1000 || timer <= 0)
			{
			ObjRender_SetColorHSV(obj,0,0,255);
			}
		else if(timer <= 500)
			{
			ObjRender_SetColorHSV(obj,0,255,255);
			if((timer*60/100)%60 == 0)
				{
				PlaySound("se_Time2",120);
				}
			}
		else if(timer <= 1000)
			{
			ObjRender_SetColorHSV(obj,0,105,255);
			if((timer*60/100)%60 == 0)
				{
				PlaySound("se_Time1",100);
				}
			}

		ObjSpriteList2D_ClearVertexCount(obj);	
		ascent(iObj in 0 .. 2)
			{
			let num = truncate(listNum[iObj]);
			ObjRender_SetX(obj, 337 + iObj * 14);
			ObjSpriteList2D_SetSourceRect(obj, num * 36, 0, (num + 1) * 36, 32);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 24);
			ObjSpriteList2D_AddVertex(obj);	
			}
		ascent(iObj in 0 .. 2)
			{
			let num = truncate(listNum[iObj+2]);	
			ObjRender_SetX(obj, 365 + iObj * 10);
			ObjSpriteList2D_SetSourceRect(obj, num * 36, 0, (num + 1) * 36, 32);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 12, 16);
			ObjSpriteList2D_AddVertex(obj);	
			}
		}
	
}



//----------------------------------------------------
//Spellcard Bonus!
//----------------------------------------------------
task TGainSpell(score)
{
	let objText = ObjText_Create();
	ObjText_SetText(objText, "Spell Card Capture!");
	ObjText_SetFontSize(objText, 35);
	ObjText_SetFontType(objText,"Century Gothic");
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 255, 255, 255);
	ObjText_SetFontColorBottom(objText, 128, 128, 255);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderColor(objText,255, 255, 255);
	ObjText_SetFontBorderWidth(objText, 1);
	Obj_SetRenderPriority(objText, 0.6);
	ObjRender_SetX(objText, 58);
	ObjRender_SetY(objText, 98);

	loop(105){yield;}
	let alpha = 255;
	loop(45)
		{
		alpha-=255/45;
		ObjRender_SetAlpha(objText,alpha);
		yield;
		}
	Obj_Delete(objText);
}

//----------------------------------------------------
//Spellcard Fail...
//----------------------------------------------------
task TFailSpell
{
	let objText = ObjText_Create();
	ObjText_SetText(objText, "Spell Card Fail...");
	ObjText_SetFontType(objText,"Century Gothic");
	ObjText_SetFontSize(objText, 35);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 255, 255, 255);
	ObjText_SetFontColorBottom(objText, 128, 128, 255);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderColor(objText,255, 255, 255);
	ObjText_SetFontBorderWidth(objText, 1);
	Obj_SetRenderPriority(objText, 0.6);
	ObjRender_SetX(objText, 64);
	ObjRender_SetY(objText, 98);

	loop(105){yield;}
	let alpha = 255;
	loop(45)
		{
		alpha-=255/45;
		ObjRender_SetAlpha(objText,alpha);
		yield;
		}
	Obj_Delete(objText);
}


//----------------------------------------------------
//Difficulty
//----------------------------------------------------
task TDifficulty()
{	
	let obj = ObjText_Create();
	ObjText_SetFontType(obj,"Century Gothic");
	ObjText_SetFontSize(obj, 24);
	ObjText_SetFontBold(obj, true);
	ObjText_SetFontColorTop(obj, 255, 255, 255);
	ObjText_SetFontColorBottom(obj,235, 235, 235);		
	ObjText_SetFontBorderType(obj, BORDER_FULL);
	ObjText_SetFontBorderWidth(obj, 2);
	Obj_SetRenderPriority(obj, 0.01);
	ObjRender_SetY(obj, 16);
	loop
		{
		let Dif = GetCommonData("DIFFICULTY",0);
		let text = "Easy";
		ObjRender_SetX(obj, 32-(Dif%2)*10);
		let color =  [125,255,255];
		if(Dif == 1)
			{
			text = "Normal";
			color = [55,255,55];
			}
		if(Dif == 2)
			{
			text = "Hard";
			color = [255,55,55];
			}
		if(Dif == 3)
			{
			text = "Unreal";
			color = [255,55,175];
			}
		ObjText_SetText(obj, text);
		ObjText_SetFontBorderColor(obj,color[0]*0.55,color[1]*0.55,color[2]*0.55);
		yield;
		}
}

//----------------------------------------------------
//FPS
//----------------------------------------------------
task TCurrentFps()
{
	let objText = ObjText_Create();
	ObjText_SetFontType(objText,"Century Gothic");
	ObjText_SetFontSize(objText, 17);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 160, 160, 255);
	ObjText_SetFontColorBottom(objText, 64, 64, 255);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderColor(objText,255, 255, 255);
	ObjText_SetFontBorderWidth(objText, 2);
	ObjText_SetHorizontalAlignment(objText, ALIGNMENT_LEFT);
	ObjText_SetMaxWidth(objText, GetScreenWidth - 8);
	Obj_SetRenderPriority(objText, 1.0);
	ObjRender_SetX(objText, 6);
	ObjRender_SetY(objText, GetScreenHeight() - 25);

	loop
	{
		let fps = GetCurrentFps();
		let text = vtos("1.2f", fps) ~ "fps";
		ObjText_SetText(objText, text);
		yield;
	}
}

task TReplayFps()
{
	if(!IsReplay()){return;}

	let objText = ObjText_Create();
	ObjText_SetFontType(objText,"Century Gothic");
	ObjText_SetFontSize(objText, 15);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 128, 128, 255);
	ObjText_SetFontColorBottom(objText, 64, 64, 255);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderColor(objText,255, 255, 255);
	ObjText_SetFontBorderWidth(objText, 1);
	Obj_SetRenderPriority(objText, 1.0);

	ObjRender_SetX(objText, 6);
	ObjRender_SetY(objText, GetScreenHeight() - 50);

	loop
	{
		let fps = GetReplayFps();
		let text = vtos("02d", fps);
		ObjText_SetText(objText, text);
		yield;
	}
}

//----------------------------------------------------
//truncate
//----------------------------------------------------
function DigitToArray(let digit,let count)
{
	let res = [];
	digit = truncate(digit);

	loop
	{
		let tnum = digit%10;
		digit /= 10;
		res = [tnum] ~ res;
		if(truncate(digit) == 0){break;}
	}

	loop(max(0, count - length(res)))
	{
		res = [0] ~ res;
	}

	return res;
}

