let dirCurrent = GetCurrentScriptDirectory();

//width = 444, height = 448

@Initialize
{
	InitFrame();
	TGraze();
	TLife();
	TPlayerSpell();
	danMathskufu;
	TBossLife();
	TBossTimer();
	TCurrentFps();
	TReplayFps();
	SetPauseScriptPath(GetCurrentScriptDirectory() ~ "./Pause.dnh");
    SetEndSceneScriptPath(GetCurrentScriptDirectory() ~ "./EndScene.dnh");
    SetReplaySaveSceneScriptPath(GetCurrentScriptDirectory() ~ "./ReplaySaveScene.dnh");
}

@MainLoop
{
	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;
	}
}


function InitFrame(){
    let bg = GetCurrentScriptDirectory() ~ "img/sysBg.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, bg);
	Obj_SetRenderPriority(obj, 0);
	ObjSprite2D_SetSourceRect(obj, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
	ObjSprite2D_SetDestRect(obj, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    SetStgFrame(42,14, 486, 462, 20, 80);
}


task TGraze(){
	let obj = ObjText_Create();
	ObjText_SetText(obj, "Graze");
	ObjText_SetFontSize(obj, 30);
	ObjText_SetFontType(obj, "Helvetica");
	ObjText_SetFontBold(obj, true);
	ObjText_SetFontColorTop(obj, 255, 255, 255);
	ObjText_SetFontColorBottom(obj, 255, 255, 255);
	ObjText_SetFontBorderType(obj, BORDER_FULL);
	ObjText_SetFontBorderColor(obj,87, 0, 0);
	ObjText_SetFontBorderWidth(obj, 2);
	Obj_SetRenderPriority(obj, 0.01);
	ObjRender_SetX(obj, 520);
	ObjRender_SetY(obj, 45);

	let num = ObjText_Create();
    ObjText_SetText(num, "0000");
    ObjText_SetFontSize(num, 26);
    ObjText_SetFontType(num, "Helvetica");
    ObjText_SetFontColorTop(num, 255, 255, 255);
    ObjText_SetFontColorBottom(num, 255, 255, 255);
    ObjText_SetFontBorderType(num, BORDER_FULL);
    ObjText_SetFontBorderColor(num, 0, 0, 0);
    ObjText_SetFontBorderWidth(num, 2);
    Obj_SetRenderPriority(num, 0.1);
    ObjRender_SetX(num, 526);
    ObjRender_SetY(num, 73);
	
	while(true){
        let score = GetGraze();
        score = min(score, 9999);
        ObjText_SetText(num, rtos("0000", score));
        yield;
    }
}


task TLife{
	let obj = ObjText_Create();
	ObjText_SetText(obj, "Life");
	ObjText_SetFontSize(obj, 30);
	ObjText_SetFontType(obj, "Helvetica");
	ObjText_SetFontBold(obj, true);
	ObjText_SetFontColorTop(obj, 255, 255, 255);
	ObjText_SetFontColorBottom(obj, 255, 255, 255);
	ObjText_SetFontBorderType(obj, BORDER_FULL);
	ObjText_SetFontBorderColor(obj,87, 0, 0);
	ObjText_SetFontBorderWidth(obj, 2);
	Obj_SetRenderPriority(obj, 0.01);
	ObjRender_SetX(obj, 520);
	ObjRender_SetY(obj, 100);

	let num = ObjText_Create();
    ObjText_SetText(num, "00");
    ObjText_SetFontSize(num, 26);
    ObjText_SetFontType(num, "Helvetica");
    ObjText_SetFontColorTop(num, 255, 255, 255);
    ObjText_SetFontColorBottom(num, 255, 255, 255);
    ObjText_SetFontBorderType(num, BORDER_FULL);
    ObjText_SetFontBorderColor(num, 0, 0, 0);
    ObjText_SetFontBorderWidth(num, 2);
    Obj_SetRenderPriority(num, 0.1);
    ObjRender_SetX(num, 526);
    ObjRender_SetY(num, 128);
	
	while(true){
        let score = GetPlayerLife();
        score = min(score, 99);
        ObjText_SetText(num, rtos("00", score));
        yield;
    }
}


task TPlayerSpell{
	let obj = ObjText_Create();
	ObjText_SetText(obj, "Bomb");
	ObjText_SetFontSize(obj, 30);
	ObjText_SetFontType(obj, "Helvetica");
	ObjText_SetFontBold(obj, true);
	ObjText_SetFontColorTop(obj, 255, 255, 255);
	ObjText_SetFontColorBottom(obj, 255, 255, 255);
	ObjText_SetFontBorderType(obj, BORDER_FULL);
	ObjText_SetFontBorderColor(obj,87, 0, 0);
	ObjText_SetFontBorderWidth(obj, 2);
	Obj_SetRenderPriority(obj, 0.01);
	ObjRender_SetX(obj, 520);
	ObjRender_SetY(obj, 155);

	let num = ObjText_Create();
    ObjText_SetText(num, "00");
    ObjText_SetFontSize(num, 26);
    ObjText_SetFontType(num, "Helvetica");
    ObjText_SetFontColorTop(num, 255, 255, 255);
    ObjText_SetFontColorBottom(num, 255, 255, 255);
    ObjText_SetFontBorderType(num, BORDER_FULL);
    ObjText_SetFontBorderColor(num, 0, 0, 0);
    ObjText_SetFontBorderWidth(num, 2);
    Obj_SetRenderPriority(num, 0.1);
    ObjRender_SetX(num, 526);
    ObjRender_SetY(num, 183);
	
	while(true){
        let score = GetPlayerSpell();
        score = min(score, 99);
        ObjText_SetText(num, rtos("00", score));
        yield;
    }
}

task danMathskufu{
    let obj = ObjText_Create();
	ObjText_SetText(obj, "DANMATHSKUFU");
	ObjText_SetFontSize(obj, 40);
	ObjText_SetFontType(obj, "Arial");
	ObjText_SetFontColorTop(obj, 255, 255, 255);
	ObjText_SetFontColorBottom(obj, 255, 255, 255);
	ObjText_SetFontBorderType(obj, BORDER_FULL);
	ObjText_SetFontBorderColor(obj,0, 70, 137);
	ObjText_SetFontBorderWidth(obj, 5);
	Obj_SetRenderPriority(obj, 0.001);
	ObjRender_SetX(obj, 392);
	ObjRender_SetY(obj, 275);
	ObjRender_SetAngleZ(obj, -70);
	ObjRender_SetAlpha(obj, 120);
}


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

	let lastRemStep = -1;
	let lifeRateRender = 0;

	let objScene = ID_INVALID;
	loop
	{
		objScene = GetEnemyBossSceneObjectID();
		ObjSpriteList2D_ClearVertexCount(obj);
		if(objScene != ID_INVALID)
		{
			RenderLife();
		}
		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, 72, 8, 72 + 270 * lifeRate, 12);
		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 = 72 + 270 * (1-rate);
			ObjSpriteList2D_SetDestRect(obj, x-1, 4, x + 1, 14);
			ObjSpriteList2D_AddVertex(obj);
		}

		//残りステップ表示
		ObjSpriteList2D_SetSourceRect(obj, 1, 1, 127, 11);
		ascent(iStep in 0 .. countRemStep)
		{
			let remStepRate = 58 / countRemStep;
			ObjSpriteList2D_SetDestRect(obj, 4 + iStep * remStepRate + 2, 8,
				4 + (iStep + 1) * remStepRate, 12);
			ObjSpriteList2D_AddVertex(obj);
		}

		lifeRateRender += 0.01;
		lifeRateRender = min(lifeRateRender, 1);
		lastRemStep = countRemStep;
	}
}

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

	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, pathDigit);
	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
	Obj_SetRenderPriority(obj, 0.75);
	ObjRender_SetY(obj, 0);
	let count = 2;

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

	function RenderTimer()
	{
		let timer = ObjEnemyBossScene_GetInfo(objScene, INFO_TIMER);
		timer = min(timer, 99);
		let listNum = DigitToArray(timer, count);

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

}


task TGainSpell()
{
	let objText = ObjText_Create();
	ObjText_SetText(objText, "Perfect!![r]Get spell bonus!");
	ObjText_SetFontType(objText, "Helvetica");
	ObjText_SetFontSize(objText, 40);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 255, 255, 255);
	ObjText_SetFontColorBottom(objText, 128, 128, 255);
	Obj_SetRenderPriority(objText, 0.6);
	ObjText_SetMaxWidth(objText, 280);
	ObjText_SetHorizontalAlignment(objText, ALIGNMENT_CENTER);
	ObjRender_SetX(objText, 82);
	ObjRender_SetY(objText, 100);

	/*let strScore = "+" ~ IntToString(score);
	let objScore = ObjText_Create();
	ObjText_SetText(objScore, strScore);
	ObjText_SetFontSize(objScore, 32);
	ObjText_SetFontBold(objScore, true);
	ObjText_SetFontColorTop(objScore, 255, 255, 255);
	ObjText_SetFontColorBottom(objScore, 255, 128, 128);
	ObjText_SetFontBorderType(objScore, BORDER_FULL);
	ObjText_SetFontBorderColor(objScore,255, 255, 255);
	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 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;
}

