#TouhouDanmakufu[Plural]
#ScriptVersion[3]
#Title["Dave"]
#Player["./player/Reimu Hakurei/Player_Reimu.txt"]
#System["./system/NSystem.dnh"]
#Background["./system/Background.dnh"]
#include "./Singles/sharedfuncDave.dnh"


let bgm1= ObjSound_Create(); let bgm1Path = GetCurrentScriptDirectory ~ "./bgm1.ogg";
let bgm2= ObjSound_Create(); let bgm2Path= GetCurrentScriptDirectory~ "./bgm2.ogg";
let bgm3= ObjSound_Create(); let bgm3Path = GetCurrentScriptDirectory ~ "./bgm3.ogg";
let bgm4= ObjSound_Create(); let bgm4Path = GetCurrentScriptDirectory ~ "./bgm4.ogg";

task StartBGM(num){
    if(num==1){
		ObjSound_Load(bgm1, bgm1Path);
		ObjSound_SetVolumeRate(bgm1, 90);
		ObjSound_SetLoopEnable(bgm1, true);
		ObjSound_SetLoopTime(bgm1, 32.252, 200);
		ObjSound_SetRestartEnable(bgm1, true);
		ObjSound_Play(bgm1);
	}
	else if(num==2){
	    ObjSound_Load(bgm2, bgm2Path);
		ObjSound_SetVolumeRate(bgm2, 80);
		ObjSound_SetLoopEnable(bgm2, true);
		ObjSound_SetRestartEnable(bgm2, true);
		ObjSound_Play(bgm2);
	}
	else if(num==3){
	    ObjSound_Load(bgm3, bgm3Path);
		ObjSound_SetVolumeRate(bgm3, 90);
		ObjSound_SetLoopEnable(bgm3, false);
		ObjSound_SetRestartEnable(bgm3, true);
		ObjSound_Play(bgm3);
	}
	else{
	    ObjSound_Load(bgm4, bgm4Path);
		ObjSound_SetVolumeRate(bgm4, 90);
		ObjSound_SetLoopEnable(bgm4, true);
		ObjSound_SetLoopTime(bgm4, 37.67, 179.46);
		ObjSound_SetRestartEnable(bgm4, true);
		ObjSound_Play(bgm4);
	}
}



function SetStageDifficulty{
    let selection = 0;
    let sdiff = 0;
	
    let TextObjects = [];
    let TextNames = ["Easy[r](For absolute beginners)","Normal[r](For gamers who enjoy a good fight)", "Hard[r](For the brave souls out there)"];
	
    let diffs = [0, 1, 2];
    let Center = [192, 224];
    ascent(i in 0..length(TextNames)){
	    let objText = ObjText_Create();
		ObjText_SetText(objText, TextNames[i]);
        TextObjects = TextObjects ~ [objText];
        ObjText_SetHorizontalAlignment(objText, ALIGNMENT_CENTER);
        ObjRender_SetPosition(objText, Center[0] - 175, Center[1] + 15+ i*60, 0);
        ObjText_SetMaxWidth(objText, 350);
		ObjText_SetFontBold(objText, true);
        ObjText_SetFontSize(objText, 18);
        ObjText_SetFontType(objText, "Helvetica");
        //ObjText_SetFontBorderColor(objText, 16, 16, 16);
    }
	
    loop{
        if(GetVirtualKeyState(VK_UP) == KEY_PUSH){selection--;}
        if(GetVirtualKeyState(VK_DOWN) == KEY_PUSH){selection++;}
        if(selection < 0){selection = 2;}
        if(selection > 2){selection = 0;}
        ascent(i in 0..length(TextObjects)){
            if(selection == i){
                ObjText_SetFontColorTop(TextObjects[i], 250, 20, 0);
                ObjText_SetFontColorBottom(TextObjects[i], 250,20, 0);
            } else {
                ObjText_SetFontColorTop(TextObjects[i], 0, 20, 160);
                ObjText_SetFontColorBottom(TextObjects[i], 0, 20, 160);
            }
        }
        if(GetVirtualKeyState(VK_SHOT) == KEY_PUSH){
            sdiff = diffs[selection];
            SetCommonData("Difficulty", sdiff);
            break;
        }
	
        yield;
    }
    ascent(i in 0..length(TextObjects)){
        Obj_Delete(TextObjects[i]);
    }
}


task FadeOutBGM(music) {
	ascent(i in 0..120) {
		ObjSound_SetVolumeRate(music, (120-i) * 5/6);
		yield;
	}
	ObjSound_Stop(music);
}


 
@Event{
    alternative(GetEventType())
    case(EV_PAUSE_ENTER) {
		ObjSound_Stop(bgm1);
		ObjSound_Stop(bgm2);
		ObjSound_Stop(bgm3);
		ObjSound_Stop(bgm4);
	}
	case(EV_PAUSE_LEAVE) {
		ObjSound_Play(bgm1);
		ObjSound_Play(bgm2);
		ObjSound_Play(bgm3);
		ObjSound_Play(bgm4);
	}
	case(EV_USER_STAGE) {
		StartBGM(1);
	}
	case(EV_USER_STAGE + 1) {
	    FadeOutBGM(bgm1);
	}
	case(EV_USER_STAGE +2){
	    StartBGM(2);
	}
	case(EV_USER_STAGE + 3) {
	    FadeOutBGM(bgm2);
	}
	case(EV_USER_STAGE +4){
	    StartBGM(3);
	}
	case(EV_USER_STAGE +5){
	    FadeOutBGM(bgm3);
	}
	case(EV_USER_STAGE +6){
	    StartBGM(4);
	}
	case(EV_USER_STAGE +7){
	    FadeOutBGM(bgm4);
	}
}


