let GCSD=GetCurrentScriptDirectory();
let bSpell = false;
let current = GetCurrentScriptDirectory();
let fo = 0;
let nbg1 = GCSD~"img/3d_7_0.png";
let nbg2 = GCSD~"img/7_1.png";
let nbg3 = GCSD~"img/3d_7_3.png";
let sbg1 = GCSD~"img/04_07_2.png";
let sbg2 = GCSD~"img/04_06.png";
let sbg3 = GCSD~"img/05_00.png";
let sbg4 = GCSD~"img/Handmade_White-Black.png";
LoadTexture(nbg1);
LoadTexture(nbg2);
LoadTexture(nbg3);
LoadTexture(sbg1);
LoadTexture(sbg2);
LoadTexture(sbg3);
LoadTexture(sbg4);
@Initialize
{
	SetCameraFocusX(-50);
	SetCameraFocusY(200);
	SetCameraFocusZ(-512);
	SetCameraRadius(900);
	SetCameraAzimuthAngle(-90);
	SetCameraElevationAngle(-5);
	TCallBg;
	TreeGenerator;
}

@MainLoop
{

	let objScene = GetEnemyBossSceneObjectID();
	if(ObjEnemyBossScene_GetInfo(objScene, INFO_IS_SPELL))
	{
		bSpell = true;
	}
	else
	{
		bSpell = false;
	}
	fo++;
	yield;
}

task TCallBg{
	yield;
	SetFogParam(800,1800, 0, 0, 0);
	while(1){
	if(bSpell==false){
	TSakura;
	ascent(i in 0..6){
	TNormalBackground(-i*1024);
	}
	TNormalBackground2();
		}
	while(bSpell==false){ yield; }
	if(bSpell==true){
	    TSpellBackground4();
		TSpellBackground2();
		TSpellBackground();
		TSpellBackground3();
		}
	while(bSpell==true){ yield; }
	yield;
	}
}

task TSakura{
	while( bSpell == false){
	if(GetCommonData("Effect",2)){TMaple(rand(0,416),rand(0,0));}
	loop((3-GetCommonData("Effect",2))*9){
	yield;
	}
	}
		task TMaple(px,py){
		let obj=ObjPrim_Create(OBJ_SPRITE_2D);
		let maple = GetCurrentScriptDirectory ~"./img/eff_maple.png";
		let cy = rand(0.5,1.5);
		let ang = [0,0,90];
		let ap = [rand(-1,1),rand(0,0),rand(1,3)];
		let size = cy/3;
		let t = 0;
		let s = rand(-0.5,0.5);
		let color = [192,192,192];
		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,21);
		ObjSprite2D_SetSourceRect(obj,0,0,32,32);
		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*4);
				ObjRender_SetScaleXYZ(obj,size,size,0);
				size +=0.003;
				if(bSpell == true){break;}
				yield;
				}
				descent(i in 0..90){
				ObjRender_SetAlpha(obj,i*4);
				ObjRender_SetScaleXYZ(obj,size,size,0);
				size +=0.003;
				if(bSpell == true){break;}
				yield;
				}
				Obj_Delete(obj);
			}
			while( bSpell == false){
			ObjRender_SetPosition(obj,px,py,0);
			ObjRender_SetAngleXYZ(obj,0,0,t*4);
			ang[0] = ang[0]+ap[0];
			ang[1] = ang[1]+ap[0]*-1;
			ang[2] = ang[2]+ap[2];
			t++;
			py += cy;
			yield;
			}
			Obj_Delete(obj);
		}
	}

