
let dirCurrent = GetCurrentScriptDirectory();
let spellcard = 0;
let hit = 0;

#include "./ZUNShot_Const.dnh"
#include"./function.txt"

@Initialize
{
	InstallFont(dirCurrent~"./ImpressBT.ttf");
	InitFrame();
	TScore();
	HScore();
	TGraze();
	TPlayerLife();
	TPlayerSpell();
	TBossLife();
	TBossTimer();
	TCurrentFps();
	TReplayFps();
	SetPauseScriptPath(dirCurrent~"./Default_Pause.txt");
}

@MainLoop
{
	yield;
}

@Event
{
	alternative(GetEventType())
	case(EV_START_BOSS_SPELL)
	{
		let path = dirCurrent ~ "Default_System_MagicCircle.txt";
		let id = LoadScript(path);
		StartScript(id);
		spellcard=1;
	}
	case(EV_PLAYER_SHOOTDOWN){
		if(spellcard==1){hit = 1;}
	}
	case(EV_PLAYER_SPELL){
		if(spellcard==1){hit = 1;}	
	}
	case(EV_END_BOSS_STEP){
		if(spellcard==1&&hit==1){
		TFailSpell;
		}
	}
	case(EV_TIMEOUT){
		if(spellcard==1){TFailSpell;}
	}
	case(EV_GAIN_SPELL)
	{
		let objScene = GetEnemyBossSceneObjectID();
		let score = ObjEnemyBossScene_GetInfo(objScene, INFO_SPELL_SCORE);
		if(hit==0){TGainSpell(score);}
	}
}

function InitFrame()
{
	let path = GetCurrentScriptDirectory() ~ "stgframe.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 objScore = ObjText_Create();
	ObjText_SetText(objScore, "Score");
	ObjText_SetFontType(objScore, "Impress BT");
	ObjText_SetFontSize(objScore, 20);
	ObjText_SetFontBold(objScore, false);
	ObjText_SetFontColorTop(objScore, 255, 255, 255);
	ObjText_SetFontColorBottom(objScore, 255,255,255);
	ObjText_SetFontBorderType(objScore, BORDER_FULL);
	ObjText_SetFontBorderColor(objScore, 0, 0, 0);
	ObjText_SetFontBorderWidth(objScore, 2);
	Obj_SetRenderPriority(objScore, 0.01);
	ObjRender_SetX(objScore, 428);
	ObjRender_SetY(objScore, 88);

	let objNumber = ObjText_Create();
	ObjText_SetFontType(objNumber, "Impress BT");
	ObjText_SetFontSize(objNumber, 20);
	ObjText_SetFontBold(objNumber, false);
	ObjText_SetFontColorTop(objNumber, 255, 255, 255);
	ObjText_SetFontColorBottom(objNumber, 255,255,255);
	ObjText_SetFontBorderType(objNumber, BORDER_FULL);
	ObjText_SetFontBorderColor(objNumber, 0, 0, 0);
	ObjText_SetFontBorderWidth(objNumber, 2);
	Obj_SetRenderPriority(objNumber, 0.01);
	ObjRender_SetX(objNumber, 428);
	ObjRender_SetY(objNumber, 108);
	ObjText_SetMaxWidth(objNumber, 200);
	ObjText_SetHorizontalAlignment(objNumber, ALIGNMENT_RIGHT);
	ObjText_SetText(objNumber, round(GetScore()));

	while(true)
	{
	let score = ToString(GetScore());
	let refinescore = "";
	loop(7){score = erase(score, (length(score)-1));}
	ascent(i in 0..length(score)){
	if((length(score)-i)%3==0&&i!=0){
	refinescore = refinescore ~ ",";
	}
	if(i==0){
	refinescore=ToString(score[0]);
              	}else{
	refinescore=refinescore~ToString(score[i]);
	}
	}
	ObjText_SetText(objNumber, refinescore);
	yield;

	}
}

