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

let bossHitbox = false;
let damage = 0;
let life = 3000;
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
{
	SetCommonData("bgMode",0);
	loop(450){yield;}
	spriteIdle(BossObj,360,imgBossObj,1);
	playSE(seCharge0,100);
	mistCharge;
	loop(240){yield;}
	bossHitbox = true;
	loop(120){BossAlpha += 255/120;yield;}
	spriteIdle(BossObj,GetCommonData("bgModeTime",0),imgBossObj,1);
	SetCommonData("bgMode",4);
	loop(GetCommonData("bgModeTime",0)){yield;}
	spriteIdle(BossObj,120,imgBossObj,1);
	playSE(seCharge0,100);
	ScreenShake(120,5);
	loop(120){yield;}
	ScreenShake(120,10);
	playSE(seExplode0,100);
	magicCircle;
	enemyMarker(BossObj);
	ObjEnemyBossScene_SetSpellTimer(objScene,1200);
	mainMove;
	TEnd;
}

task mistCharge
{
	let pos = [320,120];
	let xyz = 45;
	let time = 0;
	let alpha = 0;
	let range = 480;
	let lp = 24;
	let m = 1;
	let lpp = 12;
	let ang = rand(0,360);
	loop(lp)
	{
		time 	+= 120/lp;
		alpha 	+= 150/lp;
		xyz 	+= 105/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(240/lp){yield;}
		m *= -1;
	}
}

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,210,135,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(alpha < 240 && damage > 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/150;
		DeleteShotInCircle(TYPE_SHOT,TYPE_FADE,BossObjX,BossObjY,1280 * c);
		ObjMove_SetDestAtSpeed(BossObj,BossObjX+r*sin(ang),BossObjY+r*cos(ang),sp);
		sp -= spd/120;
		yield;
	}
	spriteIdle(BossObj,30,imgBossObj,1);
	loop(30)
	{
		c += 1/150;
		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 mainMove
{
	let m = 1;
	let spd = 3.6;
	let lp = 4;
	while(alive == true)
	{
		loop(2)
		{
			let a = rand(0,360);
			loop(lp)
			{
				if(alive == true)
				{
					createShotA2(BossObjX,BossObjY,0,a+180/lp,(spd+0.3)/60,spd+0.3,LARGE_GRAY,60,BLEND_ADD_ARGB);
					createShotA2(BossObjX,BossObjY,0,a+60/lp*m,(spd+0.6)/60,spd+0.6,SMALL_GRAY,60,BLEND_ADD_ARGB);
					createShotA2(BossObjX,BossObjY,0,a+120/lp*m,(spd+0.9)/60,spd+0.9,SMALL_GRAY,60,BLEND_ADD_ARGB);
					createShotA2(BossObjX,BossObjY,0,a,spd/60,spd,LARGE_PURPLE,60,BLEND_ALPHA);
				}
				a += 360/lp;
			}
			loop(3)
			{
				if(alive == true){playSE(seShot1,20);}
				a = rand(0,360);
				loop(lp)
				{
					if(alive == true){createShotA2(BossObjX,BossObjY,0,a,(spd+1.2)/60,spd+1.2,PELLET_DARKPURPLE,60,BLEND_ALPHA);}
					a += 360/lp;
				}
				loop(10){yield;}
			}
			m *= -1;
		}
		spd -= 2.4/20;
		lp ++;
		if(lp >= 13 && damage <= 0){damage = 100;}
	}
}

task createShotA2(x,y,s,ang,acc,maxs,st,d,blend)
{
	let obj = CreateShotA2(x,y,s,ang,acc,maxs,st,0);
	ObjRender_SetBlendType			(obj,blend);
	ObjRender_SetAlpha				(obj,0);
	ObjShot_SetIntersectionEnable	(obj,false);
	let alpha = 0;
	while (alpha < 255 && !Obj_IsDeleted(obj))
	{
		alpha += 255/d;
		ObjRender_SetAlpha(obj,alpha);
		yield;
	}
	ObjShot_SetIntersectionEnable	(obj,true);
}