let dirCurrent = GetCurrentScriptDirectory();
let typeEnd = 0;
let END_FAILED = 1;
let END_SUCCESS = 2;
let BossPosition=[0,0];
let SpellCardBonus=10;
let DefeatTime=0;
let objScene;
let objBoss;

@Initialize
{
	let pathUseSpell = dirCurrent ~ "../se/seUseSpellCard.wav";
	LoadSound(pathUseSpell);
	PlaySE(pathUseSpell);
	SetAutoDeleteObject(true);
	objBoss = GetEnemyBossObjectID();
	objScene = GetEnemyBossSceneObjectID();
	T_SCB;

	alternative(GetCommonData("BossName","None"))
	case("Flandre"){Flandre_BG;}
	case("Patchouli"){Patchouli_BG;}
	case("Magic"){Magic_BG;}
	case("Special"){Special_BG;}

	MagicCircle();
	//TWaveCircle();
}

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

@Event
{
	alternative(GetEventType())
	case(EV_END_BOSS_STEP)
	{
		//次の敵動作へ
		if(typeEnd == 0)
		{
			//スペルカード取得失敗
			typeEnd = END_FAILED;
//			BG_Flandre;
		}
	}
	case(EV_GAIN_SPELL)
	{
		LoadSound(dirCurrent ~ "../se/seGetSpellCardBonus.wav");
		PlaySE(dirCurrent ~ "../se/seGetSpellCardBonus.wav");
		//スペルカード取得

		let ChallengeArray=[];
		alternative(GetAreaCommonData("TUNING","PlayerCharacter",0))
		case(0){ChallengeArray=GetAreaCommonData("SPELL_RECORD","Reimu_Challenge",[]);}
		case(1){ChallengeArray=GetAreaCommonData("SPELL_RECORD","Marisa_Challenge",[]);}

		let GetArray=[];
		alternative(GetAreaCommonData("TUNING","PlayerCharacter",0))
		case(0){GetArray=GetAreaCommonData("SPELL_RECORD","Reimu_Get",[]);}
		case(1){GetArray=GetAreaCommonData("SPELL_RECORD","Marisa_Get",[]);}

		let FirstArray=[];
		alternative(GetAreaCommonData("TUNING","PlayerCharacter",0))
		case(0){FirstArray=GetAreaCommonData("SPELL_RECORD","Reimu_First",[]);}
		case(1){FirstArray=GetAreaCommonData("SPELL_RECORD","Marisa_First",[]);}

		let ScoreArray=[];
		alternative(GetAreaCommonData("TUNING","PlayerCharacter",0))
		case(0){ScoreArray=GetAreaCommonData("SPELL_RECORD","Reimu_Score",[]);}
		case(1){ScoreArray=GetAreaCommonData("SPELL_RECORD","Marisa_Score",[]);}

		let EarlyArray=[];
		alternative(GetAreaCommonData("TUNING","PlayerCharacter",0))
		case(0){EarlyArray=GetAreaCommonData("SPELL_RECORD","Reimu_Early",[]);}
		case(1){EarlyArray=GetAreaCommonData("SPELL_RECORD","Marisa_Early",[]);}

		if(!IsReplay)
		{
		if(GetArray[GetCommonData("SpellNumber",0)]==0)
		{
			FirstArray[GetCommonData("SpellNumber",0)]=ChallengeArray[GetCommonData("SpellNumber",0)];
			alternative(GetAreaCommonData("TUNING","PlayerCharacter",0))
			case(0){SetAreaCommonData("SPELL_RECORD","Reimu_First",FirstArray);}
			case(1){SetAreaCommonData("SPELL_RECORD","Marisa_First",FirstArray);}
		}

		if(ScoreArray[GetCommonData("SpellNumber",0)]<SpellCardBonus)
		{
			ScoreArray[GetCommonData("SpellNumber",0)]=SpellCardBonus;
			alternative(GetAreaCommonData("TUNING","PlayerCharacter",0))
			case(0){SetAreaCommonData("SPELL_RECORD","Reimu_Score",ScoreArray);}
			case(1){SetAreaCommonData("SPELL_RECORD","Marisa_Score",ScoreArray);}
		}

		if(EarlyArray[GetCommonData("SpellNumber",0)]>DefeatTime)
		{
			EarlyArray[GetCommonData("SpellNumber",0)]=DefeatTime;
			alternative(GetAreaCommonData("TUNING","PlayerCharacter",0))
			case(0){SetAreaCommonData("SPELL_RECORD","Reimu_Early",EarlyArray);}
			case(1){SetAreaCommonData("SPELL_RECORD","Marisa_Early",EarlyArray);}
		}

		GetArray[GetCommonData("SpellNumber",0)]=GetArray[GetCommonData("SpellNumber",0)]+1;
		alternative(GetAreaCommonData("TUNING","PlayerCharacter",0))
		case(0){SetAreaCommonData("SPELL_RECORD","Reimu_Get",GetArray);}
		case(1){SetAreaCommonData("SPELL_RECORD","Marisa_Get",GetArray);}
		}

		typeEnd = END_SUCCESS;
		AddScore(SpellCardBonus);
	}
}

