
let dirCurrent = GetCurrentScriptDirectory();
let BossPosition=[0,0];

@Initialize
{
	if(GetAreaCommonData("TUNING","Effect",1)==1)
	{
//	TBossDistort;
	}
	BossExplode;
	InitFrame();
	THiScore();
	TScore();
//	TSpirit();
	TGraze();
	TFPS;
/*
	TPlayerLife();
	TPlayerLifeNecessary;
	TPlayerSpell();
	TPlayerSpellNecessary;
*/
	TBossLife();

	TBossTimer();
//	BombBarrier;
	GrazeBonus;
}

@MainLoop
{
	if(length(GetEnemyBossObjectID)>0)
	{
	BossPosition[0]=ObjRender_GetX(GetEnemyBossObjectID()[0]);
	BossPosition[1]=ObjRender_GetY(GetEnemyBossObjectID()[0]);
	}
	yield;
}

@Event
{
	alternative(GetEventType())
	case(EV_START_BOSS_SPELL)
	{
		let path = dirCurrent ~ "System_MagicCircle.txt";
		let id = LoadScript(path);
		StartScript(id);
	}
	case(EV_GAIN_SPELL)
	{
		if(GetCommonData("SpecialStage",false)==false)
		{
		SetCommonData("StageRank",GetCommonData("StageRank",1)+0.2);
		let objScene = GetEnemyBossSceneObjectID();
		let score = GetCommonData("SpellCardBonus",10);
		TGainSpell(score);
		}
	}
}

task TFPS
{
		function CreateTextObject(let mx, let my, let text)
		{
			let obj = ObjText_Create();
			ObjText_SetText(obj, text);
			ObjText_SetFontSize(obj, 14+4);
			ObjText_SetFontBold(obj, true);
			ObjText_SetFontColorTop(obj, 255, 255, 255);
			ObjText_SetFontColorBottom(obj, 160, 160, 255);
		//	ObjText_SetFontBorderType(obj, BORDER_FULL);
			ObjText_SetFontBorderType(obj, BORDER_NONE);
			ObjText_SetFontBorderColor(obj,255, 255, 255);
			ObjText_SetFontBorderWidth(obj, 2*0);
			Obj_SetRenderPriorityI(obj, 100);
			ObjRender_SetX(obj, mx);
			ObjRender_SetY(obj, my);

			ObjText_SetFontType(obj,"ＭＳ 明朝");

			return obj;
		}

	let objText10=CreateTextObject(584, 458,"");
	while(true)
	{
		ObjRender_SetX(objText10,584);
		let fpslength=5;
		if(GetCurrentFps>=100){fpslength=6;ObjRender_SetX(objText10,574);}
		ObjText_SetText(objText10,vtos("3f",GetCurrentFps)[0..fpslength]);
		yield;
	}
}

task GrazeBonus
{
	let oldGraze=GetGraze;
	loop
	{
		if(oldGraze<GetGraze)
		{
			AddScore((GetGraze-oldGraze)*500);
			oldGraze=GetGraze;
		}
		yield;
	}
}

