//一時停止中スクリプト
	let csd		= GetCurrentScriptDirectory();
	let menu_on	= true;

	#include"./Load_AllTexture.dnh"

@Initialize{
	SetAutoDeleteObject(true);
	TBackground();
	TMenu();
}

@MainLoop{
	yield;
}

@Finalize{
}

task PlaySEsP(se){
	let sid		= ObjSound_Create;
	let SE		= GetAreaCommonData("Option","SE_Volume",100);
	
	ObjSound_Load(sid,se);
	ObjSound_SetVolumeRate(sid,SE);
	ObjSound_SetLoopEnable(sid,false);
	ObjSound_Play(sid);
}

task TBackground
{
	task TVertex(var index, var left, var top, var right, var bottom)
	{
		ObjPrim_SetVertexPosition(obj, index + 0, left, top, 0);
		ObjPrim_SetVertexPosition(obj, index + 1, left, bottom, 0);
		ObjPrim_SetVertexPosition(obj, index + 2, right, top, 0);
		ObjPrim_SetVertexPosition(obj, index + 3, right, top, 0);
		ObjPrim_SetVertexPosition(obj, index + 4, left, bottom, 0);
		ObjPrim_SetVertexPosition(obj, index + 5, right, bottom, 0);

		ObjPrim_SetVertexUVT(obj, index + 0, left, top);
		ObjPrim_SetVertexUVT(obj, index + 1, left, bottom);
		ObjPrim_SetVertexUVT(obj, index + 2, right, top);
		ObjPrim_SetVertexUVT(obj, index + 3, right, top);
		ObjPrim_SetVertexUVT(obj, index + 4, left, bottom);
		ObjPrim_SetVertexUVT(obj, index + 5, right, bottom);

		//STGシーン内のみアニメーション
		if(left >= 32 && right <= 416 && top >=16 && bottom <= 464)
		{
			let alpha = 255;
			while(alpha >= 128)
			{
				ObjPrim_SetVertexAlpha(obj, index + 0, alpha/4);
				ObjPrim_SetVertexAlpha(obj, index + 1, alpha/4);
				ObjPrim_SetVertexAlpha(obj, index + 2, alpha/4);
				ObjPrim_SetVertexAlpha(obj, index + 3, alpha/4);
				ObjPrim_SetVertexAlpha(obj, index + 4, alpha/4);
				ObjPrim_SetVertexAlpha(obj, index + 5, alpha/4);
				
				ObjPrim_SetVertexColor(obj, index + 0, 255,128,128);
				ObjPrim_SetVertexColor(obj, index + 1, 255,128,128);
				ObjPrim_SetVertexColor(obj, index + 2, 255,128,128);
				ObjPrim_SetVertexColor(obj, index + 3, 255,128,128);
				ObjPrim_SetVertexColor(obj, index + 4, 255,128,128);
				ObjPrim_SetVertexColor(obj, index + 5, 255,128,128);
				alpha -= 255 / frame;

				yield;
			}
		}
	}

	//分割設定
	let frame = 30;
	let countH = 20; //分割数
	let countV = 30; 
	let width = 640 / countH;
	let height = 480 / countV;
	let target = GetTransitionRenderTargetName();
	let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D); //2D頂点ブジェクト生成
	ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLELIST);
	ObjPrim_SetVertexCount(obj, countH * countV * 6);
	Obj_SetRenderPriorityI(obj, 0); //描画優先度を設定
	ObjPrim_SetTexture(obj, target); //テクスチャを設定

	ascent(ix in 0.. countH)
	{
		ascent(iy in 0.. countV)
		{
			let index = (ix + iy * countH) * 6;
			let left = ix * width;
			let right = left + width;
			let top = iy * height;
			let bottom = top + height;
			TVertex(index, left, top, right, bottom);
		}
	}

}

