let dirCurrent = GetCurrentScriptDirectory();

let stgx = GetStgFrameWidth()/2;
let stgy = GetStgFrameHeight()/2;

let objScene = ID_INVALID;
let pathHealth = dirCurrent ~ "img/lifebar.png";

InstallFont(GetCurrentScriptDirectory() ~ "../lib/Fonts/adventpro-semibold.ttf");
@Initialize{
	InitFrame();
	SetPauseScriptPath(GetCurrentScriptDirectory() ~ "./Default_Pause.txt");
    SetEndSceneScriptPath(GetCurrentScriptDirectory() ~ "./Default_EndScene.txt");

	TScore();
	THiScore();
	TGraze();
	TPlayerLife();
	TPlayerSpell();
	TBossLife();
	//TCircBossLife();
	TBossTimer();
	TCurrentFps();
	TReplayFps();
}

@MainLoop{
	objScene = GetEnemyBossSceneObjectID;
	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);
		let pass = GetCommonData("extend", false);
		TGainSpell(score, pass);
	}
}

//----------------------------------------------------
//枠外の背景表示
//----------------------------------------------------
function InitFrame(){
	let path = GetCurrentScriptDirectory() ~ "img/STG.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 y = 65;

	//Score文字列
	let text = ObjPrim_Create(OBJ_SPRITE_2D);
	let img = GetCurrentScriptDirectory() ~ "img/front01.png";
	ObjPrim_SetTexture(text,img);
	ObjSprite2D_SetSourceRect(text, 256, 18, 300, 36);
	ObjSprite2D_SetDestCenter(text);
	ObjRender_SetBlendType(text, BLEND_ALPHA);
	ObjRender_SetScaleXYZ(text, 1.25, 1.25, 0);
	Obj_SetRenderPriority(text, 1);
	ObjRender_SetPosition(text, 444, y, 0);

	//得点
	let pathDigit = GetCurrentScriptDirectory() ~ "img/front00.png";
	let count = 12;

	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, pathDigit);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	Obj_SetRenderPriority(obj, 0.1);
	ObjRender_SetY(obj, y+10);

	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, 447 + iObj * 12);
			ObjSpriteList2D_SetSourceRect(obj, 544+num * 16, 161, 544+(num + 1) * 16, 179);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 14, 14*1.4);
			ObjSpriteList2D_AddVertex(obj);
		}
		yield;
	}
}
//----------------------------------------------------
//HiScore
//----------------------------------------------------
task THiScore(){
	let y = 30;
	
	let text = ObjPrim_Create(OBJ_SPRITE_2D);
	let img = GetCurrentScriptDirectory() ~ "img/front01.png";
	ObjPrim_SetTexture(text,img);
	ObjSprite2D_SetSourceRect(text, 258, 1, 312, 16);
	ObjSprite2D_SetDestCenter(text);
	ObjRender_SetBlendType(text, BLEND_ALPHA);
	ObjRender_SetScaleXYZ(text, 1.25, 1.25, 0);
	Obj_SetRenderPriority(text, 1);
	ObjRender_SetPosition(text, 453, y, 0);

	//得点
	let pathDigit = GetCurrentScriptDirectory() ~ "img/front00.png";
	let count = 12;

	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, pathDigit);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	ObjRender_SetAlpha(obj,255);
	Obj_SetRenderPriority(obj, 0.1);
	ObjRender_SetY(obj, y+10);

	while(true){					 //
		let score = GetAreaCommonData("HiScore", "HISCORE", 0);
		score = min(score, 999999999999);
		let listNum = DigitToArray(score, count);
		
		if(GetScore>GetAreaCommonData("HiScore","HISCORE",0)){
			SetAreaCommonData("HiScore", "HISCORE", GetScore());
			SaveCommonDataAreaA1("HiScore");
		}
		
		if(!IsCommonDataAreaExists("HiScore")){
			CreateCommonDataArea("HiScore");
		}
		LoadCommonDataAreaA1("HiScore");
		
		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. count){
			let num = listNum[iObj];
			ObjRender_SetX(obj, 447 + iObj * 12);
			ObjSpriteList2D_SetSourceRect(obj, 544+num * 16, 161, 544+(num + 1) * 16, 179);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 14, 14*1.4);
			ObjSpriteList2D_AddVertex(obj);
		}
		
		yield;
	}
}
//----------------------------------------------------
//Graze表示
//----------------------------------------------------
task TGraze(){
	let y = 230;

	let text = ObjPrim_Create(OBJ_SPRITE_2D);
	let img = GetCurrentScriptDirectory() ~ "img/front01.png";
	ObjPrim_SetTexture(text, img);
	ObjSprite2D_SetSourceRect(text, 348, 18, 394, 36);
	ObjSprite2D_SetDestCenter(text);
	ObjRender_SetBlendType(text, BLEND_ALPHA);
	ObjRender_SetScaleXYZ(text, 1, 1, 0);
	Obj_SetRenderPriority(text, 1);
	ObjRender_SetPosition(text, 450, y, 0);

	//得点
	let pathDigit = GetCurrentScriptDirectory() ~ "img/front00.png";
	let count = 6;

	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, pathDigit);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	Obj_SetRenderPriority(obj, 0.1);
	ObjRender_SetY(obj, y-10);
	ObjRender_SetScaleXYZ(obj, 0.9, 0.9, 0);
	ObjRender_SetColor(obj, 256, 256, 256);
	while(true){
		let graze = GetGraze();
		graze = min(graze, 99999);
		let listNum = DigitToArray(graze, count);
		count = length(listNum);

		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. count){
			let num = listNum[iObj];
			ObjRender_SetX(obj, 562 + iObj * 12);
			ObjSpriteList2D_SetSourceRect(obj, 544+num * 16, 161, 544+(num + 1) * 16, 179);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 14, 14*1.5);
			ObjSpriteList2D_AddVertex(obj);
		}
		yield;
	}
}