task HScore()
{
	let objHScore = ObjText_Create();
	ObjText_SetText(objHScore, "High Score");
	ObjText_SetFontType(objHScore, "Impress BT");
	ObjText_SetFontSize(objHScore, 20);
	ObjText_SetFontBold(objHScore, false);
	ObjText_SetFontColorTop(objHScore, 255, 255, 255);
	ObjText_SetFontColorBottom(objHScore, 255,255,255);
	ObjText_SetFontBorderType(objHScore, BORDER_FULL);
	ObjText_SetFontBorderColor(objHScore, 0, 0, 0);
	ObjText_SetFontBorderWidth(objHScore, 2);
	Obj_SetRenderPriority(objHScore, 0.01);
	ObjRender_SetX(objHScore, 428);
	ObjRender_SetY(objHScore, 48);

	let objNumber2 = ObjText_Create();
	ObjText_SetText(objNumber2, "1,250,000");
	ObjText_SetFontType(objNumber2, "Impress BT");
	ObjText_SetFontSize(objNumber2, 20);
	ObjText_SetFontBold(objNumber2, false);
	ObjText_SetFontColorTop(objNumber2, 255, 255, 255);
	ObjText_SetFontColorBottom(objNumber2, 255,255,255);
	ObjText_SetFontBorderType(objNumber2, BORDER_FULL);
	ObjText_SetFontBorderColor(objNumber2, 0, 0, 0);
	ObjText_SetFontBorderWidth(objNumber2, 2);
	Obj_SetRenderPriority(objNumber2, 0.01);
	ObjRender_SetX(objNumber2, 428);
	ObjRender_SetY(objNumber2, 68);
	ObjText_SetMaxWidth(objNumber2, 200);
	ObjText_SetHorizontalAlignment(objNumber2, ALIGNMENT_RIGHT);

	while(true)
	{
	if(GetScore>=1250000){
	let score = ToString(GetScore());
	let refinescore = "";
	loop(7){score = erase(score, (length(score)-1));}
	ascent(i in 0..length(score)){
	if((length(score)-i)%3==0&&i!=0){
	refinescore = refinescore ~ ",";
	}
	if(i==0){
	refinescore=ToString(score[0]);
              	}else{
	refinescore=refinescore~ToString(score[i]);
	}
	}
	ObjText_SetText(objNumber2, refinescore);
	yield;
	}
	yield;
	}

}

task TGraze()
{
	let objGraze = ObjText_Create();
	ObjText_SetText(objGraze, "Graze");
	ObjText_SetFontType(objGraze, "Impress BT");
	ObjText_SetFontSize(objGraze, 20);
	ObjText_SetFontBold(objGraze, false);
	ObjText_SetFontColorTop(objGraze, 255, 255, 255);
	ObjText_SetFontColorBottom(objGraze, 255,255,255);
	ObjText_SetFontBorderType(objGraze, BORDER_FULL);
	ObjText_SetFontBorderColor(objGraze, 0, 0, 0);
	ObjText_SetFontBorderWidth(objGraze, 2);
	Obj_SetRenderPriority(objGraze, 0.01);
	ObjRender_SetX(objGraze, 428);
	ObjRender_SetY(objGraze, 218);
	
	let objNumber = ObjText_Create();
	ObjText_SetFontType(objNumber, "Impress BT");
	ObjText_SetFontSize(objNumber, 20);
	ObjText_SetFontBold(objNumber, false);
	ObjText_SetFontColorTop(objNumber, 255, 255, 255);
	ObjText_SetFontColorBottom(objNumber, 255, 255, 255);
	ObjText_SetFontBorderType(objNumber, BORDER_FULL);
	ObjText_SetFontBorderColor(objNumber, 0, 0, 0);
	ObjText_SetFontBorderWidth(objNumber, 2);
	Obj_SetRenderPriority(objNumber, 0.01);
	ObjRender_SetX(objNumber, 428);
	ObjRender_SetY(objNumber, 238);
	ObjText_SetMaxWidth(objNumber, 200);
	ObjText_SetHorizontalAlignment(objNumber, ALIGNMENT_RIGHT);
	ObjText_SetText(objNumber, round(GetScore()));

	while(true)
	{
	let score = ToString(GetGraze());
	let refinescore = "";
	loop(7){score = erase(score, (length(score)-1));}
	ascent(i in 0..length(score)){
	if((length(score)-i)%3==0&&i!=0){
	refinescore = refinescore ~ ",";
	}
	if(i==0){
	refinescore=ToString(score[0]);
              	}else{
	refinescore=refinescore~ToString(score[i]);
	}
	}
	ObjText_SetText(objNumber, refinescore);
	yield;

	}
}

