#include "../../functions/functions_boss.txt"
#include "../../functions/functions_talk.txt"
#include "../Enemies/enemylist.txt"
#include "../include.txt"

let Practice = GetCommonData("PRACTICE",false);
if(GetCommonData("STAGE_TYPE","") == "TYPE_SPELL_PRACTICE")
	{
	Practice = true;	
	}

let pathSanae = 0;
let famAngle = [0,0];

let Spell = false;
task TBackground
	{
	let current = GetCurrentScriptDirectory ~ "img/";

	if(Effect > 3)
		{
		TBlank;
		}
	else
		{
		TSpellBack;
		}
	task TBlank
		{
		let spellA1 = 0;
		let back = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(back,current ~ "Back_Main.png");
		ObjSprite2D_SetSourceRect(back,1,1,385,446);
		Obj_SetRenderPriority(back,0.2);
		ObjSprite2D_SetDestRect(back,GetStgFrameWidth/2-320,GetStgFrameHeight/2-240,GetStgFrameWidth/2+320,GetStgFrameHeight/2+240);
		while(!Obj_IsDeleted(objEnemy))
			{
			if(spellA1 < 1)
				{
				spellA1 += 1/40;
				}
			ObjRender_SetAlpha(back,255*spellA1);
			yield;
			}
		Obj_Delete(back);
		}
	task TSpellBack
		{
		let spellA1 = 0;
		let spellA2 = 0;
		let mov = 0;
	
		let back0 = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(back0,current ~ "Back_Main.png");
		ObjSprite2D_SetSourceRect(back0,0,0,384,448);
		ObjSprite2D_SetDestRect(back0,GetStgFrameWidth+8,-8,-8,GetStgFrameHeight+8);
		Obj_SetRenderPriority(back0,0.21);
	
		let back1 = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(back1,current ~ "Back_Snow.png");
		ObjSprite2D_SetDestRect(back1,-8,-8,GetStgFrameWidth+8,GetStgFrameHeight+8);
		Obj_SetRenderPriority(back1,0.22);
		ObjRender_SetBlendType(back1,BLEND_ADD_ARGB);
		let x1 = 0;
		let y1 = 0;
		let angle1 = 0;
	
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(obj,current ~ "face/face_mad.png");
		ObjSprite2D_SetSourceRect(obj,0,0,256,512);
		ObjSprite2D_SetDestCenter(obj);
		Obj_SetRenderPriority(obj, 0.21);
		ObjRender_SetPosition(obj,GetStgFrameWidth*0.25,GetStgFrameHeight*0.65,0);
		ObjRender_SetColorHSV(obj,0,255,5);
		ObjRender_SetScaleXYZ(obj,1,1,0);
		let counter = -1;
		while(!Obj_IsDeleted(objEnemy))
			{
			mov-=1.75;
			counter+=1;
			if(spellA1 < 1)
				{
				spellA1 += 1/40;
				}	
			else if(spellA2 < 1)
				{
				spellA2 += 1/40;
				}
			ObjRender_SetAlpha(back0,spellA1*255);
	
			ObjRender_SetAlpha(back1,spellA1*75);
			x1-=(1.75+cos(angle1)*0.5);
			y1-=1.75*(0.9+0.35*cos(angle1*0.75));
			angle1+=0.33;
			ObjSprite2D_SetSourceRect(back1,-16-x1,y1,384-16-x1,448+y1);
	
			ObjRender_SetAlpha(obj,spellA1*155);
			yield;
			}
		Obj_Delete(back0);
		Obj_Delete(back1);
		Obj_Delete(obj);
		}
}

task CreateIceDelay(x,y,angle,scale,time)
{
	let obj = [];
	ascent(i in 0..2)
		{
		obj = obj ~ [ObjPrim_Create(OBJ_SPRITE_2D)];
		ObjPrim_SetTexture(obj[i],GetCurrentScriptDirectory ~ "./img/Effect_Cloud.png");
		Obj_SetRenderPriority(obj[i],0.6);
		ObjSprite2D_SetSourceRect(obj[i],0,0,256,256);
		ObjSprite2D_SetDestRect(obj[i],0,0,1,1);
		ObjRender_SetScaleXYZ(obj[i],scale,scale,1);	
		}
	ObjRender_SetBlendType(obj[0],BLEND_SUBTRACT);
	ObjRender_SetBlendType(obj[1],BLEND_ADD_ARGB);
	let angleA = rand(0,360);
	let angleB = rand(-1,1);
	let scaleA = 0;
	ascent(t in 0..time)
		{
		let num = sin(scaleA*180+180/time)*1.5*scaleA;
		let num1 = sin((t+1)*180/time)*1.5*t/time;
		ascent(i in 0..2)
			{
			ObjRender_SetPosition(obj[i],x,y,0);
			ObjRender_SetAlpha(obj[i],num*(128+i*64));
			ObjRender_SetAngleZ(obj[i],angleA);
			ObjRender_SetScaleXYZ(obj[i],num*scale,num*scale,0);
			}
		angleA+=angleB;
		x+=cos(angle)*0.5*num1;
		y+=sin(angle)*0.5*num1;
		if(!Obj_IsDeleted(objEnemy))
			{
			scaleA+=1/time;
			}
		else
			{
			break;
			}
		yield;
		}
	ascent(i in 0..2)
		{
		Obj_Delete(obj[i]);
		}	
}

