task Invincible
{
/*
loop
{
	if(GetTimeOfPlayerInvincibility>0)
	{
		InvincibleEffect(1);
		InvincibleEffect(2);
		while(GetTimeOfPlayerInvincibility>0){yield;}
	}
yield;
}
*/
}

task InvincibleEffect(num)
{
	let Trianglenum=4;

	let obj = Obj_Create(OBJ_EFFECT);
	ObjEffect_SetTexture(obj, imgWhite);
	ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLELIST);
	ObjEffect_CreateVertex(obj, Trianglenum*3);
	ObjEffect_SetRenderState(obj,ADD);
	ObjEffect_SetLayer(obj,3);

	let size=30;
	let dis=300;
	let rotq=MakeRotate(0.877*3,0.488*3,1.55*3);

	let PosV=[];
	loop(Trianglenum)
	{
		PosV=PosV~[[]];
	}

	if(num==1)
	{
		PosV[0]=[size,size,size];
		PosV[1]=[size,-size,-size];
		PosV[2]=[-size,size,-size];
		PosV[3]=[-size,-size,size];
	}
	if(num==2)
	{
		PosV[0]=[-size,-size,-size];
		PosV[1]=[-size,size,size];
		PosV[2]=[size,-size,size];
		PosV[3]=[size,size,-size];
	}

	let pos=[];
	loop(Trianglenum)
	{
		pos=pos~[[]];
	}

	ascent(let i in 0..Trianglenum)
	{
		pos[i]=[0,PosV[i][0],PosV[i][1],PosV[i][2]];
	}

	let expand=60;
	if(expand<1){expand=1;}	//ゼロ割回避
	let expspd=1/expand;
	expand=0;	//現在の展開倍率として再利用

	ascent(let i in 0..Trianglenum)
	{
	ObjEffect_SetVertexXY(obj, 3*i, Perspective(pos[i][1],pos[i][3],dis)*expand, Perspective(pos[i][2],pos[i][3],dis)*expand);
	ObjEffect_SetVertexXY(obj, 3*i+1, Perspective(pos[(i+1)%Trianglenum][1],pos[(i+1)%Trianglenum][3],dis)*expand, Perspective(pos[(i+1)%Trianglenum][2],pos[(i+1)%Trianglenum][3],dis)*expand);
	ObjEffect_SetVertexXY(obj, 3*i+2, Perspective(pos[(i+2)%Trianglenum][1],pos[(i+2)%Trianglenum][3],dis)*expand, Perspective(pos[(i+2)%Trianglenum][2],pos[(i+2)%Trianglenum][3],dis)*expand);
	}

	ascent(let i in 0..Trianglenum)
	{
	ObjEffect_SetVertexUV(obj, 3*i, 96, 1);
	ObjEffect_SetVertexUV(obj, 3*i+1, 142, 1);
	ObjEffect_SetVertexUV(obj, 3*i+2, 142, 47);
	}

	Obj_SetPosition(obj,GetPlayerX, GetPlayerY);

	let Alpha=50;
	let R=96;
	let G=96;
	let B=255;

	ascent(i in 0..Trianglenum*3){ObjEffect_SetVertexColor(obj, i, Alpha,R,G,B);}

	while(GetTimeOfPlayerInvincibility>0 || OnMissed)
	{
		ascent(let i in 0..Trianglenum)
		{
			pos[i]=Rotate3D_B(pos[i],rotq);
		}

		ascent(let i in 0..Trianglenum)
		{
			ObjEffect_SetVertexXY(obj, 3*i, Perspective(pos[i][1],pos[i][3],dis)*expand, Perspective(pos[i][2],pos[i][3],dis)*expand);
			ObjEffect_SetVertexXY(obj, 3*i+1, Perspective(pos[(i+1)%Trianglenum][1],pos[(i+1)%Trianglenum][3],dis)*expand, Perspective(pos[(i+1)%Trianglenum][2],pos[(i+1)%Trianglenum][3],dis)*expand);
			ObjEffect_SetVertexXY(obj, 3*i+2, Perspective(pos[(i+2)%Trianglenum][1],pos[(i+2)%Trianglenum][3],dis)*expand, Perspective(pos[(i+2)%Trianglenum][2],pos[(i+2)%Trianglenum][3],dis)*expand);
		}

		Obj_SetPosition(obj,GetPlayerX,GetPlayerY);
		DeleteEnemyShotImmediatelyInCircle(SHOT,GetPlayerX,GetPlayerY,expand*60);

		if(GetTimeOfPlayerInvincibility>60)
		{
			if(expand<1){expand+=expspd;}	
		}
		else
		{
			expand-=expspd;
		}
		if(OnMissed)
		{
			ascent(i in 0..Trianglenum*3){ObjEffect_SetVertexColor(obj, i,0,R,G,B);}
		}
		else
		{
			ascent(i in 0..Trianglenum*3){ObjEffect_SetVertexColor(obj, i, Alpha,R,G,B);}
		}
		yield;
	}
	Obj_Delete(obj);
}

