//AJS and vanilla
let CSD = GetCurrentScriptDirectory;
let objScene = GetEnemyBossSceneObjectID;
let bSpell = false;

@Initialize{
	SetCameraFocusX(0);
	SetCameraFocusY(0);
	SetCameraFocusZ(0);
	SetCameraRadius(800);
    SetCameraElevationAngle(-10);
	SetCameraAzimuthAngle(90);
	
	bgcontrol;
	TSpellBackground;
}


@MainLoop
{

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

	yield;
}


task bgcontrol{
Background;
}

task Background{
	let pos = 0;
	let pz = (pos-1) * 1000;
	let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj2, BLEND_ALPHA);
	Obj_SetRenderPriority(obj2, 0.23);
	ObjPrim_SetTexture(obj2,CSD~"./Functions/img/back.png");
	ObjSprite2D_SetDestRect(obj2, 0, 0, 384, 448);
		let frameInvisible = 0;
	loop{
		if(bSpell)
		{
			frameInvisible++;
			if(frameInvisible >= 60)
			{
				Obj_SetVisible(obj2, false);
			}
		}
		else
		{
		frameInvisible = 0;
		Obj_SetVisible(obj2, true);
		}
	ObjSprite2D_SetSourceRect(obj2, 0, 0, 508, 448);
		pz++;
		if(pz > 1000){pz -= 3000;}
		yield;
	}
}

task TSpellBackground
{
	let path1 = CSD~"./Functions/img/main.png";
	let obj1 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj1, BLEND_ADD_RGB);
	Obj_SetRenderPriority(obj1, 0.22);
	ObjPrim_SetTexture(obj1, path1);
	ObjSprite2D_SetDestRect(obj1, 0, 0, 384, 448);

	let path2 = CSD~"./Functions/img/sub.png";
	let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj2, BLEND_ALPHA);
	Obj_SetRenderPriority(obj2, 0.23);
	ObjPrim_SetTexture(obj2, path2);
	ObjSprite2D_SetDestRect(obj2, 0, 0, 384, 448);

	let u1 = 0;
	let v1 = 0;
	let u2 = 0;
	let v2 = 0;
	let su2 = 0;
	let sv2 = 0;
	let alpha = 0;
	let frame = 0;
	loop
	{
		if(bSpell)
		{
			alpha += 4;
		}
		else
		{
			alpha = 0;
		}

		v1 -= 1;
		if(v1 < 256){v1+=256;}
		u2 += su2;
		if((su2 > 0 && u2 > 256) || (su2 < 0 && u2 < -256))
		{
			u2=0;
			su2 = rand(0.25, 1);
			if(rand(0, 1) < 0.5){su2 *= -1;}
		}
		v2 += sv2;
		if((sv2 > 0 && v2 > 256) || (sv2 < 0 && v2 < -256))
		{
			v2 = 0;
			sv2 = rand(0.25, 1);
			if(rand(0, 1) < 0.5){sv2 *= -1;}
		}


		ObjRender_SetAlpha(obj1, 55);
		Obj_SetVisible(obj1, alpha > 0);
		ObjSprite2D_SetSourceRect(obj1, u1, v1, u1 - 255, v1 - 255);

		Obj_SetVisible(obj2, alpha > 0);
		ObjRender_SetAlpha(obj2, 255);
		ObjSprite2D_SetSourceRect(obj2, u2, v2, 384, 448);
		frame++;
		yield;
	}

}

function wait(w) { loop(w) { yield; } } 