sub BackGroundLoop
{
		if(StageState!=Menu && BGDrawing==1)
		{

			if(StageProgress==1)
			{
				SetTexture(csd~"img\st1bg1.png");		//画像の貼り付け
				SetGraphicRect(0+bgX, 0+bgY  ,2400+bgX, 3000 + bgY);	//描画矩形の設定
				SetGraphicAngle(-75, 0, 0);
				SetGraphicScale(1,1);
			//	SetGraphicScale(0.25,0.25);
			SetAlpha(stage1alpha1);
				SetViewTo(0,-60,50);
				SetViewFrom(500,90,45);
				SetColor(200,200,255);
				SetRenderState(ALPHA);
				DrawGraphic3D(0, 0, -300);				//実際に表示

				SetFog(200,500,200,200,250);
				SetTexture(csd~"img\st1bg1.png");		//画像の貼り付け
				SetGraphicRect(0-bgX2, 0+bgY2  ,2400-bgX2, 3000 + bgY2);	//描画矩形の設定				//実際に表示
				SetGraphicAngle(-75, 0, 0);
				SetGraphicScale(1,1);
			//	SetGraphicScale(0.25,0.25);
			SetAlpha(stage1alpha2);
				SetViewTo(0,-60,50);
				SetViewFrom(500,90,45);
				SetColor(200,200,255);
				SetRenderState(ALPHA);
				DrawGraphic3D(0,0, -300);


				SetViewFrom(600+ViewFromX,90,40+BossAdjust);
				SetFog(200,500,200,200,250);

			if(stage1alpha3>0)
			{
				SetTexture(csd~"img\cloud.png");		//画像の貼り付け
				SetGraphicRect(0+bgX3, 0+bgY3  ,2400+bgX3, 3000 + bgY3);	//描画矩形の設定
				SetGraphicAngle(-75, 0, 0);
				SetGraphicScale(1,1);
			//	SetGraphicScale(0.25,0.25);
				SetAlpha(stage1alpha3);
				SetRenderState(ALPHA);
				SetViewTo(0,-60,50);
				SetColor(255,255,255);
				DrawGraphic3D(0,0, 0);
			}

			}
/////////////////////////////////////////////////////////////////////////////////////////////////////
			if(StageProgress==2)
			{
				SetTexture(csd~"img\st2bg1.png");		//画像の貼り付け
				SetGraphicRect(0+bgX, 0+bgY  ,2400+bgX, 3000 + bgY);	//描画矩形の設定
				SetGraphicAngle(-75, 0, 0);
				SetGraphicScale(1,1);
			//	SetGraphicScale(0.25,0.25);
				SetAlpha(stage2alpha1);
				SetViewTo(0,-60,50);
				SetViewFrom(500,90,45);
				SetColor(200,200,255);
				SetRenderState(ALPHA);
				DrawGraphic3D(0, 0, -300);				//実際に表示

				SetViewFrom(600+ViewFromX,90,40+BossAdjust);
			//	SetFog(200,500,200,200,250);

			if(stage2alpha2>0)
			{
				SetTexture(csd~"img\st2bg2.png");		//画像の貼り付け
				SetGraphicRect(0+bgX2, 0+bgY2  ,2400+bgX2, 3000 + bgY2);	//描画矩形の設定
				SetGraphicAngle(-75, 0, 0);
				SetGraphicScale(1,1);
			//	SetGraphicScale(0.25,0.25);
				SetAlpha(stage2alpha2);
				SetRenderState(ALPHA);
				SetViewTo(0,-60,50);
				SetColor(200,255,255);
				DrawGraphic3D(0,0, -200);
			}

			}

			if(StageProgress==3)
			{
				SetTexture(csd~"img\st3bg1.png");		//画像の貼り付け
				SetGraphicRect(0+bgX, 0+bgY  ,2400+bgX, 3000 + bgY);	//描画矩形の設定
				SetGraphicAngle(-75, 0, 0);
				SetGraphicScale(1,1);
			//	SetGraphicScale(0.25,0.25);
				SetAlpha(stage3alpha1);
			//	SetAlpha(0);
				SetViewTo(0,-60,50);
				SetViewFrom(500,90,45);
				SetColor(200,200,255);
				SetRenderState(ALPHA);
				DrawGraphic3D(0, 0, -300);				//実際に表示
				SetViewFrom(600+ViewFromX,90,25+BossAdjust);

			//	SetFog(200,500,200,200,250);

				SetTexture(csd~"img\cloud2.png");		//画像の貼り付け
				SetGraphicRect(0+bgX2, 0+bgY2  ,2400+bgX2, 3000 + bgY2);	//描画矩形の設定
				SetGraphicAngle(90, 0, 45);
				SetGraphicScale(2,2);
				SetAlpha(stage3alpha2);
				SetColor(200,200,255);
				SetRenderState(ALPHA);
				DrawGraphic3D(0, 200, -300);				//実際に表示

				SetTexture(csd~"img\cloud2.png");		//画像の貼り付け
				SetGraphicRect(0+bgX2, 0+bgY2  ,2400+bgX2, 3000 + bgY2);	//描画矩形の設定
				SetGraphicAngle(90, 0, -45);
				SetGraphicScale(2,2);
				SetAlpha(stage3alpha2);
				SetColor(200,200,255);
				SetRenderState(ALPHA);
				DrawGraphic3D(0, 200, -300);				//実際に表示


			}

		}
}

