@Initialize
{
	SetAutoDeleteObject(true);


    TDifficulty;


}


@MainLoop
{

	yield;
}

@Finalize
{
}





//----------------------------------------------------
task TDifficulty
{

	

	let bEndTitleScene = false;


	let dir = GetCurrentScriptDirectory();
	let pathTitle = dir ~ "./img/title.png";

	let objTitleImage = ObjPrim_Create(OBJ_SPRITE_2D); 
	Obj_SetRenderPriorityI(objTitleImage, 20); 
	ObjPrim_SetTexture(objTitleImage, pathTitle);
	ObjSprite2D_SetSourceRect(objTitleImage, 0, 0, 640, 480);
	ObjSprite2D_SetDestRect(objTitleImage, 0, 0, 640, 480);


	let objTitleText = ObjText_Create();
	ObjText_SetText(objTitleText, "パッケージサンプル");
	ObjText_SetFontSize(objTitleText, 40);
	ObjText_SetFontBold(objTitleText, true);
	ObjText_SetFontColorTop(objTitleText, 128, 128, 255);
	ObjText_SetFontColorBottom(objTitleText, 64, 64, 255);
	ObjText_SetFontBorderType(objTitleText, BORDER_FULL);
	ObjText_SetFontBorderColor(objTitleText,255, 255, 255);
	ObjText_SetFontBorderWidth(objTitleText, 2);
	Obj_SetRenderPriorityI(objTitleText, 30);
	ObjRender_SetX(objTitleText, 48);
	ObjRender_SetY(objTitleText, 48);



	let INDEX_EASY = 0;
	let INDEX_NORMAL = 1;
	let INDEX_HARD = 2;
	let INDEX_DESPERATE = 3;
	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, 128, 128, 255);
			ObjText_SetFontColorBottom(obj, 64, 64, 255);
			ObjText_SetFontBorderType(obj, BORDER_FULL);
			ObjText_SetFontBorderColor(obj,255, 255, 255);
			ObjText_SetFontBorderWidth(obj, 2);
			Obj_SetRenderPriorityI(obj, 30);
			ObjRender_SetX(obj, mx);
			ObjRender_SetY(obj, my);
			return obj;
		}

		let objText = CreateTextObject(mx, my,  text);
		let objSelect = CreateTextObject(mx, my, text);
		ObjRender_SetBlendType(objSelect, BLEND_ADD_RGB);
		while(!bEndTitleScene)
		{
			Obj_SetVisible(objSelect, index == selectIndex);
			yield;
		}
		Obj_Delete(objText);
		Obj_Delete(objSelect);
	}


	let mx = 48;
	let my = 360;
	let texts = ["Easy", "Normal","Hard", "Desperate"];
	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;}

	
	let frameKeyHold = 0;
	loop
	{

		if(GetVirtualKeyState(VK_OK) == KEY_PUSH)
		{
			if(selectIndex == INDEX_EASY)
			{
				TStageScene("");
			}
			
			else if(selectIndex == INDEX_NORMAL)
			{
				
			}
			else if(selectIndex == INDEX_HARD)
			{
			
			}
			else if(selectIndex == INDEX_DESPERATE)
			{
		
			}
			
			
			break;
		}


		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 == 30 || (frameKeyHold > 30 && (frameKeyHold % 10 == 0)))
			{
				selectIndex--;
			}
		}
		else if(GetVirtualKeyState(VK_DOWN) == KEY_HOLD)
		{
			frameKeyHold++;
			if(frameKeyHold == 30 || (frameKeyHold > 30 && (frameKeyHold % 10 == 0)))
			{
				selectIndex++;
			}
		}
		else
		{
			frameKeyHold = 0;
		}

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

		yield;
	}

	bEndTitleScene = true;
	Obj_Delete(objTitleImage);
	Obj_Delete(objTitleText);
}



//----------------------------------------------------
// ユーティリティ
//----------------------------------------------------
function CreateTextObject(let mx, let my, let size, let text)
{
	let obj = ObjText_Create();
	ObjText_SetText(obj, text);
	ObjText_SetFontSize(obj, size);
	ObjText_SetFontBold(obj, true);
	ObjText_SetFontColorTop(obj, 128, 128, 255);
	ObjText_SetFontColorBottom(obj, 64, 64, 255);
	ObjText_SetFontBorderType(obj, BORDER_FULL);
	ObjText_SetFontBorderColor(obj,255, 255, 255);
	ObjText_SetFontBorderWidth(obj, 2);
	Obj_SetRenderPriorityI(obj, 40);
	ObjRender_SetX(obj, mx);
	ObjRender_SetY(obj, my);
	return obj;
}


//-----------------

