#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;

@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-360,0);
	ObjRender_SetPosition(ImgBossT,GetStgFrameWidth/2+30,GetStgFrameHeight/2-150,0);
	ObjRender_SetPosition(ImgBossS,GetStgFrameWidth/2-30,GetStgFrameHeight/2-150,0);
	mainTask;
	ObjEnemy_SetLife(ImgBossS,6000);
	ObjEnemy_SetLife(ImgBossT,6000);
} 

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

@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(floating==true)
	{
		ObjRender_SetPosition(ImgBossT,BossObjXTt,BossObjYTt+0.4*cos(floatz)*scfloat,0);
		ObjRender_SetPosition(ImgBossS,BossObjXSs,BossObjYSs+0.4*cos(floatz)*scfloat,0);
	}
	if(hitbox==true)
	{
		ObjEnemy_SetIntersectionCircleToShot(BossObj,BossObjXS,BossObjYS,64);
		ObjEnemy_SetIntersectionCircleToShot(BossObj,BossObjXT,BossObjYT,64);
		ObjEnemy_SetIntersectionCircleToPlayer(BossObj,BossObjXS,BossObjYS,48);
		ObjEnemy_SetIntersectionCircleToPlayer(BossObj,BossObjXT,BossObjYT,48);
		
		ObjEnemy_SetIntersectionCircleToShot(ImgBossS,BossObjXS,BossObjYS,64);
		ObjEnemy_SetIntersectionCircleToPlayer(ImgBossS,BossObjXS,BossObjYS,48);
		
		ObjEnemy_SetIntersectionCircleToShot(ImgBossT,BossObjXT,BossObjYT,64);
		ObjEnemy_SetIntersectionCircleToPlayer(ImgBossT,BossObjXT,BossObjYT,48);
	}
	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+64*sin(az),BossObjYS+16*cos(az),30,255,255,255,1,BLEND_ADD_ARGB);
			Charge(BossObjXS+64*sin(-az),BossObjYS+16*cos(-az),30,255,255,255,1,BLEND_SUBTRACT);
		}
	}
}

task mainTask
{
	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;}
	Barrier(GetStgFrameWidth/2,GetStgFrameHeight/2-150,60,128,-3);
	loop(60)
	{
		if(alpha>0){alpha-=256/60;}
		yield;
	}
	Barrier(GetStgFrameWidth/2-120,GetStgFrameHeight/2-150,60,128,-3);
	Barrier(GetStgFrameWidth/2+120,GetStgFrameHeight/2-150,60,128,3);
	ObjRender_SetPosition(ImgBossT,GetStgFrameWidth/2+120,GetStgFrameHeight/2-150,0);
	ObjRender_SetPosition(ImgBossS,GetStgFrameWidth/2-120,GetStgFrameHeight/2-150,0);
	loop(60)
	{
		if(alpha<256){alpha+=256/60;}
		yield;
	}
	BossMarker(ImgBossS);
	BossMarker(ImgBossT);
	hitbox=true;
	Aura;
	loop(60){yield;}
	ObjEnemyBossScene_SetSpellTimer(objScene,60*60);
	
	mainshooter;
	ObjEnemy_SetDamageRate(BossObj,110-GetCommonData("diff",1)*10,(110-GetCommonData("diff",1)*10)/2);
	TEnd;
}

task move(code)
{
	auraeffect=false;
	hitbox=false;
	if(code==1)
	{
		Barrier(GetStgFrameWidth/2-120,GetStgFrameHeight/2-150,30,128,3);
		Barrier(GetStgFrameWidth/2+120,GetStgFrameHeight/2-150,30,128,-3);
		loop(30)
		{
			if(alpha>0){alpha-=256/30;}
			yield;
		}
		Barrier(GetStgFrameWidth/2-120,GetStgFrameHeight/2-150,30,128,-3);
		Barrier(GetStgFrameWidth/2+120,GetStgFrameHeight/2-150,30,128,3);
		ObjRender_SetPosition(ImgBossS,GetStgFrameWidth/2+120,GetStgFrameHeight/2-150,0);
		ObjRender_SetPosition(ImgBossT,GetStgFrameWidth/2-120,GetStgFrameHeight/2-150,0);
		loop(30)
		{
			if(alpha<256){alpha+=256/30;}
			yield;
		}
	}
	if(code==2)
	{
		Barrier(GetStgFrameWidth/2-120,GetStgFrameHeight/2-150,30,128,3);
		Barrier(GetStgFrameWidth/2+120,GetStgFrameHeight/2-150,30,128,-3);
		loop(30)
		{
			if(alpha>0){alpha-=256/30;}
			yield;
		}
		Barrier(GetStgFrameWidth/2-120,GetStgFrameHeight/2-150,30,128,-3);
		Barrier(GetStgFrameWidth/2+120,GetStgFrameHeight/2-150,30,128,3);
		ObjRender_SetPosition(ImgBossT,GetStgFrameWidth/2+120,GetStgFrameHeight/2-150,0);
		ObjRender_SetPosition(ImgBossS,GetStgFrameWidth/2-120,GetStgFrameHeight/2-150,0);
		loop(30)
		{
			if(alpha<256){alpha+=256/30;}
			yield;
		}
	}
	auraeffect=true;
	hitbox=true;
}