@Initialize{
    TPlural;
	SetPlayerLife(5);
	SetPlayerSpell(3);
}


@MainLoop{
    yield;
}


task TPlural{
	SetCommonData("non1", "yes");
	let ps = [GetPlayerSpeed[0],GetPlayerSpeed[1]];
	SetPlayerSpeed(0,0);
	SetForbidPlayerShot(true);
	SetForbidPlayerSpell(true);
	wait(60);
    SetStageDifficulty;
	SetPlayerSpeed(ps[0],ps[1]);
	wait(60);
	let diff = "Easy"; 
	if(GetCommonData("Difficulty", 0)==1){diff = "Normal";} 
	if(GetCommonData("Difficulty", 0)==2){diff = "Hard";} 
	SetForbidPlayerShot(false);
	SetForbidPlayerSpell(false);
	
    let dir = GetCurrentScriptDirectory();
    let obj = ObjEnemyBossScene_Create();
	
	ObjEnemyBossScene_Add(obj, 0, dir ~ "./Singles/introDave.dnh");
	ObjEnemyBossScene_Add(obj, 0, dir ~ "./Singles/" ~diff~ "/firstNon.dnh");
	ObjEnemyBossScene_Add(obj, 0, dir ~ "./Singles/" ~diff~ "/Instability.dnh");
	
	ObjEnemyBossScene_Add(obj, 1, dir ~ "./Singles/" ~diff~ "/butterflySpiral.dnh");
	ObjEnemyBossScene_Add(obj, 1, dir ~ "./Singles/" ~diff~ "/Chaos rising.dnh");
	
	ObjEnemyBossScene_Add(obj, 2, dir ~ "./Singles/" ~diff~ "/randomAim.dnh");
	ObjEnemyBossScene_Add(obj, 2, dir ~ "./Singles/" ~diff~ "/firstNonRemas.dnh");
	
	ObjEnemyBossScene_Add(obj, 3, dir ~ "./Singles/intermission.dnh");
	ObjEnemyBossScene_Add(obj, 3, dir ~ "./Singles/changeBack.dnh");
	ObjEnemyBossScene_Add(obj, 3, dir ~ "./Singles/" ~diff~ "/RandomSpiral.dnh");
	ObjEnemyBossScene_Add(obj, 3, dir ~ "./Singles/" ~diff~ "/integral.dnh");
	
	ObjEnemyBossScene_Add(obj, 4, dir ~ "./Singles/" ~diff~ "/nonspell1.dnh");
	ObjEnemyBossScene_Add(obj, 4, dir ~ "./Singles/" ~diff~ "/timestop.dnh");
	
	ObjEnemyBossScene_Add(obj, 5, dir ~ "./Singles/" ~diff~ "/knives.dnh");
	ObjEnemyBossScene_Add(obj, 5, dir ~ "./Singles/" ~diff~ "/mouth.dnh");
	
	ObjEnemyBossScene_Add(obj, 6, dir ~ "./Singles/" ~diff~ "/hourglassNon.dnh");
	ObjEnemyBossScene_Add(obj, 6, dir ~ "./Singles/intermission2.dnh");
	ObjEnemyBossScene_Add(obj, 6, dir ~ "./Singles/" ~diff~ "/arrowRain.dnh");
	
	ObjEnemyBossScene_Add(obj, 7, dir ~ "./Singles/intermissionTogether.dnh");
	ObjEnemyBossScene_Add(obj, 7, dir ~ "./Singles/" ~diff~ "/nonTogether.dnh");
	ObjEnemyBossScene_Add(obj, 7, dir ~ "./Singles/" ~diff~ "/ToughLove.dnh");
	ObjEnemyBossScene_Add(obj, 7, dir ~ "./Singles/afterMario.dnh");
	ObjEnemyBossScene_Add(obj, 7, dir ~ "./Singles/" ~diff~ "/JamesSpell.dnh");
	ObjEnemyBossScene_Add(obj, 7, dir ~ "./Singles/afterJames.dnh");
	
	ObjEnemyBossScene_Add(obj, 8, dir ~ "./Singles/" ~diff~ "/hourglass.dnh");
	ObjEnemyBossScene_Add(obj, 8, dir ~ "./Singles/end.dnh");
	
	
	
	
	ObjEnemyBossScene_LoadInThread(obj);
    ObjEnemyBossScene_Regist(obj);
	while(!Obj_IsDeleted(obj)){yield;}
    CloseScript(GetOwnScriptID());
}

