// By iVAwalys.

let CSD = GetCurrentScriptDirectory;

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

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 BackStars{
	let img2 = CSD ~ "./back_img/stg3bg1.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 BackGrid1(locX,locY,locZ){
	let img2 = CSD ~ "./back_img/stg3bg2.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(obj, img2);
	ObjSprite3D_SetSourceRect(obj, 0,0,512,512);
	ObjSprite3D_SetDestRect(obj, 0,0,512,512);
	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 BackGrid2(locX,locY,locZ){
	let img2 = CSD ~ "./back_img/stg3bg3.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(obj, img2);
	ObjSprite3D_SetSourceRect(obj, 0,0,512,512);
	ObjSprite3D_SetDestRect(obj, 0,0,512,512);
	Obj_SetRenderPriorityI(obj, 22);
	ObjRender_SetScaleXYZ(obj, 1, 1, 0);
	ObjRender_SetAngleXYZ(obj, 90, 0, 0);
	ObjRender_SetPosition(obj, locX, locY, locZ);
	ObjRender_SetAlpha(obj, 200);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	//ObjRender_SetColorHSV(obj,0,20,50);
}

task THEMOON{
	let img2 = CSD ~ "./back_img/stg3bg4.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, img2);
	ObjSprite2D_SetSourceRect(obj, 0,0,512,512);
	ObjSprite2D_SetDestRect(obj, 0,0,512,512);
	Obj_SetRenderPriorityI(obj, 22);
	ObjRender_SetScaleXYZ(obj, 1, 1, 0);
	ObjRender_SetAngleXYZ(obj, 0, 0, 0);
	ObjRender_SetPosition(obj, -30, -80, 0);
	ObjRender_SetAlpha(obj, 255);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	//ObjRender_SetColorHSV(obj,0,20,50);
}


task BackSpell1(locX, locY, locZ){
	let img1 = CSD ~ "./back_img/cdbg03a.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.001, 1.001, 0);
	ObjRender_SetAlpha(obj, 0);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetAngleXYZ(obj, 180, 180, 0);
	ObjRender_SetPosition(obj, locX, locY, locZ);

	let inc = 0;
	
	loop{
		ObjRender_SetAlpha(obj, alpha1);
		ObjSprite2D_SetSourceRect(obj, 0, 0 + (inc*1) % 512, 512, 512 + (inc*1) % 512); // Sparen is magic.
		inc++;
		yield;
	}
}

task BackSpell2{
	let spin = 0;
	
	let img2 = CSD ~ "./back_img/cdbg03b.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, img2);
	ObjSprite2D_SetSourceRect(obj, 0,0,256,256);
	ObjSprite2D_SetDestCenter(obj);
	Obj_SetRenderPriorityI(obj, 26);
	ObjRender_SetScaleXYZ(obj, 5, 5, 0);
	ObjRender_SetAngleXYZ(obj, 0, 0, 0);
	ObjRender_SetPosition(obj, cx, cy, 0);
	ObjRender_SetBlendType(obj,BLEND_SUBTRACT);
	ObjRender_SetAlpha(obj, 0);
	
	loop{
		ObjRender_SetAlpha(obj, alpha2);
		ObjRender_SetAngleXYZ(obj,0,0,spin);
		
		spin=spin-0.5;
		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;
	BackSpell2;
	BackStars;
	RenderStage;
}

@MainLoop{
	let splmove = 0;
	splmove++;
	spell = (GetEnemyBossSceneObjectID != ID_INVALID && ObjEnemyBossScene_GetInfo(GetEnemyBossSceneObjectID, INFO_IS_SPELL));
	if(spell){
		if(alpha1 < 255){
			alpha1=alpha1+4;
		}
		if(alpha2 < 255){
			alpha2=alpha2+2;
		}
		camX = 124;
			//SetCameraElevationAngle(-20);
			if(splmove > 512){
				splmove = 0;
			}
	}
	else{
		if(alpha1 > 0){
			alpha1=alpha1-4;
		}
		if(alpha2 > 0){
			alpha2=alpha2-2;
		}
		camX = 256;
	}
	yield;
}

task RenderStage{
	//SetFogEnable(true);
	
	SetFogParam(512*3,512*6,0,0,0);

	BackSpell1(512, 0, 0);
	BackSpell1(512, 512, 0);

	THEMOON;

	ascent(i in 0..7){
		ascent(j in -3..3){
			BackGrid1(256*(j*2), -256, 512*i);
			BackGrid1(-256*(j*2), 512, 512*i);
		}
	}

	ascent(i in 0..7){
		ascent(j in -3..3){
			BackGrid2(256*(j*2), -256-128, 512*i);
			BackGrid2(-256*(j*2), 512+128, 512*i);
		}
	}

	//Fog.
	//makeFogBG(0,0,2048,255,255,255);
}

task ScrollStage{
	let scroll = 0;
	let glide = 0;
	let roll = 0;

	SetCameraPerspectiveClip(16, 512*6);
	SetCameraAzimuthAngle(-90-glide);
	SetCameraYaw(0);
	SetCameraElevationAngle(0);
	SetCameraRadius(120);
	SetCameraFocusX(0);
	SetCameraFocusY(0);
	SetCameraFocusZ(0);
	
	loop{
		roll++;
		scroll++;
		glide++;
		SetCameraFocusX(0+90*sin(roll*1));
		SetCameraFocusY(140+90*sin(roll*1));
		SetCameraFocusZ(scroll*18);
		SetCameraRoll(roll*0.1);
		if(scroll*18 > 512){
			scroll = 0;
		}
		yield;
	}
	/*loop{
		SetCameraFocusY(-20+glide);
		if(glide == 256){
			glide = 0;
		}
		yield;
	}*/
}