#include "../functions.txt"

@Initialize
	{
	SetAutoDeleteObject(true);
	LoadTexture(GetCurrentScriptDirectory ~ "./img/Effect_Cloud.png");
	LoadTexture(GetCurrentScriptDirectory ~ "./img/Aura1.png");
	LoadTexture(GetCurrentScriptDirectory ~ "./img/Aura2.png");
	LoadTexture(GetCurrentScriptDirectory ~ "./img/Aura3.png");
	}
@MainLoop{yield;}
@Event
{
	alternative(GetEventType())

	case(EV_EFFECT_EXPLOSION)
		{
		Event_Explosion(GetEventArgument(0)[0],GetEventArgument(0)[1]);
		}
	case(EV_EFFECT_CHARGE)
		{
		Event_Charge(GetEventArgument(0)[0],GetEventArgument(0)[1]);
		}
	case(EV_EFFECT_SHAKE)
		{
		Event_Shake(GetEventArgument(0)[0]);
		}	
	case(EV_EFFECT_ICE_DELAY)
		{
		Event_Ice_Delay(GetEventArgument(0)[0],GetEventArgument(0)[1],GetEventArgument(0)[2],GetEventArgument(0)[3],GetEventArgument(0)[4],GetEventArgument(0)[5]);
		}	
	case(EV_EFFECT_FINAL_BOSS_AURA)
		{
		Event_FinalAura;
		if(GetEventArgument(0)[0])
			{
			Event_FinalAuraB;
			}
		}
	case(EV_ICE_BREAK)
		{
		Event_IceBreak(GetEventArgument(0)[0],GetEventArgument(0)[1]);
		}
}	
task Event_Explosion(x,y)
	{
	let obj = [];
	let count = 0;
	let angleM = rand(0,360);
	let angle = [];
	let graph = 1;
	ascent(i0 in 0..2)
		{
		ascent(i1 in 0..3)
			{
			angle = angle ~ [i1*120+i0*60+angleM+rand(-1,1)*0.25];
			obj = obj ~ [ObjPrim_Create(OBJ_SPRITE_2D)];
			ObjPrim_SetTexture(obj[count],GetCurrentScriptDirectory ~ "../img/RSS_Effect.png");
			ObjSprite2D_SetSourceRect(obj[count],(graph%4)*64,(floor(graph/4)%2)*64,(1+graph%4)*64,(1+floor(graph/4)%2)*64);
			ObjRender_SetAngleXYZ(obj[count],rand(-1,1)*45,rand(-1,1)*45,rand(0,360));
			ObjRender_SetScaleXYZ(obj[count],0.66,0.66,0);
			ObjRender_SetPosition(obj[count],x,y,0);
			Obj_SetRenderPriority(obj[count],0.38);
			ObjRender_SetAlpha(obj[count],45);
			ObjRender_SetBlendType(obj[count],BLEND_ADD_ARGB);		
			ObjRender_SetColorHSV(obj[count],180,105,255);
			count+=1;
			}
		}
	let speed = 10;
	let switch = false;
	loop(20)
		{
		speed-=10/20;
		count = 0;
		ascent(i0 in 0..2)
			{
			ascent(i1 in 0..3)
				{
				ObjRender_SetPosition(obj[count],ObjRender_GetX(obj[count])+speed*(0.5+i0*0.5)*cos(angle[count]),ObjRender_GetY(obj[count])+speed*(0.5+i0*0.5)*sin(angle[count]),0);
				ObjSprite2D_SetDestCenter(obj[count]);
				count+=1;
				}
			}
		yield;
		}
	count = 0;
	ascent(i0 in 0..2)	
		{
		ascent(i1 in 0..3)
			{
			angle[count] = 90+rand(-1,1)*30;
			count+=1;
			}
		}
	let alpha = 45;
	ascent(t in 1..13)
		{
		alpha-=45/12;
		count = 0;
		ascent(i0 in 0..2)
			{
			ascent(i1 in 0..3)
				{
				ObjRender_SetAlpha(obj[count],alpha);
				ObjSprite2D_SetDestCenter(obj[count]);
				count+=1;
				}
			}
		yield;
		}
	count = 0;
	ascent(i0 in 0..2)
		{
		ascent(i1 in 0..3)
			{
			Obj_Delete(obj[count]);
			count+=1;
			}
		}
	}

