#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-120,0);
	ObjRender_SetPosition(ImgBossT,GetStgFrameWidth/2+15,GetStgFrameHeight/2-120+3,0);
	ObjRender_SetPosition(ImgBossS,GetStgFrameWidth/2-15,GetStgFrameHeight/2-120,0);
	mainTask;
} 

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

@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.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
{
	loop(60){yield;}
	Barrier(GetStgFrameWidth/2,GetStgFrameHeight/2-120,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;
	}
	BossMarker(BossObj);
	Aura;
	loop(60){yield;}
	ObjEnemyBossScene_SetSpellTimer(objScene,60*60);
	
	mainshooter;
	TEnd;
}

task TEnd
{
	while(ObjEnemy_GetInfo(BossObj,INFO_LIFE)>0){yield;}
	scoring(GetGraze,GetPlayerLife,GetPlayerSpell,GetPlayerPower);
	otherdrop(GetStgFrameWidth/2-15,GetStgFrameHeight/2-120,100,"p");
	auraeffect=false;
	hitbox=false;
	Barrier(GetStgFrameWidth/2,GetStgFrameHeight/2-120,60,128,3);
	loop(60)
	{
		if(alpha>0){alpha-=256/60;}
		yield;
	}
	loop(60)
	{
		DeleteShotAll(TYPE_ALL,TYPE_FADE);
		yield;
	}
	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=12;}
	if(dif==3){lp=15;}
	ObjEnemy_SetDamageRate(BossObj,60-GetCommonData("diff",1)*10,(60-GetCommonData("diff",1)*10)/2);
	hitbox=true;
	while(ObjEnemy_GetInfo(BossObj,INFO_LIFE)>0)
	{
		loop(3)
		{
			loop(6)
			{
				a=rand(0,360);
				loop(lp)
				{
					shot(GetStgFrameWidth/2,GetStgFrameHeight/2-120,1,1,1.2,a,0,132,30,60,BLEND_ADD_ARGB);
					shot(GetStgFrameWidth/2,GetStgFrameHeight/2-120,1,1,1.2,a+180/lp,0,132,30,120,BLEND_ADD_ARGB);
					a+=360/lp;
				}
				loop(30){yield;}
			}
			lasers(m,lp);
			m*=-1;
		}
		bubbles(m,lp);
	}
}

task bubbles(m,lp)
{
	let a=rand(0,360);
	loop(lp)
	{
		let t=120;
		let w=60;
		let c=1;
		loop(10)
		{
			c++;
			t+=30;
			w+=30;
			if(c%2==0)
			{
				shot(GetStgFrameWidth/2,GetStgFrameHeight/2-120,1,1,1.2,a,0,255,w,t,BLEND_ADD_ARGB);
				shot(GetStgFrameWidth/2,GetStgFrameHeight/2-120,1,1,1.2,a+180/lp,0,255,w,t,BLEND_SUBTRACT);
			}
			if(c%2==1)
			{
				shot(GetStgFrameWidth/2,GetStgFrameHeight/2-120,1,1,1.2,a,0,255,w,t,BLEND_SUBTRACT);
				shot(GetStgFrameWidth/2,GetStgFrameHeight/2-120,1,1,1.2,a+180/lp,0,255,w,t,BLEND_ADD_ARGB);
			}
		}
		a+=360/lp;
	}
}

task lasers(m,lp)
{
	let a=rand(0,360);
	loop(lp)
	{
		laser(GetStgFrameWidth/2-240*m,GetStgFrameHeight/2+360*cos(a),90-60*m,640,32,60,132,120,BLEND_SUBTRACT);
		loop(60/lp){yield;}
		a+=360/lp*m;
	}
}

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,blend)
{
	let obj=CreateStraightLaserA1(x,y,a,0,w,dt,st,d);
	ObjRender_SetBlendType(obj,blend);
	ObjShot_SetAutoDelete(obj,false);
	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");}
}