let bSpell = false;
let mSpell = false;
let current = GetCurrentScriptDirectory();
let pzInc = 2.5;
@Initialize
{

	SetFogParam(0, 1000, 32, 32, 32);

	ascent(let pos in 0..3)
	{
		TNormalBackground(pos);
	}
	TSpellBackground();
	TSpellBackgroundM();
}

@MainLoop
{

	let objScene = GetEnemyBossSceneObjectID();
	if(objScene != ID_INVALID && ObjEnemyBossScene_GetInfo(objScene, INFO_IS_SPELL))
	{
		bSpell = true;
		if(Obj_GetValueD(objScene,"MIDBOSS",false) == true)
			{
			mSpell = true;
			}
		else
			{
			mSpell = false;
			}
	}
	else
	{
		bSpell = false;
	}
	

	yield;
}


task TNormalBackground(pos)
{
	SetCameraFocusX(0);
	SetCameraFocusY(0);
	SetCameraFocusZ(0);
	SetCameraRadius(256*1.25);

	let angleA = 0;
	let angleA2 = 0;

	let angleB = 0;
	let angleB2 = 0;

	let pz = (pos-1) * 1000;
	let pz2 = pz;

	let path = current ~ "img/Background_Road.png";
	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	ObjPrim_SetTexture(obj,path);
	Obj_SetRenderPriorityI(obj, 20);
		
	
	let path2 = current ~ "img/Background_Sky.png";
	let obj2 = 
			[	
			ObjPrim_Create(OBJ_SPRITE_3D),
			ObjPrim_Create(OBJ_SPRITE_3D),
			];
	ascent(i in 0..2)
		{	
		ObjPrim_SetTexture(obj2[i],path2);
		Obj_SetRenderPriorityI(obj2[i], 21);
		ObjRender_SetColorHSV(obj2[i],240,150,255);
		}

	let path3 = current ~ "img/Background_Moon.png";
	let obj3 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj3,path3);
	Obj_SetRenderPriorityI(obj3, 20);
	ObjRender_SetColorHSV(obj3,0,0,230);
	ObjRender_SetAlpha(obj3,100);
	let alph = 0;

	let time = 0;

	let frameInvisible = 0;
	loop
	{
		if(bSpell)
		{
			frameInvisible++;
			if(frameInvisible >= 60)
			{
			Obj_SetVisible(obj, false);
			ascent(i in 0..2)
				{
				Obj_SetVisible(obj2[i], false);
				}
			Obj_SetVisible(obj3, false);
			}
		}
		else
		{
			frameInvisible = 0;
			Obj_SetVisible(obj, true);
			ascent(i in 0..2)
				{
				Obj_SetVisible(obj2[i], true);
				}
			Obj_SetVisible(obj3, true);
		}

		SetCameraAzimuthAngle(90+30*sin(angleA));
		SetCameraElevationAngle(65+10*sin(angleB));

		ObjRender_SetPosition(obj,0,pz,pz);	
		ObjSprite3D_SetSourceDestRect(obj,0,-256*3,256,256*3);
		ObjRender_SetAngleX(obj,45);
		ObjRender_SetScaleXYZ(obj,1,1,1);

		ascent(i in 0..2)
			{
			ObjRender_SetPosition(obj2[i],-(i*2-1)*256*(0.5+1/12)*4.71,pz2,pz2);	
			ObjSprite3D_SetSourceDestRect(obj2[i],0,-256*3,256*5,256*3);
			ObjRender_SetAngleX(obj2[i],45);
			ObjRender_SetScaleXYZ(obj2[i],i*2-1,1,1);
			ObjRender_SetAlpha(obj2[i],200+sin(alph)*50);
			}
	
		ObjSprite2D_SetSourceRect(obj3,24,0,512+32,100+10*absolute(cos(GetCameraAzimuthAngle)));
		ObjRender_SetAngleZ(obj3,1.375*(GetCameraAzimuthAngle-90)*absolute(cos(GetCameraElevationAngle)));
		ObjRender_SetPosition(obj3,GetStgFrameWidth/2-GetStgFrameWidth*cos(GetCameraAzimuthAngle),GetStgFrameHeight/2-21+500*sin(65-GetCameraElevationAngle)-(GetStgFrameHeight/2-5*absolute(cos(GetCameraAzimuthAngle)))*sin(GetCameraAzimuthAngle),0);
		ObjRender_SetScaleXYZ(obj3,2*GetStgFrameWidth/512,(GetStgFrameHeight/2.875)/96,0);
		ObjRender_SetAlpha(obj3,175-sin(alph)*75);
		ObjSprite2D_SetDestCenter(obj3);
	
		alph+=1;
		pz+=pzInc;
		pz2+=pzInc*1.5;
		angleA+=angleA2;
		angleB+=angleB2;
		if(pz > 256*3){pz -= 256*10;}
		if(pz2 > 256*3){pz2 -= 256*10;}
		time+=1;
		if(time > 2250)
			{
			if(angleA2 < 0.25)
				{
				angleA2+=0.25/180;
				}
			if(angleA2 < 0.25)
				{
				angleB2+=0.188/180;
				}
			}
		yield;
	}
}