task Event_Charge(x,y)
	{
	if(Effect < 3)
		{
		let obj = [];
		let count = 0;;
		let angleM = rand(0,360);
		let angle = [];
		let graph = 1;
		ascent(i0 in 0..2)
			{
			ascent(i1 in 0..3)
				{
				angle = angle ~ [i1*120+i0*60+angleM+rand(-1,1)*0.25];
				obj = obj ~ [ObjPrim_Create(OBJ_SPRITE_2D)];
				ObjPrim_SetTexture(obj[count],GetCurrentScriptDirectory ~ "../img/RSS_Effect.png");
				ObjSprite2D_SetSourceRect(obj[count],(graph%4)*64,(floor(graph/4)%2)*64,(1+graph%4)*64,(1+floor(graph/4)%2)*64);
				ObjRender_SetAngleXYZ(obj[count],rand(-1,1)*45,rand(-1,1)*45,rand(0,360));
				ObjRender_SetScaleXYZ(obj[count],0.66,0.66,0);
				ObjRender_SetPosition(obj[count],x,y,0);
				Obj_SetRenderPriority(obj[count],0.38);
				ObjRender_SetAlpha(obj[count],85);
				ObjRender_SetBlendType(obj[count],BLEND_ADD_ARGB);		
				ObjRender_SetColorHSV(obj[count],180,105,255);
				count+=1;
				}
			}
		let speed = 10;
		ascent(t in 0..20)
			{
			speed-=10/20;
			count = 0;
			ascent(i0 in 0..2)
				{
				ascent(i1 in 0..3)
					{
					ObjRender_SetPosition(obj[count],ObjRender_GetX(obj[count])+speed*(0.5+i0*0.5)*cos(angle[count]),ObjRender_GetY(obj[count])+speed*(0.5+i0*0.5)*sin(angle[count]),0);
					ObjSprite2D_SetDestCenter(obj[count]);
					count+=1;
					}
				}
			}
		loop(20)
			{
			speed-=10/20;
			count = 0;
			ascent(i0 in 0..2)
				{
				ascent(i1 in 0..3)
					{
					ObjRender_SetPosition(obj[count],ObjRender_GetX(obj[count])+speed*(0.5+i0*0.5)*cos(angle[count]),ObjRender_GetY(obj[count])+speed*(0.5+i0*0.5)*sin(angle[count]),0);
					ObjSprite2D_SetDestCenter(obj[count]);
					count+=1;
					}
				}
			yield;
			}
		count = 0;
		ascent(i0 in 0..2)	
			{
			ascent(i1 in 0..3)
				{
				angle[count] = 90+rand(-1,1)*30;
				count+=1;
				}
			}
		let alpha = 65;
		ascent(t in 1..11)
			{
			alpha-=65/10;
			count = 0;
			ascent(i0 in 0..2)
				{
				ascent(i1 in 0..3)
					{
					ObjRender_SetAlpha(obj[count],alpha);
					ObjSprite2D_SetDestCenter(obj[count]);
					count+=1;
					}
				}
			yield;
			}
		count = 0;
		ascent(i0 in 0..2)
			{
			ascent(i1 in 0..3)
				{
				Obj_Delete(obj[count]);	
				count+=1;
				}
			}
		}
	}
task Event_Shake(rad)
{
	let xM = Get2DCameraX;
	let yM = Get2DCameraY;
	ascent(t in 0..30)
		{
		Set2DCameraFocusX(xM+rand(-1,1)*rad*t/30);
		Set2DCameraFocusY(yM+rand(-1,1)*rad*t/30);
		yield;
		}
	Set2DCameraFocusX(xM);
	Set2DCameraFocusY(yM);
}

task Event_Ice_Delay(x,y,angle,scale,time,boss)
	{
	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(boss))
			{
			scaleA+=1/time;
			}
		else if(t > time*0.5 && scaleA < 1)
			{
			scaleA+=2/time;
			}
		else if(scaleA > 0)
			{
			scaleA-=2/time;
			}
		else
			{
			break;
			}
		yield;
		}
	ascent(i in 0..2)
		{
		Obj_Delete(obj[i]);
		}
	}
task Event_IceBreak(x,y)
	{
	loop(2)
		{
		let shot = CreateBulletA1(x,y,rand(1,3.25),rand(0,360),DOT,0,1,SHOT_YELLOW);
		ObjShot_SetSpellResist(shot,true);
		}
	}
