let bSpell = false;
let GCSD = GetCurrentScriptDirectory();

@Initialize{
	TCallBg;
}

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

task TCallBg{
    loop{
        TNormalBackground;
        while(bSpell==false){ yield; }
        TSpellBackground;
        while(bSpell==true){ yield; }
    }
}

task TNormalBackground{
	let non = GetCurrentScriptDirectory() ~ "img/ouch.png";
	let light = GetCurrentScriptDirectory() ~ "img/light.png";

	let arr = [];//640x480
	ascent(iba in 0..3){
		arr = arr ~ [ObjPrim_Create(OBJ_SPRITE_2D)];
		
		alternative(iba)
			case(0){sos(arr[0], non, 200, 1024, 1024, gx, gy-100);} //up_rock
			case(1){sos(arr[1], non, 200, 1024, 1024, gx, gy+100);}	//bottom_rock	
			case(2){sos(arr[2], light, 255, 640, 480, gx, gy);}	//light
			
		ObjRender_SetBlendType(arr[iba], BLEND_ALPHA);
		Obj_SetRenderPriority(arr[iba], 0.21);
		ObjSprite2D_SetDestCenter(arr[iba]);

	}
	
	ObjRender_SetAngleZ(arr[1], 180);
	ObjRender_SetColor(arr[2], 0, 50, 0);
	ObjRender_SetBlendType(arr[2], BLEND_ADD_ARGB);
	
	let frame = 0;
	while(bSpell){ 
		ascent(iba in 0..length(arr)){
			Obj_SetVisible(arr[iba], false);
		}
		
		yield;
	} 
	while(!bSpell){
		ascent(iba in 0..length(arr)){
			Obj_SetVisible(arr[iba], true);
		}
		
		ObjSprite2D_SetSourceRect(arr[0], 0 + (frame*2) % 1024, 0, 1024 + (frame*2) % 1024, 1024);
		ObjSprite2D_SetSourceRect(arr[1], 0 + (-frame*2) % 1024, 0, 1024 + (-frame*2) % 1024, 1024);
		frame-=3;
		yield;
	} 
	
	ascent(iba in 0..length(arr)){
		Obj_Delete(arr[iba]);
	}
}

task TSpellBackground{
    let bg = GetCurrentScriptDirectory() ~ "img/goop.png";
	let her = GetCurrentScriptDirectory() ~ "img/her.png";

	let arr = [];
	ascent(iba in 0..2){
		arr = arr ~ [ObjPrim_Create(OBJ_SPRITE_2D)];
		alternative(iba)
			case(0){sos(arr[0], bg, 255, 512, 512, gx, gy);}  //bg
			case(1){sos(arr[1], her, 255, 597, 787, gx, gy);} //cutin dela
			
		ObjRender_SetBlendType(arr[iba], BLEND_ALPHA);
		Obj_SetRenderPriority(arr[iba], 0.21);
		ObjSprite2D_SetDestCenter(arr[iba]);
	}
	
	ObjRender_SetBlendType(arr[0], BLEND_INV_DESTRGB);
	
	let frame = 0;
	
	while(bSpell){
		ascent(iba in 0..length(arr)){
			Obj_SetVisible(arr[iba], true);
		}
		
		ObjSprite2D_SetSourceRect(arr[0], 0, 0 + (frame*2) % 512, 512, 512 + (frame*2) % 512);
		frame -= 1.5;
		yield;
	}
	
	while(!bSpell){
		ascent(iba in 0..length(arr)){
			Obj_SetVisible(arr[iba], false);
		}
		yield;
    }
	
	ascent(iba in 0..length(arr)){Obj_Delete(arr[iba]);}
}

task sos(ob, path, alpha, r, b, x, y){
	ObjPrim_SetTexture(ob, path);
	ObjRender_SetAlpha(ob, alpha);
	ObjSprite2D_SetSourceRect(ob, 0, 0, r, b);
	ObjRender_SetScaleXYZ(ob, 512/r, 512/b, 0);
	ObjRender_SetX(ob, x);
	ObjRender_SetY(ob, y);
}

function gx(){return GetStgFrameWidth() / 2;}
function gy(){return GetStgFrameHeight() / 2;}