task T_SCB
{
	let GrazeRate=1;
	let MissCount=0;
	let BombCount=0;
	let SCB_GetFlag=1;
	let OriSCB=GetCommonData("Ori_SpellCardBonus",10);
	//OriSCB=ObjEnemyBossScene_GetInfo(objScene,INFO_SPELL_SCORE);
	let OriTimer=ObjEnemyBossScene_GetInfo(objScene,INFO_ORGTIMERF);
	let Timer=ObjEnemyBossScene_GetInfo(objScene,INFO_TIMERF);
	SpellCardBonus=OriSCB;
	let str_SCB=IntToString(SpellCardBonus);
	while(length(str_SCB)<8)
	{
		str_SCB=IntToString(0)~str_SCB;
	}

	DrawSCB;
	loop(300)
	{
		GrazeRate=1+GetGraze*0.01;
	//	SpellCardBonus=OriSCB*GrazeRate;//SCBグレイズ倍率上昇
	//	SpellCardBonus=OriSCB+GetGraze*500;//SCBグレイズ倍率上昇
		str_SCB=IntToString(SpellCardBonus);
		while(length(str_SCB)<8)
		{
		str_SCB=IntToString(0)~str_SCB;
		}
		MissCount=ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SHOOTDOWN_COUNT);
		BombCount=ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SPELL_COUNT);
		if(MissCount!=0 || BombCount!=0){SCB_GetFlag=0;}
		yield;
	}
	OriTimer-=300;
	if(GetCommonData("DurableSpell",false))
	{
		SpellCardBonus=OriSCB;
		SetCommonData("SpellCardBonus",SpellCardBonus);
		str_SCB=IntToString(SpellCardBonus);
	}
	while(typeEnd==0)
	{
		if(!GetCommonData("DurableSpell",false))
		{
		Timer=ObjEnemyBossScene_GetInfo(objScene,INFO_TIMERF);
		SpellCardBonus=OriSCB*Timer/OriTimer;//SCB時間現象
		GrazeRate=1+GetGraze*0.01;
	//	SpellCardBonus*=GrazeRate;//SCBグレイズ倍率上昇
	//	SpellCardBonus=OriSCB+GetGraze*500;//SCBグレイズ倍率上昇
		SpellCardBonus=floor(SpellCardBonus);
		SetCommonData("SpellCardBonus",SpellCardBonus);
		str_SCB=IntToString(SpellCardBonus);
		}
		while(length(str_SCB)<8)
		{
		str_SCB=IntToString(0)~str_SCB;
		}
		MissCount=ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SHOOTDOWN_COUNT);
		BombCount=ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SPELL_COUNT);
		if(MissCount!=0 || BombCount!=0){SCB_GetFlag=0;}
		yield;
	}

	task DrawSCB
	{
		let text="Bonus";
		ascent(i in 0..length(text))
		{
			DrawLetter(280-144-8*length(text)+8*i,52-28,text[i]);
			loop(1){yield;}
		}

		ascent(i in 0..length(str_SCB))
		{
			DrawNum(360-144-8*length(str_SCB)+8*i,52-28,i);
			loop(1){yield;}
		}

		text="History";
		ascent(i in 0..length(text))
		{
			DrawLetter(284-8*length(text)+8*i,52-28,text[i]);
			loop(1){yield;}
		}

		let ChallengeArray=[];
		alternative(GetAreaCommonData("TUNING","PlayerCharacter",0))
		case(0){ChallengeArray=GetAreaCommonData("SPELL_RECORD","Reimu_Challenge",[]);}
		case(1){ChallengeArray=GetAreaCommonData("SPELL_RECORD","Marisa_Challenge",[]);}

		let GetArray=[];
		alternative(GetAreaCommonData("TUNING","PlayerCharacter",0))
		case(0){GetArray=GetAreaCommonData("SPELL_RECORD","Reimu_Get",[]);}
		case(1){GetArray=GetAreaCommonData("SPELL_RECORD","Marisa_Get",[]);}

		let str_gnum=IntToString(GetArray[GetCommonData("SpellNumber",0)]);
		ascent(i in 0..length(str_gnum))
		{
			DrawNum2(320-8*length(str_gnum)+8*i,52-28,str_gnum[i]);
			loop(1){yield;}
		}

		DrawLetter(324,52-28,"/");
		loop(1){yield;}

		let str_cnum=IntToString(ChallengeArray[GetCommonData("SpellNumber",0)]);
		ascent(i in 0..length(str_cnum))
		{
			DrawNum2(360-8*length(str_cnum)+8*i,52-28,str_cnum[i]);
			loop(1){yield;}
		}

		task DrawNum(x,y,number)
		{
			if(GetCommonData("SpecialStage",false)==true){x+=256}
			let obj = ObjText_Create();
			ObjText_SetText(obj, str_SCB[number]);
			ObjText_SetFontSize(obj, 12);
			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;
			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(typeEnd==0)
			{
				if(!SCB_GetFlag){break;}
				ObjText_SetText(obj, str_SCB[number]);
				if(GetPlayerY<=160)
				{		
				Alpha=(GetPlayerY/160)*255;
				}
				else{Alpha=255;}
				ObjRender_SetAlpha(obj,Alpha);
				yield;
			}
			Obj_Delete(obj);
			if(typeEnd==0 && number==0)
			{
			let text2="false";
			ascent(i in 0..length(text2))
			{
				DrawLetter(360-144-8*length(text2)+8*i,52-28,text2[i]);
			}
			}
		}

		task DrawNum2(x,y,number)
		{
			if(GetCommonData("SpecialStage",false)==true){x+=256}
			let obj = ObjText_Create();
			ObjText_SetText(obj, number);
			ObjText_SetFontSize(obj, 12);
			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;
			loop(15)
			{
				scale-=2/15;
				ObjRender_SetScaleXYZ(obj,scale,scale,scale);
				if(GetPlayerY<=128)
				{		
					Alpha=(GetPlayerY/128)*255;
				}
				else{Alpha=255;}
				ObjRender_SetAlpha(obj,Alpha);
				yield;
			}
			while(typeEnd==0)
			{
				if(GetPlayerY<=128)
				{		
					Alpha=(GetPlayerY/128)*255;
				}
				else{Alpha=255;}
				ObjRender_SetAlpha(obj,Alpha);
				yield;
			}
			Obj_Delete(obj);
		}

		task DrawLetter(x,y,letter)
		{
			if(GetCommonData("SpecialStage",false)==true){x+=256}
//			let color=[255,255,255];
//			color=SetSpellCardColor(Name);

			let obj = ObjText_Create();
			ObjText_SetText(obj,letter);
			ObjText_SetFontSize(obj, 12);
			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;
			loop(15)
			{
				scale-=2/15;
				ObjRender_SetScaleXYZ(obj,scale,scale,scale);
				if(GetPlayerY<=128)
				{		
					Alpha=(GetPlayerY/128)*255;
				}
				else{Alpha=255;}
				ObjRender_SetAlpha(obj,Alpha);
				yield;
			}
			while(typeEnd==0)
			{				
				if(GetPlayerY<=128)
				{		
					Alpha=(GetPlayerY/128)*255;
				}
				else{Alpha=255;}
				ObjRender_SetAlpha(obj,Alpha);
				yield;
			}
			Obj_Delete(obj);
		}
	}
}


