#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);

	ascent(i in 0..length(textureN))
		{
		LoadTexture(textureN[i]);
		}
	ascent(i in 0..length(textureS))
		{
		LoadTexture(textureS[i]);
		}

	SetAutoDeleteObject(true);
	TBackground;
}

@MainLoop
{

	objScene = GetEnemyBossSceneObjectID();
	yield;
}

@Finalize
{
}

task TBackground
{
	let objB = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(objB,"");
	ObjSprite2D_SetSourceRect(objB,0,0,1,1);
	ObjSprite2D_SetDestRect(objB,-64,-64,GetStgFrameWidth+64,GetStgFrameHeight+64);
	Obj_SetRenderPriority(objB, 0.22);

	let alpha = 0;
	let gA = 0;
	let color = 260;

	let c = -1;
	let a = 0;
	let colorM = 260;
	let dir = 1;
	let count = [5,7,18];

	loop
		{
		local
			{
			if(GetSpellBack >= SHOT_X && alpha > 0)
				{
				alpha -= 1/60;
				}
			else if(GetSpellBack < SHOT_X && alpha < 1)
				{
				alpha += 1/60;
				}
			alternative(GetSpellBack)
				case(SHOT_RED)
					{
					colorM = 10;
					}
				case(SHOT_YELLOW)
					{
					colorM = 75;
					}
				case(SHOT_GREEN)
					{
					colorM = 120;
					}
				case(SHOT_BLUE)
					{
					colorM = 217.5;
					}
				case(SHOT_PURPLE)
					{
					colorM = 280;
					}
				case(SHOT_X)
					{
					colorM = 260;
					}
			if(GetSpellBack == SHOT_X+1)
				{
				color+=(colorM-color)/60;
				}
			else
				{
				color = colorM;
				}
			gA+=0.625;
			ObjRender_SetColorHSV(objB,color,alpha*255,alpha*(0.55+0.45*absolute(cos(gA)))*135);
			c+=1;
			if(c%count[2] == 0 && floor(c/count[2])%count[1] == 0)
				{
				a = 90;
				dir*=-1;
				}
			if(c%count[2] == 0 && floor(c/count[2])%count[1] < count[0])
				{
				TSpin(a,dir,floor(c/count[2])%count[1]);
				}
			}
		yield;
		}

	task TSpin(a,dir,g)
		{
		let time = count[2]*(count[1]);

		let obj = ObjPrim_Create(OBJ_SPRITE_LIST_2D);
		ObjPrim_SetTexture(obj,GetCurrentScriptDirectory ~ "./img/background/img1.png");
		ObjSpriteList2D_SetSourceRect(obj,0,0,300,300);
		Obj_SetRenderPriority(obj, 0.28-g*0.01);
		ObjRender_SetAlpha(obj,205);	
		let gA = color;
		ascent(t in 0..time)
			{
			ObjSpriteList2D_ClearVertexCount(obj);
			let s = t*GetStgFrameWidth*4/time;
			ObjRender_SetPosition(obj,GetStgFrameWidth*0.5,GetStgFrameHeight*0.5,0);
			ObjRender_SetScaleXYZ(obj,s/300,s/300,0);
			ObjRender_SetAngleZ(obj,a);
			ObjRender_SetColorHSV(obj,gA,g*255/(count[0]-1),205);
			ObjSpriteList2D_SetDestCenter(obj);
			ObjSpriteList2D_AddVertex(obj);
			a+=dir*180/time;
			yield;
			}
		Obj_Delete(obj);
		}
}