task TNormalBackground(z){ // floor
	let c = 0;
	let s = 0;
	let value = 255;
	let count = 0;
	let obj=ObjPrim_Create(OBJ_SPRITE_3D);
	Obj_SetRenderPriorityI(obj,21);
	ObjPrim_SetTexture(obj,nbg1);
	ObjSprite3D_SetSourceRect(obj,0,0,512*3,512);
	ObjSprite3D_SetSourceDestRect(obj,0,0,2048,1024);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetAngleXYZ(obj,90,0,0);
	ObjRender_SetPosition(obj,-60,0,z);
	while( !Obj_IsDeleted(obj) && bSpell == false){
	c+=GetCommonData("Scroll",1)*2;
	
	ObjSprite3D_SetSourceRect(obj,0,c,512*3,512+c);
	value++;
	count++;
	yield;
	}
	descent(i in 0..30){
	ObjRender_SetAlpha(obj,i);
	yield;
	}
	Obj_Delete(obj);
}	

task TNormalBackground2(){ // sky
	let c = 0;
	let s = 0;
	let value = 255;
	let count = 0;
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,21);
	ObjPrim_SetTexture(obj,nbg2);
	ObjSprite2D_SetSourceRect(obj,0,0,221,222);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,0.35,0.35,1);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	ObjRender_SetAngleXYZ(obj,0,0,0);
	ObjRender_SetColor(obj,555,555,555);
	ObjRender_SetPosition(obj,GetStgFrameWidth/2-90,GetStgFrameHeight-365,0);
	while( !Obj_IsDeleted(obj) && bSpell == false){
	ObjSprite2D_SetSourceRect(obj,0,0,221,222);
	ObjRender_SetAngleXYZ(obj,0,0,0);
	value++;
	count++;
	yield;
	}
	ascent(i in 0..10){
	ObjRender_SetAlpha(obj,i);
	yield;
	}
	Obj_Delete(obj);
}	
	
task CreateTree(x,y,startz){
	let bg = ObjPrim_Create(OBJ_SPRITE_3D);
	let zslide = 0;
	let size = 0.5;
	let xspeed = rand(-4,4);
	let timer = 0;
	let a = 0;
	let s =0;
	ObjRender_SetBlendType(bg,BLEND_ALPHA);
	Obj_SetRenderPriority(bg,0.21);
	ObjRender_SetZWrite(bg,true);
	ObjRender_SetZTest(bg,true);	
	ObjPrim_SetTexture(bg,nbg3);
	ObjRender_SetAngleXYZ(bg,0,0,0);
	ObjSprite3D_SetSourceRect(bg,0,0,512,512);
	ObjSprite3D_SetSourceDestRect(bg,0,-512,512,0);
	ObjRender_SetScaleXYZ(bg,size,-size,1);
	ObjSprite3D_SetBillboard(bg, true );
	while(bSpell == false){
	if(a<255){s+=1;}
	ObjRender_SetColor(bg,s,s,s);
	Obj_SetVisible(bg,!bSpell);
	ObjRender_SetPosition(bg,x+xspeed,y,startz-zslide);
	if(ObjRender_GetZ(bg)<-1200){
	break;
	}
	//ObjRender_SetAlpha(bg,255*sin(timer*180/420));
	zslide+=GetCommonData("Scroll",1)*4;
	timer++;
	yield;
	}
	Obj_Delete(bg);
}

task TSpellBackground(){
	let c = 0;
	let s = 0;
	let value = 255;
	let count = 0;
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,21);
	ObjPrim_SetTexture(obj,sbg1);
	ObjSprite2D_SetSourceRect(obj,0,0,397,750);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,1.15,1.15,1.15);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	ObjRender_SetAngleXYZ(obj,0,0,0);
	ObjRender_SetColor(obj,155,200,155);
	while( !Obj_IsDeleted(obj) && bSpell == true){
	c+=GetCommonData("Scroll",1)*-0.5;
	ObjRender_SetPosition(obj,GetStgFrameWidth/2,GetStgFrameWidth,0);
	ObjSprite2D_SetSourceRect(obj,0,c,397,750+c);
	ObjRender_SetAngleXYZ(obj,0,0,0);
	ObjRender_SetAlpha(obj,174);
	value++;
	count++;
	yield;
	}
	descent(i in 0..30){
	ObjRender_SetAlpha(obj,i);
	yield;
	}
	Obj_Delete(obj);
}	

