#TouhouDanmakufu[Stage]
#ScriptVersion[3]
#Title["東方音楽花 ~ Mystical Elementic Rise (Stage)"]
//Player["./player/player.txt"]
#Text["By Gusano2314"]
#System["./system/Default_System.txt"]
#include "./lib/EV_const.dnh"
#include "./lib/Lib_Files.dnh"
#include"./lib/lib_switch.dnh"
let dir = GetCurrentScriptDirectory();

let bgm = ObjSound_Create;

let finished = false;
let stags = 0;
let Volumen = GetAreaCommonData("Config","BGMVolume", 100);
@Initialize
{
SetCommonData("stageID", GetOwnScriptID());

TStage();
SetPlayerLife(8);
SetPlayerSpell(3);

}

@MainLoop
{
	yield;
}

@Finalize
{
}
@Event{
alternative(GetEventType())
	case(EV_PAUSE_ENTER)
	{
		//ObjSound_Stop(bgm);
		ObjSound_Stop(bgm);
		ObjSound_SetRestartEnable(bgm,true);
			
	}
	case(EV_PAUSE_LEAVE)
	{
		ObjSound_Play(bgm);
		ObjSound_SetRestartEnable(bgm,false);
		
	}

    case(EV_USER+69)
    {
    StopMusic;
    ObjSound_Stop(bgm);
    
    PlayMusic(bgm,Mboss1,GetAreaCommonData("Config","BGMVolume",100),17.5,196.5);
    //SongName(3,GetAreaCommonData("Config","Idioma",0));
    if(GetAreaCommonData("Config","Idioma",1) == 0){SongName_V2("BGM. idk");}
    if(GetAreaCommonData("Config","Idioma",1) == 1){SongName_V2("BGM. Yaksha's Emotional Nature Disaster ~ Elemental Rise");}
        
    }

    case(EV_USER+70)
    {
    StopMusic;
    ObjSound_Stop(bgm);
        
    }

    case(EV_USER+71)
    {
    StopMusic;
    ObjSound_Stop(bgm);
        
        PlayMusic(bgm,Mlastspell,GetAreaCommonData("Config","BGMVolume",100),71.2,156.8);
    //SongName(3,GetAreaCommonData("Config","Idioma",0));
    if(GetAreaCommonData("Config","Idioma",1) == 0){SongName_V2("BGM. idk");}
    if(GetAreaCommonData("Config","Idioma",1) == 1){SongName_V2("BGM. World of Nature, Elemental Energy ~ Elementic Flowers");}
    }
}


task TStage
{
if(!IsReplay) {SaveCommonDataAreaToReplayFile("GAMEDATA");
}
else {
LoadCommonDataAreaFromReplayFile("GAMEDATA");
}

if(!IsReplay) {SaveCommonDataAreaToReplayFile("Config");
}
else {
LoadCommonDataAreaFromReplayFile("Config");
}

ascent(i in 0..length(Engine_Graphics)){
LoadTextureInLoadThread(Engine_Graphics[i]);
yield;
 }
 ascent(i in 0..length(St1_Graphics)){
LoadTextureInLoadThread(St1_Graphics[i]);
yield;
 }

ascent(i in 0..length(St6_Graphics)){
LoadTextureInLoadThread(St6_Graphics[i]);
yield;
 }
 
   ascent(i in 0..length(Lapros_Graphics)){
LoadTextureInLoadThread(Lapros_Graphics[i]);
yield;
 }
  ascent(i in 0..length(Gusano_Graphics)){
LoadTextureInLoadThread(Gusano_Graphics[i]);
yield;
 }

 loop(1){
    LoadScriptInThread(GetCurrentScriptDirectory~"#St1_Plural.txt");
    yield;
    LoadScriptInThread(GetCurrentScriptDirectory~"#St1.5_Plural.txt");
    yield;
 }

StartScript(LoadScriptInThread(GetCurrentScriptDirectory ~ "lib/Music.dnh"));
stags = 4;
SetAreaCommonData("HiScore", "isShowHiScore", 1);

Background_Stage4(1);
StageOver;
Difficulty(GetAreaCommonData("Config","Idioma",1),GetAreaCommonData("GAMEDATA","Dificultad",1));
//SongName(1,GetAreaCommonData("Config","Idioma",0));


if(GetAreaCommonData("Config","Idioma",1) == 0){
    SongName_V2("BGM. Teoría de las Criaturas del Mundo");
}
if(GetAreaCommonData("Config","Idioma",1) == 1){
    SongName_V2("BGM. Theory of the World's Creatures");
}

let boss1 = GetCurrentScriptDirectory~"#St1_Plural.txt";
let Lboss1 = LoadScriptInThread(boss1);

let bossP1 = GetCurrentScriptDirectory~"#St1.5_Plural.txt";
let LbossP1 = LoadScriptInThread(bossP1);


PlayMusic(bgm,Mprebattle1,GetAreaCommonData("Config","BGMVolume",100),1.6,81.6);

SetForbidPlayerShot(true);
loop(300){yield;}
	
// STAGE 1
	loop(60){yield;}
 /*   
	StartScript(LbossP1);
    CheckMusic;
	while(!IsCloseScript(LbossP1))
	{
		yield;
	}
   */ 
	

	StartScript(Lboss1);
	while(!IsCloseScript(Lboss1))
	{
        
		yield;
	}

	loop(120){yield;}


    loop(120){yield;}
    //ƒXƒe[ƒWI—¹
	finished = true;
	SetCommonData("Perfects",0);
	SetCommonData("Perfects_Bombs",0);
	ObjSound_Stop(bgm);

	Obj_Delete(bgm);
	
	CloseStgScene();
}

