
let dirCurrent = GetCurrentScriptDirectory();

//let deaths = 0;

@Initialize
{
	InitFrame();
	TScore();
	THighScore;
	TGraze();
	TPlayerLife();
	TPlayerSpell();
	TBossLife();
	TBossTimer();
	TCurrentFps();
	TReplayFps();
	TBulletCount();
	FPSDrop;
	//HUDImage;
	SetStgFrame(32, 17, 480, 464, 20, 80); // The playing field is 448 x 437
	// SetStgFrame(32, 16, 416, 464, 20, 80);
	
	SetPauseScriptPath(dirCurrent~"./Awalys_Pause.txt");
	SetEndSceneScriptPath(dirCurrent~"./Awalys_EndScene.txt");
	SetReplaySaveSceneScriptPath(dirCurrent~"./Awalys_ReplaySaveScene.txt");

	InstallFonts;
}

@MainLoop
{
	//if(GetPlayerState == STATE_DOWN){ deaths += 1/GetPlayerDownStateFrame; }
	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);
	}
}

//----------------------------------------------------
//枠外の背景表示
//----------------------------------------------------
function InitFrame()
{
	let path = GetCurrentScriptDirectory() ~ "./img/Awalys_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_SetFontType(objScore, "Russell Square Regular");
	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,100,100,255);
	ObjText_SetFontBorderWidth(objScore, 2);
	Obj_SetRenderPriority(objScore, 0.01);
	ObjRender_SetX(objScore, 528);
	ObjRender_SetY(objScore, 48);

		let objNum = ObjText_Create();
        ObjText_SetText(objNum, "000000000000");
        ObjText_SetFontSize(objNum, 16);
        ObjText_SetFontType(objNum, "Russell Square Regular");
        ObjText_SetFontBold(objNum, true);
        ObjText_SetFontColorTop(objNum, 255, 255, 255);
        ObjText_SetFontColorBottom(objNum, 255, 255, 255);
        ObjText_SetFontBorderType(objNum, BORDER_FULL);
		ObjText_SetHorizontalAlignment(objNum, ALIGNMENT_CENTER);
		ObjText_SetMaxWidth(objNum, 160);
        ObjText_SetFontBorderColor(objNum, 0, 0, 0);
        ObjText_SetFontBorderWidth(objNum, 2);
        Obj_SetRenderPriority(objNum, 0.1);
        ObjRender_SetX(objNum, 480);
        ObjRender_SetY(objNum, 72);
        while(true){
                let score = GetScore();
                score = min(score, 999999999999);
                ObjText_SetText(objNum, rtos("000000000000", score));
                yield;
        }
}