task BossExplode
{
loop
{
	if(GetCommonData("BossDefeat",false))
	{
		yield;
		SetCommonData("BossDefeat",false);
		TExplosion;
	}
	yield;
}


task TExplosion
{
	if(GetCommonData("BossName","None")=="Flandre" || GetCommonData("BossName","None")=="Magic" || 1)
	{
	let MS=30.0;
	loop(60)
	{
		E_Effect(MS);
		MS-=30/60;
		yield;
	}
	}

	if(GetCommonData("BossName","None")=="Patchouli")
	{
	let MS=60.0;
	loop(120){E_Dust2;}
	loop(120)
	{
		loop(1){E_Dust2;}
		E_Effect2(MS);
		MS-=60/120;
		yield;
	}
	}
	task E_Effect(MaxScale)
	{
		let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"../img/effect/Marisa_shots.png");
		ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjPrim_SetVertexCount(obj, 4);
		ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
		Obj_SetRenderPriorityI(obj,30);
		ObjRender_SetAngleZ(obj,0);

		ObjPrim_SetVertexPosition(obj, 0, -64, -64, -0);
		ObjPrim_SetVertexPosition(obj, 1, 64, -64, -0);
		ObjPrim_SetVertexPosition(obj, 2, 64, 64, 0);
		ObjPrim_SetVertexPosition(obj, 3, -64, 64, 0);

		ObjPrim_SetVertexUVT(obj, 0, 0, 256);
		ObjPrim_SetVertexUVT(obj, 1, 128, 256);
		ObjPrim_SetVertexUVT(obj, 2, 128, 384);
		ObjPrim_SetVertexUVT(obj, 3, 0, 384);

		ObjRender_SetPosition(obj,BossPosition[0],BossPosition[1],0);

		let Alpha=0.2;
		let Scale=0;
		let R=255;
		let G=255;
		let B=255;

		let Xangle=rand(0,360);
		let Yangle=rand(0,360);
		let Zangle=rand(0,360);

		loop(30)
		{
			Alpha-=0.2/30;
			Scale+=MaxScale/30;
			ObjRender_SetScaleXYZ(obj,Scale,Scale,1);
			ObjRender_SetColor(obj,R*Alpha,G*Alpha,B*Alpha);
			ObjRender_SetAngleXYZ(obj,Xangle,Yangle,Zangle);
			yield;
		}
		Obj_Delete(obj);
	}

	task E_Effect2(MaxScale)
	{
		let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"../img/effect/Marisa_shots.png");
		ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjPrim_SetVertexCount(obj, 4);
		ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
		Obj_SetRenderPriorityI(obj,30);
		ObjRender_SetAngleZ(obj,0);

		ObjPrim_SetVertexPosition(obj, 0, -64, -64, -0);
		ObjPrim_SetVertexPosition(obj, 1, 64, -64, -0);
		ObjPrim_SetVertexPosition(obj, 2, 64, 64, 0);
		ObjPrim_SetVertexPosition(obj, 3, -64, 64, 0);

		ObjPrim_SetVertexUVT(obj, 0, 0, 256);
		ObjPrim_SetVertexUVT(obj, 1, 128, 256);
		ObjPrim_SetVertexUVT(obj, 2, 128, 384);
		ObjPrim_SetVertexUVT(obj, 3, 0, 384);

		ObjRender_SetPosition(obj,BossPosition[0],BossPosition[1],0);

		let Alpha=0.2;
		let Scale=0;
		let Color=HueDifine(rand(0,360));

		let Xangle=rand(0,360);
		let Yangle=rand(0,360);
		let Zangle=rand(0,360);

		loop(30)
		{
			Alpha-=0.2/30;
			Scale+=MaxScale/30;
			ObjRender_SetScaleXYZ(obj,Scale,Scale,1);
			ObjRender_SetColor(obj,Color[0]*Alpha,Color[1]*Alpha,Color[2]*Alpha);
			ObjRender_SetAngleXYZ(obj,Xangle,Yangle,Zangle);
			yield;
		}
		Obj_Delete(obj);
	}

	task E_Dust
	{
		let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"../img/effect/Marisa_shots.png");
		ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjPrim_SetVertexCount(obj, 4);
		ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
		Obj_SetRenderPriorityI(obj,30);
		ObjRender_SetAngleZ(obj,0);

		ObjPrim_SetVertexPosition(obj, 0, -64, -64, -0);
		ObjPrim_SetVertexPosition(obj, 1, 64, -64, -0);
		ObjPrim_SetVertexPosition(obj, 2, 64, 64, 0);
		ObjPrim_SetVertexPosition(obj, 3, -64, 64, 0);

		ObjPrim_SetVertexUVT(obj, 0, 0, 256);
		ObjPrim_SetVertexUVT(obj, 1, 128, 256);
		ObjPrim_SetVertexUVT(obj, 2, 128, 384);
		ObjPrim_SetVertexUVT(obj, 3, 0, 384);

		ObjRender_SetPosition(obj,BossPosition[0],BossPosition[1],0);

		let speed=(rand(1,15));
		let Angle=rand(0,360);
		let Alpha=0.6;
		let Scale=0.2;
		let Zangle=0;
		let ZangleRotate=rand(-10,10);
		let R=255;
		let G=255;
		let B=255;
		loop(45)
		{
			ObjRender_SetPosition(obj,ObjRender_GetX(obj)+speed*cos(Angle),ObjRender_GetY(obj)+speed*sin(Angle),0);
			ObjRender_SetScaleXYZ(obj,Scale,Scale,1);
			ObjRender_SetColor(obj,R*Alpha,G*Alpha,B*Alpha);
			ObjRender_SetAngleZ(obj,Zangle);
			Alpha-=0.6/45;
			Scale-=0.2/45;
			Zangle+=ZangleRotate;
			yield;
		}
		Obj_Delete(obj);
	}
	task E_Dust2
	{
		let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"../img/effect/Marisa_shots.png");
		ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjPrim_SetVertexCount(obj, 4);
		ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
		Obj_SetRenderPriorityI(obj,30);
		ObjRender_SetAngleZ(obj,0);

		ObjPrim_SetVertexPosition(obj, 0, -64, -64, -0);
		ObjPrim_SetVertexPosition(obj, 1, 64, -64, -0);
		ObjPrim_SetVertexPosition(obj, 2, 64, 64, 0);
		ObjPrim_SetVertexPosition(obj, 3, -64, 64, 0);

		ObjPrim_SetVertexUVT(obj, 0, 0, 256);
		ObjPrim_SetVertexUVT(obj, 1, 128, 256);
		ObjPrim_SetVertexUVT(obj, 2, 128, 384);
		ObjPrim_SetVertexUVT(obj, 3, 0, 384);

		ObjRender_SetPosition(obj,BossPosition[0],BossPosition[1],0);

		let speed=(rand(1,15));
		let Angle=rand(0,360);
		let Alpha=0.6;
		let Scale=0.2;
		let Zangle=0;
		let ZangleRotate=rand(-10,10);
		let Color=HueDifine(rand(0,360));

		loop(45)
		{
			ObjRender_SetPosition(obj,ObjRender_GetX(obj)+speed*cos(Angle),ObjRender_GetY(obj)+speed*sin(Angle),0);
			ObjRender_SetScaleXYZ(obj,Scale,Scale,1);
			ObjRender_SetColor(obj,Color[0]*Alpha,Color[1]*Alpha,Color[2]*Alpha);
			ObjRender_SetAngleZ(obj,Zangle);
			Alpha-=0.6/45;
			Scale-=0.2/45;
			Zangle+=ZangleRotate;
			yield;
		}
		Obj_Delete(obj);
	}
}

}
//----------------------------------------------------
//枠外の背景表示
//----------------------------------------------------
function InitFrame()
{
	//let path = GetCurrentScriptDirectory() ~ "img/Default_SystemBackground.png";
	let path = GetCurrentScriptDirectory() ~ "img/Title.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 THiScore()
{
	//Score文字列
	let objScore = ObjText_Create();
	ObjText_SetText(objScore, "High 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_NONE);
	ObjText_SetFontBorderColor(objScore,128, 128, 128);
	ObjText_SetFontBorderWidth(objScore, 0);
	Obj_SetRenderPriority(objScore, 0.01);
	ObjRender_SetX(objScore, 428);
	ObjRender_SetY(objScore, 24);

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

	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, 48-4);

	let score2=0;
	while(true)
	{

	if(GetAreaCommonData("TUNING","PlayerCharacter",0)==0)
	{
		score2=GetAreaCommonData("SPELL_RECORD","Reimu_Score",[])[GetCommonData("SpellNumber",9)];
	}
	else if(GetAreaCommonData("TUNING","PlayerCharacter",0)==1)
	{
		score2=GetAreaCommonData("SPELL_RECORD","Marisa_Score",[])[GetCommonData("SpellNumber",9)];
	}

		let score = GetScore();
		//score = max(score,GetAreaCommonData("SCORE","HighScore",1000000));
		score = max(score,score2);

		let listNum = DigitToArray(score, count);

		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. count)
		{
			let num = listNum[iObj];
			ObjRender_SetX(obj, 428 + iObj * 14);
			ObjSpriteList2D_SetSourceRect(obj, num * 36, 0, (num + 1) * 36, 32);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 24);
			if(num!=0 || iObj==9 || score>=10^(10-iObj))
			{
			ObjSpriteList2D_AddVertex(obj);
			}
			if(iObj<1){ObjRender_SetColor(obj,150,255,150);}
			else if(iObj<5){ObjRender_SetColor(obj,255,255,150);}
			else{ObjRender_SetColor(obj,255,255,255);}
		}
		yield;
	}
}

task TScore()
{
	//Score文字列
	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_NONE);
	ObjText_SetFontBorderColor(objScore,128, 128, 128);
	ObjText_SetFontBorderWidth(objScore, 0);
	Obj_SetRenderPriority(objScore, 0.01);
	ObjRender_SetX(objScore, 428);
	ObjRender_SetY(objScore, 80-8);

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

	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, 104-8-4);

	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, 428 + iObj * 14);
			ObjSpriteList2D_SetSourceRect(obj, num * 36, 0, (num + 1) * 36, 32);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 24);
			if(num!=0 || iObj==9 || score>=10^(10-iObj))
			{
			ObjSpriteList2D_AddVertex(obj);
			}
			if(iObj<1){ObjRender_SetColor(obj,150,255,150);}
			else if(iObj<5){ObjRender_SetColor(obj,255,255,150);}
			else{ObjRender_SetColor(obj,255,255,255);}
		}
		yield;
	}
}