task TEnd
{
	while(ObjEnemy_GetInfo(BossObj,INFO_LIFE)>0){yield;}
	scoring(GetGraze,GetPlayerLife,GetPlayerSpell,GetPlayerPower);
	auraeffect=false;
	hitbox=false;
	Barrier(GetStgFrameWidth/2-120,GetStgFrameHeight/2-150,60,128,-3);
	Barrier(GetStgFrameWidth/2+120,GetStgFrameHeight/2-150,60,128,3);
	otherdrop(GetStgFrameWidth/2,GetStgFrameHeight/2-150,100,"p");
	loop(60)
	{
		if(alpha>0){alpha-=256/60;}
		yield;
	}
	loop(60)
	{
		DeleteShotAll(TYPE_ALL,TYPE_FADE);
		yield;
	}
	if(ObjEnemy_GetInfo(ImgBossS,INFO_LIFE)<ObjEnemy_GetInfo(ImgBossT,INFO_LIFE)){SetCommonData("nextboss","senri");}
	if(ObjEnemy_GetInfo(ImgBossS,INFO_LIFE)>ObjEnemy_GetInfo(ImgBossT,INFO_LIFE)){SetCommonData("nextboss","tenkai");}
	Obj_Delete(BossObj);
	Obj_Delete(ImgBossS);
	Obj_Delete(ImgBossT);
	CloseScript(GetOwnScriptID());
}

