let bSpell = false;
let current = GetCurrentScriptDirectory();
let angle = 0;
@Initialize
{

	/*SetCameraFocusX(0);
	SetCameraFocusY(0);
	SetCameraFocusZ(0);
	SetCameraRadius(550);
	SetCameraElevationAngle(35);          Default Values
	SetCameraAzimuthAngle(90);

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


	SetCameraFocusX(-200);
	SetCameraFocusY(0);
	SetCameraFocusZ(0);
	SetCameraRadius(550);
	SetCameraElevationAngle(25);
	SetCameraAzimuthAngle(0);

	SetFogParam(0, 2000, 0, 0, 0);

	ascent(let pos in 0..6)
	{
		TNormalBackground(pos);
	}
	TSpellBackground();
}

@MainLoop
{

	let objScene = GetEnemyBossSceneObjectID();
	if(objScene != ID_INVALID && ObjEnemyBossScene_GetInfo(objScene, INFO_IS_SPELL))
	{
		bSpell = true;
	}
	else
	{
		bSpell = false;
	}
	
	SetCameraAzimuthAngle(10+10*cos(angle/5));
	angle++;
	yield;
}


task TNormalBackground(pos)
{
	let pz = (pos-1) * 250;
	let path = current ~ "img/bg/03_00.mqo";
	let obj = ObjMesh_Create();
	ObjMesh_Load(obj, path);
	Obj_SetRenderPriorityI(obj, 20);

	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+=GetCommonData("BackgroundSpeed", 2);
		if(pz > 250*3){pz -= 250*6;}
		yield;
	}
}

//cdbg02b

task TSpellBackground
{
	let obj1=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(obj1,0.24);
	ObjPrim_SetTexture(obj1,GetCurrentScriptDirectory ~ "/img/Ingame/cdbg02b.png");
	ObjSprite2D_SetSourceRect(obj1,0,0,256,256);
	ObjSprite2D_SetDestCenter(obj1);
	ObjRender_SetScaleXYZ(obj1, 3, 3, 3);
	ObjRender_SetBlendType(obj1,BLEND_ALPHA);
	ObjRender_SetPosition(obj1,GetStgFrameWidth/2, GetStgFrameHeight/2,0);
	
	let obj2=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(obj2,0.24);
	ObjPrim_SetTexture(obj2,GetCurrentScriptDirectory ~ "/img/Ingame/cdbg02a.png");
	ObjSprite2D_SetSourceRect(obj2,0,0,256,256);
	ObjSprite2D_SetDestCenter(obj2);
	ObjRender_SetScaleXYZ(obj2, 3, 3, 3);
	ObjRender_SetBlendType(obj2,BLEND_ADD_ARGB);
	ObjRender_SetPosition(obj2,GetStgFrameWidth/2, GetStgFrameHeight/2,0);

	let alpha = 0;
	let frame = 0;
	loop
	{
		if(bSpell){
		if(alpha < 255){alpha += 5;}
		}
		else{
		if(alpha > 0){alpha -= 5;}
		}

		ObjRender_SetAlpha(obj1, alpha);
		Obj_SetVisible(obj1, alpha > 0);
		ObjRender_SetAngleZ(obj1, -frame/3);
		
		ObjRender_SetAlpha(obj2, alpha);
		Obj_SetVisible(obj2, alpha > 0);
		ObjRender_SetAngleZ(obj2, frame/2);
		frame++;
		yield;
	}

}
