#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,180,140);
	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(540){yield;}
	while(ObjEnemy_GetInfo(BossObj, INFO_LIFE) > 0)
	{
		movement(0.6);
		loop(360){yield;}
	}
}

task mainshooter
{
	let cangle=1;
	let angle=rand(0,360);
	let time;
	let lp=1;
	let c=[255,255,255];
	let av=1;
	let delay=60;
	let dt=60;
	idlesprite(ImgBoss,180,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;
		}
		if(dif==1){lp=5;}
		if(dif==2){lp=7;}
		if(dif==3){lp=13;}
		angle=atan2(GetPlayerY-BossObjY,GetPlayerX-BossObjX);
		c=[240,60,60];
		loop(lp)
		{
			shottype1(BossObjX,BossObjY,6,angle+180,60,0,30,30,c[0],c[1],c[2]);
			shottype1(BossObjX,BossObjY,6,angle+180,60,0,30,60,c[0],c[1],c[2]);
			shottype1(BossObjX,BossObjY,6,angle+180,60,0,30,90,c[0],c[1],c[2]);
			shottype1(BossObjX,BossObjY,6,angle+180,60,0,30,120,c[0],c[1],c[2]);
			angle+=360/lp;
		}
		av=6;
		delay=60;
		let caa=1/3;
		if(dif==1)
		{
			let aa=144*caa;
			let ca=1;
			loop(2)
			{
				loop(2)
				{
					laser(BossObjX,BossObjY,angle+aa*cangle*ca,-(delay*av/2+av/2),av,1,dt,20,delay);
					laser(BossObjX,BossObjY,angle+aa*cangle*ca,(delay*av/2+av/2),av,-1,dt,20,delay);
					loop(15){yield;}
					ca*=-1;
				}
				aa-=72*caa;
			}
			laser(BossObjX,BossObjY,angle,-(delay*av/2+av/2),av,1,delay,20,60);
			laser(BossObjX,BossObjY,angle,(delay*av/2+av/2),av,-1,delay,20,60);
		}
		if(dif==2)
		{
			let aa=135*caa;
			let ca=1;
			loop(3)
			{
				loop(2)
				{
					laser(BossObjX,BossObjY,angle+aa*cangle*ca,-(delay*av/2+av/2),av,1,dt,20,delay);
					laser(BossObjX,BossObjY,angle+aa*cangle*ca,(delay*av/2+av/2),av,-1,dt,20,delay);
					loop(10){yield;}
					ca*=-1;
				}
				aa-=45*caa;
			}
			laser(BossObjX,BossObjY,angle,-(delay*av/2+av/2),av,1,delay,20,60);
			laser(BossObjX,BossObjY,angle,(delay*av/2+av/2),av,-1,delay,20,60);
		}
		if(dif==3)
		{
			let aa=(180-180/7)*caa;
			let ca=1;
			loop(6)
			{
				loop(2)
				{
					laser(BossObjX,BossObjY,angle+aa*cangle*ca,-(delay*av/2+av/2),av,1,dt,20,delay);
					laser(BossObjX,BossObjY,angle+aa*cangle*ca,(delay*av/2+av/2),av,-1,dt,20,delay);
					loop(5){yield;}
					ca*=-1;
				}
				aa-=(180/7)*caa;
			}
			laser(BossObjX,BossObjY,angle,-(delay*av/2+av/2),av,1,delay,20,60);
			laser(BossObjX,BossObjY,angle,(delay*av/2+av/2),av,-1,delay,20,60);
		}
		idlesprite(ImgBoss,360,imgBossObj);
		loop(240){yield;}
		cangle*=-1;
	}
}