task TSpellBackground2(){
	let c = 0;
	let s = 0;
	let t =0;
	let value = 255;
	let count = 0;
	let r = 255;
    	let g = 255;
    	let b = 255;
	let cl = 0;
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,21);
	ObjPrim_SetTexture(obj,sbg2);
	ObjSprite2D_SetSourceRect(obj,0,0,397,750);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,1,1,1);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	ObjRender_SetAngleXYZ(obj,0,0,0);
	while( !Obj_IsDeleted(obj) && bSpell == true){
	c+=GetCommonData("Scroll",1)*-1.5;
	ObjRender_SetPosition(obj,GetStgFrameWidth/2,GetStgFrameHeight/2,0);
	ObjSprite2D_SetSourceRect(obj,0,c,397,750+c);
	ObjRender_SetAngleXYZ(obj,0,0,0);
	r = 150+105*cos(cl)*2;
	g = 150+105*cos(cl)*2;
	b = 150+105*cos(cl)*2;
	ObjRender_SetColor(obj, 55,55,55);
	ObjRender_SetAlpha(obj,100);
    	cl+=2;
	value++;
	count++;
	t++;
	yield;
	}
	descent(i in 0..30){
	ObjRender_SetAlpha(obj,i);
	yield;
	}
	Obj_Delete(obj);
}	

task TSpellBackground3(){
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,21);
	ObjPrim_SetTexture(obj,sbg3);
	ObjSprite2D_SetSourceRect(obj,0,0,250,546);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,0.85,0.85,0);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetAngleXYZ(obj,0,0,0);
	ObjRender_SetPosition(obj,GetStgFrameWidth/2,GetStgFrameHeight/2,0);
	ObjRender_SetAlpha(obj,225);
	while( !Obj_IsDeleted(obj) && bSpell == true){
	yield;
	}
	descent(i in 0..30){
	ObjRender_SetAlpha(obj,i);
	yield;
	}
	Obj_Delete(obj);
}

task TSpellBackground4(){
	let c = 0;
	let s = 0;
	let value = 255;
	let count = 0;
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,21);
	ObjPrim_SetTexture(obj,sbg4);
	ObjSprite2D_SetSourceRect(obj,0,0,465,542);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,0.85,0.85,1);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetAngleXYZ(obj,0,0,0);
	while( !Obj_IsDeleted(obj) && bSpell == true){
	c+=GetCommonData("Scroll",1)*-0;
	ObjRender_SetPosition(obj,GetStgFrameWidth/2,GetStgFrameHeight/2,0);
	ObjSprite2D_SetSourceRect(obj,0,0,465,542);
	ObjRender_SetAngleXYZ(obj,0,0,0);
	ObjRender_SetColor(obj,155,155,155);
	ObjRender_SetAlpha(obj,172);
	value++;
	count++;
	yield;
	}
	descent(i in 0..30){
	ObjRender_SetAlpha(obj,i);
	yield;
	}
	Obj_Delete(obj);
}	

task TreeGenerator{
loop{
ascent(i in 0..5){
CreateTree(-250,120,-800+1800*i/5);
CreateTree(150,120,-800+1800*i/5);
CreateTree(-420,120,-800+1800*i/5);
CreateTree(300,120,-800+1800*i/5);
CreateTree(-560,120,-800+1800*i/5);
CreateTree(450,120,-800+1800*i/5);
}
while(!bSpell){
CreateTree(-250,120,1000);
CreateTree(150,120,1000);
CreateTree(-420,120,1000);
CreateTree(300,120,1000);
CreateTree(-560,120,1000);
CreateTree(450,120,1000);
loop(60){yield;}
}
while(bSpell){
yield;
}
}
}