//一時停止中スクリプト

@Initialize
{
	SetAutoDeleteObject(true);
	TBackground_2(0,0,255);
	TBlack;
	ascent(j in -3..4)
	{
	ascent(i in -3..4)
	{
		TBackground(i*0.5,j*0.5,5);
	}
	}
	TMenu();
}

@MainLoop
{
	yield;
}

@Finalize
{
}


task TBackground(dx,dy,alpha)
{
	let target = GetTransitionRenderTargetName();
	let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D); //2D頂点ブジェクト生成
	ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
	ObjPrim_SetVertexCount(obj, 4);
	Obj_SetRenderPriorityI(obj, 100); //描画優先度を設定
	if(alpha==255){Obj_SetRenderPriorityI(obj, 0);}
	ObjPrim_SetTexture(obj, target); //テクスチャを設定

	let index=0;
	ObjPrim_SetVertexPosition(obj, index + 0, 32, 16, 0);
	ObjPrim_SetVertexPosition(obj, index + 1, 32, 464, 0);
	ObjPrim_SetVertexPosition(obj, index + 2, 416, 464, 0);
	ObjPrim_SetVertexPosition(obj, index + 3, 416,16, 0);

	ObjPrim_SetVertexUVT(obj, index + 0, 32, 16);
	ObjPrim_SetVertexUVT(obj, index + 1, 32, 464);
	ObjPrim_SetVertexUVT(obj, index + 2, 416, 464);
	ObjPrim_SetVertexUVT(obj, index + 3, 416, 16);
	ObjRender_SetX(obj,dx);
	ObjRender_SetY(obj,dy);

	ObjRender_SetAlpha(obj,alpha);
}

task TBlack
{
	let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"../img/effect/Black.png");
	ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetVertexCount(obj, 4);
//	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
	Obj_SetRenderPriorityI(obj,100);
	ObjRender_SetAngleZ(obj,0);

	ObjPrim_SetVertexPosition(obj, 0, -192, -224, -0);
	ObjPrim_SetVertexPosition(obj, 1, -192, 224, -0);
	ObjPrim_SetVertexPosition(obj, 2, 192, -224, 0);
	ObjPrim_SetVertexPosition(obj, 3, 192,224, 0);

	ObjPrim_SetVertexUVT(obj, 0, 0, 0);
	ObjPrim_SetVertexUVT(obj, 1, 0, 128);
	ObjPrim_SetVertexUVT(obj, 2, 128, 0);
	ObjPrim_SetVertexUVT(obj, 3, 128, 128);

	ObjRender_SetX(obj,192+32);
	ObjRender_SetY(obj,224+16);
	loop
	{
		yield;
	}
	Obj_Delete(obj);
}

task TBackground_2(dx,dy,alpha)
{

	let target = GetTransitionRenderTargetName();
	let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D); //2D頂点ブジェクト生成
	ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
	ObjPrim_SetVertexCount(obj, 4);
	Obj_SetRenderPriorityI(obj, 100); //描画優先度を設定
	if(alpha==255){Obj_SetRenderPriorityI(obj, 100);}
	ObjPrim_SetTexture(obj, target); //テクスチャを設定

	let index=0;
	ObjPrim_SetVertexPosition(obj, index + 0, 0, 0, 0);
	ObjPrim_SetVertexPosition(obj, index + 1, 0, 480, 0);
	ObjPrim_SetVertexPosition(obj, index + 2, 640, 480, 0);
	ObjPrim_SetVertexPosition(obj, index + 3, 640,0, 0);

	ObjPrim_SetVertexUVT(obj, index + 0, 0, 0);
	ObjPrim_SetVertexUVT(obj, index + 1, 0, 480);
	ObjPrim_SetVertexUVT(obj, index + 2, 640, 480);
	ObjPrim_SetVertexUVT(obj, index + 3, 640, 0);
	ObjRender_SetX(obj,dx);
	ObjRender_SetY(obj,dy);

	ObjRender_SetAlpha(obj,alpha);
}

