
let bSpell = true;
let current = GetCurrentScriptDirectory();
let evang = 80;
let evdown = 1.5;
let sbg1 = current~"img/stg3bg5.png";
let sbg2 = current~"img/stg3bg1.png";
@Initialize
{
	SetCameraFocusX(0);
	SetCameraFocusY(200);
	SetCameraFocusZ(250);
	SetCameraRadius(500);
	SetCameraAzimuthAngle(90);
	SetCameraElevationAngle(20);
	//SetCameraPerspectiveClip(0, 2000);
	SetFogParam(500, 1000, 40, 0, 40);

	ascent(let pos in 0..3)
	{
		TNormalBackground(pos);
	}
	TSkyBackground;
	TSpellBackground();
	TSpellBackground2();
}

@MainLoop
{

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

	yield;
}


task TNormalBackground(pos)
{
	let pz = (pos-1) * 1000;

	let path = current ~ "img/City.mqo";
	let obj = ObjMesh_Create();
	ObjMesh_Load(obj, path);
	Obj_SetRenderPriorityI(obj, 21);

	let frameInvisible = 0;
	loop
	{
		if(bSpell)
		{
			frameInvisible++;
			if(frameInvisible >= 60)
			{
				Obj_SetVisible(obj, false);
			}
		}
		else
		{
			frameInvisible = 0;
			Obj_SetVisible(obj, true);
		}
		ObjRender_SetZ(obj, pz);
		pz += 3.33;
		if(pz > 1000){pz -= 3000;}

		yield;
	}
}

task TSkyBackground
{

	let path = current ~ "img/Sky.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, path);
	ObjSprite2D_SetSourceRect(obj, 0, 0, 800, 600);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetPosition(obj, GetStgFrameWidth/2, GetStgFrameHeight/2, 0);
	Obj_SetRenderPriorityI(obj, 20);
	ObjRender_SetScaleXYZ(obj, 0.75, 0.75, 0);

	let frameInvisible = 0;
	loop
	{
		if(bSpell)
		{
			frameInvisible++;
			if(frameInvisible >= 60)
			{
				Obj_SetVisible(obj, false);
			}
		}
		else
		{
			frameInvisible = 0;
			Obj_SetVisible(obj, true);
		}
		yield;
	}
}

task TSpellBackground(){
	let c = 0;
	let s = 0;
	let value = 0;
	let count = 0;
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,21);
	ObjPrim_SetTexture(obj,sbg2);
	ObjSprite2D_SetSourceRect(obj,0,0,1024,1024);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,0.5,0.5,0.5);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetAngleXYZ(obj,0,0,0);
	ObjRender_SetColor(obj,255,255,255);
	ObjRender_SetPosition(obj,384/2,448/2,0);
	Obj_SetVisible(obj, false);
	let frameInvisible = 0;
	loop
	{
		if(bSpell==false)
		{
			if(frameInvisible==0){GSetColor(-1);}
			frameInvisible++;
		}
		else if(bSpell==true)
		{
			if(frameInvisible>0){GSetColor(1);}
			frameInvisible = 0;
		}
		yield;
	}
	task GSetColor(type)
	{
		if(type==1)
		{
			Obj_SetVisible(obj, true);
			ascent(i in 0..60)
			{
				ObjRender_SetAlpha(obj,i*4);
				yield;
			}
		}
		if(type==-1)
		{
			descent(i in 0..60)
			{
				ObjRender_SetAlpha(obj,i*4);
				yield;
			}
			Obj_SetVisible(obj, false);
		}
	}
}	

task TSpellBackground2(){
	let c = 0;
	let s = 0;
	let value = 0;
	let count = 0;
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,21);
	ObjPrim_SetTexture(obj,sbg1);
	ObjSprite2D_SetSourceRect(obj,0,0,512,512);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,1,1,1);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	ObjRender_SetAngleXYZ(obj,0,0,180);
	ObjRender_SetColor(obj,255,255,255);
	ObjRender_SetPosition(obj,384/2,448/2,0);
	Obj_SetVisible(obj, false);
	let frameInvisible = 0;
	loop
	{
		if(bSpell==false)
		{
			if(frameInvisible==0){GSetColor(-1);}
			frameInvisible++;
		}
		else if(bSpell==true)
		{
			if(frameInvisible>0){GSetColor(1);}
			frameInvisible = 0;
			
		}
			ObjSprite2D_SetSourceRect(obj,0,count*3,512,512+count*3);
			count++;
		yield;
	}
	task GSetColor(type)
	{
		if(type==1)
		{
			Obj_SetVisible(obj, true);
			ascent(i in 0..60)
			{
				ObjRender_SetColor(obj,i*0,i*3,i*4);
				yield;
			}
		}
		if(type==-1)
		{
			descent(i in 0..60)
			{
				ObjRender_SetColor(obj,i*0,i*3,i*4);
				yield;
			}
			Obj_SetVisible(obj, false);
		}
	}
}