task Background_Stage1(Size){

 BackSt1_1(Size);
 BackSt1_2(Size);
 BackSt1_3(Size);
}
task Background_Stage2(Size){
 BackSt2_1(Size);

}
task Background_Stage3(Size){
 BackSt3_1(Size);
BackSt3_2;
}

task Background_Stage4(Size){
BackSt4_1(Size);
BackSt4_2;
}

task Background_Stage5(Size){
BackSt5_1(Size);
BackSt5_2(Size);
BackSt5_3(Size);

}

task StageOver{//CK Crash
    while(GetPlayerState() != STATE_END){yield;}
    loop(60){yield;}
    ObjSound_Stop(bgm);
	Obj_Delete(bgm);
    CloseStgScene();
}

task BackSt1_1(Size){
    let SFobj = ObjPrim_Create(OBJ_SPRITE_3D);
    let SFimg = st1bg1;
     let move = 0;
    LoadTexture(SFimg);
    ObjPrim_SetTexture(SFobj,SFimg);
    ObjSprite3D_SetSourceRect(SFobj,0,0,400*Size,800*Size);
    ObjSprite3D_SetDestRect(SFobj,-200*Size,-200*Size,200*Size,600*Size);
   ObjRender_SetScaleXYZ(SFobj,1,5,0);
    ObjRender_SetAngleXYZ(SFobj,-100,100,0);
     
    SetCameraRadius(600);
     
    //SetFogEnable(false);
    //SetFogParam(1100,1500,255,255,255);
     
    Obj_SetRenderPriorityI(SFobj,20);
while(stags ==1){
  ObjSprite3D_SetSourceRect(SFobj,0*Size,0*Size+move,400*Size,400*Size+move);
move += 5;
    yield;
    }
    Obj_Delete(SFobj);
}
task BackSt1_2(Size){
    let SFobj = ObjPrim_Create(OBJ_SPRITE_3D);
    let SFimg = st1bg2;
     let move = 0;
    LoadTexture(SFimg);
    ObjPrim_SetTexture(SFobj,SFimg);
    ObjSprite3D_SetSourceRect(SFobj,0,0,400*Size,800*Size);
    ObjSprite3D_SetDestRect(SFobj,-200*Size,-200*Size,200*Size,600*Size);
   ObjRender_SetScaleXYZ(SFobj,1,5,0);
    ObjRender_SetAngleXYZ(SFobj,-100,100,0);
  ObjRender_SetPosition(SFobj,0,0,400);
     
    //SetCameraRadius(600);
     
    //SetFogEnable(false);
    //SetFogParam(1100,1500,255,255,255);
     
    Obj_SetRenderPriorityI(SFobj,20);
while(stags ==1){
  ObjSprite3D_SetSourceRect(SFobj,0*Size,0*Size+move,400*Size,400*Size+move);
move += 5;
    yield;
    }
   Obj_Delete(SFobj); 
}
task BackSt1_3(Size){
    let SFobj = ObjPrim_Create(OBJ_SPRITE_3D);
    let SFimg = st1bg2;
     let move = 0;
    LoadTexture(SFimg);
    ObjPrim_SetTexture(SFobj,SFimg);
    ObjSprite3D_SetSourceRect(SFobj,0,0,400*Size,800*Size);
    ObjSprite3D_SetDestRect(SFobj,-200*Size,-200*Size,200*Size,600*Size);
   ObjRender_SetScaleXYZ(SFobj,1.5,5,0);
    ObjRender_SetAngleXYZ(SFobj,-100,100,0);
  ObjRender_SetPosition(SFobj,0,0,-525);
     
    //SetCameraRadius(600);
     
    //SetFogEnable(false);
    //SetFogParam(1100,1500,255,255,255);
     
    Obj_SetRenderPriorityI(SFobj,20);
while(stags ==1){
  ObjSprite3D_SetSourceRect(SFobj,0*Size,0*Size+move,400*Size,400*Size+move);
move += 5;
    yield;
    }
   Obj_Delete(SFobj);  
}

