let bSpell=false;
let GCSD=GetCurrentScriptDirectory();
let stgbg = GCSD~"img/stagebg2.png";
LoadTexture(stgbg);
let stgbg2 = GCSD~"img/stagebg4.png";
let stgbg3 = GCSD~"img/stagebg3.png";
LoadTexture(stgbg2);
let bg1=GCSD~"img/spellbg1.png";
LoadTexture(bg1);
let bg2=GCSD~"img/spellbg2.png";
LoadTexture(bg2);
let ball=GCSD~"img/ball.png";
LoadTexture(ball);

@Initialize{
	TCallBg;
}

@MainLoop{
	let objScene=GetEnemyBossSceneObjectID();
	if(objScene!=ID_INVALID&&ObjEnemyBossScene_GetInfo(objScene,INFO_IS_SPELL)){
		bSpell=true;
	}else{
		bSpell=false;
	}
	yield;
}

task TCallBg{
	while(!Obj_IsDeleted(GetEnemyBossSceneObjectID())){
	if(bSpell==false){
		TNormalBackground;
		TNormalBackground2;
		}
	while(bSpell==false){ yield; }
	if(bSpell==true){
		TSpellBackground;
		TSpellBackground2;
		}
	while(bSpell==true){ yield; }
}


task TNormalBackground{
	let c=0;
	let s=1600;
	let b=s/2;
	let obj=ObjPrim_Create(OBJ_SPRITE_3D);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetAlpha(obj,128);
	Obj_SetRenderPriorityI(obj,20);
	ObjPrim_SetTexture(obj,stgbg);
	//ObjSprite3D_SetBillboard(obj,true);
	ObjSprite3D_SetSourceRect(obj,0,0,s,s);
	ObjSprite3D_SetDestRect(obj,-b,-b,b,b);
	ObjRender_SetScaleXYZ(obj,1.75,1.75,1.75);
	ObjRender_SetPosition(obj,0+ObjRender_GetX(obj),0+ObjRender_GetY(obj),0);
	ObjRender_SetColor(obj,128,128,128);
	while(bSpell==false){
	SetFogParam(0,1000,96+32*sin(c/5),128+32*sin(c/5+120),128+32*sin(c/5+240));
	ObjRender_SetAngleXYZ(obj,90,90,200-10*sin(c/10));
	ObjSprite3D_SetSourceRect(obj,0,0+c*1,s,s+c*1);
	c++;
	yield;
	}
	descent(i in 0..60){
	SetFogParam(0,1000,i*2.5,i*2.5,i*2.5);
	ObjRender_SetColor(obj,i*2,i*2,i*2);
	yield;
	}
	Obj_Delete(obj);
}

task TNormalBackground2{
	let c=0;
	let s=1600;
	let b=s/2;
	let obj=ObjPrim_Create(OBJ_SPRITE_3D);
	ObjRender_SetBlendType(obj,BLEND_ADD_RGB);
	Obj_SetRenderPriorityI(obj,20);
	ObjPrim_SetTexture(obj,stgbg2);
	//ObjSprite3D_SetBillboard(obj,true);
	ObjRender_SetScaleXYZ(obj,1.75,1.75,1.75);
	ObjSprite3D_SetSourceRect(obj,0,0,s,s);
	ObjSprite3D_SetDestRect(obj,-b,-b,b,b);
	ObjRender_SetPosition(obj,0+ObjRender_GetX(obj),0+ObjRender_GetY(obj),0);
	while(bSpell==false){
	//SetCameraFocusX(-240);
	SetCameraFocusY(0);
	SetCameraFocusZ(300);
	ObjRender_SetColor(obj,128,128,128);
	ObjRender_SetAngleXYZ(obj,90,90,190+20*sin(c/10));
	ObjSprite3D_SetSourceRect(obj,0,0+c*1,s,s+c*1);
	c++;
	yield;
	}
	descent(i in 0..60){
	ObjRender_SetColor(obj,i*2,i*2,i*2);
	yield;
	}
	Obj_Delete(obj);
}

task effball(x,y,size,color){
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	let xspd = 0;
	let yspd = 0;
	let xspeed = rand(-0.75,0.75);
	let yspeed = -size;
	let c = [0,0,0];
	let s = 0;
	if(color == 0){c = [64,0,0];}
	if(color == 1){c = [0,64,0];}
	if(color == 2){c = [0,0,64];}
	if(color == 3){c = [64,64,0];}
	ObjRender_SetBlendType(obj,BLEND_ADD_RGB);
	ObjPrim_SetTexture(obj,ball);
	ObjSprite2D_SetSourceRect(obj,0,0,256,256);
	ObjRender_SetScaleXYZ(obj,size,size,0);
	ObjSprite2D_SetDestCenter(obj);
	while(bSpell==false){
	ObjRender_SetPosition(obj,x+xspd,y+yspd,0);
	ObjRender_SetColor(obj,c[0]+64*sin(s),c[1]+64*sin(s),c[2]+64*sin(s));
	yspd += yspeed;
	xspd += xspeed;
	s++;
	yield;
	}
	Obj_Delete(obj);
	}

task halo(x,y,size){
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	let img=GCSD~"img/eff.png";
	let xspd = 0;
	let yspd = 0;
	let xspeed = rand(-size*2,-0.2*size);
	let yspeed = rand(-size*2,size);
	ObjRender_SetBlendType(obj,BLEND_ADD_RGB);
	ObjPrim_SetTexture(obj,img);
	ObjSprite2D_SetSourceRect(obj,32,224,64,256);
	ObjRender_SetScaleXYZ(obj,size,size,0);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetColor(obj,64,64,128);
	while(bSpell==false){
	ObjRender_SetPosition(obj,x+xspd,y+yspd,0);
	yspd += yspeed;
	xspd += xspeed;
	ObjRender_SetAngleXYZ(obj,-xspd,-yspd,xspd+yspd);
	yield;
	}
	Obj_Delete(obj);
	}

task TSpellBackground{
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	let a = 0;
	let c = 0;
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	Obj_SetRenderPriority(obj,0.21);
	ObjPrim_SetTexture(obj,bg1);
	ObjSprite2D_SetSourceRect(obj,0,0,416,464);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,1,1,1);
	ObjRender_SetX(obj,GetStgFrameWidth()/2);
	ObjRender_SetY(obj,GetStgFrameHeight()/2);
	ObjRender_SetAngleXYZ(obj,0,0,180);
	while(bSpell==true){
	if(a<250){a+=10;}
	ObjRender_SetAlpha(obj,a/3);
	ObjSprite2D_SetSourceRect(obj,0,0+c,416,464+c);
	c+=2;
	yield;
	}
	descent(i in 0..60){
	ObjRender_SetAlpha(obj,i*4);
	yield;
	}
	Obj_Delete(obj);
	}


task TSpellBackground2{
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	let c = 0;
	let a = 0;
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	Obj_SetRenderPriority(obj,0.21);
	ObjPrim_SetTexture(obj,bg2);
	ObjSprite2D_SetSourceRect(obj,0,0,256,256);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,1.5,1.75,1);
	ObjRender_SetX(obj,GetStgFrameWidth()/2);
	ObjRender_SetY(obj,GetStgFrameHeight()/2);
	while(bSpell==true){
	if(a<250){a+=10;}
	ObjRender_SetAlpha(obj,a);
	c++;
	yield;
	}
	descent(i in 0..60){
	ObjRender_SetAlpha(obj,i*4);
	yield;
	}
	Obj_Delete(obj);
	}
}