task TPlayerLife
{
	let objText = ObjText_Create();
	ObjText_SetText(objText, "Life");
	ObjText_SetFontType(objText, "Impress BT");
	ObjText_SetFontSize(objText, 20);
	ObjText_SetFontBold(objText, false);
	ObjText_SetFontColorTop(objText, 255, 255, 255);
	ObjText_SetFontColorBottom(objText, 255, 255, 255);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderColor(objText, 0, 0, 0);
	ObjText_SetFontBorderWidth(objText, 2);
	Obj_SetRenderPriority(objText, 0.01);
	ObjRender_SetX(objText, 428);
	ObjRender_SetY(objText, 128);

	let objText2 = ObjText_Create();
	ObjText_SetText(objText2, "★★★★★★★★★");
	ObjText_SetFontType(objText2, "Arial Unicode MS");
	ObjText_SetFontSize(objText2, 32);
	ObjText_SetFontBold(objText2, true);
	ObjText_SetFontColorTop(objText2, 0,0,0);
	ObjText_SetFontColorBottom(objText2, 0, 0, 0);
	ObjText_SetFontBorderType(objText2, BORDER_FULL);
	ObjText_SetFontBorderColor(objText2, 0,0,0);
	ObjText_SetFontBorderWidth(objText2, 2);
	Obj_SetRenderPriority(objText2, 0.01);
	ObjRender_SetX(objText2, 430);
	ObjRender_SetY(objText2, 142);
		
	let objText3 = ObjText_Create();
	ObjText_SetText(objText3, "★★★★★★★★★");
	ObjText_SetFontType(objText3, "Arial Unicode MS");
	ObjText_SetFontSize(objText3, 32);
	ObjText_SetFontBold(objText3, true);
	ObjText_SetFontColorTop(objText3, 200, 0, 0);
	ObjText_SetFontColorBottom(objText3, 200, 0, 0);
	ObjText_SetFontBorderType(objText3, BORDER_FULL);
	ObjText_SetFontBorderColor(objText3, 0,0,0);
	ObjText_SetFontBorderWidth(objText3, 2);
	Obj_SetRenderPriority(objText3, 0.01);
	ObjRender_SetX(objText3, 430);
	ObjRender_SetY(objText3, 142);

	while(true)
	{
		let point = GetPlayerLife();
		let LifeString = "";
		if(point==0){LifeString = "";}
		if(point==1){LifeString = "★";}
		if(point==2){LifeString = "★★";}
		if(point==3){LifeString = "★★★";}
		if(point==4){LifeString = "★★★★";}
		if(point==5){LifeString = "★★★★★";}
		if(point==6){LifeString = "★★★★★★";}
		if(point==7){LifeString = "★★★★★★★";}
		if(point==8){LifeString = "★★★★★★★★";}
		if(point==9){LifeString = "★★★★★★★★★";}
		if(point>=10){LifeString="★★★★★★★★★";}
		ObjText_SetText(objText3, LifeString);
		yield;
	}
}

