#TouhouDanmakufu[Package]
#ScriptVersion[3]
#Title["“Œ•û Treasure Castle Labyrinth ~ Extra Stage"]

//load
#include"./Stage/Script/LoadGraphics.txt"
#include"./Stage/Script/LoadSounds.txt"

let dir;
let dif = "";
local{
	dir = GetCurrentScriptDirectory();}

	let transit = 0;
	let pathMain =  dir ~ "./Stage/Extra_Stage.txt";
	let pathPlayer = dir ~ "./Player/Marisa by plasmaflame22/Marisa_A.txt";
	let fontFile = dir ~ "./Fonts/Blogger Sans.ttf";
	let dataFile = dir ~ "./data/main_menuTCL_common_cutin_History.dat";

//menus
#include"./replay_menuTCL.txt"
#include"./player_menuTCL.txt"
#include"./spellpractice_menuTCL.txt"

@Initialize{
	Effect;
	TTitleScene;
	LoadGraphics;
	LoadSounds;
	LoadMusics;
	//SetSkipModeKey(KEY_INVALID);

	InstallFont(fontFile);
	SetCommonData("Spell",0);
	SetCommonData("TRANSIT",1);
	if(!IsCommonDataAreaExists("MUSIC")){
		CreateCommonDataArea("MUSIC");}
	LoadCommonDataAreaA1("MUSIC");
	
	if(!IsCommonDataAreaExists("cutin_History")){
		CreateCommonDataArea("cutin_History");}
	LoadCommonDataAreaA2("cutin_History",dataFile);}

@MainLoop{yield;}
	
@Finalize{}

