#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(10000);
	}
}

@MainLoop 
{
	ObjEnemy_SetDamageRate(BossObj,200,180);
	ObjRender_SetAlpha(ImgBoss,BossAlpha);
	BossObjX = ObjMove_GetX(BossObj);
	BossObjY = ObjMove_GetY(BossObj);
	ObjRender_SetPosition(ImgBoss,BossObjX,BossObjY,0);
	if(BossHitbox==true)
	{
		ObjEnemy_SetIntersectionCircleToShot(BossObj,BossObjX,BossObjY,24);
		ObjEnemy_SetIntersectionCircleToPlayer(BossObj,BossObjX,BossObjY,32);
	}
	yield;
} 

@Finalize 
{ 
} 

task mainTask
{
	idlesprite(ImgBoss,180,imgBossObj);
	loop(60){yield;}
	BossHitbox=true;
	ObjEnemyBossScene_SetSpellTimer(objScene,60*60);
	
	mainshooter;
	move;
	TEnd;
}

task TEnd
{
	while(ObjEnemy_GetInfo(BossObj, INFO_LIFE) > 0){yield;}
	Ring (BossObjX,BossObjY,60,255,255,255,BLEND_SUBTRACT,900,60,1);
	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);}
	loop(60)
	{
		ObjMove_SetDestAtSpeed(BossObj,BossObjX+r*sin(angle),BossObjY+r*cos(angle),sp);
		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 move
{
	task movement(scale)
	{
		let xx=GetPlayerX;
		let angle=rand(-90,90);
		let direction=1;
		if(BossObjX<=GetPlayerX){direction=1;}
		if(BossObjX>GetPlayerX){direction=-1;}
		angle=90*direction+angle/3;
		if(BossObjY<GetStgFrameHeight/2-180){angle-=30*direction;}
		if(BossObjY>GetStgFrameHeight/2-90){angle+=30*direction;}
		let spd=(GetDistance(GetPlayerX,GetPlayerY,BossObjX,BossObjY)/60)*rand(0.9,1.1)*scale;
		let sp=spd;
		let r=spd/2*60;
		if(BossObjX+r*sin(angle)<=BossObjX){movesprite(ImgBoss,60,imgBossObj,1);}
		if(BossObjX+r*sin(angle)>BossObjX){movesprite(ImgBoss,60,imgBossObj,-1);}
		loop(60)
		{
			if(ObjEnemy_GetInfo(BossObj, INFO_LIFE) > 0){ObjMove_SetDestAtSpeed(BossObj,BossObjX+r*sin(angle),BossObjY+r*cos(angle),sp);}
			sp-=spd/60;
			yield;
		}
		ObjMove_SetSpeed(BossObj,0);
	}
	loop(420){yield;}
	while(ObjEnemy_GetInfo(BossObj, INFO_LIFE) > 0)
	{
		movement(0.6);
		loop(120){yield;}
		movement(0.8);
		loop(360){yield;}
	}
}

task mainshooter
{
	let cangle=1;
	let angle=rand(0,360);
	let time=1;
	let lp=1;
	let c=[255,255,255];
	idlesprite(ImgBoss,360,imgBossObj);
	while(ObjEnemy_GetInfo(BossObj, INFO_LIFE) > 0)
	{
		time=60;
		loop(60)
		{
			if(GetCommonData("bossend",false)==false){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;
		}
		c=[240,30,30];
		if(BossObjX>GetStgFrameWidth/3 && BossObjX<GetStgFrameWidth/3*2)
		{
			shottype2(BossObjX,BossObjY,2.4,rand(-60,-30),60,40,30,120,c[0],c[1],c[2]);
			shottype2(BossObjX,BossObjY,2.4,rand(210,240),60,40,30,120,c[0],c[1],c[2]);
		}
		if(BossObjX<=GetStgFrameWidth/3)
		{
			shottype2(BossObjX,BossObjY,2.4,rand(-67.5,-45),60,40,30,120,c[0],c[1],c[2]);
			shottype2(BossObjX,BossObjY,2.4,rand(-45,-22.5),60,40,30,120,c[0],c[1],c[2]);
		}
		if(BossObjX>=GetStgFrameWidth/3*2)
		{
			shottype2(BossObjX,BossObjY,2.4,rand(225,247.5),60,40,30,120,c[0],c[1],c[2]);
			shottype2(BossObjX,BossObjY,2.4,rand(202.5,225),60,40,30,120,c[0],c[1],c[2]);
		}
		loop(180){yield;}
		time=60;
		loop(60)
		{
			if(GetCommonData("bossend",false)==false){Charge(BossObjX,BossObjY,rand(time,time*4),time,255,255,255,30,BLEND_SUBTRACT,-1,-cangle*rand(0,3),rand(0,360),rand(60,120));}
			time--;
			yield;
		}
		if(dif==1){time=7;}
		if(dif==2){time=10;}
		if(dif==3){time=13;}
		if(dif==1){lp=3;}
		if(dif==2){lp=2;}
		if(dif==3){lp=1;}
		loop(time)
		{
			loop(lp)
			{
				c=[240,60,60];
				shottype1(BossObjX,BossObjY,2.4,angle,0,30,120,c[0],c[1],c[2]);
				if(dif==1)
				{
					shottype1(BossObjX,BossObjY,2.1,angle-90/time*cangle,0,30,90,c[0],c[1],c[2]);
					shottype1(BossObjX,BossObjY,2.1,angle+90/time*cangle,0,30,90,c[0],c[1],c[2]);
				}
				if(dif==2)
				{
					shottype1(BossObjX,BossObjY,2.1,angle-60/time*cangle,0,30,100,c[0],c[1],c[2]);
					shottype1(BossObjX,BossObjY,2.1,angle+60/time*cangle,0,30,100,c[0],c[1],c[2]);
					shottype1(BossObjX,BossObjY,1.8,angle-120/time*cangle,0,30,80,c[0],c[1],c[2]);
					shottype1(BossObjX,BossObjY,1.8,angle+120/time*cangle,0,30,80,c[0],c[1],c[2]);
				}
				if(dif==3)
				{
					shottype1(BossObjX,BossObjY,2.1,angle-90/time*cangle,0,30,105,c[0],c[1],c[2]);
					shottype1(BossObjX,BossObjY,2.1,angle+90/time*cangle,0,30,105,c[0],c[1],c[2]);
					shottype1(BossObjX,BossObjY,1.8,angle-180/time*cangle,0,30,90,c[0],c[1],c[2]);
					shottype1(BossObjX,BossObjY,1.8,angle+180/time*cangle,0,30,90,c[0],c[1],c[2]);
					shottype1(BossObjX,BossObjY,1.5,angle-90/time*cangle,0,30,75,c[0],c[1],c[2]);
					shottype1(BossObjX,BossObjY,1.5,angle+90/time*cangle,0,30,75,c[0],c[1],c[2]);
				}
				angle+=360/lp;
			}
			angle+=360/time*cangle;
			loop(60/time){yield;}
		}
		if(GetCommonData("bossend",false)==false){idlesprite(ImgBoss,480,imgBossObj);}
		loop(120){yield;}
		cangle*=-1;
	}
}

task shottype1(x,y,s,a,st,w,t,r,g,b)
{
	let yspd=s/120;
	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 = CreateShotB2(x,y,0,0,0,0,0,999,st+1,0);
			if(t>0){StartSpawn(obj1,0,w,0,0,1,color1[0],color1[1],color1[2],blend1);}
			if(t<=0){StartSpawn(obj1,0,w,1,1,1,color1[0],color1[1],color1[2],blend1);}
		}
		if(type==2)
		{
			obj2 = CreateShotB2(x,y,s*cos(a),s*sin(a),0,yspd,s*cos(a),999,st,0);
			if(t>0){StartSpawn(obj2,0,w,0,0,1,color2[0],color2[1],color2[2],blend2);}
			if(t<=0){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(!Obj_IsDeleted(obj1)){SEPlay(dir ~ "audio/e_shot_00.wav",60);}
	if(t>0)
	{
		let color=1;
		loop(t)
		{
			if(color>0){color-=1/(t-15);}
			ObjRender_SetColor(obj2,color2[0]*color,color2[1]*color,color2[2]*color);
			yield;
		}
		if(!Obj_IsDeleted(obj1) && t>0 && ObjMove_GetY(obj2)<GetStgFrameHeight)
		{
			let direction=15;
			shottype1(ObjMove_GetX(obj2),ObjMove_GetY(obj2),s,270-direction,st,30,0,r*2,g*2,b*2);
			shottype1(ObjMove_GetX(obj2),ObjMove_GetY(obj2),s,270+direction,st,30,0,r*2,g*2,b*2);
			Obj_Delete(obj1);
		}
	}
	if(t<=0)
	{
		let post;
		while(!Obj_IsDeleted(obj1) || !Obj_IsDeleted(obj2))
		{
			post=[ObjMove_GetX(obj2),ObjMove_GetY(obj2)];
			yield;
		}
		itemdrop(post[0],post[1],500);
	}
}
task shottype2(x,y,s,a,st1,st2,w,t,r,g,b)
{
	let yspd=s/60;
	if(t<=0){yspd=s/240;}
	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)
		{
			if(st1>=50){obj1 = CreateShotB2(x,y,s*cos(a),s*sin(a),0,yspd,s*cos(a),999,st1,0);}
			if(st1<50){obj1 = CreateShotB2(x,y,s*cos(a),s*sin(a),0,yspd,s*cos(a),999,st1+1,0);}
			if(t>0){StartSpawn(obj1,0,w,0,0,1,color1[0],color1[1],color1[2],blend1);}
			if(t<=0){StartSpawn(obj1,0,w,1,1,1,color1[0],color1[1],color1[2],blend1);}
		}
		if(type==2)
		{
			if(st1>=50){obj2 = CreateShotB2(x,y,s*cos(a),s*sin(a),0,yspd,s*cos(a),999,st1+1,0);}
			if(st1<50){obj2 = CreateShotB2(x,y,s*cos(a),s*sin(a),0,yspd,s*cos(a),999,st1,0);}
			if(t>0){StartSpawn(obj2,0,w,0,0,1,color2[0],color2[1],color2[2],blend2);}
			if(t<=0){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);}
			yield;
		}
	}
	if(!Obj_IsDeleted(obj1) && t>0){SEPlay(dir ~ "audio/e_shot_00.wav",100);}
	if(!Obj_IsDeleted(obj1) && t<=0){SEPlay(dir ~ "audio/e_shot_00.wav",60);}
	if(t>0)
	{
		let color=1;
		loop(t)
		{
			if(color>0){color-=1/(t-15);}
			ObjRender_SetColor(obj2,color2[0]*color,color2[1]*color,color2[2]*color);
			yield;
		}
		if(t>0 && !Obj_IsDeleted(obj1) || !Obj_IsDeleted(obj2) )
		{
			let xx=ObjMove_GetX(obj1);
			let yy=ObjMove_GetY(obj1);
			let direction=rand(0,360);
			let lp=1;
			if(dif==1){lp=14;}
			if(dif==2){lp=10;}
			if(dif==3){lp=6;}
			loop(lp)
			{
				let s=1.2;
				if(dif>=1)
				{
					shottype2(xx,yy,s,direction,st2,st1,w,0,r,g,b);
					shottype2(xx,yy,s,direction+20/lp,st2,st1,w,0,r,g,b);
					shottype2(xx,yy,s,direction-20/lp,st2,st1,w,0,r,g,b);
				}
				if(dif>=2)
				{
					shottype2(xx,yy,s,direction+40/lp,st2,st1,w,0,r,g,b);
					shottype2(xx,yy,s,direction-40/lp,st2,st1,w,0,r,g,b);
					shottype2(xx,yy,s,direction+60/lp,st2,st1,w,0,r,g,b);
					shottype2(xx,yy,s,direction-60/lp,st2,st1,w,0,r,g,b);
				}
				if(dif>=3)
				{
					shottype2(xx,yy,s,direction+80/lp,st2,st1,w,0,r,g,b);
					shottype2(xx,yy,s,direction-80/lp,st2,st1,w,0,r,g,b);
					shottype2(xx,yy,s,direction+100/lp,st2,st1,w,0,r,g,b);
					shottype2(xx,yy,s,direction-100/lp,st2,st1,w,0,r,g,b);
				}
				direction+=360/lp;
			}
			Obj_Delete(obj1);
			Obj_Delete(obj2);
		}
	}
	if(t<=0)
	{
		let post;
		while(!Obj_IsDeleted(obj1) || !Obj_IsDeleted(obj2))
		{
			post=[ObjMove_GetX(obj1),ObjMove_GetY(obj1)];
			yield;
		}
		itemdrop(post[0],post[1],500);
	}
}