// 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/stage02a.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(obj, img1);
	ObjSprite3D_SetSourceRect(obj, 0,0,512*3,128*100);
	ObjSprite3D_SetDestRect(obj, 0,0,512*3,128*100);
	Obj_SetRenderPriorityI(obj, 20);
	ObjRender_SetScaleXYZ(obj, 1, 1, 0);
	ObjRender_SetAlpha(obj, 210);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetPosition(obj, locX,locY,locZ);
	ObjRender_SetAngleXYZ(obj, -90, 0, 0);

	/*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{
	let img1 = CSD ~ "./back_img/stage6a01.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, img1);
	ObjSprite2D_SetSourceRect(obj, 0,0,512,128*10);
	ObjSprite2D_SetDestRect(obj, 0,0,512,128*10);
	Obj_SetRenderPriorityI(obj, 20);
	ObjRender_SetScaleXYZ(obj, 1, 1, 0);
	ObjRender_SetAlpha(obj, 210);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetPosition(obj, 512, 128*5, 0);
	ObjRender_SetAngleXYZ(obj, 0, 0, 0);
}

task BackSpell1{
	let img1 = CSD ~ "./back_img/cdbg06a.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, img1);
	ObjSprite2D_SetSourceRect(obj, 0,0,256,512);
	ObjSprite2D_SetDestRect(obj, 0,0,512,512);
	Obj_SetRenderPriorityI(obj, 20);
	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, 265, 175, 100);
	
	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 img2 = CSD ~ "./back_img/cdbg06b.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, img2);
	ObjSprite2D_SetSourceRect(obj, 0,0,448,437);
	ObjSprite2D_SetDestRect(obj, 0,0,448,437);
	Obj_SetRenderPriorityI(obj, 21);
	ObjRender_SetScaleXYZ(obj, 1, 1.025, 0);
	ObjRender_SetAngleXYZ(obj, 0, 0, 0);
	ObjRender_SetPosition(obj, 0, 0, 0);
	ObjRender_SetAlpha(obj, 0);
	ObjRender_SetColorHSV(obj, 0, 100, 200);
	
	loop{
		ObjRender_SetAlpha(obj, alpha2);
		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, 22);
	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;
	Back2;
}

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

task BackgroundCreate{
	//SetFogEnable(true);	
	//SetFogParam(0,128*50,0,0,0);
	
	Back1(0,0,512*2);
	
	//makeFogBG(100,0,100,255,255,255);
}

task ScrollStage{
	SetCameraPerspectiveClip(0, 4096);
	SetCameraAzimuthAngle(90+10*cos(scroll));
	SetCameraElevationAngle(75);
	SetCameraRadius(125);
	SetCameraFocusX(256*3);
	SetCameraFocusY(250);
	SetCameraFocusZ(0);

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

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