let CSD = GetCurrentScriptDirectory;


let texFog = CSD ~ "sprite/fog.png";


let texFloor = CSD ~ "sprite/grass.png";

let texCloud = CSD ~ "sprite/clouds.png";

let texTree = CSD ~ "sprite/tree1.png";

let texTree2 = CSD ~ "sprite/tree2.png";

let texTree3 = CSD ~ "sprite/tree3.png";

let texTree4 = CSD ~ "sprite/tree4.png";

let texDark = CSD ~ "sprite/darkness.png";



//--------------------------------------------
//function to spawn a floor piece
function makeFloor(locX,locY,locZ) {
        let obj = ObjPrim_Create(OBJ_SPRITE_3D);
        ObjPrim_SetTexture(obj,texFloor);
        Obj_SetRenderPriorityI(obj,20);
        ObjSprite3D_SetSourceDestRect(obj,0,0,800,778);
        ObjRender_SetScaleXYZ(obj,1,1,0);
        ObjRender_SetZWrite (obj, true); 

       ObjRender_SetAngleXYZ(obj,90,0,0);
       ObjRender_SetPosition(obj,locX,locY,locZ);
	  
}


function makeCloud(locX,locY,locZ) {
        let obj = ObjPrim_Create(OBJ_SPRITE_3D);
        ObjPrim_SetTexture(obj,texCloud);
        Obj_SetRenderPriorityI(obj,20);
        ObjSprite3D_SetSourceDestRect(obj,0,0,300,220);
        ObjRender_SetScaleXYZ(obj,1,1,0);
		ObjRender_SetAlpha(obj,200);
        ObjRender_SetZWrite (obj, true); 

       ObjRender_SetAngleXYZ(obj,90,0,0);
       ObjRender_SetPosition(obj,locX,locY,locZ);
	  
}


function makeTree(locX,locY,locZ) {
        let obj = ObjPrim_Create(OBJ_SPRITE_3D);
        ObjPrim_SetTexture(obj,texTree);
        Obj_SetRenderPriorityI(obj,20);
        ObjSprite3D_SetSourceDestRect(obj,0,0,200,184);
        ObjRender_SetScaleXYZ(obj,1,1,0);
        ObjRender_SetZWrite (obj, true); 

       ObjRender_SetAngleXYZ(obj,0,0,0);
       ObjRender_SetPosition(obj,locX,locY,locZ);
}

function makeTree2(locX,locY,locZ) {
        let obj = ObjPrim_Create(OBJ_SPRITE_3D);
        ObjPrim_SetTexture(obj,texTree2);
        Obj_SetRenderPriorityI(obj,20);
        ObjSprite3D_SetSourceDestRect(obj,0,0,200,222);
        ObjRender_SetScaleXYZ(obj,1,1,0);
        ObjRender_SetZWrite (obj, true); 

       ObjRender_SetAngleXYZ(obj,0,0,0);
       ObjRender_SetPosition(obj,locX,locY,locZ);
}

function makeTree4(locX,locY,locZ) {
        let obj = ObjPrim_Create(OBJ_SPRITE_3D);
        ObjPrim_SetTexture(obj,texTree4);
        Obj_SetRenderPriorityI(obj,20);
        ObjSprite3D_SetSourceDestRect(obj,0,0,200,272);
        ObjRender_SetScaleXYZ(obj,1,1,0);
        ObjRender_SetZWrite (obj, true); 

       ObjRender_SetAngleXYZ(obj,0,0,0);
       ObjRender_SetPosition(obj,locX,locY,locZ);
}

function makeTree3(locX,locY,locZ) {
        let obj = ObjPrim_Create(OBJ_SPRITE_3D);
        ObjPrim_SetTexture(obj,texTree3);
        Obj_SetRenderPriorityI(obj,20);
        ObjSprite3D_SetSourceDestRect(obj,0,0,200,222);
        ObjRender_SetScaleXYZ(obj,1,1,0);
        ObjRender_SetZWrite (obj, true); 

       ObjRender_SetAngleXYZ(obj,0,0,0);
       ObjRender_SetPosition(obj,locX,locY,locZ);
}

function makeFog(locX,locY,locZ,red,green,blue) {
        let obj = ObjPrim_Create(OBJ_SPRITE_3D);
        ObjPrim_SetTexture(obj,texFog);
        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);


    ObjSprite3D_SetBillboard(obj, true); //makes this object face camera no matter where the camera is facing 
}