task TSpirit()
{
	//Score文字列
	let objScore = ObjText_Create();
	ObjText_SetText(objScore, "Spirit");
	ObjText_SetFontSize(objScore, 20);
	ObjText_SetFontBold(objScore, true);
	ObjText_SetFontColorTop(objScore, 255, 255, 255);
	ObjText_SetFontColorBottom(objScore, 255, 255, 255);
	ObjText_SetFontBorderType(objScore, BORDER_NONE);
	ObjText_SetFontBorderColor(objScore,128, 128, 128);
	ObjText_SetFontBorderWidth(objScore, 0);
	Obj_SetRenderPriority(objScore, 0.01);
	ObjRender_SetX(objScore, 428);
	ObjRender_SetY(objScore, 120);

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

	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, 144-4);

	while(true)
	{
		let score = GetCommonData("SpiritPoint",10000);
		score = min(score, 999999999999);
		let listNum = DigitToArray(score, count);

		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. count)
		{
			let num = listNum[iObj];
			ObjRender_SetX(obj, 428 + iObj * 14);
			ObjSpriteList2D_SetSourceRect(obj, num * 36, 0, (num + 1) * 36, 32);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 24);
			if(num!=0 || iObj==9 || score>=10^(10-iObj))
			{
			ObjSpriteList2D_AddVertex(obj);
			}
			if(iObj<1){ObjRender_SetColor(obj,150,255,150);}
			else if(iObj<5){ObjRender_SetColor(obj,255,255,150);}
			else{ObjRender_SetColor(obj,255,255,255);}
		}
		yield;
	}
}

//----------------------------------------------------
//Graze表示
//----------------------------------------------------
task TGraze()
{
	//Graze文字列
	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_NONE);
	ObjText_SetFontBorderColor(objGraze,128, 128, 128);
	ObjText_SetFontBorderWidth(objGraze, 0);
	Obj_SetRenderPriority(objGraze, 0.01);
	ObjRender_SetX(objGraze, 428);
	ObjRender_SetY(objGraze, 120);

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

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

	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, 428 + iObj * 14);
			ObjSpriteList2D_SetSourceRect(obj, num * 36, 0, (num + 1) * 36, 32);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 24);
			if(num!=0 || iObj==9 || graze>=10^(10-iObj))
			{
			ObjSpriteList2D_AddVertex(obj);
			}
			if(iObj<1){ObjRender_SetColor(obj,150,255,150);}
			else if(iObj<5){ObjRender_SetColor(obj,255,255,150);}
			else{ObjRender_SetColor(obj,255,255,255);}
		}
		yield;
	}
}

//----------------------------------------------------
//残機表示
//----------------------------------------------------
task TPlayerLife
{
	//life文字列
	let objText = ObjText_Create();
	ObjText_SetText(objText, "Life");
	ObjText_SetFontSize(objText, 20);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 255, 255, 255);
	ObjText_SetFontColorBottom(objText, 255, 255, 255);
	ObjText_SetFontBorderType(objText, BORDER_NONE);
	ObjText_SetFontBorderColor(objText,128, 128, 128);
	ObjText_SetFontBorderWidth(objText, 0);
	Obj_SetRenderPriority(objText, 0.01);
	ObjRender_SetX(objText, 428);
	ObjRender_SetY(objText, 150+18+4);

	//残機
	let pathDigit = GetCurrentScriptDirectory() ~ "../img/effect/Marisa_shots.png";
	let count = 8;

	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, 160+22);
	let scale=0.15;
	ObjRender_SetScaleXYZ(obj,scale,scale,scale);
	ObjRender_SetColor(obj,255,150,150);
	Blur;
	while(true)
	{
		let point = GetPlayerLife();
		point = min(point, 99);
		point = max(point, 0);
		let listNum = DigitToArray(point, count);

		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. point)
		{
			let num = listNum[iObj];
			ObjRender_SetX(obj, 488 + iObj * 18);
			ObjSpriteList2D_SetSourceRect(obj, 0, 384, 128, 512);
			ObjSpriteList2D_SetDestRect(obj, -64, -64, 64, 64);
			ObjSpriteList2D_AddVertex(obj);
		}
		yield;
	}

	task Blur
	{
	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, 160+22);
	let Alpha=0.5;
	let scale=0.15;
	ObjRender_SetScaleXYZ(obj,scale,scale,scale);
	ObjRender_SetColor(obj,255,150,150);
	loop(60)
	{
		let point = GetPlayerLife();
		point = min(point, 99);
		point = max(point, 0);
		let listNum = DigitToArray(point, count);

		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. point)
		{
			let num = listNum[iObj];
			ObjRender_SetX(obj, 488 + iObj * 18);
			ObjSpriteList2D_SetSourceRect(obj, 0, 384, 128, 512);
			ObjSpriteList2D_SetDestRect(obj, -64, -64, 64, 64);
			ObjSpriteList2D_AddVertex(obj);
		}
		scale+=0.002;
		Alpha-=0.5/60;
		ObjRender_SetScaleXYZ(obj,scale,scale,scale);
		ObjRender_SetColor(obj,255*Alpha,150*Alpha,150*Alpha);
		yield;
	}
	Obj_Delete(obj);
	Blur;
	}

}

task TPlayerLifeNecessary
{
	//Spell文字列
	let objText = ObjText_Create();
	ObjText_SetText(objText, "Necessary");
	ObjText_SetFontSize(objText, 15);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 255, 255, 255);
	ObjText_SetFontColorBottom(objText, 255, 255, 255);
	ObjText_SetFontBorderType(objText, BORDER_NONE);
	ObjText_SetFontBorderColor(objText,128, 128, 128);
	ObjText_SetFontBorderWidth(objText, 0);
	Obj_SetRenderPriority(objText, 0.01);
	ObjRender_SetX(objText, 428);
	ObjRender_SetY(objText, 170+22);

	//スペル
	let pathDigit = GetCurrentScriptDirectory() ~ "img/Default_SystemDigit.png";
	let count = 2;

	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, 171+22);

	while(true)
	{
		let point = GetCommonData("LifeNecessary",99);
		point = min(point, 99);
		let listNum = DigitToArray(point, count);

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

//----------------------------------------------------
//残スペル表示
//----------------------------------------------------
task TPlayerSpell
{
	//Spell文字列
	let objText = ObjText_Create();
	ObjText_SetText(objText, "Bomb");
	ObjText_SetFontSize(objText, 20);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 255, 255, 255);
	ObjText_SetFontColorBottom(objText, 255, 255, 255);
	ObjText_SetFontBorderType(objText, BORDER_NONE);
	ObjText_SetFontBorderColor(objText,128, 128, 128);
	ObjText_SetFontBorderWidth(objText, 0);
	Obj_SetRenderPriority(objText, 0.01);
	ObjRender_SetX(objText, 428);
	ObjRender_SetY(objText, 202+22-4);

	//スペル
	let pathDigit = GetCurrentScriptDirectory() ~ "../img/effect/Marisa_shots.png";
	let count = 8;

	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, 212+22-4);
	let scale=0.15;
	ObjRender_SetScaleXYZ(obj,scale,scale,scale);
	ObjRender_SetColor(obj,150,255,150);
	Blur;
	while(true)
	{
		let point = GetPlayerSpell;
		point = min(point, 99);
		point = max(point, 0);
		let listNum = DigitToArray(point, count);

		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. point)
		{
			let num = listNum[iObj];
			ObjRender_SetX(obj, 488 + iObj * 18);
			ObjSpriteList2D_SetSourceRect(obj, 0, 384, 128, 512);
			ObjSpriteList2D_SetDestRect(obj, -64, -64, 64, 64);
			ObjSpriteList2D_AddVertex(obj);
		}
		yield;
	}


	task Blur
	{
	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, 212+22-4);
	let Alpha=0.5;
	let scale=0.15;
	ObjRender_SetScaleXYZ(obj,scale,scale,scale);
	ObjRender_SetColor(obj,150,255,150);
	loop(60)
	{
		let point = GetPlayerSpell();
		point = min(point, 99);
		point = max(point, 0);
		let listNum = DigitToArray(point, count);

		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. point)
		{
			let num = listNum[iObj];
			ObjRender_SetX(obj, 488 + iObj * 18);
			ObjSpriteList2D_SetSourceRect(obj, 0, 384, 128, 512);
			ObjSpriteList2D_SetDestRect(obj, -64, -64, 64, 64);
			ObjSpriteList2D_AddVertex(obj);
		}
		scale+=0.002;
		Alpha-=0.5/60;
		ObjRender_SetScaleXYZ(obj,scale,scale,scale);
		ObjRender_SetColor(obj,150*Alpha,255*Alpha,150*Alpha);
		yield;
	}
	Obj_Delete(obj);
	Blur;
	}
}

