//スペルとダミースペル
task TSpell
{//霊符「夢想封印」 
	OnSpell=true;
	SetPlayerInvincibilityFrame_Special(285);//無敵時間を設定
	let objManage = GetSpellManageObject();
	Obj_Delete(objManage);

	PlaySE(SE_SPELL1);
	PlaySE(SE_SPELL2);
	FantasySeal;
	OperationEffect;

	loop(255){yield;}
	OnSpell=false;

	task FantasySeal
	{
		let time=0;
		ascent(i in 0..8)
		{
		Seal(i*45,i*10);
		}
		task Seal(angle,time)
		{
			let ExplodeFlag=false;
			let obj = ObjSpell_Create();
			ObjSpell_Regist(obj);
			ObjSpell_SetDamage(obj, 1);
			SealEffect(obj,0);
			SealEffect(obj,1);
			SealEffect(obj,2);
			SealEffect(obj,0);
			SealEffect(obj,1);
			SealEffect(obj,2);
			SealEffect(obj,3);
			let R=0;
			loop(90+time)
			{
				ObjRender_SetPosition(obj,GetPlayerX+R*cos(angle),GetPlayerY+R*sin(angle),0);
				ObjSpell_SetIntersectionCircle(obj,ObjRender_GetX(obj),ObjRender_GetY(obj),64);
				R+=1.5;
				angle+=6;
				yield;
			}
			let speed=16;
			angle+=90;

			let EnemyObjectArray=GetIntersectionRegistedEnemyID;
			let Dis=1000^2;
			let nearObj=-1;
			let ExpCount=0;
			if(length(EnemyObjectArray)!=0)
			{
				ascent(i in 0..length(EnemyObjectArray))
				{
					let dis=(ObjRender_GetY(EnemyObjectArray[i])-ObjRender_GetY(obj))^2+(ObjRender_GetX(EnemyObjectArray[i])-ObjRender_GetX(obj))^2;
					if(dis<Dis)
					{
						nearObj=EnemyObjectArray[i];
						Dis=dis;
					}
				}
			}
	
			if(nearObj!=-1)
			{
				loop(155-time)
				{
				let ToPlayerAngle=atan2(ObjRender_GetY(nearObj)-ObjRender_GetY(obj),ObjRender_GetX(nearObj)-ObjRender_GetX(obj));
				let judgangle=sin(ToPlayerAngle-angle);

				if(judgangle>0)
				{
					if(judgangle>0.05)
					{
					angle+=asin(judgangle)/4;
					}
					else
					{
					angle=ToPlayerAngle;
					}
				}
				else
				{
					if(judgangle<-0.05)
					{
					angle+=asin(judgangle)/4;
					}
					else
					{
					angle=ToPlayerAngle;
					}
				}
				let dis=((ObjRender_GetY(nearObj)-ObjRender_GetY(obj))^2+(ObjRender_GetX(nearObj)-ObjRender_GetX(obj))^2)^0.5;
				if(dis<=64){ExpCount+=1;}
				if(ExpCount>=35){break;}
				speed=dis/20;
				ObjSpell_SetIntersectionCircle(obj,ObjRender_GetX(obj),ObjRender_GetY(obj),64);
				ObjRender_SetPosition(obj,ObjRender_GetX(obj)+speed*cos(angle),ObjRender_GetY(obj)+speed*sin(angle),0);
				yield;
				}
			}
			else
			{
			loop(120-time)
			{
				if(ObjRender_GetX(obj)<0 || ObjRender_GetY(obj)<0 || ObjRender_GetX(obj)>GetStgFrameWidth || ObjRender_GetY(obj)>GetStgFrameHeight )
				{
					speed-=16/15;
				}
				if(speed<0){break;}
				ObjRender_SetPosition(obj,ObjRender_GetX(obj)+speed*cos(angle),ObjRender_GetY(obj)+speed*sin(angle),0);
				ObjSpell_SetIntersectionCircle(obj,ObjRender_GetX(obj),ObjRender_GetY(obj),64);
				yield;
			}
			}

			PlaySE(SE_SPELL3);
			TShake();
			ExplodeFlag=true;			
			let objExp = ObjSpell_Create();
			ObjSpell_Regist(objExp);
			ObjSpell_SetDamage(objExp, 125);
			ObjSpell_SetIntersectionCircle(objExp,ObjRender_GetX(obj),ObjRender_GetY(obj),96);
			yield;
			Obj_Delete(obj);

			task SealEffect(objS,type)
			{
				let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
				ObjPrim_SetTexture(obj,IMG_SEAL);
				ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
				ObjPrim_SetVertexCount(obj, 4);
				ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
				Obj_SetRenderPriorityI(obj,40);
				ObjRender_SetAngleZ(obj,0);

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

				ObjRender_SetPosition(obj,ObjRender_GetX(objS),ObjRender_GetY(objS),0);

				let Scale=0;
				let Alpha=1.0;
				let R=255;
				let G=255;
				let B=255;
				let Div=3;
				let DivAngle=type*120;
				let Zangle=rand(0,360);
				let ZanglePlus=rand(2,3)*rand_dir;
				if(type==0)
				{
					Alpha=0.5;
					Div=0;
				}
				if(type==1)
				{
					R=255;G=0;B=0;
				}
				if(type==2)
				{
					R=0;G=255;B=0;
				}
				if(type==3)
				{
					R=0;G=0;B=255;
				}
				let AlphaMax=Alpha;
				ObjRender_SetColor(obj,R*Alpha,G*Alpha,B*Alpha);
				while(!ExplodeFlag)
				{
					ObjRender_SetPosition(obj,ObjRender_GetX(objS)+Div*cos(DivAngle),ObjRender_GetY(objS)+Div*sin(DivAngle),0);
					if(Scale<1){Scale+=1/20;}
					ObjRender_SetScaleXYZ(obj,Scale,Scale,Scale);
					ObjRender_SetAngleZ(obj,Zangle);
					Zangle+=ZanglePlus;
					DivAngle+=6;
					yield;		
				}
				if(type==0)
				{
					loop(20)
					{
					Scale+=7/20;
					Alpha-=0.5/20;
					ObjRender_SetColor(obj,R*Alpha,G*Alpha,B*Alpha);
					ObjRender_SetScaleXYZ(obj,Scale,Scale,Scale);
					yield;
					}
				}
				else
				{
					let angle=rand(0,360);
					let speed=rand(2.5,5);
					loop(45)
					{
						ObjRender_SetPosition(obj,ObjRender_GetX(obj)+speed*cos(angle),ObjRender_GetY(obj)+speed*sin(angle),0);
						Scale-=1/45;
						Alpha-=1/45;
						ObjRender_SetColor(obj,R*Alpha,G*Alpha,B*Alpha);
						ObjRender_SetScaleXYZ(obj,Scale,Scale,Scale);
						yield;
					}
				}
			}
		}
	}

	task TShake()
	{
		loop(8)
		{
			Set2DCameraFocusX(GetStgFrameWidth / 2 + 10*rand_degree);
			Set2DCameraFocusY(GetStgFrameHeight / 2 + 10*rand_degree);
			yield;
		}
		Reset2DCamera();
	}

}