task mainshooter
{
	let m=1;
	let a=0;
	let dif=GetCommonData("diff",1);
	let lp=1;
	if(dif==1){lp=10;}
	if(dif==2){lp=15;}
	if(dif==3){lp=20;}
	let count=1;
	let code=1;
	let xx=0;
	let yy=0;
	let aa=0;
	let d=0;
	let t=0;
	while(ObjEnemy_GetInfo(BossObj,INFO_LIFE)>0)
	{
		if(m<-1){m=1;}
		if(code>2){code=1;}
		if(count>3){count=1;}
		if(ObjEnemy_GetInfo(BossObj,INFO_LIFE)>0){SEPlay(dir ~ "se/charge_1.wav");}
		loop(10){if(ObjEnemy_GetInfo(BossObj,INFO_LIFE)>0){Charge(BossObjXT,BossObjYT,rand(30,60),255,255,255,-1,BLEND_SUBTRACT);}}
		loop(30){yield;}
		loop(10){if(ObjEnemy_GetInfo(BossObj,INFO_LIFE)>0){Charge(BossObjXT,BossObjYT,rand(60,120),255,255,255,1,BLEND_SUBTRACT);}}
		xx=GetPlayerX;
		yy=GetPlayerY;
		aa=atan2(yy-BossObjYT,xx-BossObjXT);
		d=GetDistances(xx,yy,BossObjXT,BossObjYT)/5*4;
		a=rand(0,360);
		t=120;
		if(ObjEnemy_GetInfo(BossObj,INFO_LIFE)>0)
		{
			loop(lp)
			{
				if(count==1){if(ObjEnemy_GetInfo(BossObj,INFO_LIFE)>0){bomb(BossObjXT,BossObjYT,BossObjXT+d*sin(-aa+90),BossObjYT+d*cos(-aa+90),a,172,132,6,-m,t,2,lp);}}
				if(count==2)
				{
					if(ObjEnemy_GetInfo(BossObj,INFO_LIFE)>0)
					{
						bomb(BossObjXT,BossObjYT,BossObjXT+d*sin(-aa+90+(50-dif*10)/2),BossObjYT+d*cos(-aa+90+(50-dif*10)/2),a-90,172,132,6,-m,t,2,lp);
						bomb(BossObjXT,BossObjYT,BossObjXT+d*sin(-aa+90-(50-dif*10)/2),BossObjYT+d*cos(-aa+90-(50-dif*10)/2),a+90,172,132,6,-m,t,2,lp);
					}
				}
				if(count==3)
				{
					if(ObjEnemy_GetInfo(BossObj,INFO_LIFE)>0)
					{
						bomb(BossObjXT,BossObjYT,BossObjXT+d*sin(-aa+90),BossObjYT+d*cos(-aa+90),a,172,132,6,-m,t,2,lp);
						bomb(BossObjXT,BossObjYT,BossObjXT+d*sin(-aa+90+(50-dif*10)),BossObjYT+d*cos(-aa+90+(50-dif*10)),a-120,172,132,6,-m,t,2,lp);
						bomb(BossObjXT,BossObjYT,BossObjXT+d*sin(-aa+90-(50-dif*10)),BossObjYT+d*cos(-aa+90-(50-dif*10)),a+120,172,132,6,-m,t,2,lp);
					}
				}
				loop(60/lp){yield;}
				t-=60/lp;
				a-=(360*3)/lp*m;
			}
		}
		if(ObjEnemy_GetInfo(BossObj,INFO_LIFE)>0){SEPlay(dir ~ "se/charge_1.wav");}
		loop(10){if(ObjEnemy_GetInfo(BossObj,INFO_LIFE)>0){Charge(BossObjXS,BossObjYS,rand(30,60),255,255,255,-1,BLEND_ADD_ARGB);}}
		loop(30){yield;}
		loop(10){if(ObjEnemy_GetInfo(BossObj,INFO_LIFE)>0){Charge(BossObjXS,BossObjYS,rand(60,120),255,255,255,1,BLEND_ADD_ARGB);}}
		xx=GetPlayerX;
		yy=GetPlayerY;
		aa=atan2(yy-BossObjYS,xx-BossObjXS);
		d=GetDistances(xx,yy,BossObjXS,BossObjYS)/5*4;
		a=rand(0,360);
		t=120;
		if(ObjEnemy_GetInfo(BossObj,INFO_LIFE)>0)
		{
			loop(lp)
			{
				if(count==1){if(ObjEnemy_GetInfo(BossObj,INFO_LIFE)>0){bomb(BossObjXS,BossObjYS,BossObjXS+d*sin(-aa+90),BossObjYS+d*cos(-aa+90),a,172,132,6,m,t,1,lp);}}
				if(count==2)
				{
					if(ObjEnemy_GetInfo(BossObj,INFO_LIFE)>0)
					{
						bomb(BossObjXS,BossObjYS,BossObjXS+d*sin(-aa+90+(50-dif*10)/2),BossObjYS+d*cos(-aa+90+(50-dif*10)/2),a-90,172,132,6,m,t,1,lp);
						bomb(BossObjXS,BossObjYS,BossObjXS+d*sin(-aa+90-(50-dif*10)/2),BossObjYS+d*cos(-aa+90-(50-dif*10)/2),a+90,172,132,6,m,t,1,lp);
					}
				}
				if(count==3)
				{
					if(ObjEnemy_GetInfo(BossObj,INFO_LIFE)>0)
					{
						bomb(BossObjXS,BossObjYS,BossObjXS+d*sin(-aa+90),BossObjYS+d*cos(-aa+90),a,172,132,6,m,t,1,lp);
						bomb(BossObjXS,BossObjYS,BossObjXS+d*sin(-aa+90+(50-dif*10)),BossObjYS+d*cos(-aa+90+(50-dif*10)),a-120,172,132,6,m,t,1,lp);
						bomb(BossObjXS,BossObjYS,BossObjXS+d*sin(-aa+90-(50-dif*10)),BossObjYS+d*cos(-aa+90-(50-dif*10)),a+120,172,132,6,m,t,1,lp);
					}
				}
				loop(60/lp){yield;}
				t-=60/lp;
				a+=(360*3)/lp*m;
			}
		}
		loop(60){yield;}
		move(code);
		loop(60){yield;}
		count+=1;
		code++;
		m-=2;
	}
}

