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 spellA = 0;

	let back = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(back,GetCurrentScriptDirectory ~ "img/Back_Spell.png");
	ObjSprite2D_SetSourceRect(back,0,0,384,448);
	ObjSprite2D_SetDestRect(back,GetStgFrameWidth+8,GetStgFrameHeight+8,-8,-8);	
	Obj_SetRenderPriority(back,0.23);

	let obj = [];
	ascent(i in 0..2)
		{		
		obj = obj ~ [ObjPrim_Create(OBJ_SPRITE_2D)];
		ObjPrim_SetTexture(obj[i],GetCurrentScriptDirectory ~ "img/Back_Spell_Effect.png");
		ObjSprite2D_SetSourceRect(obj[i],0,0,512,512);
		ObjRender_SetScaleXYZ(obj[i],GetStgFrameWidth/512,GetStgFrameHeight/512,0);
		ObjRender_SetPosition(obj[i],GetStgFrameWidth/2,GetStgFrameHeight/2,0);
		ObjRender_SetScaleX(obj[i],i*2-1);
		ObjSprite2D_SetDestCenter(obj[i]);	
		Obj_SetRenderPriority(obj[i],0.25);
		}

	let enemy = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(enemy,GetCurrentScriptDirectory ~ "img/Face/Special.png");
	ObjSprite2D_SetSourceRect(enemy,0,0,240,435);
	ObjSprite2D_SetDestRect(enemy,GetScreenWidth*0.375-2,2,GetScreenWidth*(-0.025)+2,GetScreenHeight-2);	
	Obj_SetRenderPriority(enemy,0.24);	
	
	let alphaB = 180;
	let colorB;
	let xO = 0;
	let yO = 0;
	let speedO;
	let angleO;
	let alphaO;
	let colorE;
	loop
		{
		if(bSpell == false)	
			{
			spellA = 0;
			}
		else if(spellA < 1)
			{
			spellA += 1/20;
			}
		if(spell == false)
			{
			speedO = 0.5;
			angleO = 60;
			alphaO = 25;		
			colorB = 180;
			colorE = 0;
			}
		else
			{	
			speedO = 4;
			angleO = 235;
			alphaO = 75;	
			colorB = 0;
			colorE = 255;
			}
		alphaB+=2;
		ObjRender_SetColorHSV(back,colorB,255-255*cos(colorB),215+40*cos(alphaB));
		ObjRender_SetAlpha(back,255*spellA);
		xO-=speedO*cos(angleO);
		yO-=speedO*sin(angleO);
		ascent(i in 0..2)
			{
			ObjSprite2D_SetSourceRect(obj[i],xO,yO,512+xO,512+yO);
			ObjRender_SetColorHSV(obj[i],0,0,215+40*cos(alphaB));
			ObjRender_SetAlpha(obj[i],alphaO*spellA);
			}
		ObjRender_SetColorHSV(enemy,0,0,colorE);
		ObjRender_SetAlpha(enemy,175*spellA);

		yield;
		}
		
}