task TPlayerSpellNecessary
{
	//Spell文字列
	let objText = ObjText_Create();
	ObjText_SetText(objText, "Necessary");
	ObjText_SetFontSize(objText, 15);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 255, 255, 255);
	ObjText_SetFontColorBottom(objText, 255, 255, 255);
	ObjText_SetFontBorderType(objText, BORDER_NONE);
	ObjText_SetFontBorderColor(objText,128, 128, 128);
	ObjText_SetFontBorderWidth(objText, 0);
	Obj_SetRenderPriority(objText, 0.01);
	ObjRender_SetX(objText, 428);
	ObjRender_SetY(objText, 222+24);

	//スペル
	let pathDigit = GetCurrentScriptDirectory() ~ "img/Default_SystemDigit.png";
	let count = 2;

	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, 223+24);

	while(true)
	{
		let point = GetCommonData("BombNecessary",99);
		point = min(point, 99);
		let listNum = DigitToArray(point, count);

		ObjSpriteList2D_ClearVertexCount(obj);
		ascent(iObj in 0 .. count)
		{
			let num = listNum[iObj];
			ObjRender_SetX(obj, 516 + iObj * 14);
			ObjSpriteList2D_SetSourceRect(obj, num * 36, 0, (num + 1) * 36, 32);
			ObjSpriteList2D_SetDestRect(obj, 0, 0, 16, 16);
			ObjSpriteList2D_AddVertex(obj);
		}
		yield;
	}
}
//----------------------------------------------------
//ボスのライフ表示
//----------------------------------------------------
task TBossLife
{
	let objScene;
	objScene = GetEnemyBossObjectID();
	loop
	{
	if(length(GetEnemyBossObjectID)>0 && GetCommonData("SpecialStage",false)==false && GetCommonData("GaugeInVisible",false)==false)
	{
		if(ObjEnemy_GetInfo(GetEnemyBossObjectID()[0],INFO_LIFE)!=1)
		{
		RenderLifeFrame();
		RenderLife();
		while(length(GetEnemyBossObjectID)>0 && ObjEnemy_GetInfo(GetEnemyBossObjectID()[0],INFO_LIFE)>0){yield;}
		}
	}
	yield;
	}

	task RenderLife
	{
		let Vertex=64;
		let Radius=[50,55];

		let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"img/ber2.png");
		ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
		ObjPrim_SetVertexCount(obj, Vertex);
		ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
		Obj_SetRenderPriorityI(obj,31);
		ObjRender_SetAngleZ(obj,180);

		let obj2 = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		ObjPrim_SetTexture(obj2, GetCurrentScriptDirectory~"img/ber2.png");
		ObjPrim_SetPrimitiveType(obj2, PRIMITIVE_TRIANGLEFAN);
		ObjPrim_SetVertexCount(obj2, 4);
		ObjRender_SetBlendType(obj2, BLEND_ADD_RGB);
		Obj_SetRenderPriorityI(obj2,31);
		ObjPrim_SetVertexPosition(obj2, 0, -0, -1, -0);
		ObjPrim_SetVertexPosition(obj2, 1, 384, -1, -0);
		ObjPrim_SetVertexPosition(obj2, 2, 384, 1, 0);
		ObjPrim_SetVertexPosition(obj2, 3, -0, 1, 0);

		ObjPrim_SetVertexUVT(obj2, 0, 0, 12);
		ObjPrim_SetVertexUVT(obj2, 1, 128, 12);
		ObjPrim_SetVertexUVT(obj2, 2, 128, 16);
		ObjPrim_SetVertexUVT(obj2, 3, 0, 16);
		ObjRender_SetPosition(obj2,0,448-1,0);

		ascent(i in 0..Vertex)
		{
			ObjPrim_SetVertexPosition(obj, i, Radius[i%2]*cos(360/(Vertex-1)*i), Radius[i%2]*sin(360/(Vertex-1)*i),0);
		}
		ascent(i in 0..Vertex)
		{	
			ObjPrim_SetVertexUVT(obj, i,  0+128/(Vertex)*i,  0+4*(i%2));
		}

		let Color=[255,50,50];
		let Color2=[255,255,255];
		let Alpha=1;
		let AR_Angle=0;
		let AlphaRate=0.75+0.25*cos(AR_Angle);
		let HueCount=0;
		let RingRate=0;
		let MaxLife=ObjEnemy_GetInfo(GetEnemyBossObjectID()[0],INFO_LIFE);
		while(length(GetEnemyBossObjectID)>0)
		{
			if(ObjEnemy_GetInfo(GetEnemyBossObjectID()[0],INFO_LIFE)<=0){break;}
			let Dis=((ObjRender_GetY(GetEnemyBossObjectID()[0])-GetPlayerY)^2+(ObjRender_GetX(GetEnemyBossObjectID()[0])-GetPlayerX)^2)^0.5;
			if(Dis<100)
			{
				Alpha=0.1+0.9*Dis/100;
			}
			else
			{
				Alpha=1;
			}
			if(RingRate<1){RingRate+=1/60;}
			let LifeLeft=ObjEnemy_GetInfo(GetEnemyBossObjectID()[0],INFO_LIFE)/(MaxLife/360)*RingRate;
			let LifeAngle=LifeLeft/(Vertex-2);
			ascent(i in 0..Vertex)
			{
				alternative(i)
				case(0, 1){ObjPrim_SetVertexPosition(obj,i,Radius[i%2]*sin(0),Radius[i%2]*cos(0),0); }
				case(Vertex-2, Vertex-1){ObjPrim_SetVertexPosition(obj,i,Radius[i%2]*sin(LifeLeft),Radius[i%2]*cos(LifeLeft),0); }
				others{ObjPrim_SetVertexPosition(obj,i,Radius[i%2]*sin(LifeAngle*i),Radius[i%2]*cos(LifeAngle*i),0);}			
			}
			ObjRender_SetPosition(obj,ObjRender_GetX(GetEnemyBossObjectID()[0]),ObjRender_GetY(GetEnemyBossObjectID()[0]),0);
			AR_Angle+=2;
			AlphaRate=0.75+0.25*cos(AR_Angle);
			Alpha*=AlphaRate;
			let H_Color=HueDifine(HueCount);
			ObjRender_SetColor(obj,(Color[0]-H_Color[0]/4)*Alpha,(Color[1]-H_Color[1]/4)*Alpha,(Color[2]-H_Color[2]/4)*Alpha);
			HueCount+=3;

			ObjRender_SetScaleX(obj2,LifeLeft/360);
			ObjRender_SetColor(obj2,(Color2[0]-H_Color[0]/4)*Alpha,(Color2[1]-H_Color[1]/4)*Alpha,(Color2[2]-H_Color[2]/4)*Alpha);
			yield;
		}
		Obj_Delete(obj);
		Obj_Delete(obj2);
	}

	task RenderLifeFrame
	{
		let Vertex=64;
		let Radius=[49,56];

		let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"img/ber2.png");
		ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
		ObjPrim_SetVertexCount(obj, Vertex);
		ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
		Obj_SetRenderPriorityI(obj,30);
		ObjRender_SetAngleZ(obj,180);

		ascent(i in 0..Vertex)
		{
			ObjPrim_SetVertexPosition(obj, i, Radius[i%2]*cos(360/(Vertex-1)*i), Radius[i%2]*sin(360/(Vertex-1)*i),0);
		}
		ascent(i in 0..Vertex)
		{	
			ObjPrim_SetVertexUVT(obj, i,  0+128/(Vertex)*i,  5+6*(i%2));
		}

		let Alpha=1;
		let Color=[10,10,10];
		let RingRate=0;
		while(length(GetEnemyBossObjectID)>0)
		{
			if(ObjEnemy_GetInfo(GetEnemyBossObjectID()[0],INFO_LIFE)<=0){break;}
			let Dis=((ObjRender_GetY(GetEnemyBossObjectID()[0])-GetPlayerY)^2+(ObjRender_GetX(GetEnemyBossObjectID()[0])-GetPlayerX)^2)^0.5;
			if(Dis<100)
			{
				Alpha=1*Dis/100;
			}
			else
			{
				Alpha=1;
			}
			if(RingRate<1){RingRate+=1/60;}
			let LifeLeft=360*RingRate;
			let LifeAngle=LifeLeft/(Vertex-2);
			ascent(i in 0..Vertex)
			{
				alternative(i)
				case(0, 1){ObjPrim_SetVertexPosition(obj,i,Radius[i%2]*sin(0),Radius[i%2]*cos(0),0); }
				case(Vertex-2, Vertex-1){ObjPrim_SetVertexPosition(obj,i,Radius[i%2]*sin(LifeLeft),Radius[i%2]*cos(LifeLeft),0); }
				others{ObjPrim_SetVertexPosition(obj,i,Radius[i%2]*sin(LifeAngle*i),Radius[i%2]*cos(LifeAngle*i),0);}			
			}
			ObjRender_SetPosition(obj,ObjRender_GetX(GetEnemyBossObjectID()[0]),ObjRender_GetY(GetEnemyBossObjectID()[0]),0);
			ObjRender_SetColor(obj,Color[0]*Alpha,Color[1]*Alpha,Color[2]*Alpha);
			yield;
		}
		Obj_Delete(obj);
	}
}