task TMenu
{
	let selectIndex = 0;//選択位置
	task TMenuItem(let index, let mx, let my, let text)
	{
		function CreateTextObject(let mx, let my, let text)
		{
			let obj = ObjText_Create();
			ObjText_SetText(obj, text);
			ObjText_SetFontSize(obj, 20);
			ObjText_SetFontBold(obj, true);
			ObjText_SetFontColorTop(obj, 255, 255, 255);
			ObjText_SetFontColorBottom(obj, 128, 128, 255);
		//	ObjText_SetFontBorderType(obj, BORDER_FULL);
			ObjText_SetFontBorderType(obj, BORDER_NONE);
			ObjText_SetFontBorderColor(obj,255,255, 255);
			ObjText_SetFontBorderWidth(obj, 2*0);
			Obj_SetRenderPriorityI(obj, 100);
			ObjRender_SetX(obj, mx);
			ObjRender_SetY(obj, my);

			ObjText_SetFontType(obj,"ＭＳ 明朝");
			return obj;
		}

		let objText = CreateTextObject(mx+60, my, text);
		let AlphaRate=0;
		Slide;
		loop
		{
			if(index == selectIndex)
			{
				ObjRender_SetAlpha(objText,255*AlphaRate);
				Effect(objText);
				while(index == selectIndex){ObjRender_SetAlpha(objText,255*AlphaRate);yield;}
			}
			else
			{
				ObjRender_SetAlpha(objText,150*AlphaRate);
			}
			yield;
		}

		task Slide
		{
			while(ObjRender_GetX(objText)>mx)
			{
				ObjRender_SetX(objText,ObjRender_GetX(objText)-6);
				AlphaRate+=1/10;
				yield;
			}
		}

		task Effect(obj)
		{
			let AngleX=0;
			loop(10)
			{
				AngleX+=36;
				ObjRender_SetAngleX(obj,AngleX);
				yield;
			}
		}

	}

	//メニュー配置
	let mx = 64;
	let my = 324;
	let texts = ["再挑戦", "もう帰る","リプレイを残す"];
	var countMenu = length(texts);
	ascent(var iText in 0 .. countMenu)
	{
		TMenuItem(iText, mx, my, texts[iText]);
		my += 32;
	}

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

	SelectEffect;

	//メニュー選択処理
	let frameKeyHold = 0;//キー押しっぱなしフレーム数
//	DrawMainExplain;
	loop
	{
		//決定
		if(GetVirtualKeyState(VK_OK) == KEY_PULL)
		{
			if(selectIndex==0 && GetCommonData("ContinueCount",0)>0)
			{
			}
			else
			{
			let listResult = [RESULT_RETRY, RESULT_END, RESULT_SAVE_REPLAY];
			SetScriptResult(listResult[selectIndex]);
			CloseScript(GetOwnScriptID());
			return;
			}
		}

		//カーソル移動
		if(GetVirtualKeyState(VK_UP) == KEY_PUSH)
		{
			selectIndex--;
		}
		else if(GetVirtualKeyState(VK_DOWN) == KEY_PUSH)
		{
			selectIndex++;
		}
		else if(GetVirtualKeyState(VK_UP) == KEY_HOLD)
		{
			frameKeyHold++;
			if(frameKeyHold == 24 || (frameKeyHold > 24 && (frameKeyHold % 10 == 0)))
			{
				selectIndex--;
			}
		}
		else if(GetVirtualKeyState(VK_DOWN) == KEY_HOLD)
		{
			frameKeyHold++;
			if(frameKeyHold == 24 || (frameKeyHold > 24 && (frameKeyHold % 10 == 0)))
			{
				selectIndex++;
			}
		}
		else
		{
			frameKeyHold = 0;
		}

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

		yield;
	}

	task DrawMainExplain
	{
		let obj = ObjText_Create();
		ObjText_SetText(obj, "ステージを開始します");
		ObjText_SetFontSize(obj, 18);
		ObjText_SetFontBold(obj, false);
//		ObjText_SetFontColorTop(obj, 128, 128, 255);
//		ObjText_SetFontColorBottom(obj, 64, 64, 255);
		ObjText_SetFontBorderType(obj, BORDER_SHADOW);
		ObjText_SetFontBorderColor(obj,255, 255, 255);
		ObjText_SetFontBorderWidth(obj, 0);
		Obj_SetRenderPriorityI(obj, 30);
		ObjRender_SetX(obj, 44);
		ObjRender_SetY(obj, 400);
		ObjRender_SetAlpha(obj,225);
//		let Text=["ステージを開始します","スペルプラクティスを開始します","設定を変更します","実績を確認します","終了します"];
		let Text=["リプレイを残します","タイトルに戻ります"];
		if(GetCommonData("ContinueCount",0)>0)
		{
			Text[0]="コンティニューしたので残せません"
		}
		while(1)
		{
			ObjText_SetText(obj, Text[selectIndex]);
			yield;
		}
		Obj_Delete(obj);
	}

	task SelectEffect
	{
		let x=0;
		let y=0;
		loop
		{
			y=324+32*selectIndex;
		//	Light(rand(0,640),y+rand(0,16),rand(1,3),180+rand(-5,5));
			Light(rand(32,384+32),y+rand(15,16),rand(1,2)/3,270+rand(-5,5));
			Light(rand(32,384+32),y+rand(1,0),rand(1,2)/3,90+rand(-5,5));
			yield;
		}

		task Band
		{

		}

		task Light(X,Y,speed,angle)
		{
		let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"../img/effect/star.png");
		ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
		ObjPrim_SetVertexCount(obj, 4);
		ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
		ObjRender_SetAngleZ(obj,0);
		Obj_SetRenderPriorityI(obj, 100);

		ObjPrim_SetVertexPosition(obj, 0, -16, -16, -0);
		ObjPrim_SetVertexPosition(obj, 1, -16, 16, -0);
		ObjPrim_SetVertexPosition(obj, 2, 16, -16, 0);
		ObjPrim_SetVertexPosition(obj, 3, 16,16, 0);

		ObjPrim_SetVertexUVT(obj, 0, 0, 0);
		ObjPrim_SetVertexUVT(obj, 1, 0, 32);
		ObjPrim_SetVertexUVT(obj, 2, 32, 0);
		ObjPrim_SetVertexUVT(obj, 3, 32, 32);
		ObjRender_SetPosition(obj,X,Y,0);

		let scale=0.5;
		let Alpha=180;
		let Zangle=0;
		let ZanglePlus=rand(-5,5);

		loop(30)
		{
			scale-=0.5/30;
			Zangle+=ZanglePlus;
			ObjRender_SetPosition(obj,ObjRender_GetX(obj)+speed*cos(angle),ObjRender_GetY(obj)+speed*sin(angle),0);
			ObjRender_SetScaleXYZ(obj,scale,scale,scale);
			ObjRender_SetAngleZ(obj,Zangle);
			ObjRender_SetColor(obj,Alpha*0.75,Alpha*0.75,Alpha);
			yield;
		}
		Obj_Delete(obj);
		}
	}
}