task TMenu{
	task TMenuItem(let index, let mx, let my, let text){
		let alpha1	= 0;
		let alphaA	= 0;
		let move	= 32;
		let move1	= 0;
		let moveA	= 0;
		let move2	= 0;
		let moveB	= 0;
		let count	= 0;
		function CreateTextObject(let mx, let my, let text){
			let id		= ObjPrim_Create(OBJ_SPRITE_2D);
			let sca		= 0.7;
			let sca2	= 0.7;
			
			ObjRender_SetPosition(id,mx,my,0);
			ObjPrim_SetTexture(id,csd~"../img/front/pause.png");
			ObjRender_SetBlendType(id,BLEND_ALPHA);
			ObjRender_SetAngleXYZ(id,0,0,0);
			ObjRender_SetScaleXYZ(id,sca,sca,0);
			ObjRender_SetColor(id,64,64,64);
			ObjRender_SetAlpha(id,0);
			Obj_SetRenderPriorityI(id,11);
			alternative(text)
			case("一時停止"){
				ObjSprite2D_SetSourceRect(id,0,0,256,64);
				ObjSprite2D_SetDestCenter(id);
				ObjRender_SetColor(id,255,255,255);
				ObjRender_SetScaleXYZ(id,sca2,sca2,0);
			}
			case("満身創痍"){
				ObjSprite2D_SetSourceRect(id,256,0,512,64);
				ObjSprite2D_SetDestCenter(id);
				ObjRender_SetColor(id,255,255,255);
				ObjRender_SetScaleXYZ(id,sca2,sca2,0);
			}
			case("再開"){
				ObjRender_SetPosition(id,mx-32,my,0);
				ObjSprite2D_SetSourceRect(id,0,64,256,128);
				ObjSprite2D_SetDestCenter(id);
			}
			case("再生終了"){
				ObjSprite2D_SetSourceRect(id,256,64,512,128);
				ObjSprite2D_SetDestCenter(id);
				ObjRender_SetColor(id,255,255,255);
				ObjRender_SetScaleXYZ(id,sca2,sca2,0);
			}
			case("タイトルに戻る"){
				ObjSprite2D_SetSourceRect(id,0,128,256,192);
				ObjSprite2D_SetDestCenter(id);
			}
			case("再挑戦"){
				ObjRender_SetPosition(id,mx-32,my,0);
				ObjSprite2D_SetSourceRect(id,256,128,512,192);
				ObjSprite2D_SetDestCenter(id);
			}
			case("最初から始める"){
				ObjSprite2D_SetSourceRect(id,0,192,256,256);
				ObjSprite2D_SetDestCenter(id);
			}
			case("鉄杵成針"){
				ObjSprite2D_SetSourceRect(id,256,192,512,256);
				ObjSprite2D_SetDestCenter(id);
				ObjRender_SetColor(id,255,255,255);
				ObjRender_SetScaleXYZ(id,sca2,sca2,0);
			}
			case("リプレイを保存する"){
				ObjSprite2D_SetSourceRect(id,0,256,256,320);
				ObjSprite2D_SetDestCenter(id);
			}
			return id;
		}

		let objText = CreateTextObject(mx, my, text);
		let objSelect = CreateTextObject(mx, my, text);
		loop{
			if(alpha1<255){alphaA+=5;}
			alpha1=255*sin(alphaA);
			if(index==selectIndex){
				move2=0;
				moveB=0;
				if(move1<move){moveA+=10;}
				move1=move*sin(moveA);
				alternative(text)
				case("再開"){
					ObjRender_SetPosition(objText,mx+255/2-alpha1/2-32-9+move1,my,0);
					ObjRender_SetPosition(objSelect,mx+255/2-alpha1/2-32-9+move1,my,0);
				}
				case("再挑戦"){
					ObjRender_SetPosition(objText,mx+255/2-alpha1/2-32+move1,my,0);
					ObjRender_SetPosition(objSelect,mx+255/2-alpha1/2-32+move1,my,0);
				}
				case("リプレイを保存する"){
					ObjRender_SetPosition(objText,mx+255/2-alpha1/2+16+move1,my,0);
					ObjRender_SetPosition(objSelect,mx+255/2-alpha1/2+16+move1,my,0);
				}
				others{
					ObjRender_SetPosition(objText,mx+255/2-alpha1/2+move1,my,0);
					ObjRender_SetPosition(objSelect,mx+255/2-alpha1/2+move1,my,0);
				}
			}else{
				move1=0;
				moveA=0;
				if(move2<move){moveB+=10;}
				move2=move*sin(moveB);
				alternative(text)
				case("再開"){
					ObjRender_SetPosition(objText,mx+255/2-alpha1/2-32-9+move-move2,my,0);
					ObjRender_SetPosition(objSelect,mx+255/2-alpha1/2-32-9+move-move2,my,0);
				}
				case("再挑戦"){
					ObjRender_SetPosition(objText,mx+255/2-alpha1/2-32+move-move2,my,0);
					ObjRender_SetPosition(objSelect,mx+255/2-alpha1/2-32+move-move2,my,0);
				}
				case("リプレイを保存する"){
					ObjRender_SetPosition(objText,mx+255/2-alpha1/2+16+move-move2,my,0);
					ObjRender_SetPosition(objSelect,mx+255/2-alpha1/2+16+move-move2,my,0);
				}
				others{
					ObjRender_SetPosition(objText,mx+255/2-alpha1/2+move-move2,my,0);
					ObjRender_SetPosition(objSelect,mx+255/2-alpha1/2+move-move2,my,0);
				}
			}
			ObjRender_SetAlpha(objText,alpha1);
			ObjRender_SetAlpha(objSelect,alpha1);
			Obj_SetVisible(objSelect, index == selectIndex);
			
			count+=8;
			ObjRender_SetColor(objSelect,255,128,128);
			
			yield;
		}
	}

	//メニュー配置
	let id			= ObjPrim_Create(OBJ_SPRITE_2D);
	let sca			= 1;
	let alpha1		= 0;
	let alphaA		= 0;
	let mode		= GetAreaCommonData("System","PlayMode","NULL");
	let clear		= GetAreaCommonData("System","ClearSpellPractice",false);
	let continue	= GetAreaCommonData("System","Continue",false);
	let selectIndex	= 0;//選択位置
	let cc			= 0;
	let mx			= 96+32;
	let my			= 224+64+16;
	let texts		= ["リプレイを保存する", "タイトルに戻る", "最初から始める"];
	let listResult	= [RESULT_SAVE_REPLAY, RESULT_END, RESULT_RETRY];
	let rgb			= [64,160,160];
	
	if(IsReplay){
		texts		= ["タイトルに戻る", "最初から始める"];
		listResult	= [RESULT_END, RESULT_RETRY];
	}
	if( mode == "Story" && !IsReplay ){
		if( GetAreaCommonData("System","難易度","NULL") == "Extra" ){
			texts		= ["タイトルに戻る", "最初から始める",];
			listResult	= [RESULT_END, RESULT_RETRY];
		}
		else{
			if( continue ){
				texts		= ["再挑戦", "タイトルに戻る", "最初から始める",];
				listResult	= [RESULT_RETRY, RESULT_END, RESULT_RETRY];
			}
			else{
				texts		= ["再挑戦", "リプレイを保存する", "タイトルに戻る", "最初から始める",];
				listResult	= [RESULT_RETRY, RESULT_SAVE_REPLAY, RESULT_END, RESULT_RETRY];
			}
		}
		PlayMusic_GameOver( 18 );
	}
	if(mode=="Spell" && !IsReplay){
		texts		= ["再挑戦", "リプレイを保存する", "タイトルに戻る"];
		listResult	= [RESULT_RETRY, RESULT_SAVE_REPLAY, RESULT_END];
	}
	
	var countMenu	= length(texts);
	
	ObjRender_SetPosition(id,mx-16-8,my,0);
	ObjPrim_SetTexture(id,csd~"../img/front/pause2.png");
	ObjRender_SetBlendType(id,BLEND_ALPHA);
	ObjRender_SetAngleXYZ(id,0,0,-45+alpha1/255*45);
	ObjRender_SetScaleXYZ(id,sca,sca+0.1,0);
	ObjRender_SetAlpha(id,alpha1);
	Obj_SetRenderPriorityI(id,10);
	ObjSprite2D_SetSourceRect(id,0,0,128,256);
	ObjSprite2D_SetDestCenter(id);
	
	if(IsReplay){
		TMenuItem(countMenu+1, mx-32-8, my-64-16, "再生終了");
	}
	else if(!clear){
		TMenuItem(countMenu+1, mx-32-8, my-64-16, "満身創痍");
		rgb			= [160,64,64];
	}
	else{
		TMenuItem(countMenu+1, mx-32-8, my-64-16, "鉄杵成針");
		rgb			= [160,160,64];
	}
	ascent(var iText in 0 .. countMenu){
		TMenuItem(iText, mx+32, my-32, texts[iText]);
		my += 32;
	}
	
	ObjRender_SetColor(id,rgb[0],rgb[1],rgb[2]);

	//キー状態がリセットされるまで待機
	while(GetVirtualKeyState(VK_OK) != KEY_FREE){yield;}

	//メニュー選択処理
	let frameKeyHold = 0;//キー押しっぱなしフレーム数
	loop
	{
		cc++;
		if(cc%2==0){
			if(!clear){
				CherryEffectP(rand(0,640),rand(0,480),0.4,0.4,rand(32,128),[98,0,0]);
			}
			else{
				CherryEffectP(rand(0,640),rand(0,480),0.4,0.4,rand(32,128),[98,98,0]);
			}
		}
		if(alpha1<255){alphaA+=5;}
		alpha1=255*sin(alphaA);
		ObjRender_SetAlpha(id,alpha1);
		ObjRender_SetAngleXYZ(id,0,0,(-45+alpha1/255*45)+8*sin(cc));
		//決定
		if( GetVirtualKeyState(VK_OK) == KEY_PULL )
		{
			SetScriptResult(listResult[selectIndex]);
				
			if( !IsReplay ){
				//　非リプレイ
				//　再挑戦 Story
				if( mode == "Story" && selectIndex == 0 ){
					SetAreaCommonData("System","Continue",true);
				}
				//　最初から始める Story
				if( mode == "Story" && selectIndex == 3 ){
					if( GetAreaCommonData("System","難易度","NULL") == "Extra" ){
						SetAreaCommonData("System","Continue",false);
						SetAreaCommonData("System","Stage_Index",7);
					}
					else{
						SetAreaCommonData("System","Continue",false);
						SetAreaCommonData("System","Stage_Index",1);
					}
				}
				//　最初から始める Stage
				if( mode == "Stage" && selectIndex == 2 ){
					SetAreaCommonData("System","Stage_Index",1);
					SetAreaCommonData("Trance","BGM_Stop",false);
				}
			}
			else{
				//　リプレイ
				//　最初から始める Story
				if( mode == "Story" && selectIndex == 1 ){
					if( GetAreaCommonData("System","難易度","NULL") == "Extra" ){
						SetAreaCommonData("System","Stage_Index",7);
					}
					else{
						SetAreaCommonData("System","Stage_Index",1);
					}
				}
			}
			
			AddScore(-GetScore);
			SetAreaCommonData("Trance","max_score",0);
			SetAreaCommonData("Trance","Hidden_World",false);
			SetAreaCommonData("ST_System","GetSkillItem",0);
			SetAreaCommonData("Trance","player_fragment",0);
			SetAreaCommonData("Trance","bomb_fragment",0);
			SetAreaCommonData("System","ClearSpellPractice",false);
			menu_on = false;
			PlaySEsP(csd~"../se/menudecide.wav");
			loop(12){yield;}
			CloseScript(GetOwnScriptID());
			return;
		}

		//カーソル移動
		if(GetVirtualKeyState(VK_UP) == KEY_PUSH)
		{
			selectIndex--;
			PlaySEsP(csd~"../se/menuselect.wav");
		}
		else if(GetVirtualKeyState(VK_DOWN) == KEY_PUSH)
		{
			selectIndex++;
			PlaySEsP(csd~"../se/menuselect.wav");
		}
		else if(GetVirtualKeyState(VK_UP) == KEY_HOLD)
		{
			frameKeyHold++;
			if(frameKeyHold == 30 || (frameKeyHold > 30 && (frameKeyHold % 10 == 0)))
			{
				selectIndex--;
				PlaySEsP(csd~"../se/menuselect.wav");
			}
		}
		else if(GetVirtualKeyState(VK_DOWN) == KEY_HOLD)
		{
			frameKeyHold++;
			if(frameKeyHold == 30 || (frameKeyHold > 30 && (frameKeyHold % 10 == 0)))
			{
				selectIndex++;
				PlaySEsP(csd~"../se/menuselect.wav");
			}
		}
		else
		{
			frameKeyHold = 0;
		}

		if(selectIndex < 0) 
		{
			selectIndex = countMenu - 1;
		}
		else
		{
			selectIndex %= countMenu;
		}

		yield;
	}
}

