let bSpell = false;
let sback = 1;
let y1 = 0.5;
let a = 0.5;
let current = GetCurrentScriptDirectory();
let objSTG = [];

@Initialize
{
	SetCameraFocusXYZ(0, -60, 80);
	SetCameraRadius(10);
	SetCameraAzimuthAngle(-90);
	SetCameraElevationAngle(25);
	SetFogParam(700, 1200, 255, 60, 30); 

	TNormalBackground();
	TSpellBackground();
}

@MainLoop
{
	y1 += a;
	if(a < 2){ a += 0.002; }
	let objScene = GetEnemyBossSceneObjectID();
	if(objScene != ID_INVALID && ObjEnemyBossScene_GetInfo(objScene, INFO_IS_SPELL))
	{
		bSpell = true;
		if(GetCommonData("boss", "EIKA") == "EIKA"){ sback = 1; }
		else if(GetCommonData("boss", "EIKA") == "KOISHI"){ sback = 2; }
	}
	else
	{
		bSpell = false;
	}

	yield;
}


task TNormalBackground
{
	let objA1 = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjRender_SetBlendType(objA1, BLEND_ALPHA);
	Obj_SetRenderPriorityI(objA1, 21);
	ObjRender_SetPermitCamera(objA1, true);
	ObjPrim_SetTexture(objA1, GetCurrentScriptDirectory~"../img/st01a.png");
	ObjRender_SetAngleXYZ(objA1, 70, 0, 0);
	ObjRender_SetScaleXYZ(objA1, 1.7, 1.7, 1);
	ObjRender_SetPosition(objA1, 0, -400, 1050);
	objSTG = objSTG ~ [objA1];

	let objA2 = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjRender_SetBlendType(objA2, BLEND_ALPHA);
	Obj_SetRenderPriorityI(objA2, 21);
	ObjRender_SetPermitCamera(objA2, true);
	ObjPrim_SetTexture(objA2, GetCurrentScriptDirectory~"../img/st01b.png");
	ObjRender_SetAngleXYZ(objA2, 70, 0, 0);
	ObjRender_SetScaleXYZ(objA2, 1.7, 1.7, 1);
	ObjRender_SetPosition(objA2, 0, -400, 1050);
	ObjRender_SetColor(objA2, 225, 115, 60);
	ObjRender_SetAlpha(objA2, 140);
	objSTG = objSTG ~ [objA2];

	let objA3 = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjRender_SetBlendType(objA3, BLEND_ALPHA);
	Obj_SetRenderPriorityI(objA3, 21);
	ObjRender_SetPermitCamera(objA3, true);
	ObjPrim_SetTexture(objA3, GetCurrentScriptDirectory~"../img/st01d.png");
	ObjRender_SetAngleXYZ(objA3, 70, 0, 0);
	ObjRender_SetScaleXYZ(objA3, 1.7, 1.7, 1);
	ObjRender_SetPosition(objA3, 0, -300, 1050);
	ObjRender_SetColor(objA3, 245, 185, 180);
	ObjRender_SetAlpha(objA3, 180);
	objSTG = objSTG ~ [objA3];

	let objA4 = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjRender_SetBlendType(objA4, BLEND_ALPHA);
	Obj_SetRenderPriorityI(objA4, 21);
	ObjRender_SetPermitCamera(objA4, true);
	ObjPrim_SetTexture(objA4, GetCurrentScriptDirectory~"../img/st01e.png");
	ObjRender_SetAngleXYZ(objA4, 70, 0, 0);
	ObjRender_SetScaleXYZ(objA4, 1.7, 1.7, 1);
	ObjRender_SetPosition(objA4, 0, -250, 1050);
	ObjRender_SetColor(objA4, 245, 185, 180);
	ObjRender_SetAlpha(objA4, 180);
	objSTG = objSTG ~ [objA4];

	let objB = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjRender_SetBlendType(objB, BLEND_ALPHA);
	Obj_SetRenderPriorityI(objB,20);
	ObjPrim_SetTexture(objB,GetCurrentScriptDirectory~"../img/bg2.png");
	ObjSprite3D_SetSourceDestRect(objB, 1, 1, 256, 256);
	ObjRender_SetAngleXYZ(objB, 0, 180, 180);
	ObjRender_SetScaleXYZ(objB, 10, 10, 1);
	ObjRender_SetPosition(objB, 0, 0, 1900); 
	ObjRender_SetPermitCamera(objB, true);
	ObjRender_SetColor(objB, 255, 60, 30);

	let frameInvisible = 0;
	let frame = 0;
	let yaw = 0;
	let roll = 0;

	loop
	{
		yield;
		SetCameraYaw(yaw);
		SetCameraRoll(roll);
		yaw = 5 * sin(frame * 0.5);
		roll = 10 * sin(frame * 0.5);
		frame++;
		ascent(i in 0..length(objSTG)){ ObjSprite3D_SetSourceDestRect(objSTG[i], 0, 0 + y1, 1024, 1500 + y1); }
		if(bSpell)
		{
			frameInvisible++;
			if(frameInvisible >= 1)
			{
				Obj_SetVisible(objA1, false);
				Obj_SetVisible(objA2, false);
				Obj_SetVisible(objA3, false);
				Obj_SetVisible(objA4, false);
				Obj_SetVisible(objB, false);
			}
		}
		else
		{
			frameInvisible = 0;
			Obj_SetVisible(objA1, true);
			Obj_SetVisible(objA2, true);
			Obj_SetVisible(objA3, true);
			Obj_SetVisible(objA4, true);
			Obj_SetVisible(objB, true);
		}
	}
}

