let GCSD=GetCurrentScriptDirectory();
let bSpell = false;
let current = GetCurrentScriptDirectory();
let fo = 0;
let nbg1 = GCSD~"img/stage06a.png";
let nbg2 = GCSD~"img/stage05b.png";
let nbg3 = GCSD~"img/stage06b.png";
let nbg4 = GCSD~"img/stg6bg8.png";
let sbg1 = GCSD~"img/SpellBG1.png";
let sbg2 = GCSD~"img/SpellBG2.png";
LoadTexture(nbg1);
LoadTexture(nbg2);
LoadTexture(nbg3);
LoadTexture(nbg4);
LoadTexture(sbg1);
LoadTexture(sbg2);
@Initialize
{
    TCallBg;
	SetCameraRadius(-170);
	SetCameraFocusY(1-5*sin(fo));
	SetCameraElevationAngle(49+30*sin(fo/4));
	SetCameraAzimuthAngle(90-3*sin(fo/2));
}

@MainLoop
{

	let objScene = GetEnemyBossSceneObjectID();
	if(ObjEnemyBossScene_GetInfo(objScene, INFO_IS_SPELL))
	{
		bSpell = true;
	}
	else
	{
		bSpell = false;
	}
	fo++;
	SetCameraAzimuthAngle(85+5*sin(fo/2));
	yield;
}

task TCallBg{
	yield;
	SetFogParam(100, 200, 0, 0, 0);
	while(1){
	if(bSpell==false){
	TNormalBackgroundC();
	TNormalBackground();
	TNormalBackground3();
	TNormalBackground2();
		}
	while(bSpell==false){ yield; }
	if(bSpell==true){
		BSpellBG1();
		BSpellBG2();
		BSpellBG3();
		}
	while(bSpell==true){ yield; }
	yield;
	}
}

task Camera{
//intro
loop(1360){yield;}
loop(400){
SetCameraFocusY(1-5*sin(fo));
	SetCameraElevationAngle(49+19*sin(fo*2));
	SetCameraAzimuthAngle(20-3*sin(fo*2));
	fo++;
	yield;
}
}

