let current = GetCurrentScriptDirectory;
let pathDigit = current ~ "img/13_SystemDigit.png";
let pathHealth = current ~ "img/lifebar.png";
let pathScreen = current ~ "img/frame2.png";
let dirCurrent = GetCurrentScriptDirectory();
let objDiff = ID_INVALID;
let quality = 2;
let posinc = 0;
let time1 = current ~ "sfx/se_timeout.wav";
let time2 = current ~ "sfx/se_timeout2.wav";
		let fcount=0;

@Initialize
{

	InitFrame();
	TScore();
	TGraze();
	TPlayerLife();
	TPlayerSpell();
	BossLife();
	BossTimer();
	BossShadow();
	TFps;
}

@MainLoop
{
	fcount++;
	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);
		GotSpellCard(score);
	}
}

task se_timeout {
//Clock
	let path = GetCurrentScriptDirectory~"sfx/se_timeout.wav";
	let sound = ObjSound_Create();
	ObjSound_Load(sound,path);
	ObjSound_SetVolumeRate(sound,90);
	ObjSound_Play(sound);
}

task se_timeout2 {
//NO
	let path = GetCurrentScriptDirectory~"sfx/se_timeout2.wav";
	let sound = ObjSound_Create();
	ObjSound_Load(sound,path);
	ObjSound_SetVolumeRate(sound,90);
	ObjSound_Play(sound);
}

//----------------------------------------------------
//枠外の背景表示
//----------------------------------------------------
function InitFrame()
{
	let path = GetCurrentScriptDirectory() ~ "img/frame.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 BossShadow{
	let path = GetCurrentScriptDirectory ~ "img/frame2.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, path);
	Obj_SetRenderPriority(obj, 0.01);
	ObjSprite2D_SetSourceRect(obj, 216, 496, 263, 511);
	ObjSprite2D_SetDestRect(obj, 0, -16, 48, 0);
	ObjRender_SetAlpha(obj, 0);
	ObjRender_SetY(obj, SCREEN_HEIGHT);
	let fade = 0;
	loop{
		if(GetEnemyBossSceneObjectID != ID_INVALID){
			let dx = GetCommonData("StarX1", -400);
			if(GetPlayerX>dx-32&&GetPlayerX<dx+32){
				fade = max(fade-6, 156);
			}else{
				fade = min(fade+7, 255);
			}
			ObjRender_SetX(obj, dx);
		}else{fade = 0;}
		ObjRender_SetAlpha(obj, fade);
		yield;
	}
}

//----------------------------------------------------
//スコア表示
//----------------------------------------------------
task TScore{
	/*let objScore = ObjText_Create;
	ObjText_SetText(objScore, "Score");
	ObjText_SetFontSize(objScore, 20);
	ObjText_SetFontBold(objScore, true);
	ObjText_SetFontColorTop(objScore, 255, 255, 255);
	ObjText_SetFontColorBottom(objScore, 255, 255, 255);
	ObjText_SetFontBorderType(objScore, BORDER_FULL);
	ObjText_SetFontBorderColor(objScore,128, 128, 128);
	ObjText_SetFontBorderWidth(objScore, 1);
	Obj_SetRenderPriority(objScore, 0.01);
	ObjRender_SetX(objScore, 428);
	ObjRender_SetY(objScore, 48);
	*/
	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, 68);

	while(true){
		let score = GetScore;
		score = min(score, 999999999999);
		let listNum = DigitToArray(score, count);
		count = GetNumSize(score);
		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. count){
			let num = listNum[iObj];
			ObjRender_SetX(obj, 462 + iObj * 14);
			ObjSpriteList2D_SetSourceRect(obj, num * 16, 0, (num + 1) * 16, 20);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 20);
			ObjSpriteList2D_AddVertex(obj);
		}
		yield;
	}
}

