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);*/

	SetCameraRadius(1000);
	SetCameraElevationAngle(20);
	SetCameraAzimuthAngle(90);

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

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

@MainLoop
{

	let objScene = GetEnemyBossSceneObjectID();
	if(objScene != ID_INVALID && ObjEnemyBossScene_GetInfo(objScene, INFO_IS_SPELL))
	{
		bSpell = true;
	}
	else
	{
		bSpell = false;
	}
	
	let color = GetCommonData("StgFrameColor", [255, 255, 255]);
	SetCameraAzimuthAngle(10*sin(angle/4)+90);
	SetCameraFocusY(100+200*sin(angle/7));
	SetFogParam(0, 2000, color[0]/3, color[1]/3, color[2]/3);
	angle++;
	yield;
}

task TNormalBackgroundBackground{
	let black = ObjPrim_Create(OBJ_SPRITE_3D); 
	ObjSprite3D_SetBillboard(black,true);
	Obj_SetRenderPriorityI(black,19);
	ObjRender_SetColor(black,122,12,12);
	ObjSprite3D_SetSourceDestRect(black,0,0,1500,1500);
	loop{
	let color = GetCommonData("StgFrameColor", [255, 255, 255]);
	ObjRender_SetColor(black, color[0]/3, color[1]/3, color[2]/3);
	yield;
	}
	
}


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

	let frameInvisible = 0;
	loop{
		let color = GetCommonData("StgFrameColor", [255, 255, 255]);
		ObjRender_SetColor(obj, color[0]-122, color[1]-122, color[2]-122);
		ObjRender_SetZ(obj, pz);
		pz+=GetCommonData("BackgroundSpeed", 2);
		if(pz > 250*6){pz -= 250*12;}
		yield;
	}
}

//cdbg02b

