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

#include "./functions_stage.txt"
#include "./functions_talk.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)
	{
	Obj_SetValue(objEnemy,"INVINC",false);
	Obj_SetValue(objPlayer,"enemyCountM",Obj_GetValueD(objPlayer,"enemyCountM",0)+1);
	Obj_SetValue(objPlayer,"enemyCount",Obj_GetValueD(objPlayer,"enemyCount",0)+1);
	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) || Obj_GetValueD(objEnemy,"INVINC",false) == 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)
			{
			Obj_SetValue(objPlayer,"enemyCount",Obj_GetValueD(objPlayer,"enemyCount",0)-1);
			break;
			}
		yield;
		}
	Obj_Delete(objEnemy);
	let array = GetAllEnemyID;
	ascent(i in 0..length(array))
		{
		Obj_Delete(array[i]);
		}
	EndChapter;
	loop(3)
		{
		yield;
		}
	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)
{
	PlaySound("se_Spell",120);
	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, 18);
	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]);
		}
}

task TWaveCircle()
{
	let renderTexture = GetReservedRenderTargetName(0);

	let frame = 0;
	let baseEffectRadius = 100;
	let outerFluct = 8;
	let effectRadius = 0;


	let priEffectMin = 20;
	let priEffectMax = 29;

	SetInvalidRenderPriorityA1(priEffectMin, priEffectMax);

	let frameWidth = GetStgFrameWidth();
	let frameHeight = GetStgFrameHeight();
	let frameLeft = GetStgFrameLeft();
	let frameRight = frameLeft + frameWidth;
	let frameTop = GetStgFrameTop();
	let frameBottom = frameTop + frameHeight;


	let objWave = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(objWave, 29);
	ObjPrim_SetTexture(objWave, renderTexture);
	ObjSprite2D_SetSourceRect(objWave, frameLeft, frameTop, frameRight, frameBottom);
	ObjSprite2D_SetDestRect(objWave, 0, 0, frameWidth, frameHeight);
	Obj_SetRenderPriorityI(objWave, priEffectMax + 1);

	let pathShader = GetCurrentScriptDirectory ~ "SamplePS03_HLSL.txt";
	ObjShader_SetShaderF(objWave, pathShader);
	ObjShader_SetTechnique(objWave, "TecWave");

	let objEnemy = GetEnemyBossObjectID[0];
	while(!Obj_IsDeleted(objEnemy))
	{
		effectRadius = baseEffectRadius + outerFluct * sin(frame*4);

		let enemyX = ObjMove_GetX(objEnemy);
		let enemyY = ObjMove_GetY(objEnemy);

		RenderToTextureA1(renderTexture, priEffectMin, priEffectMax, true);

		ObjShader_SetFloat(objWave, "frame_", frame);
		ObjShader_SetFloat(objWave, "enemyX_", enemyX + frameLeft);
		ObjShader_SetFloat(objWave, "enemyY_", enemyY + frameTop);
		ObjShader_SetFloat(objWave, "waveRadius_", effectRadius);
		ObjShader_SetFloat(objWave, "colorR_", Obj_GetValueD(GetPlayerObjectID,"BOSS_AURA_R",1));
		ObjShader_SetFloat(objWave, "colorG_", Obj_GetValueD(GetPlayerObjectID,"BOSS_AURA_G",1));
		ObjShader_SetFloat(objWave, "colorB_", Obj_GetValueD(GetPlayerObjectID,"BOSS_AURA_B",1));

		frame++;
		yield;
	}

	Obj_Delete(objWave);
	ClearInvalidRenderPriority();
}