task THighScore()
{
	if(!LoadCommonDataAreaA1("HighScore")){
		CreateCommonDataArea("HighScore");
		SetAreaCommonData("HighScore", "Score", 100000000);
	}
	else{
		GetAreaCommonData("HighScore", "Score", 0);
	}

	let objHScore = ObjText_Create();
	ObjText_SetText(objHScore, "High Score");
	ObjText_SetFontType(objHScore, "Russell Square Regular");
	ObjText_SetFontSize(objHScore, 20);
	ObjText_SetFontBold(objHScore, true);
	ObjText_SetFontColorTop(objHScore, 255, 255, 255);
	ObjText_SetFontColorBottom(objHScore, 255, 255, 255);
	ObjText_SetFontBorderType(objHScore, BORDER_FULL);
	ObjText_SetFontBorderColor(objHScore,120,0,255);
	ObjText_SetFontBorderWidth(objHScore, 2);
	Obj_SetRenderPriority(objHScore, 0.01);
	ObjRender_SetX(objHScore, 499);
	ObjRender_SetY(objHScore, 98);

		let objNum = ObjText_Create();
        ObjText_SetText(objNum, "000000000000");
        ObjText_SetFontSize(objNum, 16);
        ObjText_SetFontType(objNum, "Russell Square Regular");
        ObjText_SetFontBold(objNum, true);
        ObjText_SetFontColorTop(objNum, 255, 255, 255);
        ObjText_SetFontColorBottom(objNum, 255, 255, 255);
        ObjText_SetFontBorderType(objNum, BORDER_FULL);
		ObjText_SetHorizontalAlignment(objNum, ALIGNMENT_CENTER);
		ObjText_SetMaxWidth(objNum, 160);
        ObjText_SetFontBorderColor(objNum, 0, 0, 0);
        ObjText_SetFontBorderWidth(objNum, 2);
        Obj_SetRenderPriority(objNum, 0.1);
        ObjRender_SetX(objNum, 480);
        ObjRender_SetY(objNum, 124);

        let hiscore = -9;
        hiscore = GetAreaCommonData("HighScore", "Score", -9);
        if(hiscore == -9){
       		while(true){
       			let score = GetScore;
     			score = min(score, 9999999999999);
                ObjText_SetText(objNum, rtos("000000000000", score));
                yield;
        	}
        }
     	else{
     		while(true){
     			let score = GetScore;
     			score = min(score, 9999999999999);
     			if(score > hiscore){
	    			ObjText_SetText(objNum, rtos("000000000000", score));
	  			}
	  			else{
	    			ObjText_SetText(objNum, rtos("000000000000", hiscore));
	  			}
	  			yield;
	  		}
	  	}
}

//----------------------------------------------------
//Graze表示
//----------------------------------------------------
task TGraze()
{
	//Graze文字列
	let objGraze = ObjText_Create();
	ObjText_SetText(objGraze, "Graze");
	ObjText_SetFontType(objGraze, "Russell Square Regular");
	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,205,155,0);
	ObjText_SetFontBorderWidth(objGraze, 2);
	Obj_SetRenderPriority(objGraze, 0.01);
	ObjRender_SetX(objGraze, 528);
	ObjRender_SetY(objGraze, 150);

	let objNum = ObjText_Create();
        ObjText_SetText(objNum, "000000");
        ObjText_SetFontSize(objNum, 16);
        ObjText_SetFontType(objNum, "Russell Square Regular");
        ObjText_SetFontBold(objNum, true);
        ObjText_SetFontColorTop(objNum, 255, 255, 255);
        ObjText_SetFontColorBottom(objNum, 255, 255, 255);
        ObjText_SetFontBorderType(objNum, BORDER_FULL);
		ObjText_SetHorizontalAlignment(objNum, ALIGNMENT_CENTER);
		ObjText_SetMaxWidth(objNum, 160);
        ObjText_SetFontBorderColor(objNum, 0, 0, 0);
        ObjText_SetFontBorderWidth(objNum, 2);
        Obj_SetRenderPriority(objNum, 0.1);
        ObjRender_SetX(objNum, 481);
        ObjRender_SetY(objNum, 175);
        while(true){
                let score = GetGraze();
                score = min(score, 999999);
                ObjText_SetText(objNum, rtos("000000", score));
                yield;
        }
}

