#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 = [48,54];
let imgBossObj 	= bossSprite;

let bossHitbox = false;
let damage = 0;
let life = 3500;
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
{
	if(GetCommonData("bgMode",0) != 3){SetCommonData("bgMode",3);}
	mistRelease;
	spriteIdle(BossObj,120,imgBossObj,1);
	loop(60)
	{
		BossAlpha -= 255/60;
		yield;
	}
	BossAlpha = 0;
	loop(60){yield;}
	ObjMove_SetPosition(BossObj,320,240);
	stormFormation;
	loop(240){yield;}
	playSE(seSpell0,90);
	ObjEnemyBossScene_StartSpell(objScene);
	spellName(BossObj,"Winter Sigh \"Strange Labyrinth of Inhalation\"");
	loop(180){yield;}
	ObjEnemyBossScene_SetSpellTimer(objScene,1800);
	TEnd;
}

task mistRelease
{
	let pos = [320,120];
	let xyz = 180;
	let time = 120;
	let alpha = 180;
	let range = 30;
	let lp = 24;
	let m = 1;
	let lpp = 6;
	let ang = rand(0,360);
	loop(lp)
	{
		time 	-= 120/lp;
		alpha 	-= 150/lp;
		xyz 	-= 165/lp;
		range 	+= 450/lp;
		lpp		+= 6/lp;
		loop(lpp)
		{
			let post = [pos[0]+range*sin(ang),pos[1]+range*cos(ang)];
			charge(sfxCloud,BLEND_ADD_ARGB,255,255,255,xyz,post[0],post[1],time,alpha,m,ang);
			ang += 360 / lpp;
			ang = ang%360;
		}
		ang += 360 / lp;
		loop(120/lp){yield;}
		m *= -1;
	}
}

