// By iVAwalys.

let CSD = GetCurrentScriptDirectory;

let cx = GetStgFrameWidth() / 2;
let cy = GetStgFrameHeight() / 2;

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

function Back1(locX, locY, locZ){
	let texFloor = CSD ~ "./back_img/stg4bg.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(obj, texFloor);
	Obj_SetRenderPriorityI(obj, 22);
	ObjSprite3D_SetSourceDestRect(obj, 0, 0, 256, 256);
	ObjRender_SetScaleXYZ(obj, 1, 1, 0);
	ObjRender_SetZWrite(obj, true);
	
	ObjRender_SetAngleXYZ(obj, 90, 0, 0);
	
	ObjRender_SetPosition(obj, locX, locY, locZ);
}

task Back2{
	let img2 = CSD ~ "./back_img/stg7bg2.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, img2);
	ObjSprite2D_SetSourceRect(obj, 0,0,512,512);
	ObjSprite2D_SetDestCenter(obj);
	Obj_SetRenderPriorityI(obj, 21);
	ObjRender_SetScaleXYZ(obj, 1, 1, 0);
	ObjRender_SetAngleXYZ(obj, 0, 0, 0);
	ObjRender_SetPosition(obj, cx, cy, 0);
	ObjRender_SetAlpha(obj, 195);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
}

task BackBamboo(locX,locY,locZ,angle){
	let img2 = CSD ~ "./back_img/stg4bg.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(obj, img2);
	ObjSprite3D_SetSourceRect(obj, 495,0,512,512);
	ObjSprite3D_SetDestRect(obj, 0,0,17,512);
	Obj_SetRenderPriorityI(obj, 24);
	ObjRender_SetScaleXYZ(obj, 1, 1, 0);
	ObjRender_SetAngleXYZ(obj, angle, 0, 0);
	ObjRender_SetPosition(obj, locX, locY, locZ);
	ObjRender_SetAlpha(obj, 225);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
}

task BackBamboo2(locX,locY,locZ){
	let img2 = CSD ~ "./back_img/bamboo_wall.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(obj, img2);
	ObjSprite3D_SetSourceRect(obj, 0,0,850,512);
	ObjSprite3D_SetDestRect(obj, 0,0,850,512);
	Obj_SetRenderPriorityI(obj, 22);
	ObjRender_SetScaleXYZ(obj, 1.5, 1, 0);
	ObjRender_SetAngleXYZ(obj, 0, 90, 0);
	ObjRender_SetPosition(obj, locX, locY, locZ);
	ObjRender_SetAlpha(obj, 255);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetColorHSV(obj,0,20,50);
}

task BackLeaves(locX,locY,locZ,angle){
	let img2 = CSD ~ "./back_img/stg4bg.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(obj, img2);
	ObjSprite3D_SetSourceRect(obj, 292,512,220,256);
	ObjSprite3D_SetDestRect(obj, 0,0,290,256);
	Obj_SetRenderPriorityI(obj, 23);
	ObjRender_SetScaleXYZ(obj, 1, 2, 0);
	ObjRender_SetAngleXYZ(obj, 0, angle, 0);
	ObjRender_SetPosition(obj, locX, locY, locZ);
	ObjRender_SetAlpha(obj, 175);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
}


task BackSpell1(type, locX, locY, locZ){
	let img1 = CSD ~ "./back_img/02_01.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, img1);
	ObjSprite2D_SetSourceRect(obj, 0,0,512,512);
	ObjSprite2D_SetDestRect(obj, 0,0,512,512);
	Obj_SetRenderPriorityI(obj, 25);
	ObjRender_SetScaleXYZ(obj, 1, 1, 0);
	ObjRender_SetAlpha(obj, 0);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	if(type == 1){ObjRender_SetAngleXYZ(obj, 0, 180, 0);}
	else if(type == 2){ObjRender_SetAngleXYZ(obj, 0, 180, 0);}
	ObjRender_SetColor(obj, 255, 215, 125);
	ObjRender_SetPosition(obj, locX, locY, locZ);
	
	loop{
		ObjRender_SetAlpha(obj, alpha1);

		if(type == 1){ ObjRender_SetY(obj, ObjRender_GetY(obj)+1); }
		else if(type == 2){ ObjRender_SetY(obj, ObjRender_GetY(obj)-1); }
		yield;
	}
}

