let GCSD=GetCurrentScriptDirectory();
let bSpell = false;
let current = GetCurrentScriptDirectory();
let fo = 0;
let nbg1 = GCSD~"img/NBG1.png";
let nbg2 = GCSD~"img/NBG2.png";
let nbg3 = GCSD~"img/NBG3.png";
let nbg4 = GCSD~"img/NBG4.png";
let nbg5 = GCSD~"img/NBG5.png";
let sbg1 = GCSD~"img/SPBG1.png";
let sbg2 = GCSD~"img/SPBG2.png";
let sbg3 = GCSD~"img/SPBG3.png";
LoadTexture(nbg1);
LoadTexture(nbg2);
LoadTexture(nbg3);
LoadTexture(nbg4);
LoadTexture(nbg5);
LoadTexture(sbg1);
LoadTexture(sbg2);
LoadTexture(sbg3);
@Initialize
{
    TCallBg;
	SetCameraRadius(-170);
	SetCameraFocusY(1-5*sin(fo));
	SetCameraElevationAngle(80);//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){
	TNormalBackground4();
	TNormalBackground2();
	TNormalBackground();
	TNormalBackground5();
		}
	while(bSpell==false){ yield; }
	if(bSpell==true){
		TSpellBackground();
		TSpellBackground2();
		TSpellBackground3();
		TSpellBackground4();
		}
	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 TNormalBackground(){ // road
	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,256,256);
	ObjSprite3D_SetDestRect(obj,-256,-512,256,512);
	ObjRender_SetScaleXYZ(obj,0.2,0.24,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,256,256+c);
	SetCameraAzimuthAngle(90);
	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+65,448/2-256/2+25,0);
	while( !Obj_IsDeleted(obj) && bSpell == false){
	yield;
	}
	descent(i in 0..30){
	ObjRender_SetAlpha(obj,i);
	yield;
	}
	Obj_Delete(obj);
}

task TNormalBackground3() // 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 = 384/2;
	let y2 = 50;
	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,nbg3);
	Obj_SetRenderPriority(back2, 0.2);
	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 == true)	
			{
			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 TNormalBackground4(){ 
	let value = 0;
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,21);
	ObjPrim_SetTexture(obj,nbg4);
	ObjSprite2D_SetSourceRect(obj,0,0,221,222);
	ObjSprite2D_SetDestRect(obj,-221,-222,221,222);
	ObjRender_SetScaleXYZ(obj,0.2,0.2,0);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetColor(obj,255,255,255);
	ObjRender_SetPosition(obj,384/2,50,0);
	while( !Obj_IsDeleted(obj) && bSpell == false){
	yield;
	}
	descent(i in 0..30){
	ObjRender_SetAlpha(obj,i);
	yield;
	}
	Obj_Delete(obj);
}

task TNormalBackground5(){ 
	let value = 0;
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,20);
	ObjPrim_SetTexture(obj,nbg5);
	ObjSprite2D_SetSourceRect(obj,0,0,1211,312);
	ObjSprite2D_SetDestRect(obj,-1211,-313,1211,313);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,0.35,0.35,0);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetColor(obj,255,255,255);
	ObjRender_SetAlpha(obj,255);
	ObjRender_SetPosition(obj,384/2,140,0);
	while( !Obj_IsDeleted(obj) && bSpell == false){
	yield;
	}
	Obj_Delete(obj);
}
task TSpellBackground(){
	let c = 0;
	let s = 0;
	let value = 0;
	let count = 0;
	let nico = 0;
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,20);
	ObjPrim_SetTexture(obj,sbg1);
	ObjSprite2D_SetSourceRect(obj,0,0,638,851+c);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,1,1,1);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	ObjRender_SetColor(obj,255,255,255);
	ObjRender_SetAngleXYZ(obj,0,0,0);
	ObjRender_SetPosition(obj,384/2,448/2,0);
	while( !Obj_IsDeleted(obj) && bSpell == true){
	ObjRender_SetColor(obj,value,value,value);
	if(value<180){value+=5;}
	nico+=1;
	count++;
	yield;
	}
	descent(i in 0..30){
	ObjRender_SetAlpha(obj,i);
	yield;
	}
	Obj_Delete(obj);
}	

task TSpellBackground2(){
	let c = 0;
	let s = 0;
	let value = 0;
	let count = 0;
	let nico = 0;
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,22);
	ObjPrim_SetTexture(obj,sbg2);
	ObjSprite2D_SetSourceRect(obj,0,0,260,508);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,0.9,0.9,0);
	ObjRender_SetPosition(obj,384/2,448/2,0);
	while( !Obj_IsDeleted(obj) && bSpell == true){
	ObjRender_SetColor(obj,value,value,value);
	if(value<255){value+=5;}
	nico+=1;
	count++;
	yield;
	}
	descent(i in 0..30){
	ObjRender_SetAlpha(obj,i);
	yield;
	}
	Obj_Delete(obj);
}

task TSPBG3{
	let x = -100;
	let y = rand( 0, 448 );
	let brickspeed = rand( 2.5, 4.5 );
	let bricksize = rand( 0.8, 1 );
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,23);
	ObjPrim_SetTexture(obj,sbg3);
	ObjSprite2D_SetSourceRect(obj,0,0,200,76);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,bricksize,bricksize,0);
	//DelayDelete1( obj, 200 );
	while( x < GetStgFrameWidth + 100 && bSpell == true ){
	ObjRender_SetPosition( obj, x, y, 0 );
	x += brickspeed;
	yield;
	}
	Obj_Delete( obj );
}

task TSpellBackground3{
let frequency = 30;
while( bSpell == true ){
TSPBG3;
TSPBG3;
loop( frequency ){ yield; }
}
}

task TSPBG4{
	let x = GetStgFrameWidth + 100;
	let y = rand( 0, 448 );
	let brickspeed = rand( 2.5, 4.5 );
	let bricksize = rand( 0.5, 0.8 );
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,21);
	ObjPrim_SetTexture(obj,sbg3);
	ObjSprite2D_SetSourceRect(obj,0,0,200,76);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetAlpha( obj, 255/2 );
	ObjRender_SetScaleXYZ(obj,bricksize,bricksize,0);
	//DelayDelete2( obj, 200 );
	while( x > -100 && bSpell == true ){
	ObjRender_SetPosition( obj, x, y, 0 );
	x -= brickspeed;
	yield;
	}
	Obj_Delete( obj );	
}
task appear(obj){
	ascent(i in 0..255/5){
	ObjRender_SetAlpha(obj,i*5);
	yield;
	}
	}

task TSpellBackground4{
let frequency = 30;
while( bSpell == true ){
TSPBG4;
TSPBG4;
loop( frequency ){ yield; }
}
}

task DelayDelete1( obj, t ){
while( ObjRender_GetX( obj ) < GetStgFrameWidth + 100  && bSpell == true ){ yield; }
Obj_Delete(obj);
}


task DelayDelete2( obj, t ){
while( ObjRender_GetX( obj ) > -100 && bSpell == true ){ yield; }
Obj_Delete(obj);
}