//----------------------------------------------------
//Graze表示
//----------------------------------------------------
task TGraze{
	/*let objGraze = ObjText_Create;
	ObjText_SetText(objGraze, "Graze");
	ObjText_SetFontSize(objGraze, 20);
	ObjText_SetFontBold(objGraze, true);
	ObjText_SetFontColorTop(objGraze, 255, 255, 255);
	ObjText_SetFontColorBottom(objGraze, 255, 255, 255);
	ObjText_SetFontBorderType(objGraze, BORDER_FULL);
	ObjText_SetFontBorderColor(objGraze,128, 128, 128);
	ObjText_SetFontBorderWidth(objGraze, 1);
	Obj_SetRenderPriority(objGraze, 0.01);
	ObjRender_SetX(objGraze, 428);
	ObjRender_SetY(objGraze, 98);
	*/
	let count = 5;

	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, 260);

	while(true){
		let graze = GetGraze;
		count = GetNumSize(graze);
		graze = min(graze, 99999);
		let listNum = DigitToArray(graze, count);
		
		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. count){
			let num = listNum[iObj];
			ObjRender_SetX(obj, 502 + iObj * 14);
			ObjSpriteList2D_SetSourceRect(obj, num * 16, 0, (num + 1) * 16, 20);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 20);
			ObjSpriteList2D_AddVertex(obj);
		}
		yield;
	}
}

