let stagen;

task Stage1BG(){
    stagen = 1;
    let bg1 = GetCurrentScriptDirectory() ~ "img/bg1.png";
    let bg2 = GetCurrentScriptDirectory() ~ "img/bg2.png";
    let cmr =0;
    SetCameraPerspectiveClip(16,8192);
    SetCameraAzimuthAngle(-90);
    SetCameraElevationAngle(47);
    SetCameraRadius(64);
    SetCameraFocusX(0);
    SetCameraFocusY(512);
    SetCameraFocusZ(-512+cmr);


    let yp = 70;
    let yp2 = 30;
    let ap = 60;
    let xscale = 1.5;
    let yscale = 1.5;
    let obj = ObjPrim_Create(OBJ_SPRITE_3D);
    ObjPrim_SetTexture(obj,bg1);
    Obj_SetRenderPriorityI(obj, 21);
    ObjRender_SetBlendType(obj, BLEND_ALPHA);
    ObjRender_SetAngleXYZ(obj,ap,0,0);  
    let c = 90;
    ObjRender_SetColor(obj, c, c, c);
    ObjRender_SetZWrite(obj,true);
    ObjRender_SetAlpha(obj, 255);
    ObjRender_SetPosition(obj,0,yp2,0);
    ObjRender_SetScaleXYZ(obj, 1.25, 1.25, 1);
    noninvisible(obj);

    let obj2 = ObjPrim_Create(OBJ_SPRITE_3D);
    fogdetail(obj2);

     let obj4 = ObjPrim_Create(OBJ_SPRITE_3D);
    fogdetail(obj4);

    let obj5 = ObjPrim_Create(OBJ_SPRITE_3D);
    fogdetail(obj5);
    

    task fogdetail(obj){
        ObjPrim_SetTexture(obj,bg2);
        Obj_SetRenderPriorityI(obj, 22);
        ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
        ObjRender_SetScaleXYZ(obj,2,2,0);
        ObjRender_SetAngleXYZ(obj,ap,0,0);  
        ObjRender_SetColor(obj, 140, 240, 255);
        ObjRender_SetZWrite(obj,true);
        ObjRender_SetPosition(obj,0,yp,0);
        noninvisible(obj);
    }

    let frame = 0;
    let p = 3;
    let p2 = 2.5;
    let p3 = 1.5;
    let p4 = 2;
    let s = 1024;

    while(stagen == 1){
        ObjSprite3D_SetSourceDestRect(obj, 0, 0 + (frame*p) % s, 512, s + (frame*p) % s);
        ObjSprite3D_SetSourceDestRect(obj2, 256 + (frame*p2/3) % 512, 0+256 + (frame*p2) % s, s+256 + (frame*p2/3) % 512, s+256 + (frame*p2) % s);
         ObjSprite3D_SetSourceDestRect(obj4, 256 + (frame*p3/3) % 512, 0+128 + (frame*p3) % s, s +256 + (frame*p3/3) % 512, s+128 + (frame*p3) % s);
          ObjSprite3D_SetSourceDestRect(obj5, 256 + (frame*p4/3) % 512, 0+384 + (frame*p4) % s, s +256 + (frame*p4/3) % 512, s+384 + (frame*p4) % s);
        frame++;
        wait(1);
    }
    Obj_Delete(obj);
    Obj_Delete(obj2);
    Obj_Delete(obj4);
    Obj_Delete(obj5);
}

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;}
}