let ZakoColor=[255,255,255];
let ZakoMagicColor=[255,255,255];
let BulletNum;

function MSDSetCollisionB(x,y,Radius)
{
	if(GetCommonData("BURSTLEVEL")==0)
	{
		SetCollisionB(x,y,Radius);
	}
}

task GetDamege//ショットが当たった時色変更
{
GetDamegeItem;
loop
{
	if(GetHitCount!=0)
	{
		ZakoColor=[150,150,255];

		wait(10);
	}
	else
	{
		ZakoColor=[255,255,255];
	}
yield;
}
}

task GetDamegeItem//近接打ち込み
{
loop
{
		if(GetHitCount!=0 && GetDistanceToPlayer<=GetCommonData("MagicAbsorbShortRange"))
		{
			if(GetCommonData("BURSTLEVEL")==0)
			{
			ItemSet(GetX,GetY,2);
			}
			if(GetCommonData("BURSTLEVEL")==3)
			{
			ItemSet(GetX,GetY,8);
			}
		}
yield;
}
}

task MagicColor//封印時の色変更
{
loop
{
	if(GetDistanceToPlayer>90)
	{
		ZakoMagicColor=[255,255,255];
	}
	else
	{
		ZakoMagicColor=[255,150,150];
	}
yield;
}
}

function wait(w) 
{
	loop(w){yield;}
}

function GetDistanceToPlayer//敵とプレイヤーとも距離を返す
{
	let dis=((GetX-GetPlayerX)^2+(GetY-GetPlayerY)^2)^0.5;
	return(dis);
}

function GroundCreateShot01(X,Y,Speed,Angle,Graphic,Delay)
{//封印されるCreateShot01
	if(GetDistanceToPlayer>90)
	{
		CreateShot01(X,Y,Speed,Angle,Graphic,Delay);
	}
}

function ReturnBurstShotnum(num)
{//バーストでの撃ち返し発生
	if(GetCommonData("BURSTLEVEL")==3)
	{
		ascent(let i in 0..num)
		{
			CreateShotFromScript( "BurstReturnShot",GetX, GetY, 3+rand(-0.75,0.75), GetAngleToPlayer+rand(-30,30), i*2, 8);
		}
		if(GetCommonData("MAGICCOUNTER")>=10000)
		{
			ascent(let i in 0..num)
			{
				CreateShotFromScript( "BurstReturnShot",GetX, GetY, 3+rand(-0.75,0.75), GetAngleToPlayer+rand(-30,30), i*2, 8);
			}
		}
	}
}

function ReturnPowerShotnum(num)
{//マジック反応弾撃ち返し発生
	if(GetCommonData("BURSTLEVEL")==0)
	{
		ascent(let i in 0..num)
		{
			if(GetDistanceToPlayer>75)
			{
				CreateShotFromScript( "PowerReturnShot",GetX, GetY, 3+rand(-0.75,0.75), GetAngleToPlayer+rand(-30,30), i*2, 8);
			}
			else
			{
				ItemSet(GetX,GetY,7);
			}
		}
	}
}

function FinalizeItemSet(num)
{
if(GetX<GetClipMinX-16 || GetClipMaxX+16<GetX || GetY<GetClipMinY-16 || GetClipMaxY+16<GetY)
{

}
else
{
let type;
alternative(GetCommonData("BURSTLEVEL"))
case(0)
{
	type=3;
}
case(3)
{
	type=5;
}
		if(!BeVanished)
		{
		loop(num)
		{
			ItemSet(GetX,GetY,type);
			if(GetCommonData("MAGICCOUNTER")>=10000)
			{
				ItemSet(GetX,GetY,type);
			}
			if(GetDistanceToPlayer<=GetCommonData("MagicAbsorbShortRange"))
			{
				ItemSet(GetX,GetY,1);
			}
			if(GetDistanceToPlayer<=GetCommonData("MagicAbsorbLongRange"))
			{
				ItemSet(GetX,GetY,1);
			}
			if(GetCommonData("MAGICPOWER")>=1000)
			{
				ItemSet(GetX,GetY,1);
			}
			if(GetCommonDataDefault("SELECTEDDIFFICULT","None")=="Apocalypse")
			{
				ItemSet(GetX,GetY,9);
				ItemSet(GetX,GetY,9);
			}
		}
		}
}
}

function ItemSet(x,y,type)//アイテム生成関数
{
	let XY=GetCommonDataDefault("ItemXY",[]);//共通データ内容を変数に渡す
	XY=XY~[x,y,type];//
	SetCommonData("ItemXY",XY);//共通データに変更後の配列を渡す
}