task BackSt2_1(Size){
    let SFobj = ObjPrim_Create(OBJ_SPRITE_3D);
    let SFimg = st2bg3;
     let move = 0;
    LoadTexture(SFimg);
    ObjPrim_SetTexture(SFobj,SFimg);
    ObjSprite3D_SetSourceRect(SFobj,0,0,2048*Size,300*Size);
    ObjSprite3D_SetDestRect(SFobj,-1024*Size,-1200*Size,1024*Size,1200*Size);
   ObjRender_SetScaleXYZ(SFobj,0.8,1.1,0);
    ObjRender_SetAngleXYZ(SFobj,-100,100,-30);
     
    SetCameraRadius(600);
	
     
    //SetFogEnable(false);
    //SetFogParam(1100,1500,255,255,255);
     
    Obj_SetRenderPriorityI(SFobj,20);
while(stags ==2){
  ObjSprite3D_SetSourceRect(SFobj,0*Size,0*Size+move,2048*Size,2400*Size+move);
move += 3;
    yield;
    }
    Obj_Delete(SFobj);
}


task BackSt3_1(Size){
    let SFobj = ObjPrim_Create(OBJ_SPRITE_3D);
    let SFimg = st3bg1;
     let move = 0;
    LoadTexture(SFimg);
    ObjPrim_SetTexture(SFobj,SFimg);
    ObjSprite3D_SetSourceRect(SFobj,0,0,2048*Size,2700*Size);
    ObjSprite3D_SetDestRect(SFobj,-1024*Size,-1200*Size,1024*Size,1200*Size);
   ObjRender_SetScaleXYZ(SFobj,0.8,1.1,0);
    ObjRender_SetAngleXYZ(SFobj,-130,75,0);
	ObjRender_SetPosition(SFobj,0,-80,0);
     
    SetCameraRadius(600);
	
     
    SetFogEnable(true);
    SetFogParam(1100,1500,2,2,2);
     
    Obj_SetRenderPriorityI(SFobj,21);
while(stags ==3){
  ObjSprite3D_SetSourceRect(SFobj,0*Size,0*Size+move,2048*Size,2400*Size+move);
move += 3;
    yield;
    }
    Obj_Delete(SFobj);
}


    task BackSt3_2(){
    let sbobj1 = ObjPrim_Create(OBJ_SPRITE_2D);
    let sbimg = st3bg2;
     
    let sbalpha = 255;
    let sbspin = 0;
	let cx = GetStgFrameWidth/2;
let cy = GetStgFrameHeight/2;
     
    LoadTexture(sbimg);
    ObjPrim_SetTexture(sbobj1,sbimg);
    ObjSprite2D_SetSourceRect(sbobj1,0,0,550,412);
	ObjSprite2D_SetDestCenter(sbobj1);
    //ObjSprite2D_SetDestRect(sbobj1,-128,-128,128,128);
    Obj_SetRenderPriorityI(sbobj1,20);
    ObjRender_SetPosition(sbobj1,cx,cy-150,0);
    ObjRender_SetScaleXYZ(sbobj1,1,1,1);
     
    while(stags == 3){
     
    ObjRender_SetAlpha(sbobj1,sbalpha);
     
    ObjRender_SetAngleXYZ(sbobj1,0,0,sbspin);
     
    //sbspin-=0.5;
     
    //if(sbalpha<200){sbalpha+=2;}
    //if(sbalpha>200){sbalpha=200;}
     
    yield;
    }
	Obj_Delete(sbobj1);
	}
	
