#include "./../../../system/functions/functions_stage.txt"

let current = GetCurrentScriptDirectory() ~ "img/background/";
let textureN = [];
let textureS = [];
let objScene = GetEnemyBossSceneObjectID;

@Initialize
{

	textureN = textureN ~ [current~"img_cloud.png"];
	textureN = textureN ~ [current~"img_pillarbase.png"];
	textureN = textureN ~ [current~"img_pillar.png"];
	textureN = textureN ~ [current~"img_tassle.png"];
	textureN = textureN ~ [current~"stg6bg.png"];
	textureS = textureS ~ [current~"spell1.png"];
	textureS = textureS ~ [current~"../face/faceM.png"];
	ascent(i in 0..length(textureN))
		{
		LoadTexture(textureN[i]);
		}
	ascent(i in 0..length(textureS))
		{
		LoadTexture(textureS[i]);
		}

	SetCommonData("BACKGROUND_END",0);
	Obj_SetValue(GetPlayerObjectID,"BACKGROUND_CHANGE",0);
	TBackground;
	SetAutoDeleteObject(true);
}

@MainLoop
{

	objScene = GetEnemyBossSceneObjectID();
	yield;
}

@Finalize
{
	ascent(i in 0..length(textureN))
		{
		RemoveTexture(textureN[i]);
		}
	ascent(i in 0..length(textureS))
		{
		RemoveTexture(textureS[i]);
		}
}

