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

#include "./lib_SystemScene.dnh"

let bEndTutorial = false;
let imgTutorial = GetCurrentScriptDirectory() ~ "./../../img/package/TutorialFont.png";
let ImgRect = [
	[ 0,  0, 512, 32],
	[ 0, 32, 512, 96],
	[ 0, 96, 512, 128],
	[ 0, 128, 512, 160],
	[ 0, 160, 512, 192],
	[ 0, 192, 512, 224],
	[ 0, 224, 512, 256]
];

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

@MainLoop
{
	yield;
}

@Finalize
{
}

task TBackground{
	//分割設定
	let target = GetTransitionRenderTargetName();
	let obj = ObjPrim_Create(OBJ_SPRITE_2D); //2D頂点ブジェクト生成
	Obj_SetRenderPriorityI(obj, 90); //描画優先度を設定
	ObjPrim_SetTexture(obj, target); //テクスチャを設定
	ObjRender_SetPosition(obj, 0,0,0);
	ObjSprite2D_SetDestRect(obj, 0,0,640,480);
	ObjSprite2D_SetSourceRect(obj, 0,0,640,480);
	let objT = TutorialImg(GetCommonData("TNumber", 0));
	ascent(i in 0 .. 15){
		let color = 255-96*i/14;
		ObjRender_SetColor(obj, color, color, color);
		yield;
	}
	let color = 255-96;
	ObjRender_SetColor(obj, color, color, color);
	while(!bEndTutorial){
		yield;
	}
	descent(i in 0 .. 15){
		let color = 255-96*i/14;
		ObjRender_SetColor(obj, color, color, color);
		ObjRender_SetAlpha(objT, 255*i/14);
		yield;
	}
	Obj_Delete(obj);
	Obj_Delete(objT);
	
	function TutorialImg(num){
		let Rect = ImgRect[num%length(ImgRect)];
		let width  = Rect[2] - Rect[0];
		let height = Rect[3] - Rect[1];
		let obj = ObjPrim_Create(OBJ_SPRITE_2D); //2D頂点ブジェクト生成
		Obj_SetRenderPriorityI(obj, 91); //描画優先度を設定
		ObjPrim_SetTexture(obj, imgTutorial); //テクスチャを設定
		ObjRender_SetPosition(obj, GetCenterX(),GetCenterY()-60,0);
		ObjSprite2D_SetDestRect(obj, -width/2, -height/2, width/2, height/2);
		ObjSprite2D_SetSourceRect(obj, Rect[0], Rect[1], Rect[2], Rect[3]);
		return obj;
	}

}

task TMenu{
	_PlaySE(SE_PAUSE);
	//　15フレーム待機
	loop( 15 ){ yield; }

	// キー待機
	WaitForResetKeyEx([VK_OK,VK_CANCEL]);
	loop{
		//決定
		if(GetVirtualKeyState(VK_OK) == KEY_PUSH || GetVirtualKeyState(VK_CANCEL) == KEY_PUSH){
			_PlaySE(SE_DECIDE);
			bEndTutorial = true;
			break;
		}
		yield;
	}
	loop(15){
		yield;
	}
	CloseScript(GetOwnScriptID());

}


function GetCenterX(){
	return (GetCommonData("STGFrame_L", 128)+GetCommonData("STGFrame_R", 512))/2;
}
function GetCenterY(){
	return (GetCommonData("STGFrame_U", 16)+GetCommonData("STGFrame_D", 464))/2;
}
function GetClipMinX(){
	return GetCommonData("STGFrame_L", 128);
}
function GetClipMinY(){
	return GetCommonData("STGFrame_U", 16);
}
function GetClipMaxX(){
	return GetCommonData("STGFrame_R", 512);
}
function GetClipMaxY(){
	return GetCommonData("STGFrame_D", 464);
}


function GetScreenCenterX(){
	return GetStgFrameWidth()/2;
}
function GetScreenCenterY(){
	return GetStgFrameHeight()/2;
}
function GetScreenMinX(){
	return 0;
}
function GetScreenMinY(){
	return 0;
}
function GetScreenMaxX(){
	return  GetStgFrameWidth();
}
function GetScreenMaxY(){
	return GetStgFrameHeight();
}