task BackSt4_1(Size){
    let SFobj = ObjPrim_Create(OBJ_SPRITE_3D);
    let SFimg = stg6bg2;
     let move = 0;
    LoadTexture(SFimg);
    ObjPrim_SetTexture(SFobj,SFimg);
    ObjSprite3D_SetSourceRect(SFobj,0,0,2048*Size,2700*Size);
    ObjSprite3D_SetDestRect(SFobj,-1024*Size,-1200*Size,1024*Size,1200*Size);
   ObjRender_SetScaleXYZ(SFobj,0.8,1.1,0);
    ObjRender_SetAngleXYZ(SFobj,-130,75,0);
	ObjRender_SetPosition(SFobj,0,-500,0);
     
    SetCameraRadius(600);
	//ObjRender_SetColorHSV(SFobj,128,0,128);
     
    SetFogEnable(true);
    SetFogParam(1100,2100,1,1,50);
	
     
    Obj_SetRenderPriorityI(SFobj,21);
while(stags ==4){
  ObjSprite3D_SetSourceRect(SFobj,0*Size,0*Size+move,2048*Size,2400*Size+move);
move += 3;
    yield;
    }
    Obj_Delete(SFobj);
}


    task BackSt4_2(){
    let sbobj1 = ObjPrim_Create(OBJ_SPRITE_2D);
    let sbimg = stg6bg1;
     
    let sbalpha = 255;
    let sbspin = 0;
	let cx = GetStgFrameWidth/2;
let cy = GetStgFrameHeight/2;
     
    LoadTexture(sbimg);
    ObjPrim_SetTexture(sbobj1,sbimg);
    ObjSprite2D_SetSourceRect(sbobj1,0,0,1024,512);
	ObjSprite2D_SetDestCenter(sbobj1);
    //ObjSprite2D_SetDestRect(sbobj1,-128,-128,128,128);
    Obj_SetRenderPriorityI(sbobj1,20);
    ObjRender_SetPosition(sbobj1,cx+190,cy+30,0);
    ObjRender_SetScaleXYZ(sbobj1,1,1,1);
     
    while(stags == 4){
     
    ObjRender_SetAlpha(sbobj1,sbalpha);
     
    ObjRender_SetAngleXYZ(sbobj1,0,0,sbspin);
     
    //sbspin-=0.5;
     
    //if(sbalpha<200){sbalpha+=2;}
    //if(sbalpha>200){sbalpha=200;}
     
    yield;
    }
	Obj_Delete(sbobj1);
	}	
	

task BackSt5_1(Size){
    let SFobj = ObjPrim_Create(OBJ_SPRITE_3D);
    let SFimg = stg5bg1;
     let move = 0;
     let toja = 0;
    LoadTexture(SFimg);
    ObjPrim_SetTexture(SFobj,SFimg);
    ObjSprite3D_SetSourceRect(SFobj,0,0,2048*Size,2700*Size);
    ObjSprite3D_SetDestRect(SFobj,-1024*Size,-1200*Size,1024*Size,1200*Size);
   ObjRender_SetScaleXYZ(SFobj,0.8,1.1,0);
    ObjRender_SetAngleXYZ(SFobj,0,0,0);
    ObjRender_SetPosition(SFobj,0,0,0);
     
    //SetCameraRadius(600);
    ObjRender_SetColorHSV(SFobj,128,0,128);
     
    SetCameraFocusX(0);
    SetCameraFocusY(0);
    SetCameraFocusZ(0);
    SetCameraRadius(-150);
    SetCameraElevationAngle(25);
    SetCameraAzimuthAngle(90);

    SetCameraPitch(-50);
    SetFogEnable(true);
    SetFogParam(1100,1600,1,1,1);
    
     
    Obj_SetRenderPriorityI(SFobj,21);
while(stags ==5){
  ObjSprite3D_SetSourceRect(SFobj,0*Size,0*Size+move,2048*Size,2400*Size+move);
  SetCameraRoll(0+sin(toja)*20);
  toja+=0.5;
move += 3;
    yield;
    }
    Obj_Delete(SFobj);
}