task bomb(x,y,xx,yy,a,st1,st2,av,m,t,code,lp)
{
	let obj = CreateShotA1(x,y,0,a,st1,0);
	ObjShot_SetIntersectionEnable(obj,false);
	ObjShot_SetAutoDelete(obj,false);
	ObjRender_SetAlpha(obj,0);
	ObjShot_SetSpellResist(obj,true);
	let objx = CreateShotA1(x,y,0,a,st1,0);
	ObjShot_SetAutoDelete(objx,false);
	let blend;
	if(code==1){blend=BLEND_ADD_ARGB;}
	if(code==2){blend=BLEND_SUBTRACT;}
	ObjRender_SetBlendType(objx,blend);
	let c=0;
	let r=0;
	let aa=a;
	let cc=0;
	if(!Obj_IsDeleted(objx)){SEPlay(dir ~ "se/e_shot_01.wav");}
	loop(60)
	{
		cc++;
		r++;
		ObjMove_SetPosition(objx,ObjMove_GetX(obj)+r*sin(aa),ObjMove_GetY(obj)+r*cos(aa));
		ObjMove_SetAngle(objx,atan2(ObjMove_GetY(obj)-ObjMove_GetY(objx),ObjMove_GetX(obj)-ObjMove_GetX(objx))+90*m);
		if(!Obj_IsDeleted(objx)&&cc%5==0){tailshot(ObjMove_GetX(objx),ObjMove_GetY(objx),ObjMove_GetAngle(objx)+180,30,st1,blend);}
		aa+=av*m*c;
		c+=1/60;
		yield;
	}
	loop(t)
	{
		cc++;
		r-=60/t;
		ObjMove_SetDestAtFrame(obj,xx,yy,t);
		ObjMove_SetPosition(objx,ObjMove_GetX(obj)+r*sin(aa),ObjMove_GetY(obj)+r*cos(aa));
		ObjMove_SetAngle(objx,atan2(ObjMove_GetY(obj)-ObjMove_GetY(objx),ObjMove_GetX(obj)-ObjMove_GetX(objx))+90*m);
		if(!Obj_IsDeleted(objx)&&cc%5==0){tailshot(ObjMove_GetX(objx),ObjMove_GetY(objx),ObjMove_GetAngle(objx)+180,30,st1,blend);}
		aa+=av*m*c;
		c-=1/60;
		yield;
	}
	let xo=ObjMove_GetX(obj);
	let yo=ObjMove_GetY(obj);
	if(code==1)
	{
		let ww=30;
		let rr=0;
		let sp=0.3;
		if(!Obj_IsDeleted(objx))
		{
			loop(10)
			{
				ww+=3;
				rr+=1.6;
				shot(xo,yo,rr,rr,sp,a/3+180,0,st2,ww,15,blend);
				sp+=0.15;
			}
		}
	}
	if(code==2){if(!Obj_IsDeleted(objx)){laser(xo,yo,a/3,640,24,60,st2,120,1,m,blend);}}
	ObjShot_FadeDelete(obj);
	ObjShot_FadeDelete(objx);
}

task tailshot(x,y,a,w,st,blend)
{
	let obj = CreateShotA1(x,y,0,a+180,st,0);
	ObjRender_SetBlendType(obj,blend);
	ObjShot_SetIntersectionEnable(obj,false);
	let al=256;
	let xyz=1;
	loop(w)
	{
		ObjRender_SetAlpha(obj,al/2);
		ObjRender_SetScaleXYZ(obj,xyz/2,xyz/2,0);
		al-=256/w;
		xyz-=1/w;
		yield;
	}
	Obj_Delete(obj);
}

task shot(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,av,m,blend)
{
	let obj=CreateStraightLaserA1(x,y,a,0,w,dt,st,d);
	ObjRender_SetBlendType(obj,blend);
	let c=0;
	let r=0;
	let aa=a;
	if(!Obj_IsDeleted(obj)){SEPlay(dir ~ "se/warning.wav");}
	loop(d)
	{
		c+=1/d;
		r+=1*(1-c)*m;
		aa+=av*(1-c)*m;
		ObjLaser_SetLength(obj,l*c);
		ObjMove_SetPosition(obj,x+r*sin(a),y+r*cos(a));
		ObjStLaser_SetAngle(obj,-aa);
		yield;
	}
	if(!Obj_IsDeleted(obj)){SEPlay(dir ~ "se/laser_3.wav");}
}