#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 mirrorX1;
let mirrorX2;
let mirrorX3;
let mirrorX4;
let mirrorX5;

let mirrorY1;
let mirrorY2;
let mirrorY3;
let mirrorY4;
let mirrorY5;

let imgBossObj = dir ~ "sprite/dot_tenkai.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","tenkai");
	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_ADD_ARGB);}
	loop(30){yield;}
	SEPlay(dir ~ "se/spell_0.wav");
	ObjEnemyBossScene_StartSpell(objScene);
	SpellDeclare(2,"tenkai");
	SpellLeft(BossObj,5,3,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,"s");
	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-120){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(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=3;}
	if(dif==2){lp=5;}
	if(dif==3){lp=7;}
	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_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);}}
	SEPlay(dir ~ "se/laser_2.wav");
	let a=rand(0,360);
	let cc=1;
	loop(5)
	{
		mirror(cc,a);
		a+=72*3;
		cc++;
	}
	loop(30){yield;}
	move;
	ObjEnemy_SetDamageRate(BossObj,90-GetCommonData("diff",1)*10,(90-GetCommonData("diff",1)*10)/2);
	floating=true;
	let count=1;
	while(ObjEnemy_GetInfo(BossObj, INFO_LIFE) > 0) 
	{
		loop(3)
		{
			let xx=BossObjX;
			let yy=BossObjY;
			let xsp=rand(0.9,1)*6/(lp+2);
			loop(5)
			{
				let c=1;
				if(count==1)
				{
					loop(lp+4)
					{
						if(ObjEnemy_GetInfo(BossObj,INFO_LIFE)>0){shotx(xx,yy,1,1,rand(0,360),0,152,30,BLEND_ADD_ARGB,xsp/4*c,m);}
						c+=2;
					}
				}
				if(count==2)
				{
					loop(lp+4)
					{
						if(ObjEnemy_GetInfo(BossObj,INFO_LIFE)>0){shotx(xx,yy,1,1,rand(0,360),0,152,30,BLEND_ADD_ARGB,xsp/4*c,-m);}
						c+=2;
					}
				}
				if(count==3)
				{
					loop(lp+4)
					{
						if(ObjEnemy_GetInfo(BossObj,INFO_LIFE)>0)
						{
							shotx(xx,yy,1,1,rand(0,360),0,152,30,BLEND_ADD_ARGB,xsp/4*c,1);
							shotx(xx,yy,1,1,rand(0,360),0,152,30,BLEND_ADD_ARGB,xsp/4*c,-1);
						}
						c+=2;
					}
				}
				loop(6){yield;}
			}
			loop(10){yield;}
			count++;
			if(count>3){count=1;}
		}
		let a=rand(0,360);
		loop(lp)
		{
			if(ObjEnemy_GetInfo(BossObj,INFO_LIFE)>0)
			{
				laser(BossObjX,BossObjY,a,a,640,24,60,132,60,0.4,m,BLEND_SUBTRACT);
				laser(BossObjX,BossObjY,a,a+90,640,24,60,132,60,0.4,m,BLEND_SUBTRACT);
				laser(BossObjX,BossObjY,a,a+180,640,24,60,132,60,0.4,m,BLEND_SUBTRACT);
				laser(BossObjX,BossObjY,a,a-90,640,24,60,132,60,0.4,m,BLEND_SUBTRACT);
			}
			a+=360/lp;
		}
		m*=-1;
		loop(60){yield;}
	}
}

