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/non.png";
	let season;
	let move = false;
	let alpha = 100;
	let speed = 1;
	let y = 1;
	let render = BLEND_ADD_ARGB;

	let obj1 = ObjPrim_Create(OBJ_SPRITE_2D);
    ObjRender_SetBlendType(obj1, BLEND_ALPHA);
	ObjRender_SetAlpha(obj1, 250);
    Obj_SetRenderPriority(obj1, 0.21);
    ObjPrim_SetTexture(obj1, non);
    ObjSprite2D_SetSourceRect(obj1,0,0,800,800);
	ObjSprite2D_SetDestCenter(obj1);
	ObjRender_SetScaleXYZ(obj1,512/800,512/800,1);
	ObjRender_SetX(obj1,GetStgFrameWidth()/2);
	ObjRender_SetY(obj1,GetStgFrameHeight()/2);
	
	if(GetCommonData("Fall", false) == true){
		move = true;
		season = GetCurrentScriptDirectory() ~ "img/Seasons/Autumn.png";
		alpha = 150;
		speed = 1;
		y = GetStgFrameHeight()/2;
		
		render = BLEND_ADD_ARGB;
		
		SetCommonData("Cold", false);
		SetCommonData("Pretty", false);
		SetCommonData("Sun", false);
		SetCommonData("Out", false);
	}
	if(GetCommonData("Sun", false) == true){
		move = false;
		season = GetCurrentScriptDirectory() ~ "img/Seasons/Summer.png";
		alpha = 255;
		y = GetStgFrameHeight()/4;
		
		render = BLEND_ADD_ARGB;
		
		SetCommonData("Cold", false);
		SetCommonData("Fall", false);
		SetCommonData("Pretty", false);
		SetCommonData("Out", false);
	}
	if(GetCommonData("Cold", false) == true){
		move = true;
		season = GetCurrentScriptDirectory() ~ "img/Seasons/Winter.png";
		alpha = 110;
		speed = 2;
		y = GetStgFrameHeight()/2;
		
		render = BLEND_ADD_ARGB;
		
		SetCommonData("Fall", false);
		SetCommonData("Pretty", false);
		SetCommonData("Sun", false);
		SetCommonData("Out", false);
	}
	if(GetCommonData("Pretty", false) == true){
		move = true;
		season = GetCurrentScriptDirectory() ~ "img/Seasons/Spring.png";
		alpha = 100;
		speed = -1;
		y = GetStgFrameHeight()/2;
		
		render = BLEND_ADD_ARGB;
		
		SetCommonData("Cold", false);
		SetCommonData("Fall", false);
		SetCommonData("Sun", false);
		SetCommonData("Out", false);
	}
	if(GetCommonData("Out", false) == true){
		move = true;
		season = GetCurrentScriptDirectory() ~ "img/op.png";
		alpha = 0;
		speed = 1;
		y = GetStgFrameHeight()/2;
		
		render = BLEND_ALPHA;
		
		SetCommonData("Cold", false);
		SetCommonData("Fall", false);
		SetCommonData("Sun", false);
		SetCommonData("Pretty", false);
	}
	
	let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
    ObjRender_SetBlendType(obj2, BLEND_ALPHA);
	ObjRender_SetAlpha(obj2, alpha);
	ObjRender_SetBlendType(obj2, render);
    Obj_SetRenderPriority(obj2, 0.21);
    ObjPrim_SetTexture(obj2, season);
    ObjSprite2D_SetSourceRect(obj2,0,0,512,512);
	ObjSprite2D_SetDestCenter(obj2);
	ObjRender_SetScaleXYZ(obj2,512/512,512/512,1);
	ObjRender_SetX(obj2,GetStgFrameWidth()/2);
	ObjRender_SetY(obj2,y);
	
	let frame = 0;
	
	while(bSpell){
		Obj_SetVisible(obj1, false); 
		Obj_SetVisible(obj2, false);
		yield;
	}
	
	while(!bSpell){
		Obj_SetVisible(obj1, true);
		Obj_SetVisible(obj2, true);
		
		if(move == true){ObjSprite2D_SetSourceRect(obj2, 0, 0 + (frame*2) % 512, 512, 512 + (frame*2) % 512);}
		frame -= speed;
		yield;
		yield;
	} 
	
	Obj_Delete(obj1);
	Obj_Delete(obj2);
}