task TTitleScene{
	let bEndTitleScene = false;

	let pathTitle = dir ~ "./Image/menu.png";
	let pathTitle2 = dir ~ "./Image/load.png";
	let pathTitle3 = dir ~ "./Image/lock.png";
	let pathFire = dir ~ "./Image/firesheet.png";

	CreateCommonDataArea("ULTRA");
	let loaded = LoadCommonDataAreaA2("ULTRA",GetCurrentScriptDirectory ~ "./System/ultra.dat");
	if(!loaded){
		SetAreaCommonData("ULTRA","PATH",0);}
		
	if(GetCommonData("Loading",0)==0){
		let objTitleImage2 = ObjPrim_Create(OBJ_SPRITE_2D); 
		ObjPrim_SetTexture(objTitleImage2, pathTitle2);
		ObjSprite2D_SetSourceRect(objTitleImage2, 0, 0, 640, 480);
		ObjSprite2D_SetDestRect(objTitleImage2, 0, 0, 640, 480);
		Obj_SetRenderPriorityI(objTitleImage2, 20);
		loop(180){yield;}
		let alpha = 255;
		loop(60){
			ObjRender_SetAlpha(objTitleImage2, alpha);
			alpha-=255/60; yield;}
		Obj_Delete(objTitleImage2);
		Effect;
	loop(45){yield;}}
	ObjSound_Play(musicARRAY[3]);
	
	SetCommonData("Loading",1);
	let objTitleImage = ObjPrim_Create(OBJ_SPRITE_2D); 
	Obj_SetRenderPriorityI(objTitleImage, 20);
	ObjPrim_SetTexture(objTitleImage, pathTitle);
	ObjSprite2D_SetSourceRect(objTitleImage, 0, 0, 800, 600);
	ObjSprite2D_SetDestRect(objTitleImage, 0, 0, 640, 480);
	
	let objTitleImage4 = ObjPrim_Create(OBJ_SPRITE_2D);
	if(GetAreaCommonData("ULTRA","PATH",0) == 0){
	ObjPrim_SetTexture(objTitleImage4, pathTitle3);
	ObjSprite2D_SetSourceRect(objTitleImage4, 0, 0, 50, 50);
	ObjSprite2D_SetDestCenter(objTitleImage4);
	Obj_SetRenderPriorityI(objTitleImage4,24);
	ObjRender_SetPosition(objTitleImage4,208,347,0);
	ObjRender_SetScaleXYZ(objTitleImage4,0.8,0.8,0);
	ObjRender_SetAlpha(objTitleImage4,0);}

	let INDEX_START_EXTRA = 0;
	let INDEX_START_ULTRA = 1;
	let INDEX_REPLAY = 2;
	let INDEX_PRACTICE = 3;
	let INDEX_QUIT = 4;

	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, 32);
			ObjText_SetFontType(obj,"Blogger Sans");
			ObjText_SetFontColorTop(obj, 255, 225, 255);
			ObjText_SetFontColorBottom(obj, 255, 255, 255);
			Obj_SetRenderPriorityI(obj, 20);
			ObjRender_SetX(obj, mx);
			ObjRender_SetY(obj, my);
			return obj;}
				
		function CreateArrow(let mx, let my){
			let obja = ObjPrim_Create(OBJ_SPRITE_2D);
			ObjPrim_SetTexture(obja, pathFire);
			Obj_SetRenderPriorityI(obja, 25);
			ObjRender_SetScaleXYZ(obja,0.8,0.8,0);
			ObjRender_SetPosition(obja, mx, my, 0);
			ObjRender_SetAlpha(obja,0);
			task tManage{
				let animFrame = 0;let w = 3;
				while(!Obj_IsDeleted(obja)){
				ObjSprite2D_SetDestRect(obja,-38,-70,38,70);
					if(animFrame < w){ObjSprite2D_SetSourceRect(obja,0,8,76,148);}
					if(animFrame >= w && animFrame < w*3){ObjSprite2D_SetSourceRect(obja,82,8,158,148);}
					if(animFrame >= w*3 && animFrame < w*5){ObjSprite2D_SetSourceRect(obja,160,8,236,148);}
					if(animFrame >= w*5 && animFrame < w*7){ObjSprite2D_SetSourceRect(obja,230,8,306,148);}
					if(animFrame >= w*7 && animFrame < w*9){ObjSprite2D_SetSourceRect(obja,310,8,386,148);}
					if(animFrame >= w*9 && animFrame < w*11){ObjSprite2D_SetSourceRect(obja,388,10,464,150);}
					if(animFrame >= w*11 && animFrame < w*13){ObjSprite2D_SetSourceRect(obja,462,7,538,147);}
					if(animFrame >= w*13 && animFrame < w*15){ObjSprite2D_SetSourceRect(obja,544,8,620,148);}
					if(animFrame >= w*15){ObjSprite2D_SetSourceRect(obja,622,8,698,148);}
					if(animFrame>=w*17){animFrame=0;}
				animFrame++;yield;}}
			tManage;
			return obja;}

		let objMenuText = CreateTextObject(mx, my, text);
		let objArrow = CreateArrow(mx-12,my-8);
		let alphaAR = 0;
		let scaleSS = 0;
		loop(20){
			if(index == selectIndex){
				ObjRender_SetAlpha(objArrow,alphaAR);}
			ObjRender_SetScaleX(objMenuText,scaleSS);
			ObjRender_SetAlpha(objTitleImage4,alphaAR);
			scaleSS+=0.05;alphaAR+=255/20;yield;}

	let move1 = 0;
	let moveA = 0;
	let move2 = 0;
	let moveB = 0;
	let scale = 1;
		
	while(!bEndTitleScene){
		if(index == selectIndex){
			ObjRender_SetAlpha(objArrow,255);
			ObjRender_SetAlpha(objMenuText,255);
			Obj_SetRenderPriority(objMenuText,0.2);
			ObjRender_SetPosition(objMenuText,mx-move1,my,0);
			move2=0;
			moveB=0;
			move1=15*sin(moveA);
			if(move1<4){moveA+=10;}}
		else{
			ObjRender_SetAlpha(objArrow,0);
			ObjRender_SetAlpha(objMenuText,155);
			ObjRender_SetPosition(objMenuText,mx-move1+move2,my,0);
			move1=0;
			moveA=0;
			move2=4*sin(moveB);
			if(move2<4){moveB+=10;}}
		if(GetVirtualKeyState(VK_OK) == KEY_PUSH){
			loop(20){
				if(index == selectIndex){
					ObjRender_SetAlpha(objArrow,alphaAR);}
					ObjRender_SetAlpha(objTitleImage4,alphaAR);
				ObjRender_SetScaleX(objMenuText,scale);
				scale-=0.05;alphaAR-=255/20;yield;}}
		yield;}
	Obj_Delete(objMenuText);
	Obj_Delete(objArrow);}


	let mx = 150;
	let my = 300;
	let texts = ["Start Extra","Start Ultra","Replay","Practice","Quit"];
	var countMenu = length(texts);
	ascent(var iText in 0 .. countMenu){
		TMenuItem(iText, mx, my, texts[iText]);
		my += 35;}


	while(GetVirtualKeyState(VK_OK) != KEY_FREE){yield;}

	let alphaTitle = 255;
	let frameKeyHold = 0;
	loop{
		if(GetVirtualKeyState(VK_CANCEL) == KEY_PUSH){
			selectIndex = INDEX_QUIT;
			SE(se_cancel);}

		if(GetVirtualKeyState(VK_OK) == KEY_PUSH){
			if(selectIndex == INDEX_START_EXTRA){
			SE(se_ok);dif = "Extra";
			pathMain =  dir ~ "./Stage/Extra_Stage.txt";
			SetCommonData("Replay",0);loop(20){
				ObjRender_SetAlpha(objTitleImage,alphaTitle);
				alphaTitle-=135/20; yield;}
				TPlayerScene(0);}
				
			if(selectIndex == INDEX_START_ULTRA){
			SE(se_ok);dif = "Ultra";
			pathMain =  dir ~ "./Stage/Ultra_Stage.txt";
			SetCommonData("Replay",1);loop(20){
				ObjRender_SetAlpha(objTitleImage,alphaTitle);
				alphaTitle-=135/20; yield;}
				TPlayerScene(0);}

			else if(selectIndex == INDEX_REPLAY){
			SE(se_ok);loop(20){
				ObjRender_SetAlpha(objTitleImage,alphaTitle);
				alphaTitle-=135/20; yield;}
				TReplayScene();}

			else if(selectIndex == INDEX_PRACTICE){
			pathMain =  dir ~ "./Stage/Practice_Stage.txt";
			SE(se_ok); transit = 1;
			SetCommonData("Replay",2);
				loop(20){
				ObjRender_SetAlpha(objTitleImage,alphaTitle);
				alphaTitle-=135/20; yield;}
				TPracticeScene();}

			else if(selectIndex == INDEX_QUIT){
				ClosePackage();}
			break;}

		if(GetAreaCommonData("ULTRA","PATH",0)==0 && selectIndex == INDEX_START_EXTRA &&
			GetVirtualKeyState(VK_DOWN) == KEY_PUSH){
			SE(se_select);
			selectIndex+=1;}
		if(GetAreaCommonData("ULTRA","PATH",0)==0 && selectIndex == INDEX_REPLAY &&
			GetVirtualKeyState(VK_UP) == KEY_PUSH){
			SE(se_select);
			selectIndex-=1;}
			
		if(GetVirtualKeyState(VK_UP) == KEY_PUSH){
			SE(se_select);
			selectIndex--;}

		else if(GetVirtualKeyState(VK_DOWN) == KEY_PUSH){
			SE(se_select);
			selectIndex++;}

		else if(GetVirtualKeyState(VK_UP) == KEY_HOLD){
			frameKeyHold++;
			if(frameKeyHold == 30 || (frameKeyHold > 30 && (frameKeyHold % 10 == 0))){
			if(GetAreaCommonData("ULTRA","PATH",0)==0 && selectIndex == INDEX_REPLAY){selectIndex-=1;}
				SE(se_select);
				selectIndex--;}}

		else if(GetVirtualKeyState(VK_DOWN) == KEY_HOLD){
			frameKeyHold++;
			if(frameKeyHold == 30 || (frameKeyHold > 30 && (frameKeyHold % 10 == 0))){
			if(GetAreaCommonData("ULTRA","PATH",0)==0 && selectIndex == INDEX_START_EXTRA){selectIndex+=1;}
				SE(se_select);
				selectIndex++;}}

		else{frameKeyHold = 0;}
		if(selectIndex < 0) {selectIndex = countMenu - 1;}
		else{selectIndex %= countMenu;}
		yield;}
	bEndTitleScene = true;
	Obj_Delete(objTitleImage);
	Obj_Delete(objTitleImage4);}

