let GCSD=GetCurrentScriptDirectory();
let bSpell = false;
let current = GetCurrentScriptDirectory();
let fo = 0;
let nbg1 = GCSD~"img/7_0.png";
let nbg2 = GCSD~"img/stage02b.png";
let nbg3 = GCSD~"img/BG_B1_c.png";
let nbg4 = GCSD~"img/stage03a.png";
let nbg5 = GCSD~"img/BG_B1_b.png";
let sbg1 = GCSD~"img/5.png";
let sbg2 = GCSD~"img/06_01.png";
let sbg3 = GCSD~"img/07_02.png";
let sbg4 = GCSD~"img/06_00.png";
LoadTexture(nbg1);
LoadTexture(nbg2);
LoadTexture(nbg3);
LoadTexture(nbg4);
LoadTexture(nbg5);
LoadTexture(sbg1);
LoadTexture(sbg2);
LoadTexture(sbg3);
LoadTexture(sbg4);
@Initialize
{

	SetCameraFocusX(-0);
	SetCameraFocusY(50);
	SetCameraFocusZ(-512);
	SetCameraRadius(900);
	SetCameraAzimuthAngle(-90);
	SetCameraElevationAngle(-5);
	TCallBg;
	ascent(i in 0..4){
	TSakura;
	}
	//TreeGenerator;
}

@MainLoop
{

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


task TCallBg{
	yield;
	SetFogParam(600,2200, 10, 0, 10);
	VeryBackground();
	Bgmakaicir();
	TNormalBackgroundSky(0);
	ascent(i in 0..6){
	
	TNormalBackground(-i*512);
	TNormalBackgroundP(-i*512);
	TNormalBackgroundS(-i*512);
	}
	while(1){
	while(bSpell==false){ yield; }
	if(bSpell==true){
	    TSpellBackground4();
		TSpellBackground2;
		TSpellBackground();
		TSpellBackground3();
		TSpellBackground0();
		}
	while(bSpell==true){ yield; }
	yield;
	}
}

task TSakura{
	while( bSpell == false){
	if(GetCommonData("Effect",2)){TMaple(rand(0,GetStgFrameWidth),GetStgFrameHeight);}
	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(5,15);
		let ang = [0,0,90];
		let ap = [rand(-1,1),rand(0,0),rand(1,3)];
		let size = cy/18;
		let t = 0;
		let s = rand(-0.5,0.5);
		let color = [172,172,172];
		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,5,64);
		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.0035;
				if(bSpell == true){break;}
				yield;
				}
				descent(i in 0..180){
				ObjRender_SetAlpha(obj,i*2);
				ObjRender_SetScaleXYZ(obj,size,size,0);
				size +=0.0035;
				if(bSpell == true){break;}
				yield;
				}
				Obj_Delete(obj);
			}
			while( bSpell == false){
			ObjRender_SetPosition(obj,px,py,0);
			ObjRender_SetAngleXYZ(obj,0,0,0);
			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 Bgmakaicir(){ // circle
	let c = 0;
	let s = 0;
	let value = 255;
	let count = 0;
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,19.5);
	let wht = GCSD~"img/bgcircle.png";
	ObjPrim_SetTexture(obj,wht);
	ObjSprite2D_SetSourceRect(obj,0,0,1024,1024);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	ObjRender_SetPosition(obj,GetStgFrameWidth/2,GetStgFrameHeight/2-20,0);
	while( !Obj_IsDeleted(obj) ){
	c+=GetCommonData("Scroll",1)*0;
	ObjRender_SetAngleXYZ(obj,0,0,count);
	ObjRender_SetScaleXYZ(obj,0.1+0.15+0.05*sin(count*0.5),0.1+0.15+0.05*sin(count*0.5),0);
	ObjRender_SetColor(obj,255, 255, 255);
	ObjSprite2D_SetSourceRect(obj,0,0,1024,1024);
	ObjRender_SetAlpha(obj,100+55*sin(count*2));
	value++;
	count++;
	yield;
	}
	descent(i in 0..30){
	ObjRender_SetAlpha(obj,i);
	yield;
	}
	Obj_Delete(obj);
}	

