#include"./graphic/NHShot_Const.txt"
#include"./function.txt"

let BossObj;
let BossObjX = 0;
let BossObjY = 0;

let BossAlpha = 255;
let BossHitbox = false;

let ImgBoss;
let imgBossObj = dir ~ "graphic/Hibaru_dot.png";

let objScene = GetEnemyBossSceneObjectID();
let dif=GetCommonData("diff",1);

let stopall=false;

@Initialize 
{
	SetAutoDeleteObject(true);
	BossObj = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ImgBoss = ObjEnemy_Create(OBJ_ENEMY);
	ObjEnemy_Regist(BossObj);
	ObjEnemy_Regist(ImgBoss);
	ObjEnemy_SetDamageRate(BossObj,0,0);
	ObjRender_SetPosition(BossObj,GetStgFrameWidth/2,GetStgFrameHeight/2-120,0);
	ObjEnemy_SetDamageRate(BossObj,0,0);
	mainTask;
	SetForbidPlayerShot(false);
	SetForbidPlayerSpell(false);
} 

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
	{
		SetScriptResult(0);
	}
}

@MainLoop 
{
	ObjRender_SetAlpha(ImgBoss,BossAlpha);
	BossObjX = ObjMove_GetX(BossObj);
	BossObjY = ObjMove_GetY(BossObj);
	ObjRender_SetPosition(ImgBoss,BossObjX,BossObjY,0);
	yield;
} 

@Finalize 
{ 
} 

task mainTask
{
	idlesprite(ImgBoss,60,imgBossObj);
	loop(60){yield;}
	let xx=GetStgFrameWidth/2;
	let yy=GetStgFrameHeight/2;
	let angle=-atan2(BossObjY-yy,BossObjX-xx)-90;
	let spd=(GetDistance(xx,yy,BossObjX,BossObjY))/30;
	let sp=spd;
	let r=spd*60;
	movesprite(ImgBoss,60,imgBossObj,1);
	loop(60)
	{
		ObjMove_SetDestAtSpeed(BossObj,BossObjX+r*sin(angle),BossObjY+r*cos(angle),sp);
		sp-=spd/60;
		yield;
	}
	idlesprite(ImgBoss,60,imgBossObj);
	ObjMove_SetSpeed(BossObj,0);
	let time=60;
	loop(60)
	{
		Charge(BossObjX,BossObjY,rand(time,time*4),time,255,255,255,30,BLEND_SUBTRACT,-1,rand(-3,3),rand(0,360),rand(60,120));
		time--;
		yield;
	}
	idlesprite(ImgBoss,240,imgBossObj);
	let ang=rand(0,360);
	SpellRemain(1,4,ang,BossObj,3600,1);
	SpellRemain(2,4,ang+90,BossObj,3600,1);
	SpellRemain(3,4,ang+180,BossObj,3600,1);
	SpellRemain(4,4,ang+270,BossObj,3600,0);
	loop(120){yield;}
	Ring(BossObjX,BossObjY,120,255,255,255,BLEND_SUBTRACT,450,60,1);
	SEPlay(dir ~ "audio/spell_0.wav",100);
	ObjEnemyBossScene_StartSpell(objScene);
	ImageShow(dir~"graphic/SpellName.png",455,60,0,128,1024,192,BossObj,3600,240/512);
	let color=255;
	loop(120)
	{
		ObjRender_SetColor(ImgBoss,color,color,color);
		color-=255/120;
		BossAlpha-=160/120;
		yield;
	}
	ObjRender_SetColor(ImgBoss,0,0,0);
	timer;
	mainshooter;
}

task timer
{
	let time=60*60;
	loop(time)
	{
		time--;
		ObjEnemyBossScene_SetSpellTimer(objScene,time);
		yield;
	}
	TEnd;
}

task TEnd
{
	SetCommonData("bossend",true);
	BossHitbox=false;
	let range=0;
	loop(60)
	{
		range+=10;
		DeleteShotInCircle(TYPE_SHOT,TYPE_FADE,BossObjX,BossObjY,range);
		ObjMove_SetSpeed(BossObj,0);
		yield;
	}
	let xx=GetStgFrameWidth/2;
	let yy=GetStgFrameHeight/2-120;
	let angle=-atan2(BossObjY-yy,BossObjX-xx)-90;
	let spd=(GetDistance(xx,yy,BossObjX,BossObjY))/30;
	let sp=spd;
	let r=spd*60;
	if(BossObjX+r*sin(angle)<=BossObjX){movesprite(ImgBoss,60,imgBossObj,1);}
	if(BossObjX+r*sin(angle)>BossObjX){movesprite(ImgBoss,60,imgBossObj,-1);}
	let color=0;
	loop(60)
	{
		ObjMove_SetDestAtSpeed(BossObj,BossObjX+r*sin(angle),BossObjY+r*cos(angle),sp);
		ObjRender_SetColor(ImgBoss,color,color,color);
		color+=255/120;
		BossAlpha+=160/120;
		sp-=spd/60;
		yield;
	}
	ObjMove_SetSpeed(BossObj,0);
	ObjRender_SetPosition(BossObj,xx,yy,0);
	SetCommonData("bossend",false);
	Obj_Delete(BossObj);
	Obj_Delete(ImgBoss);
	CloseScript(GetOwnScriptID());
}

