let checkspell = false;

let sceneObj = GetEnemyBossSceneObjectID;

let CSD = GetCurrentScriptDirectory;
let nswall = CSD ~ "img/stage5a00.png";
let nswallwindow = CSD ~ "img/stage5b00.png";
let nswindow = CSD ~ "img/stage5c00.png";
let null = CSD ~ "img/white.png";
let maple = CSD ~ "img/maple.png";
let s = CSD ~ "img/cdbg05a00.png";
let so = CSD ~ "img/cdbg05b00.png";

LoadTexture(nswall);
LoadTexture(nswallwindow);
LoadTexture(nswindow);
LoadTexture(s);
LoadTexture(so);

@Initialize{
	call;
	SetCameraFocusX(540);
	SetCameraPerspectiveClip(10,2000);
	SetFogEnable(true);
	SetFogParam(20,500,0,0,32);
	SetCameraFocusY(1000);
	SetCameraFocusZ(540);
	SetCameraRadius(70);
	SetCameraElevationAngle(269);
	SetCameraAzimuthAngle(90);
	SetStgFrame(0,0,GetScreenWidth,GetScreenHeight,20,80);
	SetPlayerClip(8,16,GetScreenWidth-8,GetScreenHeight-16);
}

@MainLoop{
	if(sceneObj != ID_INVALID && ObjEnemyBossScene_GetInfo(sceneObj, INFO_IS_SPELL)){
		checkspell = true;
	}

	else{
		checkspell = false;
	}
	yield;
}

task call{
	while(!Obj_IsDeleted(sceneObj)){
	
		if(checkspell == false){
		background;
		}
		
		while(checkspell == false){ yield;}
	
		if(checkspell == true){
		spellbackground;
		}
	
		while(checkspell == true){ yield;}
		yield;
	}
}