task TPlayerSpell
{
	let objText = ObjText_Create();
	ObjText_SetText(objText, "Bomb");
	ObjText_SetFontType(objText, "Impress BT");
	ObjText_SetFontSize(objText, 20);
	ObjText_SetFontBold(objText, false);
	ObjText_SetFontColorTop(objText, 255, 255, 255);
	ObjText_SetFontColorBottom(objText, 255,255, 255);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderColor(objText, 0, 0, 0);
	ObjText_SetFontBorderWidth(objText, 2);
	Obj_SetRenderPriority(objText, 0.01);
	ObjRender_SetX(objText, 428);
	ObjRender_SetY(objText, 174);

	let objText2 = ObjText_Create();
	ObjText_SetText(objText2, "★★★★★★★★★");
	ObjText_SetFontType(objText2, "Arial Unicode MS");
	ObjText_SetFontSize(objText2, 32);
	ObjText_SetFontBold(objText2, true);
	ObjText_SetFontColorTop(objText2, 0,0,0);
	ObjText_SetFontColorBottom(objText2, 0,0,0);
	ObjText_SetFontBorderType(objText2, BORDER_FULL);
	ObjText_SetFontBorderColor(objText2, 0,0,0);
	ObjText_SetFontBorderWidth(objText2, 2);
	Obj_SetRenderPriority(objText2, 0.01);
	ObjRender_SetX(objText2, 430);
	ObjRender_SetY(objText2, 190);

	let objText3 = ObjText_Create();
	ObjText_SetText(objText3, "••••••••••••");
	ObjText_SetFontType(objText3, "Arial Unicode MS");
	ObjText_SetFontSize(objText3, 32);
	ObjText_SetFontBold(objText3, true);
	ObjText_SetFontColorTop(objText3, 50,200,250);
	ObjText_SetFontColorBottom(objText3,50,200,250);
	ObjText_SetFontBorderType(objText3, BORDER_FULL);
	ObjText_SetFontBorderColor(objText3, 0,0,0);
	ObjText_SetFontBorderWidth(objText3, 2);
	Obj_SetRenderPriority(objText3, 0.01);
	ObjRender_SetX(objText3, 430);
	ObjRender_SetY(objText3, 190);

	while(true)
	{
		let point = GetPlayerSpell();
		let LifeString = "";
		if(point==0){LifeString = "";}
		if(point==1){LifeString = "★";}
		if(point==2){LifeString = "★★";}
		if(point==3){LifeString = "★★★";}
		if(point==4){LifeString = "★★★★";}
		if(point==5){LifeString = "★★★★★";}
		if(point==6){LifeString = "★★★★★★";}
		if(point==7){LifeString = "★★★★★★★";}
		if(point==8){LifeString = "★★★★★★★★";}
		if(point==9){LifeString = "★★★★★★★★★";}
		if(point>=10){LifeString = "★★★★★★★★★";}
		ObjText_SetText(objText3, LifeString);
		yield;
	}
}

task TBossLife
{
	let path = GetCurrentScriptDirectory() ~ "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, 4, 4, 4 + 375 * 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 = 4 + 375 * (1-rate);
			ObjSpriteList2D_SetDestRect(obj, x-1, 4, x + 1, 12);
			ObjSpriteList2D_AddVertex(obj);
		}

		ObjSpriteList2D_SetSourceRect(obj, 1, 1, 127, 11);

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

task TBossTimer
{
	let count = 2;
	let objText = ObjText_Create();
	ObjText_SetFontType(objText, "Impress BT");
	ObjText_SetFontSize(objText, 32);
	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, 0, 0);
	ObjText_SetFontBorderWidth(objText, 2);
	Obj_SetRenderPriority(objText, 1);
	

	let objScene = ID_INVALID;
	loop
	{
		objScene = GetEnemyBossSceneObjectID();
		if(objScene != ID_INVALID)
		{
			RenderTimer();
			Obj_SetRenderPriority(objText, 1);
		}else{
			Obj_SetRenderPriority(objText, 0);
		}
		yield;
	}

	function RenderTimer()
	{
		let timer = ToString(ObjEnemyBossScene_GetInfo(objScene, INFO_TIMER));
		loop(7){timer = erase(timer, (length(timer)-1));}

		ObjText_SetText(objText, timer);
		ObjRender_SetX(objText, GetStgFrameWidth-8);
		ObjRender_SetY(objText, 25);
		if(ObjEnemyBossScene_GetInfo(objScene, INFO_TIMER)<=10){
		ObjText_SetFontColorTop(objText, 200, 0, 0);
		ObjText_SetFontColorBottom(objText, 200, 0, 0);
			yield;
		}else{
		ObjText_SetFontColorTop(objText, 255, 255, 255);
		ObjText_SetFontColorBottom(objText, 255, 255, 255);
			yield;
		}
	}

}

