
let dirCurrent = GetCurrentScriptDirectory();

@Initialize
{
	InitFrame();
	TScore();
	TGraze();
	TPlayerLife();
	TPlayerSpell();
	TBossLife();
	TBossTimer();
	TCurrentFps();
	TReplayFps();
	Invincibility;
	AddLives;
	StartShotScript(GetCurrentScriptDirectory ~ "MS_Shot.txt");
}

@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 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()
{
	let path = GetCurrentScriptDirectory() ~ "img/status_score.png";
	let Points = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(Points, path);
	Obj_SetRenderPriority(Points, 0.01);
	ObjSprite2D_SetSourceRect(Points, 0, 0, 305, 75);
	ObjSprite2D_SetDestRect(Points, 0, 0, 305, 75);
	ObjRender_SetScaleXYZ(Points, 0.3, 0.3, 0);
	ObjRender_SetX(Points, 428);
	ObjRender_SetY(Points, 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()
{
	
	let path = GetCurrentScriptDirectory() ~ "img/status_score.png";
	let Points = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(Points, path);
	Obj_SetRenderPriority(Points, 0.01);
	ObjSprite2D_SetSourceRect(Points, 0, 75, 305, 155);
	ObjSprite2D_SetDestRect(Points, 0, 75, 305, 155);
	ObjRender_SetScaleXYZ(Points, 0.3, 0.3, 0);
	ObjRender_SetX(Points, 428);
	ObjRender_SetY(Points, 72);

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

//----------------------------------------------------
//Player Lifes
//----------------------------------------------------
task TPlayerLife
{
	let path = GetCurrentScriptDirectory() ~ "img/status_resource.png";
	let Points = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(Points, path);
	Obj_SetRenderPriority(Points, 0.01);
	ObjSprite2D_SetSourceRect(Points, 0, 0, 280, 75);
	ObjSprite2D_SetDestRect(Points, 0, 0, 280, 75);
	ObjRender_SetScaleXYZ(Points, 0.3, 0.3, 0);
	ObjRender_SetX(Points, 428);
	ObjRender_SetY(Points, 78);
	ObjRender_SetX(Points, 428);
	ObjRender_SetY(Points, 150);
	
	let LifeCounter = ObjText_Create();
	ObjText_SetFontSize(LifeCounter, 15);
	ObjText_SetFontBold(LifeCounter, true);
	ObjText_SetFontColorTop(LifeCounter, 255, 255, 255);
	ObjText_SetFontColorBottom(LifeCounter, 255, 255, 255);
	ObjText_SetFontBorderType(LifeCounter, BORDER_FULL);
	ObjText_SetFontBorderColor(LifeCounter,128, 128, 128);
	ObjText_SetFontBorderWidth(LifeCounter, 2);
	Obj_SetRenderPriority(LifeCounter, 0.01);
	ObjRender_SetX(LifeCounter, 590);
	ObjRender_SetY(LifeCounter, 178);
	//Obj_SetVisible(LifeCounter, 0);

	
	let heart = GetCurrentScriptDirectory() ~ "img/status_life.png";
	let count = 6;
	
		ascent(i in 0..count){
		
		let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
		ObjPrim_SetTexture(obj, heart);
		ObjRender_SetBlendType(obj, BLEND_ALPHA);
		Obj_SetRenderPriority(obj, 0.1);
		ObjRender_SetY(obj, 176);
		ObjRender_SetX(obj, (430-26) + (i * 26));
		ObjRender_SetScaleXYZ(obj, 0.3,0.3,0);
		ObjSpriteList2D_SetSourceRect(obj, 90 ,1, 170, 79);
		ObjSpriteList2D_SetDestRect(obj, 90, 1, 170, 79);
		ObjSpriteList2D_AddVertex(obj);
		}
	
	while(true){
	let lives = GetPlayerLife();
		ascent(i in 0..lives){
		
		let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
		ObjPrim_SetTexture(obj, heart);
		ObjRender_SetBlendType(obj, BLEND_ALPHA);
		Obj_SetRenderPriority(obj, 0.1);
		ObjRender_SetY(obj, 176);
		ObjRender_SetX(obj, 430 + (i * 26));
		ObjRender_SetScaleXYZ(obj, 0.3,0.3,0);
		ObjSpriteList2D_SetSourceRect(obj, 0 ,1, 80, 79);
		ObjSpriteList2D_SetDestRect(obj, 0, 1, 80, 79);
		ObjSpriteList2D_AddVertex(obj);
		trigger(obj, lives,i);
		}
		yield;
	while(true){
	if(lives!=GetPlayerLife()){break;}
	yield;
	}
	let counting = IntToString(-6+GetPlayerLife());
	ObjText_SetText(LifeCounter, "+" ~counting); 
	if(GetPlayerLife>6){Obj_SetVisible(LifeCounter, 1);}
	else{Obj_SetVisible(LifeCounter, 0);}
}
task trigger(obj, lives,i){
if(i>5){Obj_Delete(obj);}
	while(!Obj_IsDeleted(obj)){
	if(lives!=GetPlayerLife()){
	Obj_Delete(obj);
	break;
	}
	yield;
	}
}
}

//----------------------------------------------------
//Bombs
//----------------------------------------------------
task TPlayerSpell
{
	let path = GetCurrentScriptDirectory() ~ "img/status_resource.png";
	let Points = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(Points, path);
	Obj_SetRenderPriority(Points, 0.01);
	ObjSprite2D_SetSourceRect(Points, 0, 75, 280, 155);
	ObjSprite2D_SetDestRect(Points, 0, 75, 280, 155);
	ObjRender_SetScaleXYZ(Points, 0.3, 0.3, 0);
	ObjRender_SetX(Points, 428);
	ObjRender_SetY(Points, 182);

	let star = GetCurrentScriptDirectory() ~ "img/status_bomb.png";
	let count = 6;
	
		ascent(i in 0..count){
		
		let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
		ObjPrim_SetTexture(obj, star);
		ObjRender_SetBlendType(obj, BLEND_ALPHA);
		Obj_SetRenderPriority(obj, 0.1);
		ObjRender_SetY(obj, 230);
		ObjRender_SetX(obj, (430-26) + (i * 32));
		ObjRender_SetScaleXYZ(obj, 0.25,0.25,0);
		ObjSpriteList2D_SetSourceRect(obj, 120 ,1, 230, 109);
		ObjSpriteList2D_SetDestRect(obj, 120, 1, 230, 109);
		ObjSpriteList2D_AddVertex(obj);
		}
	
	while(true){
	let bombs = GetPlayerSpell();
		ascent(i in 0..bombs){
		
		let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
		ObjPrim_SetTexture(obj, star);
		ObjRender_SetBlendType(obj, BLEND_ALPHA);
		Obj_SetRenderPriority(obj, 0.1);
		ObjRender_SetY(obj, 230);
		ObjRender_SetX(obj, 434 + (i * 32));
		ObjRender_SetScaleXYZ(obj, 0.25,0.25,0);
		ObjSpriteList2D_SetSourceRect(obj, 0 ,1, 110, 109);
		ObjSpriteList2D_SetDestRect(obj, 0, 1, 110, 109);
		ObjSpriteList2D_AddVertex(obj);
		trigger(obj, bombs);
		}
		yield;
	while(true){
	if(bombs!=GetPlayerSpell()){break;}
	yield;
	}
}
task trigger(obj, bombs)
	{
	while(!Obj_IsDeleted(obj)){
	if(bombs!=GetPlayerSpell()){
	Obj_Delete(obj);
	break;
	}
	yield;
	}
}
}

//----------------------------------------------------
//Boss Life bar
//----------------------------------------------------
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, 52, 8, 52 + 250 * 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 = 52 + 250 * (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, 20,
				4 + (iStep + 1) * remStepRate, 16);
			ObjSpriteList2D_AddVertex(obj);
		}

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

//----------------------------------------------------
//Timer
//----------------------------------------------------
InstallFont(GetCurrentScriptDirectory() ~ "DD.ttf");
task TBossTimer
{

	let objText = ObjText_Create();
	ObjText_SetFontSize(objText, 22);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontType(objText, "DD");
	ObjText_SetFontColorTop(objText, 255, 255, 255);
	ObjText_SetFontColorBottom(objText, 255, 255, 255);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderColor(objText,25, 25, 25);
	ObjText_SetFontBorderWidth(objText, 1);
	ObjText_SetHorizontalAlignment(objText, ALIGNMENT_RIGHT);
	ObjText_SetMaxWidth(objText, GetStgFrameWidth-24);
	Obj_SetRenderPriority(objText, 1.0);
	
	ObjRender_SetX(objText, 44);
	ObjRender_SetY(objText, 16);
	
	let objScene = ID_INVALID;
	loop
	{
		objScene = GetEnemyBossSceneObjectID();
		if(objScene != ID_INVALID)
		{
			RenderTimer();
		}
		yield;
	}

	function RenderTimer()
	{
	let timer = rtos("000.00", ObjEnemyBossScene_GetInfo(objScene, INFO_TIMERF) / 60);
	ObjText_SetText(objText, timer);
	}

}

/* Old
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, 999999);
		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);
		}
	}

}
*/


//----------------------------------------------------
//Capture bonus
//----------------------------------------------------
task TGainSpell(score)
{
	let objText = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(objText, GetCurrentScriptDirectory ~ "img/frame2.png");
	Obj_SetRenderPriority(objText, 0.6);
	ObjSprite2D_SetSourceRect(objText, 15, 96, 243, 127);
	ObjSprite2D_SetDestRect(objText, 15, 96, 243, 127);
	ObjRender_SetScaleXYZ(objText, 1, 1, 0);
	ObjRender_SetX(objText, 60);
	ObjRender_SetY(objText, 20);

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

	let alpha = 255;
	loop(60)
	{
		yield;
	}
	loop(122)
	{
		ObjRender_SetAlpha(objText, alpha);
		ObjRender_SetAlpha(objScore, alpha);
		alpha-=2;
		yield;
	}
	Obj_Delete(objText);
	Obj_Delete(objScore);
}

//----------------------------------------------------
//FPSCounter
//----------------------------------------------------
task TCurrentFps()
{
	let objText = ObjText_Create();
	ObjText_SetFontSize(objText, 14);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 25, 25, 25);
	ObjText_SetFontColorBottom(objText, 64, 64, 255);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderColor(objText,255, 255, 255);
	ObjText_SetFontBorderWidth(objText, 1);
	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;
	}
}

//----------------------------------------------------
//I dunno what this is
//----------------------------------------------------
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;
}

//-----------------------------------------------------
//Things to make my life easier
//-----------------------------------------------------

task Invincibility{
	AddVirtualKey(VK_USER1,KEY_SPACE,KEY_INVALID);
	
	loop{
		if(GetVirtualKeyState(VK_USER1)==KEY_HOLD){
		SetPlayerInvincibilityFrame(30);}
	yield;}	
}

task AddLives{
	AddVirtualKey(VK_USER2,KEY_L,KEY_INVALID);
	
	loop{
		if(GetVirtualKeyState(VK_USER2)==KEY_PUSH){
		SetPlayerLife(GetPlayerLife+1);}
	yield;}	
}

