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(1);
    SetCameraFocusX(0);
    SetCameraFocusY(512);
    SetCameraFocusZ(-512+cmr);


    normalbg;
}

task normalbg{
    let bg1 = GetCurrentScriptDirectory() ~ "img/bg01.png";
    let bg2 = GetCurrentScriptDirectory() ~ "img/bg02.png";
    let cimg = GetCurrentScriptDirectory() ~ "img/whitecircle2.png";

    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 = 40;
    let y = -50;

    let obj2 = imgdef3D(bg2, 21, BLEND_ADD_ARGB, 2.55, angle, 100, y);
    createfog;
    let circle = ObjPrim_Create(OBJ_SPRITE_2D);
    ObjPrim_SetTexture(circle, cimg);
    ObjRender_SetBlendType(circle, BLEND_ALPHA);
    ObjSprite2D_SetSourceRect(circle, 1, 1, 512, 512);
    ObjSprite2D_SetDestCenter(circle);
    Obj_SetRenderPriorityI(circle, 22);
    let scale = 1;
    ObjRender_SetScaleXYZ(circle, scale, scale, 1);
    ObjRender_SetAlpha(circle, 255);
    let col = 0;
    ObjRender_SetColor(circle, col, col, col);
    ObjRender_SetPosition(circle, 192, 198, 1);
    noninvisible(circle);
    
    task createfog{
        let x = 800;
        let y = 448+300;
        let posneg = -1;
        let c = 1800;
        let wt = 60;
        loop(1800/wt){
            let obj3 = imgdef2D(x, y, 3.5, c);
            c-= wt;
            x += 250 *posneg;
            y -= 150 * posneg;
            posneg *= -1;
        }
        loop{
            let obj3 = imgdef2D(x,y, 3.5, 0);
            x += 250 *posneg;
            y -= 150 * posneg;
            posneg *= -1;
            wait(wt);
        }

        function imgdef2D(x,y, scale, c){
            let img1 = ObjPrim_Create(OBJ_SPRITE_2D);
            ObjPrim_SetTexture(img1, bg1);
            ObjRender_SetBlendType(img1, BLEND_ADD_ARGB);
            ObjSprite2D_SetSourceRect(img1, 0, 0, 256, 256);
            ObjSprite2D_SetDestCenter(img1);
            Obj_SetRenderPriorityI(img1, 23);
            ObjRender_SetScaleXYZ(img1, scale, scale, 1);
            ObjRender_SetAlpha(img1, 10);
            ObjRender_SetPosition(img1, x, y, 1);
            moveimg(img1, x, y, c);
            noninvisible(img1);
            return img1;

            task moveimg(img1, x, y, c){
                let xp = 0.5;
                let yp = 0.7;
                x -= xp * c;
                y -= yp * c;
                loop(60*30-c){
                    ObjRender_SetPosition(img1, x, y, 1);
                    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/2;
    let s = 256;
    loop{
        ObjSprite3D_SetSourceDestRect(obj2, 0, 0 + (frame*p2) % s*2, s,s*2 + (frame*p2) % s*2);
        frame++;
        wait(1);
    }
}

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