let GCSD=GetCurrentScriptDirectory();
let bSpell = false;
let current = GetCurrentScriptDirectory();
let fo = 0;
let nbg1 = GCSD~"img/PS4.png";
let sbg3 = GCSD~"img/cdbg10b.png";
let sbg2 = GCSD~"img/cdbg24b.png";
let sbg1 = GCSD~"img/cdbg24.png";
let sbg0 = GCSD~"img/5.png";
LoadTexture(nbg1);
LoadTexture(sbg0);
LoadTexture(sbg1);
LoadTexture(sbg2);
LoadTexture(sbg3);
function wait(n){loop(n){yield;}}
@Initialize
{
	SetCameraFocusX(0);
	SetCameraFocusY(0);
	SetCameraFocusZ(0);
	SetCameraRadius(105);
	SetCameraElevationAngle(215);
	SetCameraAzimuthAngle(90);
	TCallBg;
}

@MainLoop
{

	let objScene = GetEnemyBossSceneObjectID();
	if(ObjEnemyBossScene_GetInfo(objScene, INFO_IS_SPELL))
	{
		bSpell = true;
	}
	else
	{
		bSpell = false;
	}
	fo++;

	yield;
}

task TCallBg{
	yield;
	SetFogParam(0, 400, 32, 0, 64);
	TNormalBackground;
	loop{

	while(bSpell==false){ yield; }
	if(bSpell==true){
	TSpellBackground0();
	   TSpellBackground();
	   TSpellBackground2();
	   TSpellBackground3();

		}
	while(bSpell==true){ yield; }
	yield;
	}
}

task TSakura{
	while( bSpell == false){
	if(GetCommonData("Effect",2)){TMaple(rand(0,416),rand(0,0));}
	loop((3-GetCommonData("Effect",2))*9){
	yield;
	}
	}
		task TMaple(px,py){
		let obj=ObjPrim_Create(OBJ_SPRITE_2D);
		let maple = GetCurrentScriptDirectory ~"./img/eff_maple.png";
		let cy = rand(1,3);
		let ang = [0,0,90];
		let ap = [rand(-1,1),rand(0,0),rand(1,3)];
		let size = cy/3;
		let s = rand(-0.5,0.5);
		let color = [80,80,80];
		if(s>0){s=1;}
		if(s<=0){s=-1;}
		if(ap[0]>-1 && ap[0]<=0){ap[0]=-1;}
		if(ap[0]<1 && ap[0]>=0){ap[0]=1;}
		ap[2] = ap[2]*s;
		ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
		ObjPrim_SetTexture(obj,maple);
		Obj_SetRenderPriorityI(obj,21);
		ObjSprite2D_SetSourceRect(obj,0,0,32,32);
		ObjSprite2D_SetDestCenter(obj);
		ObjRender_SetScaleXYZ(obj,size,size,0);
		ObjRender_SetColor(obj,color[0],color[1],color[2]);
		ManageColor;
			task ManageColor{
				ascent(i in 0..90){
				ObjRender_SetAlpha(obj,i*4);
				ObjRender_SetScaleXYZ(obj,size,size,0);
				size +=0.0035;
				if(bSpell == true){break;}
				yield;
				}
				descent(i in 0..90){
				ObjRender_SetAlpha(obj,i*2);
				ObjRender_SetScaleXYZ(obj,size,size,0);
				size +=0.0035;
				if(bSpell == true){break;}
				yield;
				}
				Obj_Delete(obj);
			}
			while( bSpell == false){
			ObjRender_SetPosition(obj,px,py,0);
			ObjRender_SetAngleXYZ(obj,ang[0],ang[1],ang[2]);
			ang[0] = ang[0]+ap[0];
			ang[1] = ang[1]+ap[0]*-1;
			ang[2] = ang[2]+ap[2];
			py += cy;
			yield;
			}
			Obj_Delete(obj);
		}
	}



task TNormalBackground() // floor
{

    let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	let c = 0;
	let s = 0;
	let count = 0;
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	Obj_SetRenderPriorityI(obj,0);
	ObjPrim_SetTexture(obj,nbg1);
	ObjSprite2D_SetSourceRect(obj,0,0,1366,768);
	ObjSprite2D_SetDestCenter(obj);
	descentdant( obj );
	dest( obj );
	ObjRender_SetPosition(obj,640/2, 450/2, 0);
	movei(obj);
}

	task descentdant( obj ){
	let a = 2;
	let b = 0.04;///1.5;
	while( a > 0.69 ){ 
	ObjRender_SetScaleXYZ( obj, a, a, 0 );
	a -= b;
	b -= 0.00062;///2.28;
	yield;
	}
	}

	task dest( obj ){
	ascent( i in 0..255/(255/63) ){
	ObjRender_SetAlpha( obj, i*(255/63));
	yield;
	}
	}

	task movei(obj){
	loop{
	ObjRender_SetPosition(obj,640/2 - 60 + GetPlayerX/5, 450/2 - 40 + GetPlayerY/10, 0);
	yield;
	}
	}
		