task Special_BG
{
	let AdjustX=-8;
	let DrawDistance=0;
	let Size=16;
	let RenderPhase=0;
	ascent(i in 0..640/Size)
	{
	ascent(j in 0..480/Size)
	{
		BG_Effect(i,j);
	}
	}
	loop(100)
	{
		DrawDistance+=10;
		yield;
	}
	RenderPhase=1;
	BG_EffectAll;

	task BG_Effect(H,V)
	{
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"img/dpm193-vx.png");//オブジェクトとテスクチャを関連付け
	ObjRender_SetPosition(obj, H*Size+Size/2, V*Size+Size/2, 0);
	Obj_SetRenderPriority(obj, 0.25);
	ObjSprite2D_SetSourceRect(obj, H*Size+Size/2+AdjustX, V*Size+Size/2*0, (H+1)*Size+Size/2+AdjustX, (V+1)*Size+Size/2*0);
	ObjSprite2D_SetDestRect(obj, -Size/2, -Size/2, Size/2, Size/2);
	ObjRender_SetColor(obj, 150, 150, 150);

	let RenderDis=((ObjRender_GetY(obj)-ObjRender_GetY(GetEnemyBossObjectID()[0]))^2+(ObjRender_GetX(obj)-ObjRender_GetX(GetEnemyBossObjectID()[0]))^2)^0.5;
	ObjRender_SetAlpha(obj,0);

	while(RenderDis>DrawDistance){yield;}

	ascent(i in 0 .. 60)
	{
		let alpha = i/60;
		ObjRender_SetAlpha(obj, 255*alpha);
		ObjRender_SetScaleXYZ(obj, 2-alpha, 2-alpha, 2-alpha);
		yield;
	}
	ObjRender_SetAlpha(obj, 255);
	ObjRender_SetScaleXYZ(obj, 1, 1, 1);
	while(RenderPhase==0){yield;}
	Obj_Delete(obj);
	}

	task BG_EffectAll
	{
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"img/dpm193-vx.png");//オブジェクトとテスクチャを関連付け
	ObjRender_SetPosition(obj, 0, 0, 0);
	Obj_SetRenderPriority(obj, 0.25);
	ObjSprite2D_SetSourceRect(obj, 0+Size/2+AdjustX, 0+Size/2*0, 640+Size/2+AdjustX, 480+Size/2*0);
	ObjSprite2D_SetDestRect(obj, 0, 0, 640, 480);
	ObjRender_SetColor(obj, 150, 150, 150);
	ObjRender_SetAlpha(obj, 255);

	while(typeEnd==0)
	{
		yield;
	}
	if(typeEnd==END_FAILED)
	{
	descent(i in 0 .. 30)
	{
		let alpha = i/30;
		ObjRender_SetAlpha(obj, 255*alpha);
		yield;
	}
	Obj_Delete(obj);
	}
	else
	{
		DrawDistance=0;
		ascent(i in 0..640/Size)
		{
		ascent(j in 0..480/Size)
		{
			BG_Break_Effect(i,j);
		}
		}
		Obj_Delete(obj);
		loop(100)
		{
			DrawDistance+=10;
			yield;
		}
	}
	}

	task BG_Break_Effect(H,V)
	{
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"img/dpm193-vx.png");//オブジェクトとテスクチャを関連付け
	ObjRender_SetPosition(obj, H*Size+Size/2, V*Size+Size/2, 0);
	Obj_SetRenderPriority(obj, 0.25);
	ObjSprite2D_SetSourceRect(obj, H*Size+Size/2+AdjustX, V*Size+Size/2, (H+1)*Size+Size/2+AdjustX, (V+1)*Size+Size/2);
	ObjSprite2D_SetDestRect(obj, -Size/2, -Size/2, Size/2, Size/2);
	ObjRender_SetColor(obj, 150, 150, 150);

	let RenderDis=((ObjRender_GetY(obj)-BossPosition[1])^2+(ObjRender_GetX(obj)-BossPosition[0])^2)^0.5;
	let RenderAngle=atan2(ObjRender_GetY(obj)-BossPosition[1],ObjRender_GetX(obj)-BossPosition[0]);
	ObjRender_SetAlpha(obj,255);

	while(RenderDis>DrawDistance){yield;}

	let speed=rand(1,2);
	let angle=rand(0,360);
	angle=RenderAngle+rand(-15,15);
	let AngleX=rand(0,360);
	let AngleXPlus=rand(-10,10);
	let AngleY=rand(0,360);
	let AngleYPlus=rand(-10,10);
	let AngleZ=rand(0,360);
	let AngleZPlus=rand(-10,10);
	descent(i in 0 .. 60)
	{
		let alpha = i/60;
		ObjRender_SetAlpha(obj, 255*alpha);
		ObjRender_SetScaleXYZ(obj, alpha, alpha, alpha);
		ObjRender_SetPosition(obj,ObjRender_GetX(obj)+speed*cos(angle),ObjRender_GetY(obj)+speed*sin(angle),0);
		ObjRender_SetAngleXYZ(obj,AngleZ,AngleY,AngleZ);
		AngleX+=AngleXPlus;
		AngleY+=AngleYPlus;
		AngleZ+=AngleZPlus;
		yield;
	}
	Obj_Delete(obj);
	}
}