task BGGraphicRect
{
	if(StageProgress==1)
	{
	stage1alpha3=0;
	descent(let i in 0..240)
	{
		if(stage1alpha1<=80)
		{
			stage1alpha1+=80/60;
		}
		if(stage1alpha2<=120)
		{
			stage1alpha2+=120/60;
		}
		if(stage1alpha3<=80)
		{
			stage1alpha3+=40/60;
		}
			if (bgX >= 640)
			{
				bgX = 0;
			}
			if (bgY >= 480)
			{
				bgY = 0;
			}

			if (bgX2 >= 640)
			{
				bgX2 = 0;
			}
			if (bgY2 >= 480)
			{
				bgY2 = 0;
			}
			if (bgY3 >= 512)
			{
				bgY3 = 0;
			}
		if(ViewFromX>0)
		{
			ViewFromX-=300/240;
		}
	bgX += 0.5;
	bgY += 1+18*i/240;
	bgX2 += 0.5;
	bgY2 += 1+18*i/240;
	bgY3 += 1+18*i/240;

	yield;
	}
	stage1scale3=1;	
	}
	while(StageProgress==1)
	{
			if (bgX >= 640)
			{
				bgX = 0;
			}
			if (bgY >= 480)
			{
				bgY = 0;
			}

			if (bgX2 >= 640)
			{
				bgX2 = 0;
			}
			if (bgY2 >= 480)
			{
				bgY2 = 0;
			}
			if (bgY3 >= 512)
			{
				bgY3 = 0;
			}
			if(GetCommonDataDefault("STAGE1MIDBOSS",false))
			{
				if(StageProgress==1)
				{
				bgX += 0.5;
				bgY += 1;
				bgX2 += 0.5;
				bgY2 += 1;
				bgY3 += 1;
				}
				if(BossAdjust>-15)
				{
					BossAdjust-=0.1;
				}
			}
			else if(GetCommonDataDefault("STAGE1BOSS",false))
			{
				bgX += 0.5;
				bgY += 1;
				bgX2 += 0.5;
				bgY2 += 1;
				bgY3 += 1;
				if(ViewFromX<100)
				{
				ViewFromX+=100/120;
				}
				if(BossAdjust>-15)
				{
					BossAdjust-=0.1;
				}
			}	
			else 
			{
				bgX += 0.5;
				bgY += 2;
				bgX2 += 0.5;
				bgY2 += 2;
				bgY3 += 2;
				if(BossAdjust<0)
				{
				BossAdjust+=0.1;
				}
			}
		if(StageChange){break;}
/*
	bgX+=BGWideRectWithPlayerMove(1);
	bgX2-=BGWideRectWithPlayerMove(1);
	bgX3+=BGWideRectWithPlayerMove(1);

	bgY+=BGLongRectWithPlayerMove(1);
	bgY2+=BGLongRectWithPlayerMove(1);
	bgY3+=BGLongRectWithPlayerMove(1);
*/
		yield;
	}

	while(StageChange)
	{
			if (bgX >= 640)
			{
				bgX = 0;
			}
			if (bgY >= 480)
			{
				bgY = 0;
			}

			if (bgX2 >= 640)
			{
				bgX2 = 0;
			}
			if (bgY2 >= 480)
			{
				bgY2 = 0;
			}
			if (bgY3 >= 512)
			{
				bgY3 = 0;
			}

				bgX += 0.5;
				bgY += 1;
				bgX2 += 0.5;
				bgY2 += 1;
				bgY3 += 1;
				if(ViewFromX<100)
				{
				ViewFromX+=100/120;
				}
				if(BossAdjust>-15)
				{
					BossAdjust-=0.1;
				}

		if(stage1alpha1>0)
		{
			stage1alpha1-=80/240;
		}
		if(stage1alpha2>0)
		{
			stage1alpha2-=120/240;
		}
		yield;
	}

	bgX=0;
	bgX2=300;
	bgY=0;
	bgY2=300;
	bgY3=300;
	BossAdjust=0;
	ViewFromX=0;

	let st2bg1angle=0;
	let MidBossEscape=false;

	if(StageProgress==2)
	{
	while(stage2alpha1<120)
	{
		stage2alpha1+=120/120;
		bgX += 1.0*cos(st2bg1angle);
		bgY += 1+1.0*cos(st2bg1angle);
		bgX2 += 1.0*cos(st2bg1angle);
		bgY2 += 1+1.0*cos(st2bg1angle);
		yield;
	}
	}
	while(StageProgress==2)
	{
			if (bgX >= 480 || bgX <= -480)
			{
				bgX = 0;
			}
			if (bgY >= 480 || bgY <= -480)
			{
				bgY = 0;
			}

			if (bgX2 >= 480 || bgX2 <= -480)
			{
				bgX2 = 0;
			}
			if (bgY2 >= 480 || bgY2 <= -480)
			{
				bgY2 = 0;
			}
			if(GetCommonDataDefault("STAGE2MIDBOSS",false))
			{
				bgX += 1.0*cos(st2bg1angle);
				bgY += 1+1.0*cos(st2bg1angle);
				bgX2 += 1.0*cos(st2bg1angle);
				bgY2 += 1+1.0*cos(st2bg1angle);

				if(st2bg1angle<90)
				{
					st2bg1angle+=0.5;
				}
				MidBossEscape=true;
			}
			else if(GetCommonDataDefault("STAGE2BOSS",false))
			{
				bgX += 1.0*cos(st2bg1angle);
				bgY += 1+1.0*cos(st2bg1angle);
				bgX2 += 1.0*cos(st2bg1angle);
				bgY2 += 1+1.0*cos(st2bg1angle);

				if(st2bg1angle>90)
				{
					st2bg1angle-=0.5;
				}

			}	
			else 
			{
				if(MidBossEscape)
				{
				bgX += 1.0*cos(st2bg1angle);
				bgY += 1-1.0*cos(st2bg1angle);
				bgX2 += 1.0*cos(st2bg1angle);
				bgY2 += 1-1.0*cos(st2bg1angle);
				}
				else
				{
				bgX += 1.0*cos(st2bg1angle);
				bgY += 1+1.0*cos(st2bg1angle);
				bgX2 += 1.0*cos(st2bg1angle);
				bgY2 += 1+1.0*cos(st2bg1angle);
				}
				if(st2bg1angle<180 && MidBossEscape)
				{
					st2bg1angle+=0.5;
				}
			}
		if(StageChange){break;}
		yield;
	}

	while(StageChange)
	{
			if (bgX >= 480 || bgX <= -480)
			{
				bgX = 0;
			}
			if (bgY >= 480 || bgY <= -480)
			{
				bgY = 0;
			}

			if (bgX2 >= 480 || bgX2 <= -480)
			{
				bgX2 = 0;
			}
			if (bgY2 >= 480 || bgY2 <= -480)
			{
				bgY2 = 0;
			}
				bgX += 1.0*cos(st2bg1angle);
				bgY += 1+1.0*cos(st2bg1angle);
				bgX2 += 1.0*cos(st2bg1angle);
				bgY2 += 1+1.0*cos(st2bg1angle);

		if(stage2alpha1>0)
		{
			stage2alpha1-=160/240;
		}
		if(stage2alpha2>0)
		{
			stage2alpha2-=120/240;
		}
		yield;
	}

	MidBossEscape=false;
	let st3bgXspeed=7;
	stage3alpha2=60;

	while(stage3alpha1<200)
	{
		stage3alpha1+=200/120;
	//	bgX += 1.0*cos(st2bg1angle);
		bgY += 7;
	//	bgX2 += 1.0*cos(st2bg1angle);
		bgY2 -= 7;
			if (bgX >= 480 || bgX <= -480)
			{
				bgX = 0;
			}
			if (bgY >= 480 || bgY <= -480)
			{
				bgY = 0;
			}

			if (bgX2 >= 400 || bgX2 <= -400)
			{
				bgX2 = 0;
			}
			if (bgY2 >= 400 || bgY2 <= -400)
			{
				bgY2 = 0;
			}
		yield;
	}
	while(StageProgress==3)
	{
			if (bgX >= 480 || bgX <= -480)
			{
				bgX = 0;
			}
			if (bgY >= 480 || bgY <= -480)
			{
				bgY = 0;
			}

			if (bgX2 >= 400 || bgX2 <= -400)
			{
				bgX2 = 0;
			}
			if (bgY2 >= 400 || bgY2 <= -400)
			{
				bgY2 = 0;
			}
			if(GetCommonDataDefault("STAGE3MIDBOSS",false))
			{
			//	bgX += 1.0*cos(st2bg1angle);
				bgY += st3bgXspeed;
			//	bgX2 += 1.0*cos(st2bg1angle);
				bgY2 -= st3bgXspeed;

				if(st2bg1angle<90)
				{
					st2bg1angle+=0.5;
				}
				if(st3bgXspeed>1)
				{
				st3bgXspeed-=0.05;
				}
				MidBossEscape=true;
			}
			else if(GetCommonDataDefault("STAGE3BOSS",false))
			{
			//	bgX += 1.0*cos(st2bg1angle);
				bgY += st3bgXspeed;
			//	bgX2 += 1.0*cos(st2bg1angle);
				bgY2 -= st3bgXspeed;

				if(st3bgXspeed>1)
				{
				st3bgXspeed-=0.05;
				}
				if(BossAdjust>-20)
				{
					BossAdjust-=0.1;
				}
			}	
			else 
			{
			//	bgX += 1.0*cos(st2bg1angle);
				bgY += st3bgXspeed;
			//	bgX2 += 1.0*cos(st2bg1angle);
				bgY2 -= st3bgXspeed;
				if(st2bg1angle<180 && MidBossEscape)
				{
					st2bg1angle+=0.5;
				}
				if(st3bgXspeed<7)
				{
				st3bgXspeed+=0.05;
				}
				if(MidBossEscape==true)
				{
					if(stage3alpha1>100)
					{
						stage3alpha1-=100/1500;
					}
				}
			}
		if(StageChange){break;}
		yield;
	}

	while(StageChange)
	{
			if (bgX >= 480 || bgX <= -480)
			{
				bgX = 0;
			}
			if (bgY >= 480 || bgY <= -480)
			{
				bgY = 0;
			}

			if (bgX2 >= 400 || bgX2 <= -400)
			{
				bgX2 = 0;
			}
			if (bgY2 >= 400 || bgY2 <= -400)
			{
				bgY2 = 0;
			}
			//	bgX += 1.0*cos(st2bg1angle);
				bgY += st3bgXspeed;
			//	bgX2 += 1.0*cos(st2bg1angle);
				bgY2 -= st3bgXspeed;

		if(stage3alpha1>0)
		{
			stage3alpha1-=100/240;
		}
		if(stage3alpha2>0)
		{
			stage3alpha2-=60/240;
		}
		yield;
	}

/*
	while(StageProgress==3)
	{
		if(stage3alpha<=150)
		{
			stage3alpha+=150/240;
		}
			bgX += 1;
			if (bgX >= 6000)
			{
				bgX = 0;
			}
			bgX2 += 1;
			if (bgX >= 6000)
			{
				bgX2 = 0;
			}
				bgrt += 1;
				if(bgrt > 512){bgrt = bgrt - 512;}
				if(bgrt <   0){bgrt = bgrt + 512;}
		yield;
	}
*/
	while(StageProgress==4)
	{
			bgX += 1;
			if (bgX >= 6000)
			{
				bgX = 0;
			}
			bgX2 += 1;
			if (bgX >= 6000)
			{
				bgX2 = 0;
			}
				bgrt += 1;
				if(bgrt > 512){bgrt = bgrt - 512;}
				if(bgrt <   0){bgrt = bgrt + 512;}
		yield;
	}
	while(StageProgress==5)
	{
		if(stage5alpha1<=100)
		{
			stage5alpha1+=100/240;
		}
		if(stage5alpha2<=150)
		{
			stage5alpha2+=150/240;
		}
			bgX += 1;
			if (bgX >= 6000)
			{
				bgX = 0;
			}
			bgX2 += 1;
			if (bgX >= 6000)
			{
				bgX2 = 0;
			}
				bgrt += 1;
				if(bgrt > 512){bgrt = bgrt - 512;}
				if(bgrt <   0){bgrt = bgrt + 512;}
		yield;
	}
	while(StageProgress>=6)
	{
		if(stage6alpha1<=120)
		{
			stage6alpha1+=120/240;
		}
		if(stage6alpha2<=80)
		{
			stage6alpha2+=80/240;
		}
			bgX += 1;
			if (bgX >= 6000)
			{
				bgX = 0;
			}
			bgX2 += 1;
			if (bgX >= 6000)
			{
				bgX2 = 0;
			}
				bgrt += 1;
				if(bgrt > 512){bgrt = bgrt - 512;}
				if(bgrt <   0){bgrt = bgrt + 512;}
		yield;
	}
}
	task BGStage1MidBossEnd
	{
	while(stage1alpha3>0)
	{
		stage1alpha3-=80/2000;
		yield;
	}
	}

	task Stage1End
	{

	}

	task BGStage2MidBossEnd
	{

	}

	task Stage2Bgshift
	{
		loop(240)
		{
		stage2alpha2+=120/240;
		yield;
		}
	}

	task Stage2End
	{

	}


	task Stage3End
	{

	}

	task BGStage3MidBossEnd
	{

	}