task background{
	let slide = 0;
	let slide2 = 0;
	let slide3 = 0;
	let alpha = 255;
	let down = 0;
	
			let bgobj = ObjPrim_Create(OBJ_SPRITE_2D);
			ObjPrim_SetTexture(bgobj,null);
			ObjRender_SetBlendType(bgobj,BLEND_ALPHA);
			ObjSprite2D_SetSourceRect(bgobj,0,0,2048,2048);
			ObjSprite2D_SetDestRect(bgobj,0,0,2048,2048);
			ObjSprite2D_SetDestCenter(bgobj);
			ObjRender_SetAlpha(bgobj,255);
			ObjRender_SetColor(bgobj,0,0,32);
			ObjRender_SetPosition(bgobj,GetStgFrameWidth/2-198,0,0);
			
			let obj1 = ObjPrim_Create(OBJ_SPRITE_3D);
			ObjPrim_SetTexture(obj1,nswallwindow);
			ObjRender_SetBlendType(obj1,BLEND_ALPHA);
			ObjRender_SetAlpha(obj1,255);
			ObjSprite3D_SetDestRect(obj1,0,0,224,2048);
			ObjSprite3D_SetSourceRect(obj1,0,0,224,2048);
			ObjSprite3D_SetSourceDestRect(obj1,0,0,262,2048);
			ObjRender_SetScaleX(obj1,1);
			ObjRender_SetScaleY(obj1,1);
			ObjRender_SetPosition(obj1,GetStgFrameWidth/2-198,0,-189);
			
			let obj1a = ObjPrim_Create(OBJ_SPRITE_3D);
			ObjPrim_SetTexture(obj1a,nswindow);
			ObjRender_SetBlendType(obj1a,BLEND_ADD_ARGB);
			ObjRender_SetAlpha(obj1a,255);
			ObjSprite3D_SetDestRect(obj1a,0,0,224,2048);
			ObjSprite3D_SetSourceRect(obj1a,0,0,224,2048);
			ObjSprite3D_SetSourceDestRect(obj1a,0,0,262,2048);
			ObjRender_SetScaleX(obj1a,1);
			ObjRender_SetScaleY(obj1a,1);
			ObjRender_SetPosition(obj1a,GetStgFrameWidth/2-198,0,-189);
			
			let obj5 = ObjPrim_Create(OBJ_SPRITE_3D);
			ObjPrim_SetTexture(obj5,nswall);
			ObjRender_SetBlendType(obj5,BLEND_ALPHA);
			ObjRender_SetAlpha(obj5,255);
			ObjSprite3D_SetDestRect(obj5,0,0,224,2048);
			ObjSprite3D_SetSourceRect(obj5,0,0,224,2048);
			ObjSprite3D_SetSourceDestRect(obj5,0,0,224,2048);
			ObjRender_SetScaleX(obj5,1);
			ObjRender_SetScaleY(obj5,1);
			ObjRender_SetPosition(obj5,GetStgFrameWidth/2-201+189,0,-96);
			
			let obj6 = ObjPrim_Create(OBJ_SPRITE_3D);
			ObjPrim_SetTexture(obj6,nswallwindow);
			ObjRender_SetBlendType(obj6,BLEND_ALPHA);
			ObjRender_SetAlpha(obj6,255);
			ObjSprite3D_SetDestRect(obj6,0,0,224,2048);
			ObjSprite3D_SetSourceRect(obj6,0,0,224,2048);
			ObjSprite3D_SetSourceDestRect(obj6,0,0,232,2048);
			ObjRender_SetScaleX(obj6,1);
			ObjRender_SetScaleY(obj6,1);
			ObjRender_SetPosition(obj6,GetStgFrameWidth/2-198+189,0,96);
			
			let obj6a = ObjPrim_Create(OBJ_SPRITE_3D);
			ObjPrim_SetTexture(obj6a,nswindow);
			ObjRender_SetBlendType(obj6a,BLEND_ADD_ARGB);
			ObjRender_SetAlpha(obj6a,255);
			ObjSprite3D_SetDestRect(obj6a,0,0,224,2048);
			ObjSprite3D_SetSourceRect(obj6a,0,0,224,2048);
			ObjSprite3D_SetSourceDestRect(obj6a,0,0,232,2048);
			ObjRender_SetScaleX(obj6a,1);
			ObjRender_SetScaleY(obj6a,1);
			ObjRender_SetPosition(obj6a,GetStgFrameWidth/2-198+189,0,94);
			
			let obj4 = ObjPrim_Create(OBJ_SPRITE_3D);
			ObjPrim_SetTexture(obj4,nswall);
			ObjRender_SetBlendType(obj4,BLEND_ALPHA);
			ObjRender_SetAlpha(obj4,255);
			ObjSprite3D_SetDestRect(obj4,0,0,224,2048);
			ObjSprite3D_SetSourceRect(obj4,0,0,224,2048);
			ObjSprite3D_SetSourceDestRect(obj4,0,0,232,2048);
			ObjRender_SetScaleX(obj4,1);
			ObjRender_SetScaleY(obj4,1);
			ObjRender_SetPosition(obj4,GetStgFrameWidth/2-198-189,0,-94);
			
			let obj3 = ObjPrim_Create(OBJ_SPRITE_3D);
			ObjPrim_SetTexture(obj3,nswallwindow);
			ObjRender_SetBlendType(obj3,BLEND_ALPHA);
			ObjRender_SetAlpha(obj3,255);
			ObjSprite3D_SetDestRect(obj3,0,0,224,2048);
			ObjSprite3D_SetSourceRect(obj3,0,0,224,2048);
			ObjSprite3D_SetSourceDestRect(obj3,0,0,232,2048);
			ObjRender_SetScaleX(obj3,1);
			ObjRender_SetScaleY(obj3,1);
			ObjRender_SetPosition(obj3,GetStgFrameWidth/2-198-189,0,94);
			
			let obj3a = ObjPrim_Create(OBJ_SPRITE_3D);
			ObjPrim_SetTexture(obj3a,nswindow);
			ObjRender_SetBlendType(obj3a,BLEND_ADD_ARGB);
			ObjRender_SetAlpha(obj3a,255);
			ObjSprite3D_SetDestRect(obj3a,0,0,224,2048);
			ObjSprite3D_SetSourceRect(obj3a,0,0,224,2048);
			ObjSprite3D_SetSourceDestRect(obj3a,0,0,232,2048);
			ObjRender_SetScaleX(obj3a,1);
			ObjRender_SetScaleY(obj3a,1);
			ObjRender_SetPosition(obj3a,GetStgFrameWidth/2-198-189,0,94);
			
			let obj2 = ObjPrim_Create(OBJ_SPRITE_3D);
			ObjPrim_SetTexture(obj2,nswall);
			ObjRender_SetBlendType(obj2,BLEND_ALPHA);
			ObjRender_SetAlpha(obj2,255);
			ObjSprite3D_SetDestRect(obj2,0,0,224,2048);
			ObjSprite3D_SetSourceRect(obj2,0,0,224,2048);
			ObjSprite3D_SetSourceDestRect(obj2,0,0,262,2048);
			ObjRender_SetScaleX(obj2,1);
			ObjRender_SetScaleY(obj2,1);
			ObjRender_SetPosition(obj2,GetStgFrameWidth/2-198,0,189);
			
			ObjRender_SetAngleXYZ(obj6,0,60,0);
			ObjRender_SetAngleXYZ(obj6a,0,60,0);
			ObjRender_SetAngleXYZ(obj5,0,-60,0);
			ObjRender_SetAngleXYZ(obj4,0,60,0);
			ObjRender_SetAngleXYZ(obj3,0,-60,0);
			ObjRender_SetAngleXYZ(obj3a,0,-60,0);
			ObjRender_SetAngleXYZ(obj2,0,0,0);
			ObjRender_SetAngleXYZ(obj1,0,0,0);
			ObjRender_SetAngleXYZ(obj1a,0,0,0);
			
			Obj_SetRenderPriority(bgobj,0.2);
			Obj_SetRenderPriority(obj1,0.2);
			Obj_SetRenderPriority(obj1a,0.2);
			Obj_SetRenderPriority(obj2,0.2);
			Obj_SetRenderPriority(obj3,0.2);
			Obj_SetRenderPriority(obj3a,0.2);
			Obj_SetRenderPriority(obj4,0.2);
			Obj_SetRenderPriority(obj5,0.2);
			Obj_SetRenderPriority(obj6,0.2);
			Obj_SetRenderPriority(obj6a,0.2);
			
			ObjRender_SetZWrite(obj1,true);
			ObjRender_SetZTest(obj1,true);
			ObjRender_SetZWrite(obj1a,true);
			ObjRender_SetZTest(obj1a,true);
			ObjRender_SetZWrite(obj2,true);
			ObjRender_SetZTest(obj2,true);
			ObjRender_SetZWrite(obj3,true);
			ObjRender_SetZTest(obj3,true);
			ObjRender_SetZWrite(obj3a,true);
			ObjRender_SetZTest(obj3a,true);
			ObjRender_SetZWrite(obj4,true);
			ObjRender_SetZTest(obj4,true);
			ObjRender_SetZWrite(obj5,true);
			ObjRender_SetZTest(obj5,true);
			ObjRender_SetZWrite(obj6,true);
			ObjRender_SetZTest(obj6,true);
			ObjRender_SetZWrite(obj6a,true);
			ObjRender_SetZTest(obj6a,true);
			
						ObjRender_SetColor(obj6a,24,24,24);
						ObjRender_SetColor(obj3a,24,24,24);
						ObjRender_SetColor(obj1a,24,24,24);
			
			lightning;
			
			task lightning{
				let a = 0;
				while(Obj_IsVisible(obj1a) || Obj_IsVisible(obj3a) || Obj_IsVisible(obj6a)){
					loop(240){yield;}
						ObjRender_SetColor(obj6a,255,255,255);
						ObjRender_SetColor(obj3a,255,255,255);
						ObjRender_SetColor(obj1a,255,255,255);
					loop(2){yield;}
						ObjRender_SetColor(obj6a,24,24,24);
						ObjRender_SetColor(obj3a,24,24,24);
						ObjRender_SetColor(obj1a,24,24,24);
					loop(2){yield;}
					loop(247){
						ObjRender_SetColor(obj6a,255-a,255-a,255-a);
						ObjRender_SetColor(obj3a,255-a,255-a,255-a);
						ObjRender_SetColor(obj1a,255-a,255-a,255-a);
						a+=1;
						yield;
					}
					a = 0;
						ObjRender_SetColor(obj6a,24,24,24);
						ObjRender_SetColor(obj3a,24,24,24);
						ObjRender_SetColor(obj1a,24,24,24);
				yield;
				}
			}
			
		loop{
		if(checkspell == true){dissolve;}
			ObjSprite3D_SetSourceRect(obj6,0,0+slide3,224,2048+slide3);
			ObjSprite3D_SetSourceRect(obj6a,0,0+slide3,224,2048+slide3);
			
			ObjSprite3D_SetSourceRect(obj5,0,0+slide3,224,2048+slide3);
			
			ObjSprite3D_SetSourceRect(obj4,0,0+slide2,224,2048+slide2);
			
			ObjSprite3D_SetSourceRect(obj3,0,0+slide2,224,2048+slide2);
			ObjSprite3D_SetSourceRect(obj3a,0,0+slide2,224,2048+slide2);
			
			ObjSprite3D_SetSourceRect(obj2,0,0+slide2,224,2048+slide2);
			
			ObjSprite3D_SetSourceRect(obj1,0,0+slide2,224,2048+slide2);
			ObjSprite3D_SetSourceRect(obj1a,0,0+slide2,224,2048+slide2);
		
			slide3+=3;
			slide2+=3;
			slide+=2;
			
			SetCameraPitch(-125);
			SetCameraYaw(90-slide/6);
			SetCameraFocusX(680*cos(slide/6)+320);
			SetCameraFocusZ(720*sin(slide/6));
			
			yield;
		}
		task dissolve{
			loop(255){
				ObjRender_SetAlpha(bgobj,255-down);
				ObjRender_SetAlpha(obj1,255-down);
				ObjRender_SetAlpha(obj1a,255-down);
				ObjRender_SetAlpha(obj2,255-down);
				ObjRender_SetAlpha(obj3,255-down);
				ObjRender_SetAlpha(obj3a,255-down);
				ObjRender_SetAlpha(obj4,255-down);
				ObjRender_SetAlpha(obj5,255-down);
				ObjRender_SetAlpha(obj6,255-down);
				ObjRender_SetAlpha(obj6a,255-down);
				down+=1;
				yield;
			}
			down = 0;
	Obj_Delete(bgobj);
	Obj_Delete(obj1);
	Obj_Delete(obj1a);
	Obj_Delete(obj2);
	Obj_Delete(obj3);
	Obj_Delete(obj3a);
	Obj_Delete(obj4);
	Obj_Delete(obj5);
	Obj_Delete(obj6);
	Obj_Delete(obj6a);
		}
	yield;
}