task TSpellBackground
{
	let obj1 = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(obj1, 0.21);
	ObjPrim_SetTexture(obj1, GetCurrentScriptDirectory() ~ "../img/cdbg0.png");
	ObjSprite2D_SetSourceRect(obj1, 0, 0, 512, 512);
	ObjSprite2D_SetDestRect(obj1, -256, -256, 256, 256);
    	ObjRender_SetPosition(obj1, 384/2, 448/2, 1);
	ObjRender_SetScaleXYZ(obj1, 1, 1, 0);
	ObjRender_SetColor(obj1, 192, 60, 60);

	let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(obj2, 0.20);
	ObjPrim_SetTexture(obj2, GetCurrentScriptDirectory() ~ "../img/cdbg1.png");
	ObjSprite2D_SetSourceRect(obj2, 0, 0, 512, 512);
	ObjSprite2D_SetDestRect(obj2, -256, -256, 256, 256);
    	ObjRender_SetPosition(obj2, 384/2, 448/2, 1);
	ObjRender_SetScaleXYZ(obj2, 1.5, 1.5, 0);
	ObjRender_SetBlendType(obj2, BLEND_ALPHA);

	let obj3 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj3, BLEND_ALPHA);
	Obj_SetRenderPriorityI(obj3, 21);
	ObjPrim_SetTexture(obj3, GetCurrentScriptDirectory() ~ "../img/cdbg4.png");
	ObjRender_SetScaleXYZ(obj3, 1, 1, 1);
	ObjSprite2D_SetSourceRect(obj3, 0, 0, 384, 448);
	ObjSprite2D_SetDestRect(obj3, -256, -256, 256, 256);
	ObjRender_SetPosition(obj3, 384/2, 448/2, 0);
	
	let obj4 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj4, BLEND_ADD_ARGB);
	Obj_SetRenderPriorityI(obj4, 22);
	ObjPrim_SetTexture(obj4, GetCurrentScriptDirectory() ~ "../img/cdbg3.png");
	ObjRender_SetScaleXYZ(obj4, 4, 4, 1);
	ObjSprite2D_SetSourceRect(obj4, 0, 0, 2000, 2000);
	ObjSprite2D_SetDestRect(obj4, -256, -256, 256, 256);
	ObjRender_SetPosition(obj4, 384/2, 448/2, 0);

    	let frame = 0;
    	let alpha = 0;
    	loop
	{
        	if (bSpell) 
		{
			if(sback == 1)
			{
				Obj_SetVisible(obj1, true);
				Obj_SetVisible(obj2, true);
        			ObjRender_SetAlpha(obj1, 100);
        			ObjRender_SetAlpha(obj2, 255);
			}
			else if(sback == 2)
			{
				Obj_SetVisible(obj3, true);
				Obj_SetVisible(obj4, true);
        			ObjRender_SetAlpha(obj3, 155);
        			ObjRender_SetAlpha(obj4, 192);
			}
        	} 
		else 
		{
           		alpha = 0;
			Obj_SetVisible(obj1, false);
			Obj_SetVisible(obj2, false);
			Obj_SetVisible(obj3, false);
			Obj_SetVisible(obj4, false);
        	}
        	ObjSprite2D_SetSourceRect(obj1, 0, 0 + (frame) % 512, 512, 512 + (frame) % 512);
        	ObjRender_SetAngleZ(obj2, frame * 1.2 );
		ObjRender_SetPosition(obj4, 192 + 100 * sin(frame * 0.4), 224 + 100 * sin(frame * 0.4), 0);
		ObjRender_SetPosition(obj3, 192, 224, 0);
		ObjRender_SetAngleZ(obj4, frame * 0.4);
        	frame++;
        	yield;
    	}
}