task Magic_BG
{
	let AdjustX=56;
	let DrawDistance=0;
	let Size=16;
	let RenderPhase=0;
	if(GetAreaCommonData("TUNING","Effect",1)==1)
	{
	ascent(i in 0..384/Size)
	{
	ascent(j in 0..448/Size)
	{
		BG_Effect(i,j);
	}
	}
	loop(100)
	{
		DrawDistance+=10;
		yield;
	}
	}
	RenderPhase=1;
	BG_EffectAll;

	task BG_Effect(H,V)
	{
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"img/dpm197-vx.png");//オブジェクトとテスクチャを関連付け
	ObjRender_SetPosition(obj, H*Size+Size/2, V*Size+Size/2, 0);
	Obj_SetRenderPriority(obj, 0.25);
	ObjSprite2D_SetSourceRect(obj, H*Size+Size/2+AdjustX, V*Size+Size/2, (H+1)*Size+Size/2+AdjustX, (V+1)*Size+Size/2);
	ObjSprite2D_SetDestRect(obj, -Size/2, -Size/2, Size/2, Size/2);
	ObjRender_SetColor(obj, 150, 150, 150);

	let RenderDis=((ObjRender_GetY(obj)-ObjRender_GetY(GetEnemyBossObjectID()[0]))^2+(ObjRender_GetX(obj)-ObjRender_GetX(GetEnemyBossObjectID()[0]))^2)^0.5;
	ObjRender_SetAlpha(obj,0);

	while(RenderDis>DrawDistance){yield;}

	ascent(i in 0 .. 60)
	{
		let alpha = i/60;
		ObjRender_SetAlpha(obj, 255*alpha);
		ObjRender_SetScaleXYZ(obj, 2-alpha, 2-alpha, 2-alpha);
		yield;
	}
	ObjRender_SetAlpha(obj, 255);
	ObjRender_SetScaleXYZ(obj, 1, 1, 1);
	while(RenderPhase==0){yield;}
	Obj_Delete(obj);
	}

	task BG_EffectAll
	{
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"img/dpm197-vx.png");//オブジェクトとテスクチャを関連付け
	ObjRender_SetPosition(obj, 0, 0, 0);
	Obj_SetRenderPriority(obj, 0.25);
	ObjSprite2D_SetSourceRect(obj, 0+Size/2+AdjustX, 0+Size/2, 384+Size/2+AdjustX, 448+Size/2);
	ObjSprite2D_SetDestRect(obj, 0, 0, 384, 448);
	ObjRender_SetColor(obj, 150, 150, 150);
	ObjRender_SetAlpha(obj, 255);
	if(GetAreaCommonData("TUNING","Effect",1)==0)
	{
	ascent(i in 0 .. 60)
	{
		let alpha = i/60;
		ObjRender_SetAlpha(obj, 255*alpha);
		yield;
	}
	}
	while(typeEnd==0)
	{
		yield;
	}
	if(typeEnd==END_FAILED || GetAreaCommonData("TUNING","Effect",1)==0)
	{
	descent(i in 0 .. 30)
	{
		let alpha = i/30;
		ObjRender_SetAlpha(obj, 255*alpha);
		yield;
	}
	Obj_Delete(obj);
	}
	else
	{
		DrawDistance=0;
		ascent(i in 0..384/Size)
		{
		ascent(j in 0..448/Size)
		{
			BG_Break_Effect(i,j);
		}
		}
		Obj_Delete(obj);
		loop(100)
		{
			DrawDistance+=10;
			yield;
		}
	}
	}

	task BG_Break_Effect(H,V)
	{
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"img/dpm197-vx.png");//オブジェクトとテスクチャを関連付け
	ObjRender_SetPosition(obj, H*Size+Size/2, V*Size+Size/2, 0);
	Obj_SetRenderPriority(obj, 0.25);
	ObjSprite2D_SetSourceRect(obj, H*Size+Size/2+AdjustX, V*Size+Size/2, (H+1)*Size+Size/2+AdjustX, (V+1)*Size+Size/2);
	ObjSprite2D_SetDestRect(obj, -Size/2, -Size/2, Size/2, Size/2);
	ObjRender_SetColor(obj, 150, 150, 150);

	let RenderDis=((ObjRender_GetY(obj)-BossPosition[1])^2+(ObjRender_GetX(obj)-BossPosition[0])^2)^0.5;
	let RenderAngle=atan2(ObjRender_GetY(obj)-BossPosition[1],ObjRender_GetX(obj)-BossPosition[0]);
	ObjRender_SetAlpha(obj,255);

	while(RenderDis>DrawDistance){yield;}

	let speed=rand(1,2);
	let angle=rand(0,360);
	angle=RenderAngle+rand(-15,15);
	let AngleX=rand(0,360);
	let AngleXPlus=rand(-10,10);
	let AngleY=rand(0,360);
	let AngleYPlus=rand(-10,10);
	let AngleZ=rand(0,360);
	let AngleZPlus=rand(-10,10);
	descent(i in 0 .. 60)
	{
		let alpha = i/60;
		ObjRender_SetAlpha(obj, 255*alpha);
		ObjRender_SetScaleXYZ(obj, alpha, alpha, alpha);
		ObjRender_SetPosition(obj,ObjRender_GetX(obj)+speed*cos(angle),ObjRender_GetY(obj)+speed*sin(angle),0);
		ObjRender_SetAngleXYZ(obj,AngleZ,AngleY,AngleZ);
		AngleX+=AngleXPlus;
		AngleY+=AngleYPlus;
		AngleZ+=AngleZPlus;
		yield;
	}
	Obj_Delete(obj);
	}
}