task spellbackground{

	let slide = 0;
	let down = 0;
		
			let obj = ObjPrim_Create(OBJ_SPRITE_2D);
			Obj_SetRenderPriority(obj, 0.21);
			ObjPrim_SetTexture(obj,s);
			ObjRender_SetBlendType(obj,BLEND_ALPHA);
			ObjSprite2D_SetDestCenter(obj);
			ObjRender_SetScaleXYZ(obj,1.675,1,1);
			ObjSprite2D_SetDestRect(obj,-1024,-512,1024,512);
			ObjRender_SetPosition(obj,GetStgFrameWidth/2,0,0);
			
			let obj1 = ObjPrim_Create(OBJ_SPRITE_2D);
			Obj_SetRenderPriority(obj1, 0.21);
			ObjPrim_SetTexture(obj1,so);
			ObjRender_SetBlendType(obj1,BLEND_SUBTRACT);
			ObjSprite2D_SetDestCenter(obj1);
			ObjRender_SetScaleXYZ(obj1,1.675,1,1);
			ObjSprite2D_SetDestRect(obj1,-1024,-512,1024,512);
			ObjRender_SetPosition(obj1,GetStgFrameWidth/2,0,0);
			alp;
			
			task alp{
			loop(255){
				ObjRender_SetAlpha(obj,down);
				ObjRender_SetAlpha(obj1,down);
				down+=1;
				yield;
			}
			down = 0;
			}
			
		loop{
			if(checkspell == false){dissolve;}
		
			ObjSprite2D_SetSourceRect(obj1,0-slide/2,0-slide/2,2048-slide/2,1024-slide/2);
			
			ObjSprite2D_SetSourceRect(obj,0,0+slide,2048,1024+slide);
			slide+=2;
			yield;
		}
	task dissolve{
			loop(255){
				ObjRender_SetAlpha(obj,255-down);
				ObjRender_SetAlpha(obj1,255-down);
				down+=1;
				yield;
			}
			down = 0;
	Obj_Delete(obj);
	Obj_Delete(obj1);
	}
	yield;
}