
let dirCurrent = GetCurrentScriptDirectory();
let quality = 2;
let hit = 0;

@Initialize
{
	InitFrame();
	TScore();
	TGraze();
	TPlayerHit();
	TPlayerSpell();
	TBossLife();
	TBossTimer();
	TCurrentFps();
	TReplayFps();
	SetPlayerLife(2);
	SetPauseScriptPath(dirCurrent ~ "Default_Pause.txt");
	SetEndSceneScriptPath(dirCurrent ~"Default_EndScene.txt");
}

@MainLoop
{
	if(GetPlayerLife() < 2){ hit++; SetPlayerLife(2); }
	yield;
}

@Event
{
	alternative(GetEventType())
	case(EV_START_BOSS_SPELL)
	{
		let path = dirCurrent ~ "Default_System_MagicCircle.txt";
		let id = LoadScript(path);
		StartScript(id);
	}
	case(EV_GAIN_SPELL)
	{
		let objScene = GetEnemyBossSceneObjectID();
		let score = ObjEnemyBossScene_GetInfo(objScene, INFO_SPELL_SCORE);
		TGainSpell(score);
	}
	case(EV_USER_SYSTEM)
	{
		TFail;
	}
}

//----------------------------------------------------
//枠外の背景表示
//----------------------------------------------------
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);
}

//----------------------------------------------------
//スコア表示
//----------------------------------------------------
task TScore()
{
	//Score文字列
	let objScore = ObjText_Create();
	ObjText_SetText(objScore, "Score");
	ObjText_SetFontSize(objScore, 20);
	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_SetX(objScore, 428);
	ObjRender_SetY(objScore, 48);

	//得点
	let pathDigit = GetCurrentScriptDirectory() ~ "img/Default_SystemDigit.png";
	let count = 12;

	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, 72);

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

		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. count)
		{
			let num = listNum[iObj];
			ObjRender_SetX(obj, 440 + iObj * 14);
			ObjSpriteList2D_SetSourceRect(obj, num * 36, 0, (num + 1) * 36, 32);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 24);
			ObjSpriteList2D_AddVertex(obj);
		}
		yield;
	}
}

//----------------------------------------------------
//Graze表示
//----------------------------------------------------
task TGraze()
{
	//Graze文字列
	let objGraze = ObjText_Create();
	ObjText_SetText(objGraze, "Graze");
	ObjText_SetFontSize(objGraze, 20);
	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_SetX(objGraze, 428);
	ObjRender_SetY(objGraze, 98);

	//得点
	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, 122);

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

		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. count)
		{
			let num = listNum[iObj];
			ObjRender_SetX(obj, 440 + iObj * 14);
			ObjSpriteList2D_SetSourceRect(obj, num * 36, 0, (num + 1) * 36, 32);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 24);
			ObjSpriteList2D_AddVertex(obj);
		}
		yield;
	}
}

//----------------------------------------------------
//残機表示
//----------------------------------------------------
task TPlayerHit
{
	//life文字列
	let objText = ObjText_Create();
	ObjText_SetText(objText, "Hit");
	ObjText_SetFontSize(objText, 20);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 255, 255, 255);
	ObjText_SetFontColorBottom(objText, 255, 255, 255);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderColor(objText,128, 128, 128);
	ObjText_SetFontBorderWidth(objText, 2);
	Obj_SetRenderPriority(objText, 0.01);
	ObjRender_SetX(objText, 428);
	ObjRender_SetY(objText, 150);

	//残機
	let pathDigit = GetCurrentScriptDirectory() ~ "img/Default_SystemDigit.png";
	let count = 2;

	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, 174);

	while(true)
	{
		let point = hit;
		point = min(point, 99);
		point = max(point, 0);
		let listNum = DigitToArray(point, count);

		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. count)
		{
			let num = listNum[iObj];
			ObjRender_SetX(obj, 440 + iObj * 14);
			ObjSpriteList2D_SetSourceRect(obj, num * 36, 0, (num + 1) * 36, 32);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 24);
			ObjSpriteList2D_AddVertex(obj);
		}
		yield;
	}
}