task TSpellBackground{
    let non = GetCurrentScriptDirectory() ~ "img/non.png";
	let season;
	let move = false;
	let move_c = false;
	let alpha = 100;
	let speed = 1;
	let y = 1;
	let render = BLEND_ADD_ARGB;

	let obj1 = ObjPrim_Create(OBJ_SPRITE_2D);
    ObjRender_SetBlendType(obj1, BLEND_ALPHA);
	ObjRender_SetAlpha(obj1, 150);
    Obj_SetRenderPriority(obj1, 0.21);
    ObjPrim_SetTexture(obj1, non);
    ObjSprite2D_SetSourceRect(obj1,0,0,800,800);
	ObjSprite2D_SetDestCenter(obj1);
	ObjRender_SetScaleXYZ(obj1,512/800,512/800,1);
	ObjRender_SetX(obj1,GetStgFrameWidth()/2);
	ObjRender_SetY(obj1,GetStgFrameHeight()/2);
	
	if(GetCommonData("Fall", false) == true){
		move = true;
		season = GetCurrentScriptDirectory() ~ "img/Seasons/Autumn.png";
		alpha = 150;
		speed = 1;
		y = GetStgFrameHeight()/2;
		
		render = BLEND_ADD_ARGB;
		
		SetCommonData("Cold", false);
		SetCommonData("Pretty", false);
		SetCommonData("Sun", false);
		SetCommonData("Out", false);
	}
	if(GetCommonData("Sun", false) == true){
		move = false;
		season = GetCurrentScriptDirectory() ~ "img/Seasons/Summer.png";
		alpha = 255;
		y = GetStgFrameHeight()/4;
		
		render = BLEND_ADD_ARGB;
		
		SetCommonData("Cold", false);
		SetCommonData("Fall", false);
		SetCommonData("Pretty", false);
		SetCommonData("Out", false);
	}
	if(GetCommonData("Cold", false) == true){
		move_c = true;
		season = GetCurrentScriptDirectory() ~ "img/Seasons/Winter.png";
		alpha = 70;
		speed = 1;
		y = GetStgFrameHeight()/2;
		
		render = BLEND_ADD_ARGB;
		
		SetCommonData("Fall", false);
		SetCommonData("Pretty", false);
		SetCommonData("Sun", false);
		SetCommonData("Out", false);
	}
	if(GetCommonData("Pretty", false) == true){
		move = true;
		season = GetCurrentScriptDirectory() ~ "img/Seasons/Spring.png";
		alpha = 100;
		speed = -1;
		y = GetStgFrameHeight()/2;
		
		render = BLEND_ADD_ARGB;
		
		SetCommonData("Cold", false);
		SetCommonData("Fall", false);
		SetCommonData("Sun", false);
		SetCommonData("Out", false);
	}
	if(GetCommonData("Out", false) == true){
		move = true;
		season = GetCurrentScriptDirectory() ~ "img/op.png";
		alpha = 100;
		speed = 1;
		y = GetStgFrameHeight()/2;
		
		render = BLEND_ALPHA;
		
		SetCommonData("Cold", false);
		SetCommonData("Fall", false);
		SetCommonData("Sun", false);
		SetCommonData("Pretty", false);
	}
	
	let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
    ObjRender_SetBlendType(obj2, BLEND_ALPHA);
	ObjRender_SetBlendType(obj2, render);
	ObjRender_SetAlpha(obj2, alpha);
    Obj_SetRenderPriority(obj2, 0.21);
    ObjPrim_SetTexture(obj2, season);
    ObjSprite2D_SetSourceRect(obj2,0,0,512,512);
	ObjSprite2D_SetDestCenter(obj2);
	ObjRender_SetScaleXYZ(obj2,512/512,512/512,1);
	ObjRender_SetX(obj2,GetStgFrameWidth()/2);
	ObjRender_SetY(obj2,y);
	
	let obj3 = ObjPrim_Create(OBJ_SPRITE_2D);
	if(GetCommonData("Cold", false) == true){
		ObjRender_SetBlendType(obj3, BLEND_ALPHA);
		ObjRender_SetBlendType(obj3, render);
		ObjRender_SetAlpha(obj3, alpha-30);
		Obj_SetRenderPriority(obj3, 0.21);
		ObjPrim_SetTexture(obj3, season);
		ObjSprite2D_SetSourceRect(obj3,0,0,512,512);
		ObjSprite2D_SetDestCenter(obj3);
		ObjRender_SetScaleXYZ(obj3,512/512,512/512,1);
		ObjRender_SetX(obj3,GetStgFrameWidth()/2);
		ObjRender_SetY(obj3,y);
	}
	
	let frame = 0;
	
	while(bSpell){
		Obj_SetVisible(obj1, true);
		Obj_SetVisible(obj2, true);
		
		if(move == true){
			ObjSprite2D_SetSourceRect(obj2, 0, 0 + (frame*2) % 512, 512, 512 + (frame*2) % 512);
		}
		if(move_c == true){
			Obj_SetVisible(obj3, true);
			
			
			ObjSprite2D_SetSourceRect(obj2, 0 + (frame*+2) % 512, 0 + (frame*-1) % 512, 512 + (frame*+2) % 512, 512 + (frame*-1) % 512);
			ObjSprite2D_SetSourceRect(obj3, 0 + (frame*-2) % 512, 0 + (frame*+1) % 512, 512 + (frame*-2) % 512, 512 + (frame*+1) % 512);
		}
		frame -= speed;
		yield;
	}
	
	while(!bSpell){
		Obj_SetVisible(obj1, false);
		Obj_SetVisible(obj2, false);
		yield;
    }
	
	Obj_Delete(obj1);
	Obj_Delete(obj2);
	Obj_Delete(obj3);
}