// By iVAwalys.

let CSD = GetCurrentScriptDirectory;

let scroll = 0;
let spell = false;
let alpha1 = 0;
let alpha2 = 0;
let camX = 0;

task Back1(locX,locY,locZ){
	let img1 = CSD ~ "./back_img/stg4bg3.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(obj, img1);
	ObjSprite3D_SetSourceRect(obj, 0,0,512*8,256*100);
	ObjSprite3D_SetDestRect(obj, 0,0,512*8,256*100);
	Obj_SetRenderPriorityI(obj, 20);
	ObjRender_SetScaleXYZ(obj, 1, 1, 0);
	ObjRender_SetAlpha(obj, 100);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetPosition(obj, locX,locY,locZ);
	ObjRender_SetAngleXYZ(obj, -90, 0, 0);
	ObjRender_SetColorHSV(obj, 50, 100, 200);

	/*let img1 = CSD ~ "./back_img/stage6a01.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(obj, img1);
	ObjSprite3D_SetSourceRect(obj, 0,0,512,128);
	ObjSprite3D_SetDestRect(obj, 0,0,512,128);
	Obj_SetRenderPriorityI(obj, 24);
	ObjRender_SetScaleXYZ(obj, 1, 1, 0);
	ObjRender_SetAngleXYZ(obj, 90, 0, 0);
	ObjRender_SetPosition(obj, locX, locY, locZ);
	ObjRender_SetAlpha(obj, 255);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);*/
}

task Back2(locX,locY,locZ){
	let img1 = CSD ~ "./back_img/stg4bg.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(obj, img1);
	ObjSprite3D_SetSourceRect(obj, 0,0,512*8,512*100);
	ObjSprite3D_SetDestRect(obj, 0,0,512*8,512*100);
	Obj_SetRenderPriorityI(obj, 21);
	ObjRender_SetScaleXYZ(obj, 1, 1, 0);
	ObjRender_SetAlpha(obj, 255);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	ObjRender_SetPosition(obj, locX,locY,locZ);
	ObjRender_SetAngleXYZ(obj, -90, 0, 0);
	ObjRender_SetColorHSV(obj, 100, 50, 200);
}

task Back3(locX,locY,locZ){
	let img1 = CSD ~ "./back_img/light4.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(obj, img1);
	ObjSprite3D_SetSourceRect(obj, 0,0,512*8,512*100);
	ObjSprite3D_SetDestRect(obj, 0,0,512*8,512*100);
	Obj_SetRenderPriorityI(obj, 22);
	ObjRender_SetScaleXYZ(obj, 1, 1, 0);
	ObjRender_SetAlpha(obj, 200);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	ObjRender_SetPosition(obj, locX,locY,locZ);
	ObjRender_SetAngleXYZ(obj, -90, 0, 0);
	ObjRender_SetColorHSV(obj, 60, 100, 255);
}

task Back4(locX, locY, locZ){
	let img = CSD ~ "./back_img/stg4bg77.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, img);
	Obj_SetRenderPriorityI(obj, 23);
	ObjSprite2D_SetSourceRect(obj, 0, 0, 512, 512);
	ObjSprite2D_SetDestRect(obj, 0, 0, 512, 512);
	ObjRender_SetScaleXYZ(obj, 1, 1, 0);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	ObjRender_SetAlpha(obj, 100);
	ObjRender_SetPosition(obj, locX, locY, locZ);
	ObjRender_SetColorHSV(obj, 60, 100, 200);
}

task Birb{
	let img1 = CSD ~ "./back_img/birb.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, img1);
	ObjSprite2D_SetSourceRect(obj, 0,0,449,299);
	ObjSprite2D_SetDestCenter(obj);
	Obj_SetRenderPriorityI(obj, 29);
	ObjRender_SetScaleXYZ(obj, 1, 1, 0);
	ObjRender_SetAlpha(obj, 175);
	ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
	ObjRender_SetPosition(obj, GetStgFrameWidth/2,GetStgFrameHeight/2, 0);
	ObjRender_SetAngleXYZ(obj, 0, 0, 0);

	let obj3 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj3, img1);
	ObjSprite2D_SetSourceRect(obj3, 0,0,449,299);
	ObjSprite2D_SetDestCenter(obj3);
	Obj_SetRenderPriorityI(obj3, 29);
	ObjRender_SetScaleXYZ(obj3, 1, 1, 0);
	ObjRender_SetAlpha(obj3, 100);
	ObjRender_SetBlendType(obj3, BLEND_ADD_ARGB);
	ObjRender_SetPosition(obj3, GetStgFrameWidth/2,GetStgFrameHeight/2, 0);
	ObjRender_SetAngleXYZ(obj3, 0, 0, 0);
	Shake(obj3);

	let img2 = CSD ~ "./back_img/darken.png";
	let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj2, img2);
	ObjSprite2D_SetSourceRect(obj2, 0,0,450,450);
	ObjSprite2D_SetDestCenter(obj2);
	Obj_SetRenderPriorityI(obj2, 28);
	ObjRender_SetScaleXYZ(obj2, 1, 1, 0);
	ObjRender_SetAlpha(obj2, 175);
	ObjRender_SetBlendType(obj2, BLEND_ALPHA);
	ObjRender_SetPosition(obj2, GetStgFrameWidth/2,GetStgFrameHeight/2, 0);
	ObjRender_SetAngleXYZ(obj2, 0, 0, 0);

	loop{
		if(GetCommonData("isBirb", 0) == 0){
			Obj_SetVisible(obj, false);
			Obj_SetVisible(obj2, false);
			Obj_SetVisible(obj3, false);
		}
		else{
			Obj_SetVisible(obj, true);
			Obj_SetVisible(obj2, true);
			Obj_SetVisible(obj3, true);
		}
		yield;
	}
}
task Shake(obj){
	loop{
		if(GetCommonData("isBirb", 0) == 1){
			ObjRender_SetPosition(obj, GetStgFrameWidth/2+rand(-10,10), GetStgFrameWidth/2+rand(-5,5), 0);
		}
		loop(5){yield;}
	}
}