//----------------------------------------------------
//残スペル表示
//----------------------------------------------------
task TPlayerSpell
{
	//Spell文字列
	let objText = ObjText_Create();
	ObjText_SetText(objText, "Spell");
	ObjText_SetFontSize(objText, 20);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 255, 255, 255);
	ObjText_SetFontColorBottom(objText, 255, 255, 255);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderColor(objText,128, 128, 128);
	ObjText_SetFontBorderWidth(objText, 2);
	Obj_SetRenderPriority(objText, 0.01);
	ObjRender_SetX(objText, 428);
	ObjRender_SetY(objText, 202);

	//スペル
	let pathDigit = GetCurrentScriptDirectory() ~ "img/Default_SystemDigit.png";
	let count = 2;

	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, 226);

	while(true)
	{
		let point = GetPlayerSpell();
		point = min(point, 99);
		let listNum = DigitToArray(point, count);

		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. count)
		{
			let num = listNum[iObj];
			ObjRender_SetX(obj, 440 + iObj * 14);
			ObjSpriteList2D_SetSourceRect(obj, num * 36, 0, (num + 1) * 36, 32);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 24);
			ObjSpriteList2D_AddVertex(obj);
		}
		yield;
	}
}

//----------------------------------------------------
//ボスのライフ表示
//----------------------------------------------------
task TBossLife
{
	let path = GetCurrentScriptDirectory() ~ "./img/Default_LifeBar.png";
	let countVertex = 64;
	let objScene = ID_INVALID;

	let objStar = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(objStar,path);
	Obj_SetRenderPriority(objStar, 0.7);
	ObjSpriteList2D_SetSourceRect(objStar,19,12,31,24);

	let objLife = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetPrimitiveType(objLife, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetVertexCount(objLife, countVertex);
	ObjRender_SetBlendType(objLife, BLEND_ALPHA);
	Obj_SetRenderPriority(objLife, 0.68);
	ObjPrim_SetTexture(objLife, path);

	let objOutline = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetPrimitiveType(objOutline, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetVertexCount(objOutline, countVertex);
	ObjRender_SetBlendType(objOutline, BLEND_ALPHA);
	Obj_SetRenderPriority(objOutline, 0.68);
	ObjPrim_SetTexture(objOutline, path);
	ObjRender_SetColor(objOutline, 155, 0, 255);

	let objKnub = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(objKnub, path);
	Obj_SetRenderPriority(objKnub, 0.68);
	ObjSpriteList2D_SetSourceRect(objKnub, 20, 0, 29, 11);
	ObjSpriteList2D_SetDestRect(objKnub, -2.5, -3, 2.5, 3);

	let objMarker = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(objMarker, 1.0);
	ObjPrim_SetTexture(objMarker, GetCurrentScriptDirectory() ~ "img/enemymarker.png");
	ObjSprite2D_SetSourceRect(objMarker, 0, 0, 42, 16);
	ObjSprite2D_SetDestCenter(objMarker);

	ascent (iVert in 0..countVertex/2)
	{
		let vertex = iVert * 2;
		ObjPrim_SetVertexUVT(objLife, vertex+0, 0, 0);
		ObjPrim_SetVertexUVT(objLife, vertex+1, 10, 32);	
		ObjPrim_SetVertexUVT(objOutline, vertex+0, 10, 0);
		ObjPrim_SetVertexUVT(objOutline, vertex+1, 19, 32);		
	}

	let mx = 0;
	let my = 0;
	let fade = 255;
	let fadec = 255;
	let maxRadius = 70;

	let lastRemStep = 0;
	let countRemStep = ObjEnemyBossScene_GetInfo(objScene, INFO_REMAIN_STEP_COUNT);
	let lifeRateRender = 0;
	let outlineRender = 0;
	let starRender = 0;

	let lifeTotalMax = ObjEnemyBossScene_GetInfo(objScene, INFO_ACTIVE_STEP_TOTAL_MAX_LIFE);
	let lifeTotal = ObjEnemyBossScene_GetInfo(objScene, INFO_ACTIVE_STEP_TOTAL_LIFE);
	let lifeRate = min(lifeTotal / lifeTotalMax, lifeRateRender);


	ObjRender_SetScaleXYZ(objLife, -1, 1, 1);
	ObjRender_SetScaleXYZ(objOutline, -1, 1, 1);

	ObjRender_SetPosition(objStar, -4, 16, 0);


	loop
	{
		ObjSpriteList2D_ClearVertexCount(objStar);
		ObjSpriteList2D_ClearVertexCount(objKnub);

		if(GetCommonData("dia", 0) == 1)
		{
			ObjRender_SetAlpha(objStar, 0);
			ObjRender_SetAlpha(objMarker, 0);
		}
		else if(starRender<1){ ObjRender_SetAlpha(objStar, min(255, 255*(starRender*3)-510)); }
		else
		{
			if(GetPlayerY < max(96, 26+(truncate(countRemStep/12)*12))){ fade = max(fade-10, 85); }
			else{ fade = min(fade+10, 255); }
			ObjRender_SetAlpha(objStar, fade);
			ObjRender_SetAlpha(objMarker, 255);
		}

		objScene = GetEnemyBossSceneObjectID;
		if(objScene != ID_INVALID)
		{
			let Boss = GetEnemyBossObjectID;
			Boss = Boss ~ [objLife];
			mx = ObjRender_GetX(Boss[0]);
			my = ObjRender_GetY(Boss[0]);
		}
		else
		{
			mx = -400;
			my = -400;
			outlineRender = 0;
			lifeRateRender = 0;
			starRender = 0;
		}
		RefreshVertex(objLife);

		if(ObjEnemyBossScene_GetInfo(objScene, INFO_IS_DURABLE_SPELL) == true){ fadec = min(fadec+10, 1); }
		else if(GetDistance(GetPlayerX, GetPlayerY, mx, my) < maxRadius * 1.75){ fadec = max(fadec-10, 85); }
		else { fadec = max(fadec-10, 255); }
		ObjRender_SetAlpha(objKnub, (fadec/100) * (lifeRateRender*100));
		ObjRender_SetAlpha(objOutline, max(fadec * 0.75, 0));
		ObjRender_SetAlpha(objLife, fadec);
	
		if(GetCommonData("ForceLifebar", 0) != 0)
		{
			outlineRender = 0;
			lifeRateRender = 0;
		}

		lastRemStep = countRemStep;

		let lifeCurrentMax = ObjEnemyBossScene_GetInfo(objScene, INFO_CURRENT_LIFE_MAX);
		let lifeCurrent = ObjEnemyBossScene_GetInfo(objScene, INFO_CURRENT_LIFE);
		let lifeCurrentRate = lifeCurrent / lifeCurrentMax;

		//ObjText_SetText(objBonus, lifeCurrentRate);

		if(lifeCurrentRate >=0.5){ ObjRender_SetColor(objLife, 100, 255, 100); }
		else if(lifeCurrentRate >= 0.2){ ObjRender_SetColor(objLife, 255, 192, 100); }
		else if(lifeCurrentRate < 0.2){ ObjRender_SetColor(objLife, 255, 100, 100); }
		yield;
	}
	Obj_Delete(objStar);
	Obj_Delete(objKnub);
	Obj_Delete(objOutline);
	Obj_Delete(objLife);

	task RefreshVertex(objid)
	{
		countRemStep = ObjEnemyBossScene_GetInfo(objScene, INFO_REMAIN_STEP_COUNT);
		if(lastRemStep != countRemStep)
		{
			lifeRateRender = 0;
		}
		ascent(iStep in 0 .. countRemStep)
		{
			ObjSpriteList2D_SetSourceRect(objStar, 19, 12, 31, 24);
			let y = truncate((iStep) / 12) * 12;
			let i = iStep - (12 * (y / 12));
			ObjSpriteList2D_SetDestRect(objStar, 4 + i * 12, 4 + y, 4 + (i + 1) * 12, 16+y);
			ObjSpriteList2D_AddVertex(objStar);
		}
		
		let listLifeDiv = [0]~ObjEnemyBossScene_GetInfo(objScene, INFO_ACTIVE_STEP_LIFE_RATE_LIST);
		ascent(iDiv in 0 .. length(listLifeDiv))
		{
			let kang = 360*(listLifeDiv[iDiv]);
			let kx = mx+(maxRadius*1.05)*cos(kang-90);
			let ky = my+(maxRadius*1.05)*sin(kang-90);
			if(1-listLifeDiv[iDiv]<lifeTotal / lifeTotalMax-0.000001)
			{
				ObjRender_SetPosition(objKnub, kx, ky, 0);
				ObjRender_SetAngleZ(objKnub, kang);
				ObjSpriteList2D_AddVertex(objKnub);
			}
		}
	
		lifeTotalMax = ObjEnemyBossScene_GetInfo(objScene, INFO_ACTIVE_STEP_TOTAL_MAX_LIFE);
		lifeTotal = ObjEnemyBossScene_GetInfo(objScene, INFO_ACTIVE_STEP_TOTAL_LIFE);
		lifeRate = min(lifeTotal / lifeTotalMax, lifeRateRender);
		
		ascent(iVert in 0..countVertex/2)
		{
			let indexVert = iVert * 2;
			let angle = (360 / (countVertex/2 - 1) * iVert);
			angle = min(360 * (outlineRender), angle);
			let x = (maxRadius) * cos(angle - 90);
			let y = (maxRadius) * sin(angle - 90);
			ObjPrim_SetVertexPosition(objOutline, indexVert + 0 , x, y, 0);
			x = (maxRadius * 1.1) * cos(angle - 90);
			y = (maxRadius * 1.1) * sin(angle - 90);
			ObjPrim_SetVertexPosition(objOutline, indexVert + 1 , x, y, 0);
			
			angle = min(360 * (lifeRate), angle);
			x = (maxRadius) * cos(angle - 90);
			y = (maxRadius) * sin(angle  -90);
			ObjPrim_SetVertexPosition(objLife, indexVert + 0 , x, y, 0);			
			x = (maxRadius * 1.1) * cos(angle - 90);
			y = (maxRadius * 1.1)* sin(angle - 90);
			ObjPrim_SetVertexPosition(objLife, indexVert + 1 , x, y, 0);
		}
		ObjRender_SetPosition(objLife, mx, my, 0);
		ObjRender_SetPosition(objOutline, mx, my, 0);
		ObjRender_SetPosition(objMarker, mx + 33, 473, 0);
		
		lifeRateRender = min(1, lifeRateRender + 0.02);
		outlineRender = min(1, outlineRender + 0.016);
		starRender = min(1, starRender + 0.016);
		
	}
}
//----------------------------------------------------
//タイマー表示
//----------------------------------------------------
task TBossTimer
{
	let pathDigit = GetCurrentScriptDirectory() ~ "img/Number.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, pathDigit);
	Obj_SetRenderPriority(obj, 0.75);
	ObjRender_SetY(obj, 0);
	let count = 2;

	/*let objBonus = ObjText_Create;

	ObjText_SetFontType(objBonus,"Cabin");
	ObjText_SetFontSize(objBonus, 10);
	ObjText_SetFontColorTop(objBonus, 255, 255, 255);
	ObjText_SetFontColorBottom(objBonus, 255, 255, 255);
	ObjText_SetFontBorderType(objBonus, BORDER_FULL);
	ObjText_SetFontBorderWidth(objBonus, 1);
	ObjText_SetFontBorderColor(objBonus, 0, 0, 0);
	ObjText_SetMaxWidth(objBonus, 500);
	ObjText_SetHorizontalAlignment(objBonus, ALIGNMENT_LEFT);
	Obj_SetRenderPriorityI(objBonus, 80);
	ObjRender_SetX(objBonus, 300);
	ObjRender_SetY(objBonus, 53);*/

	let objScene = ID_INVALID;
	AlphaChange(obj);
	loop
	{
		objScene = GetEnemyBossSceneObjectID();
		ObjSpriteList2D_ClearVertexCount(obj);
		if(objScene != ID_INVALID)
		{
			RenderTimer();
		}
		yield;
	}

	function RenderTimer()
	{
		let timer = ObjEnemyBossScene_GetInfo(objScene, INFO_TIMER);
		if(timer < 10){ timeout(timer) }
		timer = min(timer, 99);
		let listNum = DigitToArray(timer, count);
		let c = 0;

		ObjSpriteList2D_ClearVertexCount(obj);
		if(timer < 11)
		{
			timeout(timer);
			c = 1;
		}
		else
		{
			c = 0;
		}
		ascent(iObj in 0 .. count)
		{
			let num = listNum[iObj];
			ObjRender_SetX(obj, 179 + iObj * 14);
			ObjSpriteList2D_SetSourceRect(obj, num * 16, 20 * c, (num + 1) * 16, 20 + 20 * c);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 24);
			ObjSpriteList2D_AddVertex(obj);
		}
		loop(60){ yield; }
	}
	task timeout(time)
	{
		if(time < 10 && time > 4)
		{
			LoadSound(GetCurrentScriptDirectory ~ "se/time1.wav");
			PlaySE(GetCurrentScriptDirectory ~ "se/time1.wav");
		}

		else if(time < 5 && time > -1)
		{
			LoadSound(GetCurrentScriptDirectory ~ "se/time2.wav");
			PlaySE(GetCurrentScriptDirectory ~ "se/time2.wav");
		}
		else{ yield; }
	}
	task AlphaChange(obj)
	{
		let alpha = 255;
		loop
		{
			if(GetCommonData("dia", 0) == 1){ alpha = 0; }
			else if(GetPlayerY < 50 && objScene != ID_INVALID)
			{
				alpha -= 16;
				alpha = max(alpha, 95);
			}
			else
			{
				alpha += 16;
				alpha = min(alpha, 255);
			}
			ObjRender_SetAlpha(obj, alpha);
			yield;
		}
	}
}

//----------------------------------------------------
//スペルカード取得表示
//----------------------------------------------------
task TGainSpell(score)
{
	let objText = ObjText_Create();
	ObjText_SetText(objText, "Spell Card Bonus!!");
	ObjText_SetFontSize(objText, 28);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorBottom(objText, 255, 255, 255);
	ObjText_SetFontColorTop(objText, 63, 255, 128);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderColor(objText, 10, 10, 10);
	ObjText_SetFontBorderWidth(objText, 1);
	Obj_SetRenderPriority(objText, 0.6);
	ObjRender_SetX(objText, 50);
	ObjRender_SetY(objText, 98);

	let strScore = "+" ~ IntToString(score);
	let objScore = ObjText_Create();
	ObjText_SetText(objScore, strScore);
	ObjText_SetFontSize(objScore, 25);
	ObjText_SetFontBold(objScore, true);
	ObjText_SetFontColorTop(objScore, 255, 255, 255);
	ObjText_SetFontColorBottom(objScore, 255, 255, 255);
	ObjText_SetFontBorderType(objScore, BORDER_FULL);
	ObjText_SetFontBorderColor(objScore, 10, 10, 10);
	ObjText_SetFontBorderWidth(objScore, 1);
	Obj_SetRenderPriority(objScore, 0.6);
	ObjRender_SetX(objScore, 180);
	ObjRender_SetY(objScore, 140);

	loop(120)
	{
		yield;
	}
	Obj_Delete(objText);
	Obj_Delete(objScore);
}

task TFail
{
	let objText = ObjText_Create();
	ObjText_SetText(objText, "Bonus Failed...");
	ObjText_SetFontSize(objText, 28);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorBottom(objText, 255, 255, 255);
	ObjText_SetFontColorTop(objText, 150, 150, 150);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderColor(objText, 10, 10, 10);
	ObjText_SetFontBorderWidth(objText, 1);
	Obj_SetRenderPriority(objText, 0.6);
	ObjRender_SetX(objText, 70);
	ObjRender_SetY(objText, 98);

	loop(120)
	{
		yield;
	}
	Obj_Delete(objText);
}

//----------------------------------------------------
//FPS表示
//----------------------------------------------------
task TCurrentFps()
{
	let objText = ObjText_Create();
	ObjText_SetFontSize(objText, 14);
	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_RIGHT);
	ObjText_SetMaxWidth(objText, GetScreenWidth() - 8);
	Obj_SetRenderPriority(objText, 1.0);
	ObjRender_SetX(objText, 0);
	ObjRender_SetY(objText, GetScreenHeight() - 20);

	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_SetFontSize(objText, 12);
	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);

	let px = GetStgFrameLeft() + GetStgFrameWidth() - 18;
	let py = GetStgFrameTop() + GetScreenHeight() - 14;
	ObjRender_SetX(objText, px);
	ObjRender_SetY(objText, py);

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

//----------------------------------------------------
//ユーティリティ
//----------------------------------------------------
function DigitToArray(let digit,let count)
{
	let res = [];
	digit = truncate(digit);

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

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

	return res;
}

function GetDistance(x1,y1,x2,y2){
	return(((x2-x1)^2+(y2-y1)^2)^(1/2))
}