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

@Initialize{
	SetCameraAzimuthAngle(-90);
	SetFogParam(256*3, 256*5, 0, 0, 77);
	SetCameraElevationAngle(5); 
	
	TCallBg;
}

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

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

task TNormalBackground{
	//{
		let x = 10; let y = 8;
	//}
	
	let non = GetCurrentScriptDirectory() ~ "img/floor.png";
	let juicy = GetCurrentScriptDirectory() ~ "img/clouds.png";
	let walls = GetCurrentScriptDirectory() ~ "img/stars.png";

	x = 10; y = 8;
	let obj1 = ObjPrim_Create(OBJ_SPRITE_3D);
    ObjRender_SetBlendType(obj1, BLEND_ALPHA); ObjRender_SetAlpha(obj1, 255);
    Obj_SetRenderPriority(obj1, 0.01);
    ObjPrim_SetTexture(obj1, non);
	ObjSprite3D_SetSourceRect(obj1, 0, 0, 256*x, 256*y);
	ObjSprite3D_SetDestRect(obj1, -256/2*x, -256/2*y, 256/2*x, 256/2*y);
	ObjRender_SetAngleX(obj1, 90);
	ObjRender_SetY(obj1, 0);
	ObjRender_SetZ(obj1, 400);
	
	x = 6; y = 6;
	let cloud = ObjPrim_Create(OBJ_SPRITE_3D);
    ObjRender_SetBlendType(cloud, BLEND_ALPHA);
	ObjRender_SetAlpha(cloud, 0);
    Obj_SetRenderPriority(cloud, 0.01);
    ObjPrim_SetTexture(cloud, juicy);
	ObjSprite3D_SetSourceRect(cloud, 0, 0, 320*x, 320*y);
	ObjSprite3D_SetDestRect(cloud, -320/2*x, -320/2*y, 320/2*x, 320/2*y);
	ObjRender_SetAngleX(cloud, 92);
	ObjRender_SetY(cloud, 10);
	ObjRender_SetX(cloud, -320);
	ObjRender_SetZ(cloud, 800);
	
	let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
    ObjRender_SetBlendType(obj2, BLEND_ALPHA);
	ObjRender_SetAlpha(obj2, 0);
    Obj_SetRenderPriority(obj2, 0);
    ObjPrim_SetTexture(obj2, walls);
    ObjSprite2D_SetSourceRect(obj2,0,0,320,320);
	ObjSprite2D_SetDestCenter(obj2);
	ObjRender_SetScaleXYZ(obj2,640/320,960/320,1);
	ObjRender_SetX(obj2,GetStgFrameWidth()/2);
	ObjRender_SetY(obj2,GetStgFrameHeight()/2-200);
	
	let i = 0; let j = 0; let eAng = 90; let t = true;
	if(GetCommonData("op", false) == false){eAng = 168;}
	while(bSpell){Obj_SetVisible(obj1, false); Obj_SetVisible(obj2, false); Obj_SetVisible(cloud, false); yield;} 
	while(!bSpell){
		Obj_SetVisible(obj1, true); 
		Obj_SetVisible(obj2, true);  
		Obj_SetVisible(cloud, true); 
		
		SetCameraElevationAngle(eAng); 
		x = 10; y = 8;
		ObjSprite3D_SetSourceRect(obj1, 0, 0 + (i*4) % 512, 256*x, 256*y + (i*4) % 512);
		ObjSprite2D_SetSourceRect(obj2, 0, 0 + (j*2.34), 320, 320 + (j*2.34));
		
		x = 6; y = 6;
		ObjSprite3D_SetSourceRect(cloud, 0, 0 + (j*4) % 640, 320*x, 320*y + (j*4) % 640);
		ObjRender_SetAngleY(obj2, sin(i)*2);
		
		if(GetCommonData("op", false) == true){
			eAng += 0.5; 
			if(eAng > 144){if(t){ascend(cloud, 2, 150); ascend(obj2, 2, 120); t = false;}}
			if(eAng > 168){eAng = 168;}
		}
		else{eAng = 168; ObjRender_SetAlpha(cloud, 150); ObjRender_SetAlpha(obj2, 120);}
		
		
		i-=2.7; j -= 1/3;
		yield;
	} 
	
	task ascend(me, n, tt){
		let eAl = 0;
		if(GetCommonData("op", false) == true){
			while(eAl < tt){
				ObjRender_SetAlpha(me, eAl);
				eAl += n; 
				yield;
			}
		}
	}
	
	Obj_Delete(obj1);
	Obj_Delete(obj2); 
	Obj_Delete(cloud); 
	SetCommonData("op", false);
}

task TSpellBackground{
    let non = GetCurrentScriptDirectory() ~ "img/bass.jpg";
	let moon = GetCurrentScriptDirectory() ~ "img/dragon.jpg";

	let obj1 = ObjPrim_Create(OBJ_SPRITE_2D);
    ObjRender_SetBlendType(obj1, BLEND_ADD_ARGB);
	ObjRender_SetAlpha(obj1, 255);
    Obj_SetRenderPriority(obj1, 0.21);
    ObjPrim_SetTexture(obj1, non);
    ObjSprite2D_SetSourceRect(obj1,0,0,910,480);
	ObjSprite2D_SetDestCenter(obj1);
	ObjRender_SetScaleXYZ(obj1,640/910,512/480,1);
	ObjRender_SetX(obj1,GetStgFrameWidth()/2);
	ObjRender_SetY(obj1,GetStgFrameHeight()/2);
	
	let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
    ObjRender_SetBlendType(obj2, BLEND_MULTIPLY);
	ObjRender_SetAlpha(obj2, 200); ObjRender_SetColorHSV(obj2, 39, 0, 255);
    Obj_SetRenderPriority(obj2, 0.22);
    ObjPrim_SetTexture(obj2, moon);
    ObjSprite2D_SetSourceRect(obj2,0,0,626,626);
	ObjSprite2D_SetDestCenter(obj2);
	ObjRender_SetScaleXYZ(obj2,640/626,640/626,1);
	ObjRender_SetX(obj2,GetStgFrameWidth()/2);
	ObjRender_SetY(obj2,GetStgFrameHeight()/2);
	
	let i = 0; let j = 0;
	let k = 0;
	while(bSpell){
		Obj_SetVisible(obj1, true);
		Obj_SetVisible(obj2, true);
		
		ObjRender_SetColorHSV(obj2, 39, absolute(sin(k)*255), 255);
		ObjSprite2D_SetSourceRect(obj1, 0, 0 + (i*5), 910, 480 + (i*5));
		ObjSprite2D_SetSourceRect(obj2, 0 + sin(j)*100, 0, 626 + sin(j)*100, 626);
		i -= 0.3; j += 1/2; k += 1;
		yield;
	}
	
	while(!bSpell){
		Obj_SetVisible(obj1, false);
		Obj_SetVisible(obj2, false);
		yield;
    }
	
	Obj_Delete(obj1);
	Obj_Delete(obj2);
}