task stormFormation
{
	let r = 510;
	let alpha = 255;
	let a = rand(0,360);
	let cc = 0;
	let lp1 = 10;
	let lp2 = 5;
	while(alive == true)
	{
		loop(30)
		{
			cc++;
			a += 1.4;
			if(r > 270){r --;}
			if(alpha > 0){alpha -= 255/240;}
			let ag = atan2(BossObjY-GetPlayerY,BossObjX-GetPlayerX);
			if(cc >= 240){ObjMove_SetPosition(GetPlayerObjectID,GetPlayerX+0.6*cos(ag),GetPlayerY+0.6*sin(ag));}
			yield;
		}
		if(cc>= 240 && cc%60 == 0 && alive == true){playSE(seShot1,40);}
		loop(lp1)
		{
			createShotA1(BossObjX+r*sin(a),BossObjY+r*cos(a),3.6,-a+90+120,BUBBLE_BLUE,30,BLEND_ADD_ARGB,alpha,240);
			createShotA1(BossObjX+r*sin(a),BossObjY+r*cos(a),3.6,-a+90-120,BUBBLE_BLUE,30,BLEND_ADD_ARGB,alpha,240);
			charge(sfxCloud,BLEND_ADD_ARGB,120,120,120,192,BossObjX+(r-32)*sin(a),BossObjY+(r-32)*cos(a),120,255-alpha,rand(-1,1),rand(0,360));
			charge(sfxCloud,BLEND_ADD_ARGB,120,120,120,256,BossObjX+(r+64)*sin(a+15),BossObjY+(r+64)*cos(a+15),120,255-alpha,rand(-1,1),rand(0,360));
			a += 360/lp1;
		}
		loop(lp2)
		{
			if(cc >= 240 && cc%120 == 0 && alive == true)
			{
				createShotA1(BossObjX+r*sin(a+180/lp2+5),BossObjY+r*cos(a+180/lp2+5),0.72,-a+90+180-180/lp2-5,BL_SMALL_DARKAQUA,60,BLEND_ADD_ARGB,255,315);
				createShotA1(BossObjX+r*sin(a+180/lp2),BossObjY+r*cos(a+180/lp2),0.66,-a+90+180-180/lp2,BL_SMALL_DARKAQUA,60,BLEND_ADD_ARGB,255,350);
				createShotA1(BossObjX+r*sin(a+180/lp2-5),BossObjY+r*cos(a+180/lp2-5),0.6,-a+90+180-180/lp2+5,BL_SMALL_DARKAQUA,60,BLEND_ADD_ARGB,255,390);
			}
			if(cc >= 240 && cc%120 == 60 && alive == true)
			{
				createShotA1(BossObjX+r*sin(a-5),BossObjY+r*cos(a-5),0.72,-a+90+180+5,BL_SMALL_DARKAQUA,60,BLEND_ADD_ARGB,255,315);
				createShotA1(BossObjX+r*sin(a),BossObjY+r*cos(a),0.66,-a+90+180,BL_SMALL_DARKAQUA,60,BLEND_ADD_ARGB,255,350);
				createShotA1(BossObjX+r*sin(a+5),BossObjY+r*cos(a+5),0.6,-a+90+180-5,BL_SMALL_DARKAQUA,60,BLEND_ADD_ARGB,255,390);
			}
			if(cc >= (240+360) && cc%120 == 0 && alive == true)
			{
				createShotA1(BossObjX+r*sin(a),BossObjY+r*cos(a),0.9,-a+90+180,BL_SMALL_AQUA,60,BLEND_ADD_ARGB,255,240);
				createShotA1(BossObjX+r*sin(a+3),BossObjY+r*cos(a+3),0.87,-a+90+180-3,BL_SMALL_AQUA,60,BLEND_ADD_ARGB,255,250);
				createShotA1(BossObjX+r*sin(a-3),BossObjY+r*cos(a-3),0.84,-a+90+180+3,BL_SMALL_AQUA,60,BLEND_ADD_ARGB,255,265);
				createShotA1(BossObjX+r*sin(a+6),BossObjY+r*cos(a+6),0.81,-a+90+180-6,BL_SMALL_AQUA,60,BLEND_ADD_ARGB,255,275);
				createShotA1(BossObjX+r*sin(a-6),BossObjY+r*cos(a-6),0.78,-a+90+180+6,BL_SMALL_AQUA,60,BLEND_ADD_ARGB,255,280);
			}
			if(cc >= (240+360) && cc%120 == 60 && alive == true)
			{
				createShotA1(BossObjX+r*sin(a+180/lp2),BossObjY+r*cos(a+180/lp2),0.9,-a+90+180-180/lp2,BL_SMALL_AQUA,60,BLEND_ADD_ARGB,255,240);
				createShotA1(BossObjX+r*sin(a+180/lp2-3),BossObjY+r*cos(a+180/lp2-3),0.87,-a+90+180-180/lp2+3,BL_SMALL_AQUA,60,BLEND_ADD_ARGB,255,250);
				createShotA1(BossObjX+r*sin(a+180/lp2+3),BossObjY+r*cos(a+180/lp2+3),0.84,-a+90+180-180/lp2-3,BL_SMALL_AQUA,60,BLEND_ADD_ARGB,255,265);
				createShotA1(BossObjX+r*sin(a+180/lp2-6),BossObjY+r*cos(a+180/lp2-6),0.81,-a+90+180-180/lp2+6,BL_SMALL_AQUA,60,BLEND_ADD_ARGB,255,275);
				createShotA1(BossObjX+r*sin(a+180/lp2+6),BossObjY+r*cos(a+180/lp2+6),0.78,-a+90+180-180/lp2-6,BL_SMALL_AQUA,60,BLEND_ADD_ARGB,255,285);
			}
			if(cc >= (240+720) && cc%120 == 0 && alive == true)
			{
				createShotA1(BossObjX+r*sin(a+90/lp2-6),BossObjY+r*cos(a+90/lp2-6),1.14,-a-90/lp2+90+180+6,SMALL_GRAY,60,BLEND_ADD_ARGB,255,175);
				createShotA1(BossObjX+r*sin(a+90/lp2-2),BossObjY+r*cos(a+90/lp2-2),1.08,-a-90/lp2+90+180+2,SMALL_GRAY,60,BLEND_ADD_ARGB,255,190);
				createShotA1(BossObjX+r*sin(a+90/lp2+2),BossObjY+r*cos(a+90/lp2+2),1.02,-a-90/lp2+90+180-2,SMALL_GRAY,60,BLEND_ADD_ARGB,255,205);
				createShotA1(BossObjX+r*sin(a+90/lp2+6),BossObjY+r*cos(a+90/lp2+6),0.96,-a-90/lp2+90+180-6,SMALL_GRAY,60,BLEND_ADD_ARGB,255,225);
			}
			if(cc >= (240+720) && cc%120 == 60 && alive == true)
			{
				createShotA1(BossObjX+r*sin(a-90/lp2+6),BossObjY+r*cos(a-90/lp2+6),1.14,-a+90/lp2+90+180-6,SMALL_GRAY,60,BLEND_ADD_ARGB,255,175);
				createShotA1(BossObjX+r*sin(a-90/lp2+2),BossObjY+r*cos(a-90/lp2+2),1.08,-a+90/lp2+90+180-2,SMALL_GRAY,60,BLEND_ADD_ARGB,255,190);
				createShotA1(BossObjX+r*sin(a-90/lp2-2),BossObjY+r*cos(a-90/lp2-2),1.02,-a+90/lp2+90+180+2,SMALL_GRAY,60,BLEND_ADD_ARGB,255,205);
				createShotA1(BossObjX+r*sin(a-90/lp2-6),BossObjY+r*cos(a-90/lp2-6),0.96,-a+90/lp2+90+180+6,SMALL_GRAY,60,BLEND_ADD_ARGB,255,225);
			}
			if(cc >= (240+1080) && cc%120 == 0 && alive == true)
			{
				createShotA1(BossObjX+r*sin(a-90/lp2-1),BossObjY+r*cos(a-90/lp2-1),1.2,-a+90/lp2+90+180+1,R_DOT_RED,60,BLEND_ADD_ARGB,255,165);
				createShotA1(BossObjX+r*sin(a-90/lp2+1),BossObjY+r*cos(a-90/lp2+1),1.23,-a+90/lp2+90+180-1,R_DOT_RED,60,BLEND_ADD_ARGB,255,160);
				createShotA1(BossObjX+r*sin(a-90/lp2-3),BossObjY+r*cos(a-90/lp2-3),1.26,-a+90/lp2+90+180+3,R_DOT_RED,60,BLEND_ADD_ARGB,255,155);
				createShotA1(BossObjX+r*sin(a-90/lp2+3),BossObjY+r*cos(a-90/lp2+3),1.29,-a+90/lp2+90+180-3,R_DOT_RED,60,BLEND_ADD_ARGB,255,150);
				createShotA1(BossObjX+r*sin(a-90/lp2-5),BossObjY+r*cos(a-90/lp2-5),1.32,-a+90/lp2+90+180+5,R_DOT_RED,60,BLEND_ADD_ARGB,255,145);
				createShotA1(BossObjX+r*sin(a-90/lp2+5),BossObjY+r*cos(a-90/lp2+5),1.35,-a+90/lp2+90+180-5,R_DOT_RED,60,BLEND_ADD_ARGB,255,140);
			}
			if(cc >= (240+1080) && cc%120 == 60 && alive == true)
			{
				createShotA1(BossObjX+r*sin(a+90/lp2-1),BossObjY+r*cos(a+90/lp2-1),1.23,-a-90/lp2+90+180+1,R_DOT_RED,60,BLEND_ADD_ARGB,255,165);
				createShotA1(BossObjX+r*sin(a+90/lp2+1),BossObjY+r*cos(a+90/lp2+1),1.2,-a-90/lp2+90+180-1,R_DOT_RED,60,BLEND_ADD_ARGB,255,160);
				createShotA1(BossObjX+r*sin(a+90/lp2-3),BossObjY+r*cos(a+90/lp2-3),1.29,-a-90/lp2+90+180+3,R_DOT_RED,60,BLEND_ADD_ARGB,255,155);
				createShotA1(BossObjX+r*sin(a+90/lp2+3),BossObjY+r*cos(a+90/lp2+3),1.26,-a-90/lp2+90+180-3,R_DOT_RED,60,BLEND_ADD_ARGB,255,150);
				createShotA1(BossObjX+r*sin(a+90/lp2-5),BossObjY+r*cos(a+90/lp2-5),1.35,-a-90/lp2+90+180+5,R_DOT_RED,60,BLEND_ADD_ARGB,255,145);
				createShotA1(BossObjX+r*sin(a+90/lp2+5),BossObjY+r*cos(a+90/lp2+5),1.32,-a-90/lp2+90+180-5,R_DOT_RED,60,BLEND_ADD_ARGB,255,140);
			}
			if(cc >= (240+1200) && cc%60 == 0 && alive == true)
			{
				createShotA1(BossObjX+r*sin(a),BossObjY+r*cos(a),0.15,-a+90+180,BUBBLE_BLUE,60,BLEND_ADD_ARGB,255,900);
				createShotA1(BossObjX+r*sin(a-180/lp2),BossObjY+r*cos(a-180/lp2),0.15,-a+90+180+180/lp2,BUBBLE_BLUE,60,BLEND_ADD_ARGB,255,900);
			}
			a += 360/lp2;
		}
		a = a%360;
	}
}