task VeryBackground(){ // White
	let c = 0;
	let s = 0;
	let value = 255;
	let count = 0;
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,19);
	let wht = GCSD~"img/Handmade_White-Black.png";
	ObjPrim_SetTexture(obj,wht);
	ObjSprite2D_SetSourceRect(obj,0,0,2024,2024);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetAngleXYZ(obj,0,0,0);
	ObjRender_SetPosition(obj,0,0,1200);
	while( !Obj_IsDeleted(obj) ){
	c+=GetCommonData("Scroll",1)*0;
	ObjRender_SetColor(obj,10, 0, 10);
	ObjSprite2D_SetSourceRect(obj,0,0,2024,2024);
	value++;
	count++;
	yield;
	}
	descent(i in 0..30){
	ObjRender_SetAlpha(obj,i);
	yield;
	}
	Obj_Delete(obj);
}	


task ObjectRotation2( obj, target, rstart, rmax, angle, ad, av, aad, aav, avmax ){

   let ox;
   let oy;
   let r = rstart;
   while(!Obj_IsDeleted(obj)){

      ox = ObjRender_GetX( target ) + r*cos( angle );
      oy = ObjRender_GetY( target ) + r*sin( angle );

      ObjRender_SetPosition(obj,ox,oy,0);

      if(r < rmax){
      r += ad;
      ad += aad; }

      if(r > rmax){
      r -= ad;
      ad += aad; }

      if( av < avmax ){
      angle -= av;
      av += aav; }

      if( av > avmax ){
      angle -= avmax; }

      if( av == avmax ){
      angle += av; }

      yield;
   }
}

task TNormalBackgroundSky(z){ // Sky
	let c = 0;
	let s = 0;
	let value = 255;
	let count = 0;
	let obj=ObjPrim_Create(OBJ_SPRITE_3D);
	Obj_SetRenderPriorityI(obj,19);
	ObjPrim_SetTexture(obj,nbg3);
	ObjSprite3D_SetSourceRect(obj,0,0,2000,2000);
	ObjSprite3D_SetSourceDestRect(obj,0,0,3000,2000);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetAngleXYZ(obj,-70,0,0);
	ObjRender_SetPosition(obj,0,380,-00);
	loop{
	c+=GetCommonData("Scroll",1)*-4;
	
	ObjSprite3D_SetSourceRect(obj,0,c,2000,2000+c);
	value++;
	count++;
	yield;
	}
	descent(i in 0..30){
	ObjRender_SetAlpha(obj,i);
	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,20);
	ObjPrim_SetTexture(obj,nbg2);
	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,0,0,z);
	loop{
	c+=GetCommonData("Scroll",1)*10;
	
	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 TNormalBackgroundS(z){ // grid
	let c = 0;
	let s = 0;
	let value = 255;
	let count = 0;
	let obj=ObjPrim_Create(OBJ_SPRITE_3D);
	Obj_SetRenderPriorityI(obj,20);
	ObjPrim_SetTexture(obj,nbg4);
	ObjSprite3D_SetSourceRect(obj,0,0,512,512);
	ObjSprite3D_SetSourceDestRect(obj,0,0,256,1024);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetAngleXYZ(obj,90,0,0);
	ObjRender_SetPosition(obj,0,0,z);
	loop{
	c+=GetCommonData("Scroll",1)*10;
	
	ObjSprite3D_SetSourceRect(obj,0,c,256,512+c);
	value++;
	count++;
	yield;
	}
	descent(i in 0..30){
	ObjRender_SetAlpha(obj,i);
	yield;
	}
	Obj_Delete(obj);
}	

task TNormalBackgroundP(z){ // P-cloud
	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,nbg2);
	ObjSprite3D_SetSourceRect(obj,0,0,512*3,512);
	ObjSprite3D_SetSourceDestRect(obj,0,0,1012,512);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetAngleXYZ(obj,90,0,0);
	ObjRender_SetPosition(obj,0,30,z);
	loop{
	c+=GetCommonData("Scroll",1)*5;
	ObjRender_SetColor(obj,255, 125, 255);
	ObjSprite3D_SetSourceRect(obj,0,c,512*3,512+c);
	ObjRender_SetAlpha(obj,125);
	value++;
	count++;
	yield;
	}
	descent(i in 0..30){
	ObjRender_SetAlpha(obj,i);
	yield;
	}
	Obj_Delete(obj);
}	
	
	
task CreateTemple(x,y,startz){
	let bg = ObjPrim_Create(OBJ_SPRITE_3D);
	let zslide = 0;
	let size = 1;
	let xspeed = rand(-4,4);
	let timer = 0;
	let a = 0;
	let s =0;
	ObjRender_SetBlendType(bg,BLEND_ALPHA);
	Obj_SetRenderPriority(bg,0.20);
	ObjRender_SetZWrite(bg,true);
	ObjRender_SetZTest(bg,true);	
	ObjPrim_SetTexture(bg,nbg3);
	ObjRender_SetAngleXYZ(bg,0,0,0);
	ObjSprite3D_SetSourceRect(bg,0,0,256,256);
	ObjSprite3D_SetSourceDestRect(bg,0,-256,256,0);
	ObjRender_SetScaleXYZ(bg,size,-size,1);
	ObjSprite3D_SetBillboard(bg, true );
	while(!Obj_IsDeleted( bg )){//bSpell == false){
	if(a<255){s+=20;}
	ObjRender_SetColor(bg,s,s,s);
	Obj_SetVisible(bg,true);
	ObjRender_SetPosition(bg,x+xspeed,y,startz-zslide);
	if(ObjRender_GetZ(bg)<-1800){
	break;
	}
	//ObjRender_SetAlpha(bg,255*sin(timer*180/420));
	zslide+=GetCommonData("Scroll",1)*7;
	timer++;
	yield;
	}
	Obj_Delete(bg);
}

