let bSpell=false;
let GCSD=GetCurrentScriptDirectory();
let bg1=GCSD~"img/bg1.png";
LoadTexture(bg1);
let bg2=GCSD~"img/bg2.png";
let bg3=GCSD~"img/bg3.png";
LoadTexture(bg3);
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;
		}
	while(bSpell==false){ yield; }
	if(bSpell==true){
		TSpellBackground;
		TSpellBackground2;
		}
	while(bSpell==true){ yield; }
}


task TNormalBackground{
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	let c = 0;
	let n = 0;
	ObjRender_SetBlendType(obj,BLEND_ADD_RGB);
	Obj_SetRenderPriority(obj,0.21);
	ObjPrim_SetTexture(obj,bg3);
	ObjSprite2D_SetSourceRect(obj,0,0,640,720);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,1,1,1);
	ObjRender_SetX(obj,GetStgFrameWidth()/2);
	ObjRender_SetY(obj,GetStgFrameHeight()/2);
	while(bSpell==false){
	c++;
	ObjRender_SetAngleXYZ(obj,0,0,c/75-15);
	ObjRender_SetColor(obj,64,128+32*sin(c),256+64*sin(c));
	ObjSprite2D_SetSourceRect(obj,c*0.25,c/5,640+c*0.25,720+c/5);
	if(c%40==0){
	effball(rand(0,416),500,rand(0.25,0.75),n%4);
	n++;
	}
	if(c%20==0){
	halo(416,rand(0,464),rand(0.5,1.5));
	}
	yield;
	}
	descent(i in 0..60){
	ObjRender_SetColor(obj,i*4,i*4,i*4);
	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;
	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(bSpell==true){
	if(a<252){a+=4;}
	ObjRender_SetAlpha(obj,a);
	yield;
	}
	Obj_Delete(obj);
	}


task TSpellBackground2{
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	let c = 0;
	let a = 0;
	ObjRender_SetBlendType(obj,BLEND_MULTIPLY);
	Obj_SetRenderPriority(obj,0.21);
	ObjPrim_SetTexture(obj,bg2);
	ObjSprite2D_SetSourceRect(obj,0,0,512,512);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,1,1,1);
	ObjRender_SetX(obj,GetStgFrameWidth()/2);
	ObjRender_SetY(obj,GetStgFrameHeight()/2);
	while(bSpell==true){
	if(a<252){a+=4;}
	ObjRender_SetAlpha(obj,a);
	ObjRender_SetColor(obj,256,256,256);
	ObjSprite2D_SetSourceRect(obj,0,c,512,512+c);
	c++;
	yield;
	}
	Obj_Delete(obj);
	}
}