let bSpell = true;

let GCSD = GetCurrentScriptDirectory();
let huge = GCSD ~ "img/path.jpg"; let pan = GCSD ~ "img/fog01.png";
let anx = GCSD ~ "img/sorry.jpg";

let hArr = [1300, 1300]; let mArr = [512, 512];
let fogColor = [31, 147, 89]; let fogLimit = 1200;
let fogSpeed = 2.7;
@Initialize{
	SetCameraAzimuthAngle(-90); 
	LoadTexture(pan); LoadTexture(huge); LoadTexture(anx);
	SetFogParam(0, fogLimit, fogColor[0], fogColor[1], fogColor[2]);
	SetCameraElevationAngle(10 + sin(45)*50); 
	
	TCallBg;
}

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

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

task TNormalBackground{
	
	let x = 1; let y = 1;
	
	// --------------------------------------------------------- //
	let obj1 = ObjPrim_Create(OBJ_SPRITE_3D);
  ObjRender_SetBlendType(obj1, BLEND_ALPHA);
	ObjRender_SetAlpha(obj1, 80);
  Obj_SetRenderPriority(obj1, 0.212);
  ObjPrim_SetTexture(obj1, huge);
  ObjSprite3D_SetSourceRect(obj1, 0, 0, hArr[0]*x, hArr[1]*y);
	ObjSprite3D_SetDestRect(obj1, -hArr[0]/2*x, -hArr[1]/2*y, hArr[0]/2*x, hArr[1]/2*y);
	ObjRender_SetAngleX(obj1, 90+10);
	ObjRender_SetY(obj1, 56);

	let frame = 0; 
	let v = fogSpeed; let i = 0;
	// --------------------------------------------------------- //
	
	while(!bSpell){
		Obj_SetVisible(obj1, true); 

		if(GetCommonData("nop", true)){i = (i < 200) ? i : 200;}
		else{i = 200;}
		SetFogParam(i, fogLimit, fogColor[0], fogColor[1], fogColor[2]);
		
		ObjSprite3D_SetSourceRect(obj1, 0 + (frame*0.5), 0 + (frame*2), hArr[0] + (frame*0.5), hArr[1] + (frame*2));
		frame += v; 
		
		i += 4.6;
		yield;
	} 
	
	Obj_Delete(obj1); SetCommonData("nop", false);
}

task TSpellBackground{
	let x = 1; let y = 1;
	
	// --------------------------------------------------------- //
	
	let objA = ObjPrim_Create(OBJ_SPRITE_2D);
  	ObjRender_SetBlendType(objA, BLEND_ALPHA);
		ObjRender_SetAlpha(objA, 255);
  	Obj_SetRenderPriority(objA, 0.21);
  	ObjPrim_SetTexture(objA, pan);
		ObjSprite2D_SetSourceRect(objA, 0, 0, 1023, 511);
		ObjSprite2D_SetDestCenter(objA);
		ObjRender_SetScaleXYZ(objA, 640/1023, 480/511, 1);
		ObjRender_SetX(objA, GetStgFrameWidth()/2);
		ObjRender_SetY(objA, GetStgFrameHeight()/2);
		ObjRender_SetAngleZ(objA, ObjRender_GetAngleZ(objA) + 180);
		

	let objB = ObjPrim_Create(OBJ_SPRITE_2D);
  	ObjRender_SetBlendType(objB, BLEND_ALPHA);
		ObjRender_SetAlpha(objB, 255);
  	Obj_SetRenderPriority(objB, 0.21);
  	ObjPrim_SetTexture(objB, pan);
		ObjSprite2D_SetSourceRect(objB, 0, 0, 1023, 511);
		ObjSprite2D_SetDestCenter(objB);
		ObjRender_SetScaleXYZ(objB, 640/1023, 480/511, 1);
		ObjRender_SetX(objB, GetStgFrameWidth()/2);
		ObjRender_SetY(objB, GetStgFrameHeight()/2);
		
	
	let frame = 0; 
	let xpos = 0; let ypos = 0;
	// --------------------------------------------------------- //
	ObjRender_SetColor(objB, 6, 70, 6); ObjRender_SetColor(objA, 71, 6, 71);
	
	let j = 0; let k = 0; let l = 0; let rate;
	while(bSpell){
		Obj_SetVisible(objA, true); Obj_SetVisible(objB, true);

		j = ((j > 55) ? 55 : j); l = ((l > 33) ? 33 : l);
		ObjRender_SetAlpha(objA, j); ObjRender_SetAlpha(objB, l); j++; l++;
		ObjSprite2D_SetSourceRect(objA, 0 + (xpos), 0, 500 + (xpos), 511);
		ObjSprite2D_SetSourceRect(objB, 0 + (xpos), 0, 425 + (xpos), 511);

		k = ((k > 120) ? 120 : k);
		rate = Interpolate_Smooth(0, 4, k/123); xpos += rate; k++;
		yield;
	}
	
	Obj_Delete(objA); Obj_Delete(objB);
}

task TSpellBackground2{
	let x = 1; let y = 1;
	
	// --------------------------------------------------------- //
	
	let objA = ObjPrim_Create(OBJ_SPRITE_2D);
  	ObjRender_SetBlendType(objA, BLEND_ALPHA);
		ObjRender_SetAlpha(objA, 255);
  	Obj_SetRenderPriority(objA, 0.208);
  	ObjPrim_SetTexture(objA, anx);
		ObjSprite2D_SetSourceRect(objA, 0, 0, 625, 625);
		ObjSprite2D_SetDestCenter(objA);
		ObjRender_SetScaleXYZ(objA, 640/625, 480/625, 1);
		ObjRender_SetX(objA, GetStgFrameWidth()/2);
		ObjRender_SetY(objA, GetStgFrameHeight()/2);
		ObjRender_SetAngleZ(objA, ObjRender_GetAngleZ(objA) + 180);
	
	let frame = 0; 
	let xpos = 0; let ypos = 0;
	// --------------------------------------------------------- //
	
	let j = 0; let k = 0; let rate;
	while(bSpell){
		Obj_SetVisible(objA, true); 

		j = ((j > 15) ? 15 : j); 
		ObjRender_SetAlpha(objA, j); j++;
		ObjSprite2D_SetSourceRect(objA, 0, 0 + ypos, 625, 625 + ypos);

		k = ((k > 120) ? 120 : k);
		rate = Interpolate_Smooth(0, 3.3, k/123); ypos += rate; k++;
		yield;
	}
	
	Obj_Delete(objA);
}