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

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

let BossAlpha = 255;
let BossHitbox = false;

let BossPhase=1;
let TimeCount=60*60;

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

@MainLoop 
{
	ObjEnemy_SetDamageRate(BossObj,160,100);
	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,1);
	SpellRemain(4,4,ang+270,BossObj,0,1);
	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",500,60,0,192,1024,256,BossObj,0,240/512);
	loop(120){yield;}
	BossHitbox=true;
	ObjEnemyBossScene_SetSpellTimer(objScene,60*60);
	
	countdown;
	mainshooter;
	TEnd;
}

task countdown
{
	while(TimeCount>0)
	{
		TimeCount--;
		yield;
	}
}

task TEnd
{
	while(ObjEnemy_GetInfo(BossObj, INFO_LIFE) > 0){yield;}
	let time=60;
	loop(12)
	{
		SEPlay(dir ~ "audio/boss_end.wav",100);
		let a=rand(0,360);
		Ring (BossObjX+64*sin(a),BossObjY+64*cos(a),60,255,255,255,BLEND_SUBTRACT,450,60,1);
		loop(5){yield;}
		StartSlow(TARGET_ALL,time);
		time-=5;
	}
	StopSlow(TARGET_ALL);
	SEPlay(dir ~ "audio/boss_end.wav",100);
	Ring (BossObjX,BossObjY,120,255,255,255,BLEND_SUBTRACT,900,128,1);
	Ring (BossObjX+64*sin(0),BossObjY+64*cos(0),120,255,255,255,BLEND_SUBTRACT,900,128,1);
	Ring (BossObjX+64*sin(90),BossObjY+64*cos(90),120,255,255,255,BLEND_SUBTRACT,900,128,1);
	Ring (BossObjX+64*sin(180),BossObjY+64*cos(180),120,255,255,255,BLEND_SUBTRACT,900,128,1);
	Ring (BossObjX+64*sin(-90),BossObjY+64*cos(-90),120,255,255,255,BLEND_SUBTRACT,900,128,1);
	SetCommonData("bossend",true);
	BossHitbox=false;
	let range=0;
	loop(120)
	{
		range+=5;
		DeleteShotInCircle(TYPE_SHOT,TYPE_FADE,BossObjX,BossObjY,range);
		ObjMove_SetSpeed(BossObj,0);
		if(BossAlpha>0){BossAlpha-=256/30;}
		yield;
	}
	BossAlpha=0;
	SetCommonData("creditroll",true);
	SetCommonData("bossend",false);
	Obj_Delete(BossObj);
	Obj_Delete(ImgBoss);
	CloseScript(GetOwnScriptID());
}

task mainshooter
{
	let a=rand(0,360);
	let aa=rand(0,360);
	let lp=1;
	if(dif==1){lp=1;}
	if(dif==2){lp=3;}
	if(dif==3){lp=5;}
	let count=0;
	let time=0;
	let wait=0;
	let cangle=1;
	phasecheck;
	task phasecheck
	{
		while(BossPhase==1)
		{
			if(ObjEnemy_GetInfo(BossObj,INFO_LIFE)<15000){BossPhase=2;}
			if(TimeCount<45*60){BossPhase=2;}
			yield;
		}
		lp++;
		count=0;
		BossHitbox=false;
		while(BossPhase==2)
		{
			if(ObjEnemy_GetInfo(BossObj,INFO_LIFE)<10000){BossPhase=3;}
			if(TimeCount<30*60){BossPhase=3;}
			yield;
		}
		lp++;
		count=0;
		BossHitbox=false;
		while(BossPhase==3)
		{
			if(ObjEnemy_GetInfo(BossObj,INFO_LIFE)<5000){BossPhase=4;}
			if(TimeCount<15*60){BossPhase=4;}
			yield;
		}
		count=0;
		BossHitbox=false;
		while(BossPhase==4){yield;}
	}
	while(GetCommonData("bossend",false)==false)
	{
		wait=150-30*BossPhase;
		idlesprite(ImgBoss,wait+60,imgBossObj);
		if(count==0)
		{
			SEPlay(dir ~ "audio/charge_1.wav",100);
			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;
			}
			BossHitbox=true;
		}
		count++;
		let xx=320+(320/32*truncate(rand(0,32-4*BossPhase)))*cangle;
		let so=3;
		let spd=1;
		let ang=15;
		loop(lp)
		{
			shottype1(xx,GetStgFrameHeight,rand(so+spd*2,so+spd*3),-90+rand(-ang,ang),40,30,240,60,60);
			shottype1(xx,GetStgFrameHeight,rand(so+spd*3,so+spd*4),-90+rand(-ang,ang),0,30,240,60,60);
			shottype1(xx,GetStgFrameHeight,rand(so+spd*3,so+spd*4),-90+rand(-ang,ang),0,30,240,60,60);
			shottype1(xx,GetStgFrameHeight,rand(so+spd,so+spd*2),-90+rand(-ang,ang),60,30,240,60,60);
			shottype1(xx,GetStgFrameHeight,rand(so+spd*2,so+spd*3),-90+rand(-ang,ang),40,30,240,60,60);
			shottype1(xx,GetStgFrameHeight,rand(so+spd*3,so+spd*4),-90+rand(-ang,ang),0,30,240,60,60);
		}
		loop(wait){if(count>0){yield;}}
		cangle*=-1;
	}
}

task shottype1(x,y,s,a,st,w,r,g,b)
{
	let ytime=rand(60,180);
	let yspd=s/ytime;
	let ycount=s;
	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(typeX)
	{
		if(typeX==1)
		{
			if(st<50){obj1 = CreateShotB2(x,y,0,0,0,0,0,0,st+1,0);}
			if(st>=50){obj1 = CreateShotB2(x,y,0,0,0,0,0,0,st,0);}
			StartSpawn(obj1,0,w,0,0,0,color1[0],color1[1],color1[2],blend1);
		}
		if(typeX==2)
		{
			if(st<50){obj2 = CreateShotB2(x,y,s*cos(a),s*sin(a),0,yspd,s*cos(a),99,st,0);}
			if(st>=50){obj2 = CreateShotB2(x,y,s*cos(a),s*sin(a),0,yspd,s*cos(a),99,st+1,0);}
			StartSpawn(obj2,0,w,1,1,0,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;
		}
	}
	let post=[ObjMove_GetX(obj2),ObjMove_GetY(obj2)];
	ObjRender_SetAlpha(obj1,0);
	ObjRender_SetAlpha(obj2,0);
	if(!Obj_IsDeleted(obj1)){SEPlay(dir ~ "audio/e_shot_01.wav",100);}
	let al=0;
	while(ycount>0)
	{
		if(al<128){al+=128/ytime;}
		ObjRender_SetAlpha(obj1,al);
		ObjRender_SetAlpha(obj2,al);
		post=[ObjMove_GetX(obj2),ObjMove_GetY(obj2)];
		ycount-=yspd;
		yield;
	}
	while(!Obj_IsDeleted(obj2))
	{
		if(al<128){al+=128/60;}
		if(al>=128){ObjShot_SetIntersectionEnable(obj2,true);}
		ObjRender_SetAlpha(obj1,al);
		ObjRender_SetAlpha(obj2,al);
		post=[ObjMove_GetX(obj2),ObjMove_GetY(obj2)];
		yield;
	}
	itemdrop(post[0],post[1],500);
}