function makeDarkness(locX,locY,locZ) {
        let obj = ObjPrim_Create(OBJ_SPRITE_3D);
        ObjPrim_SetTexture(obj,texDark);
        Obj_SetRenderPriorityI(obj,20);
		ObjRender_SetAlpha(obj,170);
        ObjSprite3D_SetSourceDestRect(obj,0,0,20,20);
        ObjRender_SetScaleXYZ(obj,1000,1000,0);
        ObjRender_SetZWrite (obj, true); 

       
       ObjRender_SetPosition(obj,locX,locY,locZ);
	   
	   ObjSprite3D_SetBillboard(obj, true); //makes this object face camera no matter where the camera is facing 
}


//--------------------------------
task renderStage {

//enable the fog

SetFogEnable(true);

//set begining and end of fog and color
SetFogParam(1580,1836,0,0,0);

makeFog(0,0,2048,344,255,255);

makeFloor(0,10,2000);
makeFloor(0,10,1435);
makeFloor(0,10,720);
makeFloor(0,10,0);

makeFloor(800,10,2000);
makeFloor(800,10,1480);
makeFloor(800,10,720);
makeFloor(800,10,0);


makeCloud(0,400,2000);
makeCloud(0,400,1480);
makeCloud(0,400,720);
makeCloud(0,400,0);

makeCloud(-400,400,2000);
makeCloud(-400,400,1480);
makeCloud(-400,400,720);
makeCloud(-400,400,0);

makeCloud(400,400,2000);
makeCloud(400,400,1480);
makeCloud(400,400,720);
makeCloud(400,400,0);


makeFloor(-800,10,2000);
makeFloor(-800,10,1480);
makeFloor(-800,10,720);
makeFloor(-800,10,0);

makeTree(20,66,100);
makeTree(-100,66,-20);
makeTree(70,66,-100);

makeTree(20,66,820);
makeTree(-100,66,700);
makeTree(70,66,620);

makeTree2(130,86,980);
makeTree2(-70,86,480);

makeTree2(130,86,260);
makeTree2(-70,86,-236);

makeTree(20,66,1540);
makeTree(-100,66,1420);
makeTree(70,66,1340);

makeTree2(130,86,1680);
makeTree2(-70,86,1200);

makeTree3(-300,100,900);
makeTree3(390,100,1220);

makeTree3(-300,100,1620);
makeTree3(390,66,-200);



makeTree3(-300,100,180);
makeTree3(390,100,500);

makeTree4(-80,106,820+300);
makeTree4(-200,106,700+100);
makeTree4(330,106,620+200);

makeTree4(-80,106,100+300);
makeTree4(-200,106,-20+100);
makeTree4(330,106,-100+200);

makeTree4(-80,106,1540+300);
makeTree4(-200,106,1420+100);
makeTree4(330,106,1340+200);


makeTree(400,66,1520);
makeTree(-500,66,1400);
makeTree(580,66,1320);

makeTree(400,66,820);
makeTree(-500,66,700);
makeTree(580,66,620);



makeTree(400,66,100);
makeTree(-500,66,-20);
makeTree(580,66,-100);

makeTree2(-400,86,1000);
makeTree2(500,86,400);

makeTree2(-400,86,270);
makeTree2(500,86,-370);

makeTree2(-400,86,1800);
makeTree2(500,86,1120);



makeDarkness(0,0,-500);



//------






}

task scrollStage{


 let i = 0;
 let f = 0;
 let scroll = 0;


  //set the minimum and maximum render distance for your camera
    SetCameraPerspectiveClip(16,4096);


 //azimuth camera
    SetCameraAzimuthAngle (-90);
    SetCameraElevationAngle (0);
    SetCameraRadius(64);

 //focus camera
     SetCameraFocusX(0);                //left or right
    SetCameraFocusY(820);               //up or down
    SetCameraFocusZ(-512+scroll);         //depth forward and back 

 // ? camera    
     SetCameraYaw(0);
     SetCameraPitch(35);
     SetCameraRoll(0);   



    loop {

       i = sin(f)*25;
       f+=0.5;
       scroll+=4;
      
       


       SetCameraFocusZ(-680+scroll); 
       if(scroll > 725) {
          scroll = 0;

        }
        scroll+=2; //speed of the counting

        yield; 
   }
   }
