let current = GetCurrentScriptDirectory()~"./img/";
let bSpell =false;


@Initialize{
        TNonBackground1;
	TNonBackground2;
	TSpellBackground1;
	TSpellBackground2;
	TSpellBackground3;
        TSpellBackground4;
	TSpellBackgroundMario;
	TSpellBackgroundJames;
}


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


task TNonBackground1 {
    let path1 = current ~ "non11.png";
    let obj = ObjPrim_Create(OBJ_SPRITE_2D);
    ObjRender_SetBlendType(obj, BLEND_ALPHA);
    Obj_SetRenderPriority(obj, 0.21);
    ObjPrim_SetTexture(obj, path1);
    ObjSprite2D_SetSourceRect(obj, 0, 52, 660, 552);
    ObjSprite2D_SetDestRect(obj, -192, -224, 192, 224);
    ObjRender_SetPosition(obj, 384/2, 448/2, 1);
    Obj_SetVisible(obj, true);
    let frameInvisible = 0;
    let alpha = 240;
    
    loop {
         if(bSpell){
            frameInvisible++;
	    if(frameInvisible >= 150){Obj_SetVisible(obj, false);}
        }
	else if(GetCommonData("change", "") == "yes"){
	    ObjRender_SetAlpha(obj, alpha); Obj_SetVisible(obj, alpha>0);
	    alpha-=3;
        }
        else if(GetCommonData("non2", "") == "no"){Obj_SetVisible(obj, true);}
	else{
            frameInvisible = 0;
	    Obj_SetVisible(obj, true);
	}
        if(GetCommonData("change", "") == "yes"){loop(6){yield;}} 
        else{yield;}
    }
}


task TNonBackground2{
    let path1 = current ~ "non22.png";
    let obj = ObjPrim_Create(OBJ_SPRITE_2D);
    ObjRender_SetBlendType(obj, BLEND_ALPHA);
    Obj_SetRenderPriority(obj, 0.21);
    ObjPrim_SetTexture(obj, path1);
    ObjSprite2D_SetSourceRect(obj, 0, 0, 384, 448);
    ObjSprite2D_SetDestRect(obj, -192, -224, 192, 224);
    ObjRender_SetPosition(obj, 384/2, 448/2, 1);
    Obj_SetVisible(obj, false);
    let frameInvisible = 0;
    let alpha = 0;
    
    loop {
         if(bSpell){
            frameInvisible++;
	    if(frameInvisible >= 150){Obj_SetVisible(obj, false);}
        }
	else if(GetCommonData("change", "") == "yes"){
	    ObjRender_SetAlpha(obj, alpha); Obj_SetVisible(obj, alpha>10);
	    alpha+=3;
        }
        else if(GetCommonData("non2", "") == "no"){Obj_SetVisible(obj, false);}
        else{
            frameInvisible = 0;
	    Obj_SetVisible(obj, true);
	}
        if(GetCommonData("change", "") == "yes"){loop(6){yield;}}
        else{yield;}
    }
}


task TSpellBackground1 {
    let path2 = current ~ "Spell1.png";
    let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
    ObjRender_SetBlendType(obj2, BLEND_ALPHA);
    Obj_SetRenderPriority(obj2, 0.21);
    ObjPrim_SetTexture(obj2, path2);
    ObjSprite2D_SetSourceRect(obj2, 0, 0, 720, 448);
    ObjSprite2D_SetDestRect(obj2, -360, -224, 360, 224);
    
    ObjRender_SetPosition(obj2, 384/2, 448/2, 1);
 
    let ang = ObjRender_GetAngleZ(obj2);
    let alpha = 0;
    loop {
        if(bSpell && (GetCommonData("spell", "") == "Unstable forms" ||GetCommonData("spell", "") == "Integral")){alpha += 3;}
	else{alpha = 0;}
        ang+=0.2;
        ObjRender_SetAlpha(obj2, alpha);
        Obj_SetVisible(obj2, alpha > 0);
        ObjRender_SetAngleZ(obj2, ang);
        yield; yield;
    }
}


task TSpellBackground2{
    let path2 = current ~ "Spell22.png";
    let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
    ObjRender_SetBlendType(obj2, BLEND_ALPHA);
    Obj_SetRenderPriority(obj2, 0.21);
    ObjPrim_SetTexture(obj2, path2);
    ObjSprite2D_SetSourceRect(obj2, 0, 0, 384, 448);
    ObjSprite2D_SetDestRect(obj2, -192, -224, 192, 224);

    let path = current ~ "eff2.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(obj, 0.21);
	ObjPrim_SetTexture(obj, path);
        ObjSprite2D_SetSourceRect(obj,0,0,256,256);
        ObjSprite2D_SetDestRect(obj,-256,-256,256,256);

    ObjRender_SetPosition(obj2, 384/2, 448/2, 1);
    ObjRender_SetPosition(obj, 384/2, 448/2, 1);
    let frame = 0;
    let alpha = 0;
    loop {
        if(bSpell && (GetCommonData("spell", "")== "Chaos rising" || GetCommonData("spell", "")== "Mouth" )){alpha += 3;}
	else{alpha = 0;}
        ObjRender_SetAlpha(obj2, alpha);
        Obj_SetVisible(obj2, alpha > 0);

        ObjRender_SetAlpha(obj, alpha); if(alpha>=100){ObjRender_SetAlpha(obj, 100);}
        Obj_SetVisible(obj, alpha > 0);
        ObjSprite2D_SetSourceRect(obj, 0+frame,0,256+frame,256);

        frame++;
        yield; yield; //yield;
    }
}


