#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 = 2000;
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) != 1){SetCommonData("bgMode",1);}
	playSE(seCharge0,90);
	spriteIdle(BossObj,360,imgBossObj,1);
	enemyMarker(BossObj);
	fireball;
	loop(180){yield;}
	playSE(seExplode0,90);
	ScreenShake(120,10);
	ObjEnemyBossScene_StartSpell(objScene);
	spellName(BossObj,"Summer Fire \"Blazing Earth Spirits\"");
	loop(180){yield;}
	magicCircle;
	ObjEnemyBossScene_SetSpellTimer(objScene,3600);
	mainMovement;
	TEnd;
}

task fireball
{
	let pos = [320,120];
	let a = rand(0,360);
	let height = 480-128-120+32;
	let blast = false;
	loop(15){cinder(a);a += 24;}
	let c = 0;
	loop(180)
	{
		c += 1/180;
		pos[1] = pos[1] + 3.5*c;yield;
	}
	blast = true;
	loop(180){pos[1] = pos[1] - 60/180;yield;}
	task cinder(ang)
	{
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture			(obj,sfxGlow);
		ObjSprite2D_SetSourceRect	(obj,0,0,512,512);
		ObjSprite2D_SetDestCenter	(obj);
		ObjRender_SetColor			(obj,180,120,60);
		ObjRender_SetBlendType		(obj,BLEND_ADD_ARGB);
		let alpha = 0;
		let r = [0,0];
		let c = 0;
		let cc = 0;
		while(alive == true)
		{
			if(alpha < 60 && blast == false){alpha += 60/60;}
			if(alpha < 180 && blast == true){if(ang >= 270 || ang <= 90){alpha += 180/60;}}
			if(alpha > 0 && blast == true && ang < 270 && ang > 90){alpha -= 180/60;}
			ObjRender_SetAlpha(obj,alpha);
			if(c < 1){c += 1/240;}
			ObjRender_SetScaleXYZ		(obj,256/512*c,256/512*c,1);
			if(blast == false && r[0] < 64){r[0] = r[0] + 64/180;}
			if(blast == true && r[0] < 360){r[0] = r[0] + 296/180;}
			if(blast == false && r[1] < 64){r[1] = r[1] + 64/180;}
			if(blast == true && r[1] < 120){r[1] = r[1] + 56/180;}
			ObjRender_SetPosition(obj,pos[0]+r[0]*sin(ang),pos[1]+r[1]*cos(ang),0);
			cc ++;
			if(cc%15 == 0 && blast == true && alpha >= 120)
			{
				let xyz = absolute(rand(1,4))*16;
				let post = [ObjRender_GetX(obj),ObjRender_GetY(obj)+rand(-(128-xyz),-(128-xyz)*2)];
				charge(sfxGlow,BLEND_ALPHA,240,60,60,xyz,post[0],post[1],60,180,0,rand(0,360));
			}
			ang += c*0.2;
			ang = ang%360;
			yield;
		}
		while(alpha > 0)
		{
			alpha -= 180/60;
			ObjRender_SetAlpha(obj,alpha);
			yield;
		}
		Obj_Delete(obj);
	}
}

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,255,30,30);
	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/180;
		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,60,imgBossObj,1);
	loop(60)
	{
		c += 1/180;
		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 swingMove(x,y,xx,yy,time)
{
	let r = [0,0];
	let ang = [0,0];
	if(BossObjX > 320)
	{
		if(alive == true){spriteHold(BossObj,time,imgBossObj,0,1);}
		ang[0] = -(atan2(y-BossObjY,x-BossObjX))-90;
		r[0] = GetDistance(x,y,BossObjX,BossObjY);
		ang[1] = -(atan2(y-yy,x-xx))-90;
		r[1] = GetDistance(x,y,xx,yy);
	}
	if(BossObjX < 320)
	{
		if(alive == true){spriteHold(BossObj,time,imgBossObj,1,1);}
		ang[0] = -(atan2(y-BossObjY,x-BossObjX))-90;
		r[0] = GetDistance(x,y,BossObjX,BossObjY);
		ang[1] = -(atan2(y-yy,x-xx))-90;
		r[1] = GetDistance(x,y,xx,yy);
	}
	let angDif = ang[1]-ang[0];
	let rDif = r[1]-r[0];
	loop(time)
	{
		ang[0] = ang[0] + angDif/time;
		r[0] = r[0] + rDif/time;
		if(alive == true){ObjRender_SetPosition(BossObj,x+r[0]*sin(ang[0]),y+r[0]*cos(ang[0]),0);}
		yield;
	}
}

task mainMovement
{
	let wave = 1;
	while(alive == true)
	{
		spriteIdle(BossObj,60,imgBossObj,1);loop(60){yield;}
		move(380,150,60);loop(60){yield;}
		move(740,180,60);if(wave > 1){blastFormation;}loop(60){yield;}
		swingMove(320,-560,-100,180,240);
		warningCircle(240,1);
		loop(6)
		{
			loop(20){yield;}
			if(alive == true && BossObjX > 0 && BossObjX < 640)
			{
				playSE(seShot2,80);
				createShotA2ball(BossObjX,BossObjY+180,5.4,-90,0,120,1,-1.2,5);
			}
			loop(20){yield;}
		}
		move(200,150,60);loop(60){yield;}
		move(320,120,60);if(wave > 1){blastFormation;}loop(60){yield;}
		spriteIdle(BossObj,60,imgBossObj,1);loop(60){yield;}
		if(damage < 100){damage = 100;}
		spriteIdle(BossObj,60,imgBossObj,1);loop(60){yield;}
		move(260,150,60);loop(60){yield;}
		move(-100,180,60);if(wave > 1){blastFormation;}loop(60){yield;}
		swingMove(320,-560,740,180,240);
		warningCircle(240,-1);
		loop(6)
		{
			loop(20){yield;}
			if(alive == true && BossObjX > 0 && BossObjX < 640)
			{
				playSE(seShot2,80);
				createShotA2ball(BossObjX,BossObjY+180,5.4,-90,0,120,1,-1.2,5);
			}
			loop(20){yield;}
		}
		move(440,150,60);loop(60){yield;}
		move(320,120,60);if(wave > 1){blastFormation;}loop(60){yield;}
		spriteIdle(BossObj,60,imgBossObj,1);loop(60){yield;}
		wave += 1;
	}
}

task warningCircle(time,m)
{
	let c = 0;
	let w = 5;
	let lp = 6;
	let r = (hitbox[1])/(lp*2+1);
	loop(time/w)
	{
		if(alive == true)
		{
			charge(sfxRing,BLEND_ADD_ARGB,240,60,60,1740,320,-570-r/2*(1+c*2),w*lp,120,360/(w*lp)*m,0);
			charge(sfxRing,BLEND_ADD_ARGB,240,60,60,1740,320,-570+r/2*(1+c*2),w*lp,120,360/(w*lp)*m,0);
		}
		loop(w){yield;}
		c++;
		if(c > lp){c = 1;}
	}
}

task createShotA2ball(x,y,s,ang,st,d,mspin,ss,delay)
{
	loop(delay){yield;}
	let obj = CreateShotB2(x,y,s*cos(ang),s*sin(ang),0,-(s*sin(ang))/d*2,s*cos(ang),-s*sin(ang),st,0);
	ObjRender_SetAlpha				(obj,0);
	ObjShot_SetIntersectionEnable	(obj,false);
	if(!Obj_IsDeleted(obj) && alive == true)
	{
		let ag = rand(0,360);
		loop(8)
		{
			createShotA1cinder(x,y,0,ag,ORB_RED,d,ag,16);
			createShotA1cinder(x,y,0,ag+18,ORB_RED,d,ag+22.5,32);
			ag += 45;
		}
		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,240,60,60);
		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);
	}
	task Light(x,y)
	{
		loop(30){yield;}
		let objLight = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture			(objLight,sfxGlow);
		ObjSprite2D_SetSourceRect	(objLight,0,0,512,512);
		ObjSprite2D_SetDestCenter	(objLight);
		ObjRender_SetColor			(objLight,240,60,60);
		ObjRender_SetBlendType		(objLight,BLEND_ADD_ARGB);
		ObjRender_SetPosition		(objLight,x,y+256,0);
		ObjRender_SetScaleXYZ		(objLight,256/512,512/512,1);
		let alpha = 0;
		while(alive == true && alpha < 120)
		{
			alpha += 2;
			ObjRender_SetAlpha(objLight,alpha);
			yield;
		}
		while(alpha > 0)
		{
			alpha -= 2;
			ObjRender_SetAlpha(objLight,alpha);
			yield;
		}
		Obj_Delete(objLight);
	}
	loop(d-60){yield;}
	if(alive == true && !Obj_IsDeleted(obj))
	{
		playSE(seShot1,40);
		Light(ObjMove_GetX(obj),ObjMove_GetY(obj)-120);
	}
	loop(60){yield;}
	if(!Obj_IsDeleted(obj))
	{
		let a = rand(0,360);
		loop(10)
		{
			createShotA2(ObjMove_GetX(obj),ObjMove_GetY(obj),0,a,0.005,0.3,BL_SMALL_ORANGE,60,BLEND_ALPHA);
			createShotA2(ObjMove_GetX(obj),ObjMove_GetY(obj),0,a+18,0.005,0.6,BL_SMALL_ORANGE,60,BLEND_ALPHA);
			a += 36;
		}
	}
	Obj_Delete(obj);
	task createShotA1cinder(x,y,s,ang,st,d,aa,r)
	{
		let objAdd = CreateShotA1(x,y,s,ang,st,0);
		ObjRender_SetAlpha				(objAdd,0);
		ObjRender_SetBlendType			(objAdd,BLEND_ADD_ARGB);
		let pos = [0,0,0];
		let alpha = 0;
		let c = 0;
		loop(d)
		{
			alpha += 255/d;
			ObjRender_SetAlpha(objAdd,alpha);
			c += 1/d;
			pos[0] = ObjMove_GetX(obj)+r*c*sin(ObjMove_GetAngle(obj)+aa);
			pos[1] = ObjMove_GetY(obj)+r*c*cos(ObjMove_GetAngle(obj)+aa);
			pos[2] = atan2(ObjMove_GetY(obj)-ObjRender_GetY(objAdd),ObjMove_GetX(obj)-ObjRender_GetX(objAdd))+180;
			ObjMove_SetPosition(objAdd,pos[0],pos[1]);
			ObjMove_SetAngle(objAdd,pos[2]);
			aa += mspin*(1-c)*5;
			yield;
		}
		if(!Obj_IsDeleted(objAdd) && alive == true)
		{
			ObjShot_FadeDelete(objAdd);
			lantern(pos[0],pos[1],0,90+rand(-15,15),-0.005,-r/16*1.2,FIRE_RED,60);
		}
	}
}