task T3StageScene(let pathReplay){
	SetCommonData("BG",0);
	SetCommonData("MiyakoBG",0);
	ObjSound_Stop(musicARRAY[3]);
	
	RenderSceneToTransitionTexture();
	TTransition();
	
	InitializeStageScene();
	if(length(pathReplay) > 0){
		SetStageReplayFile(pathReplay);}

	let indexStage = 1; 
	SetStageIndex(indexStage);
	SetStageMainScript(pathMain);
	SetStagePlayerScript(pathPlayer);
	StartStageScene();
	
	loop{
		if(GetVirtualKeyState(VK_PAUSE) == KEY_PUSH){
			let resPause = RunPauseScene();
			alternative(resPause)
			case(RESULT_RETRY){
				if(!IsReplay()){
					SetCommonData("BG",0);
					TerminateStageScene();
					T3StageScene("");
					return;}
			}
			case(RESULT_END){
					TerminateStageScene();}}

		let stgSceneState = GetStageSceneState();
		if(stgSceneState == STAGE_STATE_FINISHED){
			let stageResult = GetStageSceneResult();
			alternative(stageResult)
			case(STAGE_RESULT_CLEARED){
				if(indexStage == 1){
					TEndScene();
					break;}}
			case(STAGE_RESULT_PLAYER_DOWN){
				TEndScene();
				break;}
			case(STAGE_RESULT_BREAK_OFF){
				TTitleScene;
				break;}}
		yield;}
	TTransition();}

