#include "./../../system/functions.txt"
let Dif = GetCommonData("DIFFICULTY",1);

let objScene = GetEnemyBossSceneObjectID();
let bspell = false;
let EnemySpell = 0;
let spellNames = GetCommonData("SPELL_NAMES",["ERROR"]);
let mid = GetCommonData("MIDBOSS",false);

let talkFace = [];

task TLife(delay,hit1,hit2,damage1,bomb1)
{
	let counter = 0;
	if(bspell == true)
		{
		damage1 /= spellLife;
		bomb1 /= spellLife;
		}
	let time = 0;
	let timeStop = false;
	while(!Obj_IsDeleted(objEnemy))
		{
		counter+=1;
		if(!Obj_IsDeleted(objEnemy))
			{
			if(counter < delay)
				{
				ObjEnemy_SetDamageRate(objEnemy,0,0);	
				}
			else
				{
				ObjEnemy_SetDamageRate(objEnemy,damage1,bomb1);	
				}
			if(hit1 > 0)
				{
				ObjEnemy_SetIntersectionCircleToShot(objEnemy,ObjMove_GetX(objEnemy),ObjMove_GetY(objEnemy),hit1);
				}
			}
		if(GetPlayerInvincibilityFrame <= 0 && hit2 > 0 && GetObjectDistance(objEnemy,objPlayer) <= hit2)
			{
			let shot = CreateBulletA1(GetPlayerX,GetPlayerY,0,0,BUBBLE,0,1,SHOT_WHITE);
			Obj_SetVisible(shot,false);
			Obj_DelayDelete(shot,2);
			}
		yield;
		}
}

task ObjEnemy_Move(obj,weight,speed,x,y,distm,dir)
{	
	let numb =  round(rand(0,1))*2-1;
	if(dir == 2 && cos(GetAngleToPlayer(obj)) != 0)
		{
		numb = absolute(cos(GetAngleToPlayer(obj)))/cos(GetAngleToPlayer(obj));
		}
	else if(dir != 0)
		{
		numb = dir;
		}
	if(ObjMove_GetX(obj)-x < distm)
		{
		numb =1;	
		}
	else if (ObjMove_GetX(obj)+x > GetStgFrameWidth-distm)
		{	
		numb = -1;
		}
	y = GetStgFrameHeight/2-y;
	x *= numb;
	ObjMove_SetDestAtWeight(obj,ObjMove_GetX(obj)+x,y,weight,speed);
}
	
task TSpellName(text)
{
	ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID());
	let spell = ObjText_Create;
	ObjText_SetText(spell, text);
	ObjText_SetFontColorTop(spell,250,250,250);
	ObjText_SetFontColorBottom(spell,125,125,125);
	ObjText_SetFontBorderWidth(spell,1);
	ObjText_SetFontBorderColor(spell,0,0,0);
	ObjText_SetFontBorderType(spell,BORDER_FULL);
	ObjText_SetHorizontalAlignment(spell, ALIGNMENT_RIGHT);
	ObjText_SetMaxWidth(spell, GetStgFrameWidth);
	ObjText_SetFontType(spell,"Cirno");
	ObjText_SetFontSize(spell, 17);
	Obj_SetRenderPriorityI(spell,75);
	ObjText_SetFontBold(spell,true);
	ObjText_SetSidePitch(spell,1);
	ObjRender_SetScaleXYZ(spell,1,1.25,0);
	let xM = ObjText_GetTotalWidth(spell);
	let x = xM;
	let y = 30;
	let alpha = 255;

	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj,GetCurrentScriptDirectory ~ "../../system/img/Default_System.png");	
	ObjSprite2D_SetSourceRect(obj,0,53,256,90);
	ObjRender_SetScaleXYZ(obj,1.4,1.4,0);
	Obj_SetRenderPriorityI(obj,74);
	ObjRender_SetPosition(spell,x,y,0);
	ObjRender_SetPosition(obj,x,y+3,0);
	EnemySpell = -1;
	ascent(i in 0..30)
		{
		x-=(xM)/(30*0.925);
		ObjRender_SetPosition(spell,x+16,y,0);
		ObjRender_SetPosition(obj,-256+i*(GetStgFrameWidth*1.25)/30,y+6,0);
		ObjSprite2D_SetDestCenter(obj);
		yield;
		}
	EnemySpell = 0;
	while(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)>0)
		{
		if(GetPlayerY < GetStgFrameHeight/4)
			{
			if(alpha > 20)	
				{
				alpha -=255/10;
				}
			else
				{
				alpha = 20;
				}
			}
		else
			{
			if(alpha < 255)	
				{
				alpha +=255/10;
				}
			else
				{
				alpha = 255;
				}
			}
		ObjRender_SetAlpha(spell,alpha);
		ObjRender_SetAlpha(obj,alpha);
		yield;
		}
	Obj_Delete(spell);
	Obj_Delete(obj);
}