task BackSt5_2(Size){
    let SFobj = ObjPrim_Create(OBJ_SPRITE_3D);
    let SFimg = stg5bg2;
     let move = 0;
    LoadTexture(SFimg);
    ObjPrim_SetTexture(SFobj,SFimg);
    ObjSprite3D_SetSourceRect(SFobj,0,0,2048*Size,2700*Size);
    ObjSprite3D_SetDestRect(SFobj,-1200*Size,-1200*Size,1200*Size,1200*Size);
   ObjRender_SetScaleXYZ(SFobj,0.8,1.1,0);
    ObjRender_SetAngleXYZ(SFobj,0,90,90);
    ObjRender_SetPosition(SFobj,180,600,-1300);
     //SetCameraFocusXYZ(300,100 ,0 );
    

    ObjRender_SetColorHSV(SFobj,0,8,48);
     

    
     
    Obj_SetRenderPriorityI(SFobj,21);
while(stags ==5){

  ObjSprite3D_SetSourceRect(SFobj,0*Size+move,0*Size,2400*Size+move,2400*Size);
move += 4.5;
    yield;
    }
    Obj_Delete(SFobj);
}

task BackSt5_3(Size){
    let SFobj = ObjPrim_Create(OBJ_SPRITE_3D);
    let SFimg = stg5bg2;
     let move = 0;
    LoadTexture(SFimg);
    ObjPrim_SetTexture(SFobj,SFimg);
    ObjSprite3D_SetSourceRect(SFobj,0,0,2048*Size,2700*Size);
    ObjSprite3D_SetDestRect(SFobj,-1200*Size,-1200*Size,1200*Size,1200*Size);
   ObjRender_SetScaleXYZ(SFobj,0.8,1.1,0);
    ObjRender_SetAngleXYZ(SFobj,0,90,90);
    ObjRender_SetPosition(SFobj,-180,600,-1300);
     //SetCameraFocusXYZ(300,100 ,0 );

    ObjRender_SetColorHSV(SFobj,0,8,48);
     
    
     
    Obj_SetRenderPriorityI(SFobj,21);
while(stags ==5){

  ObjSprite3D_SetSourceRect(SFobj,0*Size+move,0*Size,2400*Size+move,2400*Size);
move += 4.5;
    yield;
    }
    Obj_Delete(SFobj);
}

	
	
task PlayMusic(obj,str,v,loopstart,loopend){
let mus;


	ObjSound_Load(obj,str);
	ObjSound_SetSoundDivision(obj,SOUND_BGM);
	ObjSound_SetVolumeRate(obj,v);
	ObjSound_SetLoopEnable(obj,true);
	ObjSound_SetLoopTime(obj,loopstart,loopend);
	ObjSound_SetRestartEnable(obj,false);
	ObjSound_Play(obj);
}


