#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 = 9000;
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) != 4){SetCommonData("bgMode",4);}
	spriteIdle(BossObj,120,imgBossObj,1);
	enemyMarker(BossObj);
	loop(120){yield;}
	move(320,240,60);loop(60){yield;}
	spriteIdle(BossObj,180,imgBossObj,1);
	playSE(seSpell0,90);
	ObjEnemyBossScene_StartSpell(objScene);
	spellName(BossObj,"Spring Arcanum \"Devastation in Three Steps\"");
	loop(180){yield;}
	magicCircle;
	ObjEnemyBossScene_SetSpellTimer(objScene,3960);
	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,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);
	spriteIdle(BossObj,180,imgBossObj,1);
	bossHitbox = false;
	damage = 0;
	let fps = 60;
	let c = 0;
	let xyz = 64;
	let time = 60;
	loop(30)
	{
		c += 1/180;
		fps -= 1.5;
		DeleteShotInCircle(TYPE_SHOT,TYPE_FADE,BossObjX,BossObjY,1280 * c);
		StartSlow(TARGET_ALL,fps);
		charge(sfxGlow,BLEND_ADD_ARGB,210,135,195,xyz,BossObjX,BossObjY,time,60,0,rand(0,360));
		if(BossAlpha > 0){BossAlpha -= 255/30;}
		xyz += 448/30;
		time += 60/30;
		yield;
	}
	SetCommonData("bgmBoss",false);
	playSE(seEnemyDeath1,100);
	StopSlow(TARGET_ALL);
	loop(150)
	{
		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 mainMovement
{
	let m = 1;
	let lp = 6;
	while(alive == true)
	{
		spriteIdle(BossObj,120,imgBossObj,1);loop(60){yield;}
		if(alive == true){playSE(seCharge0,55);}
		warning(0,60,60,6,240);warning(60,120,60,6,240);
		loop(60){yield;}
		crack1(BossObjX,BossObjY,m,lp);
		spriteIdle(BossObj,120,imgBossObj,1);loop(60){yield;}
		if(alive == true){playSE(seCharge0,70);}
		warning(120,180,60,6,180);warning(180,240,60,6,180);
		loop(60){yield;}
		crack2(BossObjX,BossObjY,-m,lp);
		spriteIdle(BossObj,180,imgBossObj,1);loop(60){yield;}
		if(alive == true){playSE(seCharge0,100);}
		let range = 300;
		warning(range,range+120,120,6,120);warning(range+120,range+240,120,6,120);
		warning(range+240,range+360,120,6,120);warning(range+360,range+480,120,6,120);
		loop(120){yield;}
		ScreenShake(120,10);crack3(BossObjX,BossObjY,m);
		spriteIdle(BossObj,240,imgBossObj,1);loop(240){yield;}
		m *= -1;
		if(damage < 100){damage = 100;}
		lp += 1;
	}
}

task crack1(x,y,m,lp)
{
	if(alive == true){playSE(seShot1,60);}
	let pos = [x,y];
	let time = 450;
	let spd = 2.4;
	let spd2 = 0.6;
	loop(3)
	{
		let a = rand(0,360);
		spd += 0.15;
		spd2 += 0.15;
		loop(lp)
		{
			if(alive == true)
			{
				if(m > 0){createShotA1(pos[0],pos[1],spd,a,LARGE_PURPLE,BLEND_ALPHA,time,m,spd2);}
				if(m < 0){createShotA1(pos[0],pos[1],spd,a,LARGE_BLUE,BLEND_ALPHA,time,m,spd2);}
			}
			a += 360/lp;
		}
		loop(10){yield;}
		time -= 10;
		lp += 1;
	}
}

task crack2(x,y,m,lp)
{
	if(alive == true){playSE(seShot2,80);}
	let pos = [x,y];
	let time = 270;
	let spd = 1.2;
	let spd2 = 0.6;
	loop(3)
	{
		let a = rand(0,360);
		spd += 1.2;
		spd2 += 0.15;
		loop(lp)
		{
			if(alive == true)
			{
				if(m < 0){createShotA1(pos[0]+60*cos(a),pos[1]+60*sin(a),spd,a,BL_SMALL_DARKBLUE,BLEND_ALPHA,time,m,spd2);}
				if(m > 0){createShotA1(pos[0]+60*cos(a),pos[1]+60*sin(a),spd,a,BL_SMALL_DARKPURPLE,BLEND_ALPHA,time,m,spd2);}
			}
			a += 360/lp;
		}
		loop(10){yield;}
		time -= 10;
		lp += 2;
	}
}

task crack3(x,y,m)
{
	if(alive == true){playSE(seExplode0,100);}
	let pos = [x,y];
	let lp = 12;
	let time = 180;
	let spd = 0.75;
	let spd2 = 0.75;
	loop(3)
	{
		let a = rand(0,360);
		spd2 += 0.15;
		loop(lp)
		{
			if(alive == true)
			{
				if(m > 0){createShotA1(pos[0]+180*cos(a),pos[1]+180*sin(a),spd,a,BUBBLE_RED,BLEND_ADD_ARGB,time,m,spd2);}
				if(m < 0){createShotA1(pos[0]+180*cos(a),pos[1]+180*sin(a),spd,a,BUBBLE_RED,BLEND_ADD_ARGB,time,m,spd2);}
			}
			a += 360/lp;
		}
		loop(10){yield;}
		spd += 10.2;
		time -= 10;
		lp += 4;
	}
}

task warning(r1,r2,time,lp,alp)
{
	let c = 0;
	let w = 5;
	loop(time/w)
	{
		if(alive == true)
		{
			charge(sfxRing,BLEND_ADD_ARGB,210,135,195,r1+(r2-r1)/lp*c,320,240,w*lp,alp,360/lp,0);
			charge(sfxRing,BLEND_ADD_ARGB,210,135,195,r1+(r2-r1)/lp*c,320,240,w*lp,alp,360/lp,0);
		}
		loop(w){yield;}
		c++;
		if(c > lp){c = 1;}
	}
}

task createShotA1(x,y,s,a,st,blend,d,m,ss)
{

	let obj = CreateShotA1(x,y,s,a,st,0);
	if(st >= 313 && st <= 320){ObjShot_SetAutoDelete(obj,false);}
	ObjRender_SetAlpha				(obj,0);
	ObjShot_SetIntersectionEnable	(obj,false);
	
	if(!Obj_IsDeleted(obj) && alive == true)
	{
		let ag = 0;
		let r = 8;
		if(st >= 313 && st <= 320){r = 16;}
		if(st >= 409 && st <= 416){r = 32;}
		loop(5){createShotA1petal(x,y,ag,st,ag,r);ag += 72;}
		if(st < 409 || st > 416){Glow(64);}
	}
	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,210,135,195);
		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);
	}
	let cm = 1;
	loop(60)
	{
		cm -= 1/60;
		ObjMove_SetAngularVelocity		(obj,-6*m*cm);
		ObjMove_SetSpeed(obj,ObjMove_GetSpeed(obj)-s/60);
		yield;
	}
	ObjMove_SetAngularVelocity		(obj,0);
	ObjMove_SetSpeed(obj,0);
	loop(d-60){yield;}
	ObjShot_FadeDelete(obj);
	
	task createShotA1petal(x,y,a,st,aa,r)
	{
		let objAlp = CreateShotA1(x,y,0,a,st,0);
		if(st >= 313 && st <= 320){ObjShot_SetAutoDelete(obj,false);}
		ObjRender_SetAlpha				(objAlp,0);
		ObjShot_SetIntersectionEnable	(objAlp,false);
		let alpha = 0;
		let c = 0;
		loop(d)
		{
			if(c < 1){c += 1/(d-60);}
			if(alpha < 255){alpha += 255/60;if(alpha >= 255){ObjShot_SetIntersectionEnable	(objAlp,true);}}
			ObjRender_SetAlpha(objAlp,alpha);
			let pos = [ObjMove_GetX(obj)+r*c*sin(ObjMove_GetAngle(obj)+aa),ObjMove_GetY(obj)+r*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 += m*(1-c)*1800/d;
			yield;
		}
		if(st >= 313 && st <= 320){ObjShot_SetAutoDelete(obj,true);}
		if(st >= 409 && st <= 416)
		{
			let agg = atan2(BossObjY-ObjRender_GetY(objAlp),BossObjX-ObjRender_GetX(objAlp))+180;
			ObjMove_SetAngle(objAlp,agg);
		}
		while(!Obj_IsDeleted(objAlp) && ObjMove_GetSpeed(objAlp) < ss)
		{
			ObjMove_SetSpeed(objAlp,ObjMove_GetSpeed(objAlp) + ss/120);
			yield;
		}
	}
}