task CherryEffectP(x,y,px,py,long,rgb_h){
	let id		= ObjPrim_Create(OBJ_SPRITE_2D);
	let csd		= GetCurrentScriptDirectory();
	let ps		= [0,0];
	let cc		= 0;
	let lspin	= truncate(rand(0,100));
	let alpha1	= 0;
	let alphaA	= 0;
	let alpha2	= 0;
	let alphaB	= 0;
	let alpha3	= 0;
	let alphaC	= 0;
	let sca		= rand(0.2,1);
	let flg		= 0;
	let flg2	= false;
	
	if(lspin>=0  && lspin<20){flg=0;}
	if(lspin>=20 && lspin<40){flg=1;}
	if(lspin>=30 && lspin<60){flg=2;}
	if(lspin>=40 && lspin<80){flg=3;}
	if(lspin>=80 && lspin<=100){flg=4;}
	
	ObjRender_SetPosition(id,x,y,0);
	ObjPrim_SetTexture(id,csd~"../img/eff/eff_default.png");
	ObjRender_SetBlendType(id,BLEND_ADD_RGB);
	ObjRender_SetAngleXYZ(id,0,0,0);
	ObjRender_SetScaleXYZ(id,sca+0.2,sca,0);
	Obj_SetRenderPriorityI(id,9);
	ObjSprite2D_SetSourceRect(id,33,223,65,255);
	ObjSprite2D_SetDestCenter(id);
	
	while(long>cc){
		let gpos	= [ObjRender_GetX(id),ObjRender_GetY(id)];
		if(gpos[0]<(32+16) || gpos[0]>(32+384-16) || gpos[1]<(16+16) || gpos[1]>(16+448-16)){
			break;
		}
		
		ps[0] =  ps[0]+px;
		ps[1] =  ps[1]+py;
		ObjRender_SetPosition(id,x+ps[0],y+ps[1],0);
		
		cc++;
		if(flg==0){ObjRender_SetAngleXYZ(id,(cc*1)*-1,(cc*1)*1,(cc*4)*-1);}
		if(flg==1){ObjRender_SetAngleXYZ(id,(cc*1)*1,(cc*1)*-1,(cc*4)*-1);}
		if(flg==2){ObjRender_SetAngleXYZ(id,(cc*1)*-1,(cc*1)*1,(cc*4)*1);}
		if(flg==3){ObjRender_SetAngleXYZ(id,(cc*1)*1,(cc*1)*-1,(cc*4)*1);}
		if(flg==4){ObjRender_SetAngleXYZ(id,(cc*1)*1,(cc*1)*1,(cc*4)*-1);}
		
		if(alpha1<rgb_h[0]){
			alphaA+=5;
			alpha1= rgb_h[0] * sin(alphaA);
		}
		if(alpha2<rgb_h[1]){
			alphaB+=5;
			alpha2= rgb_h[1] * sin(alphaB);
		}
		if(alpha3<rgb_h[2]){
			alphaC+=5;
			alpha3= rgb_h[2] * sin(alphaC);
		}
		ObjRender_SetColor(id,alpha1,alpha2,alpha3);
		
		yield;
	}
	loop{
		ps[0] =  ps[0]+px;
		ps[1] =  ps[1]+py;
		ObjRender_SetPosition(id,x+ps[0],y+ps[1],0);
		
		cc++;
		if(flg==0){ObjRender_SetAngleXYZ(id,(cc*1)*-1,(cc*1)*1,(cc*4)*-1);}
		if(flg==1){ObjRender_SetAngleXYZ(id,(cc*1)*1,(cc*1)*-1,(cc*4)*-1);}
		if(flg==2){ObjRender_SetAngleXYZ(id,(cc*1)*-1,(cc*1)*1,(cc*4)*1);}
		if(flg==3){ObjRender_SetAngleXYZ(id,(cc*1)*1,(cc*1)*-1,(cc*4)*1);}
		if(flg==4){ObjRender_SetAngleXYZ(id,(cc*1)*1,(cc*1)*1,(cc*4)*-1);}
		
		if(alpha1>0){alphaA+=5;}
		if(alpha2>0){alphaB+=5;}
		if(alpha3>0){alphaC+=5;}
		alpha1= rgb_h[0] * sin(alphaA);
		alpha2= rgb_h[1] * sin(alphaB);
		alpha3= rgb_h[2] * sin(alphaC);
		ObjRender_SetColor(id,alpha1,alpha2,alpha3);
		
		if(alpha1<=0 && alpha2<=0 && alpha3<=0){break;}
		
		yield;
	}
	Obj_Delete(id);
}