task TSpellBackground3{
    let path2 = current ~ "Spell3.png";
    let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
    ObjRender_SetBlendType(obj2, BLEND_ALPHA);
    Obj_SetRenderPriority(obj2, 0.21);
    ObjPrim_SetTexture(obj2, path2);
    ObjSprite2D_SetSourceRect(obj2, 0, 0, 458, 448);
    ObjSprite2D_SetDestRect(obj2, -192, -224, 192, 224);

    let path = current ~ "eff3.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(obj, 0.21);
	ObjPrim_SetTexture(obj, path);
        ObjSprite2D_SetSourceRect(obj,0,0,256,256);
        ObjSprite2D_SetDestRect(obj,-256,-256,256,256);

    ObjRender_SetPosition(obj2, 384/2, 448/2, 1);
    ObjRender_SetPosition(obj, 384/2, 448/2, 1);
    let frame = 0;
    let alpha = 0;
    loop {
        if(bSpell && (GetCommonData("spell", "")== "Timestop" || GetCommonData("spell", "")== "Hourglass")){alpha += 3;}
	else{alpha = 0;}
        ObjRender_SetAlpha(obj2, alpha);
        Obj_SetVisible(obj2, alpha > 0);

        ObjRender_SetAlpha(obj, alpha); if(alpha>=150){ObjRender_SetAlpha(obj, 150);}
        Obj_SetVisible(obj, alpha > 0);
        ObjSprite2D_SetSourceRect(obj, 0,0+frame,256,256+frame);

        frame++;
        yield; yield; //yield;
    }
}


task TSpellBackground4{
    let path2 = current ~ "Spell4.png";
    let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
    ObjRender_SetBlendType(obj2, BLEND_ALPHA);
    Obj_SetRenderPriority(obj2, 0.21);
    ObjPrim_SetTexture(obj2, path2);
    ObjSprite2D_SetSourceRect(obj2, 0, 0, 384, 448);
    ObjSprite2D_SetDestRect(obj2, -192, -224, 192, 224);

    let path = current ~ "eff3.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(obj, 0.21);
	ObjPrim_SetTexture(obj, path);
        ObjSprite2D_SetSourceRect(obj,0,0,256,256);
        ObjSprite2D_SetDestRect(obj,-256,-256,256,256);

    ObjRender_SetPosition(obj2, 384/2, 448/2, 1);
    ObjRender_SetPosition(obj, 384/2, 448/2, 1);
    let frame = 0;
    let alpha = 0;
    loop {
        if(bSpell && (GetCommonData("spell", "")== "Vector field")){alpha += 3;}
	else{alpha = 0;}
        ObjRender_SetAlpha(obj2, alpha); //if(alpha>=10){ObjRender_SetAlpha(obj2, 140);}
        Obj_SetVisible(obj2, alpha > 0);

        ObjRender_SetAlpha(obj, alpha); if(alpha>=120){ObjRender_SetAlpha(obj, 120);}
        Obj_SetVisible(obj, alpha > 0);
        ObjSprite2D_SetSourceRect(obj, 0+frame,0,256+frame,256);

        frame++;
        yield; yield; //yield;
    }
}



task TSpellBackgroundMario{
    let path2 = current ~ "SpellMarioBG.png";
    let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
    ObjRender_SetBlendType(obj2, BLEND_ALPHA);
    Obj_SetRenderPriority(obj2, 0.21);
    ObjPrim_SetTexture(obj2, path2);
    ObjSprite2D_SetSourceRect(obj2, 6, 0, 390, 448);
    ObjSprite2D_SetDestRect(obj2, -192, -224, 192, 224);

    let path = current ~ "eff3.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(obj, 0.21);
	ObjPrim_SetTexture(obj, path);
        ObjSprite2D_SetSourceRect(obj,0,0,256,256);
        ObjSprite2D_SetDestRect(obj,-256,-256,256,256);

    ObjRender_SetPosition(obj2, 384/2, 448/2, 1);
    let frame = 0;
    let alpha = 0;
    loop {
        if(bSpell && GetCommonData("spell", "") == "MarioSpell"){alpha += 3;}
	else{alpha = 0;}
        ObjRender_SetAlpha(obj2, alpha);
        Obj_SetVisible(obj2, alpha > 0);
        ObjSprite2D_SetSourceRect(obj2, 6, 0, 390,  510);

        ObjRender_SetAlpha(obj, alpha); if(alpha>=120){ObjRender_SetAlpha(obj, 120);}
        Obj_SetVisible(obj, alpha > 0);
        ObjSprite2D_SetSourceRect(obj, 0,0+frame,256,256+frame); frame++;
        yield; yield;
    }
}


task TSpellBackgroundJames {
    let path2 = current ~ "mathsag.png";
    let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
    ObjRender_SetBlendType(obj2, BLEND_ALPHA);
    Obj_SetRenderPriority(obj2, 0.21);
    ObjPrim_SetTexture(obj2, path2);
    ObjSprite2D_SetSourceRect(obj2, 0, 0, 583, 500);
    ObjSprite2D_SetDestRect(obj2, -192, -224, 192, 224);
    
    ObjRender_SetPosition(obj2, 384/2, 448/2, 1);
    let frame = 0;
    let alpha = 0;
    loop {
        if(bSpell && GetCommonData("spell", "") == "JamesSpell"){alpha += 3;}
	else{alpha = 0;}
        ObjRender_SetAlpha(obj2, alpha);
        Obj_SetVisible(obj2, alpha > 0);
        ObjSprite2D_SetSourceRect(obj2, 0+ (frame)%800, 0, 583+ frame%800, 500);
        frame++;
        yield;
    }
}