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


    sea(0,0,0);
    moon;
}

let bg = GetCurrentScriptDirectory() ~ "img/bg.png";
let bg2 = GetCurrentScriptDirectory() ~ "img/bg2.png";

task moon{
    let glow = 155;
    let img = ObjPrim_Create(OBJ_SPRITE_2D);
    Obj_SetRenderPriority(img, 0.21);
    ObjRender_SetBlendType(img,BLEND_ADD_ARGB);
    ObjPrim_SetTexture(img, GetCurrentScriptDirectory() ~ "img/bg3.png");   
    ObjSprite2D_SetSourceRect(img, 0, 0, 384, 140);
    ObjSprite2D_SetDestCenter(img);
    ObjRender_SetPosition(img, GetStgFrameWidth()/2, 100/2-5, 1);
    noninvisible(img);
    while(!Obj_IsDeleted(img)){
        glow+=0.5;
        ObjRender_SetColor(img, glow, glow, glow);
        yield;
        if(glow >= 255) {
            wait(120);
            loop(200) {
            ObjRender_SetColor(img, glow, glow, glow);
            glow-=0.5;
            yield;
            }
        }
    }
}

task sea(locX,locY,locZ) {
    let yp = 50;
    let ap = 5;
    let xscale = 1.85;
    let yscale = 2.4;
    let obj = ObjPrim_Create(OBJ_SPRITE_3D);
    ObjPrim_SetTexture(obj,bg);
    Obj_SetRenderPriorityI(obj, 22);
    ObjRender_SetScaleXYZ(obj,xscale,yscale,0);
    ObjRender_SetAngleXYZ(obj,110+ap,0,0);  
    ObjRender_SetZWrite(obj,true);
    ObjRender_SetPosition(obj,locX,locY+yp,locZ);
    noninvisible(obj);

    let obj2 = ObjPrim_Create(OBJ_SPRITE_3D);
    ObjPrim_SetTexture(obj2,bg2);
    Obj_SetRenderPriorityI(obj2, 22);
    ObjRender_SetBlendType(obj2, BLEND_ADD_ARGB);
    ObjRender_SetScaleXYZ(obj2,xscale,yscale,0);
    ObjRender_SetAngleXYZ(obj2,110+ap,0,0);  
    ObjRender_SetColor(obj2, 100, 100, 255);
    ObjRender_SetZWrite(obj2,true);
    ObjRender_SetAlpha(obj2, 255);
    ObjRender_SetPosition(obj2,locX,locY+yp,locZ);
    noninvisible(obj);

    let alpha = 235;
    let c = 0;
    while(alpha > 0){
        let img = ObjPrim_Create(OBJ_SPRITE_2D);
        Obj_SetRenderPriority(img, 0.23);
        ObjRender_SetBlendType(img, BLEND_ALPHA);
        ObjRender_SetAlpha(img, alpha);
        ObjPrim_SetTexture(img, GetCurrentScriptDirectory() ~ "img/bg3.png");   
        ObjSprite2D_SetSourceRect(img, 0, 349, 384, 351);
        ObjSprite2D_SetDestRect(img, -200, -3, 200, 3);
        ObjRender_SetPosition(img, GetStgFrameWidth()/2, 107+c*6, 1);
        c += 1;
        alpha += -12;
        noninvisible(img);
    }
    let frame = 2200;
    let p = 0.5;
    let p2 = 0.7;

    loop{
        ObjSprite3D_SetSourceDestRect(obj, 0, 0 + (frame*p) % 512, 512, 512 + (frame*p) % 512);
        ObjSprite3D_SetSourceDestRect(obj2, 0, 0 + (frame*p2) % 512, 512, 512 + (frame*p2) % 512);
        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/junkospell11.png";
	let path2 = GetCurrentScriptDirectory() ~ "img/junkospell3.png";

    let obj1 = ObjPrim_Create(OBJ_SPRITE_2D);
    ObjRender_SetBlendType(obj1, BLEND_ALPHA);
    Obj_SetRenderPriority(obj1, 0.23);
    ObjPrim_SetTexture(obj1, path1);
    ObjSprite2D_SetSourceRect(obj1, 0, 0, 512, 512);
    ObjSprite2D_SetDestRect(obj1, -256, -256, 256, 256);
    ObjRender_SetPosition(obj1, 384/2+64, 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_SetRenderPriority(obj2, 0.23);
    ObjPrim_SetTexture(obj2, path2);
    ObjSprite2D_SetSourceRect(obj2, 0, 0, 512, 512);
    ObjSprite2D_SetDestRect(obj2, -256, -256, 256, 256);
    ObjRender_SetPosition(obj2, 384/2, 448/2, 1);
    ObjRender_SetScaleX(obj2, 2);
   	ObjRender_SetColor(obj2, 120, 255, 255);
    ObjRender_SetAlpha(obj2, 255);
    //spellbg2(obj2, path2);

    let frame = 0;
    let alpha = 0;
    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(obj2, 0, 0 - (frame*1) % 512, 512, 512 - (frame*1) % 512);
        ObjSprite2D_SetSourceRect(obj1, 0, 0 + (frame*0.25) % 512, 512, 512 + (frame*0.25) % 512);

        frame++;
        yield;
    }

}

task spellbg2(obj, path){
    ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
    ObjRender_SetBlendType(obj,BLEND_SUBTRACT);
    ObjPrim_SetTexture(obj, path);
    Obj_SetRenderPriorityI(obj, 24);
    ObjRender_SetPosition(obj, 384/2, 135, 0);
    let vc= 450;
    ObjPrim_SetVertexCount(obj, vc);
    let angle = 270;
    let cosin = 512;
    ascent(i in 0..vc){
        i = i*2;
        angle += 360/(vc/2-1);
        ObjPrim_SetVertexPosition(obj, i+0, 0, 0,0);
        ObjPrim_SetVertexPosition(obj, i+1, cosin*cos(angle), cosin*sin(angle),0);

       
    }
   
    let n = 512/vc*2;
    let c = 255;
    let r = 90;
    let g = 170;
    let b = 220;
    let yp = 0;
    let x = 0;
    loop{
        x = 0;
        yp -= 2.2;
        ascent(i in 0..vc){
            i = i*2;
            x += n;
            ObjPrim_SetVertexUVT(obj, i, x, 0+yp);
            ObjPrim_SetVertexUVT(obj, i+1, x, 600+yp);
            ObjPrim_SetVertexColor(obj, i, r, g, b);
            ObjPrim_SetVertexColor(obj, i+1, r, g, b);
        } 
        wait(2);
        //debugtext(n);
    }
}

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