//----------------------------------------------------
//タイマー表示
//----------------------------------------------------
task TBossTimer
{
	let OriTime=ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID(), INFO_TIMERF);
	let Time=OriTime;
	let str_SCB=IntToString(Time);
	loop
	{
	OriTime=ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID(), INFO_TIMERF);
	Time=OriTime;
	str_SCB=IntToString(Time);
	if(length(GetEnemyBossObjectID)>0 && GetCommonData("SpecialStage",false)==false)
	{
		Time=ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID(), INFO_TIMERF)*100/60;
		if(Time>=10000){Time=9999;}
		str_SCB=IntToString(Time);
		if(OriTime>0)
		{
		ascent(i in 0..length(str_SCB))
		{
			DrawNum(198-168-7*length(str_SCB)+7*i,11.5,i);
			loop(1){yield;}
		}
		while(length(GetEnemyBossObjectID)>0)
		{
			Time=ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID(), INFO_TIMERF)*100/60;
			if(Time>=10000){Time=9999;}

			str_SCB=IntToString(Time);
			while(length(str_SCB)<4)
			{
				str_SCB=IntToString(0)~str_SCB
			}
			yield;
		}
		}
	}
	yield;
	}
		task DrawNum(x,y,number)
		{
			let obj = ObjText_Create();
			ObjText_SetText(obj, str_SCB[number]);
			ObjText_SetFontSize(obj, 10);
			ObjText_SetFontBold(obj, true);
			ObjText_SetFontColorTop(obj, 255, 255, 255);
//			ObjText_SetFontColorBottom(obj, color[0], color[1], color[2]);
			ObjText_SetFontBorderType(obj, BORDER_NONE);
			ObjText_SetFontBorderColor(obj,0, 255, 0);
			ObjText_SetFontBorderWidth(obj, 0);
			Obj_SetRenderPriorityI(obj, 72);
			ObjRender_SetX(obj,16+x);
			ObjRender_SetY(obj, y);
			//ObjText_SetFontType(obj,"ＭＳ 明朝");

			ObjText_SetMaxWidth(obj,360);
			let Alpha=255;
			let scale=3;

			if(number<2)
			{
				ObjText_SetFontSize(obj, 20);
				ObjRender_SetX(obj,16+x-16+7*number);
				ObjRender_SetY(obj, y-9.0);
			}

			loop(15)
			{
				scale-=2/15;
				ObjRender_SetScaleXYZ(obj,scale,scale,scale);
				if(GetPlayerY<=160)
				{		
					Alpha=(GetPlayerY/160)*255;
				}
				else{Alpha=255;}
				ObjRender_SetAlpha(obj,Alpha);
				yield;
			}
			while(length(GetEnemyBossObjectID)>0)
			{
				ObjText_SetText(obj, str_SCB[number]);
				if(GetPlayerY<=160)
				{		
				Alpha=(GetPlayerY/160)*255;
				}
				else{Alpha=255;}
				ObjRender_SetAlpha(obj,Alpha);
				yield;
			}
			Obj_Delete(obj);
		}

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

}

//----------------------------------------------------
//スペルカード取得表示
//----------------------------------------------------
task TGainSpell(score)
{
	let objText = ObjText_Create();
	ObjText_SetText(objText, "Spell Card Bonus!");
	ObjText_SetFontSize(objText, 32);
	ObjText_SetFontBold(objText, true);
	ObjText_SetFontColorTop(objText, 255, 255, 255);
	ObjText_SetFontColorBottom(objText, 128, 128, 255);
	ObjText_SetFontBorderType(objText, BORDER_FULL);
	ObjText_SetFontBorderColor(objText,255, 255, 255);
	ObjText_SetFontBorderWidth(objText, 1);
	Obj_SetRenderPriority(objText, 0.6);
	ObjRender_SetX(objText, 32);
	ObjRender_SetY(objText, 98);

	let strScore = "+" ~ IntToString(score);
	let objScore = ObjText_Create();
	ObjText_SetText(objScore, strScore);
	ObjText_SetFontSize(objScore, 32);
	ObjText_SetFontBold(objScore, true);
	ObjText_SetFontColorTop(objScore, 255, 255, 255);
	ObjText_SetFontColorBottom(objScore, 255, 128, 128);
	ObjText_SetFontBorderType(objScore, BORDER_FULL);
	ObjText_SetFontBorderColor(objScore,255, 255, 255);
	ObjText_SetFontBorderWidth(objScore, 1);
	Obj_SetRenderPriority(objScore, 0.6);
	ObjRender_SetX(objScore, 180);
	ObjRender_SetY(objScore, 140);

	loop(120)
	{
		yield;
	}
	Obj_Delete(objText);
	Obj_Delete(objScore);
}