task TEnd(objT){
	let scale = 0; loop(50){
	ObjRender_SetScaleY(objT,scale);
	scale-=0.02; yield;}
	return;}

task Effect{
	let alpha = 0;
	let obj = ObjPrim_Create(OBJ_SPRITE_2D); 
	Obj_SetRenderPriorityI(obj, 50);
	ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"./Image/black.png");
	ObjSprite2D_SetSourceRect(obj, 0, 0, 640, 480);
	ObjSprite2D_SetDestRect(obj, 0, 0, 640, 480);
	wait(60);
	loop(85){
		ObjRender_SetAlpha(obj,255-alpha);
		alpha+=3;yield;}
	Obj_Delete(obj);}

task TReplaySelectScene(){
	let pathScript = GetCurrentScriptDirectory() ~ "./replay_listTCL.txt";

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

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

	let result = GetScriptResult(idScript);
	if(length(result) == 0){
		ObjSound_Play(musicARRAY[3]);
		TReplayScene;}
 	else{
		T3StageScene(result);}}

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

task TTransition{
	let textureName = GetTransitionRenderTargetName();

	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 RunPauseScene(){
	RenderSceneToTransitionTexture();
	PauseStageScene(true);

	let pathScript = GetCurrentScriptDirectory ~ "./System/Miyako_Pause.txt";

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

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

	PauseStageScene(false);

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

task TReplaySaveScene(){
	let pathScript = GetCurrentScriptDirectory ~ "./System/Miyako_ReplaySaveScene.txt";

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

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

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

	let pathScript = GetCurrentScriptDirectory ~ "./System/Miyako_EndScene.txt";
	let idScript = LoadScript(pathScript);
	StartScript(idScript);

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

	let result2 = GetScriptResult(idScript);
	alternative(result2)
	case(RESULT_SAVE_REPLAY){
		TReplaySaveScene();}
	case(RESULT_END){
		if(GetCommonData("Replay",0) == 2){
			ObjSound_Play(musicARRAY[3]);
			TSpellScene;}
		else{
			TTitleScene}}
	case(RESULT_RETRY){
		T3StageScene("");}}

function wait(w){ loop(w){yield;}}