
let dirCurrent = GetCurrentScriptDirectory();

@Initialize
{
	BouclePauveEnd();
	
	InitFrame();
	TScore();
	TGraze();
	TPlayerLife();
	TPlayerSpell();
	TBossLife();
	TBossTimer();
	TCurrentFps();
	TReplayFps();
}

@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(score);
	}
}

function BouclePauveEnd() {
	SetPauseScriptPath(dirCurrent ~ "Default_Pause.txt");
	SetEndSceneScriptPath(dirCurrent ~ "Default_EndScene.txt");
	SetReplaySaveSceneScriptPath(dirCurrent ~ "Default_ReplaySaveScene.txt");
}

//----------------------------------------------------
//枠外の背景表示
//----------------------------------------------------
function InitFrame()
{
	let path = GetCurrentScriptDirectory() ~ "img/front00.png";
	let path1 = GetCurrentScriptDirectory() ~ "img/front01.png";
	
	let right = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(right, path);
	Obj_SetRenderPriority(right, 0);
	ObjSprite2D_SetSourceRect(right, 32, 0, 256, 480);
	ObjSprite2D_SetDestRect(right, 416, 0, 640, 480);
	
	let left = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(left, path);
	Obj_SetRenderPriority(left, 0);
	ObjSprite2D_SetSourceRect(left, 0, 0, 32, 480);
	ObjSprite2D_SetDestRect(left, 0, 0, 32, 480);
	
	let top = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(top, path);
	Obj_SetRenderPriority(top, 0);
	ObjSprite2D_SetSourceRect(top, 0, 480, 384, 496);
	ObjSprite2D_SetDestRect(top, 32, 0, 416, 16);
	
	let bottom = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(bottom, path);
	Obj_SetRenderPriority(bottom, 0);
	ObjSprite2D_SetSourceRect(bottom, 0, 496, 384, 512);
	ObjSprite2D_SetDestRect(bottom, 32, 464, 416, 480);
	
	let extra = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(extra, path);
	Obj_SetRenderPriority(extra, 0.01);
	ObjSprite2D_SetSourceRect(extra, 256, 368, 342, 384);
	ObjSprite2D_SetDestRect(extra, 486, 32, 572, 48);
	
	let score = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(score, path);
	Obj_SetRenderPriority(score, 0.01);
	ObjSprite2D_SetSourceRect(score, 256, 20, 336, 40);
	ObjSprite2D_SetDestRect(score, 436, 64, 516, 84);
	
	let player = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(player, path1);
	Obj_SetRenderPriority(player, 0.01);
	ObjSprite2D_SetSourceRect(player, 258, 35, 288, 52);
	ObjSprite2D_SetDestRect(player, 469, 106, 499, 123);
	
	let spell = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(spell, path1);
	Obj_SetRenderPriority(spell, 0.01);
	ObjSprite2D_SetSourceRect(spell, 330, 0, 384, 18);
	ObjSprite2D_SetDestRect(spell, 445, 136, 499, 154);
	
	let graze = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(graze, path1);
	Obj_SetRenderPriority(graze, 0.01);
	ObjSprite2D_SetSourceRect(graze, 349, 17, 384, 34);
	ObjSprite2D_SetDestRect(graze, 464, 165, 499, 182);
}

//----------------------------------------------------
//スコア表示
//----------------------------------------------------
task TScore()
{
	let pathDigit = GetCurrentScriptDirectory() ~ "img/front00.png";
	let count = 9;

	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, pathDigit);
	Obj_SetRenderPriority(obj, 0.1);
	ObjRender_SetY(obj, 64);

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

		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. count)
		{
			let num = listNum[iObj];
			ObjRender_SetX(obj, 516 + iObj * 12);
			ObjSpriteList2D_SetSourceRect(obj, 336 + num * 16, 0, 336 + (1 + num) * 16, 20);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 20);
			ObjSpriteList2D_AddVertex(obj);
		}
		yield;
	}
}

//----------------------------------------------------
//Graze表示
//----------------------------------------------------
task TGraze()
{
	let pathDigit = GetCurrentScriptDirectory() ~ "img/ascii.png";
	let count = 5;

	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, pathDigit);
	Obj_SetRenderPriority(obj, 0.1);
	ObjRender_SetY(obj, 167);

	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, 516 + iObj * 12);
			ObjSpriteList2D_SetSourceRect(obj, num * 16, 216, (num + 1) * 16, 231);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 15);
			ObjSpriteList2D_AddVertex(obj);
		}
		yield;
	}
}

//----------------------------------------------------
//残機表示
//----------------------------------------------------
task TPlayerLife
{
	let pathDigit = GetCurrentScriptDirectory() ~ "img/front01.png";

	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, pathDigit);
	Obj_SetRenderPriority(obj, 0.01);
	ObjRender_SetY(obj, 108);

	while(true)
	{
		let point = GetPlayerLife();
		point = min(point, 8);
		point = max(point, 0);

		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. point)
		{
			ObjRender_SetX(obj, 516 + iObj * 14);
			ObjSpriteList2D_SetSourceRect(obj, 464, 2, 480, 18);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 16);
			ObjSpriteList2D_AddVertex(obj);
		}
		yield;
	}
}