//----------------------------------------------------
//ユーティリティ
//----------------------------------------------------
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;
}

task TBossDistort
{
	let objScene;
	objScene = GetEnemyBossObjectID();
	loop
	{
	if(length(GetEnemyBossObjectID)>0 && GetCommonData("SpecialStage",false)==false)
	{
		Distort();
		while(length(GetEnemyBossObjectID)>0 && ObjEnemy_GetInfo(GetEnemyBossObjectID()[0],INFO_LIFE)>0){yield;}
	}
	yield;
	}

	task Distort()
	{
		//レンダリングターゲットに使用するテクスチャ
		let renderTexture = GetReservedRenderTargetName(0);

		let frame = 0; //フレーム
		let baseEffectRadius = 144; //基準エフェクト半径
		let outerFluct = 16; //エフェクト半径の最大変化量
		let effectRadius = 0; //エフェクト半径
		let circleVertexCount = 12; //円をあらわすのに使用する頂点の数
		let innerCircleCount = 2;

		let priEffectMin = 20-0; //エフェクトをかける最小優先度
		let priEffectMax = 28-0; //エフェクトをかける最大優先度

		//背景のみエフェクトの対象とする
		//エフェクトの描画でまかなえるため、
		//優先度20〜28の通常描画を無効にする。
	//	SetInvalidRenderPriorityA1(priEffectMin, priEffectMax);


		let frameWidth = GetStgFrameWidth();
		let frameHeight = GetStgFrameHeight();
		let frameLeft = GetStgFrameLeft();
		let frameRight = frameLeft + frameWidth;
		let frameTop = GetStgFrameTop();
		let frameBottom = frameTop + frameHeight;

		//--------------------------------
		//円形の影
/*
		let path = GetCurrentScriptDirectory() ~ "Effect02.png";
		let objShadow = ObjPrim_Create(OBJ_SPRITE_2D); //2Dスプライトオブジェクト生成
		Obj_SetRenderPriorityI(objShadow, 25); //描画優先度を設定
		ObjPrim_SetTexture(objShadow, path); //テクスチャを設定
		ObjRender_SetBlendType(objShadow, BLEND_MULTIPLY);
		ObjSprite2D_SetSourceRect(objShadow, 1, 1, 255, 255); //描画元設定(44, 54)-(71,105)
		ObjSprite2D_SetDestCenter(objShadow);
		ObjRender_SetScaleXYZ(objShadow, 1.2, 1.2, 0);
*/
		//--------------------------------
		//円内
		let objInnerC = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		Obj_SetRenderPriorityI(objInnerC, priEffectMax + 1);
		ObjPrim_SetTexture(objInnerC, renderTexture);
		ObjPrim_SetVertexCount(objInnerC, 6 * circleVertexCount * innerCircleCount);

/*
		//円外〜エフェクト枠
		let objOuterC = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		Obj_SetRenderPriorityI(objOuterC, priEffectMax + 1);
		ObjPrim_SetTexture(objOuterC, renderTexture); //テクスチャを設定
		ObjPrim_SetVertexCount(objOuterC, 3 * circleVertexCount);

		//エフェクト枠外オブジェクト
		let objOuterE = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
		Obj_SetRenderPriorityI(objOuterE, priEffectMax + 1);
		ObjPrim_SetTexture(objOuterE, renderTexture); //テクスチャを設定
*/
		let DistortionAngle=0;

		//ボスのライフが0になるまでエフェクトをかける。
		let objEnemy = GetEnemyBossObjectID[0];
		while(length(GetEnemyBossObjectID)>0)
		{
			if(ObjEnemy_GetInfo(GetEnemyBossObjectID()[0],INFO_LIFE)<=0){break;}
			//エフェクト半径
			effectRadius = baseEffectRadius + outerFluct * sin(frame*4)*0;

			let enemyX = ObjMove_GetX(objEnemy); //敵座標X
			let enemyY = ObjMove_GetY(objEnemy); //敵座標Y
			let circleLeft = enemyX - effectRadius + frameLeft; //エフェクト円の左端
			let circleRight = enemyX + effectRadius + frameLeft; //エフェクト円の右端
			let circleTop = enemyY - effectRadius + frameTop; //エフェクト円の上端
			let circleBottom = enemyY + effectRadius + frameTop; //エフェクト円の下端

			//--------------------------------
			//優先度20〜28(背景)をエフェクト用のテクスチャに描画
			RenderToTextureA1(renderTexture, priEffectMin, priEffectMax, true);
/*
			//--------------------------------
			//影
			ObjRender_SetPosition(objShadow, enemyX, enemyY, 0);
*/
			//--------------------------------
			//円内
			//エフェクト内円を描画
			//ゆらいでいる部分


			ObjRender_SetPosition(objInnerC, -frameLeft, -frameTop, 0);
			ascent(let iPosI in 0 .. circleVertexCount) 
			{
				let xyOldRadius = 0;
				ascent(let iCircle in 0 .. innerCircleCount) 
				{
					let vertexIndex = (iPosI * innerCircleCount + iCircle) * 6;
					let angle1 =  iPosI * (360 / circleVertexCount);
					let angle2 =  (iPosI + 1) * (360 / circleVertexCount);

					//テクスチャのUV座標
					let uvRadius1 = iCircle * effectRadius / innerCircleCount;
					let uvRadius2 = (iCircle + 1) * effectRadius / innerCircleCount;

					//テクスチャのUV座標と描画先のXY座標を変化させることでゆらぎを表現する
					let xyRadius1t = iCircle * effectRadius / innerCircleCount;
					let xyRadius2t = (iCircle + 1) * effectRadius / innerCircleCount;
					let xyRadius1 = xyOldRadius;
					let xyBias = (xyRadius2t - xyRadius1t) / 2*1.75 + (xyRadius2t - xyRadius1t) * sin(frame*16/8 + (iCircle * 360 / innerCircleCount)) / 4;
					let xyRadius2 = xyRadius2t + xyBias / ((innerCircleCount - iCircle + 1) / 2);

					xyOldRadius = xyRadius2;
					if(iCircle == innerCircleCount - 1)
					{
						xyRadius2 = uvRadius2;
					}

					//上記の頂点座標を設定
					let leftBias = enemyX + frameLeft;
					let topBias = enemyY + frameTop;

					let VertexXY_X=[leftBias + xyRadius1 * cos(angle1),leftBias + xyRadius2 * cos(angle1),leftBias + xyRadius1 * cos(angle2),leftBias + xyRadius2 * cos(angle2)];
					let VertexXY_Y=[topBias + xyRadius1 * sin(angle1),topBias + xyRadius2 * sin(angle1),topBias + xyRadius1 * sin(angle2),topBias + xyRadius2 * sin(angle2)];

					if(iCircle==0)
					{
					let dx=96*cos(5*DistortionAngle);
					let dy=96*sin(6*DistortionAngle);
					VertexXY_X[0]=VertexXY_X[0]+dx;
					VertexXY_X[2]=VertexXY_X[2]+dx;
					VertexXY_Y[0]=VertexXY_Y[0]+dy;
					VertexXY_Y[2]=VertexXY_Y[2]+dy;
					}
					ObjPrim_SetVertexPosition(objInnerC, vertexIndex  , VertexXY_X[0], VertexXY_Y[0], 0);
					ObjPrim_SetVertexPosition(objInnerC, vertexIndex+1, VertexXY_X[1], VertexXY_Y[1], 0);
					ObjPrim_SetVertexPosition(objInnerC, vertexIndex+2, VertexXY_X[2], VertexXY_Y[2], 0);
				
					ObjPrim_SetVertexPosition(objInnerC, vertexIndex+3, VertexXY_X[2], VertexXY_Y[2], 0);
					ObjPrim_SetVertexPosition(objInnerC, vertexIndex+4, VertexXY_X[1], VertexXY_Y[1], 0);
					ObjPrim_SetVertexPosition(objInnerC, vertexIndex+5, VertexXY_X[3], VertexXY_Y[3], 0);

					let VertexUV_X=[leftBias + uvRadius1 * cos(angle1),leftBias + uvRadius2 * cos(angle1),leftBias + uvRadius1 * cos(angle2),leftBias + uvRadius2 * cos(angle2)];
					let VertexUV_Y=[topBias + uvRadius1 * sin(angle1),topBias + uvRadius2 * sin(angle1),topBias + uvRadius1 * sin(angle2),topBias + uvRadius2 * sin(angle2)];

					ObjPrim_SetVertexUVT(objInnerC, vertexIndex  , VertexUV_X[0], VertexUV_Y[0]);
					ObjPrim_SetVertexUVT(objInnerC, vertexIndex+1, VertexUV_X[1], VertexUV_Y[1]);
					ObjPrim_SetVertexUVT(objInnerC, vertexIndex+2, VertexUV_X[2], VertexUV_Y[2]);
				
					ObjPrim_SetVertexUVT(objInnerC, vertexIndex+3, VertexUV_X[2], VertexUV_Y[2]);
					ObjPrim_SetVertexUVT(objInnerC, vertexIndex+4, VertexUV_X[1], VertexUV_Y[1]);
					ObjPrim_SetVertexUVT(objInnerC, vertexIndex+5, VertexUV_X[3], VertexUV_Y[3]);

				}
			}
			DistortionAngle+=0.1;
			ObjRender_SetColor(objInnerC,255,225,255);
/*
			//--------------------------------
			//円外〜エフェクト枠
			//揺らいでいる円の外側から矩形までを埋める

			ObjRender_SetPosition(objOuterC, -frameLeft, -frameTop, 0);
			ascent(let iPosO in 0 .. circleVertexCount) 
			{
				let angle1 = iPosO * (360 / circleVertexCount);
				let angle2 = (iPosO + 1) * (360 / circleVertexCount);
				let rectX = 0;
				let rectY = 0;
				if(angle1 >= 0 && angle1 < 90)
				{
					rectX = circleRight;
					rectY = circleBottom;
				}
				else if(angle1 >= 90 && angle1 < 180)
				{
					rectX = circleLeft;
					rectY = circleBottom;
				}
				else if(angle1 >= 180 && angle1 < 270)
				{
					rectX = circleLeft;
					rectY = circleTop;
				}
				else if(angle1 >= 270 && angle1 < 360)
				{
					rectX = circleRight;
					rectY = circleTop;
				}
				let vertexIndex = iPosO * 3;
				ObjPrim_SetVertexPosition(objOuterC, vertexIndex, enemyX + effectRadius * cos(angle1) + frameLeft, enemyY + effectRadius * sin(angle1) + frameTop, 0);
				ObjPrim_SetVertexPosition(objOuterC, vertexIndex+1, rectX, rectY, 0);
				ObjPrim_SetVertexPosition(objOuterC, vertexIndex+2, enemyX + effectRadius * cos(angle2) + frameLeft, enemyY + effectRadius * sin(angle2) + frameTop, 0);

				ObjPrim_SetVertexUVT(objOuterC, vertexIndex, enemyX + effectRadius * cos(angle1) + frameLeft, enemyY + effectRadius * sin(angle1) + frameTop);
				ObjPrim_SetVertexUVT(objOuterC, vertexIndex+1, rectX, rectY);
				ObjPrim_SetVertexUVT(objOuterC, vertexIndex+2, enemyX + effectRadius * cos(angle2) + frameLeft, enemyY + effectRadius * sin(angle2) + frameTop);
			}
			


			//--------------------------------
			//エフェクト枠外
			//エフェクト枠外の背景を描画する
			ObjSpriteList2D_ClearVertexCount(objOuterE);
			ObjRender_SetPosition(objOuterE, -frameLeft, -frameTop, 0);
			ObjSpriteList2D_SetSourceRect(objOuterE, frameLeft, frameTop, frameRight, circleTop + 1);
			ObjSpriteList2D_SetDestRect(objOuterE, frameLeft, frameTop, frameRight, circleTop + 1);
			ObjSpriteList2D_AddVertex(objOuterE);

			ObjSpriteList2D_SetSourceRect(objOuterE, frameLeft, circleTop, circleLeft + 1, circleBottom);
			ObjSpriteList2D_SetDestRect(objOuterE, frameLeft, circleTop, circleLeft + 1, circleBottom);
			ObjSpriteList2D_AddVertex(objOuterE);

			ObjSpriteList2D_SetSourceRect(objOuterE, circleRight, circleTop, frameRight, circleBottom);
			ObjSpriteList2D_SetDestRect(objOuterE, circleRight, circleTop, frameRight, circleBottom);
			ObjSpriteList2D_AddVertex(objOuterE);

			ObjSpriteList2D_SetSourceRect(objOuterE, frameLeft, circleBottom, frameRight, frameBottom);
			ObjSpriteList2D_SetDestRect(objOuterE, frameLeft, circleBottom, frameRight, frameBottom);
			ObjSpriteList2D_AddVertex(objOuterE);
*/
			frame++;
			yield;
		}

		//エフェクト用オブジェクト削除
		Obj_Delete(objInnerC);
	//	Obj_Delete(objOuterC);
	//	Obj_Delete(objOuterE);
	//	Obj_Delete(objShadow);
		ClearInvalidRenderPriority();
	}
}