//----------------------------------------------------
//残機表示
//----------------------------------------------------
task TPlayerLife{
	let y = 130;
	
	TText;
	
	ascent(i in 0..8){
		EmptyLife(stgx+315+i*17);
		RealLife(stgx+315+i*17,i);
	}
	
	task EmptyLife(px){
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		let img = GetCurrentScriptDirectory() ~ "img/front00.png";
		ObjPrim_SetTexture(obj,img);
		ObjSprite2D_SetSourceRect(obj, 832, 0, 864, 36);
		ObjSprite2D_SetDestCenter(obj);
		ObjRender_SetBlendType(obj, BLEND_ALPHA);
		ObjRender_SetScaleXYZ(obj, 0.6, 0.6, 0);
		Obj_SetRenderPriority(obj, 1);
		ObjRender_SetPosition(obj, px, y, 0);
	}
	
	task RealLife(px, num){
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		let img = GetCurrentScriptDirectory() ~ "img/front00.png";
		ObjPrim_SetTexture(obj, img);
		ObjSprite2D_SetSourceRect(obj, 991, 0, 1022, 36);
		ObjSprite2D_SetDestCenter(obj);
		ObjRender_SetBlendType(obj, BLEND_ALPHA);
		ObjRender_SetScaleXYZ(obj, 0.6, 0.6, 0);
		Obj_SetRenderPriority(obj, 1);  
		ObjRender_SetPosition(obj, px-2, y, 0);
		while(true){
			let point = GetPlayerLife();
			if(point > num){ObjRender_SetAlpha(obj, 255);}
			else{ObjRender_SetAlpha(obj, 0);}
			yield;
		}
	}
	
	task TText{
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		let img = GetCurrentScriptDirectory() ~ "img/front01.png";
		ObjPrim_SetTexture(obj, img);
		ObjSprite2D_SetSourceRect(obj, 256, 36, 304, 54);
		ObjSprite2D_SetDestCenter(obj);
		ObjRender_SetBlendType(obj, BLEND_ALPHA);
		ObjRender_SetScaleXYZ(obj, 1.15, 1.15, 0);
		Obj_SetRenderPriority(obj, 1);
		ObjRender_SetPosition(obj, 452, y, 0);
	}
}

