#TouhouDanmakufu[Package]
#ScriptVersion[3]
#Title["Game Thing"]
#Text["By Sphe"]
#Player["./player/Player.txt"]

function _PlaySE(name){NotifyEventAll(EV_USER_PACKAGE + 10, [name]);}
function _PlayBGM(name){NotifyEventAll(EV_USER_PACKAGE + 11, [name]);}
function wait(w){loop(w){yield;}}

let lineobj = GetCurrentScriptDirectory ~ "img/bg/fog.png";
let player_sprite = GetCurrentScriptDirectory ~ "player/img/";

let objTitleImage = ObjPrim_Create(OBJ_SPRITE_2D);
Obj_SetRenderPriorityI(objTitleImage, 5);
ObjPrim_SetTexture(objTitleImage, GetCurrentScriptDirectory ~ "img/system/Package_Fond.png"); //テクスチャを設定
ObjSprite2D_SetSourceRect(objTitleImage, 0, 0, 640, 480);
ObjSprite2D_SetDestRect(objTitleImage, 0, 0, 640, 480);

#include "./lib/package/lib_package_sound.txt"
#include "./lib/package/lib_package_options.txt"
#include "./lib/package/lib_package_player.txt"
#include "./lib/package/lib_package_pauses.txt"

@Initialize{
	InstallFont(GetCurrentScriptDirectory() ~ "font/Russell Square Regular.ttf");
	TTitleScene();
	BGMControl();
}

@MainLoop{yield;}

@Event{
	alternative(GetEventType()) 
	
	case(EV_USER_PACKAGE + 10){
		let a = GetEventArgument(0)[0];
		ObjSound_SetVolumeRate(soundLoad[a],GetAreaCommonData("Data","SE",85));
		ObjSound_Play(soundLoad[a]);
	}
	case(EV_USER_PACKAGE + 11){
		let a = GetEventArgument(0)[0];
		bgmState = a;
	}
	case(EV_USER_PACKAGE + 12){
		if(bgmState-1 >= 0){ObjSound_Play(BGMLoad[bgmState-1]);}
	}
	case(EV_USER_PACKAGE + 13){
		ObjSound_Stop(BGMLoad[bgmState-1]);
	}
}

task TStageScene(let pathReplay,pathPlayer2)
{
	let dirCurrent = GetCurrentScriptDirectory();
	let dirModule = GetModuleDirectory();
	let pathMainScript = dirCurrent ~ "stage.txt";
	let pathPlayer = dirCurrent ~ "player/" ~ pathPlayer2 ~ ".txt";

	CreateRenderTarget("ImagePauss");
	RenderToTextureA1("ImagePauss",0,100,true);
	SetCommonData("ImagePause","ImagePauss");

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

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

	TTransSp("ImagePauss");

	ObjSprite2D_SetSourceRect(objTitleImage, 0, 0, 0, 0);
	ObjSprite2D_SetDestRect(objTitleImage, 0, 0, 0, 0);

	let t = 0;

	loop
	{
		if(GetVirtualKeyState(VK_PAUSE) == KEY_PUSH && t >= 60+30)
		{
			CreateRenderTarget("ImagePauss");
			RenderToTextureA1("ImagePauss",0,100,true);
			SetCommonData("ImagePause","ImagePauss");
			NotifyEventAll(EV_USER_PACKAGE + 13, NULL);
			ObjSound_Stop(BGMLoad[bgmState-1]);
			if(IsReplay()){
				ObjSprite2D_SetSourceRect(objTitleImage, 0, 0, 640, 480);
				ObjSprite2D_SetDestRect(objTitleImage, 0, 0, 640, 480);
				TerminateStageScene();
				TTitleScene();
				break;
			}
			PauseStageScene(true);
			let resPause = TPauseScript();
			alternative(resPause)
			case(1)//Retry
			{
				TerminateStageScene();
				TStageScene(pathReplay,pathPlayer2);
				NotifyEventAll(EV_USER_PACKAGE + 11, [0]);
				return;
			}
			case(2)//Lolterminate
			{
				ObjSound_Stop(BGMLoad[bgmState-1]);
				NotifyEventAll(EV_USER_PACKAGE + 11, [0]);
				TerminateStageScene();
			}
			case(3){//Lolback
				NotifyEventAll(EV_USER_PACKAGE + 12, [0]);
			}
			PauseStageScene(false);
			NotifyEventAll(EV_USER_PACKAGE + 12, [0]);
		}

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

		let stgSceneState = GetStageSceneState();
		if(stgSceneState == STAGE_STATE_FINISHED)
		{
			let stageResult = GetStageSceneResult();
			alternative(stageResult)
			case(STAGE_RESULT_CLEARED)
			{
				TEndScene(pathReplay,pathPlayer2);
				break;
			}
			case(STAGE_RESULT_PLAYER_DOWN)
			{
				TEndScene(pathReplay,pathPlayer2);
				break;
			}
			case(STAGE_RESULT_BREAK_OFF)
			{
				ObjSprite2D_SetSourceRect(objTitleImage, 0, 0, 640, 480);
				ObjSprite2D_SetDestRect(objTitleImage, 0, 0, 640, 480);
				TTitleScene();
				break;
			}
		}

		if(t < 60+50){t++;}

		yield;
	}	

	TTransition();
}

