
let bSpell = false;
let current = GetCurrentScriptDirectory();
@Initialize
{
	SetCameraFocusX(20);
	SetCameraFocusY(0);
	SetCameraFocusZ(0);
	SetCameraRadius(550);
	SetCameraElevationAngle(11);
	SetCameraAzimuthAngle(90);

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

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

@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/Default_Background_IceMountain.mqo";
	let path2 = current ~ "img/moonlight.png";
	let path3 = current ~ "img/stars.png";
	let path4 = current ~ "img/shootingstars.png";
	
	let obj = ObjMesh_Create();
	ObjMesh_Load(obj, path);
	Obj_SetRenderPriorityI(obj, 20);
	ObjRender_SetAngleY(obj, 90);
	let frameInvisible = 0;
	
	let obj2 = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(obj2, path2);
	ObjSprite3D_SetSourceDestRect(obj2, 0, 0, 32, 32);
	Obj_SetRenderPriority(obj2, 0.21);
	ObjRender_SetZ(obj2, -550);
	ObjRender_SetY(obj2, 150);
	ObjRender_SetX(obj2, 120);
	ObjRender_SetAngleZ(obj2, 180);
	ObjRender_SetScaleXYZ(obj2, 3, 3, 0);
	ObjSprite3D_SetBillboard(obj2, true);
	
	let obj3 = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(obj3, path3);
	ObjSprite3D_SetSourceDestRect(obj3, 0, 0, 563, 352);
	Obj_SetRenderPriority(obj3, 0.20);
	ObjRender_SetZ(obj3, -1000);
	ObjRender_SetY(obj3, 150);
	ObjRender_SetAngleZ(obj3, 180);
	ObjRender_SetScaleXYZ(obj3, 4, 4, 0);
	ObjSprite3D_SetBillboard(obj3, true);
	
	let obj4 = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(obj4, path4);
	ObjSprite3D_SetSourceDestRect(obj4, 1, 1, 255, 255);
	Obj_SetRenderPriority(obj4, 0.20);
	ObjRender_SetZ(obj4, -999);
	ObjRender_SetY(obj4, 170);
	ObjRender_SetX(obj4, 160);
	ObjRender_SetScaleXYZ(obj4, 2, 2, 0);
	ObjSprite3D_SetBillboard(obj4, true);
	
	let obj5 = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(obj5, path4);
	ObjSprite3D_SetSourceDestRect(obj5, 1, 1, 255, 255);
	Obj_SetRenderPriority(obj5, 0.20);
	ObjRender_SetZ(obj5, -999);
	ObjRender_SetY(obj5, 170);
	ObjRender_SetX(obj5, 160);
	ObjRender_SetScaleXYZ(obj5, 4, 4, 0);
	ObjSprite3D_SetBillboard(obj5, true);
	
	let camAngleH = 90;
	let camAngleV = 11;
	let angle = 0;
	loop
	{
		if(bSpell)
		{
			frameInvisible++;
			if(frameInvisible >= 60)
			{
				Obj_SetVisible(obj, false);
				Obj_SetVisible(obj2, false);
				Obj_SetVisible(obj3, false);
				Obj_SetVisible(obj4, false);
				Obj_SetVisible(obj5, false);
			}
		}
		else
		{
			
			frameInvisible = 0;
			Obj_SetVisible(obj, true);
			Obj_SetVisible(obj2, true);
			Obj_SetVisible(obj3, true);
			Obj_SetVisible(obj4, false);
			Obj_SetVisible(obj5, false);
			if(GetCommonData("ChoosingDifficulty", 0) == 0){
				if(!(GetVirtualKeyState(VK_LEFT)==KEY_HOLD && GetVirtualKeyState(VK_RIGHT)==KEY_HOLD)){
					if(GetVirtualKeyState(VK_LEFT)==KEY_HOLD){
						if(GetVirtualKeyState(VK_SLOWMOVE)==KEY_HOLD){camAngleH-= 0.025;}
						else{camAngleH-= 0.05;}
						if(camAngleH <86.2){camAngleH=86.2;}
					}
					else if(GetVirtualKeyState(VK_RIGHT)==KEY_HOLD){
						if(GetVirtualKeyState(VK_SLOWMOVE)==KEY_HOLD){camAngleH+= 0.025;}
						else{camAngleH+= 0.05;}
						if(camAngleH >93.8){camAngleH=93.8;}
					}
				}
				if(!(GetVirtualKeyState(VK_UP)==KEY_HOLD && GetVirtualKeyState(VK_DOWN)==KEY_HOLD)){
					if(GetVirtualKeyState(VK_DOWN)==KEY_HOLD){
						if(GetVirtualKeyState(VK_SLOWMOVE)==KEY_HOLD){camAngleV-= 0.025;}
						else{camAngleV-= 0.05;}
						if(camAngleV <11){camAngleV=11;}
					}
					else if(GetVirtualKeyState(VK_UP)==KEY_HOLD){
						if(GetVirtualKeyState(VK_SLOWMOVE)==KEY_HOLD){camAngleV+= 0.025;}
						else{camAngleV+= 0.05;}
						if(camAngleV >16){camAngleV=16;}
					}
				}
			}
		}

		ObjRender_SetZ(obj, pz);
		pz++;
		if(pz > 1000){pz -= 3000;}
		SetCameraElevationAngle(camAngleV);
		SetCameraAzimuthAngle(camAngleH);
		ObjRender_SetAngleZ(obj4, angle);
		ObjRender_SetAngleZ(obj5, angle/2);
		angle -= 0.5;
		yield;
	}
}