task lantern(x,y,s,ang,acc,maxs,st,d)
{
	let obj = CreateShotA2(x,y,s,ang,acc,maxs,st,0);
	ObjRender_SetAlpha				(obj,0);
	ObjShot_SetIntersectionEnable	(obj,false);
	Ring;
	let alpha = 0;
	while(alpha < 255)
	{
		alpha += 255/d;
		ObjRender_SetAlpha	(obj,alpha);
		yield;
	}
	task Ring
	{
		let objRing = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture			(objRing,sfxGlow);
		ObjSprite2D_SetSourceRect	(objRing,0,0,512,512);
		ObjSprite2D_SetDestCenter	(objRing);
		ObjRender_SetColor			(objRing,240,60,60);
		ObjRender_SetBlendType		(objRing,BLEND_ADD_ARGB);
		ObjRender_SetScaleXYZ		(objRing,64/512,64/512,1);
		let alpha = 0;
		let pos = [0,0];
		while(!Obj_IsDeleted(obj) || ObjMove_GetY(obj) > 0)
		{
			if(alpha < 60){alpha ++;}
			ObjRender_SetAlpha(objRing,alpha);
			pos = [ObjMove_GetX(obj),ObjMove_GetY(obj)];
			ObjRender_SetPosition(objRing,ObjMove_GetX(obj),ObjMove_GetY(obj),0);
			yield;
		}
		let c = 1;
		while(alpha > 0)
		{
			c += 1/60;
			alpha --;
			ObjRender_SetAlpha			(objRing,alpha);
			ObjRender_SetScaleXYZ		(objRing,64/512*c,64/512*c,1);
			ObjRender_SetPosition		(objRing,pos[0],pos[1],0);
			yield;
		}
		Obj_Delete(objRing);
	}
	let pos = [0,0];
	while(ObjMove_GetY(obj) > 0){pos = [ObjMove_GetX(obj),ObjMove_GetY(obj)];yield;}
	if(alive == true && pos[0] > 0 && pos[0] < 640)
	{
		playSE(seShot0,20);
		createShotA2(pos[0],pos[1],0,75,0.01,rand(0.6,1.2),R_DOT_RED,60,BLEND_ALPHA);
		createShotA2(pos[0],pos[1],0,105,0.01,rand(0.6,1.2),R_DOT_RED,60,BLEND_ALPHA);
		createShotA2(pos[0],pos[1],0,45,0.01,rand(1.2,1.8),R_DOT_RED,75,BLEND_ALPHA);
		createShotA2(pos[0],pos[1],0,135,0.01,rand(1.2,1.8),R_DOT_RED,75,BLEND_ALPHA);
	}
}

task blastFormation
{
	let a = atan2(GetPlayerY-BossObjY,GetPlayerX-BossObjX);
	if(alive == true)
	{
		playSE(seShiny0,60);
		loop(18)
		{
			createShotA2(BossObjX,BossObjY,1.2,a,-0.01,0.6,ORB_RED,45,BLEND_ADD_ARGB);
			a+= 20;
		}
	}
}

task createShotA2(x,y,s,ang,acc,maxs,st,d,blend)
{
	loop(d-30){yield;}
	let obj = CreateShotA2(x,y,s,ang,acc,maxs,st,0);
	ObjRender_SetAlpha				(obj,0);
	ObjShot_SetIntersectionEnable	(obj,false);
	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);
}