task Event_FinalAura
	{
	let boss = GetEnemyBossObjectID;
	let objEnemy;
	let enmx = -64;
	let enmy = -64;

	let obj = [ObjPrim_Create(OBJ_SPRITE_2D)];
	ObjPrim_SetTexture(obj[0],GetCurrentScriptDirectory ~ "img/Aura1.png");
	ObjSprite2D_SetSourceRect(obj[0],0,0,256,256);
	ObjSprite2D_SetDestRect(obj[0],0,0,1,1);
	ObjRender_SetScaleXYZ(obj[0],300,300,0);

	obj = obj ~ [ObjPrim_Create(OBJ_SPRITE_2D)];
	ObjPrim_SetTexture(obj[1],GetCurrentScriptDirectory ~ "img/Aura2.png");
	ObjSprite2D_SetSourceRect(obj[1],0,0,128,128);
	ObjSprite2D_SetDestRect(obj[1],0,0,1,1);
	ObjRender_SetScaleXYZ(obj[1],158,158,0);	
	ObjRender_SetAlpha(obj[1],175);

	obj = obj ~ [ObjPrim_Create(OBJ_SPRITE_2D)];
	ObjPrim_SetTexture(obj[2],GetCurrentScriptDirectory ~ "img/Aura3.png");
	ObjSprite2D_SetSourceRect(obj[2],0,0,80,80);
	ObjSprite2D_SetDestRect(obj[2],-50,-50,50,50);

	let spread = length(obj);

	ascent(i in 0..spread)
		{
		Obj_SetRenderPriorityI(obj[i],29+i*2);
		}
	Obj_SetRenderPriorityI(obj[2],30);

	let counter = -1;
	while(Obj_GetValueD(GetEnemyBossSceneObjectID,"AURA_DELETE",false) == false)
		{
		boss = GetEnemyBossObjectID;
		if(length(boss) > 0)
			{
			objEnemy = boss[0];
			enmx = ObjMove_GetX(objEnemy);
			enmy = ObjMove_GetY(objEnemy);
			}
		counter+=1;
		ascent(i in 0..spread)
			{	
			let spin = min(1,i);
			ObjRender_SetPosition(obj[i],enmx,enmy,0);
			ObjRender_SetAngleY(obj[i],((spin%2)*2-1)*(1-min(counter/52,1))*90);
			}
		ascent(i in 0..2)
			{
			ObjRender_SetAngleZ(obj[i],(i*2-1)*((0.3+i*0.6)*counter/60)*90);
			}
		ObjRender_SetAngleZ(obj[2],-90+(min(1,0.9*counter/52))*90);
		if(Effect < 1)
			{		
			ascent(i in 0..2)
				{
				if((i == 0 && counter%90 == 0) || (i == 1 && counter%5 == 0))
					{
					TGlow(i);
					}	
				}
			}
		yield;
		}
	Obj_SetValue(GetEnemyBossSceneObjectID,"AURA_DELETE",false);
	ascent(i in 0..spread)
		{
		Obj_Delete(obj[i]);
		ObjRender_SetAngleY(obj[i],((i%2)*2-1)*(1-min(counter/60,1))*90);
		}	

	task TGlow(i)
		{
		let glw = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(glw,GetCurrentScriptDirectory ~ "img/Aura" ~ IntToString(i+1) ~ ".png");
		ObjSprite2D_SetSourceRect(glw,0,0,256-i*128,256-i*128);
		ObjSprite2D_SetDestRect(glw,0,0,1,1);
		ObjRender_SetPosition(glw,enmx,enmy,0);
		Obj_SetRenderPriority(glw,0.28+i*0.01);
		ObjRender_SetBlendType(glw,BLEND_ADD_ARGB);
		ObjRender_SetAngleZ(glw,ObjRender_GetAngleZ(obj[i]));
		ObjRender_SetAngleY(glw,ObjRender_GetAngleY(obj[i]));
		let time = 60-i*30;
		let rate = 0.45-i*0.15;
		let xS = ObjRender_GetScaleX(obj[i]);
		let yS = ObjRender_GetScaleY(obj[i]);
		ascent(t in 0..time)
			{
			ObjRender_SetColorHSV(glw,210+i*30+t*30/time,255,255);
			ObjRender_SetAlpha(glw,(time-t-1)*255/time);
			ObjRender_SetScaleXYZ(glw,(1+t*rate/time)*xS,(1+t*rate/time)*yS,0);
			if(i == 0)
				{
				ObjRender_SetAngleZ(glw,ObjRender_GetAngleZ(obj[i]));
				}
			if(Obj_IsDeleted(obj[i]))
				{
				Obj_Delete(glw);
				}
			yield;
			}
		Obj_Delete(glw);
		}
	}
task Event_FinalAuraB
	{
	
	}