//#TouhouDanmakufu[Stage]
#ScriptVersion[3]
#System["./default_system/Default_System.txt"]
#Title["Stage 3"]
#Text["Stage 3"]

#include "./img/All_Star_Shots/AllStarShot_Const.txt"
#include "./Sounds.txt"
#include "./Functions.txt"
#include "./Animation.txt"
#include "./Effects.txt"
#include "./EnnemyL.txt"

let bossObj;
let sceneObj = GetEnemyBossSceneObjectID;
let stagepart = 0;
let bgm;
let bgm2;
let Fini = 0;


@Initialize
{
	TStage();
	Mort;

	renderStage;
	scrollStage;
	Cristaux(0);
	Cristaux(120);
	Cristaux(240);
	
	bossObj = ObjEnemy_Create(OBJ_ENEMY);
	ObjMove_SetX(bossObj, GetCommonData("bossX", -32));
	ObjMove_SetY(bossObj, GetCommonData("bossY", -32));
	ObjEnemy_Regist(bossObj);
	
	DeleteShotAll(TYPE_ALL, TYPE_IMMEDIATE);
	SetCommonData("animStyle", "idle");
	
	
	renderBoss3;
	TFinalize;
	Stage;
}

@Event
{
alternative(GetEventType())
    case(EV_PAUSE_ENTER){
        if(stagepart == 0){
            ObjSound_Stop(bgm);
        } 
        if(stagepart == 1){
            ObjSound_Stop(bgm2);
        } 
    }
    case(EV_PAUSE_LEAVE){
        if(stagepart == 0){
            ObjSound_Play(bgm);
        } 
        if(stagepart == 1){
            ObjSound_Play(bgm2);
        } 
    }
}

@MainLoop
{
	SetCommonData("bossX", ObjMove_GetX(bossObj));
	SetCommonData("bossY", ObjMove_GetY(bossObj));
	yield;
}

task TStage{
    bgm = ObjSound_Create();
    ObjSound_Load(bgm, GetCurrentScriptDirectory() ~ "./se/Stage3.ogg");
    StageBGM(bgm, 1);
    ObjSound_Play(bgm);
	}

task Stage {
	//CreateStageEnemy1(vie,appX,appY,debutX,debutY,finX,finY,tempsdebut,tempsfin)
	wait(120);
	CreateStageEnemy10(600,-105,-300,-105,-120,-105,-400,120,240);
	CreateStageEnemy10(600,105,-300,105,-120,105,-400,120,240);
	wait(200);
	loop(8) {
	wait(30);
	CreateStageEnemy11(100,400,400,-125,-120,-125,-300,180,340);
	CreateStageEnemy11(100,440,400,-85,-120,-85,-300,180,340);
	CreateStageEnemy11(100,-400,400,125,-120,125,-300,180,340);
	CreateStageEnemy11(100,-440,400,85,-120,85,-300,180,340);
	}
	wait(120);
	CreateStageEnemy12(600,-105,-300,-150,-80,-105,-400,120,240);
	CreateStageEnemy12(600,105,-300,150,-80,105,-400,120,240);
	CreateStageEnemy12(600,-105,-300,-50,-80,-105,-400,120,240);
	CreateStageEnemy12(600,105,-300,50,-80,105,-400,120,240);
	wait(360);
	CreateStageEnemy12(600,-105,-300,-175,-80,-105,-400,120,240);
	CreateStageEnemy12(600,105,-300,175,-80,105,-400,120,240);
	CreateStageEnemy12(600,-105,-300,-75,-80,-105,-400,120,240);
	CreateStageEnemy12(600,105,-300,75,-80,105,-400,120,240);
	CreateStageEnemy13(1200,0,-300,0,-80,0,-400,120,240);
	wait(480);
	loop(8) {
	wait(30);
	CreateStageEnemy11(100,400,400,-125,-120,-125,-300,180,340);
	CreateStageEnemy11(100,440,400,-85,-120,-85,-300,180,340);
	CreateStageEnemy11(100,-400,400,125,-120,125,-300,180,340);
	CreateStageEnemy11(100,-440,400,85,-120,85,-300,180,340);
	}
	wait(30);
	CreateStageEnemy12(600,-105,-300,-150,-80,-105,-400,120,240);
	CreateStageEnemy12(600,105,-300,150,-80,105,-400,120,240);
	wait(30);
	CreateStageEnemy12(600,-105,-300,-50,-80,-105,-400,120,240);
	CreateStageEnemy12(600,105,-300,50,-80,105,-400,120,240);
	wait(240);
	loop(20) {
	wait(30);
	CreateStageEnemy14(100,-200,-150,300,-150,400,-150,120,0);
	CreateStageEnemy14(100,400,-150,-200,-150,-400,-150,120,0);
	}
	wait(360);
	ChangementDeDirection;
	let dir1 = GetCurrentScriptDirectory();

	let path1 = dir1 ~ "./Boss3Mid-Boss_BattleL.txt";
	let idScript1 = LoadScriptInThread(path1);
	loop(60){yield;}
	StartScript(idScript1);
	
	while(!IsCloseScript(idScript1) && GetPlayerState() != STATE_END){ yield;}
	wait(120);
	CreateStageEnemy10(600,-105,-300,-105,-120,-105,-400,120,240);
	CreateStageEnemy10(600,105,-300,105,-120,105,-400,120,240);
	wait(200);
	CreateStageEnemy12(600,-105,-300,-150,-80,-105,-400,120,240);
	CreateStageEnemy12(600,105,-300,150,-80,105,-400,120,240);
	CreateStageEnemy12(600,-105,-300,-50,-80,-105,-400,120,240);
	CreateStageEnemy12(600,105,-300,50,-80,105,-400,120,240);
	wait(360);
	CreateStageEnemy12(600,-105,-300,-175,-80,-105,-400,120,240);
	CreateStageEnemy12(600,105,-300,175,-80,105,-400,120,240);
	CreateStageEnemy12(600,-105,-300,-75,-80,-105,-400,120,240);
	CreateStageEnemy12(600,105,-300,75,-80,105,-400,120,240);
	CreateStageEnemy13(1200,0,-300,0,-80,0,-400,120,240);
	wait(540);
	let dir2 = GetCurrentScriptDirectory();

	let path2 = dir2 ~ "./Boss3_BattleL.txt";
	stagepart = 1;
    ObjSound_Stop(bgm);
	bgm2 = ObjSound_Create();
    ObjSound_Load(bgm2, GetCurrentScriptDirectory() ~ "./se/Boss3.ogg");
    StageBGM(bgm2, 1);
	ObjSound_Play(bgm2);
	let idScript2 = LoadScriptInThread(path2);
	loop(60){yield;}
	StartScript(idScript2);
	while(!IsCloseScript(idScript2) && GetPlayerState() != STATE_END){ yield;}
	ObjSound_Stop(bgm2);
	SetPlayerLife(-1);
}

