#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 = 256;
let hitbox = false;

let ImgBoss;

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);
	ObjEnemy_SetDamageRate(BossObj,0,0);
	ObjRender_SetPosition(BossObj,GetStgFrameWidth/2,GetStgFrameHeight/2-120,0);
	mainTask;
} 

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

@MainLoop 
{
	BossObjX = ObjMove_GetX(BossObj);
	BossObjY = ObjMove_GetY(BossObj);
	ObjRender_SetPosition(BossObj,BossObjX,BossObjY+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
{
	SetCommonData("bossspell","senri");
	SetCommonData("spellcolor","white");
	idlesprite(BossObj,4,64,64,imgBossObj);
	loop(60){yield;}
	loop(10){Charge(BossObjX,BossObjY,rand(30,60),255,255,255,-1,BLEND_SUBTRACT);}
	loop(30){yield;}
	SEPlay(dir ~ "se/spell_0.wav");
	ObjEnemyBossScene_StartSpell(objScene);
	SpellDeclare(4,"senri");
	SpellLeft(BossObj,3,5,BLEND_ADD_ARGB);
	loop(30){yield;}
	BossMarker(BossObj);
	hitbox=true;
	floating=true;
	Aura;
	loop(90){yield;}
	ObjEnemyBossScene_SetSpellTimer(objScene,60*60);
	
	mainshooter;
	TEnd;
}

task TEnd
{
	while(ObjEnemy_GetInfo(BossObj, INFO_LIFE) > 0){yield;}
	scoring(GetGraze,GetPlayerLife,GetPlayerSpell,GetPlayerPower);
	otherdrop(BossObjX,BossObjY,50,"p");
	otherdrop(BossObjX,BossObjY,100,"l");
	auraeffect=false;
	hitbox=false;
	floating=false;
	Barrier(BossObjX,BossObjY,60,128,-3);
	loop(60)
	{
		if(alpha>0){alpha-=256/60;}
		yield;
	}
	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-150){aa=0+rand(-15,15)-90;}
			if(xx<=150){aa=180+rand(-15,15)-90;}
			let spd=rand(0.6,1.2);
			let sp=spd;
			let r=spd/2*60;
			if(xx+r*sin(aa)<BossObjX){movesprite(BossObj,4,64,64,64,imgBossObj,60);}
			if(xx+r*sin(aa)>=BossObjX){movesprite(BossObj,4,128,64,64,imgBossObj,60);}
			loop(60)
			{
				if(ObjEnemy_GetInfo(BossObj, INFO_LIFE) > 0)
				{
					ObjMove_SetDestAtSpeed(BossObj,BossObjX+r*sin(aa),BossObjY+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;
	if(dif==1){lp=12;}
	if(dif==2){lp=15;}
	if(dif==3){lp=20;}
	floating=false;
	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);}}
	move;
	ObjEnemy_SetDamageRate(BossObj,70-GetCommonData("diff",1)*10,(70-GetCommonData("diff",1)*10)/2);
	floating=true;
	let code=1;
	while(ObjEnemy_GetInfo(BossObj, INFO_LIFE) > 0) 
	{
		if(code>3){code=1;}
		let a=rand(0,360);
		let xx=BossObjX;
		let yy=BossObjY;
		let sp;
		if(code==1)
		{
			sp=1.8;
			let mm=m*3;
			loop(lp)
			{
				let aa=0;
				loop(8)
				{
					if(ObjEnemy_GetInfo(BossObj,INFO_LIFE)>0){shotx(xx,yy,1,1,sp,a+aa,0,132,30,30,BLEND_ADD_ARGB,mm,180);}
					aa+=45;
				}
				loop(120/lp){yield;}
				sp-=1.2/lp;
				a+=120/lp*m;
				mm-=(m*3)/lp;
			}
		}
		if(code==2)
		{
			sp=2.4;
			let mm=m*6;
			loop(lp)
			{
				let aa=0;
				loop(6)
				{
					if(ObjEnemy_GetInfo(BossObj,INFO_LIFE)>0){shotx(xx,yy,1,1,sp,a+aa,0,215,30,30,BLEND_ADD_ARGB,mm,120);}
					aa+=60;
				}
				loop(120/lp){yield;}
				sp-=1.8/lp;
				a-=180/lp*m;
				mm-=(m*6)/lp;
			}
		}
		if(code==3)
		{
			sp=3;
			let mm=m*9;
			loop(lp)
			{
				let aa=0;
				loop(4)
				{
					if(ObjEnemy_GetInfo(BossObj,INFO_LIFE)>0){shotx(xx,yy,1,1,sp,a+aa,0,255,30,30,BLEND_ADD_ARGB,mm,60);}
					aa+=90;
				}
				loop(120/lp){yield;}
				sp-=2.4/lp;
				a+=240/lp*m;
				mm-=(m*9)/lp;
			}
		}
		let w=30;
		loop(6)
		{
			a=rand(0,360);
			loop(lp)
			{
				if(ObjEnemy_GetInfo(BossObj,INFO_LIFE)>0)
				{
					shot(BossObjX,BossObjY,1,1,1.2,a,0,15,w,60,BLEND_SUBTRACT);
					shot(BossObjX,BossObjY,1,1,1.2,a+2,0,15,w+5,60,BLEND_SUBTRACT);
					shot(BossObjX,BossObjY,1,1,1.2,a-2,0,15,w+5,60,BLEND_SUBTRACT);
					shot(BossObjX,BossObjY,1,1,1.2,a,0,15,w+10,60,BLEND_SUBTRACT);
				}
				a+=360/lp;
			}
			w+=20;
			loop(10){yield;}
		}
		code++;
		m*=-1;
	}
}

task fadeout(x,y,a,st,w,t,blend)
{
	let obj = CreateShotA1(x,y,0,a,st,0);
	ObjRender_SetBlendType(obj,blend);
	ObjShot_SetIntersectionEnable(obj,true);
	let al=256;
	loop(w){if(Obj_IsDeleted(obj)){itemdrop(x,y,100);}yield;}
	ObjShot_SetIntersectionEnable(obj,false);
	loop(t)
	{
		ObjRender_SetAlpha(obj,al);
		al-=256/t;
		yield;
	}
	Obj_Delete(obj);
}

task shotx(x,y,rx,ry,s,a,aa,st,w,t,blend,av,dur)
{
	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;
	let c=0;
	let cc=1;
	while(!Obj_IsDeleted(obj))
	{
		if(cc>0){cc-=1/dur;}
		if(c<=0){c=0;}
		c++;
		xx=ObjMove_GetX(obj);
		yy=ObjMove_GetY(obj);
		ObjMove_SetAngle(obj,ObjMove_GetAngle(obj)+av*cc);
		ObjMove_SetSpeed(obj,ObjMove_GetSpeed(obj)+s/t);
		if(c%5==0){fadeout(xx,yy,ObjMove_GetAngle(obj),st,15,15,blend);}
		yield;
	}
	itemdrop(xx,yy,100);
}

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