//----------------------------------------------------
//残スペル表示
//----------------------------------------------------
task TPlayerSpell
{
	let pathDigit = GetCurrentScriptDirectory() ~ "img/front01.png";

	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, pathDigit);
	Obj_SetRenderPriority(obj, 0.01);
	ObjRender_SetY(obj, 138);

	while(true)
	{
		let point = GetPlayerSpell();
		point = min(point, 8);
		point = max(point, 0);

		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. point)
		{
			ObjRender_SetX(obj, 516 + iObj * 14);
			ObjSpriteList2D_SetSourceRect(obj, 464, 2, 480, 18);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 16);
			ObjSpriteList2D_AddVertex(obj);
		}
		yield;
	}
	
}

//----------------------------------------------------
//ボスのライフ表示
//----------------------------------------------------
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;
	let alpha = 255;
	loop {
		objScene = GetEnemyBossSceneObjectID();
		ObjSpriteList2D_ClearVertexCount(obj);
		if(objScene != ID_INVALID)
		{
			//残りステップ
			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;
			
			ObjRender_SetAlpha(obj, alpha);
		}
		
		if (GetPlayerY > 100) {
			if (alpha < 255) {
				alpha += 180 / 20;
			}
		} else {
			if (alpha > 75) {
				alpha -= 180 / 20;
			}
		}
		
		yield;
	}
}

//----------------------------------------------------
//タイマー表示
//----------------------------------------------------
task TBossTimer
{
	let pathDigit = GetCurrentScriptDirectory() ~ "img/front00.png";

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

	let objScene = ID_INVALID;
	let alpha = 255;
	loop
	{
		objScene = GetEnemyBossSceneObjectID();
		ObjSpriteList2D_ClearVertexCount(obj);
		
		if(objScene != ID_INVALID) {
			let timer = ObjEnemyBossScene_GetInfo(objScene, INFO_TIMER);
			timer = min(timer, 999);
			let listNum = DigitToArray(timer, count);

			if (timer >= 100) {count = 3;}
			else if (timer >= 10) {count = 2;}
			else {count = 1;}
			
			ascent(iObj in 0 .. count)
			{
				let num = listNum[iObj];
				if (count == 3) {
					ObjRender_SetX(obj, 346 + iObj * 11);
				} else if (count == 2) {
					ObjRender_SetX(obj, 352 + iObj * 12);
				} else {
					ObjRender_SetX(obj, 358 + iObj * 11);
				}
				ObjSpriteList2D_SetSourceRect(obj, 336 + num * 16, 0, 336 + (1 + num) * 16, 20);
				ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 20);
				ObjSpriteList2D_AddVertex(obj);
			}
			
			if (GetPlayerY > 100) {
				if (alpha < 255) {
					alpha += 180 / 20;
				}
			} else {
				if (alpha > 75) {
				alpha -= 180 / 20;
				}
			}	
			
			ObjRender_SetAlpha(obj, alpha);
		}
		
		yield;
	}

	function RenderTimer()
	{

	}
	


}

//----------------------------------------------------
//スペルカード取得表示
//----------------------------------------------------
task TGainSpell(score)
{
	let path = GetCurrentScriptDirectory() ~ "img/front00.png";
	
	let bonusText = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(bonusText, path);
	Obj_SetRenderPriority(bonusText, 0.61);
	ObjSprite2D_SetSourceRect(bonusText, 256, 100, 480, 130);
	ObjSprite2D_SetDestRect(bonusText, GetStgFrameWidth / 2 - 112, 110, GetStgFrameWidth / 2 + 112, 140);
	
	let count = numDigits(score);
	let listNum = DigitToArray(score, count);
	let leftDigit = GetStgFrameWidth / 2 - 7 * count;
	let numTexts = [];
	
	ascent(iObj in 0 .. count)
	{
		let num = listNum[iObj];
		let numText = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(numText, path);
		Obj_SetRenderPriority(numText, 0.61);
		ObjRender_SetColor(numText, 125, 125, 125);

		
		ObjRender_SetY(numText, 145);
		ObjRender_SetX(numText, leftDigit + iObj * 14);
		ObjSprite2D_SetSourceRect(numText, 336 + num * 16, 0, 336 + (1 + num) * 16, 20);
		ObjSprite2D_SetDestRect(numText, 0, 0, 16, 20);
		
		numTexts = numTexts ~ [numText];
	}
	
	cycleLight;
	
	ascent(i in 0..30) {
		if (i % 2 == 0) {
			ObjRender_SetColor(bonusText, 125, 125, 125);
		} else {
			ObjRender_SetColor(bonusText, 255, 255, 255);
		}
		
		wait(5);
	}
	
	Obj_Delete(bonusText);
	ascent (i in 0..count) {
		Obj_Delete(numTexts[i]);
	}
	
	task cycleLight
	{
		let j = 0;
		let lighting = true;
		loop(150) {
			if (lighting) {
				if (j < count) {
					ObjRender_SetColor(numTexts[j], 255, 255, 255);
					j++;
				} else {
					j = 0;
					lighting = false;
					ObjRender_SetColor(numTexts[j], 125, 125, 125);
				}
			} else {
				if (j < count) {
					ObjRender_SetColor(numTexts[j], 125, 125, 125);
					j++;
				} else {
					j = 0;
					lighting = true;
					ObjRender_SetColor(numTexts[j], 255, 255, 255);
				}
			}
			yield;
		}
	}
	
}

//----------------------------------------------------
//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;
}

//wait n frames
function wait(n) 
{
	loop (n) {
		yield;
	}
}

//gives number of digits in an integer
function numDigits(n)
{
	if (n - n % 10 > 0) {
		return numDigits(n / 10) + 1;
	} else {
		return 1;
	}
}