task PlayerRect
{
	loop
	{
		if(GetKeyState(VK_LEFT)==KEY_PUSH || GetKeyState(VK_LEFT)==KEY_HOLD)
		{
			if(GetCommonDataDefault("MoveReverse",false)==true && OnEnemySpell)
			{
			//	SetGraphicRect(10, 101, 38, 142);
			}
			else
			{
				PlayerRectLeft=0;
				PlayerRectUp=48;
				PlayerRectRight=48;
				PlayerRectDown=96;
			}

		}
		else if(GetKeyState(VK_RIGHT)==KEY_PUSH || GetKeyState(VK_RIGHT)==KEY_HOLD)
		{
			if(GetCommonDataDefault("MoveReverse",false)==true && OnEnemySpell)
			{
			//	SetGraphicRect(10, 53, 38, 94);
			}
			else
			{
				PlayerRectLeft=0;
				PlayerRectUp=96;
				PlayerRectRight=48;
				PlayerRectDown=144;
			}
		}
		else
		{
				PlayerRectLeft=0;
				PlayerRectUp=0;
				PlayerRectRight=48;
				PlayerRectDown=48;
		}
	yield;
	}
}

task Shadow
{
/*
let count=0;
loop
{
	if(!OnMissed && count>=2)
	{
	PlayerShadow;
	ascent(let i in 0..4)
	{
		OptionShadow(i);
	}
	count=0;
	}
	count+=1;
	yield;
}
*/
}

task PlayerShadow//プレイヤーの残像エフェクト
{
		function randrect
		{
			return(rand_int(-3,3));
		}
  		let obj = Obj_Create(OBJ_EFFECT);
		Obj_SetPosition(obj,GetPlayerX,GetPlayerY);
		ObjEffect_SetTexture(obj, img);
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjEffect_SetRenderState(obj, ADD);
		ObjEffect_CreateVertex(obj, 4);
		ObjEffect_SetVertexXY(obj, 0, -28+randrect, -46+randrect);
		ObjEffect_SetVertexXY(obj, 1,  28+randrect, -46+randrect);
		ObjEffect_SetVertexXY(obj, 2,  28+randrect,  46+randrect);
		ObjEffect_SetVertexXY(obj, 3, -28+randrect,  46+randrect);
		
		ObjEffect_SetVertexUV(obj, 0,  PlayerRectLeft,  PlayerRectUp);
		ObjEffect_SetVertexUV(obj, 1,  PlayerRectRight, PlayerRectUp);
		ObjEffect_SetVertexUV(obj, 2, PlayerRectRight,  PlayerRectDown);
		ObjEffect_SetVertexUV(obj, 3, PlayerRectLeft, PlayerRectDown);

		ascent(let i in 0..4)
		{
		ObjEffect_SetVertexColor(obj,i,100,127,127,255);
		}
		
		ObjEffect_SetLayer(obj, 2);
		Obj_SetAngle(obj,rand(0,360));
		Obj_SetSpeed(obj,0.02);
		let Alpha=120;
		let ScaleX=0.5;
		let ScaleY=0.5;
		let DisScale=0.015;
		if(BurstMode==1)
		{
			ScaleX=0.75;
			ScaleY=0.75;
			DisScale=0.0225;
		}
		let ady=0;
		loop(5)
		{
		if(OnMissed){Obj_Delete(obj); break;}
		Obj_SetPosition(obj,Obj_GetX(obj),Obj_GetY(obj)+ady);
			ascent(let i in 0..4)
			{
				if(Power<1000)
				{
				ObjEffect_SetVertexColor(obj,i,0,127,127,255);
				}
				if(Power>=1000 && Power<2000 || BurstLevel==1)
				{
				ObjEffect_SetVertexColor(obj,i,Alpha,127,127,255);
				}
				if(Power>=2000 && Power<3000 || BurstLevel==2)
				{
				ObjEffect_SetVertexColor(obj,i,Alpha,255,127,127);
				}
				if(Power>=3000 || BurstLevel==3)
				{
				ObjEffect_SetVertexColor(obj,i,Alpha,ColorRed,ColorGreen,ColorBlue);
				}
			}
			ObjEffect_SetScale(obj,ScaleX,ScaleY);
			ScaleX+=DisScale;
			ScaleY-=DisScale;
			Alpha-=7.5*2;
			ady+=1.5;
			yield;
		}
		
		Obj_Delete(obj);
}

