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

@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 vertexcount1 = 15;
	vertexcount1*=2;
	let x1 = GetStgFrameWidth*0.5;
	let y1 = GetStgFrameHeight+64;
	let innerrad = GetStgFrameHeight*0.4;
	let outerrad = max(((0-y1)^2+(0-x1)^2)^0.5,((GetStgFrameHeight-y1)^2+(GetStgFrameWidth-x1)^2)^0.5)+8;
	let mov = 0;

	let back1 = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetPrimitiveType(back1, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetTexture(back1, GetCurrentScriptDirectory ~ "img/Back_Fire_1.png");
	Obj_SetRenderPriority(back1, 0.2);
	ObjPrim_SetVertexCount(back1, vertexcount1);
	ObjRender_SetPosition(back1, x1,y1, 0);

	let mov2 = 0.5;
	let back2 = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetPrimitiveType(back2, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetTexture(back2, GetCurrentScriptDirectory ~ "img/Back_Fire_2.png");
	Obj_SetRenderPriority(back2, 0.21);
	ObjPrim_SetVertexCount(back2, vertexcount1);
	ObjRender_SetPosition(back2, x1,y1, 0);
	ObjRender_SetBlendType(back2,BLEND_ADD_ARGB);

	let rate = 0.5;
	ascent(i in 0..vertexcount1/2)
		{
		let indexvert = i*2;
		let angle = 150/(vertexcount1/2-1)*i+30/2+180;
		ObjPrim_SetVertexPosition(back1,indexvert+0,innerrad*cos(angle),innerrad*sin(angle),0);
		ObjPrim_SetVertexColor(back1,indexvert+0,175*rate,105*rate,105*rate);
		ObjPrim_SetVertexColor(back1,indexvert+1,255*0.925,255*0.925,255*0.925);
		ObjPrim_SetVertexPosition(back1,indexvert+1,outerrad*cos(angle),outerrad*sin(angle),0);

		ObjPrim_SetVertexPosition(back2,indexvert+0,innerrad*cos(angle),innerrad*sin(angle),0);
		ObjPrim_SetVertexColor(back2,indexvert+0,255,155,155);
		ObjPrim_SetVertexPosition(back2,indexvert+1,(outerrad-1)*cos(angle),(outerrad-1)*sin(angle),0);
		}

	let obj1 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj1,GetCurrentScriptDirectory ~ "img/Back_Planet.png");
	ObjSprite2D_SetSourceRect(obj1,128,208,390,448);
	ObjSprite2D_SetDestRect(obj1,0,0,1,1);
	ObjRender_SetPosition(obj1,x1,y1,0);
	ObjRender_SetScaleXYZ(obj1,innerrad*3,innerrad*3,0);
	Obj_SetRenderPriority(obj1, 0.21);
	ObjRender_SetAngleZ(obj1,30);
	ObjRender_SetColorHSV(obj1,0,0,205);

	let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj2,GetCurrentScriptDirectory ~ "img/Face/Face.png");
	ObjSprite2D_SetSourceRect(obj2,0,0,320,529);
	ObjRender_SetPosition(obj2,GetStgFrameWidth/2+15,GetStgFrameHeight/2+50,0);
	Obj_SetRenderPriority(obj2, 0.2);
	ObjRender_SetColorHSV(obj2,0,0,0);
	ObjRender_SetScaleX(obj2,-1);
	ObjSprite2D_SetDestCenter(obj2);
	let spellA3 = 180-40;
	loop
		{
		mov-=1.25;
		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(obj1,false);
				Obj_SetVisible(obj2,false);
				}
			}
		else if(spellA1 < 1)
			{
			Obj_SetVisible(back1,true);
			Obj_SetVisible(back2,true);
			Obj_SetVisible(obj1,true);
			Obj_SetVisible(obj2,true);
			spellA1 += 1/40;
			}
		else if(spellA1 >= 1 && spellA2 < 1)
			{
			spellA2 += 1/20;
			}
		spellA3+=1;
		ascent(i in 0..vertexcount1/2)
			{
			let indexvert = i*2;
			let angle = 150/(vertexcount1/2-1)*i+30/2+180;
			ObjPrim_SetVertexUVT(back1, indexvert, 512/(vertexcount1/2-1)*i, 0+mov);
			ObjPrim_SetVertexUVT(back1, indexvert+1, 512/(vertexcount1/2-1)*i, 256+mov);

			ObjPrim_SetVertexUVT(back2, indexvert, 512/(vertexcount1/2-1)*i, 0+mov*mov2);
			ObjPrim_SetVertexUVT(back2, indexvert+1, 512/(vertexcount1/2-1)*i, 256+mov*mov2);
			}
		ObjRender_SetAlpha(back1,spellA1*255);
		ObjRender_SetAlpha(back2,spellA1*255);
		ObjRender_SetAlpha(obj1,spellA1*255);
		ObjRender_SetAlpha(obj2,spellA1*(0.575+cos(spellA3)*0.425)*255);
		yield;
		}
		
}