#TouhouDanmakufu[Plural]
#ScriptVersion[3]
#Title["Saul"]
#System["./system/NSystem.dnh"]
#Player["./player/Reimu by plasmaflame22/Reimu_A.txt", "./player/Reimu by plasmaflame22/Reimu_C.txt"]
#Background["./system/Background.dnh"]
#include "./Singles/sharedfuncSaul.dnh"


let bgm0= ObjSound_Create(); let bgm0Path = GetCurrentScriptDirectory~"./bgm0.ogg";
let bgm1= ObjSound_Create(); let bgm1Path = GetCurrentScriptDirectory ~ "./bgm1.ogg";
let bgm2= ObjSound_Create(); let bgm2Path= GetCurrentScriptDirectory~ "./bgm2.ogg";



task StartBGM(num){
    if(num==0){
		ObjSound_Load(bgm0, bgm0Path);
		ObjSound_SetVolumeRate(bgm0, 90);
		ObjSound_SetRestartEnable(bgm0, true);
		ObjSound_Play(bgm0);
	}
    if(num==1){
		ObjSound_Load(bgm1, bgm1Path);
		ObjSound_SetVolumeRate(bgm1, 90);
		ObjSound_SetLoopEnable(bgm1, true);
		ObjSound_SetLoopTime(bgm1, 175.87, 10000);
		ObjSound_SetRestartEnable(bgm1, true);
		ObjSound_Play(bgm1);
	}
	if(num==2){
		ObjSound_Load(bgm2, bgm2Path);
		ObjSound_SetVolumeRate(bgm2, 100);
		ObjSound_SetLoopEnable(bgm2, true);
		ObjSound_SetLoopTime(bgm1, 0, 10000);
		ObjSound_SetRestartEnable(bgm2, true);
		ObjSound_Play(bgm2);
	}
}

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



function SetStageDifficulty{
    let selection = 0;
    let sdiff = 0;
	
    let TextObjects = [];
    let TextNames = ["Normal[r](For gamers who enjoy a good fight)", "Hard[r](For maniacs who like maths and bullet-hell)", "Mathsochist[r](Unreasonable..Why would you do this[r]to yourself?)"];
	
    let diffs = [0, 1, 2];
    let Center = [222, 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] - 120 + i*75, 0);
        ObjText_SetMaxWidth(objText, 350);
		ObjText_SetFontBold(objText, true);
        ObjText_SetFontSize(objText, 20);
        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], 220, 100, 0);
                ObjText_SetFontColorBottom(TextObjects[i], 220, 100, 0);
            } else {
                ObjText_SetFontColorTop(TextObjects[i], 220, 200, 220);
                ObjText_SetFontColorBottom(TextObjects[i], 220, 200, 220);
            }
        }
        if(GetVirtualKeyState(VK_SHOT) == KEY_PUSH){
            sdiff = diffs[selection];
            SetCommonData("Difficulty", sdiff);
            break;
        }
	
        yield;
    }
    ascent(i in 0..length(TextObjects)){
        Obj_Delete(TextObjects[i]);
    }
}

 
 
@Event{
    alternative(GetEventType())
    case(EV_PAUSE_ENTER) {
	    ObjSound_Stop(bgm0);
	    ObjSound_Stop(bgm1);
		ObjSound_Stop(bgm2);
	}
	case(EV_PAUSE_LEAVE) {
	    ObjSound_Play(bgm0);
	    ObjSound_Play(bgm1);
		ObjSound_Play(bgm2);
	}
	case(EV_USER_STAGE) {StartBGM(0);}
	case(EV_USER_STAGE + 1) {FadeOutBGM(bgm0, 0.9);}
	case(EV_USER_STAGE + 2) {StartBGM(1);}
	case(EV_USER_STAGE + 3) {FadeOutBGM(bgm1, 0.8);}
	case(EV_USER_STAGE + 4) {StartBGM(2);}
	case(EV_USER_STAGE + 5) {FadeOutBGM(bgm2, 0.85);}
}


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


@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 = "Normal"; 
	if(GetCommonData("Difficulty", 0)==1){diff = "Hard";} 
	if(GetCommonData("Difficulty", 0)==2){diff = "Mathsochist";}
	SetForbidPlayerShot(false);
	SetForbidPlayerSpell(false);
    let dir = GetCurrentScriptDirectory();
    let obj = ObjEnemyBossScene_Create();
	
	ObjEnemyBossScene_Add(obj, 0, dir ~ "./Singles/introSaul.dnh");
	ObjEnemyBossScene_Add(obj, 0, dir ~ "./Singles/" ~diff~ "/geyserNon1S.dnh");
	ObjEnemyBossScene_Add(obj, 0, dir ~ "./Singles/" ~diff~ "/quotientCylinder.dnh");
	
	ObjEnemyBossScene_Add(obj, 1, dir ~ "./Singles/" ~diff~ "/laserNon.dnh");
	ObjEnemyBossScene_Add(obj, 1, dir ~ "./Singles/" ~diff~ "/CWSpell.dnh");
	
	ObjEnemyBossScene_Add(obj, 2, dir ~ "./Singles/" ~diff~ "/wavySpiralNon.dnh");
	ObjEnemyBossScene_Add(obj, 2, dir ~ "./Singles/" ~diff~ "/sizeSpell.dnh");
	
	ObjEnemyBossScene_Add(obj, 3, dir ~ "./Singles/" ~diff~ "/geyserNon.dnh");
	ObjEnemyBossScene_Add(obj, 3, dir ~ "./Singles/" ~diff~ "/homotopySpell.dnh");
	
	ObjEnemyBossScene_Add(obj, 4, dir ~ "./Singles/" ~diff~ "/fallingNon.dnh");
	ObjEnemyBossScene_Add(obj, 4, dir ~ "./Singles/" ~diff~ "/helixNon.dnh");
	
	ObjEnemyBossScene_Add(obj, 5, dir ~ "./Singles/dialogueDave.dnh");
	ObjEnemyBossScene_Add(obj, 5, dir ~ "./Singles/transitionBG.dnh");
	ObjEnemyBossScene_Add(obj, 5, dir ~ "./Singles/" ~diff~ "/laneSpiralNon.dnh");
	ObjEnemyBossScene_Add(obj, 5, dir ~ "./Singles/DaveRescue.dnh");
	ObjEnemyBossScene_Add(obj, 5, dir ~ "./Singles/" ~diff~ "/DaveNon.dnh");
	ObjEnemyBossScene_Add(obj, 5, dir ~ "./Singles/" ~diff~ "/DaveSpell.dnh");
	ObjEnemyBossScene_Add(obj, 5, dir ~ "./Singles/SaulBack.dnh");
	
	ObjEnemyBossScene_Add(obj, 6, dir ~ "./Singles/" ~diff~ "/hypocycloidNon.dnh");
	ObjEnemyBossScene_Add(obj, 6, dir ~ "./Singles/" ~diff~ "/sizeSpell2.dnh");
	ObjEnemyBossScene_Add(obj, 6, dir ~ "./Singles/lastStand.dnh");
	
	ObjEnemyBossScene_Add(obj, 7, dir ~ "./Singles/" ~diff~ "/retractionSpell.dnh");
	ObjEnemyBossScene_Add(obj, 7, dir ~ "./Singles/" ~diff~ "/Credits.dnh");
	  
	
	
	
	ObjEnemyBossScene_LoadInThread(obj);
    ObjEnemyBossScene_Regist(obj);
	while(!Obj_IsDeleted(obj)){yield;}
    CloseScript(GetOwnScriptID());
}
