#東方弾幕風[Stage]
#ScriptVersion[3]
#Title["IDF1 - The 1st Stage of WBaWC"]
#Text["Illusionary Danmaku Festival 1 - The 1st Stage of WBaWC"]
#Background["./background/Default_Background.txt"]
#System["./system/Default_System.txt"]

let stagepart = 1;
let bgm1;
let bgm2;
let bgm3;

@Initialize
{
	TStage();
}

@Event 
{
	alternative(GetEventType())
	case(EV_PAUSE_ENTER)
	{
		if(stagepart == 1){ ObjSound_Stop(bgm1); }
        	if(stagepart == 2){ ObjSound_Stop(bgm2); }
	}
	case(EV_PAUSE_LEAVE)
	{
		if(stagepart == 1){ ObjSound_Play(bgm1); }
        	if(stagepart == 2){ ObjSound_Play(bgm2); }
	}
}

@MainLoop
{
	yield;
}

@Finalize
{

}


task TStage
{
	let dir = GetCurrentScriptDirectory();
	bgm1 = ObjSound_Create();
	bgm2 = ObjSound_Create();

	let path1 = dir ~ "./EikaPlural.txt";
	let idScript1 = LoadScriptInThread(path1);

	bgm1 = ObjSound_Create();
	ObjSound_Load(bgm1, dir ~ "BGM.mp3");
	StageBGM(bgm1, 1);
	ObjSound_Play(bgm1);

	loop(60){yield;}
	StartScript(idScript1);

	while(!IsCloseScript(idScript1) && GetPlayerState() != STATE_END){ yield; }

	loop(120){ yield; }
	FinalStop(bgm1);
	loop(60){ yield; }

	stagepart = 2;

	let path2 = dir ~ "KoishiPlural1.txt";
	let idScript2 = LoadScriptInThread(path2);

	ObjSound_Load(bgm2, dir ~ "BGM1.mp3");
	StageBGM(bgm2, 2);
	ObjSound_Play(bgm2);

	StartScript(idScript2);

	while(!IsCloseScript(idScript2) && GetPlayerState() != STATE_END){ yield; }
	loop(60){ yield; }

	let path3 = dir ~ "KoishiPlural2.txt";
	let idScript3 = LoadScriptInThread(path3);

	StartScript(idScript3);
	while(!IsCloseScript(idScript3) && GetPlayerState() != STATE_END){ yield; }

	loop(180){yield;}
	FinalStop(bgm2);
	loop(60){ yield; }

	CloseStgScene();
}

function StageBGM(obj, ID)
{
	ObjSound_SetSoundDivision(obj, SOUND_BGM);
	ObjSound_SetRestartEnable(obj, true);
	ObjSound_SetLoopEnable(obj, true);
	if(ID == 1)
	{
		ObjSound_SetLoopTime(obj, 50.2, 128.8);
		ObjSound_SetVolumeRate(obj, 100);
	}
	if(ID == 2)
	{
		ObjSound_SetLoopTime(obj, 100.3, 225.7);
		ObjSound_SetVolumeRate(obj, 100);
	}
    	return obj;
}

task FinalStop(obj)
{
	let x = 100;
	loop(50)
	{
		ObjSound_SetVolumeRate(obj, x);
		x -= 2;
		yield;
	}
	loop(10){ yield; }
	ObjSound_Stop(obj);
}