task TGainSpell(score)
{
	spellcard=0;
	let frame = 0;
	let objText = ObjText_Create();
	ObjText_SetText(objText, "Spellcard Bonus:");
	ObjText_SetFontSize(objText, 32);
	ObjText_SetFontBold(objText, false);
	ObjText_SetFontType(objText, "Impress BT");
	ObjText_SetFontColorTop(objText, 255, 255, 255);
	ObjText_SetFontColorBottom(objText, 128, 128, 255);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderColor(objText, 0, 0, 0);
	ObjText_SetFontBorderWidth(objText, 2);
	Obj_SetRenderPriority(objText, 0.6);
	ObjText_SetMaxWidth(objText, GetStgFrameWidth-GetStgFrameLeft);
	ObjText_SetHorizontalAlignment(objText, ALIGNMENT_CENTER);
	ObjRender_SetX(objText, GetStgFrameLeft/2);
	ObjRender_SetY(objText, 98);

	let strScore = IntToString(score) ~ " points!";
	let objScore = ObjText_Create();
	ObjText_SetText(objScore, strScore);
	ObjText_SetFontSize(objScore, 32);
	ObjText_SetFontBold(objScore, false);
	ObjText_SetFontType(objScore, "Impress BT");
	ObjText_SetFontColorTop(objScore, 255, 255, 255);
	ObjText_SetFontColorBottom(objScore, 255, 128, 128);
	ObjText_SetFontBorderType(objScore, BORDER_FULL);
	ObjText_SetFontBorderColor(objScore, 0, 0, 0);
	ObjText_SetFontBorderWidth(objScore, 2);
	Obj_SetRenderPriority(objScore, 0.6);
	ObjText_SetMaxWidth(objScore, GetStgFrameWidth-GetStgFrameLeft);
	ObjText_SetHorizontalAlignment(objScore, ALIGNMENT_CENTER);
	ObjRender_SetX(objScore, GetStgFrameLeft/2);
	ObjRender_SetY(objScore, 140);

	AddScore(score);

	loop(180)
	{
		frame+=255/120;
	 	if(frame>=90){ObjRender_SetAlpha(objText, 255-(frame-90));
		ObjRender_SetAlpha(objScore, 255-(frame-90));}
		yield;
	}
	Obj_Delete(objText);
	Obj_Delete(objScore);
		
}

task TFailSpell{
	spellcard=0;
	let frame = 0;
	let objFail = ObjText_Create();
	ObjText_SetText(objFail, "Bonus Failed...");
	ObjText_SetFontSize(objFail, 32);
	ObjText_SetFontBold(objFail, false);
	ObjText_SetFontType(objFail, "Impress BT");
	ObjText_SetFontColorTop(objFail, 255, 255, 255);
	ObjText_SetFontColorBottom(objFail, 255,255,255);
	ObjText_SetFontBorderType(objFail, BORDER_FULL);
	ObjText_SetFontBorderColor(objFail, 0, 0, 0);
	ObjText_SetFontBorderWidth(objFail, 2);
	Obj_SetRenderPriority(objFail, 0.6);
	ObjText_SetMaxWidth(objFail, GetStgFrameWidth-GetStgFrameLeft);
	ObjText_SetHorizontalAlignment(objFail, ALIGNMENT_CENTER);
	ObjRender_SetX(objFail, GetStgFrameLeft/2);
	ObjRender_SetY(objFail, 98);
	
	loop(180)
	{
		frame+=255/120;
	 	if(frame>=90){ObjRender_SetAlpha(objFail, 255-(frame-90));}
		yield;
	}
	Obj_Delete(objFail);
}

task TCurrentFps()
{
	let objText = ObjText_Create();
	ObjText_SetFontSize(objText, 18);
	ObjText_SetFontBold(objText, false);
	ObjText_SetFontType(objText, "Impress BT");
	ObjText_SetFontColorTop(objText, 255, 255, 255);
	ObjText_SetFontColorBottom(objText, 255,255,255);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderColor(objText, 0, 0, 0);
	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, 18);
	ObjText_SetFontBold(objText, false);
	ObjText_SetFontType(objText, "Impress BT");
	ObjText_SetFontColorTop(objText, 255, 255, 255);
	ObjText_SetFontColorBottom(objText, 255, 255, 128);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderColor(objText, 0, 0, 0);
	ObjText_SetFontBorderWidth(objText, 2);
	Obj_SetRenderPriority(objText, 1.0);

	let px = GetStgFrameWidth+4;
	let py = GetStgFrameHeight-4;
	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;
}