task BackSpell2{
	let spin = 0;
	
	let img2 = CSD ~ "./back_img/02_00.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, img2);
	ObjSprite2D_SetSourceRect(obj, 0,0,426,488);
	ObjSprite2D_SetDestCenter(obj);
	Obj_SetRenderPriorityI(obj, 26);
	ObjRender_SetScaleXYZ(obj, 1.2, 1, 0);
	ObjRender_SetAngleXYZ(obj, 0, 0, 0);
	ObjRender_SetPosition(obj, cx, cy, 0);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetAlpha(obj, 0);
	ObjRender_SetColorHSV(obj, 0, 255, 255);
	
	loop{
		ObjRender_SetAlpha(obj, alpha2);
		//ObjRender_SetAngleXYZ(obj,0,0,spin);
		
		spin=spin-1;
		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{
	ScrollStage;
	//BackSpell1(512, 512, 0);
	BackSpell2;
	Back2;
	RenderStage;
	SpellScroll;
}

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

task RenderStage{
	SetFogEnable(true);
	
	SetFogParam(150,850,0,0,0);
	
	// Floor.
	Back1(0,0,256*9);
	Back1(0,0,256*8);
	Back1(0,0,256*7);
	Back1(0,0,256*6);
	Back1(0,0,256*5);
	Back1(0,0,256*4);
	Back1(0,0,256*3);
	Back1(0,0,256*2);
	Back1(0,0,256);
	Back1(0,0,0);
	
	Back1(256,0,256*9);
	Back1(256,0,256*8);
	Back1(256,0,256*7);
	Back1(256,0,256*6);
	Back1(256,0,256*5);
	Back1(256,0,256*4);
	Back1(256,0,256*3);
	Back1(256,0,256*2);
	Back1(256,0,256);
	Back1(256,0,0);
	
	Back1(-256,0,256*9);
	Back1(-256,0,256*8);
	Back1(-256,0,256*7);
	Back1(-256,0,256*6);
	Back1(-256,0,256*5);
	Back1(-256,0,256*4);
	Back1(-256,0,256*3);
	Back1(-256,0,256*2);
	Back1(-256,0,256);
	Back1(-256,0,0);
	
	BackBamboo(110,-0,0,5);
	BackBamboo(120,-0,96,10);
	BackBamboo(134,-0,96*2,17);
	BackBamboo(121,-0,96*3,11);
	BackBamboo(113,-0,96*4,7);
	BackBamboo(110,-0,96*5,5);
	
	BackBamboo(110,-0,96*6,5);
	BackBamboo(120,-0,96*7,10);
	BackBamboo(134,-0,96*8,17);
	BackBamboo(121,-0,96*9,11);
	BackBamboo(113,-0,96*10,7);
	BackBamboo(110,-0,96*11,5);
	
	BackBamboo(110,-0,96*12,5);
	BackBamboo(120,-0,96*13,10);
	BackBamboo(134,-0,96*14,17);
	BackBamboo(121,-0,96*15,11);
	BackBamboo(113,-0,96*16,7);
	BackBamboo(110,-0,96*17,5);
	
	BackBamboo(-110,-0,0,5);
	BackBamboo(-120,-0,96,10);
	BackBamboo(-134,-0,96*2,17);
	BackBamboo(-121,-0,96*3,11);
	BackBamboo(-113,-0,96*4,7);
	BackBamboo(-110,-0,96*5,5);
	
	BackBamboo(-110,-0,96*6,5);
	BackBamboo(-120,-0,96*7,10);
	BackBamboo(-134,-0,96*8,17);
	BackBamboo(-121,-0,96*9,11);
	BackBamboo(-113,-0,96*10,7);
	BackBamboo(-110,-0,96*11,5);
	
	BackBamboo(-110,-0,96*12,5);
	BackBamboo(-120,-0,96*13,10);
	BackBamboo(-134,-0,96*14,17);
	BackBamboo(-121,-0,96*15,11);
	BackBamboo(-113,-0,96*16,7);
	BackBamboo(-110,-0,96*17,5);
	
	BackLeaves(0,422,0,0);
	BackLeaves(0,382,92,0);
	BackLeaves(0,372,92*2,0);
	BackLeaves(0,362,92*3,0);
	BackLeaves(0,382,92*4,0);
	BackLeaves(0,422,92*5,0);
	
	BackLeaves(0,422,92*6,0);
	BackLeaves(0,382,92*7,0);
	BackLeaves(0,372,92*8,0);
	BackLeaves(0,362,92*9,0);
	BackLeaves(0,382,92*10,0);
	BackLeaves(0,422,92*11,0);
	
	BackLeaves(0,422,92*12,0);
	BackLeaves(0,382,92*13,0);
	BackLeaves(0,372,92*14,0);
	BackLeaves(0,362,92*15,0);
	BackLeaves(0,382,92*16,0);
	BackLeaves(0,422,92*17,0);
	
	BackLeaves(0,422,96*18,0);
	BackLeaves(0,382,96*19,0);
	BackLeaves(0,372,96*20,0);
	BackLeaves(0,362,96*21,0);
	BackLeaves(0,382,96*22,0);
	BackLeaves(0,422,96*23,0);
	
	BackLeaves(0,422,0,180);
	BackLeaves(0,382,96,180);
	BackLeaves(0,372,96*2,180);
	BackLeaves(0,362,96*3,180);
	BackLeaves(0,382,96*4,180);
	BackLeaves(0,422,96*5,180);
	
	BackLeaves(0,422,96*6,180);
	BackLeaves(0,382,96*7,180);
	BackLeaves(0,372,96*8,180);
	BackLeaves(0,362,96*9,180);
	BackLeaves(0,382,96*10,180);
	BackLeaves(0,422,96*11,180);
	
	BackLeaves(0,422,96*12,180);
	BackLeaves(0,382,96*13,180);
	BackLeaves(0,372,96*14,180);
	BackLeaves(0,362,96*15,180);
	BackLeaves(0,382,96*16,180);
	BackLeaves(0,422,96*17,180);
	
	BackLeaves(0,422,96*18,180);
	BackLeaves(0,382,96*19,180);
	BackLeaves(0,372,96*20,180);
	BackLeaves(0,362,96*21,180);
	BackLeaves(0,382,96*22,180);
	BackLeaves(0,422,96*23,180);
	
	BackLeaves(0,222,0,0);
	BackLeaves(0,182,96,0);
	BackLeaves(0,172,96*2,0);
	BackLeaves(0,162,96*3,0);
	BackLeaves(0,182,96*4,0);
	BackLeaves(0,222,96*5,0);
	
	BackLeaves(0,222,96*6,0);
	BackLeaves(0,182,96*7,0);
	BackLeaves(0,172,96*8,0);
	BackLeaves(0,162,96*9,0);
	BackLeaves(0,182,96*10,0);
	BackLeaves(0,222,96*11,0);
	
	BackLeaves(0,222,96*12,0);
	BackLeaves(0,182,96813,0);
	BackLeaves(0,172,96*14,0);
	BackLeaves(0,162,96*15,0);
	BackLeaves(0,182,96*16,0);
	BackLeaves(0,222,96*17,0);
	
	BackLeaves(0,222,96*18,0);
	BackLeaves(0,182,96*19,0);
	BackLeaves(0,172,96*20,0);
	BackLeaves(0,162,96*21,0);
	BackLeaves(0,182,96*22,0);
	BackLeaves(0,222,96*23,0);
	
	BackLeaves(0,222,0,180);
	BackLeaves(0,182,96,180);
	BackLeaves(0,172,96*2,180);
	BackLeaves(0,162,96*3,180);
	BackLeaves(0,182,96*4,180);
	BackLeaves(0,222,96*5,180);
	
	BackLeaves(0,222,96*6,180);
	BackLeaves(0,182,96*7,180);
	BackLeaves(0,172,96*8,180);
	BackLeaves(0,162,96*9,180);
	BackLeaves(0,182,96*10,180);
	BackLeaves(0,222,96*11,180);
	
	BackLeaves(0,222,96*12,180);
	BackLeaves(0,182,96813,180);
	BackLeaves(0,172,96*14,180);
	BackLeaves(0,162,96*15,180);
	BackLeaves(0,182,96*16,180);
	BackLeaves(0,222,96*17,180);
	
	BackLeaves(0,222,96*18,180);
	BackLeaves(0,182,96*19,180);
	BackLeaves(0,172,96*20,180);
	BackLeaves(0,162,96*21,180);
	BackLeaves(0,182,96*22,180);
	BackLeaves(0,222,96*23,180);
	
	BackLeaves(0,122,0,0);
	BackLeaves(0,82,96,0);
	BackLeaves(0,72,96*2,0);
	BackLeaves(0,62,96*3,0);
	BackLeaves(0,82,96*4,0);
	BackLeaves(0,122,96*5,0);
	
	BackLeaves(0,122,96*6,0);
	BackLeaves(0,82,96*7,0);
	BackLeaves(0,72,96*8,0);
	BackLeaves(0,62,96*9,0);
	BackLeaves(0,82,96*10,0);
	BackLeaves(0,122,96*11,0);
	
	BackLeaves(0,122,96*12,0);
	BackLeaves(0,82,96813,0);
	BackLeaves(0,72,96*14,0);
	BackLeaves(0,62,96*15,0);
	BackLeaves(0,82,96*16,0);
	BackLeaves(0,122,96*17,0);
	
	BackLeaves(0,122,96*18,0);
	BackLeaves(0,82,96*19,0);
	BackLeaves(0,72,96*20,0);
	BackLeaves(0,62,96*21,0);
	BackLeaves(0,82,96*22,0);
	BackLeaves(0,122,96*23,0);
	
	BackLeaves(0,122,0,180);
	BackLeaves(0,82,96,180);
	BackLeaves(0,72,96*2,180);
	BackLeaves(0,62,96*3,180);
	BackLeaves(0,82,96*4,180);
	BackLeaves(0,122,96*5,180);
	
	BackLeaves(0,122,96*6,180);
	BackLeaves(0,82,96*7,180);
	BackLeaves(0,72,96*8,180);
	BackLeaves(0,62,96*9,180);
	BackLeaves(0,82,96*10,180);
	BackLeaves(0,122,96*11,180);
	
	BackLeaves(0,122,96*12,180);
	BackLeaves(0,82,96813,180);
	BackLeaves(0,72,96*14,180);
	BackLeaves(0,62,96*15,180);
	BackLeaves(0,82,96*16,180);
	BackLeaves(0,122,96*17,180);
	
	BackLeaves(0,122,96*18,180);
	BackLeaves(0,82,96*19,180);
	BackLeaves(0,72,96*20,180);
	BackLeaves(0,62,96*21,180);
	BackLeaves(0,82,96*22,180);
	BackLeaves(0,122,96*23,180);
	
	BackBamboo2(140,-20,0);
	BackBamboo2(140,-20,1275);
	BackBamboo2(140,-20,1275*2);
	BackBamboo2(140,-20,1275*3);
	BackBamboo2(140,-20,1275*4);
	BackBamboo2(-140,-20,0);
	BackBamboo2(-140,-20,1275);
	BackBamboo2(-140,-20,1275*2);
	BackBamboo2(-140,-20,1275*3);
	BackBamboo2(-140,-20,1275*4);
	
	//Fog.
	makeFogBG(0,0,2048,255,255,255);
}

task ScrollStage{
	SetCameraPerspectiveClip(16, 1596);
	SetCameraAzimuthAngle(-90-glide);
	SetCameraElevationAngle(-20);
	SetCameraRadius(120);
	SetCameraFocusX(0);
	SetCameraFocusY(200);
	SetCameraFocusZ(-128);
	
	loop{
		SetCameraFocusZ(scroll);
		if(scroll > 576){
			scroll = 0;
		}
		yield;
	}
	loop{
		SetCameraFocusY(20+glide);
		if(glide == 256){
			glide = 0;
		}
		yield;
	}
}

task SpellScroll{
	loop{
		BackSpell1(2, 512, 0, 0);
		//BackSpell1(1, 512, 0, 0);
		BackSpell1(2, 512, 512, 0);
		//BackSpell1(1, 520, -512, 0);
		wait(512);
	}
}

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