task OutDamageRateZero
{
loop
{
	if(GetX<GetClipMinX-16 || GetClipMaxX+16<GetX || GetY<GetClipMinY-16 || GetClipMaxY+16<GetY)
	{
		SetDamageRate(0,0);
	}
	else
	{
		if(GetDistanceToPlayer<=GetCommonData("MagicAbsorbShortRange"))
		{
			SetDamageRate(105,105);
		}
		if(GetDistanceToPlayer<=GetCommonData("MagicAbsorbLongRange"))
		{
			SetDamageRate(102.5,102.5);
		}
		else
		{
			SetDamageRate(100,100);
		}
	}

yield;
}
}

task AutoErazeTime(w)
{
ZakoEnemyLifeDisplay;
loop(w)
{
if(GetCommonDataDefault("VanishSignal",false))
{
	VanishEnemy;
	return;
}
yield;
}
VanishEnemy;
}


task ZakoEnemyLifeDisplay//下部に敵のライフを表示
{
//ZakoLifeBerDisplay;
ZakoLifeDisplay;
}

task ZakoLifeBerDisplay//ライフバーの枠の設定
{
	let BossLifeMax=GetLife;

		let obj = Obj_Create(OBJ_EFFECT);
		ObjEffect_SetTexture(obj, csd ~ "..\img\ber.png");
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjEffect_SetRenderState(obj, ADD); 
		ObjEffect_CreateVertex(obj, 4);
		ObjEffect_SetVertexXY(obj, 0, -71, -5);
		ObjEffect_SetVertexXY(obj, 1, 71,  -5);
		ObjEffect_SetVertexXY(obj, 2, 71, 5);
		ObjEffect_SetVertexXY(obj, 3,  -71,  5);
		
		ObjEffect_SetVertexUV(obj, 0,  6,  11);
		ObjEffect_SetVertexUV(obj, 1,  148, 11);
		ObjEffect_SetVertexUV(obj, 2, 148,  21);
		ObjEffect_SetVertexUV(obj, 3, 6, 21);
		ObjEffect_SetLayer(obj,8);
		ObjEffect_SetScale(obj, 0.5,0.16);

		while(GetLife>0)
		{
			Obj_SetPosition(obj,GetX,GetY-16);
			yield
		}
		Obj_Delete(obj);
}


task ZakoLifeDisplay//ライフバーのバー部分の設定
{
	let ZakoLifeMax=GetLife;
	if(ZakoLifeMax<=20){return;}
	let scale=0.5;
	let Sscale=0.5;
	let XAdjust=25;

		let obj = Obj_Create(OBJ_EFFECT);
		ObjEffect_SetTexture(obj, csd ~ "..\img\ber.png");
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjEffect_SetRenderState(obj, ADD); 
		ObjEffect_CreateVertex(obj, 4);
		ObjEffect_SetVertexXY(obj, 0, 0, -3);
		ObjEffect_SetVertexXY(obj, 1, 100,  -3);
		ObjEffect_SetVertexXY(obj, 2, 100, 3);
		ObjEffect_SetVertexXY(obj, 3,  0,  3);
		
		ObjEffect_SetVertexUV(obj, 0,  6,  27);
		ObjEffect_SetVertexUV(obj, 1,  206, 27);
		ObjEffect_SetVertexUV(obj, 2, 206,  33);
		ObjEffect_SetVertexUV(obj, 3, 6, 33);
		ObjEffect_SetLayer(obj,2);
		ObjEffect_SetScale(obj, scale,0.16);

		ObjEffect_SetVertexColor(obj,0,150,255,255,0);
		ObjEffect_SetVertexColor(obj,1,150,255,0,0);
		ObjEffect_SetVertexColor(obj,2,150,255,0,0);
		ObjEffect_SetVertexColor(obj,3,150,255,255,0);

		while(GetLife>0)
		{
			Obj_SetPosition(obj,GetX-XAdjust,GetY-16);
			scale=Sscale*GetLife/ZakoLifeMax;
			ObjEffect_SetVertexColor(obj,0,255,255,255,0);
			ObjEffect_SetVertexColor(obj,1,255,255,255-255*GetLife/ZakoLifeMax,0);
			ObjEffect_SetVertexColor(obj,2,255,255,255-255*GetLife/ZakoLifeMax,0);
			ObjEffect_SetVertexColor(obj,3,255,255,255,0);
			if(GetLife==ZakoLifeMax)
			{
			ObjEffect_SetVertexColor(obj,0,0,255,255,0);
			ObjEffect_SetVertexColor(obj,1,0,255,255-255*GetLife/ZakoLifeMax,0);
			ObjEffect_SetVertexColor(obj,2,0,255,255-255*GetLife/ZakoLifeMax,0);
			ObjEffect_SetVertexColor(obj,3,0,255,255,0);
			}
			ObjEffect_SetScale(obj, scale,0.8);
			yield;
		}
		Obj_Delete(obj);
}