task TBackground
{
	let backS = false;

	SetCameraFocusX(0);
	SetCameraFocusY(45);
	SetCameraRadius(300);
	SetCameraElevationAngle(30);
	SetCameraAzimuthAngle(90);
	SetCameraPerspectiveClip(10, 512*7.5);
	SetFogParam(512*0.75, 512*1.75, 12,12,105);

	let aM = 0;

	let shad= ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(shad,"");
	ObjSprite2D_SetSourceRect(shad,0,0,1,1);
	ObjRender_SetColor(shad,0,0,0);
	ObjSprite2D_SetDestRect(shad,-64,-64,GetStgFrameWidth+64,GetStgFrameHeight+64);
	Obj_SetRenderPriority(shad,0.25);

	let backN = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(backN,"");
	ObjSprite2D_SetSourceRect(backN,0,0,1,1);
	ObjRender_SetColor(backN,12,12,105);
	ObjSprite2D_SetDestRect(backN,-64,-64,GetStgFrameWidth+64,GetStgFrameHeight*0.5);
	Obj_SetRenderPriority(backN,0.2);

	let objN = [];
	let NSpread = 40;
	ascent(dir in 0..2)
		{
		dir = dir*2-1;
		descent(i0 in 0..1)
			{
			descent(i1 in 0..NSpread)
				{
				objN = objN ~ [CreatePillar(dir*(-80+(i1%3)*165)+dir*(180+i0*256),250,512+(-8*52)*(i1*0.5+0.5+(i1%3)*0.5-(dir+1)*0.5*0.67)/2,dir)];
				}
			}
		}
	
	
	let x = 0;
	let y = 0;
	let z = 0;

	let aS = 0;

	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj,GetCurrentScriptDirectory ~ "img/background/cardbg2.png");
	ObjSprite2D_SetSourceRect(obj,0,0,512,512);
	ObjSprite2D_SetDestRect(obj,-8,-8,GetStgFrameWidth+8,GetStgFrameHeight+8);
	Obj_SetRenderPriority(obj,0.26);
	let color = 180;

	let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj2,GetCurrentScriptDirectory ~ "img/background/cardbg3.png");
	ObjSprite2D_SetSourceRect(obj2,0,0,512,512);
	ObjSprite2D_SetDestRect(obj2,-8,-8,GetStgFrameWidth+8,GetStgFrameHeight+8);
	Obj_SetRenderPriority(obj2,0.28);	


	let countM = 3;
	let count2 = 2;
	let count = countM;
	let objF = [];
	let objF2 = [];
	ascent(n in 0..count2)
		{
		objF = objF ~ [ObjPrim_Create(OBJ_SPRITE_2D)];
		ObjPrim_SetTexture(objF[n],GetCurrentScriptDirectory ~ "img/background/cardbg1.png");
		ObjSprite2D_SetSourceRect(objF[n],0,0,256,256);
		Obj_SetRenderPriority(objF[n],0.27);		
		ObjRender_SetBlendType(objF[n],BLEND_ALPHA);
		ObjPrim_SetVertexCount(objF[n],count*2);

		objF2 = objF2 ~ [ObjPrim_Create(OBJ_SPRITE_2D)];
		ObjPrim_SetTexture(objF2[n],GetCurrentScriptDirectory ~ "img/background/cardbg4.png");
		ObjSprite2D_SetSourceRect(objF2[n],0,0,256,256);
		Obj_SetRenderPriority(objF2[n],0.26);		
		ObjRender_SetBlendType(objF2[n],BLEND_ALPHA);
		ObjPrim_SetVertexCount(objF2[n],count*2);
		}

	ascent(n in 0..count2)
		{
		ascent(i0 in 0..count+1)
			{				
			ascent(i1 in 0..2)
				{
				ObjPrim_SetVertexUV(objF[n],i0*2+i1,1,i0/(count-1));
				ObjPrim_SetVertexUV(objF2[n],i0*2+i1,1,i0/(count-1));
				}
			}
		}
	let xF = 0;
	let alphaF = 180;	
	let c = -1;
	let aMA = 1/40;
	loop
		{
		c+=1;
		if(GetCommonData("BACKGROUND_END",0) == 2)
			{
			aM -= 1/40;
			}
		else
			{
			aM+=aMA;
			}
		if(c == 61)
			{
			ObjRender_SetColor(shad,256,256,236);
			}
		if(Obj_GetValueD(GetPlayerObjectID,"BACKGROUND_CHANGE",0) != 0)
			{
			if(aM >= 1)
				{
				aM = 0.99;
				aMA = -1/4;
				}	
			else if(aM <= 0)
				{
				if(Obj_GetValueD(GetPlayerObjectID,"BACKGROUND_CHANGE",0) == 1)
					{
					TBackChange;
					}
				Obj_SetValue(GetPlayerObjectID,"BACKGROUND_CHANGE",0);
				aM = 0;
				aMA = 1/90;
				}
			}
		if(!Obj_IsDeleted(shad))
			{
			ObjRender_SetAlpha(shad,(1-aM)*255);
			}
		SetCameraFocusZ(-512-(z%(156*12)));
		if(backS == false)
			{
			z+=4.125;
			}
		else
			{
			z+=0.35;
			}

		if(ObjEnemyBossScene_GetInfo(objScene,INFO_IS_SPELL))
			{
			if(aS < 1)
				{
				aS+=1/16;
				}
			}
		else 
			{
			if(aS > 0)
				{
				aS -=1/8;
				}
			}

		color+=1;
		xF+=0.33;
		alphaF+=1;

		ObjRender_SetAlpha(obj,aS*255);
		ObjRender_SetAlpha(obj2,aS*255);
		ascent(n in 0..count2)
			{
			ascent(i0 in 0..count+1)
				{
				let i2 = i0;
				i0 = (i0)%(count)-2;
				ascent(i1 in 0..2)
					{
					let i = i2*2+i1;	
					let x = i1*(GetStgFrameWidth+16)-8;
					let y = i0%GetStgFrameHeight;		
					local
						{
						ObjPrim_SetVertexPosition(objF[n],i,x,y,0);
						ObjPrim_SetVertexUVT(objF[n],i,x+(xF*(2+n*0.5/max(1,count2-1)))%256,y+n*142%GetStgFrameHeight);
						ObjPrim_SetVertexAlpha(objF[n],i,155*aS);
						ObjPrim_SetVertexColor(objF[n],i,180,180,180);
						}
					local
						{
						ObjPrim_SetVertexPosition(objF2[n],i,x,y,0);
						ObjPrim_SetVertexUVT(objF2[n],i,(x+GetStgFrameWidth*0.8)*0.9+n*20,(y-(xF*(2.5+n*0.5))%256)*1);
						ObjPrim_SetVertexAlpha(objF2[n],i,85*aS);
						ObjPrim_SetVertexColor(objF2[n],i,180,180,180);
						}
					}
				}
			}
		yield;
		}
	task TBackChange
		{
		backS = true;
		ascent(dir in 1..2)
			{
			ascent(i in 0..NSpread)
				{
				let obj = objN[dir*NSpread+i];		
				ascent(n in 0..length(obj))
					{	
					Obj_Delete(obj[n]);
					}
				}
			}
		ascent(i in 0..NSpread)
			{
			let obj = objN[i];
			ascent(n in 0..length(obj))
				{
				ObjRender_SetColorHSV(obj[n],0,0,0);
				ObjRender_SetScaleY(obj[n],-5);
				}
			}
		SetCameraFocusX(600);
		SetCameraFocusY(525);
		SetCameraFocusZ(-100);
		SetCameraRadius(300);
		SetCameraElevationAngle(-5);
		SetCameraAzimuthAngle(90);
		SetCameraYaw(90);
		SetCameraPerspectiveClip(10, 512*7.5);
		SetFogEnable(false);

		ObjPrim_SetTexture(backN,textureN[4]);
		ObjSprite2D_SetSourceRect(backN,0,0,512,380);
		ObjSprite2D_SetDestRect(backN,-64,-64,GetStgFrameWidth+64,GetStgFrameHeight+64);
		ObjRender_SetColor(backN,155,155,205);
		loop(90){yield;}
		Obj_Delete(shad);
		}
	function CreatePillar(x,y,z,dir)
		{
		let d = 32;
		d = d/2;
		let da = ((d*tan(67.5)^2)+(d^2))^0.5;
		let  obj = [];
		ascent(i in 0..8)
			{
			obj = obj ~ [ObjPrim_Create(OBJ_SPRITE_3D)];
			let a = i*360/8;
			Obj_SetRenderPriority(obj[i],0.21);
			ObjPrim_SetTexture(obj[i],textureN[2]);
			ObjSprite3D_SetSourceDestRect(obj[i],0,0,32,256);
			ObjRender_SetScaleXYZ(obj[i],d/32,-1,d/32);
			ObjRender_SetAngleXYZ(obj[i],0,-a+90,0);
			ObjRender_SetPosition(obj[i],x+d*cos(a),y-120,z+d*sin(a));
			ObjRender_SetZWrite(obj[i],true);
			ObjRender_SetBlendType(obj[i],BLEND_ALPHA);
			ObjRender_SetColor(obj[i],96,96,128);
			}
		TRend;
		return obj;
		task TRend
			{
			let base = ObjPrim_Create(OBJ_SPRITE_3D);
			Obj_SetRenderPriority(base,0.21);
			ObjPrim_SetTexture(base,textureN[1]);
			ObjSprite3D_SetSourceDestRect(base,128,1,256,255);
			ObjRender_SetAngleX(base,-90);
			ObjRender_SetScaleXYZ(base,dir*1,1,1);
			ObjRender_SetPosition(base,x+dir*64,y-240,z-32);
			ObjRender_SetZWrite(base,true);
			ObjRender_SetBlendType(base,BLEND_ALPHA);
	
			let baseN = ObjPrim_Create(OBJ_SPRITE_3D);
			Obj_SetRenderPriority(baseN,0.21);
			ObjPrim_SetTexture(baseN,"");
			ObjSprite3D_SetSourceDestRect(baseN,0,0,256,256);
			ObjRender_SetAngleX(baseN,-90);
			ObjRender_SetScaleXYZ(baseN,dir*3,1,3);
			ObjRender_SetPosition(baseN,x-dir*8,y-241,z-32);
			ObjRender_SetZWrite(baseN,true);
			ObjRender_SetColor(baseN,0,0,0);
			ObjRender_SetBlendType(baseN,BLEND_ALPHA);

			while(!Obj_IsDeleted(obj[0]) && backS == false){yield;}
			Obj_Delete(base);
			Obj_Delete(baseN);
			}
		}
}