//----------------------------------------------------
//残機表示
//----------------------------------------------------
task TPlayerLife
{
	//life文字列
	let objText = ObjText_Create();
	ObjText_SetText(objText, "Lives");
	ObjText_SetFontType(objText, "Russell Square Regular");
	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,200,0,0);
	ObjText_SetFontBorderWidth(objText, 2);
	Obj_SetRenderPriority(objText, 0.01);
	ObjRender_SetX(objText, 530);
	ObjRender_SetY(objText, 202);

	let objText2 = ObjText_Create();
	let objText3 = ObjText_Create();

	let countLife = GetPlayerLife;
    let path = GetCurrentScriptDirectory() ~ "img/Default_System 2.png";
    let objLife = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
    Obj_SetRenderPriority(objLife, 0.01);

    let player = ID_INVALID;

    let lifeRateRender = 0;
    let lastRemStep = -1;

    loop{
		player = GetPlayerObjectID;
		ObjSpriteList2D_ClearVertexCount(objLife);
		countLife = GetPlayerLife;
		if(player != ID_INVALID){
			RenderLives;
		}
		yield;
	}

	 /*let objNum = ObjText_Create();
        ObjText_SetText(objNum, "00");
        ObjText_SetFontSize(objNum, 16);
        ObjText_SetFontType(objNum, "Russell Square Regular");
        ObjText_SetFontBold(objNum, true);
        ObjText_SetFontColorTop(objNum, 255, 255, 255);
        ObjText_SetFontColorBottom(objNum, 255, 255, 255);
        ObjText_SetFontBorderType(objNum, BORDER_FULL);
        ObjText_SetFontBorderColor(objNum, 0, 0, 0);
        ObjText_SetFontBorderWidth(objNum, 2);
        Obj_SetRenderPriority(objNum, 0.1);
        ObjRender_SetX(objNum, 548);
        ObjRender_SetY(objNum, 225);
        while(true){
                let score = round(deaths);
                score = min(score, 99);
				score = max(score, 0);
                ObjText_SetText(objNum, rtos("00", score));
                yield;
        }*/
    

	function RenderLives{
		ascent(iStep in 0 .. countLife){
			ObjPrim_SetTexture(objLife, path);
			ObjSpriteList2D_SetSourceRect(objLife, 130, 1, 142, 13);
			ObjSpriteList2D_SetDestCenter(objLife);
			ObjRender_SetX(objLife, 500+15*iStep);
			ObjRender_SetY(objLife, 235);
			ObjSpriteList2D_AddVertex(objLife);
			ObjRender_SetScaleXYZ(objLife, 1, 1, 0);
		}
	}
}

