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

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

let BossRadius = 0;
let BulletChange = false;

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,170,120);
	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,240,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,1);
	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",410,60,0,64,1024,128,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;}
	BulletChange=false;
	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(420){yield;}
	while(ObjEnemy_GetInfo(BossObj, INFO_LIFE) > 0)
	{
		movement(0.8,-1);
		loop(60){yield;}
		if(GetCommonData("bossend",false)==false)
		{
			SEPlay(dir ~ "audio/soukou_on.wav",100);
			BulletChange=true;
			Ring (BossObjX,BossObjY,120,255,255,255,BLEND_SUBTRACT,900,60,1);
			loop(120)
			{
				BossRadius+=450/120;
				yield;
			}
			BulletChange=false;
			BossRadius=0;
		}
		if(GetCommonData("bossend",false)==true){loop(120){yield;}}
		loop(180){yield;}
	}
}

task mainshooter
{
	let xx=0;
	let cangle=1;
	let lp=1;
	if(dif==1){lp=4;}
	if(dif==2){lp=6;}
	if(dif==3){lp=8;}
	let time=60;
	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;
		}
		xx=GetStgFrameWidth/(lp+1);
		if(cangle==-1){xx=GetStgFrameWidth-GetStgFrameWidth/(lp+1);}
		loop(lp)
		{
			if(GetCommonData("bossend",false)==false){shottype1(xx,0,6,rand(0,360),60,30,60,240,60,60,0);}
			loop(30){yield;}
			xx+=GetStgFrameWidth/lp*cangle;
		}
		loop(10-lp){loop(30){yield;}}
		idlesprite(ImgBoss,360,imgBossObj);
		cangle*=-1;
	}
}

task shottype1(x,y,s,a,st,w,t,r,g,b,type)
{
	let obj1;
	let obj2;
	let blend1=BLEND_ADD_ARGB;
	let blend2=BLEND_SUBTRACT;
	let color1=[r,g,b];
	if(type==1){color1=[r/2,g/2,b/2];}
	let color2=[255,255,255];
	shot(1);
	shot(2);
	connection;
	task shot(typeX)
	{
		if(typeX==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(type==1){StartSpawn(obj1,0,w,0,0,0,color1[0],color1[1],color1[2],blend1);}
			if(type==0){StartSpawn(obj1,0,w,0,0,0,color1[0],color1[1],color1[2],blend1);}
		}
		if(typeX==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(type==1){StartSpawn(obj2,0,w,1,1,1,color2[0],color2[1],color2[2],blend2);}
			if(type==0){StartSpawn(obj2,0,w,0,0,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(type==0)
	{
		let count=0;
		let cs=1;
		while(count<3)
		{
			let ag=rand(0,360);
			let lp=5;
			if(dif==2){lp=7;}
			if(dif==3){lp=9;}
			loop(lp)
			{
				if(GetCommonData("bossend",false)==false){shottype1(ObjMove_GetX(obj2),ObjMove_GetY(obj2),0.3,ag,10,30,240,r,g,b,1);}
				ag+=360/lp;
			}
			ObjMove_SetSpeed(obj2,s*cs);
			loop(t)
			{
				let ang=atan2(GetPlayerY-ObjMove_GetY(obj2),GetPlayerX-ObjMove_GetX(obj2));
				ObjMove_SetAngle(obj2,ang);
				ObjMove_SetSpeed(obj2,ObjMove_GetSpeed(obj2)-(s*cs)/t);
				yield;
			}
			ObjMove_SetSpeed(obj2,0);
			count++;
			cs-=1/12;
		}
		let al=256;
		let xyz=1;
		ObjShot_SetIntersectionEnable(obj2,false);
		loop(60)
		{
			al-=256/60;
			xyz+=1/60;
			ObjRender_SetAlpha(obj1,al);
			ObjRender_SetAlpha(obj2,al);
			ObjRender_SetScaleXYZ(obj1,xyz,xyz,1);
			ObjRender_SetScaleXYZ(obj2,xyz,xyz,1);
			yield;
		}
		Obj_Delete(obj2);
	}
	if(type==1)
	{
		let count=0;
		let post=[ObjMove_GetX(obj2),ObjMove_GetY(obj2)];
		let bulletform=0;
		let cs=1;
		while(!Obj_IsDeleted(obj1) || !Obj_IsDeleted(obj2))
		{
			if(BulletChange==false){bulletform=0;}
			ObjMove_SetSpeed(obj2,(ObjMove_GetSpeed(obj2)+s/t*cs));
			let dis=GetDistance(BossObjX,BossObjY,ObjMove_GetX(obj2),ObjMove_GetY(obj2));
			if(BulletChange==true && dis<BossRadius)
			{
				if(bulletform==0)
				{
					if(st==40){st=60;cs=0.25;}
					if(st==0){st=40;cs=0.5;}
					if(st==10){st=0;cs=0.75;}
					if(!Obj_IsDeleted(obj1)){SEPlay(dir ~ "audio/e_shot_00.wav",60);}
					bulletform=1;
				}
				if(st<50)
				{
					ObjShot_SetGraphic(obj1,st+1);
					ObjShot_SetGraphic(obj2,st);
				}
				if(st>=50)
				{
					ObjShot_SetGraphic(obj1,st);
					ObjShot_SetGraphic(obj2,st+1);
				}
			}
			post=[ObjMove_GetX(obj2),ObjMove_GetY(obj2)];
			count++;
			yield;
		}
		itemdrop(post[0],post[1],500);
	}
}