//共通エフェクト（グレイズ、低速魔方陣、被弾（弾消しダメージ衝撃波含む））

task GrazeEffect(BulletArray)
{//グレイズエフェクト
	ascent(i in 0..length(BulletArray))
	{
		if(!Obj_IsDeleted(BulletArray[i]))
		{
		Effect(BulletArray[i]);
		}
	}

	task Effect(Bobj)
	{
		let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		ObjPrim_SetTexture(obj,IMG_WHITECIRCLE_S);
		ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjPrim_SetVertexCount(obj, 4);
		ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
		Obj_SetRenderPriorityI(obj,30);
		ObjRender_SetAngleZ(obj,0);

		ObjPrim_SetDestCenter(obj,0,0,128,128);

		let x=(GetPlayerX+ObjRender_GetX(Bobj))/2;
		let y=(GetPlayerY+ObjRender_GetY(Bobj))/2;
		ObjRender_SetPosition(obj,x,y,0);

		let Alpha=0.75;
		let R=255;
		let G=255;
		let B=255;
		let speed=rand(2,6);
		let angle=rand(0,360);
		let Scale=0.1;
		let ScaleMax=Scale;
		loop(10)
		{
			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-=1.0/8;
			Scale-=ScaleMax/10;
			yield;
		}
		Obj_Delete(obj);
	}
}
task Barrier(X,Y)
{
	PlaySE(SE_BARRIER);
	let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetTexture(obj,IMG_BARRIER);
	ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
	ObjPrim_SetVertexCount(obj, 4);
	Obj_SetRenderPriorityI(obj,44);
	//ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	ObjRender_SetAngleZ(obj,rand(0,360));
	let scale =0;
	let Alpha = 255;
	ObjPrim_SetDestCenter(obj,0,0,50,50);
	ObjRender_SetPosition(obj,X,Y,0);
	loop(20)
	{
		ObjRender_SetScaleXYZ(obj,scale,scale,scale);
		scale+=1.2/20;
		ObjRender_SetAlpha(obj,Alpha);
		Alpha-=128/20;
		yield;
	}
	Obj_Delete(obj);
}
task SlowEffect
{//低速エフェクト
	loop
	{
		if(GetVirtualKeyState(VK_SLOWMOVE)==KEY_PUSH || GetVirtualKeyState(VK_SLOWMOVE)==KEY_HOLD)
		{
			SlowEffectRender1;
			SlowEffectRender2;
			while(GetVirtualKeyState(VK_SLOWMOVE)==KEY_PUSH || GetVirtualKeyState(VK_SLOWMOVE)==KEY_HOLD){yield;}
		}
		yield;
	}

	//時計、反時計で若干挙動が違う

	task SlowEffectRender1
	{
		let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		ObjPrim_SetTexture(obj,IMG_MOVEMAGICCIRCLE);
		ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjPrim_SetVertexCount(obj, 4);
	//	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
		Obj_SetRenderPriorityI(obj,44);
		ObjRender_SetAngleZ(obj,0);

		ObjPrim_SetDestCenter(obj,0,0,70,70);

		let Alpha=0;
		let R=255;
		let G=255;
		let B=255;
		let Zangle=0;
		let Scale=0;
		let ScaleMax=Scale;
		loop(5)
		{
			if(GetVirtualKeyState(VK_SLOWMOVE)!=KEY_PUSH && GetVirtualKeyState(VK_SLOWMOVE)!=KEY_HOLD){break;}
			ObjRender_SetPosition(obj,GetPlayerX,GetPlayerY,0);
			ObjRender_SetScaleXYZ(obj,Scale,Scale,1);
			ObjRender_SetAlpha(obj,255*Alpha);
			ObjRender_SetAngleZ(obj,Zangle);
			Alpha+=1.0/15;
			Scale+=1/2.5;
			yield;
		}
		loop(10)
		{
			if(GetVirtualKeyState(VK_SLOWMOVE)!=KEY_PUSH && GetVirtualKeyState(VK_SLOWMOVE)!=KEY_HOLD){break;}
			ObjRender_SetPosition(obj,GetPlayerX,GetPlayerY,0);
			ObjRender_SetScaleXYZ(obj,Scale,Scale,1);
			ObjRender_SetAlpha(obj,255*Alpha);
			ObjRender_SetAngleZ(obj,Zangle);
			Alpha+=1.0/15;
			Scale-=1/10;
			yield;
		}
		loop
		{
			if(GetVirtualKeyState(VK_SLOWMOVE)!=KEY_PUSH && GetVirtualKeyState(VK_SLOWMOVE)!=KEY_HOLD){break;}
			ObjRender_SetPosition(obj,GetPlayerX,GetPlayerY,0);
			ObjRender_SetScaleXYZ(obj,Scale,Scale,1);
			ObjRender_SetAlpha(obj,255*Alpha);
			ObjRender_SetAngleZ(obj,Zangle);
			Zangle-=3;
			yield;
		}
		Obj_Delete(obj);
	}

	task SlowEffectRender2
	{
		let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		ObjPrim_SetTexture(obj,IMG_MOVEMAGICCIRCLE);
		ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjPrim_SetVertexCount(obj, 4);
	//	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
		Obj_SetRenderPriorityI(obj,44);
		ObjRender_SetAngleZ(obj,0);

		ObjPrim_SetDestCenter(obj,0,0,70,70);

		let Alpha=0;
		let R=255;
		let G=255;
		let B=255;
		let Zangle=0;
		let ZanglePlus=6;
		let Scale=1;
		loop(5)
		{
			Zangle+=ZanglePlus;
			ZanglePlus-=6/15;
			if(GetVirtualKeyState(VK_SLOWMOVE)!=KEY_PUSH && GetVirtualKeyState(VK_SLOWMOVE)!=KEY_HOLD){break;}
			ObjRender_SetPosition(obj,GetPlayerX,GetPlayerY,0);
			ObjRender_SetScaleXYZ(obj,Scale,Scale,1);
			ObjRender_SetAlpha(obj,255*Alpha);
			ObjRender_SetAngleZ(obj,Zangle);
			Alpha+=1.0/15;
			yield;
		}
		loop(10)
		{
			Zangle+=ZanglePlus;
			ZanglePlus-=6/15;
			if(GetVirtualKeyState(VK_SLOWMOVE)!=KEY_PUSH && GetVirtualKeyState(VK_SLOWMOVE)!=KEY_HOLD){break;}
			ObjRender_SetPosition(obj,GetPlayerX,GetPlayerY,0);
			ObjRender_SetScaleXYZ(obj,Scale,Scale,1);
			ObjRender_SetAlpha(obj,255*Alpha);
			ObjRender_SetAngleZ(obj,Zangle);
			Alpha+=1.0/15;
			yield;
		}
		loop
		{
			if(GetVirtualKeyState(VK_SLOWMOVE)!=KEY_PUSH && GetVirtualKeyState(VK_SLOWMOVE)!=KEY_HOLD){break;}
			ObjRender_SetPosition(obj,GetPlayerX,GetPlayerY,0);
			ObjRender_SetScaleXYZ(obj,Scale,Scale,1);
			ObjRender_SetAlpha(obj,255*Alpha);
			ObjRender_SetAngleZ(obj,Zangle);
			Zangle+=3;
			yield;
		}
		Obj_Delete(obj);
	}
}

