let current = GetCurrentScriptDirectory();
let objScene = GetEnemyBossSceneObjectID;
let OnSpell = false;
#include"./../Spells/LoadGraphics.txt"

@Initialize{
	SetCameraFocusX(0);
	SetCameraFocusY(0);
	SetCameraFocusZ(0);
	SetCameraRadius(750);
	SetCameraElevationAngle(22);
	SetCameraAzimuthAngle(90);
	THiperbola; Fog;
	Autumnfall;
	TSunset;
	ascent(let pos in 0..3){
		TNormalBackground(pos);}}

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

task Autumnfall{
	let count = 0;
	loop{
		if(GetCommonData("BG2",0) != 0){
			loop(count){yield;}count+=0.2;
			if(count >= 5){count = 5;}}
		CreateLeaf(0,rand(-50,500),rand(-600,500));
		yield;}}

task CreateLeaf(x,y,z){
	let move = 0;
	let move2 = 0;
	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(obj,leaf);
	ObjSprite3D_SetSourceDestRect(obj,0,0,38,38);
	ObjRender_SetColor(obj,rand(255,180),rand(100,180),20);
	Obj_SetRenderPriorityI(obj,22);
	loop(240){
		if(OnSpell){Obj_SetVisible(obj,false);}
		else{Obj_SetVisible(obj,true);}
		ObjRender_SetAngleZ(obj,move);
		ObjRender_SetPosition(obj,x-600+move,y+250-move2,z-600+move);
		move+=6;move2+=2;yield;}
	Obj_Delete(obj);}

task Fog{
	let color = 120;
	SetFogParam(0,1500,250,color,0);
	loop(180){yield;}
	loop(120){
		SetFogParam(0,1500,250,color,0);
		color--;yield;}
	while(GetCommonData("BG",0) == 0){yield;}
	let weg = 0;
	let rgb = 0;
	loop(90){
		SetFogParam(weg,1500,rgb,0,0);
		weg-=10;rgb-=25/9;yield;}}

task TNormalBackground(pos){
	let e = 0; let y = 0;
	let pz = (pos-1) * 1000;
	let path = current ~ "stgbg.mqo";
	let obj = ObjMesh_Create();
	ObjMesh_Load(obj, path);
	Obj_SetRenderPriorityI(obj, 21);

	let frameInvisible = 0;
	loop{
		if(GetCommonData("BG3",0) != 0){
			loop(90){
			ObjRender_SetZ(obj, pz);
			SetCameraElevationAngle(22+e);
			e-=0.1;pz+=14;yield;}
			loop(180){
			ObjRender_SetZ(obj, pz);
			ObjRender_SetY(obj, y);
			SetCameraElevationAngle(22+e);
			e-=0.1;y-=0.5;pz+=14;yield;}
			break;}
		if(OnSpell){Obj_SetVisible(obj,false);}
		else{Obj_SetVisible(obj,true);}
		ObjRender_SetZ(obj, pz);
		pz+=14;
		if(pz > 1000){pz -= 3000;}
		yield;}
	Obj_Delete(obj);}


task THiperbola{ let alpha = 255;
	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(obj,current~"UN.png");
	ObjSprite3D_SetSourceDestRect(obj,0,-200,1200,1200);
	ObjSprite3D_SetBillboard(obj,true);
	Obj_SetRenderPriorityI(obj,21);
	ObjRender_SetPosition(obj,0,0,-900);
	loop{
		if(OnSpell){Obj_SetVisible(obj,false);}
		else{Obj_SetVisible(obj,true);}
		if(GetCommonData("BG",0) != 0){
			loop(120){yield;}
			ObjPrim_SetTexture(obj,black);
			while(GetCommonData("BG3",0) == 0){yield;}
			loop(120){
				ObjRender_SetAlpha(obj,alpha);
				alpha-=255/120;yield;}
			break;}
		yield;}
	Obj_Delete(obj);}

task TSunset{ let move = 0;
	while(GetCommonData("BG",0) == 0){yield;}
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj,bg1);
	ObjSprite2D_SetSourceRect(obj,0,0,3648,2736);
	ObjSprite2D_SetDestCenter(obj);
	Obj_SetRenderPriorityI(obj,20);
	ObjRender_SetPosition(obj,192,224,0);
	ObjRender_SetScaleXYZ(obj,0.2,0.2,0);
	while(GetCommonData("BG3",0) == 0){yield;}
	loop{
		ObjRender_SetY(obj,224+10*sin(move));
		if(OnSpell){Obj_SetVisible(obj,false);}
		else{Obj_SetVisible(obj,true);}
		move+=0.2;yield;}
	Obj_Delete(obj);}