task TStageScene(let pathReplay)
{
	let dirCurrent = GetCurrentScriptDirectory();
	let dirModule = GetModuleDirectory();
	let pathMainScript = dirModule ~ "script/sample/nociception/stagetest.txt";
	let pathPlayer = dirModule ~ "script/player/newplayer3/player2.txt";

	//--------------------------------

	RenderSceneToTransitionTexture();
	TTransition();

	//--------------------------------

	InitializeStageScene();
	if(length(pathReplay) > 0)
	{
		SetStageReplayFile(pathReplay);
	}

	//--------------------------------

	let indexStage = 1; 
	SetStageIndex(indexStage);
	SetStageMainScript(pathMainScript);
	SetStagePlayerScript(pathPlayer);
	StartStageScene();

	//--------------------------------

	loop
	{
		//--------------------------------

		if(GetVirtualKeyState(VK_PAUSE) == KEY_PUSH)
		{
			let resPause = RunPauseScene();
			alternative(resPause)
			case(RESULT_RETRY)
			{
				
				if(!IsReplay())
				{
					//リトライ
					TerminateStageScene();
					TStageScene("");
					return;
				}
			}
			case(RESULT_END)
			{
				
				TerminateStageScene();
			}
		}

		if(!IsReplay() && GetKeyState(KEY_BACK) == KEY_PUSH)
		{
			
			TerminateStageScene();
			TStageScene("");
			return;
		}

		//--------------------------------
		
		let stgSceneState = GetStageSceneState();
		if(stgSceneState == STAGE_STATE_FINISHED)
		{
		
			let stageResult = GetStageSceneResult();
			alternative(stageResult)
			case(STAGE_RESULT_CLEARED)
			{
				
				if(indexStage == 2)
				{
					
					TEndScene();
					break;
				}
				else
				{
					
					indexStage++;
					SetStageIndex(indexStage);
					SetStageMainScript(pathMainScript); 
					SetStagePlayerScript(pathPlayer);
					StartStageScene();
					TTransition();
				}
			}
			case(STAGE_RESULT_PLAYER_DOWN)
			{
				
				TEndScene();
				break;
			}
			case(STAGE_RESULT_BREAK_OFF)
			{
				
				break;
			}
		}
		yield;
	}	

	TTransition();
}

task TTransition
{
	let textureName = GetCurrentScriptDirectory ~ "img/title.png";

	let objImage = ObjPrim_Create(OBJ_SPRITE_2D); 
	Obj_SetRenderPriorityI(objImage, 100); 
	ObjPrim_SetTexture(objImage, textureName); 
	ObjSprite2D_SetSourceRect(objImage, 0, 0, 640, 480);
	ObjSprite2D_SetDestRect(objImage, 0, 0, 640, 480);

	let alpha = 255;
	while(alpha > 0)
	{
		ObjRender_SetAlpha(objImage, alpha);
		alpha -= 16;
		yield;
	}
	Obj_Delete(objImage);
}

function RenderSceneToTransitionTexture()
{

	let textureName = GetTransitionRenderTargetName();
	RenderToTextureA1(textureName, 0, 100, true);
}

//----------------------------------------------------
// リプレイ保存シーン
//----------------------------------------------------
task TReplaySaveScene()
{
	let dirModule = GetModuleDirectory();
	let pathScript = dirModule ~ "script/default_system/Default_ReplaySaveScene.txt";

	//--------------------------------
	
	let idScript = LoadScript(pathScript);
	StartScript(idScript);

	while(!IsCloseScript(idScript))
	{
		yield;
	}


	//--------------------------------

	
}

task TEndScene()
{
	if(IsReplay())
	{
	
		return;
	}

	//--------------------------------

	FinalizeStageScene();

	//--------------------------------

	let dirModule = GetModuleDirectory();
	let pathScript = dirModule ~ "script/default_system/Default_EndScene.txt";
	let idScript = LoadScript(pathScript);
	StartScript(idScript);

	while(!IsCloseScript(idScript))
	{
		yield;
	}

	//--------------------------------

	let result = GetScriptResult(idScript);
	alternative(result)
	case(RESULT_SAVE_REPLAY)
	{
		//リプレイ保存
		TReplaySaveScene();
	}
	case(RESULT_END)
	{
		//再生終了
		
	}
	case(RESULT_RETRY)
	{
		//リトライ
		TStageScene("");
	}
}



//----------------------------------------------------
function RunPauseScene()
{
	RenderSceneToTransitionTexture();
	PauseStageScene(true);

	let dirModule = GetModuleDirectory();
	let pathScript = dirModule ~ "script/sample/nociception/my_system/Default_Pause.txt";

	//--------------------------------

	let idScript = LoadScript(pathScript);
	StartScript(idScript);

	while(!IsCloseScript(idScript))
	{
		yield;
	}

	PauseStageScene(false);

	let res = GetScriptResult(idScript);
	
	return res;
}