task mirror(code,aa)
{
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj,dir ~ "eff/barrier.png");
	ObjSprite2D_SetSourceRect(obj,0,0,256,256);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	let yy;
	if(code==1){yy=GetStgFrameHeight/2-50;}
	if(code==2){yy=GetStgFrameHeight/2+20;}
	if(code==3){yy=GetStgFrameHeight/2+90;}
	if(code==4){yy=GetStgFrameHeight/2+160;}
	if(code==5){yy=GetStgFrameHeight/2+230;}
	let c=0;
	let min=30;
	let delete=false;
	loop
	{
		if(ObjEnemy_GetInfo(BossObj, INFO_LIFE)<=0){delete=true;} 
		if(delete==false && c<1){c+=1/min;}
		if(delete==true && c>0){c-=1/min;}
		if(delete==true && c<=0){Obj_Delete(obj);}
		ObjRender_SetAlpha(obj,256*c);
		ObjRender_SetScaleXYZ(obj,1/4*c,1/32*c,1);
		ObjRender_SetPosition(obj,GetStgFrameWidth/2+(GetStgFrameWidth/2-256/8)*sin(aa),yy,0);
		aa+=0.5;
		if(code==1)
		{
			mirrorX1=ObjRender_GetX(obj);
			mirrorY1=ObjRender_GetY(obj);
		}
		if(code==2)
		{
			mirrorX2=ObjRender_GetX(obj);
			mirrorY2=ObjRender_GetY(obj);
		}
		if(code==3)
		{
			mirrorX3=ObjRender_GetX(obj);
			mirrorY3=ObjRender_GetY(obj);
		}
		if(code==4)
		{
			mirrorX4=ObjRender_GetX(obj);
			mirrorY4=ObjRender_GetY(obj);
		}
		if(code==5)
		{
			mirrorX5=ObjRender_GetX(obj);
			mirrorY5=ObjRender_GetY(obj);
		}
		yield;
	}
}

task shotx(x,y,rx,ry,a,aa,st,w,blend,xsp,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);
	let xx=0;
	let yy=0;
	let xsp2=2.4;
	let xsp3=0.06;
	let limitx=33;
	let limity=5;
	let c=0;
	let cmax=5;
	ObjMove_AddPatternB2(obj,1,xsp*m,-xsp2,0,xsp3,xsp*2*m,xsp2*5);
	while(!Obj_IsDeleted(obj))
	{
		xx=ObjMove_GetX(obj);
		yy=ObjMove_GetY(obj);
		c++;
		if(yy>mirrorY1-limity&&yy<mirrorY1+limity&&xx>mirrorX1-limitx&&xx<mirrorX1+limitx&&c>cmax)
		{
			xsp*=-1;
			ObjMove_AddPatternB2(obj,1,xsp*m,-xsp2,0,xsp3,xsp*2*m,xsp2*10);
			c=0;
		}
		if(yy>mirrorY2-limity&&yy<mirrorY2+limity&&xx>mirrorX2-limitx&&xx<mirrorX2+limitx&&c>cmax)
		{
			xsp*=-1;
			ObjMove_AddPatternB2(obj,1,xsp*m,-xsp2,0,xsp3,xsp*2*m,xsp2*10);
			c=0;
		}
		if(yy>mirrorY3-limity&&yy<mirrorY3+limity&&xx>mirrorX3-limitx&&xx<mirrorX3+limitx&&c>cmax)
		{
			xsp*=-1;
			ObjMove_AddPatternB2(obj,1,xsp*m,-xsp2,0,xsp3,xsp*2*m,xsp2*10);
			c=0;
		}
		if(yy>mirrorY4-limity&&yy<mirrorY4+limity&&xx>mirrorX4-limitx&&xx<mirrorX4+limitx&&c>cmax)
		{
			xsp*=-1;
			ObjMove_AddPatternB2(obj,1,xsp*m,-xsp2,0,xsp3,xsp*2*m,xsp2*10);
			c=0;
		}
		if(yy>mirrorY5-limity&&yy<mirrorY5+limity&&xx>mirrorX5-limitx&&xx<mirrorX5+limitx&&c>cmax)
		{
			xsp*=-1;
			ObjMove_AddPatternB2(obj,1,xsp*m,-xsp2,0,xsp3,xsp*2*m,xsp2*10);
			c=0;
		}
		yield;
	}
	itemdrop(xx,yy,100);
}

task laser(x,y,a,aa,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;
	if(!Obj_IsDeleted(obj)){SEPlay(dir ~ "se/warning.wav");}
	loop(d)
	{
		c+=1/d;
		r+=2*(1-c)*m;
		aa-=av*m*(1-c);
		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");}
}