task ZakoEffect(R,G,B)
{
	ZakoEffect0(R,G,B);
loop
{
	ZakoEffect1(R,G,B);
yield;
}
}

task ZakoEffect0(R,G,B)
{
		let scax=1.0;
		let scay=1.0;
		let angle=0;
		let Xangle=-90;
		let Yangle=-90;	
		let Zangle=0;
		let RotateAngle=3;
		let alpha=100;
		if(GetCommonDataDefault("LightMode",0)==1){alpha=200;}
  		let obj = Obj_Create(OBJ_EFFECT);
		ObjEffect_SetTexture(obj,GetCurrentScriptDirectory~"..\img\CircleEffect.png");
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjEffect_SetRenderState(obj, ADD);
		ObjEffect_CreateVertex(obj, 4);
		ObjEffect_SetVertexXY(obj, 0, -24, -24);
		ObjEffect_SetVertexXY(obj, 1,  24, -24);
		ObjEffect_SetVertexXY(obj, 2,  24,  24);
		ObjEffect_SetVertexXY(obj, 3, -24,  24);
		
		ObjEffect_SetVertexUV(obj, 0,  0,  0);
		ObjEffect_SetVertexUV(obj, 1,  48, 0);
		ObjEffect_SetVertexUV(obj, 2, 48,  48);
		ObjEffect_SetVertexUV(obj, 3, 0, 48);

			ascent(let i in 0..4)
			{
			ObjEffect_SetVertexColor(obj,i,alpha,R,G,B);
			}

		ObjEffect_SetScale(obj, scax,scay);
		loop
		{
		Obj_SetPosition(obj,GetX,GetY);
			yield;
		}
}


task ZakoEffect1(R,G,B)
{
		let randrect=5;
	let XYrect=[];
	loop(8)
	{
	XYrect=XYrect~[48/2+rand(-randrect,randrect)];
	}
	if(GetCommonDataDefault("LightMode",0)==1){return;}

		let scax=1.0;
		let scay=1.0;
		let angle=0;
		let Xangle=-90;
		let Yangle=-90;	
		let Zangle=0;
		let RotateAngle=3;
		let alpha=50;
  		let obj = Obj_Create(OBJ_EFFECT);
		ObjEffect_SetTexture(obj,GetCurrentScriptDirectory~"..\img\CircleEffect.png");
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjEffect_SetRenderState(obj, ADD);
		ObjEffect_CreateVertex(obj, 4);

	ObjEffect_SetVertexXY(obj, 0, -XYrect[0], -XYrect[1]);
	ObjEffect_SetVertexXY(obj, 1, XYrect[2], -XYrect[3]);
	ObjEffect_SetVertexXY(obj, 2, XYrect[4], XYrect[5]);
	ObjEffect_SetVertexXY(obj, 3, -XYrect[6], XYrect[7]);
		
		ObjEffect_SetVertexUV(obj, 0,  0,  0);
		ObjEffect_SetVertexUV(obj, 1,  48, 0);
		ObjEffect_SetVertexUV(obj, 2, 48,  48);
		ObjEffect_SetVertexUV(obj, 3, 0, 48);


		Obj_SetPosition(obj,GetX,GetY);
		ObjEffect_SetScale(obj, scax,scay);
		Obj_SetSpeed(obj,1);
		Obj_SetAngle(obj,-90);
		loop(10)
		{
			ascent(let i in 0..4)
			{
			ObjEffect_SetVertexColor(obj,i,alpha,R,G,B);
			}
			ObjEffect_SetScale(obj, scax,scay);
			scax+=0.3/10;
			scay+=0.3/10;
			alpha-=50/10;
			yield;
		}
		Obj_Delete(obj);
}

function moveToPlayer03(xMove, yAdd, weight,MaxSpeed, left, top, right, bottom)
{

	if(GetX>GetPlayerX)
	{
		xMove=xMove*-1;
	}

	if(GetX+xMove<left || GetX+xMove>right)
	{
		xMove=xMove*-1;
	}

	if(GetY+yAdd<top || GetY+yAdd>bottom)
	{
		yAdd=yAdd*-1;
	}


	SetMovePosition03(GetX+xMove,GetY+yAdd, weight,MaxSpeed)
}