task attendres(aaa) {
	while(!Obj_IsDeleted(aaa)) {
	ObjMove_SetAngularVelocity(aaa, -1);
	wait(20);
	ObjMove_SetAngularVelocity(aaa, 1);
	wait(20);
	}
}

function StageBGM(obj, ID){
    ObjSound_SetSoundDivision(obj, SOUND_BGM);
    ObjSound_SetRestartEnable(obj, true);
    ObjSound_SetLoopEnable(obj, true);
    ObjSound_SetLoopTime(obj, 0, 300);
    if(ID == 1){ObjSound_SetLoopTime(obj, 0, 60);}
    if(ID == 2){ObjSound_SetLoopTime(obj, 60, 192);}
    return obj;
}

task TFinalize
{
	loop {yield;}
	wait(5);
	SetCommonData("bossX", ObjMove_GetX(bossObj));
	SetCommonData("bossY", ObjMove_GetY(bossObj));
	Obj_Delete(bossObj);
	DeleteShotAll(TYPE_ALL, TYPE_ITEM);
	SetAutoDeleteObject(true);
	CloseScript(GetOwnScriptID());
	return;
}


//BACKGROUND

function Cristaux(aaa) {
	// background
	wait(aaa);
	let CSD = GetCurrentScriptDirectory;
	let texCristaux = CSD ~ "img/Cristaux.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,21); 
	ObjPrim_SetTexture(obj,texCristaux); 
	ObjSprite2D_SetSourceRect(obj,0,0,GetStgFrameWidth,560);
	ObjSprite2D_SetDestRect(obj,0,0,GetStgFrameWidth,560);
	scrollCristaux(obj);
	couleurCristaux(obj);
	deleteBackground(obj);
}

task couleurCristaux(aaa) {
	let scrollTex1 = 255;
	loop {
	while(scrollTex1>0) {
		ObjRender_SetAlpha(aaa, scrollTex1);
		scrollTex1 -= 1;
		yield;
	}
	while(scrollTex1<255) {
		ObjRender_SetAlpha(aaa, scrollTex1);
		scrollTex1 += 1;
		yield;
	}
	}
}

task scrollCristaux(aaa) {
	let scrollTex1 = 0;
	let scrollTex2 = 0;
	let scrollTex3 = 0;
	let scrollTex4 = 0;
	loop {
		ObjSprite2D_SetSourceRect(aaa,0+scrollTex1,0+scrollTex2,540+scrollTex3,870+scrollTex4);
		scrollTex2 -= 2;
		scrollTex4 -= 2;
		yield;
	}
}