task Difficulty(lang,dif){
let Dimg;
let fanobj = ObjPrim_Create(OBJ_SPRITE_2D);
if(lang == 0){
    Dimg = GetCurrentScriptDirectory ~ "img/DifficultyEs.png";
	}
	else{
	Dimg = GetCurrentScriptDirectory ~ "img/DifficultyEn.png";
	}
     
    let sbalpha = 0;
     let fangle = 90;
    LoadTexture(Dimg);
    ObjPrim_SetTexture(fanobj,Dimg);
	if(dif == 1){
    ObjSprite2D_SetSourceRect(fanobj,0,0,256,62);
	}
	else if (dif == 2){
	ObjSprite2D_SetSourceRect(fanobj,0,64,256,120);
	}
	else if (dif == 3){
	ObjSprite2D_SetSourceRect(fanobj,0,120,256,178);
	}
	else if (dif == 4){
	ObjSprite2D_SetSourceRect(fanobj,0,180,255,236);
	}
    ObjSprite2D_SetDestCenter(fanobj);
    //ObjSprite2D_SetDestRect(fanobj,-256,-150,256,150);
    Obj_SetRenderPriorityI(fanobj,81);
    ObjRender_SetPosition(fanobj,530,20,0);
    ObjRender_SetScaleXYZ(fanobj,0.4,0.4,0.4);
     ObjRender_SetAlpha(fanobj,255);
    


}

task SongName(song,lang){
let Dimg;
let fanobj = ObjPrim_Create(OBJ_SPRITE_2D);
let alph = 0;
if(lang == 0){
    Dimg = GetCurrentScriptDirectory ~ "img/NBBGM_Es.png";
	}
	else{
	Dimg = GetCurrentScriptDirectory ~ "img/NBBGM_En.png";
	}
	LoadTexture(Dimg);
    ObjPrim_SetTexture(fanobj,Dimg);
    ObjSprite2D_SetSourceRect(fanobj,0,0+18*(song-1),1024,18*song);
    ObjSprite2D_SetDestCenter(fanobj);
    //ObjSprite2D_SetDestRect(fanobj,-256,-150,256,150);
    Obj_SetRenderPriorityI(fanobj,50);
    ObjRender_SetPosition(fanobj,GetStgFrameWidth/2+65,440,0);
    ObjRender_SetScaleXYZ(fanobj,0.5,0.5,0.5);
     ObjRender_SetAlpha(fanobj,0);
	 
	 loop(30){
	 ObjRender_SetAlpha(fanobj,alph);
	 alph+= 255/30;
	 yield;
	 }
	 
	 loop(180){yield;}
	 
	 loop(30){
	 ObjRender_SetAlpha(fanobj,alph);
	 alph-= 255/30;
	 yield;
	 }
	Obj_Delete(fanobj);
}

task SongName_V2(name){
    let alph = 0;
    let objTitleText = ObjText_Create();
ObjText_SetFontType(objTitleText,"AR ESSENCE");
    ObjText_SetText(objTitleText, name);
    ObjText_SetFontSize(objTitleText, 18);
    ObjText_SetFontBold(objTitleText, false);
    ObjText_SetFontColorTop(objTitleText, 255, 255, 255);
    ObjText_SetFontColorBottom(objTitleText, 255, 255, 255);
    ObjText_SetFontBorderType(objTitleText, BORDER_FULL);
    ObjText_SetFontBorderColor(objTitleText,0, 0, 0);
    ObjText_SetFontBorderWidth(objTitleText, 1);
    Obj_SetRenderPriorityI(objTitleText, 70);
    ObjText_SetSidePitch(objTitleText,-1.8 );
    ObjRender_SetX(objTitleText, GetStgFrameWidth/2-190);
    ObjRender_SetY(objTitleText, 430);
    ObjRender_SetAlpha(objTitleText,0);
     
     loop(30){
     ObjRender_SetAlpha(objTitleText,alph);
     alph+= 255/30;
     yield;
     }
     
     loop(180){yield;}
     
     loop(30){
     ObjRender_SetAlpha(objTitleText,alph);
     alph-= 255/30;
     yield;
     }
    Obj_Delete(objTitleText);
}

task CheckMusic{
    
while(GetAreaCommonData("Music","Change Music" ,0 ) ==0) {yield;}

    
    StopMusic;
    ObjSound_Stop(bgm);
    
    PlayMusic(bgm,Mboss1,GetAreaCommonData("Config","BGMVolume",100),12.97,192);
    //SongName(3,GetAreaCommonData("Config","Idioma",0));
    if(GetAreaCommonData("Config","Idioma",1) == 0){SongName_V2("BGM. idk");}
    if(GetAreaCommonData("Config","Idioma",1) == 1){SongName_V2("BGM. Flowering Child of the Eden ~ Dangerous Yaksha (temporal name)");}

}