//----------------------------------------------------
//ボスのライフ表示
//----------------------------------------------------
task TPlayerLife{
/*
	let objText = ObjText_Create;
	ObjText_SetText(objText, "Player");
	ObjText_SetFontSize(objText, 20);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 255, 255, 255);
	ObjText_SetFontColorBottom(objText, 255, 255, 255);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderColor(objText,128, 128, 128);
	ObjText_SetFontBorderWidth(objText, 1);
	Obj_SetRenderPriority(objText, 0.01);
	ObjRender_SetX(objText, 428);
	ObjRender_SetY(objText, 148);
*/



/*
ascent(i in 0..8){
	let life= ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(life,pathScreen);
	ObjRender_SetBlendType(life,BLEND_ALPHA);
	ObjSprite2D_SetSourceRect(life,168,1,184,17);
	ObjSprite2D_SetDestCenter(life);
	Obj_SetRenderPriority(life, 0.1);

	ObjRender_SetPosition(life,512+(i*13),108,0);
	yield;
	
}
*/
	let lives = GetPlayerLife;


	let count = 2;

	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, 98);

	while(true){
		let point = GetPlayerLife;
		point = min(max(0, point), 99);
		let listNum = DigitToArray(point, count);

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

//----------------------------------------------------
//残機表示
//----------------------------------------------------
task TPlayerSpell{
	/*let objText = ObjText_Create;
	ObjText_SetText(objText, "Spell");
	ObjText_SetFontSize(objText, 20);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 255, 255, 255);
	ObjText_SetFontColorBottom(objText, 255, 255, 255);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderColor(objText,128, 128, 128);
	ObjText_SetFontBorderWidth(objText, 1);
	Obj_SetRenderPriority(objText, 0.01);
	ObjRender_SetX(objText, 428);
	ObjRender_SetY(objText, 202);
	*/
	let count = 2;
	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, 140);
	
/*
	
	ascent(i in 0..8){
	let spell= ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(spell,pathScreen);
	ObjRender_SetBlendType(spell,BLEND_ALPHA);
	ObjSprite2D_SetSourceRect(spell,168,20,184,36);
	ObjSprite2D_SetDestCenter(spell);
	Obj_SetRenderPriority(spell, 0.1);

	ObjRender_SetPosition(spell,512+(i*13),148,0);
	
}
*/

	while(true)
	{
		let point = GetPlayerSpell();
		point = min(point, 99);
		let listNum = DigitToArray(point, count);

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



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

	let lastRemStep = -1;
	let lifeRateRender = 0;
	let fade = 0;
	let objScene = ID_INVALID;
	
	loop{
		objScene = GetEnemyBossSceneObjectID;
		ObjSpriteList2D_ClearVertexCount(obj);
		if(GetPlayerY < 96){
			fade = max(fade-10, 85);
		}else{fade = min(fade+10, 255);}
		ObjRender_SetAlpha(obj, fade);
		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 BossTimer{

	let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(obj, pathDigit);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	Obj_SetRenderPriority(obj, 0.81);
	
	let ypos = 8;
	let yielder = 0;
	let grow = 0;
	let count = 4;
	let maxt = 99.99;
	let scale = 1;
	let fade = 0;
	let bosstime = truncate(maxt);
	let cyoff = 0;
	if(quality<=1){cyoff = 12}
	
	let objScene = ID_INVALID;
	let x = (GetStgFrameWidth/2)-( (6 + (count-1) * (14*scale))/2 ) + 32;
	loop{
		x = (GetStgFrameWidth/2)-( (6+(count-1) * (14*scale))/2) + 32;
		if(GetPlayerY < 96){
			fade = max(fade-10, 85);
		}else{fade = min(fade+10, 255);}
		
		
		objScene = GetEnemyBossSceneObjectID;
		ObjSpriteList2D_ClearVertexCount(obj);
		if(objScene != ID_INVALID){
			RenderTimer;
			SpellTime(objScene);
			TimerSound(objScene);
			if(ObjEnemyBossScene_GetInfo(objScene, INFO_IS_SPELL) == true ||
				ObjEnemyBossScene_GetInfo(objScene, INFO_IS_LAST_SPELL) == true ||
				ObjEnemyBossScene_GetInfo(objScene, INFO_IS_DURABLE_SPELL) == true){
					if(yielder>25){
						ypos = min(max(ypos, 8+cyoff)+posinc, 32+cyoff);	
						posinc = max(posinc+0.05, 0);
					}
					yielder++;
			}else{
					ypos = max(min(ypos, 32+cyoff)+posinc, 8+cyoff);		
					posinc = min(posinc-0.05, 0);
					yielder = 0;
			}
		}else{fade = 0;ypos = 8;}
		
		ObjRender_SetAlpha(obj, fade);
		ObjRender_SetY(obj, ypos+24);
		
		yield;
	}

	function RenderTimer{
		let timer = ObjEnemyBossScene_GetInfo(objScene, INFO_TIMERF)/60;
		timer = min(max(0, timer), maxt);
		timer *= 100;
		
		let listNum = DigitToArray(timer, count);
		
		ascent(iObj in 0 .. count-2){
			let num = listNum[iObj];
			ObjRender_SetX(obj, x + (iObj-grow/20) * (14+grow));
			ObjSpriteList2D_SetSourceRect(obj, num * 16, 0, (num + 1) * 16, 20);
			ObjSpriteList2D_SetDestRect(obj, -grow, 0, 16, 20+grow);
			ObjSpriteList2D_AddVertex(obj);
		}
		
		ObjRender_SetX(obj, x + (count-2) * 14 - 2);
		ObjSpriteList2D_SetSourceRect(obj, 11 * 16, 0, (11 + 1) * 16, 20);
		ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 20);
		ObjSpriteList2D_AddVertex(obj);
		
		ascent(iObj in count-2 .. count){
			let num = listNum[iObj];
			ObjRender_SetX(obj, x + 15 + iObj * 10);
			ObjSpriteList2D_SetSourceRect(obj, num * 16, 0, (num + 1) * 16, 20);
			ObjSpriteList2D_SetDestRect(obj, 0, 5, 12, 20);
			ObjSpriteList2D_AddVertex(obj);
		}
		
		
		yield;
		
	}
	task SpellTime(SpellTimeSceneObjectID){
		let GetTimer = ObjEnemyBossScene_GetInfo(SpellTimeSceneObjectID, INFO_TIMER);
		if(bosstime != GetTimer && GetTimer <11){
			if(GetTimer > 4){grow = 6;}
			if(GetTimer < 5 && GetTimer>-1){grow = 8;}
			bosstime = GetTimer;
		}
		if(GetTimer<=10){
			ObjRender_SetColor(obj, 255, 100-grow*8, 100-grow*8);
			
		}else{ObjRender_SetColor(obj, 255, 255, 255);}
		grow = max(grow-1, 0);
	}
	
	task TimerSound(SpellTimeSceneObjectID){
		let GetTimer = ObjEnemyBossScene_GetInfo(SpellTimeSceneObjectID, INFO_TIMER);

		if(GetTimer<=10 && fcount>=60 && GetTimer>3){					
					se_timeout;
					fcount=0;
				}
				
				if(GetTimer<=3 && fcount>=60 && GetTimer>=0){					
					se_timeout2;
					fcount=0;
				}
		

	
	}
}

//----------------------------------------------------
//スペルカード取得表示
//----------------------------------------------------
task GotSpellCard(score){
	let path = GetCurrentScriptDirectory ~ "img/frame2.png";
	let objText = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(objText, path);
	ObjSprite2D_SetSourceRect(objText, 0, 99, 264, 131);
	ObjSprite2D_SetDestRect(objText, 0, 0, 264, 32);
	Obj_SetRenderPriority(objText, 0.8);
	ObjRender_SetX(objText, 64);
	ObjRender_SetY(objText, 98);

	let objScore = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	let count = GetNumSize(score);
	let listNum = DigitToArray(score, count);
	ObjPrim_SetTexture(objScore, pathDigit);
	Obj_SetRenderPriority(objScore, 0.8);
	ObjRender_SetY(objScore, 140);
	
	RefreshVertex;
	loop(120){
		yield;
	}
	let alpha = 255;
	while(alpha>0){
		alpha-=15;
		RefreshVertex;
		ObjRender_SetAlpha(objText, alpha);
		ObjRender_SetAlpha(objScore, alpha);
		yield;
	}
	
	ObjSpriteList2D_ClearVertexCount(objScore);
	Obj_Delete(objText);
	Obj_Delete(objScore);

	task RefreshVertex{
		ObjSpriteList2D_ClearVertexCount(objScore);
		ascent(iObj in 0 .. count){
			let num = listNum[iObj];
			ObjRender_SetX(objScore, 182 + iObj * 20);
			ObjSpriteList2D_SetSourceRect(objScore, num * 16, 0, (num + 1) * 16, 20);
			ObjSpriteList2D_SetDestRect(objScore, 0, 0, 20, 25);
			ObjSpriteList2D_AddVertex(objScore);
		}
	}
}


task TFps{
	let objText = ObjText_Create;
	ObjText_SetFontSize(objText, 14);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 255, 255, 255);
	ObjText_SetFontColorBottom(objText, 160, 160, 255);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderColor(objText, 32, 32, 32);
	ObjText_SetFontBorderWidth(objText, 2);
	ObjText_SetHorizontalAlignment(objText, ALIGNMENT_RIGHT);
	ObjText_SetMaxWidth(objText, GetScreenWidth() - 8);
	Obj_SetRenderPriority(objText, 1);
	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;
	}
}