task TSpellBackground
{
	let black = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(black,0.23);
	ObjPrim_SetTexture(black,GetCurrentScriptDirectory ~ "/img/effects/dummy.png");
	ObjSprite2D_SetSourceRect(black,0,0,1024,1024);
	ObjSprite2D_SetDestCenter(black);
	ObjRender_SetScaleXYZ(black, 1, 1, 1);
	ObjRender_SetBlendType(black,BLEND_ALPHA);
	ObjRender_SetColor(black, 0, 0, 0);
	ObjRender_SetPosition(black,GetStgFrameWidth/2, GetStgFrameHeight/2,0);
	
	//Garaiya
	
	let obj1=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(obj1,0.24);
	ObjPrim_SetTexture(obj1,GetCurrentScriptDirectory ~ "/img/Ingame/Garaiya_01.png");
	ObjSprite2D_SetSourceRect(obj1,0,0,1024,1024);
	ObjSprite2D_SetDestCenter(obj1);
	ObjRender_SetScaleXYZ(obj1, 1, 1, 1);
	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/Garaiya_01.png");
	ObjSprite2D_SetSourceRect(obj2,0,0,1024,1024);
	ObjSprite2D_SetDestCenter(obj2);
	ObjRender_SetScaleXYZ(obj2, 1, 1, 1);
	ObjRender_SetBlendType(obj2,BLEND_ALPHA);
	ObjRender_SetPosition(obj2,GetStgFrameWidth/2, GetStgFrameHeight/2,0);
	
	let obj3=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(obj3,0.24);
	ObjPrim_SetTexture(obj3,GetCurrentScriptDirectory ~ "/img/Ingame/Garaiya_00.png");
	ObjSprite2D_SetSourceRect(obj3,0,0,512,512);
	ObjSprite2D_SetDestCenter(obj3);
	ObjRender_SetScaleXYZ(obj3, 1, 1, 1);
	ObjRender_SetBlendType(obj3,BLEND_ALPHA);
	ObjRender_SetPosition(obj3,GetStgFrameWidth/2, GetStgFrameHeight/2,0);
	
	//Kujiru
	
	let obj4=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(obj4,0.24);
	ObjPrim_SetTexture(obj4,GetCurrentScriptDirectory ~ "/img/Ingame/Kujiru_01.png");
	ObjSprite2D_SetSourceRect(obj4,0,0,1022,1022);
	ObjSprite2D_SetDestCenter(obj4);
	ObjRender_SetScaleXYZ(obj4, 1, 1, 1);
	ObjRender_SetBlendType(obj4,BLEND_ALPHA);
	ObjRender_SetPosition(obj4,GetStgFrameWidth/2, GetStgFrameHeight/2,0);
	
	let obj5=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(obj5,0.24);
	ObjPrim_SetTexture(obj5,GetCurrentScriptDirectory ~ "/img/Ingame/Kujiru_00.png");
	ObjSprite2D_SetSourceRect(obj5,0,0,512,512);
	ObjSprite2D_SetDestCenter(obj5);
	ObjRender_SetScaleXYZ(obj5, 0.9, 0.9, 1);
	ObjRender_SetBlendType(obj5,BLEND_ALPHA);
	ObjRender_SetPosition(obj5,GetStgFrameWidth/2, GetStgFrameHeight/2,0);
	
	//Kaisen
	
	let obj6=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(obj6,0.24);
	ObjPrim_SetTexture(obj6,GetCurrentScriptDirectory ~ "/img/Ingame/Kaisen_01.png");
	ObjSprite2D_SetSourceRect(obj6,0,0,1022,1022);
	ObjSprite2D_SetDestCenter(obj6);
	ObjRender_SetScaleXYZ(obj6, 1, 1, 1);
	ObjRender_SetBlendType(obj6,BLEND_ALPHA);
	ObjRender_SetPosition(obj6,GetStgFrameWidth/2, GetStgFrameHeight/2,0);
	
	let obj7=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(obj7,0.24);
	ObjPrim_SetTexture(obj7,GetCurrentScriptDirectory ~ "/img/Ingame/Kaisen_00.png");
	ObjSprite2D_SetSourceRect(obj7,0,0,512,512);
	ObjSprite2D_SetDestCenter(obj7);
	ObjRender_SetScaleXYZ(obj7, 0.9, 0.9, 1);
	ObjRender_SetBlendType(obj7,BLEND_ALPHA);
	ObjRender_SetPosition(obj7,GetStgFrameWidth/2, GetStgFrameHeight/2,0);
	
	

	let alpha = 0;
	let alpha2 = 0;
	let alpha3 = 0;
	let alpha4 = 0;
	let alphablack = 0;
	let frame = 0;
	loop
	{
		if(bSpell){
		if(alphablack < 255){alphablack += 5;}
			if(GetCommonData("SP", "") == "Garaiya"){
			if(alpha < 255){alpha += 5;}
			}
			if(GetCommonData("SP", "") == "Kujiru"){
			if(alpha2 < 255){alpha2 += 5;}
			}
			if(GetCommonData("SP", "") == "Kaisen"){
			if(alpha3 < 255){alpha3 += 5;}
			}
			if(GetCommonData("SP", "") == "All"){
			if(alpha4 < 255){alpha4 += 5;}
			}
		}
		else{
		if(alphablack > 0){alphablack -= 5;}
			if(GetCommonData("SP", "") == "Garaiya"){
			if(alpha > 0){alpha -= 5;}
			}
			if(GetCommonData("SP", "") == "Kujiru"){
			if(alpha2 > 0){alpha2 -= 5;}
			}
			if(GetCommonData("SP", "") == "Kaisen"){
			if(alpha3 > 0){alpha3 -= 5;}
			}
			if(GetCommonData("SP", "") == "All"){
			if(alpha4 > 0){alpha4 -= 5;}
			}
		}
		
		Obj_SetVisible(black, alphablack+alpha4/2 > 0);
		ObjRender_SetAlpha(black, alphablack/2+alpha4);

		//Garaiya
		ObjRender_SetAlpha(obj1, alpha+alpha4/4);
		Obj_SetVisible(obj1, alpha+alpha4/2 > 0);
		ObjRender_SetScaleXYZ(obj1, 1+0.25*sin(frame), 1+0.25*sin(frame), 1);
		ObjRender_SetAngleZ(obj1, frame/4);
		
		ObjRender_SetAlpha(obj2, alpha+alpha4/4);
		Obj_SetVisible(obj2, alpha+alpha4/2 > 0);
		ObjRender_SetScaleXYZ(obj2, 1+0.25*sin(frame+180), 1+0.25*sin(frame+180), 1);
		ObjRender_SetAngleZ(obj2, -frame/4);
		
		Obj_SetVisible(obj3, alpha > 0);
		ObjRender_SetAlpha(obj3, alpha);
		
		
		//Kujiru
		Obj_SetVisible(obj4, alpha2+alpha4/4 > 0);
		ObjRender_SetAlpha(obj4, alpha2+alpha4/4);
		ObjSprite2D_SetSourceRect(obj4, 0+frame/2, 0-frame, 1022+frame/2, 1022-frame);
		
		Obj_SetVisible(obj5, alpha2 > 0);
		ObjRender_SetAlpha(obj5, alpha2);
		
		
		//Kaisen
		Obj_SetVisible(obj6, alpha3+alpha4/4 > 0);
		ObjRender_SetAlpha(obj6, alpha3/2+alpha4/4);
		ObjSprite2D_SetSourceRect(obj6, 0-frame, 0-frame/4, 1022-frame, 1022-frame/4);
		
		Obj_SetVisible(obj7, alpha3 > 0);
		ObjRender_SetAlpha(obj7, alpha3/2);
		
		frame++;
		yield;
	}

}
