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

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

let talkFace = [];

task TLife(delay,hit1,hit2,damage1,bomb1,damage2,bomb2)
{
	let counter = 0;
	if(bspell == true)
		{
		damage1 /= spellLife;
		bomb1 /= spellLife;
		damage2 /= spellLife;
		bomb2 /= spellLife;
		}
	let shot = CreateBulletA1(enmx,enmy,0,0,DS_BALL_S_WHITE,0,hit2/16);
	Obj_SetVisible(shot,false);
	ObjShot_SetSpellResist(shot,true);	
	ObjShot_SetAutoDelete(shot,false);
	while(!Obj_IsDeleted(objEnemy))
		{
		counter+=1;
		if(!Obj_IsDeleted(objEnemy))
			{
			if(counter >=delay)
				{
				ObjEnemy_SetDamageRate(objEnemy,damage2,bomb2);		
				}
			else
				{
				ObjEnemy_SetDamageRate(objEnemy,damage1,bomb1);	
				}
			if(hit1 > 0)
				{
				ObjEnemy_SetIntersectionCircleToShot(objEnemy,ObjMove_GetX(objEnemy),ObjMove_GetY(objEnemy),hit1);
				}
			}
		ObjMove_SetPosition(shot,enmx,enmy);
		yield;
		}
	Obj_Delete(shot);
}

task TShake
{
	let xM = Get2DCameraX;
	let yM = Get2DCameraY;
	let dist = 3;
	loop(30)
		{
		dist+=0.5;
		Set2DCameraFocusX(xM+rand(-1,1)*dist);
		Set2DCameraFocusY(yM+rand(-1,1)*dist);
		yield;
		}
	Set2DCameraFocusX(xM);
	Set2DCameraFocusY(yM);
}

task ObjEnemy_Move(obj,weight,speed,distm,distM,distym,distyM,distx,vary)
{	
	let numb =  round(rand(0,1))*2-1;
	if(numb == 0 && cos(GetAngleToPlayer(obj)) != 0)
		{
		numb = absolute(cos(GetAngleToPlayer(obj)))/cos(GetAngleToPlayer(obj));
		}
	else if(ObjMove_GetX(obj) < distx)
		{
		numb =1;	
		}
	else if (ObjMove_GetX(obj) > GetStgFrameWidth-distx)
		{	
		numb = -1;
		}
	let y = GetStgFrameHeight/2-rand(distym,distyM);
	let x = numb*rand(distm,distM);
	ObjMove_SetDestAtWeight(obj,ObjMove_GetX(obj)+x,y,weight,speed);
}
	
task TSpellName
{
	let spell = ObjText_Create;
	ObjText_SetText(spell, ToString(spellNames[GetCommonData("CURRENT_SPELL",Dif)]));
	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,"Century Gothic");
	ObjText_SetFontSize(spell, 16);
	ObjRender_SetScaleX(spell,1.125);
	Obj_SetRenderPriorityI(spell,75);
	ObjText_SetFontBold(spell,true);
	ObjText_SetSidePitch(spell,1);
	ObjRender_SetScaleXYZ(spell,0.875,1.35,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..45)
		{
		x-=(xM)/45;
		ObjRender_SetPosition(spell,x+16,y,0);
		ObjRender_SetPosition(obj,-256+i*(GetStgFrameWidth*1.25)/45,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 TBossName(name)
{
	let scene = GetEnemyBossSceneObjectID;
	if(scene == ID_INVALID){return;}
	let x = 14;
	let y = 15;
	let z = 0;
	let spell = ObjText_Create;
	ObjText_SetText(spell, name);
	ObjText_SetFontColorTop(spell,75,250,150);
	ObjText_SetFontColorBottom(spell,50,150,75);
	ObjText_SetFontBorderWidth(spell,1);
	ObjText_SetFontBorderColor(spell,0,0,0);
	ObjText_SetFontBorderType(spell,BORDER_SHADOW);
	ObjText_SetHorizontalAlignment(spell, ALIGNMENT_LEFT);
	ObjText_SetMaxWidth(spell, GetStgFrameWidth);
	ObjText_SetFontSize(spell,16);
	Obj_SetRenderPriorityI(spell,75);
	ObjText_SetFontType(spell,"Century Gothic");
	let alpha = 255;
	loop(20)
		{
		ObjRender_SetPosition(spell,x,y,0);
		ObjRender_SetAlpha(spell,z);
		z+=255/19;
		yield;
		}	
	while(Obj_IsDeleted(scene) == false)
		{
		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);
		yield;
		}
	Obj_Delete(spell);
}

task TSpellFace(path)
{

}


task TEnd
{
	let numb = GetAreaCommonData("SCORE", "SPELL_UNLOCK",[false]);
	let unlock = [];
	ascent(i in 0..length(numb))
		{
		if(i == GetCommonData("CURRENT_SPELL",Dif)+Dif)
			{
			unlock = unlock ~ [true];
			}
		else
			{
			unlock = unlock ~ [GetAreaCommonData("SCORE", "SPELL_UNLOCK",[false])[i]];
			}
		}
	SetAreaCommonData("SCORE", "SPELL_UNLOCK",unlock);
	SaveCommonDataAreaA2("SCORE",GetCurrentScriptDirectory ~ "../../system/data/SCORE");
	
	loop(timer*60-1)
		{
		if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE) <= 0)
			{
			End;
			}
		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(1){yield;}
		if(GetCommonData("PRACTICE",false)==false)
			{
			SetCommonData("CURRENT_SPELL",GetCommonData("CURRENT_SPELL",Dif)+3);
			}	
		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);
			}
		TExplosion(objEnemy);
		TExplosion(objEnemy);
		loop(5){yield;}
		}
	StopSlow(TARGET_ALL);
	Obj_Delete(objEnemy);		
	return;
	}

task TXY
{
	while(!Obj_IsDeleted(objEnemy))
		{
		enmx = ObjMove_GetX(objEnemy);	
		enmy = ObjMove_GetY(objEnemy);	
		if(Obj_GetValueD(objEnemy,"SPECIAL_VISIBLE",false) == false)
			{
			Obj_SetValue(objEnemy,"ENEMY_X",enmx);
			Obj_SetValue(objEnemy,"ENEMY_Y",enmy);
			}
		let x = 0;
		let y = 0;
		let scene = GetEnemyBossSceneObjectID;
		if(scene != ID_INVALID)
			{
			x = Obj_GetValueD(scene,"ENMX",GetStgFrameWidth/2);
			y = Obj_GetValueD(scene,"ENMY",GetStgFrameHeight/2-110);
			}
		EnemyX = Obj_GetValueD(objEnemy,"ENEMY_X",x);
		EnemyY = Obj_GetValueD(objEnemy,"ENEMY_Y",y);
		yield;
		}
}