task TEndScene(pathReplay,pathPlayer2)
{
	if(IsReplay())
	{
		TTitleScene();
		return;
	}

	FinalizeStageScene();
	let result = TEndScript();
	alternative(result)
	case(1)
	{
		TReplaySaveScene(pathReplay,pathPlayer2);
	}
	case(2)
	{
		ObjSprite2D_SetSourceRect(objTitleImage, 0, 0, 640, 480);
		ObjSprite2D_SetDestRect(objTitleImage, 0, 0, 640, 480);
		TTitleScene();
	}
	case(3)
	{
		TTransSp(GetCommonData("ImagePause",""));
		TStageScene(pathReplay,pathPlayer2);
	}
}

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

	let dirCurrent = GetCurrentScriptDirectory();
	let pathScript = dirCurrent ~ "lib/Pause.txt";

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

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

	PauseStageScene(false);

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

task TReplaySaveScene(pathReplay,pathPlayer2)
{
	let dirCurrent = GetCurrentScriptDirectory();
	let pathScript = dirCurrent ~ "lib/ReplaySave.txt";

	SetCommonData("PlayerReplay",pathPlayer2);

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

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

	let result = GetScriptResult(idScript);
	alternative(result)
	case(RESULT_CANCEL)
	{
		TEndScene(pathReplay,pathPlayer2);
	}
	case(RESULT_END)
	{
		SetCommonData("PlayerReplay","");
		ObjSprite2D_SetSourceRect(objTitleImage, 0, 0, 640, 480);
		ObjSprite2D_SetDestRect(objTitleImage, 0, 0, 640, 480);
		TTitleScene();
	}
}

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);
}

task TTransSp(tet){
	
	ascent(a in 0..12){
		ascent(b in 0..16){
			ItemFall(128+a*384/12,16+b*448/16,tet);
		}
	}


	task ItemFall(let x,let y,tet){
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(obj, tet);
		ObjSprite2D_SetSourceRect(obj, x,y,x+384/12,y+448/16);
		ObjSprite2D_SetDestCenter(obj);
		ObjRender_SetPosition(obj,x+20,y+20,0);
		Obj_SetRenderPriorityI(obj,100);
		let rax = rand(-1,1);
		let ray = rand(-1,1);
		let raz = rand(-1,1);
		let rx = rand(-2,2);
		let ry = rand(1,2);
		let sc = 1;
		let t = 0;
		loop(y/40){yield;}
		while(y > -40){
			ObjRender_SetAngleXYZ(obj,rax*t,ray*t,raz*t);
			ObjRender_SetPosition(obj,x+rx*t,y,0);
			ObjRender_SetScaleXYZ(obj,sc,sc,0);

			sc = max(0,sc-0.5/60);
			y-=t/10*ry;
			t++;
			yield
		}
		Obj_Delete(obj);
	}
}

function DigitToArray(let digit,let count)
{
	let res = [];
	digit = truncate(digit);

	loop
	{
		let tnum = truncate(digit % 10);
		digit /= 10;
		res = [tnum] ~ res;
		if(truncate(digit) == 0){break;}
	}

	loop(max(0, count - length(res)))
	{
		res = [0] ~ res;
	}

	return res;
}

function GetNumSize(value){
	if(value<=1){return 1;}
	else{return truncate(log10(value))+1;}
}