let bSpell = false;
let current = GetCurrentScriptDirectory();
@Initialize
{
    TNormalBackground();
	//TSpellBackground();
    //TWaveCircle;
}

@MainLoop
{

	let objScene = GetEnemyBossSceneObjectID();
	if(objScene != ID_INVALID && ObjEnemyBossScene_GetInfo(objScene, INFO_IS_SPELL))
	{
		bSpell = true;
	}
	else
	{
		bSpell = false;
	}
	

	yield;
}


task TNormalBackground(){

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


    normalbg;
}

task normalbg{
    let bg = GetCurrentScriptDirectory() ~ "img/bg1.png";
    let bg2 = GetCurrentScriptDirectory() ~ "img/bg2.png";
    let bg3 = GetCurrentScriptDirectory() ~ "img/bg3.png";
    let bg4 = GetCurrentScriptDirectory() ~ "img/bg4.png";
    let xscale = 1;
    let yscale = 1;

    let obj = ObjPrim_Create(OBJ_SPRITE_3D);
    ObjPrim_SetTexture(obj,bg);
    Obj_SetRenderPriorityI(obj, 21);
    ObjRender_SetBlendType(obj, BLEND_ALPHA);
    ObjRender_SetScaleXYZ(obj,xscale,yscale,0);
    ObjRender_SetAngleXYZ(obj,317,0,0);  
    ObjRender_SetColor(obj, 255, 255, 255);
    ObjRender_SetZWrite(obj,true);
    ObjRender_SetAlpha(obj, 255);
    ObjRender_SetPosition(obj,0,400,0);
    noninvisible(obj);

    let obj2 = ObjPrim_Create(OBJ_SPRITE_3D);
    ObjPrim_SetTexture(obj2,bg2);
    Obj_SetRenderPriorityI(obj2, 21);
    ObjRender_SetBlendType(obj2, BLEND_ALPHA);
    ObjRender_SetScaleXYZ(obj2,xscale,yscale,0);
    ObjRender_SetAngleXYZ(obj2,317,0,0);  
    ObjRender_SetColor(obj2, 255, 255, 255);
    ObjRender_SetZWrite(obj2,true);
    ObjRender_SetAlpha(obj2, 255);
    ObjRender_SetPosition(obj2,0,-400,0);
    noninvisible(obj2);

    let obj3 = ObjPrim_Create(OBJ_SPRITE_2D);
    ObjPrim_SetTexture(obj3,bg3);
    Obj_SetRenderPriorityI(obj3, 22);
    ObjRender_SetBlendType(obj3, BLEND_ALPHA);
    ObjRender_SetScaleXYZ(obj3,xscale,yscale,0);
    ObjSprite2D_SetSourceRect(obj3, 0, 0, 1024, 512);
    ObjSprite2D_SetDestCenter(obj3);
    ObjRender_SetAlpha(obj3, 255);
    ObjRender_SetPosition(obj3,248,206.5,0);
    let xscale2 = 0.9;
    let yscale2 = 0.55;
    ObjRender_SetScaleXYZ(obj3, xscale2, yscale2, 1);
    noninvisible(obj3);



    let obj4 = ObjPrim_Create(OBJ_SPRITE_2D);
    ObjPrim_SetTexture(obj4,bg4);
    Obj_SetRenderPriorityI(obj4, 20);
    ObjRender_SetBlendType(obj4, BLEND_ALPHA);
    ObjRender_SetScaleXYZ(obj4,xscale,yscale,0);
    ObjSprite2D_SetSourceRect(obj4, 0, 0,384, 348);
    ObjSprite2D_SetDestCenter(obj4);
    ObjRender_SetAlpha(obj4, 255);
    ObjRender_SetPosition(obj4,192,158,0);
    let xscale3 = 1;
    let yscale3 = 1;
    ObjRender_SetScaleXYZ(obj4, xscale3, yscale3, 1);
    noninvisible(obj4);

        let img = ObjPrim_Create(OBJ_SPRITE_2D);
        Obj_SetRenderPriorityI(img, 21);
        ObjRender_SetBlendType(img, BLEND_ALPHA);
        ObjRender_SetAlpha(img, 170); 
        ObjRender_SetColor(img, 0,0, 0);
         ObjSprite2D_SetSourceRect(img, 0, 349, 384, 351);
        ObjSprite2D_SetDestRect(img, -400, -3, 400, 3);
        ObjRender_SetPosition(img, 384/2, 66, 1);
        noninvisible(img);

    let alpha = 255;
    let c = 0;
    let frame = 0;
    let p = 1.7;
    let p3 = 1.1;
    let s = 1024;
    loop{
        ObjSprite3D_SetSourceDestRect(obj, 0  + (frame*p) % s, 0, s  + (frame*p) % s,s);
        ObjSprite3D_SetSourceDestRect(obj2, 0  + (frame*p) % s, 0, s  + (frame*p) % s,s);
        ObjSprite2D_SetSourceRect(obj3, 0 + (frame*p3) % s, 0, s + (frame*p3) % s, 390);
        frame++;
        wait(1);
    }
}

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

    } 
}


task TSpellBackground{
	let path1 = GetCurrentScriptDirectory() ~ "img/spellbg1.png";
	let path2 = GetCurrentScriptDirectory() ~ "img/spellbg3.png";

    let obj1 = ObjPrim_Create(OBJ_SPRITE_2D);
    ObjRender_SetBlendType(obj1, BLEND_ALPHA);
    Obj_SetRenderPriorityI(obj1, 22);
    ObjPrim_SetTexture(obj1, path1);
    ObjSprite2D_SetSourceRect(obj1, 0, 0, 638, 851);
    ObjSprite2D_SetDestCenter(obj1);
    ObjSprite2D_SetDestRect(obj1, -192, -851/2, 192, 851/2);
    ObjRender_SetPosition(obj1, 384/2, 448/2, 1);
   // ObjRender_SetScaleXYZ(obj1, 1, 1, 1);
    ObjRender_SetAlpha(obj1,255);

    let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
    ObjRender_SetBlendType(obj2, BLEND_SUBTRACT);
    Obj_SetRenderPriorityI(obj2, 23);
    ObjPrim_SetTexture(obj2, path2);
    ObjSprite2D_SetSourceRect(obj2, 0, 0, 512, 512);
    let d = 2;
    ObjSprite2D_SetDestRect(obj2, -512/d, -512/d, 512/d, 512/d);
    ObjRender_SetPosition(obj2, 384/2, 448/2, 1);
    let c = 255;
    ObjRender_SetColor(obj2, c, 255, c);
    ObjRender_SetAlpha(obj2,130);
    let sc = 1.7;
    ObjRender_SetScaleXYZ(obj2, sc, sc*1.5, 1);

    let frame = 0;
    let alpha = 0;
    let fn = 0.2;
    let fn2 = -0.3;
    let s = 2;
    loop {
        if (bSpell) { //Transition from stage background
            alpha = 255;
        } else {
            alpha = 0; //If no longer spell
        }
        Obj_SetVisible(obj1, alpha > 0);
        Obj_SetVisible(obj2, alpha > 0);
        ObjSprite2D_SetSourceRect(obj1, 0, 0 - (frame*fn2) % 851, 638, 851 - (frame*fn2) % 851);
        ObjSprite2D_SetSourceRect(obj2, 0, 0 - (frame*fn) % 512*s, 512*s, 512*s - (frame*fn) % 512*s);
        frame++;
        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;}
}