task mainshooter
{
	idlesprite(ImgBoss,3600,imgBossObj);
	let xo=GetStgFrameWidth;
	let yo=GetStgFrameHeight;
	let ang=rand(0,360);
	let ag=rand(0,360);
	let aa=0;
	let lp=4;
	let lpp=8;
	if(dif==2){lpp=10;}
	if(dif==3){lpp=12;}
	let range1=640;
	let range2=192;
	if(dif==2){range2=160;}
	if(dif==3){range2=128;}
	let range3=range1-range2;
	let range=240-(range2/2);
	let count=0;
	let cangle=1;
	let FormX=GetStgFrameWidth/2;
	let FormY=GetStgFrameHeight/2;
	let time=60;
	phasing;
	guideline;
	
	task phasing
	{
		let rangeA=0;
		let angA=(truncate(rand(1,4))*90)+90;
		SEPlay(dir ~ "audio/charge_2.wav",100);
		loop(720)
		{
			if(range1<=range2 && rangeA<range){rangeA+=range/180;}
			FormX=xo/2+rangeA*sin(angA);
			FormY=yo/2+rangeA*cos(angA);
			angA+=0.75;
			yield;
		}
		let rangeB=range;
		SEPlay(dir ~ "audio/charge_3.wav",100);
		time=60;
		loop(60)
		{
			Charge(BossObjX,BossObjY,rand(time,time*8),time,255,255,255,30,BLEND_SUBTRACT,-1,rand(-3,3),rand(0,360),rand(60,120));
			FormX=xo/2+rangeB*sin(angA);
			FormY=yo/2+rangeB*cos(angA);
			angA+=0.75;
			time--;
			yield;
		}
		loop(60)
		{
			range1-=32/60;
			rangeB-=(range/3)/60;
			FormX=xo/2+rangeB*sin(angA);
			FormY=yo/2+rangeB*cos(angA);
			angA+=0.75;
			yield;
		}
		rangeB=range/3*2;
		loop(8)
		{
			SEPlay(dir ~ "audio/cell_move_00.wav",80);
			let aB=135;
			let Post1=[FormX,FormY];
			let Post2=[xo/2+rangeB*sin(angA+aB),yo/2+rangeB*cos(angA+aB)];
			let ang=-atan2(Post2[1]-Post1[1],Post2[0]-Post1[0])+90;
			let dis=GetDistance(Post1[0],Post1[1],Post2[0],Post2[1]);
			let cB=0;
			loop(150)
			{
				cB+=1/150;
				FormX=Post1[0]+dis*cB*sin(ang);
				FormY=Post1[1]+dis*cB*cos(ang);
				yield;
			}
			loop(60)
			{
				FormX=Post1[0]+dis*cB*sin(ang);
				FormY=Post1[1]+dis*cB*cos(ang);
				yield;
			}
			angA+=aB;
		}
		let angC=-atan2(FormY-yo/2,FormX-xo/2)+90;
		let rangeC=range/3*2;
		SEPlay(dir ~ "audio/charge_3.wav",100);
		time=60;
		loop(60)
		{
			Charge(BossObjX,BossObjY,rand(time,time*8),time,255,255,255,30,BLEND_SUBTRACT,-1,rand(-3,3),rand(0,360),rand(60,120));
			time--;
			yield;
		}
		loop(60)
		{
			range1-=32/60;
			rangeC-=(range/3)/60;
			FormX=xo/2+rangeC*sin(angC);
			FormY=yo/2+rangeC*cos(angC);
			yield;
		}
		rangeC=range/3;
		let cC=1;
		let rC=range1;
		loop(960)
		{
			cC-=0.9/960;
			range1=rC*(0.5+cC/2);
			FormX=xo/2+rangeC*cC*sin(angC);
			FormY=yo/2+rangeC*cC*cos(angC);
			yield;
		}
	}
	
	task guideline
	{
		let obj=ObjPrim_Create(OBJ_SPRITE_2D);
		Obj_SetRenderPriority(obj,0.4);
		ObjPrim_SetTexture(obj,dir ~ "graphic/GuideCircle.png");
		ObjSprite2D_SetSourceRect(obj,0,0,512,512);
		ObjSprite2D_SetDestCenter(obj);
		ObjRender_SetColor(obj,240,60,60);
		let alpha=0;
		let az=rand(0,360);
		loop(60*60)
		{
			if(alpha<256){alpha+=256/180;}
			ObjRender_SetAlpha(obj,alpha/2);
			ObjRender_SetScaleXYZ(obj,range1/512*2,range1/512*2,1);
			ObjRender_SetPosition(obj,FormX,FormY,0);
			ObjRender_SetAngleZ(obj,az+ang/4);
			yield;
		}
		loop(60)
		{
			alpha-=256/60;
			ObjRender_SetAlpha(obj,alpha);
			yield;
		}
		Obj_Delete(obj);
	}
	
	loop(60*60)
	{
		count++;
		if(range1>range2){range1-=range3/180;}
		aa=0;
		let c=1;
		loop(lp)
		{
			if(count%4==0){shottype1(FormX+range1*cos(ang+aa),FormY+range1*sin(ang+aa),2.4,ang+aa+90,20,5,30,240,60,60);}
			aa+=360/lp;
			c++;
		}
		if(count%60==0)
		{
			ag=rand(0,360);
			if(count==14*60){lpp+=6;}
			if(count==34*60){lpp+=6;}
			loop(lpp)
			{
				if(count<14*60){shottype1(BossObjX,BossObjY,0.6,ag,40,30,180,120,30,30);}
				if(count>=14*60 && count<44*60){shottype1(BossObjX,BossObjY,0.6,ag,0,30,180,120,30,30);}
				if(count>=44*60){shottype1(BossObjX,BossObjY,0.6,ag,10,30,180,120,30,30);}
				ag+=360/lpp;
			}
		}
		ang-=8*cangle;
		yield;
	}
}