task Patchouli_BG
{
	let AdjustX=56;
	let DrawDistance=0;
	let Size=16;
	let RenderPhase=0;
	if(GetAreaCommonData("TUNING","Effect",1)==1)
	{
	ascent(i in 0..384/Size)
	{
	ascent(j in 0..448/Size)
	{
		BG_Effect(i,j);
	}
	}
	loop(100)
	{
		DrawDistance+=10;
		yield;
	}
	}
	RenderPhase=1;
	BG_EffectAll;

	task BG_Effect(H,V)
	{
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"img/dpm187-vx.png");//オブジェクトとテスクチャを関連付け
	ObjRender_SetPosition(obj, H*Size+Size/2, V*Size+Size/2, 0);
	Obj_SetRenderPriority(obj, 0.25);
	ObjSprite2D_SetSourceRect(obj, H*Size+Size/2+AdjustX, V*Size+Size/2, (H+1)*Size+Size/2+AdjustX, (V+1)*Size+Size/2);
	ObjSprite2D_SetDestRect(obj, -Size/2, -Size/2, Size/2, Size/2);
	ObjRender_SetColor(obj, 150, 150, 150);

	let RenderDis=((ObjRender_GetY(obj)-ObjRender_GetY(GetEnemyBossObjectID()[0]))^2+(ObjRender_GetX(obj)-ObjRender_GetX(GetEnemyBossObjectID()[0]))^2)^0.5;
	ObjRender_SetAlpha(obj,0);

	while(RenderDis>DrawDistance){yield;}

	ascent(i in 0 .. 60)
	{
		let alpha = i/60;
		ObjRender_SetAlpha(obj, 255*alpha);
		ObjRender_SetScaleXYZ(obj, 2-alpha, 2-alpha, 2-alpha);
		yield;
	}
	ObjRender_SetAlpha(obj, 255);
	ObjRender_SetScaleXYZ(obj, 1, 1, 1);
	while(RenderPhase==0){yield;}
	Obj_Delete(obj);
	}

	task BG_EffectAll
	{
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"img/dpm187-vx.png");//オブジェクトとテスクチャを関連付け
	ObjRender_SetPosition(obj, 0, 0, 0);
	Obj_SetRenderPriority(obj, 0.25);
	ObjSprite2D_SetSourceRect(obj, 0+Size/2+AdjustX, 0+Size/2, 384+Size/2+AdjustX, 448+Size/2);
	ObjSprite2D_SetDestRect(obj, 0, 0, 384, 448);
	ObjRender_SetColor(obj, 150, 150, 150);
	ObjRender_SetAlpha(obj, 255);
	if(GetAreaCommonData("TUNING","Effect",1)==0)
	{
	ascent(i in 0 .. 60)
	{
		let alpha = i/60;
		ObjRender_SetAlpha(obj, 255*alpha);
		yield;
	}
	}
	while(typeEnd==0)
	{
		yield;
	}
	if(typeEnd==END_FAILED || GetAreaCommonData("TUNING","Effect",1)==0)
	{
	descent(i in 0 .. 30)
	{
		let alpha = i/30;
		ObjRender_SetAlpha(obj, 255*alpha);
		yield;
	}
	Obj_Delete(obj);
	}
	else
	{
		DrawDistance=0;
		ascent(i in 0..384/Size)
		{
		ascent(j in 0..448/Size)
		{
			BG_Break_Effect(i,j);
		}
		}
		Obj_Delete(obj);
		loop(100)
		{
			DrawDistance+=10;
			yield;
		}
	}
	}

	task BG_Break_Effect(H,V)
	{
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"img/dpm187-vx.png");//オブジェクトとテスクチャを関連付け
	ObjRender_SetPosition(obj, H*Size+Size/2, V*Size+Size/2, 0);
	Obj_SetRenderPriority(obj, 0.25);
	ObjSprite2D_SetSourceRect(obj, H*Size+Size/2+AdjustX, V*Size+Size/2, (H+1)*Size+Size/2+AdjustX, (V+1)*Size+Size/2);
	ObjSprite2D_SetDestRect(obj, -Size/2, -Size/2, Size/2, Size/2);
	ObjRender_SetColor(obj, 150, 150, 150);

	let RenderDis=((ObjRender_GetY(obj)-BossPosition[1])^2+(ObjRender_GetX(obj)-BossPosition[0])^2)^0.5;
	let RenderAngle=atan2(ObjRender_GetY(obj)-BossPosition[1],ObjRender_GetX(obj)-BossPosition[0]);
	ObjRender_SetAlpha(obj,255);

	while(RenderDis>DrawDistance){yield;}

	let speed=rand(1,2);
	let angle=rand(0,360);
	angle=RenderAngle+rand(-15,15);
	let AngleX=rand(0,360);
	let AngleXPlus=rand(-10,10);
	let AngleY=rand(0,360);
	let AngleYPlus=rand(-10,10);
	let AngleZ=rand(0,360);
	let AngleZPlus=rand(-10,10);
	descent(i in 0 .. 60)
	{
		let alpha = i/60;
		ObjRender_SetAlpha(obj, 255*alpha);
		ObjRender_SetScaleXYZ(obj, alpha, alpha, alpha);
		ObjRender_SetPosition(obj,ObjRender_GetX(obj)+speed*cos(angle),ObjRender_GetY(obj)+speed*sin(angle),0);
		ObjRender_SetAngleXYZ(obj,AngleZ,AngleY,AngleZ);
		AngleX+=AngleXPlus;
		AngleY+=AngleYPlus;
		AngleZ+=AngleZPlus;
		yield;
	}
	Obj_Delete(obj);
	}
}