task TSpellBackground0(){
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,26);
	ObjPrim_SetTexture(obj,sbg0);
	ObjSprite2D_SetSourceRect(obj,0,0,256,256);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,3,2,0);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetPosition(obj,640/2,450/2,0);
	ObjRender_SetAlpha( obj, 255 );
	ascent( i in 0..255/3.5 ){
	ObjRender_SetAlpha( obj, i*3.5 );
	yield;
	}
	loop( 10 ){ yield; }
	descent( i in 0..255/2.5 ){
	ObjRender_SetAlpha( obj, i*2.5 );
	yield;
	}
	Obj_Delete(obj);
}


task TSpellBackground(){
	wait( 75 );
	let c = 0;
	let s = 0;
	let value = 0;
	let count = 0;
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,21);
	ObjPrim_SetTexture(obj,sbg1);
	ObjSprite2D_SetSourceRect(obj,0,0,512,512);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,1.7,1.7,0);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetAngleXYZ(obj,0,0,0);
	ObjRender_SetColor(obj,55,55,55);
	c+=GetCommonData("Scroll",1)*3;
	ObjRender_SetPosition(obj,640/2 + 110,480/2,0);
	while( !Obj_IsDeleted(obj) && bSpell == true){
	ObjRender_SetColor(obj,value,value,value);
	if(value<175){value+=5;}
	
	count++;
	yield;
	}
	descent(i in 0..30){
	ObjRender_SetAlpha(obj,i);
	yield;
	}
	Obj_Delete(obj);
}


task TSpellBackground2(){
	wait( 75 );
	let c = 0;
	let s = 0;
	let value = 0;
	let count = 0;
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,21);
	ObjPrim_SetTexture(obj,sbg2);
	ObjSprite2D_SetSourceRect(obj,0,0,256,256);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,3.2,3.2,0);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	ObjRender_SetPosition(obj,640/2,480/2,0);
	ObjRender_SetAlpha( obj, 255/2.5 );
	while( !Obj_IsDeleted(obj) && bSpell == true){
	ObjRender_SetAngleXYZ(obj,0,180,count/4);
	count -= 1;
	yield;
	}
	descent(i in 0..30){
	ObjRender_SetAlpha(obj,i);
	yield;
	}
	Obj_Delete(obj);
}	

task TSpellBackground3(){
	wait( 75 );
	let c = 0;
	let s = 0;
	let value = 0;
	let count = 0;
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,21);
	ObjPrim_SetTexture(obj,sbg2);
	ObjSprite2D_SetSourceRect(obj,0,0,256,256);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,3.2,3.2,0);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	ObjRender_SetPosition(obj,640/2,480/2,0);
	ObjRender_SetAlpha( obj, 255/3 );
	while( !Obj_IsDeleted(obj) && bSpell == true){
	ObjRender_SetAngleXYZ(obj,0,0,108 + count/4);
	count -= 1.5;
	yield;
	}
	descent(i in 0..30){
	ObjRender_SetAlpha(obj,i);
	yield;
	}
	Obj_Delete(obj);
}

task TSpellBackground4(){
	let c = 0;
	let s = 0;
	let value = 0;
	let count = 0;
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,21);
	ObjPrim_SetTexture(obj,sbg2);
	ObjSprite2D_SetSourceRect(obj,0,0,640,512);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,1,1,1);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	ObjRender_SetAngleXYZ(obj,0,0,180);
	ObjRender_SetColor(obj,55,55,55);
	ObjRender_SetPosition(obj,640/2,480/2,0);
	while( !Obj_IsDeleted(obj) && bSpell == true){
	ObjRender_SetColor(obj,value,value,value);
	if(value<145){value+=1;}
	ObjSprite2D_SetSourceRect(obj,0,count,-512,-512+count);
	count+=1;
	yield;
	}
	descent(i in 0..30){
	ObjRender_SetAlpha(obj,i);
	yield;
	}
	Obj_Delete(obj);
}

task TSpellBackground5(){
	let c = 0;
	let s = 0;
	let value = 0;
	let count = 0;
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,21);
	ObjPrim_SetTexture(obj,sbg3);
	ObjSprite2D_SetSourceRect(obj,0,0,256,256);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,2.5,2.5,2.5);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	ObjRender_SetPosition(obj,384/2,448/2,0);
	while( !Obj_IsDeleted(obj) && bSpell == true){
	ObjRender_SetColor(obj,155,155,155);
	if(value<255){value+=5;}
	ObjRender_SetAngleXYZ(obj,0,0,count/2);
	count++;
	yield;
	}
	descent(i in 0..30){
	ObjRender_SetAlpha(obj,i);
	yield;
	}
	Obj_Delete(obj);
}