//----------------------------------------------------
//ユーティリティ
//----------------------------------------------------
function DigitToArray(digit, 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 GetNumSize(value){
	let numsize = 1;
	value = truncate(value);
	if(value>=10&&value<100){numsize = 2;}
	if(value>=100&&value<1000){numsize = 3;}
	if(value>=1000&&value<10000){numsize = 4;}
	if(value>=10000&&value<100000){numsize = 5;}
	if(value>=100000&&value<1000000){numsize = 6;}
	if(value>=1000000&&value<10000000){numsize = 7;}
	if(value>=10000000&&value<100000000){numsize = 8;}
	if(value>=100000000&&value<1000000000){numsize = 9;}
	if(value>=1000000000&&value<10000000000){numsize = 10;}
	if(value>=10000000000&&value<100000000000){numsize = 11;}
	if(value>=1000000000000){numsize = 12;}
	return numsize;
}

task AddScoreEx(val){
	let time = val/45;
	while(val>1){
		val -= time;
		AddScore(time);
		yield;
	}
}

task BossLife{
	let path = pathHealth;
	let countVertex = 128;
	let objScene = ID_INVALID;
	
	let objStar = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(objStar, GetCurrentScriptDirectory ~ "img/frame2.png");
	Obj_SetRenderPriority(objStar, 0.7);
	ObjSpriteList2D_SetSourceRect(objStar, 144, 80, 156, 92);

	let objOutline = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetPrimitiveType(objOutline, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetVertexCount(objOutline, countVertex);
	ObjRender_SetBlendType(objOutline, BLEND_ALPHA);
	Obj_SetRenderPriority(objOutline, 0.69);
	ObjPrim_SetTexture(objOutline, path);
	
	let objLife = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetPrimitiveType(objLife, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetVertexCount(objLife, countVertex);
	ObjRender_SetBlendType(objLife, BLEND_ALPHA);
	Obj_SetRenderPriority(objLife, 0.68);
	ObjPrim_SetTexture(objLife, path);
	
	let objLife2 = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetPrimitiveType(objLife2, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetVertexCount(objLife2, countVertex);
	ObjRender_SetBlendType(objLife2, BLEND_ALPHA);
	Obj_SetRenderPriority(objLife2, 0.68);
	ObjPrim_SetTexture(objLife2, path);
	
	ascent (iVert in 0..countVertex/2){
		let vertex = iVert * 2;
		ObjPrim_SetVertexUVT(objOutline, vertex+0, 0, 0);
		ObjPrim_SetVertexUVT(objOutline, vertex+1, 4, 16);	
		ObjPrim_SetVertexUVT(objLife, vertex+0, 6, 0);
		ObjPrim_SetVertexUVT(objLife, vertex+1, 10, 16);	
		ObjPrim_SetVertexUVT(objLife2, vertex+0, 6, 0);
		ObjPrim_SetVertexUVT(objLife2, vertex+1, 10, 16);		
	}
	
	
	let mx = 0;
	let my = 0;
	let fade = 255;
	let maxRadius = 56;
	
	let lastRemStep = -1;
	let countRemStep = ObjEnemyBossScene_GetInfo(objScene, INFO_REMAIN_STEP_COUNT);
	let lifeRateRender = 0;
	let outlineRender = 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);
	
	let newStep = 1;
	CreateStepInd;
	
	
	ObjRender_SetScaleXYZ(objOutline, -1, 1, 1);
	ObjRender_SetScaleXYZ(objLife, -1, 1, 1);
	ObjRender_SetScaleXYZ(objLife2, -1, 1, 1);
	
	ObjRender_SetPosition(objStar, 0, 12, 0);
	
	loop{
		ObjSpriteList2D_ClearVertexCount(objStar);
		
		
		if(outlineRender<1){
			ObjRender_SetAlpha(objStar, min(255, 255*(outlineRender*3)-510));
		}else{
			if(GetPlayerY < max(96, 26+(truncate(countRemStep/12)*12))){
				fade = max(fade-10, 85);
			}else{fade = min(fade+10, 255);}
			ObjRender_SetAlpha(objStar, fade);
		}
		
		objScene = GetEnemyBossSceneObjectID;
		if(objScene != ID_INVALID){
			let Boss = GetEnemyBossObjectID;
			Boss = Boss ~ [objOutline];
			mx = ObjRender_GetX(Boss[0]);
			my = ObjRender_GetY(Boss[0]);
		}else{
			mx = -400;
			my = -400;
			outlineRender = 0;
			lifeRateRender = 0;
		}
		RefreshVertex(objOutline);
		
		if(GetCommonData("ForceLifebar", 0) != 0){
			outlineRender = 0;
			lifeRateRender = 0;
		}
		
		
		lastRemStep = countRemStep;
		
		yield;
	}
	

	task RefreshVertex(objid){
		
		countRemStep = ObjEnemyBossScene_GetInfo(objScene, INFO_REMAIN_STEP_COUNT);
		if(lastRemStep != countRemStep){
			newStep = 1;
			CreateStepInd;
			lifeRateRender = 0;
		}
		ascent(iStep in 0 .. countRemStep){
			let y = truncate((iStep)/12)*12;
			let i = iStep-(12*(y/12));
			ObjSpriteList2D_SetDestRect(objStar, 4 + i * 12, 4+y,
				4 + (i + 1) * 12, 16+y);
			ObjSpriteList2D_AddVertex(objStar);
		}
	
		lifeTotalMax = ObjEnemyBossScene_GetInfo(objScene, INFO_ACTIVE_STEP_TOTAL_MAX_LIFE);
		lifeTotal = ObjEnemyBossScene_GetInfo(objScene, INFO_ACTIVE_STEP_TOTAL_LIFE);
		lifeRate = min(lifeTotal / lifeTotalMax, lifeRateRender);
		
		
		
		ascent(iVert in 0..countVertex/2){
			let indexVert = iVert * 2;
			let angle = (360 / (countVertex/2 - 1) * iVert);
			angle = min(360*(outlineRender), angle);
			let x = maxRadius*cos(angle-90);
			let y = maxRadius*sin(angle-90);
			ObjPrim_SetVertexPosition(objLife, indexVert + 0 , x, y, 0);
			x = (maxRadius*1.025)*cos(angle-90);
			y = (maxRadius*1.025)*sin(angle-90);
			ObjPrim_SetVertexPosition(objLife, indexVert + 1 , x, y, 0);
			
			x = (maxRadius*1.065)*cos(angle-90);
			y = (maxRadius*1.065)*sin(angle-90);
			ObjPrim_SetVertexPosition(objLife2, indexVert + 0 , x, y, 0);
			x = (maxRadius*1.09)*cos(angle-90);
			y = (maxRadius*1.09)*sin(angle-90);
			ObjPrim_SetVertexPosition(objLife2, indexVert + 1 , x, y, 0);
			
			angle = min(360*(lifeRate), angle);
			x = (maxRadius)*cos(angle-90);
			y = (maxRadius)*sin(angle-90);
			ObjPrim_SetVertexPosition(objOutline, indexVert + 0 , x, y, 0);			
			x = (maxRadius*1.075)*cos(angle-90);
			y = (maxRadius*1.075)*sin(angle-90);
			ObjPrim_SetVertexPosition(objOutline, indexVert + 1 , x, y, 0);
		}
		ObjRender_SetPosition(objOutline, mx, my, 0);
		ObjRender_SetPosition(objLife, mx, my, 0);
		ObjRender_SetPosition(objLife2, mx, my, 0);
		
		lifeRateRender = min(1, lifeRateRender+0.02);
		outlineRender = min(1, outlineRender+0.016);
		
	}
	task CreateStepInd{
		let listLifeDiv = ObjEnemyBossScene_GetInfo(objScene, INFO_ACTIVE_STEP_LIFE_RATE_LIST);
		ascent(iDiv in 0 .. length(listLifeDiv)){
			let rate = listLifeDiv[iDiv];
			let angle = 360*(rate);
			CreateKnub(angle, rate);
		}
		task CreateKnub(angle, rate){
			let obj = ObjPrim_Create(OBJ_SPRITE_2D);
			ObjRender_SetBlendType(obj, BLEND_ALPHA);
			Obj_SetRenderPriority(obj, 0.69);
			ObjPrim_SetTexture(obj, path);
			ObjSprite2D_SetSourceRect(obj, 10, 0, 15, 7);
			ObjSprite2D_SetDestCenter(obj);
			ObjRender_SetScaleXYZ(obj, 1.25, 1.25, 1);
			ObjRender_SetPosition(obj, mx, my, 0);
			ObjRender_SetAngleZ(obj, angle);
			yield;
			newStep = 0;
			let life = (lifeTotal / lifeTotalMax);
			while(newStep == 0 && 1-rate<life-0.000001){
				let x = mx+(maxRadius*1.05)*cos(angle-90);
				let y = my+(maxRadius*1.05)*sin(angle-90);
				life = (lifeTotal / lifeTotalMax);
				ObjRender_SetPosition(obj, x, y, 0);
				ObjRender_SetAlpha(obj, min(255, 255*(outlineRender*3)-510));
				yield;
			}
			Obj_Delete(obj);
		}
	}

}

task DetermineQuality{
	loop(4){yield;}
	while(GetCommonData("SelectionSet", 2)!=2){yield}
	quality = GetCommonData("quality", 2);
	if(quality>=2){BossLife;}
	else{TBossLife;}
	BossTimer;
}

