let CurDir = GetCurrentScriptDirectory;
let bgm = ObjSound_Create;
let vol = 90;
let volReset = false;
ObjSound_Load(bgm,CurDir ~ "./bgm/boss.mp3");
ObjSound_SetRestartEnable(bgm,true);
ObjSound_SetVolumeRate(bgm,vol);
ObjSound_SetLoopEnable(bgm,true);

function playBGM(){
	ObjSound_Play(bgm);
	keepPlaying;
	task keepPlaying(){
		while(ObjSound_IsPlaying(bgm)){
			yield;
			if(!ObjSound_IsPlaying(bgm)){
				ObjSound_Play(bgm);
			}
		}
	}
}

function BgmVolDecrease(targetVol, time){
	
	if(ObjSound_IsPlaying(bgm)){
		let originalVol = ObjSound_GetVolumeRate(bgm);
		let dif = ObjSound_GetVolumeRate(bgm) - targetVol;
		dif = dif/time;
		let i = ObjSound_GetVolumeRate(bgm);
		loop(time){
			
			ObjSound_SetVolumeRate(bgm, i);
			i -= dif;
			yield;
			if(volReset){break;}
		}
		resetVol;
	}
}

function BgmVolIncrease(targetVol, time){
	if(ObjSound_IsPlaying(bgm)){
		let dif = targetVol - ObjSound_GetVolumeRate(bgm);
		dif = dif/time;
		let i = ObjSound_GetVolumeRate(bgm);
		loop(time){
			ObjSound_SetVolumeRate(bgm, i);
			i += dif;
			yield;
		}
	}
}

function resetVol(){
	volReset = true;
	ObjSound_SetVolumeRate(bgm, vol);
	yield;
	volReset = false;
}