task OperationEffect
{
	ShockEffect;
	task ShockEffect
	{
		let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		ObjPrim_SetTexture(obj,IMG_SHOCK);
		ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjPrim_SetVertexCount(obj, 4);
		ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
		Obj_SetRenderPriorityI(obj,40);
		ObjRender_SetAngleZ(obj,0);

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

		ObjRender_SetPosition(obj,GetPlayerX,GetPlayerY,0);

		//let Zangle=-num*60;
		let Alpha=0.2;
		let AlphaMax=Alpha;
		let R=255;
		let G=50;
		let B=50;
		let Scale=0;
		let ScaleSpeed=0.005;
		ObjRender_SetScaleXYZ(obj,Scale,Scale,Scale);
		loop(180)
		{
			//
			ObjRender_SetColor(obj,R*Alpha,G*Alpha,B*Alpha);
			ObjRender_SetScaleXYZ(obj,Scale,Scale,Scale);
		//	Alpha-=1/20;
			Scale+=ScaleSpeed;
			ScaleSpeed+=0.0005;
			yield;
		}
		loop(75){yield;}
		Obj_Delete(obj);
	}
}

task DummySpell
{//特殊無敵システム用
	SetPlayerInvincibilityFrame(0);//無敵時間を設定
	let objManage = GetSpellManageObject();
	Obj_Delete(objManage);
	if(OnSpell)
	{
		SetPlayerSpeed(4.5*2,2.0*2);	
	}
	else
	{
		SetPlayerSpeed(4.5*2,2.0*2);
	}
	yield;
	if(OnSpell)
	{
		SetPlayerSpeed(4.5,2.0);	
	}
	else
	{
		SetPlayerSpeed(4.5,2.0);
	}
}