#TouhouDanmakufu[Package]
#Title["The One-Winged White Heron"]
#Text["Totally not re-used content <_< >_>"]
#Player["./Player/pl0/player.txt"]

#include "./Menu/MenuResult.txt"
#include "./system/package/package_sound.txt"

@Initialize
{
	InstallFont(GetCurrentScriptDirectory ~ "./system/img/GOTHIC.ttf");
	InstallFont(GetCurrentScriptDirectory ~ "./system/img/GOTHICB.ttf");
	InstallFont(GetCurrentScriptDirectory ~ "./system/img/GOTHICBI.ttf");
	InstallFont(GetCurrentScriptDirectory ~ "./system/img/GOTHICI.ttf");

	SetSkipModeKey(KEY_INVALID);
	AddVirtualKey(KEY_BACK,KEY_INVALID,KEY_INVALID);
	AddVirtualKey(VK_USER1,KEY_LCONTROL,KEY_INVALID);

	LoadCommonDataAreaA2("CONFIG",GetCurrentScriptDirectory ~ "./system/data/CONFIG");
	if(!IsCommonDataAreaExists("CONFIG"))
		{
		CreateCommonDataArea("CONFIG");
		SetAreaCommonData("CONFIG", "MUSIC_VOLUME", 1);
		SetAreaCommonData("CONFIG", "SOUND_VOLUME", 0.85);
		SetAreaCommonData("CONFIG", "EFFECT", 0);
		SetAreaCommonData("CONFIG", "TEXT_ENABLED",true);
		SetAreaCommonData("CONFIG", "CHEAT_ENABLED", true);
		SaveCommonDataAreaA2("CONFIG",GetCurrentScriptDirectory ~ "./system/data/CONFIG");
		}
	
	InitializeStageUnlock;
	InitializeSound;
	TTitleScene;
}

@Event
{
	alternative(GetEventType())
		case(EV_SOUND_PLAY)
			{
			TSound(GetEventArgument(0)[0],GetEventArgument(0)[1]);
			}
		case(EV_SOUND_STOP)
			{
			TSoundStop(GetEventArgument(0)[0]);
			}
}

@MainLoop
{
	yield;
}

@Finalize
{
}

task TTitleScene
{

	DeleteCommonData("PLAYER_LIFE");
	DeleteCommonData("PLAYER_BOMB");
	DeleteCommonData("PLAYER_FLASH");
	DeleteCommonData("BACKGROUND_END");
	DeleteCommonData("STAGE_TYPE");
	SetCommonData("INIT_SPELL",0);
	SetCommonData("CURRENT_SPELL",GetCommonData("INIT_SPELL",0));

	SetCommonData("EFFECT",GetAreaCommonData("CONFIG","EFFECT",0));
	SetCommonData("TEXT_ENABLED",GetAreaCommonData("CONFIG","TEXT_ENABLED",true));
	SetCommonData("CHEAT_ENABLED",GetAreaCommonData("CONFIG","CHEAT_ENABLED",false));
	let flash = GetAreaCommonData("CONFIG","FLASH_KEY",true);
	SetCommonData("FLASH_KEY",flash);

	if(flash == true)
		{
		AddVirtualKey(VK_USER2,KEY_C,KEY_INVALID);
		}	
	else
		{
		AddVirtualKey(VK_USER2,KEY_X,KEY_INVALID);
		}
	

	let Title = LoadScript(GetCurrentScriptDirectory ~ "Menu/Title.txt");
	StartScript(Title);
	while(!IsCloseScript(Title)){yield;}
	let array = GetScriptResult(Title);
	let mainSelect = array[0];
	alternative(array[0])
		case("START")
			{
			TStageScene("");
			}
		case("REPLAY")
			{
			TStageScene(array[1]);
			}
		case("EXIT")
			{
			ClosePackage;
			}
	Obj_Delete(Title);
	
}


task TStageScene(let pathReplay)
{
	let pathMainScript = GetCurrentScriptDirectory ~ "./Stage/Stage.txt";
	let pathPlayer = GetCurrentScriptDirectory ~ "./Player/pl" ~ IntToString(GetCommonData("PLAYER_1",0)) ~ "/player.txt";

	RenderSceneToTransitionTexture();
	TTransition();

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

	SetStageMainScript(pathMainScript);
	SetStagePlayerScript(pathPlayer);
	StartStageScene();

	loop
		{
		if(GetVirtualKeyState(VK_PAUSE) == KEY_PUSH)
			{
			let resPause = RunPauseScene();
			alternative(resPause)
			case(RESULT_RESTART)
				{
				if(!IsReplay())
					{
					SetCommonData("CURRENT_SPELL",GetCommonData("INIT_SPELL",0));
					DeleteCommonData("PLAYER_LIFE");
					DeleteCommonData("PLAYER_BOMB");
					TerminateStageScene();
					TStageScene("");
					return;
					}
				}
			case(RESULT_TITLE)
				{
				TerminateStageScene();
				TTitleScene();
				break;
				}
			case(RESULT_CLOSE)
				{
				ClosePackage;
				}
			}
		if(!IsReplay() && GetKeyState(KEY_BACK) == KEY_PUSH)
			{
			TerminateStageScene();
			TStageScene("");
			return;
			}
		if(GetStageSceneState == STAGE_STATE_FINISHED)
			{
			TEndScene();
			break;
			}
		yield;
		}	
	TTransition();
}


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

	FinalizeStageScene();

	let idScript = LoadScript(GetCurrentScriptDirectory ~ "Menu/EndScene.txt");
	StartScript(idScript);

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

	let result = GetScriptResult(idScript);
	alternative(result)
	case(RESULT_RESTART)
		{
		TerminateStageScene();
		yield;
		SetCommonData("CURRENT_SPELL",GetCommonData("INIT_SPELL",0));
		TStageScene("");
		}
	case(RESULT_TITLE)
		{
		TerminateStageScene();
		yield;
		TTitleScene();
		}
	case(RESULT_CLOSE)
		{
		TerminateStageScene();
		yield;
		ClosePackage;
		}
}

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

	let idScript = LoadScript(GetCurrentScriptDirectory ~ "Menu/Pause.txt");
	StartScript(idScript);

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

	PauseStageScene(false);

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

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

task TTransition
{
	let objImage = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(objImage, 100);
	ObjPrim_SetTexture(objImage, GetTransitionRenderTargetName());
	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);
}

task InitializeStageUnlock
{
	LoadCommonDataAreaA2("STAGE_UNLOCK",GetCurrentScriptDirectory ~ "system/data/SCORE");
	if(!IsCommonDataAreaExists("STAGE_UNLOCK"))
		{
		CreateCommonDataArea("STAGE_UNLOCK");
		}	
	let unlock = GetAreaCommonData("STAGE_UNLOCK", "STAGE_UNLOCK",[]);
	while(length(unlock) < 7)
		{
		unlock = unlock ~ [false];
		}
	SetAreaCommonData("STAGE_UNLOCK", "STAGE_UNLOCK",unlock);
	SaveCommonDataAreaA2("STAGE_UNLOCK",GetCurrentScriptDirectory ~ "system/data/SCORE");
}		