//--------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------
//Functions to be used in boss fights.
//--------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------

#include "./functions_stage.txt"

let objScene = GetEnemyBossSceneObjectID;
let timer = -1;
let life = -1;
let spellLife = 1/3;
let enmx;
let enmy;

let EnemySpell = 0;
let EnemyWave = false;
let TYPE_BASIC = 0;
let TYPE_FIRE = 1;
let TYPE_ICE = 2;
let TYPE_GLOW = 3;

task TLife(delay,hit1,hit2,dam,bomb,ex)
	{
	TEnd;
	let c = -1;
	let bspell = false;
	if(ObjEnemyBossScene_GetInfo(objScene,INFO_IS_SPELL))
		{
		bspell = true;
		dam *= spellLife;
		bomb *= spellLife;
		}
	while(!Obj_IsDeleted(objEnemy))
		{
		c+=1;
		if(c < delay || (ex == true && bspell == true && IsPlayerSpellActive == true))
			{
			ObjEnemy_SetDamageRate(objEnemy,0,0);
			}
		else
			{
			ObjEnemy_SetDamageRate(objEnemy,dam,bomb);
			}
		if(hit1 >= 0)
			{
			ObjEnemy_SetIntersectionCircleToShot(objEnemy,ObjMove_GetX(objEnemy),ObjMove_GetY(objEnemy),hit1);
			}
		if(hit2 >= 0)
			{
			ObjEnemy_SetIntersectionCircleToPlayer(objEnemy,ObjMove_GetX(objEnemy),ObjMove_GetY(objEnemy),hit2);
			}
		enmx = ObjMove_GetX(objEnemy);
		enmy = ObjMove_GetY(objEnemy);
		yield;
		}
	}

task TEnd
{
	ObjEnemyBossScene_SetSpellTimer(objScene,timer*60);
	while(ObjEnemyBossScene_GetInfo(objScene,INFO_TIMERF) > 0)
		{
		if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE) <= 0)
			{
			break;
			}
		yield;
		}
	ObjShot_DeleteAll(TARGET_ENEMY);
	Obj_Delete(objEnemy);
	loop(3)
		{
		ObjShot_DeleteAll(TARGET_ENEMY);
		yield;
		}
	ObjShot_DeleteAll(TARGET_ENEMY);
	yield;
	CloseScript(GetOwnScriptID);
}

task ObjEnemy_Move(obj,w,s,x,y,d,dir)
	{
	let n = round(rand(0,1))*2-1;
	if(dir == 2 && cos(GetAngleToPlayer(obj)) != 0)
		{
		n = absolute(cos(GetAngleToPlayer(obj)))/cos(GetAngleToPlayer(obj));
		}
	else if(dir != 0)
		{
		n = dir;
		}
	if(ObjMove_GetX(obj)-x < d)
		{
		n =1;	
		}
	else if (ObjMove_GetX(obj)+x > GetStgFrameWidth-d)
		{	
		n = -1;
		}
	y = GetStgFrameHeight*0.5-y;
	x *= n;
	ObjMove_SetDestAtWeight(obj,ObjMove_GetX(obj)+x,y,w,s);
	}

task TSpellName(text)
{
	ObjEnemyBossScene_StartSpell(objScene);
	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);
	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;
		}
	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 CreateEnemyIcon(obj)
{
	let img = [];
	
	ascent(i in 0..2)
		{
		img = img ~ [ObjPrim_Create(OBJ_SPRITE_2D)];
		ObjPrim_SetTexture(img[i],"");
		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(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]);
		}
}