task TSpellFace(path)
{

}


task TEnd(type)
{
	ObjEnemyBossScene_SetSpellTimer(objScene,timer*60);
	let path = "STAGE" ~ IntToString(GetCommonData("STAGE",1)) ~ "_SPELL";
	let numb = GetAreaCommonData("STAGE_UNLOCK",path,[false]);
	let unlock = [];
	ascent(i in 0..length(numb))
		{
		if(i == GetCommonData("CURRENT_SPELL",Dif)+Dif)
			{
			unlock = unlock ~ [true];
			}
		else
			{
			unlock = unlock ~ [GetAreaCommonData("STAGE_UNLOCK",path,[false])[i]];
			}
		}
	SetAreaCommonData("STAGE_UNLOCK",path,unlock);
	SaveCommonDataAreaA2("STAGE_UNLOCK",GetCurrentScriptDirectory ~ "../../system/data/SCORE");
	
	while(ObjEnemyBossScene_GetInfo(objScene,INFO_TIMERF) > 0)
		{
		if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE) <= 0)
			{
			End;
			break;
			}
		if(GetPlayerLife < 0)
			{
			CloseStgScene;
			}
		yield;
		}
	if(Obj_GetValueD(objEnemy,"LAST_SPELL",false) == true)
		{
		TFinalExplosion;
		}
	End;
	function End	
		{
		Obj_SetValue(GetEnemyBossSceneObjectID,"ENMX",enmx);
		Obj_SetValue(GetEnemyBossSceneObjectID,"ENMY",enmy);
		if(Obj_GetValueD(objEnemy,"LAST_SPELL",false) == true)
			{
			TFinalExplosion;
			}
		Obj_Delete(objEnemy);
		DeleteShotAll(TYPE_ALL,TYPE_ITEM);
		loop(3){yield;}
		if(GetCommonData("STAGE_TYPE","") != "TYPE_SPELL_PRACTICE")
			{
			SetCommonData("CURRENT_SPELL",GetCommonData("CURRENT_SPELL",Dif)+4);
			}
		DeleteShotAll(TYPE_ALL,TYPE_ITEM);
		CloseScript(GetOwnScriptID);
		}
}

function TFinalExplosion
	{
	SetPlayerInvincibilityFrame(120);
	ascent(t in 0..22)
		{
		if(t == 5)
			{
			PlaySound("se_Die3",200);
			StartSlow(TARGET_ALL,30);
			}
		ObjEffect_Explosion(enmx,enmy);
		ObjEffect_Explosion(enmx,enmy);
		loop(5){yield;}
		}
	StopSlow(TARGET_ALL);
	Obj_Delete(objEnemy);		
	return;
	}

task TXY
{
	while(!Obj_IsDeleted(objEnemy))
		{
		enmx = ObjMove_GetX(objEnemy);	
		enmy = ObjMove_GetY(objEnemy);	
		yield;
		}
}

task CreateEnemyIcon(obj)
{
	let img = [];
	
	ascent(i in 0..2)
		{
		img = img ~ [ObjPrim_Create(OBJ_SPRITE_2D)];
		ObjPrim_SetTexture(img[i],GetCurrentScriptDirectory ~ "img/WhiteSquare.png");
		ObjSprite2D_SetSourceRect(img[i],214,0,255,18);
		Obj_SetRenderPriority(img[i],0.3);
		}
	ObjRender_SetColorHSV(img[0],180,255,175);
	ObjSprite2D_SetDestRect(img[0],-GetStgFrameWidth*10,-0.75,GetStgFrameWidth*10,0.75);
	ObjRender_SetColorHSV(img[1],0,255,175);
	ObjSprite2D_SetDestRect(img[1],-0.75,-GetStgFrameHeight*10,0.75,GetStgFrameHeight*10);

	let alpha = 0;
	while(!Obj_IsDeleted(objEnemy) && !Obj_IsDeleted(obj))
		{
		if(Obj_GetValueD(obj,"ICON",true) == false)
			{
			if(alpha > 0.3)
				{
				alpha-=0.7/10;
				}
			}
		else if(alpha < 1)
			{
			alpha+=0.7/10;
			}
		else
			{
			alpha = 1;
			}
		ascent(i in 0..2)
			{
			ObjRender_SetPosition(img[i],ObjMove_GetX(obj),ObjMove_GetY(obj),0);
			ObjRender_SetAlpha(img[i],alpha*105);
			}
		if(IsBeyondClip(obj,64))
			{
			ascent(i in 0..2)
				{
				Obj_SetVisible(img[i],false);
				}		
			}
		else
			{
			ascent(i in 0..2)
				{
				Obj_SetVisible(img[i],true);
				}		
			}
		yield;
		}
	ascent(i in 0..2)
		{
		Obj_Delete(img[i]);
		}
}