task TExplosion
{//被弾エフェクト
	PlaySE(SE_HIT);
	
	while(GetPlayerState==STATE_HIT)
	{//喰らいボム可能時のエフェクト
		if(GetPlayerLife<=1)
		{
			BarrierDanger(0);
		}
		else
		{
			BarrierDanger(1);
		}
		yield;
	}
	
	//反転エフェクトの終了合図用フラグ
	let ReverseEffectFlag=true;
	
	if(GetPlayerState!=STATE_NORMAL)
	{

		if(GetPlayerLife<0)
		{
			PlaySE(SE_PLAYERDOWN2);
		}
		else
		{
			PlaySE(SE_PLAYERDOWN);
		}
		ShakeSpell(1.5);
		ShootDown;
		loop(30){Particle(2);}
		RebirthEffect;
		ExplodeEffect;
		ShockWave;
		let rx=GetPlayerX;
		let ry=GetPlayerY;

	}
	ReverseEffectFlag=false;

	task ShockWave
	{//衝撃波
		let x=GetPlayerX;
		let y=GetPlayerY;
		loop(30){yield;}
		let obj=CreatePlayerShotA1(x,y,0,0,8,65535,100);
		let range=0;
		loop(30)
		{
			//ObjShot_SetIntersectionCircleA1(obj,range);
			range+=20;
			yield;
		}
		//DeleteShotAll(TYPE_SHOT,TYPE_IMMEDIATE);
		Obj_Delete(obj);
	}

	task Particle(type)
	{
		let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		let bit = round(rand(0,1));
		if(bit==0)
		{
			ObjPrim_SetTexture(obj,IMG_SHARD2);
		}
		else
		{
			ObjPrim_SetTexture(obj,IMG_SHARD3);
		}
		ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjPrim_SetVertexCount(obj, 4);
		Obj_SetRenderPriorityI(obj,69);
		ObjRender_SetAngleZ(obj,0);

		ObjPrim_SetDestCenter(obj,0,0,40,60);

		ObjRender_SetPosition(obj,GetPlayerX,GetPlayerY,0);
		let Scale=rand(0.5,0.75);
		let ScaleMax=Scale;
		let speed=rand(1,7);
		let speed2=speed;
		let Angle=rand(0,360);
		let time=40;
		ObjRender_SetAngleZ(obj,rand(0,360));
		let spin = rand(-2,2);
		ObjRender_SetAlpha(obj,164);
		loop(time/4*3)
		{
			ObjRender_SetAngleZ(obj,ObjRender_GetAngleZ(obj)+spin);
			ObjRender_SetPosition(obj,ObjRender_GetX(obj)+speed*cos(Angle),ObjRender_GetY(obj)+speed*sin(Angle),0);
			ObjRender_SetScaleXYZ(obj,Scale,Scale,1);
			yield;
		}
		loop(time/4)
		{
			ObjRender_SetAngleZ(obj,ObjRender_GetAngleZ(obj)+spin);
			ObjRender_SetPosition(obj,ObjRender_GetX(obj)+speed*cos(Angle),ObjRender_GetY(obj)+speed*sin(Angle),0);
			ObjRender_SetScaleXYZ(obj,Scale,Scale,1);
			Scale-=ScaleMax/(time/4);
			speed-=speed2/(time/4);
			
			yield;
		}
		Obj_Delete(obj);
	}
	task ReverseEffect(x,y)
	{//反転複合エフェクト
		let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		ObjPrim_SetTexture(obj,IMG_WHITECIRCLE_L);
		ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjRender_SetBlendType(obj, BLEND_INV_DESTRGB);
		ObjPrim_SetVertexCount(obj, 4);
		Obj_SetRenderPriorityI(obj,69);
		ObjRender_SetAngleZ(obj,0);

		ObjPrim_SetDestCenter(obj,0,0,512,512);

		ObjRender_SetPosition(obj,x,y,0);

		let Alpha=1.0;
		let Scale=0;
		let ScalePlus=0;
		let ScalePlusSpeed=0.0125/8*1.33;
		let R=255;
		let G=255;
		let B=255;

		while(ReverseEffectFlag)
		{
			ScalePlus+=ScalePlusSpeed;
			Scale+=ScalePlus;
			ObjRender_SetScaleXYZ(obj,Scale,Scale,1);
			yield;
		}
		Obj_Delete(obj);
	}

	task ExplodeEffect
	{
		let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		ObjPrim_SetTexture(obj,IMG_DEADCIRCLE);
		ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjPrim_SetVertexCount(obj, 4);
		ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
		Obj_SetRenderPriorityI(obj,30);
		ObjRender_SetAngleZ(obj,0);

		ObjPrim_SetDestCenter(obj,0,0,309,309);

		ObjRender_SetPosition(obj,GetPlayerX,GetPlayerY,0);
		
		let Alpha=255;
		let Scale=0;

		let am = 2;

		loop(70)
		{
			DeleteShotInCircle(TYPE_SHOT,TYPE_ITEM,ObjRender_GetX(obj),ObjRender_GetY(obj),(309/2*Scale));
			ObjRender_SetAlpha(obj,Alpha);
			ObjRender_SetScaleXYZ(obj,Scale,Scale,1);
			Alpha-=255/70;
			Scale+=0.8/70*am;
			am-=2/70;
			yield;
		}
		Obj_Delete(obj);
	}

	task RebirthEffect
	{//復活時の自機制御
		let X=GetStgFrameWidth/2;
		let Y=466;//416
		RebirthFlag=true;
		loop(5)
        {
        //    ObjRender_SetPosition(objPlayer, X,Y, 0);
            yield;
        }
        SetForbidPlayerShot(true);
        loop(5)
        {
            //ObjRender_SetPosition(objPlayer, X,Y, 0);
            yield;
        }
		RebirthFlag=false;
		SetForbidPlayerShot(false);
	}
}
task BarrierDanger(type)
{
	let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);		
	ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
	ObjPrim_SetVertexCount(obj, 4);
	Obj_SetRenderPriorityI(obj,69);
	if(type==1)
	{
		ObjPrim_SetTexture(obj,IMG_SHARD1);
		ObjPrim_SetDestCenter(obj,0,0,40,60);
		ObjRender_SetScaleXYZ(obj,rand(0.9,1.1)*2,rand(0.9,1.1)*2,1);
	}
	else
	{
		ObjPrim_SetTexture(obj,IMG_BARRIER);
		ObjPrim_SetDestCenter(obj,0,0,60,60);
		let scale = rand(0.99,1.01)*2;
		ObjRender_SetScaleXYZ(obj,scale,scale,1);
	}
	ObjRender_SetPosition(obj,GetPlayerX,GetPlayerY,0);
	ObjRender_SetAngleZ(obj,rand(0,360));
	
	yield;
	
	Obj_Delete(obj);
}