//----------------------------------------------------
//残スペル表示
//----------------------------------------------------
task TPlayerSpell
{
	//Spell文字列
	let objText = ObjText_Create();
	ObjText_SetText(objText, "Spell");
	ObjText_SetFontType(objText, "Russell Square Regular");
	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,0,120,70);
	ObjText_SetFontBorderWidth(objText, 2);
	Obj_SetRenderPriority(objText, 0.01);
	ObjRender_SetX(objText, 530);
	ObjRender_SetY(objText, 247);

	let countSpell = GetPlayerSpell;
    let path = GetCurrentScriptDirectory() ~ "img/Default_System 2.png";
    let objSpell = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
    Obj_SetRenderPriority(objSpell, 0.01);

    let objText2 = ObjText_Create();
	let objText3 = ObjText_Create();

    let player = ID_INVALID;

    let spellRateRender = 0;
    let spellRemStep = -1;

	loop{
		player = GetPlayerObjectID;
		ObjSpriteList2D_ClearVertexCount(objSpell);
		countSpell = GetPlayerSpell;
		if(player != ID_INVALID){
			RenderSpells;
		}
		yield;
	}

	/*let objNum = ObjText_Create();
        ObjText_SetText(objNum, "00");
        ObjText_SetFontSize(objNum, 16);
        ObjText_SetFontType(objNum, "Russell Square Regular");
        ObjText_SetFontBold(objNum, true);
        ObjText_SetFontColorTop(objNum, 255, 255, 255);
        ObjText_SetFontColorBottom(objNum, 255, 255, 255);
        ObjText_SetFontBorderType(objNum, BORDER_FULL);
        ObjText_SetFontBorderColor(objNum, 0, 0, 0);
        ObjText_SetFontBorderWidth(objNum, 2);
        Obj_SetRenderPriority(objNum, 0.1);
        ObjRender_SetX(objNum, 547);
        ObjRender_SetY(objNum, 272);
        while(true){
                let score = GetPlayerSpell();
                score = min(score, 99);
				score = max(score, 0);
                ObjText_SetText(objNum, rtos("00", score));
                yield;
        }
    */

	function RenderSpells{
		ascent(iStep in 0 .. countSpell){
			ObjPrim_SetTexture(objSpell, path);
			ObjSpriteList2D_SetSourceRect(objSpell, 130, 14, 142, 26);
			ObjSpriteList2D_SetDestCenter(objSpell);
			ObjRender_SetScaleXYZ(objSpell, 1, 1, 0);
			ObjRender_SetX(objSpell, 500+15*iStep);
			ObjRender_SetY(objSpell, 280);
			ObjSpriteList2D_AddVertex(objSpell);
		}

		/*ObjText_SetText(objText2, IntToString(GetPlayerSpell));
		ObjText_SetFontType(objText2, "Arial");
		ObjText_SetFontSize(objText2, 9);
		ObjText_SetFontBold(objText2, false);
		ObjText_SetFontColorTop(objText2, 255, 255, 255);
		ObjText_SetFontColorBottom(objText2, 255, 255, 255);
		ObjText_SetFontBorderType(objText2, BORDER_FULL);
		ObjText_SetFontBorderColor(objText2,0, 0, 0);
		ObjText_SetFontBorderWidth(objText2, 1);
		Obj_SetRenderPriority(objText2, 0.01);
		ObjRender_SetX(objText2, 537);
		ObjRender_SetY(objText2, 270);

		ObjText_SetText(objText3, "/1300");
		ObjText_SetFontType(objText3, "Arial");
		ObjText_SetFontSize(objText3, 9);
		ObjText_SetFontBold(objText3, false);
		ObjText_SetFontColorTop(objText3, 255, 255, 255);
		ObjText_SetFontColorBottom(objText3, 255, 255, 255);
		ObjText_SetFontBorderType(objText3, BORDER_FULL);
		ObjText_SetFontBorderColor(objText3,0, 0, 0);
		ObjText_SetFontBorderWidth(objText3, 1);
		Obj_SetRenderPriority(objText3, 0.01);
		ObjRender_SetX(objText3, 557);
		ObjRender_SetY(objText3, 270);

		let countRemStep = 0;
		let spellTotalMax = 1300;
		let spellTotal = GetPlayerSpell;
		let spellRate = min(spellTotal / spellTotalMax, spellRateRender);
		ObjPrim_SetTexture(objSpell, path);
		ObjSpriteList2D_SetSourceRect(objSpell, 2, 9, 0, 0);
		ObjSpriteList2D_SetDestRect(objSpell, 2, 9, 3 + 100 * spellRate, 0);
		ObjSpriteList2D_AddVertex(objSpell);
		ObjRender_SetX(objSpell, 507);
		ObjRender_SetY(objSpell, 280);

		/*if(lastRemStep != countRemStep)
		{
			//ステップが変化
			lifeRateRender = 0;
		}*/

		spellRateRender += 10;
		spellRateRender = min(spellRateRender, 1);
		//lastRemStep = countRemStep;
		yield;
	}
}

