let bSpell = false;
let current = GetCurrentScriptDirectory();
let spell = GetCommonData("REI_SPELL",false);
let Effect = GetCommonData("EFFECT",0);

@Initialize
{
	SetAutoDeleteObject(true);
	SetCameraFocusX(0);
	SetCameraFocusY(30);
	SetCameraFocusZ(-100);
	SetCameraRadius(300);
	SetCameraElevationAngle(5);
	SetCameraAzimuthAngle(90);

	SetFogParam(0, 1000, 32, 32, 32);

	TBackground;
}

@MainLoop
{

	let objScene = GetEnemyBossSceneObjectID();
	if(objScene != ID_INVALID && ObjEnemyBossScene_GetInfo(objScene, INFO_IS_SPELL))
		{
		bSpell = true;
		}
	else
		{
		bSpell = false;
		}
	spell = GetCommonData("REI_SPELL",false);

	yield;
}

task TBackground
{

	let spellA1 = 0;
	let spellA2 = 0;

	let back1 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(back1,GetCurrentScriptDirectory ~ "img/Back_Plaid.png");
	ObjSprite2D_SetDestRect(back1,-8,-8,GetStgFrameWidth+8,GetStgFrameHeight+8);
	Obj_SetRenderPriority(back1,0.21);
	ObjRender_SetColorHSV(back1,0,125,255);
	ObjRender_SetBlendType(back1,BLEND_ADD_ARGB);


	let vertexcount1 = 180;
	if(Effect == 4)
		{
		vertexcount1 = 45;
		}
	else if(Effect >= 1)
		{
		vertexcount1 = 90;
		}
	vertexcount1*=2;
	let x2 = GetStgFrameWidth*0.5-1;
	let y2 = GetStgFrameHeight*0.0575;
	let innerrad = 42;
	let outerrad = max(((0-y2)^2+(0-x2)^2)^0.5,((GetStgFrameHeight-y2)^2+(GetStgFrameWidth-x2)^2)^0.5)+8;
	let mov = 0;

	let back2 = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetPrimitiveType(back2, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetTexture(back2, GetCurrentScriptDirectory ~ "img/Back_Plaid2.png");
	Obj_SetRenderPriority(back2, 0.22);
	ObjPrim_SetVertexCount(back2, vertexcount1);
	ObjRender_SetPosition(back2, x2,y2,0);
	ascent(i in 0..vertexcount1/2)
		{
		let indexvert = i*2;
		let angle = i*360/(vertexcount1/2-1)-120;
		ObjPrim_SetVertexPosition(back2,indexvert+0,innerrad*cos(angle),innerrad*sin(angle),0);
		ObjPrim_SetVertexColor(back2,indexvert+1,255,64,64);
		ObjPrim_SetVertexPosition(back2,indexvert+1,outerrad*cos(angle),outerrad*sin(angle),0);
		ObjPrim_SetVertexColor(back2,indexvert+1,255,205,64);
		}

	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj,GetCurrentScriptDirectory ~ "img/face/face.png");
	ObjSprite2D_SetSourceRect(obj,0,0,256,336);
	ObjSprite2D_SetDestCenter(obj);
	Obj_SetRenderPriority(obj, 0.2);
	ObjRender_SetPosition(obj,GetStgFrameWidth*0.25,GetStgFrameHeight*0.65,0);
	ObjRender_SetColorHSV(obj,0,255,5);
	ObjRender_SetScaleXYZ(obj,1.5,1.5,0);
	let counter = -1;
	loop
		{
		mov-=1.75;
		counter+=1;
		if(bSpell == false)	
			{
			if(spellA1 > 0)	
				{
				spellA1 -= 1/10;
				spellA2 -= 1/10;
				}
			else
				{
				spellA1 = 0;
				spellA2 = 0;
				Obj_SetVisible(back1,false);
				Obj_SetVisible(back2,false);
				Obj_SetVisible(obj,false);
				}
			}
		else if(spellA1 < 1)
			{
			Obj_SetVisible(back1,true);
			Obj_SetVisible(back2,true);
			Obj_SetVisible(obj,true);
			spellA1 += 1/40;
			}	
		else if(spellA2 < 1)
			{
			spellA2 += 1/40;
			}
		ObjRender_SetAlpha(back1,spellA1*125);
		ObjSprite2D_SetSourceRect(back1,-16-mov*0.3,-mov*0.5,384*0.5-16-mov*0.3,448*0.5-mov*0.5);

		ascent(i in 0..vertexcount1/2)
			{
			let indexvert = i*2;
			ObjPrim_SetVertexUVT(back2, indexvert, 512/(vertexcount1/2-1)*i, 0+mov);
			ObjPrim_SetVertexAlpha(back2,indexvert+0,spellA1*255);
			ObjPrim_SetVertexUVT(back2, indexvert+1, 512/(vertexcount1/2-1)*i, 256+mov);
			ObjPrim_SetVertexAlpha(back2,indexvert+1,spellA1*60);
			}
		ObjRender_SetAlpha(obj,spellA1*175);
		yield;
		}
		
}