let GCSD=GetCurrentScriptDirectory;
let path=GCSD~"img/water.png";
let path2=GCSD~"img/bg2.png";
let path3=GCSD~"img/bg2b.png";
let objScene=GetEnemyBossSceneObjectID();
let bspell=false;

@Initialize{
	SetCameraFocusX(0);
	SetCameraFocusY(0);
	SetCameraFocusZ(0);
	SetCameraRadius(650);
	SetCameraElevationAngle(-50);
	TCallBg;
}

@MainLoop{
	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){
		ascent(i in -3..3){
			ascent(k in -3..3){
				TDraw(k*512,i*512);
			}
		}
	}
	while(bspell==false){ yield; }
	if(bspell==true){
		TDrawSpell;
		}
	while(bspell==true){ yield; }
	}
	ascent(i in -3..3){
		ascent(k in -3..3){
			TDraw(k*512,i*512);
		}
	}
}

task TDraw(x,y){
	let a=0;
	SetFogParam(0,1000,0,128,255);
	let obj=ObjPrim_Create(OBJ_SPRITE_3D);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	Obj_SetRenderPriorityI(obj,20);
	ObjPrim_SetTexture(obj,path);
	let s=512;
	let b=s/2;
	ObjSprite3D_SetSourceRect(obj,0,0,s,s);
	ObjSprite3D_SetDestRect(obj,-b,-b,b,b);
	ObjRender_SetPosition(obj,x+ObjRender_GetX(obj),y+ObjRender_GetY(obj),0);
	let x2=0;
	let y2=0;
	TCheck;
	loop{
		ObjSprite3D_SetSourceRect(obj,0,0+y2,s,s+y2);
		SetCameraAzimuthAngle(sin(a)*12.5+90);
		x2+=20;
		y2+=20;
		a+=0.75;
		yield;
	}
	task TCheck{
		let alpha=255;
		while(!Obj_IsDeleted(obj)){
			while(bspell==false){ yield; }
			loop(30){
				ObjRender_SetAlpha(obj,alpha);
				alpha-=255/30;
				yield;
			}
			Obj_Delete(obj);
		}
	}
}

task TDrawSpell{
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	let obj2=ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj,path2);
	ObjPrim_SetTexture(obj2,path3);
	ObjSprite2D_SetSourceRect(obj,0,0,512,512);
	ObjSprite2D_SetSourceRect(obj2,0,0,1024,1024);
	ObjSprite2D_SetDestCenter(obj);
	ObjSprite2D_SetDestCenter(obj2);
	ObjRender_SetAlpha(obj,0);
	ObjRender_SetAlpha(obj2,0);
	Obj_SetRenderPriorityI(obj,20);
	Obj_SetRenderPriorityI(obj2,20);
	ObjRender_SetPosition(obj,GetStgFrameWidth/2,GetStgFrameHeight/2,0);
	ObjRender_SetScaleXYZ(obj,1.5,1.5,1.5);
	let alpha=0;
	let alpha2=0;
	TAction;
	TCheck;
	let objalpha=192;
	let obj2alpha=100;
	loop(30){
		ObjRender_SetAlpha(obj,alpha);
		ObjRender_SetAlpha(obj2,alpha2);
		alpha+=objalpha/30;
		alpha2+=obj2alpha/30;
		yield;
	}
	task TAction{
		let angle=0;
		let x=0;
		let y=0;
		while(Obj_IsDeleted(obj)==false&&Obj_IsDeleted(obj2)==false){
			ObjRender_SetAngleXYZ(obj,0,0,angle);
			ObjSprite2D_SetSourceRect(obj2,0,0+y,1024,1024+y);
			angle+=0.25;
			x++;
			y++;
			yield;
		}
	}
	task TCheck{
		while(bspell==true){ yield; }
		loop(20){
			ObjRender_SetAlpha(obj,alpha);
			ObjRender_SetAlpha(obj2,alpha2);
			alpha-=objalpha/20;
			alpha2-=obj2alpha/20;
			yield;
		}
		Obj_Delete(obj);
		Obj_Delete(obj2);
	}
}