#System["./system/Default_System.txt"]
#Background["./background.txt"]
#include"./Shot/VigorShot_Const.txt"
#include"./function.txt"

let BossObj;
let BossObjXT = 0;
let BossObjYT = 0;
let BossObjXS = 0;
let BossObjYS = 0;

let alpha = 0;
let hitbox = false;

let ImgBossT;
let ImgBossS;

let scfloat=1;
let floating=false;
let floatz=0;

let auraeffect=true;

let imgBossObjT = dir ~ "sprite/dot_tenkai.png";
let imgBossObjS = dir ~ "sprite/dot_senri.png";

let objScene = GetEnemyBossSceneObjectID();

let bulletcount=0;

let crane1=false;
let crane2=true;
let crane3=true;
let rest=true;

@Initialize 
{
	SetAutoDeleteObject(true);
	BossObj = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ImgBossT = ObjEnemy_Create(OBJ_ENEMY);
	ImgBossS = ObjEnemy_Create(OBJ_ENEMY);
	ObjEnemy_Regist(BossObj);
	ObjEnemy_Regist(ImgBossT);
	ObjEnemy_Regist(ImgBossS);
	Obj_SetRenderPriority(ImgBossS,0.31);
	Obj_SetRenderPriority(ImgBossT,0.32);
	ObjRender_SetPosition(BossObj,GetStgFrameWidth/2,GetStgFrameHeight/2-150,0);
	ObjRender_SetPosition(ImgBossT,GetStgFrameWidth/2+15,GetStgFrameHeight/2-150+3,0);
	ObjRender_SetPosition(ImgBossS,GetStgFrameWidth/2-15,GetStgFrameHeight/2-150,0);
	mainTask;
} 

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
	{
		SetScriptResult(3000);
	}
}

@MainLoop 
{
	BossObjXT = ObjMove_GetX(ImgBossT);
	BossObjYT = ObjMove_GetY(ImgBossT);
	BossObjXS = ObjMove_GetX(ImgBossS);
	BossObjYS = ObjMove_GetY(ImgBossS);
	let BossObjXTt = BossObjXT;
	let BossObjXSs = BossObjXS;
	let BossObjYTt = BossObjYT;
	let BossObjYSs = BossObjYS;
	if(rest==false){hitbox=true;}
	if(rest==true){hitbox=false;}
	if(floating==true)
	{
		ObjRender_SetPosition(ImgBossT,BossObjXTt,BossObjYTt+0.1*cos(floatz)*scfloat,0);
		ObjRender_SetPosition(ImgBossS,BossObjXSs,BossObjYSs+0.1*cos(floatz)*scfloat,0);
	}
	if(hitbox==true)
	{
		ObjEnemy_SetIntersectionCircleToShot(BossObj,GetStgFrameWidth/2,GetStgFrameHeight/2-120,60);
		ObjEnemy_SetIntersectionCircleToPlayer(BossObj,GetStgFrameWidth/2,GetStgFrameHeight/2-120,60);
	}
	if(floating==true && scfloat<1){scfloat+=1/30;}
	if(floating==false && scfloat>0){scfloat-=1/30;}
	floatz+=5;
	ObjRender_SetAlpha(ImgBossS,alpha);
	ObjRender_SetAlpha(ImgBossT,alpha);
	yield;
} 

@Finalize 
{ 
} 

task Aura
{
	let az=90;
	while(ObjEnemy_GetInfo(BossObj, INFO_LIFE) > 0)
	{
		loop(5)
		{
			az+=1.9;
			yield;
		}
		if(auraeffect==true)
		{
			Charge(BossObjXT+64*sin(az),BossObjYT+16*cos(az),30,255,255,255,1,BLEND_SUBTRACT);
			Charge(BossObjXT+64*sin(-az),BossObjYT+16*cos(-az),30,255,255,255,1,BLEND_ADD_ARGB);
			Charge(BossObjXS+128*sin(az),BossObjYS+16*cos(az),30,255,255,255,1,BLEND_ADD_ARGB);
			Charge(BossObjXS+128*sin(-az),BossObjYS+16*cos(-az),30,255,255,255,1,BLEND_SUBTRACT);
		}
	}
}