let CSD = GetCurrentScriptDirectory;


let texRoche = CSD ~ "background/Roche.png";
let texChuteDeau = CSD ~ "background/ChuteDeau.png";
let texfogBG = CSD ~ "background/fog.png";


function makeFogBG(locX,locY,locZ,red,green,blue) {
	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(obj,texfogBG);
	Obj_SetRenderPriorityI(obj,20);
	ObjSprite3D_SetSourceDestRect(obj,0,0,4096,4096);
	ObjRender_SetScaleXYZ(obj,1,1,0);
	ObjRender_SetZWrite(obj,true);
	
	ObjRender_SetPosition(obj,locX,locY,locZ);
	deleteBackground(obj);
	
	ObjRender_SetColor(obj,red,green,blue);
	
	ObjSprite3D_SetBillboard(obj,true);
}


function makeRoche(locX,locY,locZ) {
	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(obj,texRoche);
	Obj_SetRenderPriorityI(obj,20);
	ObjSprite3D_SetSourceDestRect(obj,0,0,512,512);
	ObjRender_SetScaleXYZ(obj,1,1,0);
	ObjRender_SetZWrite(obj,true);
	
	ObjRender_SetAngleXYZ(obj,0,0,180);
	
	ObjRender_SetPosition(obj,locX,locY,locZ);
	deleteBackground(obj);
}

function makeRoche2(locX,locY,locZ) {
	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(obj,texRoche);
	Obj_SetRenderPriorityI(obj,20);
	ObjSprite3D_SetSourceDestRect(obj,0,0,512,512);
	ObjRender_SetScaleXYZ(obj,1,1,0);
	ObjRender_SetZWrite(obj,true);
	
	ObjRender_SetAngleXYZ(obj,180,0,0);
	
	ObjRender_SetPosition(obj,locX,locY,locZ);
	deleteBackground(obj);
}

function makeChuteDeau(locX,locY,locZ) {
	let bX = locX;
	let cY = locY;
	let dZ = locZ;
	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(obj,texChuteDeau);
	Obj_SetRenderPriorityI(obj,21);
	ObjSprite3D_SetSourceDestRect(obj,0,0,512,512);
	ObjRender_SetScaleXYZ(obj,1,1,0);
	ObjRender_SetZWrite(obj,true);
	
	ObjRender_SetAngleXYZ(obj,0,0,180);
	
	ObjRender_SetPosition(obj,locX,locY,locZ);
	deleteBackground(obj);
	chute(obj,bX,cY,dZ);
}

task deleteBackground(aaa) {
	while(Fini == 0) {
		yield;
	}
	Obj_Delete(aaa);
}

task renderStage {

	SetFogEnable(true);
	
	SetFogParam(1536,2048,230,195,130);

	makeFogBG(0,0,2560,255,255,255);
	
	makeChuteDeau(0,1536,10);
	makeChuteDeau(0,1024,10);
	makeChuteDeau(0,512,10);
	makeChuteDeau(0,0,10);
	makeChuteDeau(0,-512,10);
	
	makeRoche(0,1536,300);
	makeRoche(0,1024,300);
	makeRoche(0,512,300);
	makeRoche(0,0,300);
	makeRoche(0,-512,300);
	
	makeRoche2(-510,1536,300);
	makeRoche2(-510,1024,300);
	makeRoche2(-510,512,300);
	makeRoche2(-510,0,300);
	makeRoche2(-510,-512,300);
	
	makeRoche2(512,1536,300);
	makeRoche2(512,1024,300);
	makeRoche2(512,512,300);
	makeRoche2(512,0,300);
	makeRoche2(512,-512,300);
	
}

task chute(aaa,bX,cY,dZ) {
	let chut = 0;
	loop {
		ObjRender_SetPosition(aaa,bX,cY+chut,dZ);
		chut -=16;
		if(chut < -512) {
		chut = 0;
		}
		yield;
	}
}

task ChangementDeDirection {
	let direction = 12;
	loop(64) {
	SetCameraElevationAngle(direction);
	direction -= 0.5;
	wait(2);
	}
}


task scrollStage {
	let scroll = 0;
	let i = 0;
	let f = 0;

	SetCameraPerspectiveClip(16,4096);
	
	
	SetCameraAzimuthAngle(-90);
	SetCameraElevationAngle(12);
	SetCameraRadius(16);
	SetCameraFocusX(0);
	SetCameraFocusY(-512+scroll);
	SetCameraFocusZ(-600);
	
	
	loop {
		i = sin(f)*25;
		f+=0.5;
		scroll+=4;
		
		SetCameraRoll(0+i/3);
		SetCameraYaw(0+i/4);
		
		SetCameraFocusY(0+scroll);
		if(scroll > 512) {
		scroll = 0;
		}
		
		yield;
	}
}