let bSpell1 = false;
let bSpell2 = false;
let bSpell3 = false;
let current = GetCurrentScriptDirectory()~"./img/";

@Initialize{
        TNormalBackground;
	TSpellBackground1;
	TSpellBackground2;
	TSpellBackground3;
}


@MainLoop{
	let objScene = GetEnemyBossSceneObjectID();
	if(objScene != ID_INVALID && ObjEnemyBossScene_GetInfo(objScene, INFO_IS_SPELL)){
            if(ObjEnemyBossScene_GetInfo(objScene, INFO_ORGTIMERF)>5500){bSpell3 = true; bSpell1=false; bSpell2=false;}
	    else if(ObjEnemyBossScene_GetInfo(objScene, INFO_ORGTIMERF)>4900){bSpell1 = true; bSpell2= false; bSpell3=false;}
	    else{bSpell2 = true; bSpell1 = false; bSpell3 = false;}
        }
        else{bSpell1 = false; bSpell2= false; bSpell3=false;}
	yield;
}


task TNormalBackground {
    let path1 = current ~ "nonSpell22.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, 130, 0, 570, 448);
    ObjSprite2D_SetDestRect(obj, -192, -224, 192, 224);
    ObjRender_SetPosition(obj, 384/2, 448/2, 1);
    let frameInvisible = 0;
    let alpha = 0;
    
    loop {
         if(bSpell1 || bSpell2 ||bSpell3){
            frameInvisible++;
	    if(frameInvisible >= 60){Obj_SetVisible(obj, false);}
        }
	else{
            frameInvisible = 0;
	    Obj_SetVisible(obj, true);
	}
        yield;
    }
}


task TSpellBackground1 {
    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(bSpell1){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;
    }
}


task TSpellBackground2{
    let path2 = current ~ "skull3.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, 400, 285);
    ObjSprite2D_SetDestRect(obj2, -210, -224, 210, 224);

    ObjRender_SetPosition(obj2, 384/2, 448/2, 1);
    let frame = 0;
    let alpha = 0;
    loop {
        if(bSpell2){alpha += 3;}
	else{alpha = 0;}
        ObjRender_SetAlpha(obj2, alpha);
        Obj_SetVisible(obj2, alpha > 0);
        ObjSprite2D_SetSourceRect(obj2, 0, 0 + (frame) % 285, 400,  285+ (frame) % 285);
        frame++;
        yield; yield;
    }
}

task TSpellBackground3{
    let path2 = current ~ "mathsa.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, -210, -224, 210, 224);

    ObjRender_SetPosition(obj2, 384/2, 448/2, 1);
    let frame = 0;
    let alpha = 0;
    loop {
        if(bSpell3){alpha += 3;}
	else{alpha = 0;}
        ObjRender_SetAlpha(obj2, alpha);
        Obj_SetVisible(obj2, alpha > 0);
        ObjSprite2D_SetSourceRect(obj2, 0+(frame)%796, 0, 384+(frame)%796,  448);
        frame++;
        yield; yield;
    }
}