task Flandre_BG
{
	let DrawDistance=0;
	let Size=16;
	let RenderPhase=0;

	if(GetAreaCommonData("TUNING","Effect",1)==1)
	{
	ascent(i in 0..384/Size)
	{
	ascent(j in 0..448/Size)
	{
		BG_Effect(i,j);
	}
	}
	loop(100)
	{
		DrawDistance+=10;
		yield;
	}
	}
	RenderPhase=1;
	BG_EffectAll;

	task BG_Effect(H,V)
	{
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"img/dpm232-vx.png");//オブジェクトとテスクチャを関連付け
	ObjRender_SetPosition(obj, H*Size+Size/2, V*Size+Size/2, 0);
	Obj_SetRenderPriority(obj, 0.25);
	ObjSprite2D_SetSourceRect(obj, H*Size+Size/2, V*Size+Size/2, (H+1)*Size+Size/2, (V+1)*Size+Size/2);
	ObjSprite2D_SetDestRect(obj, -Size/2, -Size/2, Size/2, Size/2);
	ObjRender_SetColor(obj, 150, 150, 150);

	let RenderDis=((ObjRender_GetY(obj)-ObjRender_GetY(GetEnemyBossObjectID()[0]))^2+(ObjRender_GetX(obj)-ObjRender_GetX(GetEnemyBossObjectID()[0]))^2)^0.5;
	ObjRender_SetAlpha(obj,0);

	while(RenderDis>DrawDistance){yield;}

	ascent(i in 0 .. 60)
	{
		let alpha = i/60;
		ObjRender_SetAlpha(obj, 255*alpha);
		ObjRender_SetScaleXYZ(obj, 2-alpha, 2-alpha, 2-alpha);
		yield;
	}
	ObjRender_SetAlpha(obj, 255);
	ObjRender_SetScaleXYZ(obj, 1, 1, 1);
	while(RenderPhase==0){yield;}
	Obj_Delete(obj);
	}

	task BG_EffectAll
	{
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"img/dpm232-vx.png");//オブジェクトとテスクチャを関連付け
	ObjRender_SetPosition(obj, 0, 0, 0);
	Obj_SetRenderPriority(obj, 0.25);
	ObjSprite2D_SetSourceRect(obj, 0+Size/2, 0+Size/2, 384+Size/2, 448+Size/2);
	ObjSprite2D_SetDestRect(obj, 0, 0, 384, 448);
	ObjRender_SetColor(obj, 150, 150, 150);
	ObjRender_SetAlpha(obj, 255);
	if(GetAreaCommonData("TUNING","Effect",1)==0)
	{
	ascent(i in 0 .. 60)
	{
		let alpha = i/60;
		ObjRender_SetAlpha(obj, 255*alpha);
		yield;
	}
	}
	while(typeEnd==0)
	{
		yield;
	}
	if(typeEnd==END_FAILED || GetAreaCommonData("TUNING","Effect",1)==0)
	{
	descent(i in 0 .. 30)
	{
		let alpha = i/30;
		ObjRender_SetAlpha(obj, 255*alpha);
		yield;
	}
	Obj_Delete(obj);
	}
	else
	{
		DrawDistance=0;
		ascent(i in 0..384/Size)
		{
		ascent(j in 0..448/Size)
		{
			BG_Break_Effect(i,j);
		}
		}
		Obj_Delete(obj);
		loop(100)
		{
			DrawDistance+=10;
			yield;
		}
	}
	}

	task BG_Break_Effect(H,V)
	{
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"img/dpm232-vx.png");//オブジェクトとテスクチャを関連付け
	ObjRender_SetPosition(obj, H*Size+Size/2, V*Size+Size/2, 0);
	Obj_SetRenderPriority(obj, 0.25);
	ObjSprite2D_SetSourceRect(obj, H*Size+Size/2, V*Size+Size/2, (H+1)*Size+Size/2, (V+1)*Size+Size/2);
	ObjSprite2D_SetDestRect(obj, -Size/2, -Size/2, Size/2, Size/2);
	ObjRender_SetColor(obj, 150, 150, 150);

	let RenderDis=((ObjRender_GetY(obj)-BossPosition[1])^2+(ObjRender_GetX(obj)-BossPosition[0])^2)^0.5;
	let RenderAngle=atan2(ObjRender_GetY(obj)-BossPosition[1],ObjRender_GetX(obj)-BossPosition[0]);
	ObjRender_SetAlpha(obj,255);

	while(RenderDis>DrawDistance){yield;}

	let speed=rand(1,2);
	let angle=rand(0,360);
	angle=RenderAngle+rand(-15,15);
	let AngleX=rand(0,360);
	let AngleXPlus=rand(-10,10);
	let AngleY=rand(0,360);
	let AngleYPlus=rand(-10,10);
	let AngleZ=rand(0,360);
	let AngleZPlus=rand(-10,10);
	descent(i in 0 .. 60)
	{
		let alpha = i/60;
		ObjRender_SetAlpha(obj, 255*alpha);
		ObjRender_SetScaleXYZ(obj, alpha, alpha, alpha);
		ObjRender_SetPosition(obj,ObjRender_GetX(obj)+speed*cos(angle),ObjRender_GetY(obj)+speed*sin(angle),0);
		ObjRender_SetAngleXYZ(obj,AngleZ,AngleY,AngleZ);
		AngleX+=AngleXPlus;
		AngleY+=AngleYPlus;
		AngleZ+=AngleZPlus;
		yield;
	}
	Obj_Delete(obj);
	}
}

