//Sound Effect and BGM Script

let CSD = GetCurrentScriptDirectory;
let MD = GetModuleDirectory;

let objBGM = ID_INVALID;

#include"./../LIB/Function_Misc.dnh"
#include"./SE_Const.dnh"

@Initialize {
	LoadSounds;
	// LoadBGM;
}

@MainLoop {
	yield;
}

@Event {
	alternative(GetEventType)
	case(EV_SOUND) {
		let eventParam = GetEventArgument(0);
		if(eventParam[1]==100) {
			PlaySE(SE_ALL[eventParam[0]]);
		}
		else {
			PlaySound(SE_ALL[eventParam[0]],eventParam[1]);
		}
	}
	case(EV_MUSIC_START) {
		let eventParam = GetEventArgument(0);
		objBGM = PlayMusic(eventParam);
	}
	case(EV_PAUSE_ENTER){ ObjSound_Stop(objBGM); }
	case(EV_PAUSE_LEAVE){ ObjSound_Play(objBGM); }
}

task PlaySound(sound,volume) {
	let obj = ObjSound_Create;
	ObjSound_SetSoundDivision(obj,SOUND_SE);
	ObjSound_Load(obj,sound);
	ObjSound_SetVolumeRate(obj,volume);
	ObjSound_Play(obj);
	
	loop(120) { yield; }
	
	ObjSound_Load(obj,"");
	Obj_Delete(obj);
}
function PlayMusic(id) {
	Obj_Delete(objBGM);
	let objSound = ObjSound_Create;
	let offset = 0;
	if(id==0.1) {
		offset = 420;
		ObjSound_Load(objSound,MD ~ "BGM/title.ogg");
		ObjSound_SetRestartEnable(objSound,true);
		// ObjSound_SetLoopSampleCount(objSound,5594400,5594400);
		ObjSound_SetLoopEnable(objSound,true);
	}
	if(id==0.2) {
		offset = 420;
		ObjSound_Load(objSound,MD ~ "BGM/playerscore.ogg");
		ObjSound_SetRestartEnable(objSound,true);
		// ObjSound_SetLoopSampleCount(objSound,5594400,5594400);
		ObjSound_SetLoopEnable(objSound,true);
	}
	if(id==1.1) {
		offset = 100;
		ObjSound_Load(objSound,MD ~ "BGM/stage1.ogg");
		ObjSound_SetRestartEnable(objSound,true);
		ObjSound_SetLoopSampleCount(objSound,5594400,11188800);//55934426
		ObjSound_SetLoopEnable(objSound,true);
		DrawBGMText("Dream Sunrise");
	}
	if(id==1.2) {
		offset = 275;
		ObjSound_Load(objSound,MD ~ "BGM/stage1boss.ogg");
		ObjSound_SetRestartEnable(objSound,true);
		ObjSound_SetLoopSampleCount(objSound,66152,4828950);
		ObjSound_SetLoopEnable(objSound,true);
		DrawBGMText("Unfired Jar of Awakened Vengeance");
	}
	if(id==2.1) {
		offset = 600;
		ObjSound_Load(objSound,MD ~ "BGM/stage2.ogg");
		ObjSound_SetRestartEnable(objSound,true);
		// ObjSound_SetLoopSampleCount(objSound,4233729,8184960);
		ObjSound_SetLoopEnable(objSound,true);
		DrawBGMText("???");
	}
	if(id==2.2) {
		offset = 600;
		ObjSound_Load(objSound,MD ~ "BGM/stage2boss.ogg");
		ObjSound_SetRestartEnable(objSound,true);
		// ObjSound_SetLoopSampleCount(objSound,4233729,8184960);
		ObjSound_SetLoopEnable(objSound,true);
		DrawBGMText("???");
	}
	if(id==3.1) {
		offset = 600;
		ObjSound_Load(objSound,MD ~ "BGM/stage3.ogg");
		ObjSound_SetRestartEnable(objSound,true);
		// ObjSound_SetLoopSampleCount(objSound,4233729,8184960);
		ObjSound_SetLoopEnable(objSound,true);
		DrawBGMText("???");
	}
	if(id==3.2) {
		offset = 600;
		ObjSound_Load(objSound,MD ~ "BGM/stage3boss.ogg");
		ObjSound_SetRestartEnable(objSound,true);
		// ObjSound_SetLoopSampleCount(objSound,4233729,8184960);
		ObjSound_SetLoopEnable(objSound,true);
		DrawBGMText("???");
	}
	if(id==4.1) {
		offset = 620;
		ObjSound_Load(objSound,MD ~ "BGM/stage4.ogg");
		ObjSound_SetRestartEnable(objSound,true);
		// ObjSound_SetLoopSampleCount(objSound,4233729,8184960);
		ObjSound_SetLoopEnable(objSound,true);
		DrawBGMText("???");
	}
	if(id==4.2) {
		offset = 625;
		ObjSound_Load(objSound,MD ~ "BGM/stage4boss.ogg");
		ObjSound_SetRestartEnable(objSound,true);
		// ObjSound_SetLoopSampleCount(objSound,4233729,8184960);
		ObjSound_SetLoopEnable(objSound,true);
		DrawBGMText("The Flower Bearer's March ~ Fairy General");
	}
	if(id==5.1) {
		offset = 600;
		ObjSound_Load(objSound,MD ~ "BGM/stage5.ogg");
		ObjSound_SetRestartEnable(objSound,true);
		// ObjSound_SetLoopSampleCount(objSound,4233729,8184960);
		ObjSound_SetLoopEnable(objSound,true);
		DrawBGMText("???");
	}
	if(id==5.2) {
		offset = 600;
		ObjSound_Load(objSound,MD ~ "BGM/stage5boss.ogg");
		ObjSound_SetRestartEnable(objSound,true);
		// ObjSound_SetLoopSampleCount(objSound,4233729,8184960);
		ObjSound_SetLoopEnable(objSound,true);
		DrawBGMText("Sealed Innocence");
	}
	if(id==6.1) {
		offset = 600;
		ObjSound_Load(objSound,MD ~ "BGM/stage6.ogg");
		ObjSound_SetRestartEnable(objSound,true);
		ObjSound_SetLoopSampleCount(objSound,4233729,8184960);
		ObjSound_SetLoopEnable(objSound,true);
		DrawBGMText("Spring Equinox ~ Perfect Sky");
	}
	if(id==6.2) {
		offset = 600;
		ObjSound_Load(objSound,MD ~ "BGM/stage6boss.ogg");
		ObjSound_SetRestartEnable(objSound,true);
		// ObjSound_SetLoopSampleCount(objSound,4233729,8184960);
		ObjSound_SetLoopEnable(objSound,true);
		DrawBGMText("Battle at the Capitol of Flowers");
	}
	if(id==6.3) {
		offset = 600;
		ObjSound_Load(objSound,MD ~ "BGM/stage6bossfinal.ogg");
		ObjSound_SetRestartEnable(objSound,true);
		// ObjSound_SetLoopSampleCount(objSound,4233729,8184960);
		ObjSound_SetLoopEnable(objSound,true);
		DrawBGMText("???");
	}
	if(id==7.1) {
		offset = 600;
		ObjSound_Load(objSound,MD ~ "BGM/stageEx.ogg");
		ObjSound_SetRestartEnable(objSound,true);
		// ObjSound_SetLoopSampleCount(objSound,4233729,8184960);
		ObjSound_SetLoopEnable(objSound,true);
		DrawBGMText("???");
	}
	if(id==7.2) {
		offset = 600;
		ObjSound_Load(objSound,MD ~ "BGM/stageExboss.ogg");
		ObjSound_SetRestartEnable(objSound,true);
		// ObjSound_SetLoopSampleCount(objSound,4233729,8184960);
		ObjSound_SetLoopEnable(objSound,true);
		DrawBGMText("Righteous Struggle ~ Duel with a Devil");
	}
	if(id==8.1) {
		offset = 600;
		ObjSound_Load(objSound,MD ~ "BGM/ending.ogg");
		ObjSound_SetRestartEnable(objSound,true);
		// ObjSound_SetLoopSampleCount(objSound,4233729,8184960);
		ObjSound_SetLoopEnable(objSound,true);
		DrawBGMText("???");
	}
	if(id==8.2) {
		offset = 600;
		ObjSound_Load(objSound,MD ~ "BGM/credits.ogg");
		ObjSound_SetRestartEnable(objSound,true);
		// ObjSound_SetLoopSampleCount(objSound,4233729,8184960);
		ObjSound_SetLoopEnable(objSound,true);
		DrawBGMText("???");
	}
	
	ObjSound_SetVolumeRate(objSound,100);
	ObjSound_SetSoundDivision(objSound,SOUND_BGM);
	ObjSound_Play(objSound);
	return objSound;
	
	task DrawBGMText(text) {
		let objText = ObjText_Create;
		let x = 0;
		let y = 0;
		
		ObjText_SetText(objText, "BGM: " ~ text);
		ObjText_SetFontType(objText, "Russo One");
		ObjText_SetFontSize(objText, 13);
		ObjText_SetFontBold(objText, false);
		ObjText_SetFontColorTop(objText, 255, 0, 255);
		ObjText_SetFontColorBottom(objText, 255, 255, 255);
		ObjText_SetFontBorderType(objText, BORDER_FULL);
		ObjText_SetFontBorderColor(objText, 0, 0, 0);
		ObjText_SetFontBorderWidth(objText, 1);
		ObjText_SetHorizontalAlignment(objText, ALIGNMENT_RIGHT);
		ObjText_SetMaxWidth(objText, 500);
		Obj_SetRenderPriorityI(objText, 70);
		
		ObjRender_SetY(objText,GetStgFrameHeight-19);
		
		while(x<offset) {
			ObjRender_SetX(objText,150-x);
			x+=3;
			yield;
		}
		ObjRender_SetX(objText,150-offset);
		loop(180) { yield; }
		while(y<20) {
			ObjRender_SetY(objText,GetStgFrameHeight-19+y);
			y+=1;
			yield;
		}
		Obj_Delete(objText);
	}
}
// task LoadBGM {
	// ascent(i in 0..length(BGM_ALL)) {
		// LoadSound(BGM_ALL[i]);
	// }
// }

task LoadSounds {
	ascent(i in 0..length(SE_ALL)) {
		LoadSound(SE_ALL[i]);
	}
}