//----------------------------------------------------
//ボスのライフ表示
//----------------------------------------------------
task TBossLife
{
	let path = GetCurrentScriptDirectory() ~ "img/Default_System 2.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
	let objLife = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, path);
	Obj_SetRenderPriority(obj, 0.01);
	Obj_SetRenderPriority(objLife, 0.7);
	ObjRender_SetScaleXYZ(objLife, 1, 1, 1);

	let lastRemStep = -1;
	let lifeRateRender = 0;

	let objScene = ID_INVALID;
	
	loop
	{
		objScene = GetEnemyBossSceneObjectID();
		ObjSpriteList2D_ClearVertexCount(obj);
		ObjSpriteList2D_ClearVertexCount(objLife);
		if(objScene != ID_INVALID)
		{
			RenderLife();
			LifeMeter;
			Obj_SetVisible(obj2, true);
		}
		if(objScene == ID_INVALID){
			Obj_SetVisible(obj2, false);
		}
		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 lifeRate = min(lifeTotal / lifeTotalMax, lifeRateRender);
		ObjSpriteList2D_SetSourceRect(obj, 1, 1, 127, 11);
		ObjSpriteList2D_SetDestRect(obj, 32, 1, 32 + 450 * lifeRate, 14);
		ObjSpriteList2D_AddVertex(obj);

		//ライフの区切り表示
		ObjSpriteList2D_SetSourceRect(obj, 144, 13, 148, 0);
		let listLifeDiv = [0] ~ ObjEnemyBossScene_GetInfo(objScene, INFO_ACTIVE_STEP_LIFE_RATE_LIST);
		ascent(iDiv in 0 .. length(listLifeDiv))
		{
			let rate = listLifeDiv[iDiv];
			let x = 32 + 450 * (1-rate);
			ObjSpriteList2D_SetDestRect(obj, x-2, 0, x + 1, 16);
			ObjSpriteList2D_AddVertex(obj);
		}

		//残りステップ表示
		ascent(iStep in 0 .. countRemStep)
		{
			ObjPrim_SetTexture(objLife, path);
			ObjSpriteList2D_SetSourceRect(objLife, 130, 26, 142, 39);
			ObjSpriteList2D_SetDestCenter(objLife);
			ObjRender_SetX(objLife, 5+15*iStep);
			ObjRender_SetY(objLife, 7.5);
			ObjSpriteList2D_AddVertex(objLife);
		}

		lifeRateRender += 0.01;
		lifeRateRender = min(lifeRateRender, 1);
		lastRemStep = countRemStep;
	}
	
	function LifeMeter{
		ObjPrim_SetTexture(obj2, path);
		ObjSprite2D_SetSourceRect(obj2, 148, 12, 256, 0);
		ObjSprite2D_SetDestRect(obj2, 0, 0, 450, 16);
		ObjRender_SetPosition(obj2,32,0,0);
		ObjRender_SetAlpha(obj2, 255);
		Obj_SetRenderPriority(obj2, 0.001);
		ObjRender_SetScaleXYZ(obj2, 1,1,0);
	}
}

//----------------------------------------------------
//タイマー表示
//----------------------------------------------------
task TBossTimer
{
	let objScene;
	loop{
		objScene = GetEnemyBossSceneObjectID();
		if(objScene != ID_INVALID){
			RenderTimer;
		}
		yield;
	}
	
	function RenderTimer{
		let objNum = ObjText_Create;
		let timer = ObjEnemyBossScene_GetInfo(objScene, INFO_TIMER);
        ObjText_SetFontSize(objNum, 24);
        ObjText_SetText(objNum, rtos("00", min(timer,999)));
 
        ObjText_SetFontBold(objNum, true);
        ObjText_SetFontColorTop(objNum, 255, 255, 255);
        ObjText_SetFontColorBottom(objNum, 255, 255, 255);
        ObjText_SetFontBorderType(objNum, BORDER_FULL);
        ObjText_SetFontBorderColor(objNum, 0, 0, 0);
        ObjText_SetFontBorderWidth(objNum, 2);
        Obj_SetRenderPriority(objNum, 0.5);
		ObjText_SetFontType(objNum, "Russell Square Regular");
        ObjRender_SetX(objNum, GetStgFrameWidth/2-15);
        ObjRender_SetY(objNum, 2);
        while(GetEnemyBossSceneObjectID() != ID_INVALID) {  
			timer = ObjEnemyBossScene_GetInfo(objScene, INFO_TIMER); 
			ObjText_SetText(objNum, rtos("00", min(timer,999))); 
			yield; 
		}
		Obj_Delete(objNum);	
	}
}