task TPlayerSpell{
	let y = 160;

	TText;
	
	ascent(i in 0..8){
		EmptyBomb(stgx+315+i*17);
		RealBomb(stgx+315+i*17,i);
	}
	
	task EmptyBomb(px){
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		let img = GetCurrentScriptDirectory() ~ "img/front00.png";
		let c = 255;
		ObjPrim_SetTexture(obj, img);
		ObjSprite2D_SetSourceRect(obj, 832, 36, 864, 75);
		ObjSprite2D_SetDestCenter(obj);
		ObjRender_SetBlendType(obj, BLEND_ALPHA);
		ObjRender_SetScaleXYZ(obj, 0.6, 0.6, 0);
		Obj_SetRenderPriority(obj, 1);
		ObjRender_SetPosition(obj, px, y, 0);
	}
	
	task RealBomb(px,num){
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		let img = GetCurrentScriptDirectory() ~ "img/front00.png";
		let c = 255;
		ObjPrim_SetTexture(obj,img);
		ObjSprite2D_SetSourceRect(obj,991,36,1022,75);
		ObjSprite2D_SetDestCenter(obj);
		ObjRender_SetBlendType(obj,BLEND_ALPHA);
		ObjRender_SetScaleXYZ(obj,0.6,0.6,0);
		Obj_SetRenderPriority(obj,1);
		ObjRender_SetPosition(obj,px-1,y,0);
		while(true){
			let point = GetPlayerSpell();
			if(point>num){ObjRender_SetAlpha(obj,255);}
			else{ObjRender_SetAlpha(obj,0);}
			yield;
		}
	}
	
	task TText{
		let obj=ObjPrim_Create(OBJ_SPRITE_2D);
		let img=GetCurrentScriptDirectory()~"img/front01.png";
		ObjPrim_SetTexture(obj,img);
		ObjSprite2D_SetSourceRect(obj,320,0,400,18);
		ObjSprite2D_SetDestCenter(obj);
		ObjRender_SetBlendType(obj,BLEND_ALPHA);
		ObjRender_SetScaleXYZ(obj, 1.15, 1.15, 0);
		Obj_SetRenderPriority(obj,1);
		ObjRender_SetPosition(obj,458,y+3,0);
	}
}

//----------------------------------------------------
//ボスのライフ表示
//----------------------------------------------------
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(score, test){
	//276x44

	let pic = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(pic, GetCurrentScriptDirectory() ~ "./img/messages/spell_yes.png");
	ObjRender_SetAlpha(pic, 255);
	ObjSprite2D_SetSourceRect(pic, 0, 0, 276, 44);
	ObjRender_SetX(pic, 190);
	ObjRender_SetY(pic, 55);
	ObjRender_SetBlendType(pic, BLEND_ALPHA);
	ObjSprite2D_SetDestCenter(pic);

	let strScore = "+" ~ IntToString(score);
	let objScore = ObjText_Create();
	ObjText_SetText(objScore, strScore);
	ObjText_SetFontType(objScore, "Advent Pro");
	ObjText_SetFontSize(objScore, 32);
	ObjText_SetFontBold(objScore, true);
	ObjText_SetFontColorTop(objScore, 110, 184, 173);
	ObjText_SetFontColorBottom(objScore, 41, 126, 112);
	Obj_SetRenderPriority(objScore, 0.6);
	ObjRender_SetX(objScore, 135);
	ObjRender_SetY(objScore, 65); 
	
	let extend = ObjSound_Create;
		ObjSound_Load(extend, GetCurrentScriptDirectory() ~ "../lib/SFX/1up.wav");
		ObjSound_SetSoundDivision(extend, SOUND_SE);
	
	let gg = ObjPrim_Create(OBJ_SPRITE_2D);
	if(test){
		ObjPrim_SetTexture(gg, GetCurrentScriptDirectory() ~ "img/messages/extend.png");
		ObjSprite2D_SetSourceRect(gg, 0, 0, 125, 46);
		ObjSprite2D_SetDestCenter(gg);
		ObjRender_SetBlendType(gg, BLEND_ALPHA);
		ObjRender_SetScaleXYZ(gg, 1, 1, 0);
		Obj_SetRenderPriority(gg, 1);
	
		ObjRender_SetPosition(gg, 227, 40, 0);
		ObjSound_Play(extend);
	}

	loop(120){yield;}
	SetCommonData("extend", false);
	Obj_Delete(pic);
	Obj_Delete(objScore);
	Obj_Delete(gg);
	Obj_Delete(extend);
	
}

//----------------------------------------------------
//FPS表示
//----------------------------------------------------
task TCurrentFps(){
	let objText = ObjText_Create();
	ObjText_SetFontType(objText, "Advent Pro");
	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_SetFontType(objText, "Advent Pro");
	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;
}

function wait(n){loop(n){yield;}}
function GetDistance(x1,y1,x2,y2){return(((x2-x1)^2+(y2-y1)^2)^(1/2))}
function GetGapX(let x, let gapLength, let gapAngle){ return x+gapLength*cos(gapAngle); }
function GetGapY(let y, let gapLength, let gapAngle){ return y+gapLength*sin(gapAngle); }