task BG_Flandre
{
	let Size=16;
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"img/dpm232-vx.png");//オブジェクトとテスクチャを関連付け
	ObjRender_SetPosition(obj, 0, 0, 0);
	Obj_SetRenderPriority(obj, 0.25);
	ObjSprite2D_SetSourceRect(obj, 0+Size/2, 0+Size/2, 384+Size/2, 448+Size/2);
	ObjSprite2D_SetDestRect(obj, 0, 0, 384, 448);
	ObjRender_SetColor(obj, 150, 150, 150);
	ObjRender_SetAlpha(obj, 255);
	descent(i in 0 .. 60)
	{
		let alpha = i/60;
		ObjRender_SetAlpha(obj, 255*alpha);
		yield;
	}
	Obj_Delete(obj);
}

task MagicCircle()
{
	let AX=rand(0,360);
	let AXP=rand(1.5,2.5)*(floor(rand(0,1.999))*2-1);
	let AY=rand(0,360);
	let AYP=rand(1.5,2.5)*(floor(rand(0,1.999))*2-1);
	let AZ=rand(0,360);
	let AZP=3;
	let AX2=rand(0,360);
	let AXP2=rand(1.5,2.5)*(floor(rand(0,1.999))*2-1);
	let AY2=rand(0,360);
	let AYP2=rand(1.5,2.5)*(floor(rand(0,1.999))*2-1);
	let AZ2=rand(0,360);
	let AZP2=3;

	if(GetAreaCommonData("TUNING","Effect",1)==1)
	{
	MC(AX,AXP,AY,AYP,AZ,AZP);
	MC(AX2,AXP2,AY2,AYP2,AZ2,AZP2);
	}

	task MC(AngleX,AngleXPlus,AngleY,AngleYPlus,AngleZ,AngleZPlus)
	{
	let ex;
	let ey;
	let objBoss = GetEnemyBossObjectID()[0];
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
	ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"img/Default_MagicCircle.png");//オブジェクトとテスクチャを関連付け
	ObjRender_SetPosition(obj, 0, 0, 0);
	Obj_SetRenderPriority(obj, 0.31);
	ObjSprite2D_SetSourceRect(obj, 0, 0,256, 256);
	ObjSprite2D_SetDestRect(obj, -128, -128, 128, 128);
//	ObjRender_SetColor(obj, 150, 150, 150);
	ObjRender_SetAlpha(obj, 255);

	let AlphaRate=1;
	ascent(i in 0 .. 60)
	{
		ex=ObjRender_GetX(objBoss);
		ey=ObjRender_GetY(objBoss);
		ObjRender_SetPosition(obj, ex, ey, 0);
		let alpha = i/120*AlphaRate;
		ObjRender_SetColor(obj, 255*alpha, 255*alpha, 255*alpha);
		ObjRender_SetScaleXYZ(obj, 2*alpha, 2*alpha, 2*alpha);
		ObjRender_SetAngleXYZ(obj,AngleX,AngleY,AngleZ);
		MCBlur(ex,ey,alpha*AlphaRate,AngleX,AngleY,AngleZ);
		AngleX+=AngleXPlus;
		AngleY+=AngleYPlus;
		AngleZ+=AngleZPlus;
		if(length(GetEnemyBossObjectID())>0)
		{
		let Dis=((ObjRender_GetY(GetEnemyBossObjectID()[0])-GetPlayerY)^2+(ObjRender_GetX(GetEnemyBossObjectID()[0])-GetPlayerX)^2)^0.5;
		if(Dis<150)
		{
			AlphaRate=0.1+0.9*Dis/150;
		}
		else
		{
			AlphaRate=1;
		}
		}
		yield;
	}
	while(typeEnd == 0)
	{
		if(length(GetEnemyBossObjectID())>0)
		{
		ex=ObjRender_GetX(GetEnemyBossObjectID()[0]);
		ey=ObjRender_GetY(GetEnemyBossObjectID()[0]);
		}
		ObjRender_SetPosition(obj, ex, ey, 0);
		let alpha = 0.5*AlphaRate;
		ObjRender_SetColor(obj, 255*alpha, 255*alpha, 255*alpha);
		ObjRender_SetAngleXYZ(obj,AngleX,AngleY,AngleZ);
		MCBlur(ex,ey,AlphaRate,AngleX,AngleY,AngleZ);
		AngleX+=AngleXPlus;
		AngleY+=AngleYPlus;
		AngleZ+=AngleZPlus;
		if(length(GetEnemyBossObjectID())>0)
		{
		let Dis=((ObjRender_GetY(GetEnemyBossObjectID()[0])-GetPlayerY)^2+(ObjRender_GetX(GetEnemyBossObjectID()[0])-GetPlayerX)^2)^0.5;
		if(Dis<150)
		{
			AlphaRate=0.1+0.9*Dis/150;
		}
		else
		{
			AlphaRate=1;
		}
		}
		yield;
	}
	if(typeEnd == END_FAILED)
	{
	descent(i in 0 .. 60)
	{
		if(length(GetEnemyBossObjectID())>0)
		{
		ex=ObjRender_GetX(GetEnemyBossObjectID()[0]);
		ey=ObjRender_GetY(GetEnemyBossObjectID()[0]);
		}
		ObjRender_SetPosition(obj, ex, ey, 0);
		let alpha = i/120;
		ObjRender_SetColor(obj, 255*alpha, 255*alpha, 255*alpha);
		ObjRender_SetAngleXYZ(obj,AngleX,AngleY,AngleZ);
		MCBlur(ex,ey,alpha,AngleX,AngleY,AngleZ);
		AngleX+=AngleXPlus;
		AngleY+=AngleYPlus;
		AngleZ+=AngleZPlus;
		yield;
	}

		Obj_Delete(obj);
		loop(60){yield;}
		CloseScript(GetOwnScriptID());
		return;
	}
	else if(typeEnd == END_SUCCESS)
	{
	descent(i in 0 .. 30)
	{
		if(length(GetEnemyBossObjectID())>0)
		{
		ex=ObjRender_GetX(GetEnemyBossObjectID()[0]);
		ey=ObjRender_GetY(GetEnemyBossObjectID()[0]);
		}
		ObjRender_SetPosition(obj, ex, ey, 0);
		let alpha = i/60;
		ObjRender_SetColor(obj, 255*alpha, 255*alpha, 255*alpha);
		ObjRender_SetScaleXYZ(obj, 4*(0.5-alpha), 4*(0.5-alpha), 4*(0.5-alpha));
		ObjRender_SetAngleXYZ(obj,AngleX,AngleY,AngleZ);
		MCBlur(ex,ey,alpha,AngleX,AngleY,AngleZ);
		AngleX+=AngleXPlus;
		AngleY+=AngleYPlus;
		AngleZ+=AngleZPlus;
		yield;
	}
	ascent(i in 0 .. 30)
	{
		ex=GetPlayerX;
		ey=GetPlayerY;
		ObjRender_SetPosition(obj, ex, ey, 0);
		let alpha = i/60;
		ObjRender_SetColor(obj, 255*alpha, 255*alpha, 255*alpha);
		ObjRender_SetScaleXYZ(obj, 4*(0.5-alpha), 4*(0.5-alpha), 4*(0.5-alpha));
		ObjRender_SetAngleXYZ(obj,AngleX,AngleY,AngleZ);
		MCBlur(ex,ey,alpha,AngleX,AngleY,AngleZ);
		AngleX+=AngleXPlus;
		AngleY+=AngleYPlus;
		AngleZ+=AngleZPlus;
		yield;
	}
		Obj_Delete(obj);
		loop(60){yield;}
		CloseScript(GetOwnScriptID());
		return;
	}

	Obj_Delete(obj);
	}

	task MCBlur(ex,ey,alpha,AngleX,AngleY,AngleZ)
	{
	let alphaMax=alpha/8;
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
	ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"img/Default_MagicCircle.png");//オブジェクトとテスクチャを関連付け
	ObjRender_SetPosition(obj, 0, 0, 0);
	Obj_SetRenderPriority(obj, 0.31);
	ObjSprite2D_SetSourceRect(obj, 0, 0,256, 256);
	ObjSprite2D_SetDestRect(obj, -128, -128, 128, 128);
//	ObjRender_SetColor(obj, 150, 150, 150);
	ObjRender_SetAlpha(obj, 255);
	ObjRender_SetScaleXYZ(obj, 1*alpha, 1*alpha, 1*alpha);
	descent(i in 0 .. 15)
	{
		ObjRender_SetPosition(obj, ex, ey, 0);
		let alpha = alphaMax*i/15;
		ObjRender_SetColor(obj, 255*alpha, 255*alpha, 255*alpha);
		ObjRender_SetAngleXYZ(obj,AngleX,AngleY,AngleZ);
		yield;
	}
	Obj_Delete(obj);
	}
}