task TSpellBackground
{
	let path1 = current ~ "img/Background_Spell_Kana.png";
	let obj1 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj1, BLEND_ALPHA);
	Obj_SetRenderPriorityI(obj1, 21);
	ObjPrim_SetTexture(obj1, path1);
	ObjSprite2D_SetSourceRect(obj1, 0, 0, 384, 448);
	ObjRender_SetPosition(obj1,GetStgFrameWidth/2,GetStgFrameHeight/2,0);
	ObjSprite2D_SetDestCenter(obj1);

	let path2 = current ~ "img/Background_Spell_KanaEffect.png";
	let obj2 = 
		[
		ObjPrim_Create(OBJ_SPRITE_2D),
		ObjPrim_Create(OBJ_SPRITE_2D),
		ObjPrim_Create(OBJ_SPRITE_2D),
		];
	ascent(i in 0..3)
		{
		ObjRender_SetBlendType(obj2[i], BLEND_ADD_ARGB);
		Obj_SetRenderPriorityI(obj2[i], 21);
		ObjPrim_SetTexture(obj2[i], path2);
		ObjSprite2D_SetSourceRect(obj2[i], 0,-448, 384, 448*2);
		}

	let path3 = current ~ "img/Face_Kana.png";
	let obj3 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj3, BLEND_ALPHA);
	Obj_SetRenderPriorityI(obj3, 22);
	ObjPrim_SetTexture(obj3, path3);
	ObjSprite2D_SetSourceRect(obj3, 0, 0, 512, 512);
	ObjRender_SetPosition(obj3,GetStgFrameWidth/2,GetStgFrameHeight/2,0);
	ObjRender_SetScaleXYZ(obj3,0.875,0.875,0);
	ObjRender_SetColorHSV(obj3,0,0,0);
	ObjSprite2D_SetDestCenter(obj3);
	

	let alpha = 0;
	let angleA = [
			rand(0,360),
			rand(0,360),
			rand(0,360),
			];
	loop
	{
		if(bSpell && mSpell == false)
		{
			if(alpha < 255)
				{
				alpha += 255/90;
				}
		}
		else
		{
			alpha = 0;
		}
		ObjRender_SetAlpha(obj1, alpha);
		Obj_SetVisible(obj1, alpha > 0);

		ascent(i in 0..3)
			{
			angleA[i] = angleA[i]+(i+1)/2;
			ObjRender_SetAlpha(obj2[i], alpha);
			Obj_SetVisible(obj2[i], alpha > 0);
			ObjRender_SetPosition(obj2[i],GetStgFrameWidth/2,GetStgFrameHeight/2+GetStgFrameHeight*cos(angleA[i]),0);
			ObjRender_SetColorHSV(obj2[i],angleA[i],100,66);
			ObjSprite2D_SetDestCenter(obj2[i]);
			}

		ObjRender_SetAlpha(obj3, alpha*0.625);
		Obj_SetVisible(obj3, alpha > 0);
		yield;
	}

}

task TSpellBackgroundM
{
	let path1 = current ~ "img/Background_Spell_Suwa.png";
	let obj1 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj1, BLEND_ALPHA);
	Obj_SetRenderPriorityI(obj1, 21);
	ObjPrim_SetTexture(obj1, path1);
	ObjSprite2D_SetSourceRect(obj1, 0, 0, 512, 512);
	ObjRender_SetPosition(obj1,GetStgFrameWidth/2,GetStgFrameHeight/2,0);
	ObjRender_SetColorHSV(obj1,0,0,200);
	ObjSprite2D_SetDestCenter(obj1);

	let path2 = current ~ "img/Background_Spell_SuwaEffect.png";
	let obj2 = 
		[
		ObjPrim_Create(OBJ_SPRITE_2D),
		ObjPrim_Create(OBJ_SPRITE_2D)
		];
	ascent(i in 0..2)
		{
		ObjRender_SetBlendType(obj2[i], BLEND_ALPHA);
		Obj_SetRenderPriorityI(obj2[i], 21);
		ObjPrim_SetTexture(obj2[i], path2);
		ObjSprite2D_SetSourceRect(obj2[i], 0,0, 384, 448);
		}

	let path3 = current ~ "img/Face_Suwa.png";
	let obj3 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjRender_SetBlendType(obj3, BLEND_ALPHA);
	Obj_SetRenderPriorityI(obj3, 22);
	ObjPrim_SetTexture(obj3, path3);
	ObjSprite2D_SetSourceRect(obj3, 0, 0, 256, 512);
	ObjRender_SetPosition(obj3,GetStgFrameWidth/2,GetStgFrameHeight,0);
	ObjRender_SetScaleXYZ(obj3,0.875,0.875,0);
	ObjRender_SetColorHSV(obj3,0,0,0);
	ObjRender_SetScaleX(obj3,-1);
	ObjSprite2D_SetDestCenter(obj3);
	

	let alpha = 0;
	let angleA = [
			0,
			GetStgFrameWidth,
			];
	loop
	{
		if(bSpell && mSpell == true)
		{
			if(alpha < 255)
				{
				alpha += 255/90;
				}
		}
		else
		{
			alpha = 0;
		}
		ObjRender_SetAlpha(obj1, alpha);
		Obj_SetVisible(obj1, alpha > 0);

		ascent(i in 0..2)
			{
			ObjRender_SetAlpha(obj2[i], alpha);
			Obj_SetVisible(obj2[i], alpha > 0);
			angleA[i] = angleA[i]+1;
			if(angleA[i] > GetStgFrameWidth*2)
				{
				angleA[i] = 0;	
				}
			ObjSprite2D_SetDestRect(obj2[i],angleA[i]-GetStgFrameWidth,0,angleA[i],GetStgFrameHeight);
			ObjRender_SetColorHSV(obj2[i],angleA[i]%360,255,255);
			}

		ObjRender_SetAlpha(obj3, alpha);
		Obj_SetVisible(obj3, alpha > 0);
		yield;
	}

}