function BGLongRectWithPlayerMove(let BGLongMove)
{
	if((KeyStateUp=="PUSH" || KeyStateUp=="HOLD"))
	{
		if(KeyStateLeft=="PUSH" || KeyStateLeft=="HOLD")
		{
			return(-BGLongMove*2^0.5);
		}
		else if(KeyStateLeft=="PUSH" || KeyStateLeft=="HOLD")
		{
			return(-BGLongMove*2^0.5);
		}
		else
		{
			return(-BGLongMove);
		}
	}
	else if((KeyStateDown=="PUSH" || KeyStateDown=="HOLD"))
	{
		if(KeyStateLeft=="PUSH" || KeyStateLeft=="HOLD")
		{
			return(+BGLongMove*2^0.5);
		}
		else if(KeyStateLeft=="PUSH" || KeyStateLeft=="HOLD")
		{
			return(+BGLongMove*2^0.5);
		}
		else
		{
			return(+BGLongMove);
		}
	}
}

function BGWideRectWithPlayerMove(let BGWideMove)
{
	if((KeyStateLeft=="PUSH" || KeyStateLeft=="HOLD"))
	{
		if(KeyStateUp=="PUSH" || KeyStateUp=="HOLD")
		{
			return(-BGWideMove*2^0.5);
		}
		else if(KeyStateDown=="PUSH" || KeyStateDown=="HOLD")
		{
			return(-BGWideMove*2^0.5);
		}
		else
		{
			return(-BGWideMove);
		}
	}
	else if((KeyStateRight=="PUSH" || KeyStateRight=="HOLD"))
	{
		if(KeyStateUp=="PUSH" || KeyStateUp=="HOLD")
		{
			return(BGWideMove*2^0.5);
		}
		else if(KeyStateDown=="PUSH" || KeyStateDown=="HOLD")
		{
			return(BGWideMove*2^0.5);
		}
		else
		{
			return(BGWideMove);
		}
	}
}