task BossEnter(x,y,color)
{
	let radius = 384;
	let objar = [];
	let num = 6;
	let darkwait = true;
	DarkenScreen();
	ascent(i in 0..num)
	{

		let ang = 360/num*i;
		objar = append(objar,CreateHexagonSprite(x+radius*cos(ang),y+radius*sin(ang),6,color));
	}
	let a = 0;
	while(radius>0)
	{
		ascent(i in 0..length(objar))
		{
			let ang = 360/num*i+a;
			ObjRender_SetPosition(objar[i],x+radius*cos(ang),y+radius*sin(ang),0);
		}
		if(a%10==0)
		{
			let xy = [x+rand(0,96)*cos(rand(0,9999)),y+rand(0,96)*cos(rand(0,9999))];
			CreateSpark(xy[0],xy[1],color);
		}
		if(radius == 8)
		{
			times(16)
			{
				CreateSpark(x,y,color);
			}
		}
		radius-=3;
		a++;
		yield;
	}

	ascent(i in 0..length(objar))
	{
		Obj_Delete(objar[i]);
	}

	times(16)
	{
		CreateSpark(x+rand(0,96)*cos(rand(0,9999)),y+rand(0,96)*cos(rand(0,9999)),color);
		yield;
	}
	
	loop(30){yield;}
	darkwait = false;
	
	task DarkenScreen()
		{
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjSprite2D_SetSourceRect(obj,0,0,384,448);
		ObjSprite2D_SetDestRect(obj,0,0,GetStgFrameWidth,448);
		let alpha = 0;
		//ObjRender_SetBlendType(obj,BLEND_SUBTRACT);
		let scale = 0.15;
		ObjRender_SetColor(obj,color[0]*scale,color[1]*scale,color[2]*scale);
		while(alpha<200&&darkwait)
			{
			ObjRender_SetAlpha(obj,alpha);
			alpha+=4;
			yield;
			}
	
		while(darkwait){yield;}
	
		while(alpha>0)
			{
			ObjRender_SetAlpha(obj,alpha);
			alpha-=4;
			yield;
			}
		Obj_Delete(obj);
		}	
}

task CreateSpark(x,y,color)
{
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	let gx = 2;
	let alpha = 255;
	let sw = 0;

	ObjPrim_SetTexture(obj,GetCurrentScriptDirectory~"./../img/WhiteCircleAlpha.png");
	ObjSprite2D_SetSourceRect(obj,0,256+64,128,256+128+64);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetPosition(obj,x,y,0);	
	ObjRender_SetBlendType(obj,3);
	ObjRender_SetAngleZ(obj,trunc(rand(-999,999)));
	ObjRender_SetColor(obj,color[0],color[1],color[2]);
	let scale = 0;
	while(alpha>0)
	{
		ObjRender_SetScaleXYZ(obj,scale,scale,1);
		ObjRender_SetAlpha(obj,alpha);
		ObjRender_SetAngleZ(obj,ObjRender_GetAngleZ(obj)+16);
		scale+=0.03;
		alpha-=4;
		yield;
	}
	Obj_Delete(obj);

}
function CreateHexagonSprite(x,y,dir,color)
{
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	let gx = 2;
	let alpha = 0;
	let sw = 0;

	ObjPrim_SetTexture(obj,GetCurrentScriptDirectory~"./../img/WhiteCircleAlpha.png");
	ObjSprite2D_SetSourceRect(obj,64*gx,256,64*(gx+1),256+64);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetPosition(obj,x,y,0);	
	//ObjRender_SetDestAtSpeed(obj,speed,dx,dy);
	ObjRender_SetBlendType(obj,3);
	ObjRender_SetColor(obj,color[0],color[1],color[2]);
	Rotate();
task Rotate()
{
	let timer = 0;
	while(!Obj_IsDeleted(obj))
	{
		if(timer%1==0)
		{
			CreateHexagonSpriteChild(ObjRender_GetX(obj),ObjRender_GetY(obj),dir,color)
		}
		ObjRender_SetAngleZ(obj,ObjRender_GetAngleZ(obj)+dir);
		timer++;
		yield;
	}
	
}	
task CreateHexagonSpriteChild(x,y,dir,color)
{
	let objc = ObjPrim_Create(OBJ_SPRITE_2D);
	let gx = 2;
	let alpha = 128;
	let sw = 0;

	ObjPrim_SetTexture(objc,GetCurrentScriptDirectory~"./../img/WhiteCircleAlpha.png");
	ObjSprite2D_SetSourceRect(objc,64*gx,256,64*(gx+1),256+64);
	ObjSprite2D_SetDestCenter(objc);
	ObjRender_SetPosition(objc,x,y,0);	
	//ObjRender_SetDestAtSpeed(objc,speed,dx,dy);
	ObjRender_SetBlendType(objc,3);
	ObjRender_SetColor(objc,color[0],color[1],color[2]);
	ObjRender_SetAngleZ(objc,ObjRender_GetAngleZ(obj));	
	while(!Obj_IsDeleted(objc)&&alpha>0)
	{
		ObjRender_SetAlpha(objc,alpha);
		alpha-=6;
		yield;
	}
	Obj_Delete(objc);
}
	return obj
}
