#東方弾幕風[Stage]
#ScriptVersion[3]
#Title["Dross Rotzank y los 7 patrones de danmaku"]
//Player["./player/player.txt"]
#Text["Perturbador."]
#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(15);
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);
		
	}
}


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(DrossGraphics)){
LoadTextureInLoadThread(DrossGraphics[i]);
yield;
 }



 

StartScript(LoadScriptInThread(GetCurrentScriptDirectory ~ "lib/Music.dnh"));
stags = 3;
SetAreaCommonData("HiScore", "isShowHiScore", 1);
Background_Stage3(1);
StageOver;
Difficulty(GetAreaCommonData("Config","Idioma",0),GetAreaCommonData("GAMEDATA","Dificultad",1));
//SongName(1,GetAreaCommonData("Config","Idioma",0));
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),14,101.26);


loop(300){yield;}
	

	loop(60){yield;}
	StartScript(LbossP1);
	while(!IsCloseScript(LbossP1))
	{
		yield;
	}
	StopMusic;
	ObjSound_Stop(bgm);
	
	PlayMusic(bgm,Mboss1,GetAreaCommonData("Config","BGMVolume",100),23,176.1);
	SongName(1,GetAreaCommonData("Config","Idioma",0));

	StartScript(Lboss1);
	while(!IsCloseScript(Lboss1))
	{
	if(GetCommonData("Oscuridad",false) == true){
	ObjSound_Stop(bgm);
		ObjSound_SetRestartEnable(bgm,true); 
		loop(30){yield;};
		ObjSound_Play(bgm);
		ObjSound_SetRestartEnable(bgm,false);
		SetCommonData("Oscuridad",false);
		}
	
		yield;
	}

	loop(120){yield;}
CallSwitch_OnGame(130,[100,50,16]);
 ascent(i in 0..length(St1_Graphics)){
RemoveTexture(St1_Graphics[i]);
yield;
 }
 
	
	loop(120){yield;}
    //ステージ終了
	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 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 = stg4bg2;
     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,-900,0);
     
    SetCameraRadius(600);
	ObjRender_SetColorHSV(SFobj,128,0,128);
     
    SetFogEnable(true);
    SetFogParam(1100,2100,1,1,1);
	
     
    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 = stg4bg1;
     
    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,GetStgFrameWidth,GetStgFrameHeight);
	ObjSprite2D_SetDestCenter(sbobj1);
    //ObjSprite2D_SetDestRect(sbobj1,-128,-128,128,128);
    Obj_SetRenderPriorityI(sbobj1,20);
    ObjRender_SetPosition(sbobj1,cx,cy-50,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 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_SetRenderPriority(fanobj,0.01);
    ObjRender_SetPosition(fanobj,530,30,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+43*(song-1),512,43*song);
    ObjSprite2D_SetDestCenter(fanobj);
    //ObjSprite2D_SetDestRect(fanobj,-256,-150,256,150);
    Obj_SetRenderPriorityI(fanobj,50);
    ObjRender_SetPosition(fanobj,GetStgFrameWidth/2-10,440,0);
    ObjRender_SetScaleXYZ(fanobj,0.7,0.7,0.7);
     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);
}