let BGAllow = true;

task StageBG(tobj){
	camera;
	

	
	let BManage = ObjPrim_Create(OBJ_SPRITE_3D);
	
	Circle(BManage);
	BG2(tobj);
	
	SetFogEnable(true);
	SetFogParam(100, 1000, 105, 100, 120);
	let graphic = GetCurrentScriptDirectory ~ "/st02b.png";
	let graphic1 = GetCurrentScriptDirectory ~ "/st02a.png";
	
	let obj1 = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(obj1, graphic1);
	Obj_SetRenderPriorityI(obj1,22);
	//ObjSprite2D_SetSourceRect(obj1,0,0,485, 570);
	ObjSprite3D_SetSourceDestRect(obj1,0,0,512*2, 512*2);
	//ObjSprite2D_SetDestCenter(obj1);
	ObjRender_SetPosition(obj1,GetStgFrameLeft+30,GetStgFrameHeight/2+200, -140);
	ObjRender_SetScaleXYZ(obj1, 0.6, -0.6, NULL);
	
	
	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(obj, graphic);
	Obj_SetRenderPriorityI(obj,22);
	//ObjSprite2D_SetSourceRect(obj,0,0,485, 570);
	ObjSprite3D_SetSourceDestRect(obj,0,0,512*2, 512*2);
	//ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetPosition(obj,GetStgFrameLeft+30,GetStgFrameHeight/2+200, -140);
	ObjRender_SetScaleXYZ(obj, 0.6, -0.6, NULL);
	
	ObjRender_SetAlpha(obj, 200);
	
	
	
	//ObjRender_SetColor(obj, 200, 200, 200);
	//ObjRender_SetColor(obj1, 200, 200, 200);
	
	

	let frame = 0;
	
	let M = 0;
	let M1 = 0;
	let M2 = 0;
	let F = 0;
	while(!Obj_IsDeleted(BManage)){
		frame++;
		
		ObjSprite3D_SetSourceDestRect(obj,0,0+M,512*3, 512*3+M);
		ObjSprite3D_SetSourceDestRect(obj1,0,0+M*0.7,512*3, 512*3+M*0.7);
		
		if(frame==3){
			Snow(BManage, rand(15, GetStgFrameWidth-15), rand(15, GetStgFrameHeight-15), rand(0.5, 1.5));
			
			frame=0;
			}
		
		M-=3.5;
		if(Obj_IsDeleted(tobj)){
			Obj_Delete(BManage);
			}
		
		if(BGAllow==false){
			ObjRender_SetAlpha(obj, 0);
			//ObjRender_SetAlpha(obj1, 0);
			//ObjRender_SetAlpha(obj2, 0);
			}
			
		else{
			ObjRender_SetAlpha(obj, 255);
			//ObjRender_SetAlpha(obj1, 255);
			//ObjRender_SetAlpha(obj2, 255);
			}
		yield;
		}
		
	Obj_Delete(obj);
	}
	
task camera{

	SetCameraFocusX(0);
	SetCameraFocusY(40);
	SetCameraFocusZ(100);
	SetCameraRadius(480);
	SetCameraAzimuthAngle(90);
	
	
	let a = 0;
	let b = 0;

	loop{

		SetCameraFocusX(sin(a)*15+60);
		SetCameraElevationAngle(300+(sin(b)*15));
		// SetCameraRoll(a/5);
		//a++;
		//b+=0.5;
		yield;

	}
	}
	
task Circle(tobj){
	let graphic = GetCurrentScriptDirectory ~ "/circle.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, graphic);
	Obj_SetRenderPriorityI(obj,24);
	//ObjSprite2D_SetSourceRect(obj,0,0,485, 570);
	ObjSprite2D_SetSourceRect(obj,0,0,512, 512);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetPosition(obj,GetStgFrameWidth/2,GetStgFrameHeight/5, 0);
	ObjRender_SetScaleXYZ(obj, 0.8, 0.8, NULL);
	ObjRender_SetAlpha(obj, 100);
	let angle = 0;
	while(!Obj_IsDeleted(tobj)){
		ObjRender_SetAngleZ(obj, angle);
		angle+=0.1;
		
		yield;
		}
		
	Obj_Delete(obj);
	}
	
task BG2(tobj){
	let graphic = GetCurrentScriptDirectory ~ "/bg_2.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, graphic);
	Obj_SetRenderPriorityI(obj,21);
	ObjSprite2D_SetSourceRect(obj,0,0,485, 570);
	//ObjSprite2D_SetSourceRect(obj,0,0,512, 512);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetPosition(obj,GetStgFrameWidth/2,GetStgFrameHeight/3, 0);
	//ObjRender_SetScaleXYZ(obj, 0.8, 0.8, NULL);
	//ObjRender_SetAlpha(obj, 100);
	let angle = 0;
	while(!Obj_IsDeleted(tobj)){
		//ObjRender_SetAngleZ(obj, angle);
		angle+=0.1;
		
		yield;
		}
		
	Obj_Delete(obj);
	}
	
task Snow(tobj, x, y, scale){
	let graphic = GetCurrentScriptDirectory ~ "/eff_maple.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, graphic);
	Obj_SetRenderPriorityI(obj,25);
	//ObjSprite2D_SetSourceRect(obj,0,0,485, 570);
	ObjSprite2D_SetSourceRect(obj,64/2,64/2,64, 64);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetPosition(obj,x,y, 0);
	ObjRender_SetScaleXYZ(obj, scale, scale , NULL);
	ObjRender_SetAlpha(obj, 200);
	let angle = rand(0, 360);
	let alpha = 150;
	let anglemod = rand(-1, 1);
	let C = rand(255, 200);
	ObjRender_SetColor(obj, C, C, C);
	let XMod = rand(0.1, 0.3)*3;
	let YMod = rand(0.1, 0.3)*3;
	let angleX = 0;
	let angleY=0;
	while(!Obj_IsDeleted(tobj)){
		ObjRender_SetAlpha(obj, alpha);
		ObjRender_SetAngleZ(obj, angle);
		ObjRender_SetAngleX(obj, angleX);
		ObjRender_SetAngleY(obj, angleY);
		ObjRender_SetPosition(obj,x,y, 0);
		if(alpha>=0){
			alpha-=150/40;
			angle+=anglemod;
			angleX+=XMod;
			angleY+=YMod;
			x+=XMod;
			y+=YMod;
			}
			
		else{
			Obj_Delete(obj);
			}
		
		yield;
		}
	}