task TSpellBackground0(){
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,26);
	ObjPrim_SetTexture(obj,sbg1);
	ObjSprite2D_SetSourceRect(obj,0,0,256,256);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,3,2,0);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetPosition(obj,640/2,450/2,0);
	ObjRender_SetAlpha( obj, 255 );
	ascent( i in 0..255/3.5 ){
	ObjRender_SetAlpha( obj, i*3.5 );
	yield;
	}
	loop( 10 ){ yield; }
	descent( i in 0..255/2.5 ){
	ObjRender_SetAlpha( obj, i*2.5 );
	yield;
	}
	Obj_Delete(obj);
}

task TSpellBackground(){
	let c = 0;
	let s = 0;
	let value = 0;
	let count = 0;
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,23);
	ObjPrim_SetTexture(obj,sbg1);
	ObjSprite2D_SetSourceRect(obj,0,0,256,256);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,2.5,2,0);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetAngleXYZ(obj,0,0,0);
	ObjRender_SetColor(obj,255,255,255);
	ObjRender_SetPosition(obj,640/2,450/2,0);
	ObjRender_SetAlpha( obj, 255/1.5 );
	while( !Obj_IsDeleted(obj) && bSpell == true){
	ObjRender_SetColor(obj,value,value,value);
	if(value<255){value+=5;}
	count++;
	yield;
	}
	descent(i in 0..30){
	ObjRender_SetAlpha(obj,i);
	yield;
	}
	Obj_Delete(obj);
}	

task TSpellBackground3(){
	let c = 0;
	let s = 0;
	let value = 0;
	let count = 0;
	let t = 0;
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,21);
	ObjPrim_SetTexture(obj,sbg3);
	ObjSprite2D_SetSourceRect(obj,0,0,500,500);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetPosition(obj,640/2,450/2,0);
	while( !Obj_IsDeleted(obj) && bSpell == true){
	ObjRender_SetColor(obj,205,205,205);
	if(value<255){value+=5;}
	ObjRender_SetScaleXYZ(obj,1.5,1.5,0);
	ObjRender_SetAngleXYZ(obj,0,0,-count/10);
	count += 0.25;
	t++;
	yield;
	}
	descent(i in 0..30){
	ObjRender_SetAlpha(obj,i);
	yield;
	}
	Obj_Delete(obj);
}

task TSpellBackground2{
	let c = 0;
	let s = 0;
	let value = 0;
	let count = 0;
	let t = 0;
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,24);
	ObjPrim_SetTexture(obj,sbg2);
	ObjSprite2D_SetSourceRect(obj,0,0,1024,1024);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetColor(obj,235,235,235);
	let i = 0;
	let r = 80;
	let angle = 270;
	Seek( obj );
	while( !Obj_IsDeleted(obj) && bSpell == true){
	ObjRender_SetAngleXYZ(obj,0,0,i);
	ObjRender_SetPosition(obj,(640/2)+r*cos( angle ),(450/2) + r*sin( angle ),0);
	i -= 0.1;
	angle +=0.3;
	yield;
	}
	descent(i in 0..30){
	ObjRender_SetAlpha(obj,i);
	yield;
	}
	Obj_Delete(obj);
}

	task Seek( obj ){
	ObjRender_SetAlpha( obj, 0 );
	loop( 75 ){ yield; }
	ObjRender_SetAlpha( obj, 255 );
	}
	
