let GCSD=GetCurrentScriptDirectory();
let bSpell = false;
let current = GetCurrentScriptDirectory();
let fo = 0;
let nbg1 = GCSD~"img/Handmade_White-Black.png";
let nbg2 = GCSD~"img/02_01_3d_02.png";
let nbg3 = GCSD~"img/04_00_3d_01.png";
let nbg4 = GCSD~"img/BG_B1_c.png";
let nbg5 = GCSD~"img/BG_B1_b.png";
let sbg0 = GCSD~"img/5.png";
let sbg1 = GCSD~"img/07_00.png";
let sbg2 = GCSD~"img/07_01.png";
let sbg3 = GCSD~"img/07_02.png";
let sbg4 = GCSD~"img/5.png";
LoadTexture(nbg1);
LoadTexture(nbg2);
LoadTexture(nbg3);
LoadTexture(nbg4);
LoadTexture(nbg5);
LoadTexture(sbg0);
LoadTexture(sbg1);
LoadTexture(sbg2);
LoadTexture(sbg3);
LoadTexture(sbg4);
@Initialize
{
	SetCameraFocusX(-000);
	SetCameraFocusY(300);
	SetCameraFocusZ(100);
	SetCameraRadius(1200);
	SetCameraAzimuthAngle(-90);
	SetCameraElevationAngle(-5);
	TCallBg;
	//TreeGenerator;
	TSakura;
}

@MainLoop
{

	let objScene = GetEnemyBossSceneObjectID();
	if(ObjEnemyBossScene_GetInfo(objScene, INFO_IS_SPELL))
	{
		bSpell = true;
	}
	else
	{
		bSpell = false;
	}
	fo++;
	SetCameraElevationAngle(50+15*sin(fo*0.5));
    SetCameraAzimuthAngle(80+15*sin(fo*0.3));
	yield;
}

task TCallBg{
	yield;
	SetFogParam(1200,2000, 255, 200, 150);
	TNormalBackground(0,15);
	VeryBackground();
	ascent(let pos in -10..10)
	{
		TNormalBackground3D(pos,0,1,7);
		TNormalBackground3D2(pos,0,1.5,7);
	}
	while(1){
		
	while(bSpell==false){ yield; }
	if(bSpell==true){
		TSpellBackground0;
		loop( 75 ){ yield; }
		TSpellBackground4();
		TSpellBackground2();
		TSpellBackground21();
		TSpellBackground();
		TSpellBackground3();
		}
	while(bSpell==true){ yield; }
	yield;
	}
}

task TSakura{
	while( bSpell == false){
	if(GetCommonData("Effect",2)){TMaple(rand(0,GetStgFrameWidth),0);}
	loop((3-GetCommonData("Effect",2))*9){
	yield;
	}
	}
		task TMaple(px,py){
		let obj=ObjPrim_Create(OBJ_SPRITE_2D);
		let maple = GetCurrentScriptDirectory ~"./img/mapla.png";
		let cy = rand(0.5,4);
		let ang = [0,0,90];
		let ap = [rand(-1,1),rand(0,0),rand(1,3)];
		let size = cy/5;
		let t = 0;
		let s = rand(-0.5,0.5);
		let color = [255,198,152];
		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,41,49);
		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*1);
				ObjRender_SetScaleXYZ(obj,size,size,0);
				size +=0.0035;
				if(bSpell == true){break;}
				yield;
				}
				descent(i in 0..180){
				ObjRender_SetAlpha(obj,i*1);
				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,ang[0],ang[1],ang[2]);
			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 VeryBackground(){ // White
	let c = 0;
	let s = 0;
	let value = 255;
	let count = 0;
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,20);
	ObjPrim_SetTexture(obj,nbg1);
	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,-10000);
	while( !Obj_IsDeleted(obj)){
	c+=GetCommonData("Scroll",1)*0;
	ObjRender_SetColor(obj,255, 200, 150);
	ObjSprite2D_SetSourceRect(obj,0,0,2024,2024);
	//ObjRender_SetAlpha(obj,120);
	value++;
	count++;
	yield;
	}
	descent(i in 0..30){
	ObjRender_SetAlpha(obj,i);
	yield;
	}
	Obj_Delete(obj);
}	