task BombBarrier
{
let objEnemy;
let BarrierCount=0;
loop
{
if(length(GetEnemyBossObjectID)>0)
{
	BarrierCount=0;
	objEnemy=GetEnemyBossObjectID[0];
	while(length(GetEnemyBossObjectID)>0)
	{
		objEnemy=GetEnemyBossObjectID[0];
		if(BarrierRequirement)
		{
			SetRatio;
			Barrier;
			if(GetCommonData("BombBarrierLevel",2)>=4)
			{
			BarrierCount=GetCommonData("BombBarrierLevel",2)*30-60;
			}
			while(BarrierRequirement)
			{
				if(length(GetEnemyBossObjectID)>0)
				{
				objEnemy=GetEnemyBossObjectID[0];
				}
				yield;
			}
			SetCommonData("BombBarrierLevel",GetCommonData("BombBarrierLevel",2)-1);
			SetCommonData("DamageRateRatio",1);
		}
		yield;
	}
}
yield;
}

	task Barrier
	{
		ascent(i in 0..5)
		{
		E_Circle(i*72,i);
		}

		task E_Circle(Angle,type)
		{
			let R=255;let G=255;let B=255;
			alternative(type)
			case(0){R=0;G=255;B=0;}
			case(1){R=255;G=0;B=0;}
			case(2){R=152;G=53;B=0;}
			case(3){R=255;G=255;B=100;}
			case(4){R=0;G=0;B=255;}
			let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
			ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"img/circle.png");
			ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
			ObjPrim_SetVertexCount(obj, 4);
			ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
			Obj_SetRenderPriorityI(obj,31);
			ObjPrim_SetVertexPosition(obj, 0, -128, -128, -0);
			ObjPrim_SetVertexPosition(obj, 1,128, -128, -0);
			ObjPrim_SetVertexPosition(obj, 2, 128, 128, 0);
			ObjPrim_SetVertexPosition(obj, 3, -128, 128, 0);

			ObjPrim_SetVertexUVT(obj, 0, 0, 0);
			ObjPrim_SetVertexUVT(obj, 1, 256, 0);
			ObjPrim_SetVertexUVT(obj, 2, 256, 256);
			ObjPrim_SetVertexUVT(obj, 3, 0, 256);

			let Scale=0;
			let Radius=0;
			let Zangle=0;
			let Alpha=0;
			Obj_SetVisible(obj,false);
			loop
			{
				if(!BarrierRequirement){break;}
				if(Scale<0.4)
				{
				Scale+=0.4/60;
				Alpha+=1/60;
				}
				Radius=128*Scale;
				ObjRender_SetScaleXYZ(obj,Scale,Scale,Scale);
				ObjRender_SetAngleZ(obj,Zangle);
				ObjRender_SetColor(obj,R*Alpha,G*Alpha,B*Alpha);
				Angle+=6;
				Zangle-=6;
				E_CircleBlur(ObjRender_GetX(obj),ObjRender_GetY(obj),Angle,Scale,Zangle,Alpha,R,G,B);
				if(length(GetEnemyBossObjectID)>0 && !Obj_IsDeleted(objEnemy))
				{
					ObjRender_SetPosition(obj,ObjRender_GetX(objEnemy)+Radius*cos(Angle),ObjRender_GetY(objEnemy)+Radius*sin(Angle),0);
				}
				yield;
			}
			loop(60)
			{
				Scale-=0.4/60;
				Alpha-=1/60;

				Radius=128*Scale;
				ObjRender_SetScaleXYZ(obj,Scale,Scale,Scale);
				ObjRender_SetAngleZ(obj,Zangle);
				ObjRender_SetColor(obj,R*Alpha,G*Alpha,B*Alpha);
				Angle+=6;
				Zangle-=6;
				E_CircleBlur(ObjRender_GetX(obj),ObjRender_GetY(obj),Angle,Scale,Zangle,Alpha,R,G,B);
				if(length(GetEnemyBossObjectID)>0 && !Obj_IsDeleted(objEnemy))
				{
					ObjRender_SetPosition(obj,ObjRender_GetX(objEnemy)+Radius*cos(Angle),ObjRender_GetY(objEnemy)+Radius*sin(Angle),0);
				}
				yield;
			}
			Obj_Delete(obj);
		}

		task E_CircleBlur(x,y,Angle,Scale,Zangle,Alpha,R,G,B)
		{
			Alpha*=0.2;
			let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
			ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"img/circle.png");
			ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
			ObjPrim_SetVertexCount(obj, 4);
			ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
			Obj_SetRenderPriorityI(obj,31);
			ObjPrim_SetVertexPosition(obj, 0, -128, -128, -0);
			ObjPrim_SetVertexPosition(obj, 1,128, -128, -0);
			ObjPrim_SetVertexPosition(obj, 2, 128, 128, 0);
			ObjPrim_SetVertexPosition(obj, 3, -128, 128, 0);

			ObjPrim_SetVertexUVT(obj, 0, 0, 0);
			ObjPrim_SetVertexUVT(obj, 1, 256, 0);
			ObjPrim_SetVertexUVT(obj, 2, 256, 256);
			ObjPrim_SetVertexUVT(obj, 3, 0, 256);
			
			ObjRender_SetPosition(obj,x,y,0);
			ObjRender_SetScaleXYZ(obj,Scale,Scale,Scale);
			ObjRender_SetAngleZ(obj,Zangle);
			let MaxAlpha=Alpha;

			loop(10)
			{
				Alpha-=MaxAlpha/10;
				ObjRender_SetColor(obj,R*Alpha,G*Alpha,B*Alpha);
				if(length(GetEnemyBossObjectID)>0 && !Obj_IsDeleted(objEnemy))
				{
					ObjRender_SetPosition(obj,x,y,0);
				}
				yield;
			}
			Obj_Delete(obj);
		}
	}

	function BarrierRequirement
	{
		if(GetCommonData("BossName","None")!="Patchouli"){return(false);}
		let Level=GetCommonData("BombBarrierLevel",2);
		if(ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID,INFO_IS_SPELL)==false){return(false);}
		if(IsPlayerSpellActive){return(true);}
		if(Level>=3)
		{
			if(GetPlayerInvincibilityFrame>0){return(true);}
		}
		if(Level>=4)
		{
			BarrierCount-=1/6;
			if(BarrierCount>0){return(true);}
		}
		return(false);
	}

	function SetRatio
	{
		let Level=GetCommonData("BombBarrierLevel",2);
		if(Level<1){SetCommonData("DamageRateRatio",0.5);}
		else if(Level<2){SetCommonData("DamageRateRatio",0.25);}
		else{SetCommonData("DamageRateRatio",0);}
	}
}

function HueDifine(HueCount)
{
let ColorRed=0;
let ColorGreen=0;
let ColorBlue=0;
let MinColor=0;
while(HueCount>360){HueCount-=360;}
while(HueCount<=0){HueCount+=360;}
	if(0<HueCount && HueCount<=60)
	{
		ColorRed=255;
		ColorGreen=255*(HueCount)/60;
		ColorBlue=0;
	}
	else if(HueCount<=120)
	{
		ColorRed=255*(120-HueCount)/60;
		ColorGreen=255;
		ColorBlue=0;
	}
	else if(HueCount<=180)
	{
		ColorRed=0;
		ColorGreen=255;
		ColorBlue=255*(HueCount-120)/60;
	}
	else if(HueCount<=240)
	{
		ColorRed=0;
		ColorGreen=255*(240-HueCount)/60;
		ColorBlue=255;
	}
	else if(HueCount<=300)
	{
		ColorRed=255*(HueCount-240)/60;
		ColorGreen=0;
		ColorBlue=255;
	}
	else if(HueCount<=360)
	{
		ColorRed=255;
		ColorGreen=0;
		ColorBlue=255*(360-HueCount)/60;
	}
	return([ColorRed,ColorGreen,ColorBlue]);
}