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

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

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


@Initialize
{
	TStage();
	Mort;

	renderStage;
	scrollStage;
	Cristaux(0);
	Cristaux(120);
	
	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");
	
	
	renderBoss1;
	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/Stage2.ogg");
    StageBGM(bgm, 1);
    ObjSound_Play(bgm);
	}

task Stage {
	//CreateStageEnemy1(vie,appX,appY,debutX,debutY,finX,finY,tempsdebut,tempsfin)
	wait(120);
	loop(8) {
	wait(30);
	CreateStageEnemy5(100,-125,-300,-125,-120,400,400,120,340);
	CreateStageEnemy5(100,-85,-300,-85,-120,440,400,120,340);
	}
	loop(8) {
	wait(30);
	CreateStageEnemy5(100,125,-300,125,-120,-400,400,120,340);
	CreateStageEnemy5(100,85,-300,85,-120,-440,400,120,340);
	}
	wait(120);
	CreateStageEnemy6(350,-105,-300,-105,-120,-105,-400,120,180);
	CreateStageEnemy6(350,105,-300,105,-120,105,-400,120,180);
	wait(60);
	loop(8) {
	wait(30);
	CreateStageEnemy5(100,-125,-300,-125,-120,400,400,120,340);
	CreateStageEnemy5(100,125,-300,125,-120,-400,400,120,340);
	}
	wait(120);
	loop(3) {
	wait(60);
	CreateStageEnemy7(300,-200,50,-80,50+rand(-50,50),-200,50,120,180);
	CreateStageEnemy7(300,200,50,80,50+rand(-50,50),200,50,120,180);
	}
	wait(120);
	loop(3) {
	wait(60);
	CreateStageEnemy7(300,-200,-50,-80,-50+rand(-50,50),-200,-50,120,180);
	CreateStageEnemy7(300,200,-50,80,-50+rand(-50,50),200,-50,120,180);
	}
	wait(240);
	CreateStageEnemy7(300,-200,-50,-80,-50,-200,-50,120,180);
	CreateStageEnemy7(300,200,-50,80,-50,200,-50,120,180);
	CreateStageEnemy7(300,-200,50,-80,50,-200,50,120,180);
	CreateStageEnemy7(300,200,50,80,50,200,50,120,180);
	wait(180);
	CreateStageEnemy6(350,-105,-300,-105,-120,-105,-400,120,180);
	CreateStageEnemy6(350,105,-300,105,-120,105,-400,120,180);
	wait(60);
	loop(8) {
	wait(30);
	CreateStageEnemy5(100,-125,-300,-125,-120,400,400,120,340);
	CreateStageEnemy5(100,125,-300,125,-120,-400,400,120,340);
	}
	wait(120);
	CreateStageEnemy8(350,-105,-300,-105,-120,-105,-400,120,140);
	CreateStageEnemy8(350,105,-300,105,-120,105,-400,120,140);
	CreateStageEnemy9(700,0,-300,0,-120,0,-400,120,180);
	wait(645+120);
	CreateStageEnemy8(350,-105,-300,-105,-120,-105,-400,120,140);
	CreateStageEnemy8(350,105,-300,105,-120,105,-400,120,140);
	CreateStageEnemy9(700,0,-300,0,-120,0,-400,120,180);
	wait(645+180);
	let dir1 = GetCurrentScriptDirectory();

	let path1 = dir1 ~ "./Boss2Mid-Boss_BattleE.txt";
	let idScript1 = LoadScriptInThread(path1);
	loop(60){yield;}
	StartScript(idScript1);
	
	while(!IsCloseScript(idScript1) && GetPlayerState() != STATE_END){ yield;}
	wait(30);
	CreateStageEnemy7(300,-200,-50,-80,-50,-200,-50,120,180);
	CreateStageEnemy7(300,200,-50,80,-50,200,-50,120,180);
	CreateStageEnemy7(300,-200,50,-80,50,-200,50,120,180);
	CreateStageEnemy7(300,200,50,80,50,200,50,120,180);
	wait(180);
	CreateStageEnemy6(350,-105,-300,-105,-120,-105,-400,120,180);
	CreateStageEnemy6(350,105,-300,105,-120,105,-400,120,180);
	wait(60);
	loop(8) {
	wait(30);
	CreateStageEnemy5(100,-125,-300,-125,-120,400,400,120,340);
	CreateStageEnemy5(100,125,-300,125,-120,-400,400,120,340);
	}
	wait(120);
	CreateStageEnemy8(350,-105,-300,-105,-120,-105,-400,120,140);
	CreateStageEnemy8(350,105,-300,105,-120,105,-400,120,140);
	CreateStageEnemy9(700,0,-300,0,-120,0,-400,120,180);
	wait(645+120);
	let dir2 = GetCurrentScriptDirectory();

	let path2 = dir2 ~ "./Boss2_BattleE.txt";
	stagepart = 1;
    ObjSound_Stop(bgm);
	bgm2 = ObjSound_Create();
    ObjSound_Load(bgm2, GetCurrentScriptDirectory() ~ "./se/Boss2.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);
	let dir3 = GetCurrentScriptDirectory();

	let path3 = dir3 ~ "./Boss3StageE.txt";
	let idScript3 = LoadScriptInThread(path3);
	loop(60){yield;}
	Fini = 1;
	wait(60);
	CloseScript(GetOwnScriptID());
	StartScript(idScript3);
}

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, 116);}
    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 texFloor = CSD ~ "background/Boss2parterreAutomne.png";
