
task MagicMissile
{
	let speed=1.0;
	let maxspeed=7.5;
	let angle=CharaDirection*45-90;
	let damage=Status_MagicPower*1.25;
	let type=7;
	if(IsPermitPlayerShot==false){return;}
	if(GetCommonData("SceneChange",false)==true){return;}
	if(MagicMissileNum>=MagicMissileNumMax){return;}

	if(SpiritPoint>=5*SpiritReduce)
	{
		SpiritPoint-=5*SpiritReduce;
	}
	else
	{
		SpiritPointLack;
		return;
	}

	Obj_PlaySE(GetCurrentScriptDirectory~"../se/tama1.wav");
	MagicMissileNum+=1;

	let objShot = ObjShot_Create(OBJ_SHOT);
	ObjMove_SetX(objShot, GetPlayerX);//x座標設定
	ObjMove_SetY(objShot, GetPlayerY);//y座標設定;
	ObjMove_SetSpeed(objShot, speed);//速度設定
	ObjMove_SetAngle(objShot, angle);
	ObjShot_SetGraphic(objShot, type);//画像設定
	ObjShot_SetDamage(objShot, damage*0);//ダメージを設定
	ObjShot_SetPenetration(objShot, 1);//貫通力は適当に大きく
	ObjShot_Regist(objShot);

	ShotForbitTime(15);

	let ShotCount=0;
	while( !Obj_IsDeleted(objShot) )
	{
		if(speed<maxspeed){speed+=0.1;}
		ObjMove_SetSpeed(objShot, speed);//速度設定
		let sr=Obj_GetValue(GetPlayerObjectID,"Move_Right");
		let sl=Obj_GetValue(GetPlayerObjectID,"Move_Left");
		let su=Obj_GetValue(GetPlayerObjectID,"Move_Up");
		let sd=Obj_GetValue(GetPlayerObjectID,"Move_Down");
		if(sr!=0)
		{
			ObjMove_SetX(objShot, ObjMove_GetX(objShot)-sr);
		}
		if(sl!=0)
		{
			ObjMove_SetX(objShot, ObjMove_GetX(objShot)-sl);
		}
		if(su!=0)
		{
			ObjMove_SetY(objShot, ObjMove_GetY(objShot)-su);
		}
		if(sd!=0)
		{
			ObjMove_SetY(objShot, ObjMove_GetY(objShot)-sd);
		}
		ShotCount+=1;
		if(ShotCount>=50){Obj_Delete(objShot);}
		if(ObjCol_IsIntersected(objShot))
		{
			let dx=ObjMove_GetX(objShot);
			let dy=ObjMove_GetY(objShot);
			DamageTask(dx,dy);
		}
		yield;
	}
	
	MagicMissileNum-=1;

	task DamageTask(x,y)
	{
		ascent(i in 0..3)
		{
			DamageShot(x,y,i*5);
		}
		ascent(i in 0..15)
		{
			HitEffect(x,y,i);
		}
	}

	task DamageShot(x,y,time)
	{
		loop(time)
		{
			let sr=Obj_GetValue(GetPlayerObjectID,"Move_Right");
			let sl=Obj_GetValue(GetPlayerObjectID,"Move_Left");
			let su=Obj_GetValue(GetPlayerObjectID,"Move_Up");
			let sd=Obj_GetValue(GetPlayerObjectID,"Move_Down");
			if(sr!=0)
			{
				x-=sr;
			}
			if(sl!=0)
			{
				x-=sl;
			}
			if(su!=0)
			{
				y-=su;
			}
			if(sd!=0)
			{
				y-=sd;
			}
		yield;
		}
		let objShot = ObjShot_Create(OBJ_SHOT);
		ObjMove_SetX(objShot, x);//x座標設定
		ObjMove_SetY(objShot, y);//y座標設定;
		ObjShot_SetGraphic(objShot, 8);//画像設定
		ObjShot_SetDamage(objShot, damage);//ダメージを設定
		ObjShot_SetPenetration(objShot, 10000);//貫通力は適当に大きく
		ObjShot_Regist(objShot);

	//	ObjRender_SetAlpha(objShot,0);

		let ShotCount=0;
	//	while( !Obj_IsDeleted(objShot) )
		loop(1)
		{
			let sr=Obj_GetValue(GetPlayerObjectID,"Move_Right");
			let sl=Obj_GetValue(GetPlayerObjectID,"Move_Left");
			let su=Obj_GetValue(GetPlayerObjectID,"Move_Up");
			let sd=Obj_GetValue(GetPlayerObjectID,"Move_Down");
			if(sr!=0)
			{
				ObjMove_SetX(objShot, ObjMove_GetX(objShot)-sr);
			}
			if(sl!=0)
			{
				ObjMove_SetX(objShot, ObjMove_GetX(objShot)-sl);
			}
			if(su!=0)
			{
				ObjMove_SetY(objShot, ObjMove_GetY(objShot)-su);
			}
			if(sd!=0)
			{
				ObjMove_SetY(objShot, ObjMove_GetY(objShot)-sd);
			}
			ShotCount+=1;
		//	if(ShotCount>=45){Obj_Delete(objShot);}
			yield;
		}
		Obj_Delete(objShot);
	}

	task HitEffect(x,y,time)
	{
		loop(time)
		{
			let sr=Obj_GetValue(GetPlayerObjectID,"Move_Right");
			let sl=Obj_GetValue(GetPlayerObjectID,"Move_Left");
			let su=Obj_GetValue(GetPlayerObjectID,"Move_Up");
			let sd=Obj_GetValue(GetPlayerObjectID,"Move_Down");
			if(sr!=0)
			{
				x-=sr;
			}
			if(sl!=0)
			{
				x-=sl;
			}
			if(su!=0)
			{
				y-=su;
			}
			if(sd!=0)
			{
				y-=sd;
			}
		yield;
		}
		let imgWhite = GetCurrentScriptDirectory() ~ "../img/effects.png";
		let obje = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		ObjPrim_SetTexture(obje, imgWhite);
		ObjPrim_SetPrimitiveType(obje, PRIMITIVE_TRIANGLEFAN);
		ObjPrim_SetVertexCount(obje, 4);
		ObjRender_SetBlendType(obje, BLEND_ADD_RGB);
		Obj_SetRenderPriorityI(obje,20);
		ObjRender_SetAngleZ(obje,0);

		ObjPrim_SetVertexPosition(obje, 0, -64, -64, 0);
		ObjPrim_SetVertexPosition(obje, 1, 64, -64, 0);
		ObjPrim_SetVertexPosition(obje, 2, 64, 64, 0);
		ObjPrim_SetVertexPosition(obje, 3,-64, 64, 0);

		ObjPrim_SetVertexUVT(obje, 0, 0, 0);
		ObjPrim_SetVertexUVT(obje, 1, 128, 0);
		ObjPrim_SetVertexUVT(obje, 2, 128, 128);
		ObjPrim_SetVertexUVT(obje, 3, 0, 128);

		let ox=GetPlayerX;
		let oy=GetPlayerY;
		let Speed=rand(2,10);
		let Angle=rand(0,360);

		let Scale=0.5;

		let Xangle=rand(0,360)*0;
		let Yangle=rand(0,360)*0;
		let Zangle=rand(0,360)*0;

		let XanglePlus=rand(-10,10);
		let YanglePlus=rand(-10,10);
		let ZanglePlus=rand(-10,10);

		ObjRender_SetPosition(obje,ox,oy,0);

		let Alpha=1;
		ObjRender_SetColor(obje,128,64,64*Alpha);

		ObjRender_SetPosition(obje,x,y,0);

		loop(30)
		{
			let sr=Obj_GetValue(GetPlayerObjectID,"Move_Right");
			let sl=Obj_GetValue(GetPlayerObjectID,"Move_Left");
			let su=Obj_GetValue(GetPlayerObjectID,"Move_Up");
			let sd=Obj_GetValue(GetPlayerObjectID,"Move_Down");
			if(sr!=0)
			{
				ObjRender_SetX(obje, ObjRender_GetX(obje)-sr);
			}
			if(sl!=0)
			{
				ObjRender_SetX(obje, ObjRender_GetX(obje)-sl);
			}
			if(su!=0)
			{
				ObjRender_SetY(obje, ObjRender_GetY(obje)-su);
			}
			if(sd!=0)
			{
				ObjRender_SetY(obje, ObjRender_GetY(obje)-sd);
			}
			ObjRender_SetAngleXYZ(obje,Xangle,Yangle,Zangle);
			ObjRender_SetScaleXYZ(obje,Scale,Scale,Scale);
			ObjRender_SetColor(obje,64*Alpha,255*Alpha,64*Alpha);
			Alpha-=1/30;
		//	Scale-=1/30;
			Xangle+=XanglePlus;
			Yangle+=YanglePlus;
			Zangle+=ZanglePlus;
			yield;
		}

		Obj_Delete(obje);
	
	}
}