task mainTask
{
	SetCommonData("bossspell","both");
	SetCommonData("spellcolor","grey");
	loop(60){yield;}
	Barrier(GetStgFrameWidth/2,GetStgFrameHeight/2-150,60,128,3);
	idlesprite(ImgBossT,4,64,64,imgBossObjT);
	idlesprite(ImgBossS,4,64,64,imgBossObjS);
	floating=true;
	loop(60)
	{
		if(alpha<256){alpha+=256/60;}
		yield;
	}
	loop(60){yield;}
	SEPlay(dir ~ "se/spell_0.wav");
	ObjEnemyBossScene_StartSpell(objScene);
	SpellDeclare(6,"both");
	loop(60){yield;}
	BossMarker(BossObj);
	Aura;
	ObjEnemyBossScene_SetSpellTimer(objScene,60*60);
	
	mainshooter;
	ObjEnemy_SetDamageRate(BossObj,40-GetCommonData("diff",1)*10,(40-GetCommonData("diff",1)*10)/2);
	TEnd;
}

task TEnd
{
	while(ObjEnemy_GetInfo(BossObj,INFO_LIFE)>0){yield;}
	scoring(GetGraze,GetPlayerLife,GetPlayerSpell,GetPlayerPower);
	auraeffect=false;
	hitbox=false;
	SEPlay(dir ~ "se/charge_1.wav");
	loop(60)
	{
		Charge(GetStgFrameWidth/2,GetStgFrameHeight/2-150,rand(30,60),255,255,255,-1,BLEND_SUBTRACT);
		Charge(GetStgFrameWidth/2,GetStgFrameHeight/2-150,rand(30,60),255,255,255,-1,BLEND_ADD_ARGB);
	}
	loop(60){yield;}
	loop(60)
	{
		DeleteShotAll(TYPE_ALL,TYPE_FADE);
		yield;
	}
	SetCommonData("scriptend",true);
	Obj_Delete(BossObj);
	Obj_Delete(ImgBossS);
	Obj_Delete(ImgBossT);
	CloseScript(GetOwnScriptID());
}

task mainshooter
{
	let phase=1;
	let dif=GetCommonData("diff",1);
	let lp=10+dif*5;
	let a=rand(0,360);
	loop(lp)
	{
		let t=120;
		let aa=0;
		loop(6)
		{
			shots(GetStgFrameWidth/2,GetStgFrameHeight/2-150,1,1,2.4,a+aa,0,255,30,t,BLEND_ADD_ARGB);
			shots(GetStgFrameWidth/2,GetStgFrameHeight/2-150,1,1,2.4,a+180/lp+aa,0,255,30,t,BLEND_SUBTRACT);
			t+=30;
			aa+=180/lp;
		}
		a+=360/lp;
	}
	loop(180){yield;}
	rest=false;
	while(ObjEnemy_GetInfo(BossObj,INFO_LIFE)>0)
	{
		if(ObjEnemy_GetInfo(BossObj,INFO_LIFE)<=2000 && phase<2){phase=2;crane2=false;rest=true;}
		if(ObjEnemy_GetInfo(BossObj,INFO_LIFE)<=1000 && phase<3){phase=3;crane3=false;rest=true;}
		if(rest==true)
		{
			loop(60){DeleteShotAll(TYPE_ALL,TYPE_FADE);yield;}
			a=rand(0,360);
			loop(lp)
			{
				let t=120;
				let aa=0;
				loop(6)
				{
					shots(GetStgFrameWidth/2,GetStgFrameHeight/2-150,1,1,2.4,a+aa,0,255,30,t,BLEND_ADD_ARGB);
					shots(GetStgFrameWidth/2,GetStgFrameHeight/2-150,1,1,2.4,a+180/lp+aa,0,255,30,t,BLEND_SUBTRACT);
					t+=30;
					aa+=180/lp;
				}
				a+=360/lp;
			}
			loop(180)
			{
				yield;
			}
			rest=false;
		}
		if(phase==1 && crane1==false)
		{
			crane(GetStgFrameWidth/2,GetStgFrameHeight/2-150,90,1,lp);
			crane1=true;
		}
		if(phase==2 && crane2==false)
		{
			crane1=false;
			crane(GetStgFrameWidth/2+90,GetStgFrameHeight/2-150,120,2,lp);
			crane(GetStgFrameWidth/2-90,GetStgFrameHeight/2-150,60,2,lp);
			crane2=true;
		}
		if(phase==3 && crane3==false)
		{
			crane2=false;
			crane(GetStgFrameWidth/2,GetStgFrameHeight/2-150,90,3,lp);
			crane(GetStgFrameWidth/2+120,GetStgFrameHeight/2-120,115,3,lp);
			crane(GetStgFrameWidth/2-120,GetStgFrameHeight/2-120,75,3,lp);
			crane3=true;
		}
		if(phase%1==0){phase+=0.1;}
		yield;
	}
}