let texFloor2 = CSD ~ "background/Boss2TerreAutomne.png";
let texWall = CSD ~ "background/Boss2arbre.png";
let texFeuille = CSD ~ "background/feuille.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 to spawn a floor piece
function makeFloor1(locX,locY,locZ) {
	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(obj,texFloor);
	Obj_SetRenderPriorityI(obj,20);
	ObjSprite3D_SetSourceDestRect(obj,0,0,512,512);
	ObjRender_SetScaleXYZ(obj,1,1,0);
	ObjRender_SetZWrite(obj,true);
	
	ObjRender_SetAngleXYZ(obj,90,0,0);
	
	ObjRender_SetPosition(obj,locX,locY,locZ);
	deleteBackground(obj);
}

function makeFloor2(locX,locY,locZ) {
	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(obj,texFloor2);
	Obj_SetRenderPriorityI(obj,20);
	ObjSprite3D_SetSourceDestRect(obj,0,0,2048,2048);
	ObjRender_SetScaleXYZ(obj,1,1,0);
	ObjRender_SetZWrite(obj,true);
	
	ObjRender_SetAngleXYZ(obj,90,0,0);
	
	ObjRender_SetPosition(obj,locX,locY,locZ);
	deleteBackground(obj);
}


function makeWall1(locX,locY,locZ) {
	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(obj,texWall);
	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,40,180);
	
	ObjRender_SetPosition(obj,locX,locY,locZ);
	deleteBackground(obj);
}

function makeWall2(locX,locY,locZ) {
	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(obj,texWall);
	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,140,180);
	
	ObjRender_SetPosition(obj,locX,locY,locZ);
	deleteBackground(obj);
}

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

task renderStage {

	SetFogEnable(true);
	
	SetFogParam(1536,2048,70,30,30);

	makeFogBG(0,0,2560,255,255,255);
	
	makeFloor1(0,0,2560);
	makeFloor1(0,0,2048);
	makeFloor1(0,0,1536);
	makeFloor1(0,0,1024);
	makeFloor1(0,0,512);
	makeFloor1(0,0,0);
	
	makeFloor2(0,-1,2560);
	makeFloor2(0,-1,2048);
	makeFloor2(0,-1,1536);
	makeFloor2(0,-1,1024);
	makeFloor2(0,-1,512);
	makeFloor2(0,-1,0);
	
	makeWall2(-256,256,2560);
	makeWall2(-256,256,2048);
	makeWall2(-256,256,1536);
	makeWall2(-256,256,1024);
	makeWall2(-256,256,512);
	makeWall2(-256,256,0);
	
	makeWall1(256,256,2560);
	makeWall1(256,256,2048);
	makeWall1(256,256,1536);
	makeWall1(256,256,1024);
	makeWall1(256,256,512);
	makeWall1(256,256,0);
	
}

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

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