task TNormalBackground3D(pos,X,sc,z)
{
	let pz = (pos) * 250;
	let path = current ~ "./chacha/01_00.mqo";
	let obj = ObjMesh_Create();
	ObjMesh_Load(obj, path);
	Obj_SetRenderPriorityI(obj, 20);

	let frameInvisible = 0;
	ObjRender_SetScaleXYZ(obj,sc,sc,-sc);
	loop
	{


		ObjRender_SetZ(obj, pz);
		ObjRender_SetX(obj, 0);
		ObjRender_SetY(obj, -0);
		pz+=z;
		if(pz > 1000){pz -= 5000;}

		yield;
	}
}

task TNormalBackground3D2(pos,X,sc,z)
{
	let pz = (pos) * 8000;
	let path = current ~ "./chacha/02_01.mqo";
	let obj = ObjMesh_Create();
	ObjMesh_Load(obj, path);
	Obj_SetRenderPriorityI(obj, 20);

	let frameInvisible = 0;
	ObjRender_SetScaleXYZ(obj,sc,sc,-sc);
	loop
	{


		ObjRender_SetZ(obj, pz);
		ObjRender_SetX(obj, 0);
		ObjRender_SetY(obj, 30);
		pz+=z;
		if(pz > 1500){pz -= 3500;}

		yield;
	}
}

task TNormalBackground(z,spd){ // floor
	let c = 0;
	let s = 0;
	let value = 255;
	let count = 0;
	let obj=ObjPrim_Create(OBJ_SPRITE_3D);
	Obj_SetRenderPriorityI(obj,21);
	let img = current ~ "./chacha/02_01_3d_02.png";
	ObjPrim_SetTexture(obj,img);
	ObjSprite3D_SetSourceRect(obj,0,0,512,256);
	ObjSprite3D_SetSourceDestRect(obj,0,0,350,2024);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetAngleXYZ(obj,90,0,0);
	ObjRender_SetPosition(obj,-0,40,z);
	while( !Obj_IsDeleted(obj) ){
	c+=GetCommonData("Scroll",1)*-12;
	ObjRender_SetAlpha(obj,255);
	ObjSprite3D_SetSourceRect(obj,0,c,350,2024+c);
	ObjRender_SetColor(obj,155,200,255);
	value++;
	count++;
	yield;
	}
	descent(i in 0..30){
	ObjRender_SetAlpha(obj,i);
	yield;
	}
	Obj_Delete(obj);
}

task TSpellBackground0(){
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,26);
	ObjPrim_SetTexture(obj,sbg0);
	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,500,582);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,0.75,0.75,0);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetAngleXYZ(obj,0,0,0);
	ObjRender_SetColor(obj,255,255,255);
	ObjRender_SetPosition(obj,384/2,448/2,0);
	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,384/2,448/2,0);
	while( !Obj_IsDeleted(obj) && bSpell == true){
	ObjRender_SetColor(obj,value,value,value);
	if(value<255){value+=5;}
	ObjRender_SetScaleXYZ(obj,1.2,1.2,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,22);
	ObjPrim_SetTexture(obj,sbg2);
	ObjSprite2D_SetSourceRect(obj,0,0,500,500);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetPosition(obj,384/2,448/2,0);
	BGMasquerade11( obj );
	BGMasquerade12( obj );
	BGMasquerade13( obj );
	while( !Obj_IsDeleted(obj) && bSpell == true){
	yield;
	}
	descent(i in 0..30){
	ObjRender_SetAlpha(obj,i);
	yield;
	}
	Obj_Delete(obj);
}
	
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,384/2,448/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,384/2,448/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,21);
	ObjPrim_SetTexture(obj,sbg4);
	ObjSprite2D_SetSourceRect(obj,0,0,638,851);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,1,1,1);
	ObjRender_SetBlendType(obj,BLEND_ALPHA);
	ObjRender_SetAlpha( obj, 255/2 );
	ObjRender_SetColor(obj,255,255,255);
	ObjRender_SetAngleXYZ(obj,0,0,0);
	ObjRender_SetPosition(obj,384/2,448/2,0);
	while( bSpell == true){
	yield;
	}
	Obj_Delete(obj);
}