task crane(x,y,aa,code,lpp)
{
	let ag=-30;
	let av=5;
	let c=0;
	let sp=6;
	let w=5;
	let lp=5;
	let shot=true;
	while(shot==true)
		{
		loop(120/av)
		{
			c++;
			if(c%2==1)
			{
				wingloop(x,y,sp,aa+180+ag,95,w,BLEND_SUBTRACT,1,lp);
				wingloop(x,y,sp,aa+180-ag,95,w,BLEND_SUBTRACT,-1,lp);
			}
			if(c%2==0)
			{
				wingloop(x,y,sp,aa+180+ag,95,w,BLEND_ADD_ARGB,1,lp);
				wingloop(x,y,sp,aa+180-ag,95,w,BLEND_ADD_ARGB,-1,lp);
			}
			if(c%15==0)
			{
				let a=rand(0,360);
				loop(lpp)
				{
					if(c%2==1){laser(x,y,a,480,24,60,132,60,BLEND_ADD_ARGB);}
					if(c%2==0){laser(x,y,a,480,24,60,132,60,BLEND_SUBTRACT);}
					a+=360/lpp;
				}
				a=rand(0,360);
				loop(lpp)
				{
					if(c%2==1)
					{
						shots(x,y,1,1,1.2,a,0,15,30,120,BLEND_ADD_ARGB);
						shots(x,y,1,1,1.2,a+180/lpp,0,15,30,240,BLEND_ADD_ARGB);
					}
					if(c%2==0)
					{
						shots(x,y,1,1,1.2,a,0,15,30,120,BLEND_SUBTRACT);
						shots(x,y,1,1,1.2,a+180/lpp,0,15,30,240,BLEND_SUBTRACT);
					}
					a+=360/lpp;
				}
			}
			loop(av)
			{
				ag+=1;
				if(shot==true){yield;}
				if(code==1){if(crane1==false || rest==true){shot=false;}}
				if(code==2){if(crane2==false || rest==true){shot=false;}}
			}
		}
		loop(120/av)
		{
			c++;
			if(c%2==1)
			{
				wingloop(x,y,sp,aa+180+ag,95,w,BLEND_SUBTRACT,1,lp);
				wingloop(x,y,sp,aa+180-ag,95,w,BLEND_SUBTRACT,-1,lp);
			}
			if(c%2==0)
			{
				wingloop(x,y,sp,aa+180+ag,95,w,BLEND_ADD_ARGB,1,lp);
				wingloop(x,y,sp,aa+180-ag,95,w,BLEND_ADD_ARGB,-1,lp);
			}
			if(c%15==0)
			{
				let a=rand(0,360);
				loop(lpp)
				{
					if(c%2==1){laser(x,y,a,480,24,60,132,60,BLEND_ADD_ARGB);}
					if(c%2==0){laser(x,y,a,480,24,60,132,60,BLEND_SUBTRACT);}
					a+=360/lpp;
				}
				a=rand(0,360);
				loop(lpp)
				{
					if(c%2==1)
					{
						shots(x,y,1,1,1.2,a,0,15,30,120,BLEND_ADD_ARGB);
						shots(x,y,1,1,1.2,a+180/lpp,0,15,30,240,BLEND_ADD_ARGB);
					}
					if(c%2==0)
					{
						shots(x,y,1,1,1.2,a,0,15,30,120,BLEND_SUBTRACT);
						shots(x,y,1,1,1.2,a+180/lpp,0,15,30,240,BLEND_SUBTRACT);
					}
					a+=360/lpp;
				}
			}
			loop(av)
			{
				ag-=1;
				if(shot==true){yield;}
				if(code==1){if(crane1==false || rest==true){shot=false;}}
				if(code==2){if(crane2==false || rest==true){shot=false;}}
			}
		}
	}
}

task wingloop(x,y,sp,a,st,w,blend,m,lp)
{
	loop(lp)
	{
		angular(x,y,sp,a,st,w,blend,m);
		w+=5;
	}
}

