#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,190,160);
	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,300,imgBossObj);
	loop(60){yield;}
	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;
	}
	let ang=rand(0,360);
	SpellRemain(1,4,ang,BossObj,0,1);
	SpellRemain(2,4,ang+90,BossObj,0,0);
	SpellRemain(3,4,ang+180,BossObj,0,0);
	SpellRemain(4,4,ang+270,BossObj,0,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,0,1024,64,BossObj,0,240/512);
	loop(120){yield;}
	BossHitbox=true;
	ObjEnemyBossScene_SetSpellTimer(objScene,64*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,type)
	{
		let xx=GetPlayerX;
		let angle=rand(-90,90);
		let direction=1;
		if(BossObjX<=GetPlayerX){direction=-1*type;}
		if(BossObjX>GetPlayerX){direction=1*type;}
		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(360){yield;}
	while(ObjEnemy_GetInfo(BossObj, INFO_LIFE) > 0)
	{
		movement(0.8,-1);
		loop(240){yield;}
		movement(0.2,1);
		loop(300){yield;}
	}
}

task mainshooter
{
	let cangle=1;
	let angle=rand(0,360);
	let time=1;
	let lp=1;
	let c=[255,255,255];
	let cc=1;
	idlesprite(ImgBoss,480,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,60,60];
		if(dif==1){lp=12;}
		if(dif==2){lp=18;}
		if(dif==3){lp=24;}
		angle=-atan2(GetPlayerY-BossObjY,GetPlayerX-BossObjX)+90-(180-72)*cangle;
		let sp=0.6;
		loop(5)
		{
			if(sp>1.2){sp=0.6;}
			cc=0;
			loop(lp)
			{
				shottype1(BossObjX,BossObjY,1.8,sp,angle,-cangle,360/lp*cc,0.2,0,30,60,c[0],c[1],c[2]);
				cc++;
			}
			sp+=1.2;
			angle-=36*cangle;
		}
		angle=rand(0,360);
		loop(lp-3)
		{
			shottype1(BossObjX,BossObjY,0.15,1.2,angle,0,0,0,60,30,0,c[0],c[1],c[2]);
			if(dif<=2){shottype1(BossObjX,BossObjY,0.3,1.2,angle,0,0,0,60,30,0,c[0],c[1],c[2]);}
			if(dif<=1){shottype1(BossObjX,BossObjY,0.45,1.2,angle,0,0,0,60,30,0,c[0],c[1],c[2]);}
			angle+=360/(lp-3);
		}
		loop(240){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,rand(-3,3),rand(0,360),rand(60,120));}
			time--;
			yield;
		}
		sp=0.3;
		loop(4)
		{
			angle=-atan2(GetPlayerY-BossObjY,GetPlayerX-BossObjX)+90;
			cc=0;
			loop(lp)
			{
				shottype1(BossObjX,BossObjY,3.6,sp,angle,-cangle,360/lp*cc,0.1,0,30,60,c[0],c[1],c[2]);
				cc++;
			}
			sp+=0.3;
			loop(15){yield;}
		}
		loop(60){yield;}
		if(GetCommonData("bossend",false)==false){idlesprite(ImgBoss,540,imgBossObj);}
		loop(60){yield;}
		cangle*=-1;
	}
}

task shottype1(x,y,s,s2,a,av,ang,distance,st,w,t,r,g,b)
{
	let obj1;
	let obj2;
	let blend1=BLEND_ADD_ARGB;
	let blend2=BLEND_SUBTRACT;
	let color1=[r,g,b];
	if(t<=0){color1=[r/2,g/2,b/2];}
	let color2=[255,255,255];
	shot(1);
	shot(2);
	connection;
	task shot(type)
	{
		if(type==1)
		{
			if(st<50){obj1 = CreateShotA1(x,y,0,a,st+1,0);}
			if(st>=50){obj1 = CreateShotA1(x,y,s,a,st,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(st<50){obj2 = CreateShotA1(x,y,0,a,st,0);}
			if(st>=50){obj2 = CreateShotA1(x,y,s,a,st+1,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(obj2);}
			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 cspin=0;
		let range=0;
		let angle=ang;
		let post=[ObjMove_GetX(obj2),ObjMove_GetY(obj2),ObjMove_GetAngle(obj2)];
		let dist=0;
		while(post[0]>dist && post[0]<GetStgFrameWidth-dist && post[1]>dist && post[1]<GetStgFrameHeight-dist)
		{
			if(cspin<1){cspin+=1/t;}
			range+=s;
			angle+=av*cspin;
			ObjRender_SetPosition(obj2,x+range*sin(a),y+range*cos(a),0);
			ObjRender_SetPosition(obj2,ObjMove_GetX(obj2)+range*distance*sin(angle),ObjMove_GetY(obj2)+range*distance*cos(angle),0);
			ObjMove_SetAngle(obj2,atan2(ObjMove_GetY(obj2)-(y+range*cos(a)),ObjMove_GetX(obj2)-(x+range*sin(a))));
			post=[ObjMove_GetX(obj2),ObjMove_GetY(obj2),ObjMove_GetAngle(obj2)];
			yield;
		}
		Obj_Delete(obj2);
		let direction=0;
		if(GetCommonData("bossend",false)==false)
		{
			let lp=1;
			let range=1;
			if(dif==1)
			{
				lp=6;
				range=1;
			}
			if(dif==2)
			{
				lp=5;
				range=2;
			}
			if(dif==3)
			{
				lp=4;
				range=3;
			}
			loop(lp)
			{
				shottype1(post[0],post[1],s2,s,post[2]+direction,0,0,0,st,30,0,r,g,b);
				direction+=(360*range)/lp;
			}
		}
	}
	if(t<=0)
	{
		let post=[ObjMove_GetX(obj2),ObjMove_GetY(obj2)];
		while(!Obj_IsDeleted(obj1) || !Obj_IsDeleted(obj2))
		{
			ObjMove_SetSpeed(obj2,ObjMove_GetSpeed(obj2)+s/240);
			post=[ObjMove_GetX(obj2),ObjMove_GetY(obj2)];
			yield;
		}
		itemdrop(post[0],post[1],500);
	}
}