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

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

@Initialize
{
	SetCameraFocusX(-16);
	SetCameraFocusY(75);
	SetCameraRadius(500);
	SetCameraElevationAngle(8);
	SetCameraAzimuthAngle(92);
	SetCameraPerspectiveClip(10, 512*7.5);
	SetFogParam(512*5.5, 512*7.5, 102, 102, 255);

	textureN = textureN ~ [current~"img1.png"];
	textureN = textureN ~ [current~"img2.png"];
	textureN = textureN ~ [current~"img3.png"];
	textureN = textureN ~ [current~"img4.png"];
	textureN = textureN ~ [current~"img5.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]);
		}

	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 aM = 0;

	let backN = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(backN,"");
	ObjSprite2D_SetSourceRect(backN,0,0,1,1);
	ObjRender_SetColor(backN,102,102,255);
	ObjSprite2D_SetDestRect(backN,-64,-64,GetStgFrameWidth+64,GetStgFrameHeight+64);
	Obj_SetRenderPriority(backN,0.2);

	ascent(t in 0..5)
		{
		ascent(dir in 0..2)
			{
			dir = dir*2-1;
			let img = ObjPrim_Create(OBJ_SPRITE_3D);
			Obj_SetRenderPriority(img,0.21);
			ObjPrim_SetTexture(img,textureN[1]);
			ObjSprite3D_SetSourceDestRect(img,0,0,512,512);
			ObjRender_SetAngleX(img,-90);
			ObjRender_SetScaleXYZ(img,dir*1.5,2,0);
			ObjRender_SetPosition(img,dir*(0.67+1.5-0.25)*(512*0.5),0,-(t)*511*2);
			ObjRender_SetZWrite(img,true);
			ObjRender_SetBlendType(img,BLEND_ALPHA);
			}
		let img = ObjPrim_Create(OBJ_SPRITE_3D);
		Obj_SetRenderPriority(img,0.21);
		ObjPrim_SetTexture(img,textureN[0]);
		ObjSprite3D_SetSourceDestRect(img,0,0,512,512);
		ObjRender_SetAngleX(img,-90);
		ObjRender_SetScaleXYZ(img,0.67,2,0);
		ObjRender_SetZ(img,-(t)*511*2);
		ObjRender_SetY(img,1);
		ObjRender_SetZWrite(img,true);
		}

	ascent(dir in 0..2)
		{
		dir = dir*2-1;
		descent(i0 in 0..2)
			{
			descent(i1 in 0..10)
				{
				let img = ObjPrim_Create(OBJ_SPRITE_3D);
				Obj_SetRenderPriority(img,0.23);
				if(i1%2 == 1)	
					{
					ObjPrim_SetTexture(img,textureN[3]);
					}
				else
					{
					ObjPrim_SetTexture(img,textureN[2]);
					}
				ObjSprite3D_SetSourceDestRect(img,0,0,512,512);
				ObjRender_SetAngleXYZ(img,0,-dir*15,180);
				ObjRender_SetScaleXYZ(img,dir,1,0);
				ObjRender_SetPosition(img,dir*(290+i0*256),250,(-5*511*2)*(i1+0.5+i0/3)/10);
				}
			}
		}
	
	
	let x = 0;
	let y = 0;
	let z = 0;

	let back = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
	ObjPrim_SetTexture(back,"");
	ObjSpriteList2D_SetSourceRect(back,0,0,1,1);
	ObjSpriteList2D_SetDestRect(back,-64,-64,GetStgFrameWidth+64,GetStgFrameHeight+64);

	let face = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(face,textureS[1]);
	ObjSprite2D_SetSourceRect(face,0,0,300,519);
	ObjRender_SetColorHSV(face,0,0,0);	
	ObjRender_SetPosition(face,GetStgFrameWidth*0.5,GetStgFrameHeight*0.5+30,0);
	ObjSprite2D_SetDestCenter(face);
	ObjRender_SetScaleXYZ(face,0.8,0.8,0);
	Obj_SetRenderPriority(face,0.25);

	let aS = 0;
	let posS = 0;
	let spread = 8;


	loop
		{
		if(GetCommonData("BACKGROUND_END",0) == 2)
			{
			aM -= 1/20;
			}
		else if(aM < 1)
			{
			aM+=1/20;
			}	
		z+=10;
		SetCameraFocusZ(-(z%(512*2)));

		ObjSpriteList2D_ClearVertexCount(back);
		if(ObjEnemyBossScene_GetInfo(objScene,INFO_IS_SPELL))
			{
			if(aS < 1)
				{
				aS+=1/60;
				}
			Obj_SetVisible(face,true);
			ObjRender_SetAlpha(face,aS*125);
			posS+=1;
			let c = 1;
			let i = 0;
			ascent(i1 in 0..2)
				{
				ascent(i0 in 0..spread)
					{
					let xA = (i%5)%2;
					let yA = (i%5)%3;
					let x = (i*120+(1+c)*posS)%(GetStgFrameWidth+220)-110;
					c = ((c*4+9)%15)*0.175;
					i+=1;
					if(i0 == spread-1)
						{
						let a = aS*(0.65+0.2*(0.5-0.5*cos(posS)));
						let c = 125;
						if(i1 == 1)
							{
							a = 1-aS;
							c = 0;
							}	
						ObjRender_SetAlpha(back,155);	
						ObjRender_SetColorHSV(back,0,0,0);
						Obj_SetRenderPriority(back,0.25);
						ObjSpriteList2D_AddVertex(back);
						}
					}
				}
			}
		else
			{
			aS = 0;
			posS = 0;
			Obj_SetVisible(face,false);
			}	
		yield;
		}
}