task BackSpell1{
	let img1 = CSD ~ "./back_img/cdbg04b.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, img1);
	ObjSprite2D_SetSourceRect(obj, 0,0,450,450);
	ObjSprite2D_SetDestRect(obj, 0,0,450,450);
	Obj_SetRenderPriorityI(obj, 25);
	ObjRender_SetScaleXYZ(obj, 1, 1, 0);
	ObjRender_SetAlpha(obj, 0);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetPosition(obj, 0, 0, 0);
	ObjRender_SetAngleXYZ(obj, 0, 0, 0);
	ObjRender_SetColorHSV(obj, 40, 50, 150);
	
	let inc = 0;
	
	loop{
		ObjRender_SetAlpha(obj, alpha1);
		ObjSprite2D_SetSourceRect(obj, 0, 0 + (inc*1) % 450, 450, 450 + (inc*1) % 450); // Sparen is magic.
		inc++;
		yield;
	}
}

task BackSpell2{
	let img2 = CSD ~ "./back_img/cdbg04a.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, img2);
	ObjSprite2D_SetSourceRect(obj, 0,0,448,448);
	ObjSprite2D_SetDestRect(obj, -1,0,450,448); // DON'T ASK.
	Obj_SetRenderPriorityI(obj, 24);
	ObjRender_SetScaleXYZ(obj, 1, 1, 0);
	ObjRender_SetAngleXYZ(obj, 0, 0, 0);
	ObjRender_SetPosition(obj, 0, 0, 0);
	ObjRender_SetAlpha(obj, 0);
	ObjRender_SetColorHSV(obj, 0, 0, 200);
	
	loop{
		ObjRender_SetAlpha(obj, alpha2);
		//if(GetCommonData("isBirb", 0) == 1){ ObjRender_SetColorHSV(obj, 0, 0, 100); }
		//else{ ObjRender_SetColorHSV(obj, 0, 0, 200); }
		yield;
	}
}

function makeFogBG(locX, locY, locZ, red, green, blue){
	let texFogBG = CSD ~ "./back_img/fog.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(obj, texFogBG);
	Obj_SetRenderPriorityI(obj, 20);
	ObjSprite3D_SetSourceDestRect(obj, 0, 0, 4096, 4096);
	ObjRender_SetScaleXYZ(obj, 1, 1, 0);
	ObjRender_SetZWrite(obj, true);
	ObjRender_SetPosition(obj, locX, locY, locZ);
	ObjSprite3D_SetBillboard(obj, true);
}

@Initialize{
	BackgroundCreate;
	ScrollStage;
	BackSpell2;
	BackSpell1;
	Birb;
}

@MainLoop{
	scroll += 2;
	camX = 256*4;
	spell = (GetEnemyBossSceneObjectID != ID_INVALID && ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID, INFO_IS_SPELL));
	if(spell){
		if(alpha1 < 175){
			alpha1=alpha1+1;
		}
		if(alpha2 < 255){
			alpha2=alpha2+4;
		}
	}
	else{
		if(alpha1 > 0){
			alpha1=alpha1-1;
		}
		if(alpha2 > 0){
			alpha2=alpha2-4;
		}
	}
	yield;
}

task BackgroundCreate{
	SetFogEnable(true);	
	//makeFogBG(0,0,512*10,0,0,255);
	SetFogParam(512*4, 512*8, 200, 255, 255);
	
	Back1(-128*5,-90,256*5);

	Back2(-128*5,90*3,256*5);

	Back4(0, 0, 512);

	//Back2(-128*5,90*3,256*2);
	//Back2(128*2,90,256*2);

	//Back3(-128*5,0,256*2);

	//Back2(-128,90*3,256*2);
	//Back2(128*2,90*3,256*2);

	//ascent(i in -2..6){ loop(2){ Back3(-128,0,512*i); } }
	
}

task ScrollStage{
	SetCameraPerspectiveClip(0, 420);
	SetCameraAzimuthAngle(90);
	SetCameraElevationAngle(120);
	SetCameraRadius(450);
	SetCameraFocusX(128);
	SetCameraFocusY(176);
	SetCameraFocusZ(0);

	let a = 0;
	
	loop{
		SetCameraFocusZ(scroll*1);

		SetCameraAzimuthAngle(90+2*cos(a/5));
		a++;
		if(scroll > 512/1){
			scroll = 0;
		}
		yield;
	}
}