task TSpellBackground
{
	let path1 = current ~ "img/bg2.png";
	let obj1 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj1, BLEND_ALPHA);
	Obj_SetRenderPriority(obj1, 0.21);
	ObjPrim_SetTexture(obj1, path1);
	ObjSprite2D_SetDestRect(obj1, 0, 0, 512, 512);
	//ObjSprite2D_SetDestCenter(obj1);
	//ObjRender_SetPosition(obj1, GetStgFrameWidth()/2, GetStgFrameHeight()/2, 0);
	ObjRender_SetScaleXYZ(obj1, 1.3, 1.3, 0);

	let path2 = current ~ "img/bg3.png";
	let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj2, BLEND_ALPHA);
	Obj_SetRenderPriority(obj2, 0.20);
	ObjPrim_SetTexture(obj2, path2);
	ObjSprite2D_SetDestRect(obj2, 0, 0, 384, 448);
	//ObjSprite2D_SetDestCenter(obj2);
	ObjRender_SetScaleXYZ(obj2, 1.7, 1.3, 0);
	ObjRender_SetPosition(obj2, 0, 0, 0);
	
	let path3 = current ~ "img/bg1.png";
	let obj3 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj3, BLEND_ALPHA);
	Obj_SetRenderPriority(obj3, 0.21);
	ObjPrim_SetTexture(obj3, path3);
	ObjSprite2D_SetDestRect(obj3, 0, 0, 384, 448);
	//ObjSprite2D_SetDestCenter(obj2);
	ObjRender_SetScaleXYZ(obj3, 1.7, 1.3, 0);
	ObjRender_SetPosition(obj3, 0, 0, 0);
	let alpha = 0;
	let frame = 0;
	let frame2 = 512;
	loop
	{
		if(bSpell)
		{
			alpha += 4;
		}
		else
		{
			alpha = 0;
		}

		
		
		ObjRender_SetAlpha(obj1, alpha);
		Obj_SetVisible(obj1, alpha > 0);
		ObjSprite2D_SetSourceRect(obj1, 0 - (frame/2) % 512, 0 - (frame/2) % 512, 512 - (frame/2) % 512, 512 - (frame/2) % 512);
		
		ObjRender_SetAlpha(obj2, alpha);
		Obj_SetVisible(obj2, alpha > 0);
		ObjSprite2D_SetSourceRect(obj2, 0, 0, 384, 448);
		
		ObjRender_SetAlpha(obj3, alpha);
		Obj_SetVisible(obj3, alpha > 0);
		ObjSprite2D_SetSourceRect(obj3, 0, 0, 384, 448);
		
		frame++;
		frame2--;
		yield;
	}

}
