#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,4,4,BLEND_SUBTRACT);
	loop(60){yield;}
	BossMarker(BossObj);
	hitbox=true;
	Aura;
	loop(60){yield;}
	ObjEnemyBossScene_SetSpellTimer(objScene,60*60);
	
	mainshooter;
	move;
	ObjEnemy_SetDamageRate(BossObj,80-GetCommonData("diff",1)*10,(80-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(120){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.8,2.4);
			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;
		}
	}
}

task mainshooter
{
	let m=1;
	let dif=GetCommonData("diff",1);
	let lp=1;
	let lpp=1;
	if(dif==1){lp=5;lpp=3;}
	if(dif==2){lp=10;lpp=5;}
	if(dif==3){lp=15;lpp=6;}
	let code=1;
	while(ObjEnemy_GetInfo(BossObj,INFO_LIFE)>0)
	{
		if(code>3){code=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_SUBTRACT);}}
		loop(30){yield;}
		loop(10){if(ObjEnemy_GetInfo(BossObj,INFO_LIFE)>0){Charge(BossObjX,BossObjY,rand(60,120),255,255,255,1,BLEND_SUBTRACT);}}
		let xx=BossObjX;
		let yy=BossObjY;
		let a=rand(0,360);
		if(code==1)
		{
			a=rand(0,360);
			loop(lp)
			{
				let aa=0;
				loop(4)
				{
					if(ObjEnemy_GetInfo(BossObj,INFO_LIFE)>0)
					{
						shot(xx,yy,1,1,3,a+aa,0,132,30,60,BLEND_ADD_ARGB,75,-m);
					}
					aa+=90;
				}
				a+=m;
				loop(60/lp){yield;}
			}
		}
		if(code==2)
		{
			a=rand(0,360);
			let cc=1;
			loop(2)
			{
				loop(lp)
				{
					let aa=0;
					loop(6)
					{
						if(ObjEnemy_GetInfo(BossObj,INFO_LIFE)>0)
						{
							shot(xx,yy,1,1,3,a+aa,0,132,30,60,BLEND_ADD_ARGB,75,-m*cc);
						}
						aa+=60;
					}
					a+=m;
					loop(30/lp){yield;}
				}
				cc=-1;
			}
		}
		if(code==3)
		{
			a=rand(0,360);
			let cc=1;
			loop(4)
			{
				loop(lp)
				{
					let aa=0;
					loop(8)
					{
						if(ObjEnemy_GetInfo(BossObj,INFO_LIFE)>0)
						{
							shot(xx,yy,1,1,3,a+aa,0,132,30,60,BLEND_ADD_ARGB,75,-m*cc);
						}
						aa+=45;
					}
					a+=m;
					loop(15/lp){yield;}
				}
				cc*=-1;
			}
		}
		loop(30){yield;}
		a=rand(240,300)/lpp;
		let c=0;
		let t=0;
		loop(lpp)
		{
			c++;
			t+=3;
			if(ObjEnemy_GetInfo(BossObj,INFO_LIFE)>0)
			{
				beam(BossObjX,BossObjY,4.2,-90+a/2*c,235,115,t,30,BLEND_SUBTRACT);
				beam(BossObjX,BossObjY,4.2,-90-a/2*c,235,115,t,30,BLEND_SUBTRACT);
			}
		}
		loop(60){yield;}
		code++;
		m*=-1;
	}
}

task beam(x,y,s,a,st1,st2,w,t,blend)
{
	let obj = CreateShotA1(x,y,0,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)
	{
		tailshot(ObjMove_GetX(obj),ObjMove_GetY(obj),ObjMove_GetAngle(obj)+180,30,st1,blend);
		yield;
	}
	if(!Obj_IsDeleted(obj)){SEPlay(dir ~ "se/e_shot_00.wav");}
	let aa=rand(0,360);
	let lp=5;
	loop(lp)
	{
		if(!Obj_IsDeleted(obj))
		{
			fadeout(ObjMove_GetX(obj),ObjMove_GetY(obj),1.2,0.6,aa,st2,30,60,blend);
			fadeout(ObjMove_GetX(obj),ObjMove_GetY(obj),1.2,1.2,aa+180/lp,st2,30,60,blend);
		}
		aa+=360/lp;
	}
	Obj_Delete(obj);
}

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 fadeout(x,y,s,ss,a,st,w,t,blend)
{
	let obj = CreateShotA1(x,y,s,a,st,0);
	ObjRender_SetBlendType(obj,blend);
	ObjShot_SetIntersectionEnable(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;
	}
	ObjShot_SetIntersectionEnable(obj,true);
	ObjRender_SetAlpha(obj,256);
	ObjRender_SetScaleXYZ(obj,1,1,0);
	ObjMove_SetSpeed(obj,0);
	while(!Obj_IsDeleted(obj))
	{
		ObjMove_SetSpeed(obj,ObjMove_GetSpeed(obj)+ss/t);
		yield;
	}
}

task shot(x,y,rx,ry,s,a,aa,st,w,t,blend,av,m)
{
	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);
	ObjMove_SetSpeed(obj,s);
	let xx=0;
	let yy=0;
	let count=0;
	while(!Obj_IsDeleted(obj))
	{
		loop(t)
		{
			xx=ObjMove_GetX(obj);
			yy=ObjMove_GetY(obj);
			if(count<3){ObjMove_SetSpeed(obj,ObjMove_GetSpeed(obj)-s/t);}
			if(count>=3){ObjMove_SetSpeed(obj,ObjMove_GetSpeed(obj)+s/(t*2));}
			yield;
		}
		if(count<3){ObjMove_SetSpeed(obj,s);ObjMove_SetAngle(obj,ObjMove_GetAngle(obj)+av*m);}
		count++;
		if(count==3){ObjMove_SetAngle(obj,ObjMove_GetAngle(obj)+90*m);}
	}
	itemdrop(xx,yy,100);
}