//　ゲームオーバー用
task PlayMusic_GameOver(PLAY_NO){
	let id		= ObjSound_Create();
	let bgm_vol	= GetAreaCommonData("Option","BGM_Volume",100);
	let vol1	= 0;
	let volA	= 0;
	
	let sample	= [
		[236016,3452579],//タイトル
		[385152,7525193],//1 ROAD
		[185328,3632080],//1 BOSS
		[194304,6516144],//2 ROAD
		[356832,4440720],//2 BOSS
		[208896,6292752],//3 ROAD
		[323040,6392016],//3 BOSS
		[398064,7870848],//4 ROAD
		[251376,6371616],//4 BOSS
		[868224,7726272],//5 ROAD
		[1721088,8210352],//5 BOSS
		[357600,4717296],//6 BOSS
		[1199616,9809232],//6 BOSS
		[3326976,4386288],//6 LAST
		[378336,8058864],//EX ROAD
		[494880,7660176],//EX BOSS
		[1775088,2927424],//ED
		[1645200,4205616],//SR
		[207984,2766768],//PS
	];
	
	ObjSound_Load(id, Music_all[PLAY_NO] );
	ObjSound_SetSoundDivision(id, SOUND_BGM );
	ObjSound_SetVolumeRate(id, bgm_vol );
	ObjSound_SetLoopEnable(id, true );
	ObjSound_SetRestartEnable(id, true );
	ObjSound_SetLoopSampleCount(id, sample[PLAY_NO][0], sample[PLAY_NO][1] );
	ObjSound_Play(id);
	
	while( menu_on ){
		bgm_vol	= GetAreaCommonData("Option","BGM_Volume",100);
		
		ObjSound_SetVolumeRate(id, bgm_vol );
		
		yield;
	}
	while( vol1 < 1 ){
		volA += 5;
		vol1 = 1 * sin(volA);
		ObjSound_SetVolumeRate(id, bgm_vol - vol1*bgm_vol );
		yield;
	}
	ObjSound_Stop(id);
	Obj_Delete(id);
}