task BGMasquerade11( obj ){
while( !Obj_IsDeleted( obj )){
	ascent( i in 0..1*450 ){
	ObjRender_SetScaleXYZ( obj, 0.5 + i/450, 0.5 + i/450, 0 );
	yield;
	}
	descent( i in 0..1*450 ){
	ObjRender_SetScaleXYZ( obj, 0.5 + i/450, 0.5 + i/450, 0 );
	yield;
	}
}
}

task BGMasquerade12( obj ){
let i = 0;
while( !Obj_IsDeleted( obj )){
	ObjRender_SetAngleXYZ(obj,0,0,i);
	i += 0.2;
	yield;
	}
}

task BGMasquerade13( obj ){
while( !Obj_IsDeleted( obj )){
	descent( i in 0..1*900 ){
	ObjRender_SetAlpha( obj,  i /900 * 255 );
	yield;
	}
	ascent( i in 0..1*900 ){
	ObjRender_SetAlpha( obj,  i /900 * 255 );
	yield;
	}
}
}

task TSpellBackground21(){
	let c = 0;
	let s = 0;
	let value = 0;
	let count = 0;
	let t = 0;
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,22);
	ObjPrim_SetTexture(obj,sbg2);
	ObjSprite2D_SetSourceRect(obj,0,0,500,500);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetPosition(obj,640/2,450/2,0);
	BGMasquerade21( obj );
	BGMasquerade22( obj );
	BGMasquerade23( obj );
	while( !Obj_IsDeleted(obj) && bSpell == true){
	yield;
	}
	descent(i in 0..30){
	ObjRender_SetAlpha(obj,i);
	yield;
	}
	Obj_Delete(obj);
}
	
task BGMasquerade21( obj ){
while( !Obj_IsDeleted( obj )){
	descent( i in 0..1*450 ){
	ObjRender_SetScaleXYZ( obj, 0.5 + i/450, 0.5 + i/450, 0 );
	yield;
	}
	ascent( i in 0..1*450 ){
	ObjRender_SetScaleXYZ( obj, 0.5 + i/450, 0.5 + i/450, 0 );
	yield;
	}
}
}

task BGMasquerade22( obj ){
let i = 0;
while( !Obj_IsDeleted( obj )){
	ObjRender_SetAngleXYZ(obj,0,0,i);
	i -= 0.2;
	yield;
	}
}

task BGMasquerade23( obj ){
while( !Obj_IsDeleted( obj )){
	ascent( i in 0..1*900 ){
	ObjRender_SetAlpha( obj,  i /900 * 255 );
	yield;
	}
	descent( i in 0..1*900 ){
	ObjRender_SetAlpha( obj,  i /900 * 255 );
	yield;
	}
}
}

task TSpellBackground22(){
	let c = 0;
	let s = 0;
	let value = 0;
	let count = 0;
	let t = 0;
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,21);
	ObjPrim_SetTexture(obj,sbg2);
	ObjSprite2D_SetSourceRect(obj,0,0,500,500);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	ObjRender_SetPosition(obj,640/2,450/2,0);
	while( !Obj_IsDeleted(obj) && bSpell == true){
	ObjRender_SetColor(obj,value,value,value);
	if(value<255){value+=5;}
	ObjRender_SetScaleXYZ(obj,-0.5+0.5*sin(t/3),-0.5+0.5*sin(t/3),1.5);
	ObjRender_SetAngleXYZ(obj,0,0,-count/3);
	count++;
	t++;
	yield;
	}
	descent(i in 0..30){
	ObjRender_SetAlpha(obj,i);
	yield;
	}
	Obj_Delete(obj);
}

task TSpellBackground4(){
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,25);
	ObjPrim_SetTexture(obj,sbg4);
	ObjSprite2D_SetSourceRect(obj,0,0,620,619);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,0.85,0.85,0);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetPosition(obj,640/2 + 20,450/2,0);
	Seek( obj );
	while( !Obj_IsDeleted(obj) && bSpell == true){
	yield;
	}
	Obj_Delete(obj);
}

task TreeGenerator{
loop{
ascent(i in 0..3){
CreateTemple(0,90,-800+1800*i/5);
}
while(!bSpell||bSpell){
CreateTemple(0,90,220);
loop(60){yield;}
}
//while(bSpell||){
//yield;
//}
}
}