task TNormalBackgroundC() // Wave
{

    let Effect = GetCommonData("EFFECT",0);
	let spellA1 = 0;
	let spellA2 = 1;

	let back1 = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(back1,GetCurrentScriptDirectory ~ "img/Back_Plaid.png");
	ObjSprite2D_SetDestRect(back1,-8,-8,GetStgFrameWidth+8,GetStgFrameHeight+8);
	Obj_SetRenderPriority(back1,0.21);
	ObjRender_SetColorHSV(back1,0,0,0);
	ObjRender_SetBlendType(back1,BLEND_ADD_ARGB);


	let vertexcount1 = 90;
	if(Effect == 4)
		{
		vertexcount1 = 45;
		}
	else if(Effect >= 1)
		{
		vertexcount1 = 90;
		}
	vertexcount1*=2;
	let x2 = GetStgFrameWidth*0.5-4;
	let y2 = GetStgFrameHeight-405;
	let innerrad = 0;
	let outerrad = max(((0-y2)^2+(0-x2)^2)^0.5-500,((GetStgFrameHeight-y2)^2+(GetStgFrameWidth-x2)^2)^0.5)+8;
	let mov = 0;
	let count = 0;

	let back2 = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetPrimitiveType(back2, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetTexture(back2,nbg4);
	Obj_SetRenderPriority(back2, 0.20);
	ObjRender_SetAngleXYZ(back2,0,0,12*sin(count/5));
	ObjPrim_SetVertexCount(back2, vertexcount1);
	ObjRender_SetPosition(back2, x2,y2,0);
	ascent(i in 0..vertexcount1)
		{
		let indexvert = i*2;
		let angle = i*360/(vertexcount1/2-1)-120;
		ObjPrim_SetVertexPosition(back2,indexvert+0,innerrad*cos(angle),innerrad*sin(angle),0);
		ObjPrim_SetVertexColor(back2,indexvert+1,25,25,25);
		ObjPrim_SetVertexPosition(back2,indexvert+1,outerrad*cos(angle),outerrad*sin(angle),0);
		ObjPrim_SetVertexColor(back2,indexvert+1,25,25,25);
		}

	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj,GetCurrentScriptDirectory ~ "img/face/face.png");
	ObjSprite2D_SetSourceRect(obj,0,0,256,336);
	ObjSprite2D_SetDestCenter(obj);
	Obj_SetRenderPriority(obj, 0.2);
	ObjRender_SetPosition(obj,GetStgFrameWidth*0.25,GetStgFrameHeight*0.65,0);
	ObjRender_SetColorHSV(obj,0,255,5);
	ObjRender_SetScaleXYZ(obj,1.5,1.5,0);
	let counter = -1;
	loop
		{
		mov-=1.75;
		counter+=1;
		if(bSpell == false)	
			{
			if(spellA1 < 1)
			{
			Obj_SetVisible(back1,true);
			Obj_SetVisible(back2,true);
			Obj_SetVisible(obj,true);
			spellA1 += 1/40;
			}	
			else if(spellA2 < 1)
			{
			spellA2 += 1/40;
			}
			}
		else if(spellA1 < 1)
			{
			Obj_SetVisible(back1,true);
			Obj_SetVisible(back2,true);
			Obj_SetVisible(obj,true);
			spellA1 += 1/40;
			}	
		else if(spellA2 < 1)
			{
			spellA2 += 1/40;
			}
		ObjRender_SetAlpha(back1,spellA1*125);
		ObjSprite2D_SetSourceRect(back1,-16-mov*0.3,-mov*0.5,384*0.5-16-mov*0.3,408*0.5-mov*0.5);

		ascent(i in 0..vertexcount1/2)
			{
			let indexvert = i*2;
			ObjPrim_SetVertexUVT(back2, indexvert, 512/(vertexcount1/2-1)*i, 0+mov);
			ObjPrim_SetVertexAlpha(back2,indexvert+0,spellA1*155);
			ObjPrim_SetVertexUVT(back2, indexvert+1, 512/(vertexcount1/2-1)*i, 356+mov);
			ObjPrim_SetVertexAlpha(back2,indexvert+1,spellA1*60);
			}
		ObjRender_SetAlpha(obj,spellA1*175);
		yield;
		}
		
}

task TNormalBackground(){ // floor
	let c = 0;
	let s = 0;
	let value = 255;
	let count = 0;
	let obj=ObjPrim_Create(OBJ_SPRITE_3D);
	Obj_SetRenderPriorityI(obj,21);
	ObjPrim_SetTexture(obj,nbg1);
	ObjSprite3D_SetSourceRect(obj,0,0,1024,1024);
	ObjSprite3D_SetDestRect(obj,-512/1.3,-512/1.3,512/1.3,512/1.3);
	ObjRender_SetScaleXYZ(obj,0.2,0.2,0.2);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	ObjRender_SetAngleXYZ(obj,0,0,0);
	ObjRender_SetColor(obj,555,555,555);
	while( !Obj_IsDeleted(obj) && bSpell == false){
	c+=GetCommonData("Scroll",1)*1;
	ObjRender_SetPosition(obj,0,0,0);
	ObjSprite3D_SetSourceRect(obj,0,c,312,312+c);
	ObjRender_SetAngleXYZ(obj,0,0,2*sin(count/5));
	value++;
	count++;
	yield;
	}
	descent(i in 0..30){
	ObjRender_SetAlpha(obj,i);
	yield;
	}
	Obj_Delete(obj);
}	

task TNormalBackground3(){ // floorred
	let c = 0;
	let s = 0;
	let value = 255;
	let count = 0;
	let obj=ObjPrim_Create(OBJ_SPRITE_3D);
	Obj_SetRenderPriorityI(obj,21);
	ObjPrim_SetTexture(obj,nbg3);
	ObjSprite3D_SetSourceRect(obj,0,0,1024,1024);
	ObjSprite3D_SetDestRect(obj,-512,-512,512,512);
	ObjRender_SetScaleXYZ(obj,0.2,0.2,0.2);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	ObjRender_SetAngleXYZ(obj,0,0,0);
	ObjRender_SetColor(obj,1555,25,25);
	while( !Obj_IsDeleted(obj) && bSpell == false){
	c+=GetCommonData("Scroll",1)*2;
	ObjRender_SetPosition(obj,0,0,0);
	ObjSprite3D_SetSourceRect(obj,0,c,312,312+c);
	ObjRender_SetAngleXYZ(obj,0,0,2*sin(count/5));
	value++;
	count++;
	yield;
	}
	descent(i in 0..30){
	ObjRender_SetAlpha(obj,i);
	yield;
	}
	Obj_Delete(obj);
}	

