#include"./lib/lib_shot/shot_const.txt"
#include"./lib/lib_sound/_PathSound.txt"
#include"./lib/lib_img/_PathImage.txt"
#include"./function.txt"

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

let BossAlpha = 255;
let hitbox = [64,54];
let imgBossObj 	= bossSprite;

let bossHitbox = true;
let damage = 0;
let life = 4000;
let alive = true;

let objScene = GetEnemyBossSceneObjectID();

@Initialize 
{
	SetAutoDeleteObject	(true);
	BossObj = ObjEnemy_Create	(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist				(BossObj);
	ObjRender_SetPosition		(BossObj,320,120,0);
	mainTask;
} 

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

@MainLoop 
{
	ObjEnemy_SetDamageRate	(BossObj,damage,damage);
	ObjRender_SetAlpha		(BossObj,BossAlpha);
	BossObjX = ObjMove_GetX	(BossObj);
	BossObjY = ObjMove_GetY	(BossObj);
	if(bossHitbox == true)
	{
		ObjEnemy_SetIntersectionCircleToShot(BossObj,BossObjX,BossObjY,hitbox[0]/2);
		ObjEnemy_SetIntersectionCircleToPlayer(BossObj,BossObjX,BossObjY,hitbox[1]/2);
	}
	if(ObjEnemy_GetInfo(BossObj, INFO_LIFE) > 0){alive = true;}
	if(ObjEnemy_GetInfo(BossObj, INFO_LIFE) <= 0){alive = false;}
	yield;
} 

@Finalize 
{ 
} 

task mainTask
{
	spriteIdle(BossObj,GetCommonData("bgModeTime",0),imgBossObj,1);
	SetCommonData("bgMode",3);
	loop(GetCommonData("bgModeTime",0)){yield;}
	magicCircle;
	enemyMarker(BossObj);
	ObjEnemyBossScene_SetSpellTimer(objScene,3600);
	spawnClouds;
	mainMovement;
	TEnd;
}

task magicCircle
{
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture			(obj,sfxMagicCircle);
	ObjSprite2D_SetSourceRect	(obj,0,0,512,512);
	ObjSprite2D_SetDestCenter	(obj);
	ObjRender_SetColor			(obj,150,150,195);
	ObjRender_SetBlendType		(obj,BLEND_ADD_ARGB);
	let c = 0.5;
	let ang = rand(0,360);
	visibility;
	task visibility
	{
		let alpha = 0;
		while(alive == true)
		{
			if(damage > 0)
			{
				if(alpha < 240 && bossHitbox == true){alpha += 4;}
				if(alpha > 0 && bossHitbox == false){alpha -= 4;}
			}
			if(damage <= 0 && alpha > 0){alpha -= 4;}
			ObjRender_SetAlpha(obj,alpha);
			yield;
		}
		while(alpha > 0)
		{
			alpha -= 4;
			ObjRender_SetAlpha(obj,alpha);
		}
		Obj_Delete(obj);
	}
	let mm = 1;
	while(!Obj_IsDeleted(obj))
	{
		loop(60)
		{
			c += 1/60*mm;
			ObjRender_SetScaleXYZ	(obj,(128/512)*(0.8+c*0.2/0.5),128/512*(0.8+c*0.2/0.5),1);
			ObjRender_SetAngleZ		(obj,ang);
			ObjRender_SetPosition	(obj,BossObjX,BossObjY,0);
			ang += 6 *c;
			yield;
		}
		mm *= -1;
	}
}

task TEnd
{
	while(alive == true){yield;}
	SetCommonData("autoSnap",true);
	bossHitbox = false;
	damage = 0;
	let ang = -(atan2(BossObjY-120,BossObjX-320))-90;
	let spd = GetDistance(320,120,BossObjX,BossObjY)/60;
	let sp = spd;
	let r = sp * 120;
	spriteIdle(BossObj,120,imgBossObj,1);
	if(BossObjX + r * sin(ang) < BossObjX-16)	{spriteHold(BossObj,120,imgBossObj,0,1);} // Going Left
	if(BossObjX + r * sin(ang) > BossObjX+16)	{spriteHold(BossObj,120,imgBossObj,1,1);} // Going Right
	let c = 0;
	loop(120)
	{
		c += 1/180;
		if(BossAlpha < 255){BossAlpha += 255/60;}
		DeleteShotInCircle(TYPE_SHOT,TYPE_FADE,BossObjX,BossObjY,720 * c);
		ObjMove_SetDestAtSpeed(BossObj,BossObjX+r*sin(ang),BossObjY+r*cos(ang),sp);
		sp -= spd/120;
		yield;
	}
	spriteIdle(BossObj,60,imgBossObj,1);
	loop(60)
	{
		c += 1/180;
		if(BossAlpha < 255){BossAlpha += 255/60;}
		DeleteShotInCircle(TYPE_SHOT,TYPE_FADE,BossObjX,BossObjY,1280 * c);
		yield;
	}
	Obj_Delete(BossObj);
	CloseScript(GetOwnScriptID());
}

task move(x,y,time)
{
	let ang = -(atan2(BossObjY-y,BossObjX-x))-90;
	let spd = GetDistance(x,y,BossObjX,BossObjY)/(time/2);
	let sp = spd;
	let r = sp * time;
	if(BossObjX + r * sin(ang) < BossObjX && alive == true)	{spriteHold(BossObj,time,imgBossObj,0,1);} // Going Left
	if(BossObjX + r * sin(ang) >= BossObjX && alive == true){spriteHold(BossObj,time,imgBossObj,1,1);} // Going Right
	loop(time)
	{
		if(alive == true){ObjMove_SetDestAtSpeed(BossObj,BossObjX+r*sin(ang),BossObjY+r*cos(ang),sp);}
		sp -= spd/time;
		yield;
	}
	ObjMove_SetSpeed(BossObj,0);
}

task mainMovement
{
	let wave = 1;
	while(alive == true)
	{
		spriteIdle(BossObj,120,imgBossObj,1);loop(120){yield;}
		move(380,180,120);loop(240){yield;}
		spriteIdle(BossObj,120,imgBossObj,1);loop(120){yield;}
		move(320,120,60);loop(60){yield;}
		if(wave > 1){bubbleFormation;}
		spriteIdle(BossObj,60,imgBossObj,1);loop(60){yield;}
		if(damage <= 0){damage = 100;}
		spriteIdle(BossObj,120,imgBossObj,1);loop(120){yield;}
		move(260,180,120);loop(240){yield;}
		spriteIdle(BossObj,120,imgBossObj,1);loop(120){yield;}
		move(320,120,60);loop(60){yield;}
		if(wave > 1){bubbleFormation;}
		spriteIdle(BossObj,60,imgBossObj,1);loop(60){yield;}
		wave ++;
	}
}

task spawnClouds
{
	let m = 1;
	while(alive == true)
	{
		loop(60){yield;}
		let a = rand(0,360);
		loop(2)
		{
			let aa = 0;
			loop(6)
			{
				spawn(BossObjX,BossObjY,6,a+aa,m,0);
				spawn(BossObjX,BossObjY,3,a+aa-15*m,m,15);
				aa += 60;
			}
			a += 30*m;
			loop(30){yield;}
		}
		loop(15){BossAlpha -= 255/15;yield;}
		bossHitbox = false;
		loop(225){yield;}
		spreadFormation(-m);
		bossHitbox = true;
		loop(15){BossAlpha += 255/15;yield;}
		loop(225){yield;}
		m *= -1;
	}
	task spawn(x,y,s,a,m,d)
	{
		if(d > 0){loop(d){yield;}}
		let obj = CreateShotA1(x,y,s,a,0,0);
		ObjRender_SetAlpha				(obj,0);
		ObjShot_SetIntersectionEnable	(obj,false);
		ObjMove_SetAngularVelocity		(obj,-m*1.5);
		trails;
		task trails
		{
			while(!Obj_IsDeleted(obj) && alive == true)
			{
				loop(5){yield;}
				charge(sfxCloud,BLEND_ADD_ARGB,120,120,120,128,ObjMove_GetX(obj),ObjMove_GetY(obj),20,120,rand(-1,1),rand(0,360));
			}
		}
		loop(120)
		{
			ObjMove_SetSpeed(obj,ObjMove_GetSpeed(obj)-s/120);
			yield;
		}
		ObjMove_SetSpeed(obj,0);
		let post = [BossObjX,BossObjY];
		let ag = atan2(post[1]-ObjMove_GetY(obj),post[0]-ObjMove_GetX(obj));
		let dis = GetDistance(ObjMove_GetX(obj),ObjMove_GetY(obj),post[0],post[1]);
		if(ObjMove_GetX(obj) > 0 && ObjMove_GetX(obj) < 640 && ObjMove_GetY(obj) > 0 && ObjMove_GetY(obj) < 480 && alive == true)
		{
			playSE(seShot1,40);
			let aa = atan2(120-ObjMove_GetY(obj),320-ObjMove_GetX(obj));
			createShotA1flower(ObjMove_GetX(obj),ObjMove_GetY(obj),1.2,aa+60,0,120,m);
			createShotA1flower(ObjMove_GetX(obj),ObjMove_GetY(obj),1.2,aa-60,0,120,m);
		}
		loop(60){yield;}
		ObjMove_SetDestAtFrame(obj,post[0],post[1],120);
		loop(120){yield;}
		Obj_Delete(obj);
	}
}

task spreadFormation(m)
{
	if(alive == true){playSE(seShot0,60);}
	let pos = [BossObjX,BossObjY];
	let a = rand(0,360);
	let sp = 0.6;
	loop(12)
	{
		let aa = 0;
		loop(10)
		{
			sp += 1.8/120;
			if(alive == true)
			{
				createShotA1(pos[0],pos[1],sp,a+aa,BL_SMALL_DARKAQUA,30,BLEND_ALPHA,m);
				createShotA1(pos[0],pos[1],sp,a+aa+180,BL_SMALL_AQUA,30,BLEND_ALPHA,m);
			}
			aa += 36*m;
		}
		a += 15*m;
		loop(10){yield;}
	}
}

task bubbleFormation
{
	if(alive == true){playSE(seShot2,80);}
	let a = atan2(GetPlayerY-BossObjY,GetPlayerX-BossObjX);
	let pos = [BossObjX,BossObjY];
	let spd = 2.4;
	loop(6)
	{
		spd -= 0.3;
		let aa = -60;
		loop(6)
		{
			if(alive == true){createShotA1(pos[0],pos[1],spd,a+aa,BUBBLE_BLUE,30,BLEND_ADD_ARGB,0);}
			aa += 120/5;
		}
		loop(5){yield;}
	}
}

task createShotA1(x,y,s,ang,st,d,blend,m)
{
	loop(d-30){yield;}
	let obj = CreateShotA1(x,y,s,ang,st,0);
	ObjRender_SetAlpha				(obj,0);
	ObjShot_SetIntersectionEnable	(obj,false);
	ObjMove_SetAngularVelocity		(obj,m*0.75);
	ObjRender_SetBlendType			(obj,blend);
	let alpha = 0;
	while (alpha < 255 && !Obj_IsDeleted(obj))
	{
		alpha += 255/30;
		ObjRender_SetAlpha(obj,alpha);
		yield;
	}
	ObjShot_SetIntersectionEnable	(obj,true);
	loop(240){yield;}
	ObjMove_SetAngularVelocity		(obj,0);
}

task createShotA1flower(x,y,s,ang,st,d,mspin)
{
	let obj = CreateShotA1(x,y,s,ang,st,0);
	ObjRender_SetAlpha				(obj,0);
	ObjShot_SetIntersectionEnable	(obj,false);
	
	if(!Obj_IsDeleted(obj) && alive == true)
	{
		let ag = 0;
		loop(5){createShotA2petal(x,y,0,ag,0.005,0.6,SMALL_GRAY,d,ag+36,8);ag += 72;}
		loop(5){createShotA2petal(x,y,0,ag,0.005,1.2,R_DOT_RED,d,ag,4);ag += 72;}
		Glow(32);
	}
	task Glow(xyz)
	{
		let objGlow = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture			(objGlow,sfxGlow);
		ObjSprite2D_SetSourceRect	(objGlow,0,0,512,512);
		ObjSprite2D_SetDestCenter	(objGlow);
		ObjRender_SetColor			(objGlow,120,120,120);
		ObjRender_SetBlendType		(objGlow,BLEND_ADD_ARGB);
		let alpha = 0;
		while(!Obj_IsDeleted(obj))
		{
			if(alpha < 60){alpha ++;}
			xyz += 64/d;
			ObjRender_SetAlpha(objGlow,alpha);
			ObjRender_SetScaleXYZ(objGlow,xyz/512,xyz/512,1);
			ObjRender_SetPosition(objGlow,ObjMove_GetX(obj),ObjMove_GetY(obj),0);
			yield;
		}
		while(alpha > 0)
		{
			alpha --;
			ObjRender_SetAlpha(objGlow,alpha);
			yield;
		}
		Obj_Delete(objGlow);
	}
	loop(d)
	{
		ObjMove_SetSpeed(obj,ObjMove_GetSpeed(obj)-s/d);
		yield;
	}
	if(alive == true && !Obj_IsDeleted(obj)){playSE(seShiny1,30);}
	ObjShot_FadeDelete(obj);
	task createShotA2petal(x,y,s,ang,acc,maxs,st,d,aa,rr)
	{
		let objAlp = CreateShotA2(x,y,s,ang,acc,maxs,st,0);
		ObjRender_SetAlpha				(objAlp,0);
		ObjShot_SetIntersectionEnable	(objAlp,false);
		let alpha = 0;
		let c = 0;
		loop(d)
		{
			if(c < 1){c += 1/d;}
			if(alpha < 255){alpha += 255/(d-60);if(alpha >= 255){ObjShot_SetIntersectionEnable	(objAlp,true);}}
			ObjRender_SetAlpha(objAlp,alpha);
			let pos = [ObjMove_GetX(obj)+rr*c*sin(ObjMove_GetAngle(obj)+aa),ObjMove_GetY(obj)+rr*c*cos(ObjMove_GetAngle(obj)+aa)];
			ObjMove_SetPosition(objAlp,pos[0],pos[1]);
			let agg = atan2(ObjMove_GetY(obj)-ObjRender_GetY(objAlp),ObjMove_GetX(obj)-ObjRender_GetX(objAlp))+180;
			ObjMove_SetAngle(objAlp,agg);
			aa += mspin*(1-c)*20;
			yield;
		}
		loop(240){yield;}
		ObjShot_FadeDelete(objAlp);
	}
}