//----------------------------------------------------
//スペルカード取得表示
//----------------------------------------------------
task TGainSpell(score)
{
	let objText = ObjText_Create();
	ObjText_SetText(objText, "Spell Captured!");
	ObjText_SetFontType(objText, "Russell Square Regular");
	ObjText_SetFontSize(objText, 32);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 255, 255, 255);
	ObjText_SetFontColorBottom(objText, 64, 64, 64);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderColor(objText,255, 255, 255);
	ObjText_SetFontBorderWidth(objText, 1);
	Obj_SetRenderPriority(objText, 0.6);
	ObjRender_SetX(objText, 105);
	ObjRender_SetY(objText, 98);
	
	if(!EV_GAIN_SPELL){
		ObjText_SetText(objText, "Bonus Failed...");
	}

	let strScore = "+" ~ IntToString(score);
	let objScore = ObjText_Create();
	ObjText_SetText(objScore, strScore);
	ObjText_SetFontType(objScore, "Russell Square Regular");
	ObjText_SetFontSize(objScore, 32);
	ObjText_SetFontBold(objScore, true);
	ObjText_SetFontColorTop(objScore, 160, 0, 80);
	ObjText_SetFontColorBottom(objScore, 160, 0, 0);
	ObjText_SetFontBorderType(objScore, BORDER_FULL);
	ObjText_SetFontBorderColor(objScore,255, 255, 255);
	ObjText_SetFontBorderWidth(objScore, 1);
	Obj_SetRenderPriority(objScore, 0.6);
	ObjRender_SetX(objScore, 120);
	ObjRender_SetY(objScore, 140);

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

//----------------------------------------------------
//FPS表示
//----------------------------------------------------
task TCurrentFps()
{
	let objText = ObjText_Create();
	ObjText_SetFontType(objText, "Russell Square Regular");
	ObjText_SetFontSize(objText, 14);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 160, 160, 160);
	ObjText_SetFontColorBottom(objText, 64, 64, 64);
	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 TBulletCount()
{
	let objText = ObjText_Create();
	ObjText_SetFontType(objText, "Russell Square Regular");
	ObjText_SetFontSize(objText, 12);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 160, 160, 160);
	ObjText_SetFontColorBottom(objText, 64, 64, 64);
	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() - 40);

	loop
	{
		let count = GetShotCount(TARGET_ENEMY);
		let text = IntToString(count);
		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, 64);
	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;
	}
}

task FPSDrop{
	let objText = ObjText_Create();
	ObjText_SetFontType(objText, "Russell Square Regular");
	ObjText_SetFontSize(objText, 12);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 160, 160, 160);
	ObjText_SetFontColorBottom(objText, 64, 64, 64);
	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() - 60);
	
	loop{
		if(GetCurrentFps >= 55.00){
			ObjText_SetText(objText, "");
		}
		else if(GetCurrentFps < 55.00){
			ObjText_SetText(objText, "FPS Drop!");
		}
		
		if(GetCurrentFps >= 100.00){
			ObjText_SetText(objText, "HAXX!!");
		}
		
		yield;
	}
}

task HUDImage{
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, GetCurrentScriptDirectory ~ "./img/hud.png");
	ObjSprite2D_SetSourceRect(obj, 0,0,448,62);
	ObjSprite2D_SetDestCenter(obj);
	Obj_SetRenderPriorityI(obj, 49);
	ObjRender_SetScaleXYZ(obj, 1, 1, 0);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	ObjRender_SetPosition(obj, GetStgFrameWidth/2+30, 30, 0);
	ObjRender_SetAlpha(obj, 200);
}

//----------------------------------------------------
//ユーティリティ
//----------------------------------------------------
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;
}

task InstallFonts{
	if(!InstallFont(GetCurrentScriptDirectory() ~ "../fonts/AndaleMono.ttf")){
		RaiseError("Font install failed. (Andale Mono)");
	}
	if(!InstallFont(GetCurrentScriptDirectory() ~ "../fonts/Russell Square Regular.ttf")){
		RaiseError("Font install failed. (Russell Square Regular)");
	}
	if(!InstallFont(GetCurrentScriptDirectory() ~ "../fonts/Tahoma.ttf")){
		RaiseError("Font install failed. (Tahoma)");
	}
}