task TEnd
{
	while(alive == true){yield;}
	SetCommonData("autoSnap",true);
	if(GetCommonData("bgMode",3) != 0){SetCommonData("bgMode",0);}
	SetCommonData("bgmBoss",false);
	bossHitbox = false;
	damage = 0;
	let c = 0;
	loop(180)
	{
		c += 1/180;
		DeleteShotInCircle(TYPE_SHOT,TYPE_FADE,BossObjX,BossObjY,1280 * c);
		yield;
	}
	Obj_Delete(BossObj);
	CloseScript(GetOwnScriptID());
}

task createShotA1(x,y,s,ang,st,d,blend,alp,dt)
{
	let obj = CreateShotA1(x,y,s,ang,st,0);
	ObjRender_SetAlpha				(obj,0);
	ObjShot_SetIntersectionEnable	(obj,false);
	ObjRender_SetBlendType			(obj,blend);
	let alpha = 0;
	while (alpha < alp && !Obj_IsDeleted(obj))
	{
		alpha += alp/d;
		ObjRender_SetAlpha(obj,alpha);
		yield;
	}
	ObjShot_SetIntersectionEnable	(obj,true);
	loop(dt){yield;}
	ObjShot_SetIntersectionEnable	(obj,false);
	while(alpha > 0)
	{
		alpha -= alp/5;
		ObjRender_SetAlpha(obj,alpha);
		yield;
	}
	Obj_Delete(obj);
}