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

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

		let Alpha=0;
		let R=255;
		let G=255;
		let B=255;
		let Zangle=0;
		let Scale=0.25;
		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+=0.0125;
			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-=0.00625;
			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,256,256);

		let Alpha=0;
		let R=255;
		let G=255;
		let B=255;
		let Zangle=0;
		let ZanglePlus=6;
		let Scale=0.25;
		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
{//被弾エフェクト
	while(GetPlayerState==STATE_HIT)
	{//喰らいボム可能時のエフェクト
		loop(3)
		{
		Particle(1);
		}
		yield;
	}

	//反転エフェクトの終了合図用フラグ
	let ReverseEffectFlag=true;

	if(GetPlayerState!=STATE_NORMAL)
	{
		loop(30){Particle(2);}
		RebirthEffect;
		ExplodeEffect;
		ShockWave;
		let rx=GetPlayerX;
		let ry=GetPlayerY;
		ReverseEffect(rx,ry);
		loop(4){yield;}
		ReverseEffect(rx+32,ry);
		ReverseEffect(rx-32,ry);
		ReverseEffect(rx,ry+32);
		ReverseEffect(rx,ry-32);
		loop(23){yield;}
		ReverseEffect(rx,ry);
		loop(13){yield;}
		loop(47){yield;}
	}
	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);
		ObjPrim_SetTexture(obj,IMG_LIGHT);
		ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjPrim_SetVertexCount(obj, 4);
		Obj_SetRenderPriorityI(obj,80);
		ObjRender_SetAngleZ(obj,0);

		ObjPrim_SetDestCenter(obj,0,0,64,64);

		ObjRender_SetPosition(obj,GetPlayerX,GetPlayerY,0);

		let Alpha=1.0;
		let Scale=rand(0.5,0.75);
		let ScaleMax=Scale;
		let R=255;
		let G=255;
		let B=255;
		if(rand_dir>0 || type==2)
		{
			ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
			R=255;
			G=150;
			B=255;
		}
		else
		{
			ObjRender_SetBlendType(obj, BLEND_SUBTRACT);
			R=0;
			G=105;
			B=0;
		}
		ObjRender_SetColor(obj,R*Alpha,G*Alpha,B*Alpha);
		let speed=rand(5,15);
		let Angle=rand(0,360);
		let time=rand_int(15,20);
		loop(time)
		{
			ObjRender_SetPosition(obj,ObjRender_GetX(obj)+speed*cos(Angle),ObjRender_GetY(obj)+speed*sin(Angle),0);
			Scale-=ScaleMax/time;
			ObjRender_SetScaleXYZ(obj,Scale,Scale,1);
			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,80);
		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_RGB);
		Obj_SetRenderPriorityI(obj,30);
		ObjRender_SetAngleZ(obj,0);

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

		ObjRender_SetPosition(obj,GetPlayerX,GetPlayerY,0);

		let Alpha=1.0;
		let Scale=1;
		let R=255;
		let G=255;
		let B=255;


		loop(24)
		{
			Alpha-=1.0/24;
			Scale+=4.5/24;
			ObjRender_SetScaleXYZ(obj,Scale,Scale,1);
			ObjRender_SetColor(obj,R*Alpha,G*Alpha,B*Alpha);
			yield;
		}
		Obj_Delete(obj);
	}

	task RebirthEffect
	{//復活時の自機制御
		let X=GetStgFrameWidth/2;
		let Y=466;//416
		RebirthFlag=true;
		loop(10){yield;}
		loop(25)
		{
			ObjRender_SetPosition(objPlayer, X,Y, 0);
			yield;
		}
		SetForbidPlayerShot(true);
		loop(5)
		{
			ObjRender_SetPosition(objPlayer, X,Y, 0);
			yield;
		}
		loop(50)
		{
			ObjRender_SetPosition(objPlayer, X,Y, 0);
			Y-=1;
			yield;
		}
		RebirthFlag=false;
		SetForbidPlayerShot(false);
	}
}