let GCSD=GetCurrentScriptDirectory();
let bSpell = false;
let current = GetCurrentScriptDirectory();
let stgbg = GCSD~"img/stg6bg.png";
let stgbg2 = GCSD~"img/stg3bg4.png";
let bg1 = GCSD~"img/cdbg06a.png";
let bg2 = GCSD~"img/cdbg06b.png";
let fo = 0;
LoadTexture(bg2);
LoadTexture(bg1);
@Initialize
{
	SetCameraFocusX(180);
	SetCameraFocusY(0);
	SetCameraFocusZ(0);
	SetCameraRadius(300);
	SetCameraElevationAngle(230);
	SetCameraAzimuthAngle(90);
	TCallBg;
}

@MainLoop
{

	let objScene = GetEnemyBossSceneObjectID();
	if(ObjEnemyBossScene_GetInfo(objScene, INFO_IS_SPELL))
	{
		bSpell = true;
	}
	else
	{
		bSpell = false;
	}
	fo++;
	SetCameraFocusY(180+5*sin(fo));

	yield;
}

task TCallBg{
	yield;
	while(1){
	if(bSpell==false){
	TNormalBackground(0,50);
	TFalling;
		}
	while(bSpell==false){ yield; }
	if(bSpell==true){
		TSpell;
		TSpell2;
		}
	while(bSpell==true){ yield; }
	yield;
	}
}	

task TNormalBackground(dir,alpha){
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	let c = 0;
	let s = 0;
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	Obj_SetRenderPriority(obj,0.21);
	ObjPrim_SetTexture(obj,stgbg);
	ObjSprite2D_SetSourceRect(obj,0,0,512,448);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,1,1,1);
	ObjRender_SetX(obj,GetStgFrameWidth()/2);
	ObjRender_SetY(obj,GetStgFrameHeight()/2);
	while( !Obj_IsDeleted(obj) && bSpell == false){
	if(c<60){s+=3;}
	ObjRender_SetAlpha(obj,s);
	c++;
	yield;
	}
	descent(i in 0..60){
	ObjRender_SetAlpha(obj,i*2);
	yield;
	}
	Obj_Delete(obj);
}

task TNormalBackground2(dir,way){
	let c=0;
	let s=1600;
	let b=s/2;
	let f = 0;
	let alpha = 0;
	let size = rand(1.75,2.25);
	let color = [0,0,0];
	let obj=ObjPrim_Create(OBJ_SPRITE_3D);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	Obj_SetRenderPriorityI(obj,20);
	ObjPrim_SetTexture(obj,stgbg2);
	ObjRender_SetPermitCamera(obj,true);
	ObjSprite3D_SetSourceRect(obj,0,0,s,s);
	ObjSprite3D_SetDestRect(obj,-b,-b,b,b);
	ObjRender_SetScaleXYZ(obj,size,size,1.5);
	ObjRender_SetPosition(obj,0+ObjRender_GetX(obj),0+ObjRender_GetY(obj),0);
	while(bSpell==false){
	if(alpha<192){alpha+=5;}
	if(way==1){ObjRender_SetColor(obj,alpha/6,alpha/2,alpha/4);}
	if(way==-1){ObjRender_SetColor(obj,alpha/6,alpha,alpha/2);}
	ObjRender_SetAngleXYZ(obj,0,0,dir);
	ObjSprite3D_SetSourceRect(obj,0+c*0.5*way,0+c*1.75,s+c*0.5*way,s+c*1.75);
	c++;
	yield;
	}
	descent(i in 0..60){
	ObjRender_SetColor(obj,i*2,i*2,i*2);
	yield;
	}
	ObjRender_SetColor(obj,0,0,0);
	Obj_Delete(obj);
}

task TFalling{
	while(bSpell==false){
	if(GetCommonData("Effect",2)){TMaple(rand(0,416),rand(-30,0));}
	loop((3-GetCommonData("Effect",2))*15){
	yield;
	}
	}
		task TMaple(px,py){
		let obj=ObjPrim_Create(OBJ_SPRITE_2D);
		let maple = GCSD~"img/eff2.png";
		let cy = rand(1,3);
		let cx = rand(-0.5,0.5);
		let ang = [0,0,90];
		let ap = [rand(-0.5,0.5),rand(0,0),rand(1,3)];
		let size = cy/3.5;
		let s = rand(-1,1);
		let color = [rand(255,255),rand(0,64),0];
		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,30);
		ObjSprite2D_SetSourceRect(obj,192-160,224,224-160,256);
		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*2);
				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]+90,ang[1]+90,ang[2]);
			ang[0] = ang[0]+ap[0];
			ang[1] = ang[1]+ap[0]*-1;
			ang[2] = ang[2]+ap[2];
			py += cy;
			px += cx;
			yield;
			}
			Obj_Delete(obj);
		}
	}


task TSpell{
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	let c = 0;
	let s = 0;
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	Obj_SetRenderPriority(obj,0.21);
	ObjPrim_SetTexture(obj,bg1);
	ObjSprite2D_SetSourceRect(obj,0,0,384,448);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,1,1,1);
	ObjRender_SetX(obj,GetStgFrameWidth()/2);
	ObjRender_SetY(obj,GetStgFrameHeight()/2);
	while( !Obj_IsDeleted(obj) && bSpell == true){
	if(c<60){s+=4;}
	ObjRender_SetColor(obj,s,s,s);
	ObjRender_SetAlpha(obj,s);
	c++;
	yield;
	}
	descent(i in 0..60){
	ObjRender_SetAlpha(obj,i*2);
	yield;
	}
	Obj_Delete(obj);
}
	
task TSpell2{
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	let c = 0;
	let s = 0;
	Obj_SetRenderPriority(obj,0.21);
	ObjPrim_SetTexture(obj,bg2);
	ObjRender_SetScaleXYZ(obj,2,2,1);
	ObjSprite2D_SetSourceRect(obj,0,0,256,256);
	ObjRender_SetX(obj,GetStgFrameWidth()/2);
	ObjRender_SetY(obj,GetStgFrameHeight()/2);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetBlendType(obj,BLEND_ADD_RGB);
	while( !Obj_IsDeleted(obj) && bSpell == true){
	if(c<60){s+=2;}
	ObjRender_SetColor(obj,s*sin(c),s*0.75*sin(c+120),s*1.5*sin(c+240));
	ObjSprite2D_SetSourceRect(obj,0,c,512,512+c);
	c++;
	yield;
	}
	descent(i in 0..60){
	ObjRender_SetColor(obj,i*4,i*4,i*4);
	yield;
	}
	Obj_Delete(obj);
}