task shottype1(x,y,s,a,st,w,t,r,g,b)
{
	let obj1;
	let obj2;
	let blend1=BLEND_ADD_ARGB;
	let blend2=BLEND_SUBTRACT;
	let color1=[r,g,b];
	let color2=[255,255,255];
	shot(1);
	shot(2);
	connection;
	task shot(type)
	{
		if(type==1)
		{
			obj1 = CreateShotA1(x,y,s,a,st+1,0);
			if(t<=60){StartSpawn(obj1,0,w,0,0,1,color1[0],color1[1],color1[2],blend1);}
			if(t>60){StartSpawn(obj1,0,w,1,1,1,color1[0],color1[1],color1[2],blend1);}
		}
		if(type==2)
		{
			obj2 = CreateShotA1(x,y,s,a,st,0);
			if(t<=60){StartSpawn(obj2,0,w,0,0,1,color2[0],color2[1],color2[2],blend2);}
			if(t>60){StartSpawn(obj2,0,w,1,1,1,color2[0],color2[1],color2[2],blend2);}
		}
	}
	task connection
	{
		loop
		{
			if(GetCommonData("bossend",false)==true){Obj_Delete(obj1);}
			if(Obj_IsDeleted(obj1)){Obj_Delete(obj2);}
			if(Obj_IsDeleted(obj2)){Obj_Delete(obj1);}
			ObjMove_SetPosition(obj1,ObjMove_GetX(obj2),ObjMove_GetY(obj2));
			ObjMove_SetAngle(obj1,ObjMove_GetAngle(obj2));
			yield;
		}
	}
	if(r>120){SEPlay(dir ~ "audio/e_shot_00.wav",40);}
	if(r<=120){SEPlay(dir ~ "audio/e_shot_01.wav",100);}
	let post=[ObjMove_GetX(obj2),ObjMove_GetY(obj2)];
	while(!Obj_IsDeleted(obj1) || !Obj_IsDeleted(obj2))
	{
		if(ObjMove_GetX(obj1)<0 || ObjMove_GetX(obj1)>GetStgFrameWidth || ObjMove_GetY(obj1)<0 || ObjMove_GetY(obj1)>GetStgFrameHeight){Obj_Delete(obj1);}
		ObjMove_SetSpeed(obj2,ObjMove_GetSpeed(obj2)+s/t);
		post=[ObjMove_GetX(obj2),ObjMove_GetY(obj2)];
		yield;
	}
	itemdrop(post[0],post[1],500);
}