//Credits to Ultima and Miransu for the help!

let objScene = GetEnemyBossSceneObjectID;
let CSD = GetCurrentScriptDirectory;
let OnSpell = false;

@Initialize
{
	BGControl;
}

@MainLoop
{
	OnSpell = (GetEnemyBossSceneObjectID != ID_INVALID && ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID, INFO_IS_SPELL));
	yield; 
}

task BGControl
{
	Background;
	SpellBG
}

task Background()
{
	SetFogEnable(true);

	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	let objTWO = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, CSD ~ "./Stgbg.png");
	ObjPrim_SetTexture(objTWO, CSD ~ "./Stgbg TWO.png");
	
	//sets the position of the sprite to the center of the playing field
	ObjRender_SetPosition(obj,0,-194,0);
	Obj_SetRenderPriorityI(obj, 23);
	SetCameraRadius(300);
	SetCameraAzimuthAngle(90);
	SetCameraElevationAngle(-75);

	SetFogParam(270, 325, 0, 0, 0);

	ObjRender_SetPosition(objTWO,244,174,0);
	Obj_SetRenderPriorityI(objTWO, 22);
	
	let mx = 0; //variabled used to scroll the image
	
	while(true)
	{
		//Sets the viewing recting of the sprite using the given pixels of the texture
		//to show. The arguments refer to the sides of the texture
		// to scroll a bg downward, add the mx value to the top and bottom arguments
		
		ObjSprite3D_SetSourceDestRect(obj, 0, 0 + mx, 500, 500 + mx);
		ObjSprite2D_SetSourceRect(objTWO, 0, 0, 488, 488);
		ObjSprite2D_SetDestCenter(objTWO);
		
		//Sets the origin of the sprite to the center based on the source rectangle you
		//defined above
		
		//Increment the mx value each frame to make the bg scroll
		mx-=1;
		yield;
	}
	//Once you change the above conditional statement so that it can fail
	//at some point in time (when the boss dies for example)
	// it will continue to this line which deletes the object
	Obj_Delete(obj);
}

task SpellBG
{
	let obj1 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj1, BLEND_ALPHA);
	Obj_SetRenderPriorityI(obj1, 23);
	ObjPrim_SetTexture(obj1, GetCurrentScriptDirectory~"./Spellbg.png");
	ObjRender_SetScaleXYZ(obj1, 2, 2, 2);
	ObjSprite2D_SetSourceRect(obj1, 0, 0, 256, 256);
	ObjSprite2D_SetDestRect(obj1, -256, -256, 256, 256);

	let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj2, BLEND_ALPHA);
	Obj_SetRenderPriorityI(obj2, 23);
	ObjPrim_SetTexture(obj2, GetCurrentScriptDirectory~"./Spellbg TWO.png");
	ObjRender_SetScaleXYZ(obj2, 2, 2, 2);
	ObjSprite2D_SetSourceRect(obj2, 0, 0, 256, 256);
	ObjSprite2D_SetDestRect(obj2, -256, -256, 256, 256);

	let ang = 0;
	let ang2 = 0;
	let ang3 = rand(0,360);
	let x = 0;
	let y = 0;
	let mx = 0;
	let mx2 = 0;
	let alpha = 0;
	loop
	{
		if(OnSpell)
		{
			if(alpha < 180)
			{
				alpha += 2;
			}
		}
		else
		{
			alpha = 0;
		}

		ang3+=sin(ang)/6; ang3%=360;
		ObjRender_SetAlpha(obj1, alpha);
		
		ObjSprite2D_SetSourceRect(obj1, 0, 0+mx, 488, 488+mx);
		ObjSprite2D_SetDestCenter(obj1);
		ObjRender_SetPosition(obj1, GetStgFrameWidth/2, GetStgFrameHeight/2, 0);
		ObjRender_SetScaleXYZ(obj1, 1, 1, 0);

		ObjRender_SetAlpha(obj2, alpha*2);
		
		ObjSprite2D_SetSourceRect(obj2, 0, 0+mx2, 488, 488+mx2);
		ObjSprite2D_SetDestCenter(obj2);
		ObjRender_SetPosition(obj2, GetStgFrameWidth/2, GetStgFrameHeight/2, 0);
		ObjRender_SetScaleXYZ(obj2, 2, 2, 0);

		ang+=0.5;
		mx-=1;
		mx2-=2;
		yield;
	}
}