task TNormalBackground2(){ // Earth
	let value = 0;
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,20);
	ObjPrim_SetTexture(obj,nbg2);
	ObjSprite2D_SetSourceRect(obj,0,0,512,256);
	ObjSprite2D_SetDestRect(obj,-256,-128,256,128);
	ObjRender_SetScaleXYZ(obj,1,1,1);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	ObjRender_SetColor(obj,255,255,255);
	ObjRender_SetPosition(obj,384/2+60,448/2-256/2+20,0);
	while( !Obj_IsDeleted(obj) && bSpell == false){
	yield;
	}
	descent(i in 0..30){
	ObjRender_SetAlpha(obj,i);
	yield;
	}
	Obj_Delete(obj);
}

task BSpellBG1{
	let bg = ObjPrim_Create(OBJ_SPRITE_2D);
	let a = 0;
	let s =0;
	let slide = 0;
	ObjRender_SetBlendType(bg,BLEND_ALPHA);
	Obj_SetRenderPriority(bg,0.21);
	ObjPrim_SetTexture(bg,sbg2);
	ObjSprite2D_SetSourceRect(bg,0,0,512,512);
	ObjSprite2D_SetDestCenter(bg);
	ObjRender_SetScaleXYZ(bg,1.15,1.15,1.15);
	ObjRender_SetX(bg,GetCenterX);
	ObjRender_SetY(bg,GetCenterY);
	while( !Obj_IsDeleted(bg) && bSpell == true){
	if(s<140){s+=1;}
	ObjRender_SetColor(bg,s,s,s);
	ObjRender_SetAngleXYZ(bg,0,0,slide/4);
	slide+=1;
	a++;
	yield;
	}
	Obj_Delete(bg);
}

task BSpellBG2{
	let bg = ObjPrim_Create(OBJ_SPRITE_2D);
	let a = 0;
	let s =0;
	let slide = 0;
	ObjRender_SetBlendType(bg,BLEND_ADD_ARGB);
	Obj_SetRenderPriority(bg,0.21);
	ObjPrim_SetTexture(bg,sbg2);
	ObjSprite2D_SetSourceRect(bg,0,0,512,512);
	ObjSprite2D_SetDestCenter(bg);
	ObjRender_SetScaleXYZ(bg,1.15,1.15,1.15);
	ObjRender_SetX(bg,GetCenterX);
	ObjRender_SetY(bg,GetCenterY);
	while( !Obj_IsDeleted(bg) && bSpell == true){
	if(s<140){s+=1;}
	ObjRender_SetColor(bg,s,s,s);
	ObjRender_SetAngleXYZ(bg,0,0,slide/4);
	slide-=1;
	a++;
	yield;
	}
	Obj_Delete(bg);
}

task BSpellBG3{
	let bg = ObjPrim_Create(OBJ_SPRITE_2D);
	let a = 0;
	let s =0;
let slide = 0;
	ObjRender_SetBlendType(bg,BLEND_SUBTRACT);
	Obj_SetRenderPriority(bg,0.21);
	ObjPrim_SetTexture(bg,sbg1);
	ObjRender_SetScaleXYZ(bg,1,1,1);
	ObjRender_SetX(bg,GetCenterX);
	ObjRender_SetY(bg,GetCenterY);
	while( !Obj_IsDeleted(bg) && bSpell == true){
	ObjSprite2D_SetSourceRect(bg,0,0+slide,256*2,256*2+slide);
	ObjSprite2D_SetDestCenter(bg);
	slide+=0.15;
	if(a<200){s+=1.50;}
	ObjRender_SetAlpha(bg,s);
	a++;
	yield;
	}
	Obj_Delete(bg);
}

function GetCenterX{
	let x = GetStgFrameWidth / 2;
	return x;
}

function GetCenterY{
	let y = GetStgFrameHeight / 2; 
	return y; 
}