let stagen;

task Stage1BG(){
    stagen = 1;

    let cmr =0;
    SetCameraPerspectiveClip(16,8192);
    SetCameraAzimuthAngle(-90);
    SetCameraElevationAngle(47);
    SetCameraRadius(1);
    SetCameraFocusX(0);
    SetCameraFocusY(512);
    SetCameraFocusZ(-512+cmr);

    let bg1 = GetCurrentScriptDirectory() ~ "img/bg1.png";
    let bg2 = GetCurrentScriptDirectory() ~ "img/bg2.png";
    let bg3 = GetCurrentScriptDirectory() ~ "img/bg3.png";
    LoadTexture(bg1);
    LoadTexture(bg2);
    LoadTexture(bg3);

    function imgdef3D(path, prio, blendtype, scale, angle, alpha, y){
        let img = ObjPrim_Create(OBJ_SPRITE_3D);
        ObjPrim_SetTexture(img, path);
        Obj_SetRenderPriorityI(img, prio);
        ObjRender_SetBlendType(img, blendtype);
        ObjRender_SetScaleXYZ(img,scale, scale,0);
        ObjRender_SetAngleXYZ(img,angle,0,0);
        ObjRender_SetZWrite(img,true);  
        ObjRender_SetAlpha(img, alpha);
        ObjRender_SetPosition(img,0,y,0);
        noninvisible(img);
       
        return img;
    }

    let angle = 77;
    let y = 100;

    let obj2 = imgdef3D(bg2, 21, BLEND_ALPHA, 0.7, angle, 170, y);
    let obj = imgdef3D(bg1, 21, BLEND_ALPHA, 0.7, angle, 175, y+2);
    createfog;
    
    task createfog{
        let x = 700;
        let y = -300;
        let posneg = -1;
        let c = 1500;
        let xp = -0.6;
        let yp = 0.7;
        let cm = 100;
        let alpha = 255;
        let t = 60*14;
        let am = alpha/t;
        loop(c/cm){
            let obj3 = imgdef2D(x + c*xp,y + c*yp, 3, c);
            c-= cm;
            x += 250 *posneg;
            y += 100 * posneg;
            posneg *= -1;
        }
        loop{
            let obj3 = imgdef2D(x,y, 3, 0);
            x += 250 *posneg;
            y += 100 * posneg;
            posneg *= -1;
            wait(cm);
        }

        function imgdef2D(x,y, scale, c){
            let img1 = ObjPrim_Create(OBJ_SPRITE_2D);
            ObjPrim_SetTexture(img1, bg3);
            ObjRender_SetBlendType(img1, BLEND_ALPHA);
            ObjSprite2D_SetSourceRect(img1, 0, 0, 256, 256);
            ObjSprite2D_SetDestCenter(img1);
            Obj_SetRenderPriorityI(img1, 22);
            ObjRender_SetScaleXYZ(img1, scale, scale, 1);
            
            ObjRender_SetPosition(img1, x, y, 1);
            moveimg(img1, x, y, c);
            noninvisible(img1);
            return img1;

            task moveimg(img1, x, y, c){
                
                
                let t2 = t-c;
                let calpha = alpha - c *am;
                ObjRender_SetAlpha(img1, calpha-c*am);
                loop(t2){
                    ObjRender_SetPosition(img1, x, y, 1);
                    ObjRender_SetAlpha(img1, calpha);
                    calpha -= am;
                    x += xp;
                    y += yp;
                    yield;
                }
                Obj_Delete(img1);
            }
        }


    }


    let alpha = 255;
    let c = 0;
    let frame = 0;
    let p = 0.8;
    let p2 = 0.8;
    let s = 1024;
    while(stagen == 1){
        ObjSprite3D_SetSourceDestRect(obj, 0 + (frame*p) % s, 0 + (frame*p) % s, s + (frame*p) % s, s + (frame*p) % s);
        ObjSprite3D_SetSourceDestRect(obj2, 0, 0 + (frame*p2) % s, s,s + (frame*p2) % s);
        frame++;
        wait(1);
    }
    Obj_Delete(obj);
    Obj_Delete(obj2);
}

task noninvisible(obj){
    while(!Obj_IsDeleted(obj))
    {
        if(bSpell)
        {
            Obj_SetVisible(obj, false);
        }
        else
        {
            Obj_SetVisible(obj, true);
        }
        yield;

    } 
}

task debugtext(text){
     let objText = ObjText_Create();
    ObjText_SetText(objText, text);
    ObjText_SetFontSize(objText, 24);
    ObjText_SetFontType(objText, "Russell Square Regular");
    ObjText_SetFontBold(objText, true);
    ObjText_SetFontColorTop(objText, 255, 255, 255);
    ObjText_SetFontColorBottom(objText, 255, 255, 255);
    ObjText_SetFontBorderType(objText, BORDER_FULL);
    ObjText_SetFontBorderColor(objText, 224, 64, 192);
    ObjText_SetFontBorderWidth(objText, 1);
    wait(1);
    Obj_Delete(objText);
}

function wait(n){
     loop(n){yield;}
}