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

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

let alpha = 0;
let hitbox = false;

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

let auraeffect=true;

let imgBossObj = dir ~ "sprite/dot_senri.png";

let objScene = GetEnemyBossSceneObjectID();

@Initialize 
{
	SetAutoDeleteObject(true);
	BossObj = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(BossObj);
	Obj_SetRenderPriority(BossObj,0.31);
	ObjRender_SetPosition(BossObj,GetStgFrameWidth/2,GetStgFrameHeight/2-150,0);
	mainTask;
} 

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

@MainLoop 
{
	BossObjX = ObjMove_GetX(BossObj);
	BossObjY = ObjMove_GetY(BossObj);
	let BossObjxx = BossObjX;
	let BossObjyy = BossObjY;
	if(floating==true){ObjRender_SetPosition(BossObj,BossObjxx,BossObjyy+0.4*cos(floatz)*scfloat,0);}
	if(hitbox==true)
	{
		ObjEnemy_SetIntersectionCircleToShot(BossObj,BossObjX,BossObjY,64);
		ObjEnemy_SetIntersectionCircleToPlayer(BossObj,BossObjX,BossObjY,48);
	}
	if(floating==true && scfloat<1){scfloat+=1/30;}
	if(floating==false && scfloat>0){scfloat-=1/30;}
	floatz+=5;
	ObjRender_SetAlpha(BossObj,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(BossObjX+64*sin(az),BossObjY+16*cos(az),30,255,255,255,1,BLEND_SUBTRACT);
			Charge(BossObjX+64*sin(-az),BossObjY+16*cos(-az),30,255,255,255,1,BLEND_ADD_ARGB);
		}
	}
}

task mainTask
{
	loop(60){yield;}
	Barrier(GetStgFrameWidth/2,GetStgFrameHeight/2-150,60,128,-3);
	idlesprite(BossObj,4,64,64,imgBossObj);
	floating=true;
	loop(60)
	{
		if(alpha<256){alpha+=256/60;}
		yield;
	}
	SpellLeft(BossObj,6,2,BLEND_ADD_ARGB);
	loop(60){yield;}
	BossMarker(BossObj);
	hitbox=true;
	Aura;
	loop(60){yield;}
	ObjEnemyBossScene_SetSpellTimer(objScene,60*60);
	
	mainshooter;
	move;
	ObjEnemy_SetDamageRate(BossObj,100-GetCommonData("diff",1)*10,(100-GetCommonData("diff",1)*10)/2);
	TEnd;
}

task TEnd
{
	while(ObjEnemy_GetInfo(BossObj,INFO_LIFE)>0){yield;}
	scoring(GetGraze,GetPlayerLife,GetPlayerSpell,GetPlayerPower);
	otherdrop(BossObjX,BossObjY,50,"p");
	auraeffect=false;
	hitbox=false;
	floating=false;
	if(BossObjX>=GetStgFrameWidth/2){movesprite(BossObj,4,64,64,64,imgBossObj,60);}
	if(BossObjX<GetStgFrameWidth/2){movesprite(BossObj,4,128,64,64,imgBossObj,60);}
	ObjMove_SetDestAtFrame(BossObj,GetStgFrameWidth/2,GetStgFrameHeight/2-120,60);
	loop(60)
	{
		DeleteShotAll(TYPE_ALL,TYPE_FADE);
		yield;
	}
	Obj_Delete(BossObj);
	CloseScript(GetOwnScriptID());
}

task move
{
	while(ObjEnemy_GetInfo(BossObj, INFO_LIFE) > 0)
	{
		loop(90){yield;}
		let lp=1;
		floating=false;
		loop(lp)
		{
			let aa=rand(0,360);
			let xx=BossObjX;
			let yy=BossObjY;
			if(yy<=90){aa=90+rand(-15,15)-90;}
			if(yy>=GetStgFrameHeight/2-90){aa=270+rand(-15,15)-90;}
			if(xx>=GetStgFrameWidth-120){aa=0+rand(-15,15)-90;}
			if(xx<=120){aa=180+rand(-15,15)-90;}
			let spd=rand(1.2,1.8);
			let sp=spd;
			let r=spd/2*60;
			if(xx+r*sin(aa)<xx){movesprite(BossObj,4,64,64,64,imgBossObj,60);}
			if(xx+r*sin(aa)>=xx){movesprite(BossObj,4,128,64,64,imgBossObj,60);}
			loop(60)
			{
				if(ObjEnemy_GetInfo(BossObj, INFO_LIFE) > 0)
				{
					ObjMove_SetDestAtSpeed(BossObj,xx+r*sin(aa),yy+r*cos(aa),sp);
					sp-=spd/60;
					yield;
				}
			}
			ObjMove_SetSpeed(BossObj,0);
			floating=true;
		}
		loop(30){yield;}
	}
}