task laser(x,y,a,angle,av,ang,dt,st,d)
{
	let l=640;
	let w=64;
	dt=120;
	let obj = CreateStraightLaserA1(x,y,a,l,w,dt-15,st,d+15);
	if(GetCommonData("bossend",false)==true){Obj_Delete(obj);}
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	ObjShot_SetIntersectionEnable(obj,false);
	ObjShot_SetItemChange(obj,false);
	ObjShot_SetSpellResist(obj,true);
	animation;
	task animation
	{
		let aa=a;
		let obj1= ObjPrim_Create(OBJ_SPRITE_2D);
		let obj2= ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(obj1,dir ~ "graphic/laser.png");
		ObjPrim_SetTexture(obj2,dir ~ "graphic/laser.png");
		ObjSprite2D_SetSourceRect(obj1,0,256,512,512);
		ObjSprite2D_SetSourceRect(obj2,0,0,512,256);
		ObjSprite2D_SetDestRect(obj1,-64,-128,448,128);
		ObjSprite2D_SetDestRect(obj2,-64,-128,448,128);
		ObjRender_SetColor(obj1,240,60,60);
		ObjRender_SetBlendType(obj1,BLEND_ADD_ARGB);
		ObjRender_SetBlendType(obj2,BLEND_ADD_ARGB);
		let alpha=1;
		let calpha=1;
		let xyz=2;
		let cxyz=1;
		while(alpha>0)
		{
			if(alpha>=256){calpha*=-1;}
			if(xyz<=1){cxyz*=-1;}
			alpha+=256/d*calpha;
			xyz-=1/d*cxyz;
			ObjRender_SetAlpha(obj1,alpha/2);
			ObjRender_SetAlpha(obj2,alpha/2);
			ObjRender_SetScaleXYZ(obj1,256/512*xyz,128/256*xyz,1);
			ObjRender_SetScaleXYZ(obj2,256/512*xyz,128/256*xyz,1);
			ObjRender_SetAngleZ(obj1,aa);
			ObjRender_SetAngleZ(obj2,aa);
			ObjRender_SetPosition(obj1,ObjMove_GetX(obj),ObjMove_GetY(obj),0);
			ObjRender_SetPosition(obj2,ObjMove_GetX(obj),ObjMove_GetY(obj),0);
			if(GetCommonData("bossend",false)==true && calpha>0 && xyz<2)
			{
				calpha=-1;
				cxyz=1;
			}
			yield;
		}
		Obj_Delete(obj1);
		Obj_Delete(obj2);
	}
	let cang=1;
	let al=0;
	let ca=1;
	disappearing;
	loop(d+15)
	{
		ObjRender_SetAlpha(obj,al*ca);
		ObjStLaser_SetAngle(obj,a+angle);
		ObjLaser_SetLength(obj,l*(1-cang));
		ObjLaser_SetRenderWidth(obj,w*8*(1-cang));
		if(al<256){al+=256/d;}
		if(al<256){a+=av*ang*cang;}
		if(cang>0){cang-=1/d;}
		yield;
	}
	if(!Obj_IsDeleted(obj)){SEPlay(dir ~ "audio/laser_3.wav",70);}
	ObjLaser_SetLength(obj,l);
	ObjLaser_SetRenderWidth(obj,w);
	ObjStLaser_SetAngle(obj,a+angle);
	ObjShot_SetIntersectionEnable(obj,true);
	cang=1;
	loop(dt-15)
	{
		ObjLaser_SetRenderWidth(obj,w*cang);
		ObjLaser_SetIntersectionWidth(obj,(w/4)*cang);
		if((w/4)*cang<8){ObjShot_SetIntersectionEnable(obj,false);}
		cang-=1/(dt-15);
		yield;
	}
	Obj_Delete(obj);
	task disappearing
	{
		while(GetCommonData("bossend",false)==false){yield;}
		while(ca>0)
		{
			ObjShot_SetIntersectionEnable(obj,false);
			ObjRender_SetAlpha(obj,al*ca);
			ca-=1/60;
			yield;
		}
		Obj_Delete(obj);
	}
}
task shottype1(x,y,s,a,st1,st2,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)
		{
			if(st1<50){obj1 = CreateShotA1(x,y,s,a,st1+1,0);}
			if(st1>=50){obj1 = CreateShotA1(x,y,s,a,st1,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 = CreateShotA1(x,y,s,a,st1,0);}
			if(st1>=50){obj2 = CreateShotA1(x,y,s,a,st1+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(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) && 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)
	{
		ObjMove_SetSpeed(obj2,s);
		let color=1;
		loop(t)
		{
			if(color>0){color-=1/(t-15);}
			ObjMove_SetSpeed(obj2,ObjMove_GetSpeed(obj2)-s/t);
			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(obj2);
			let yy=ObjMove_GetY(obj2);
			let direction=rand(0,360);
			let lp=7;
			loop(lp)
			{
				let s=0.6;
				shottype1(xx,yy,s,direction,st2,st1,w,0,r,g,b);
				shottype1(xx,yy,s/2,direction,st2,st1,w,0,r/2,g/2,b/2);
				direction+=360/lp;
			}
			Obj_Delete(obj1);
			Obj_Delete(obj2);
		}
	}
	if(t<=0)
	{
		let post;
		while(!Obj_IsDeleted(obj1) || !Obj_IsDeleted(obj2))
		{
			ObjMove_SetSpeed(obj2,ObjMove_GetSpeed(obj2)+s/120);
			post=[ObjMove_GetX(obj2),ObjMove_GetY(obj2)];
			yield;
		}
		itemdrop(post[0],post[1],500);
	}
}