task ObjEnemy_Render
{
	if(floor(GetCommonData("CURRENT_SPELL",0)/3) < 2)
		{
		ObjEnemy_RenderM;
		return;
		}
	CreateEnemyIcon(objEnemy);	
	let img = SpritePathBoss;
	ObjPrim_SetTexture(objEnemy,img);
	ObjSprite2D_SetDestRect(objEnemy,-24,-32,24,32);

	let anim = 0;
	let spell = 0;
	let move = 0;
	let spellD = 0;
	let render_x = 0;
	let render_y = 0;
	
	let count = 1/8;

	while(!Obj_IsDeleted(objEnemy))
		{
		if(spellD != EnemySpell)
			{
			spell = 0;
			}
		spellD = EnemySpell;
		if(ObjMove_GetSpeed(objEnemy) > 0.1 || move > 0)
			{
			anim = 0;
			spell = 0;
						
			if(ObjMove_GetSpeed(objEnemy) > 0.1)
				{
				if(move < 3)
					{
					move+=count;
					}
				}
			else
				{
				move-=count;
				}
			
			render_x = floor(move);

			render_y = 1;
			if(cos(ObjMove_GetAngle(objEnemy)) > cos(90+20))
				{
				ObjRender_SetScaleX(objEnemy,absolute(ObjRender_GetScaleX(objEnemy)));	
				}
			else if(cos(ObjMove_GetAngle(objEnemy)) < cos(90-20))
				{
				ObjRender_SetScaleX(objEnemy,-absolute(ObjRender_GetScaleX(objEnemy)));	
				}
			}
		else if(EnemySpell == 1 || spell > 0)
			{
			anim = 0;
			move = 0;
				
			if(EnemySpell == 1)
				{
				if(spell < 3)
					{
					spell+=count;
					}
				}
			else
				{
				spell-=count;
				}
			render_y = 2;
			}
		else
			{
			move = 0;
			spell = 0;	
			anim += count;
			render_x = floor(anim)%4;
			render_y = 0;
			}
		ObjSprite2D_SetSourceRect(objEnemy,render_x*48,render_y*64,(render_x+1)*48,(render_y+1)*64);
		yield;
		}
}

task ObjEnemy_RenderM
{
	CreateEnemyIcon(objEnemy);	
	let img = SpritePathBoss;
	ObjPrim_SetTexture(objEnemy,img);
	ObjSprite2D_SetDestRect(objEnemy,-24,-32,24,32);

	let anim = 0;
	let spell = 0;
	let move = 0;
	let spellD = 0;
	let render_x = 0;
	let render_y = 0;
	
	let count = 1/8;

	while(!Obj_IsDeleted(objEnemy))
		{
		if(spellD != EnemySpell)
			{
			spell = 0;
			}
		spellD = EnemySpell;
		if(ObjMove_GetSpeed(objEnemy) > 0.1 || move > 0)
			{
			anim = 0;
			spell = 0;
						
			if(ObjMove_GetSpeed(objEnemy) > 0.1)
				{
				if(move < 3)
					{
					move+=count;
					}
				}
			else
				{
				move-=count;
				}
			
			render_x = floor(move);

			render_y = 1;
			if(cos(ObjMove_GetAngle(objEnemy)) > cos(90+20))
				{
				ObjRender_SetScaleX(objEnemy,absolute(ObjRender_GetScaleX(objEnemy)));	
				}
			else if(cos(ObjMove_GetAngle(objEnemy)) < cos(90-20))
				{
				ObjRender_SetScaleX(objEnemy,-absolute(ObjRender_GetScaleX(objEnemy)));	
				}
			}
		else if(EnemySpell == 1 || spell > 0)
			{
			anim = 0;
			move = 0;
				
			if(EnemySpell == 1)
				{
				if(spell < 3)
					{
					spell+=count;
					}
				}
			else
				{
				spell-=count;
				}
			render_y = 2;
			}
		else
			{
			move = 0;
			spell = 0;	
			anim += count;
			render_x = floor(anim)%4;
			render_y = 0;
			}
		ObjSprite2D_SetSourceRect(objEnemy,render_x*48,render_y*64,(render_x+1)*48,(render_y+1)*64);
		yield;
		}
}

task CreatePatternA1(x,y,spread,speedM,angleM,graph,blend,del,scale)
{
	let g = floor(rand(0,length(graph)));
	let speedA = (speedM[1]-speedM[0])*0.5;

	ascent(i in 0..spread)	
		{
		let angleA = i*360/spread+180+angleM[0]+angleM[1];
		let xA = (speedM[0]+speedA)*cos(angleM[0])+speedA*cos(angleA);
		let yA = (speedM[0]+speedA)*sin(angleM[0])+speedA*sin(angleA);	
		
		let angle = atan2(yA,xA);
		let speed = GetDistance(0,0,xA,yA);

		let shot = CreateBulletA1(x,y,speed,angle,graph[(g+i)%length(graph)],del,scale);
		ObjRender_SetBlendType(shot,blend);
		}
}