task mainshooter
{
	let m=1;
	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;
	while(ObjEnemy_GetInfo(BossObj,INFO_LIFE)>0)
	{
		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(BossObjX,BossObjY,rand(30,60),255,255,255,-1,BLEND_ADD_ARGB);}}
		loop(30){yield;}
		loop(10){if(ObjEnemy_GetInfo(BossObj,INFO_LIFE)>0){Charge(BossObjX,BossObjY,rand(60,120),255,255,255,1,BLEND_ADD_ARGB);}}
		let xx=GetPlayerX;
		let yy=GetPlayerY;
		let aa=atan2(yy-BossObjY,xx-BossObjX);
		let d=GetDistances(xx,yy,BossObjX,BossObjY)/5*3;
		let a=rand(0,360);
		let t=120;
		if(ObjEnemy_GetInfo(BossObj,INFO_LIFE)>0)
		{
			loop(lp)
			{
				if(count==1){if(ObjEnemy_GetInfo(BossObj,INFO_LIFE)>0){bomb(BossObjX,BossObjY,BossObjX+d*sin(-aa+90),BossObjY+d*cos(-aa+90),a,172,132,6,m,t,lp);}}
				if(count==2)
				{
					if(ObjEnemy_GetInfo(BossObj,INFO_LIFE)>0)
					{
						bomb(BossObjX,BossObjY,BossObjX+d*sin(-aa+90+(50-dif*10)),BossObjY+d*cos(-aa+90+(50-dif*10)),a-60,172,132,6,m,t,lp);
						bomb(BossObjX,BossObjY,BossObjX+d*sin(-aa+90-(50-dif*10)),BossObjY+d*cos(-aa+90-(50-dif*10)),a+60,172,132,6,m,t,lp);
					}
				}
				if(count==3)
				{
					if(ObjEnemy_GetInfo(BossObj,INFO_LIFE)>0)
					{
						bomb(BossObjX,BossObjY,BossObjX+d*sin(-aa+90),BossObjY+d*cos(-aa+90),a,172,132,6,m,t,lp);
						bomb(BossObjX,BossObjY,BossObjX+d*sin(-aa+90+(50-dif*10)*2),BossObjY+d*cos(-aa+90+(50-dif*10)*2),a-72,172,132,6,m,t,lp);
						bomb(BossObjX,BossObjY,BossObjX+d*sin(-aa+90-(50-dif*10)*2),BossObjY+d*cos(-aa+90-(50-dif*10)*2),a+72,172,132,6,m,t,lp);
					}
				}
				loop(60/lp){yield;}
				t-=60/lp;
				a+=(360*3)/lp*m;
			}
		}
		a=rand(0,360);
		loop(lp)
		{
			if(ObjEnemy_GetInfo(BossObj,INFO_LIFE)>0)
			{
				beam(BossObjX,BossObjY,2.4,a,235,95,30,30,BLEND_SUBTRACT);
			}
			a+=360/lp;
		}
		loop(90){yield;}
		count+=1;
		code++;
		m*=-1;
	}
}

task bomb(x,y,xx,yy,a,st1,st2,av,m,t,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);
	ObjRender_SetBlendType(objx,BLEND_ADD_ARGB);
	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),30,st1,BLEND_ADD_ARGB);}
		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),30,st1,BLEND_ADD_ARGB);}
		aa+=av*m*c;
		c-=1/60;
		yield;
	}
	let xo=ObjMove_GetX(obj);
	let yo=ObjMove_GetY(obj);
	let ww=30;
	let rr=0;
	let sp=0.3;
	if(!Obj_IsDeleted(objx))
	{
		loop(5)
		{
			ww+=3;
			rr+=1.6;
			shot(xo,yo,rr,rr,sp,a/3+180,0,st2,ww,15,BLEND_ADD_ARGB);
			sp+=0.15;
		}
	}
	ObjShot_FadeDelete(obj);
	ObjShot_FadeDelete(objx);
}

task tailshot(x,y,a,w,st,blend)
{
	let obj = CreateShotA1(x,y,0,a,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 beam(x,y,s,a,st1,st2,w,t,blend)
{
	let obj = CreateShotA1(x,y,s,a,st1,0);
	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);
	loop(t)
	{
		ObjMove_SetSpeed(obj,ObjMove_GetSpeed(obj)-s/t);
		tailshot(ObjMove_GetX(obj),ObjMove_GetY(obj),ObjMove_GetAngle(obj)+180,30,st1,blend);
		yield;
	}
	let aa=-ObjMove_GetAngle(obj)+90;
	let lp=3;
	loop(lp)
	{
		if(!Obj_IsDeleted(obj))
		{
			shot(ObjMove_GetX(obj),ObjMove_GetY(obj),1,1,s/5,aa,0,st2,30,60,blend);
			shot(ObjMove_GetX(obj),ObjMove_GetY(obj),1,1,s/10,aa+180/lp,0,st2,30,60,blend);
		}
		aa+=360/lp;
	}
	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);
}