task OptionShadow(num)//オプションの残像エフェクト
{
		function randrect
		{
			return(rand_int(-1,1));
		}

  		let obj = Obj_Create(OBJ_EFFECT);
		Obj_SetPosition(obj,opX[num],opY[num]);
		ObjEffect_SetTexture(obj, img);
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjEffect_SetRenderState(obj, ADD);
		ObjEffect_CreateVertex(obj, 4);

		ObjEffect_SetVertexXY(obj, 0, -7+randrect, -7+randrect);
		ObjEffect_SetVertexXY(obj, 1,  7+randrect, -7+randrect);
		ObjEffect_SetVertexXY(obj, 2,  7+randrect,  7+randrect);
		ObjEffect_SetVertexXY(obj, 3, -7+randrect,  7+randrect);
		
		ObjEffect_SetVertexUV(obj, 0,  3,  162);
		ObjEffect_SetVertexUV(obj, 1,  17, 162);
		ObjEffect_SetVertexUV(obj, 2, 17,  176);
		ObjEffect_SetVertexUV(obj, 3, 3, 176);

		ascent(let i in 0..4)
		{
		ObjEffect_SetVertexColor(obj,i,100,127,127,255);
		}
		
		ObjEffect_SetLayer(obj, 2);
		Obj_SetAngle(obj,rand(0,360));
		Obj_SetSpeed(obj,0.02);
		let Alpha=60;
		let ScaleX=1;
		let ScaleY=1;
		let DisScale=0.03;
		if(Power>=3000)
		{
			ScaleX=1.25;
			ScaleY=1.25;
			DisScale=0.0375;
		}
		if(BurstMode==1)
		{
			ScaleX=1.5;
			ScaleY=1.5;
			DisScale=0.045;
		}
		let ady=0;
		loop(5)
		{
		if(OnMissed){Obj_Delete(obj); break;}
		Obj_SetPosition(obj,Obj_GetX(obj),Obj_GetY(obj)+ady);
			ascent(let i in 0..4)
			{
				if(Power<1000)
				{
				ObjEffect_SetVertexColor(obj,i,0,127,127,255);
				}
				if(Power>=1000 && Power<2000 || BurstLevel==1)
				{
				ObjEffect_SetVertexColor(obj,i,Alpha,127,127,255);
				}
				if(Power>=2000 && Power<3000 || BurstLevel==2)
				{
				ObjEffect_SetVertexColor(obj,i,Alpha,255,127,127);
				}
				if(Power>=3000 || BurstLevel==3)
				{
				ObjEffect_SetVertexColor(obj,i,Alpha,ColorRed,ColorGreen,ColorBlue);
				}
			}
			ObjEffect_SetScale(obj,ScaleX,ScaleY);
			ScaleX+=0.03;
			ScaleY-=0.03;
			Alpha-=7.5;
			ady+=1.5;
			yield;
		}
		Obj_Delete(obj);
}