task angular(x,y,s,a,st,w,blend,m)
{
	let obj = CreateShotA1(x,y,0,a,st,0);
	if(ObjEnemy_GetInfo(BossObj, INFO_LIFE)<=0){ObjShot_FadeDelete(obj);}
	ObjRender_SetBlendType(obj,blend);
	ObjShot_SetIntersectionEnable(obj,false);
	ObjShot_SetAutoDelete(obj,false);
	let al=0;
	let xyz=0;
	loop(w)
	{
		ObjRender_SetAlpha(obj,al);
		ObjRender_SetScaleXYZ(obj,xyz,xyz,0);
		ObjMove_SetSpeed(obj,ObjMove_GetSpeed(obj)+s/w);
		al+=256/w;
		xyz+=1/w;
		yield;
	}
	if(!Obj_IsDeleted(obj)){SEPlay(dir ~ "se/e_shot_00.wav");}
	ObjRender_SetAlpha(obj,256);
	ObjRender_SetScaleXYZ(obj,1,1,1);
	ObjShot_SetIntersectionEnable(obj,true);
	ObjShot_SetAutoDelete(obj,true);
	ObjMove_SetSpeed(obj,s);
	let xx=0;
	let yy=0;
	let av=5;
	loop(135/av)
	{
		xx=ObjMove_GetX(obj);
		yy=ObjMove_GetY(obj);
		ObjMove_SetAngularVelocity(obj,av*m);
		if(!Obj_IsDeleted(obj)){yield;}
	}
	loop(180/av)
	{
		xx=ObjMove_GetX(obj);
		yy=ObjMove_GetY(obj);
		ObjMove_SetAngularVelocity(obj,-av*m);
		if(!Obj_IsDeleted(obj)){yield;}
	}
	let ag=-atan2(y-yy,x-xx)+90;
	let dd=GetDistances(x,y,xx,yy);
	loop(180/av)
	{
		xx=ObjMove_GetX(obj);
		yy=ObjMove_GetY(obj);
		ObjRender_SetPosition(obj,x+dd*sin(ag),y+dd*cos(ag),0);
		ag-=av*m;
		if(!Obj_IsDeleted(obj)){yield;}
	}
	while(!Obj_IsDeleted(obj)){yield;}
	itemdrop(xx,yy,100);
}

task shots(x,y,rx,ry,s,a,aa,st,w,t,blend)
{
	let ao=atan2(y+rx*cos(a)-y,x+ry*sin(a)-x)+aa;
	let obj = CreateShotA1(x+rx*sin(a),y+ry*cos(a),0,ao,st,0);
	if(ObjEnemy_GetInfo(BossObj, INFO_LIFE)<=0){ObjShot_FadeDelete(obj);}
	ObjRender_SetBlendType(obj,blend);
	ObjShot_SetIntersectionEnable(obj,false);
	ObjShot_SetAutoDelete(obj,false);
	let al=0;
	let xyz=0;
	loop(w)
	{
		ObjRender_SetAlpha(obj,al);
		ObjRender_SetScaleXYZ(obj,xyz,xyz,0);
		al+=256/w;
		xyz+=1/w;
		yield;
	}
	if(!Obj_IsDeleted(obj)){SEPlay(dir ~ "se/e_shot_00.wav");}
	ObjRender_SetAlpha(obj,256);
	ObjRender_SetScaleXYZ(obj,1,1,1);
	ObjShot_SetIntersectionEnable(obj,true);
	ObjShot_SetAutoDelete(obj,true);
	let xx=0;
	let yy=0;
	while(!Obj_IsDeleted(obj))
	{
		xx=ObjMove_GetX(obj);
		yy=ObjMove_GetY(obj);
		ObjMove_SetSpeed(obj,ObjMove_GetSpeed(obj)+s/t);
		yield;
	}
	itemdrop(xx,yy,100);
}

task laser(x,y,a,l,w,dt,st,d,blend)
{
	let obj=CreateStraightLaserA1(x,y,a,0,w,dt,st,d);
	ObjRender_SetBlendType(obj,blend);
	let c=0;
	if(!Obj_IsDeleted(obj)){SEPlay(dir ~ "se/warning.wav");}
	loop(d)
	{
		c+=1/d;
		ObjLaser_SetLength(obj,l*c);
		yield;
	}
	if(!Obj_IsDeleted(obj)){SEPlay(dir ~ "se/laser_3.wav");}
}