
let bSpell = false;
let current = GetCurrentScriptDirectory();
let t=0; let alpha=0;
@Initialize
{

	SetCameraRadius(512);
	
	SetCameraElevationAngle(-5);
	SetCameraAzimuthAngle(90);
	//SetCameraElevationAngle(20);

	SetFogParam(0, 2500,31, 0, 51);

	//TNormalBackground();
	//Floor();
	//Smoke();
	Trees();
	StageBG1();
	MoonAura;
	TSpellBackground();
	TSpellBackground2();

}

@MainLoop
{

	let objScene = GetEnemyBossSceneObjectID();
	if(objScene != ID_INVALID && ObjEnemyBossScene_GetInfo(objScene, INFO_IS_SPELL))
	{
		bSpell = true;
	}
	else
	{
		bSpell = false;
	}
	

	yield;
}

task TCallBG{
	
	Trees();
	while(GetCommonData("UserBG",0)==1){yield;}
	
	////Placeholer here
	while(GetCommonData("UserBG",0)==2){yield;}
	
	////Placeholer here
	while(GetCommonData("UserBG",0)==3){yield;}
	
////Placeholer here
	while(GetCommonData("UserBG",0)==4){yield;}
	
}


task Floor
{
let param=0;

Background1; 

	task Background1{
	let bg=ObjPrim_Create(OBJ_SPRITE_3D);
	let t=0;
	ObjPrim_SetTexture(bg, GetCurrentScriptDirectory~"/img/stg3bg1.png");
	ObjSprite3D_SetSourceDestRect(bg, 0, 0, 512, 512);
	Obj_SetRenderPriorityI(bg, 1);
	
	let alpha=255;
	ObjRender_SetScaleXYZ(bg, 1,1,1);
	ObjRender_SetPosition(bg, GetScreenWidth/2, 90,0);
	//ObjRender_SetAngleY(bg, 270 );
	ObjRender_SetAngleX(bg, 95 );
	
	while(!Obj_IsDeleted(bg)){
if (bSpell==false&&alpha<255){alpha+=15;}
if (bSpell==true&&alpha>0){alpha-=5;}
ObjRender_SetAlpha(bg, alpha);
	t+=-1.75; 
	ObjSprite3D_SetSourceDestRect(bg, 0, t, 4096, 4096+t);
	yield;}
	}
	
}

task Smoke()
{
let param=0;

Background1; 

	task Background1{
	let bg=ObjPrim_Create(OBJ_SPRITE_3D);
	let t=0;
	ObjPrim_SetTexture(bg, GetCurrentScriptDirectory~"/img/fog.png");
	ObjSprite3D_SetSourceDestRect(bg, 0, 0, 256, 256);
	Obj_SetRenderPriorityI(bg, 1);
	ObjRender_SetBlendType(bg, BLEND_ALPHA);
	let alpha=255;
	ObjRender_SetScaleXYZ(bg, 3,3,3);
	ObjRender_SetPosition(bg, GetScreenWidth/2+rand(-60,60), -190,380);
	//ObjRender_SetAngleY(bg, 270 );
	ObjRender_SetAngleX(bg, 60 );
	
	while(!Obj_IsDeleted(bg)){
if (bSpell==false&&alpha<255){alpha+=15;}
if (bSpell==true&&alpha>0){alpha-=5;}
ObjRender_SetAlpha(bg, alpha);
	t+=-3; 
	ObjSprite3D_SetSourceDestRect(bg, 0, t, 4096, 4096+t);
	yield;}
	}
	
}



	task StageBG1(){
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		Obj_SetRenderPriority(obj, 0);
		ObjPrim_SetTexture(obj, GetCurrentScriptDirectory ~ "img/lunaticmoonbg.png");
		ObjSprite2D_SetSourceRect(obj, 0, 0, 512, 512);
		ObjRender_SetBlendType(obj, BLEND_ALPHA);
		ObjSprite2D_SetDestCenter(obj);
		ObjRender_SetPosition(obj, GetStgFrameWidth/2+30, GetStgFrameHeight/2, 0);
		while(!Obj_IsDeleted(obj)){
			
if (bSpell==false&&alpha<255){alpha+=15;}
if (bSpell==true&&alpha>0){alpha-=5;}
		yield;}
		Obj_Delete(obj);
	}

task Trees(){
		let pol = 1.1;
		let Y=0;
		while(GetPlayerLife>=0){
			SpawnTree(Y,pol);
			pol = -pol;
			Y*=-1;
			loop(30){yield;}
		}
		
		task SpawnTree(Y,p){
			let alpha=10;
			let obj = ObjPrim_Create(OBJ_SPRITE_3D);
			Obj_SetRenderPriority(obj,0.2);
			ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~ "img/bamboo.png");
			ObjRender_SetBlendType(obj, BLEND_ALPHA);
			ObjSprite3D_SetSourceDestRect(obj,0,0,512,512);
			ObjRender_SetScaleXYZ(obj, 2.75, 2.75, 2.75);
			ObjRender_SetAngleX(obj, 180);
			ObjRender_SetAngleY(obj, Y );
			ObjRender_SetY(obj, -400);

			
			let offset = rand(-60, 60);


ascent(i in 0 .. 200){
			
if (bSpell==false&&alpha<255){alpha+=3;}
if (bSpell==true&&alpha>0){alpha-=5;}
ObjRender_SetX(obj, (i * 2 + 480) * p + offset);
ObjRender_SetZ(obj, i * 8 - 1200);
ObjRender_SetAlpha(obj, alpha);
alpha+=2;
yield;
			}

			Obj_Delete(obj);
			
		}
	}

task MoonAura{
	
	let v = 384;
	let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	let alpha=255;
	ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"/img/stg6bg8.png");
	ObjPrim_SetVertexCount(obj, (v+1)*2);
	ObjRender_SetPosition(obj, GetStgFrameWidth/2-60, GetStgFrameHeight/2-180, 0);
	Obj_SetRenderPriorityI(obj, 20);
	ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
	ObjSprite2D_SetDestCenter(obj);
	let x = 0;

		while(!Obj_IsDeleted(obj)){

loop{
		ascent(i in 0..v+1){
			ObjPrim_SetVertexUVT(obj, i*2, 64+x, 0+(512/v)*i);
			ObjPrim_SetVertexUVT(obj, i*2+1, 448+x, 0+(512/v)*i);
		}

		ascent(i in 0..v+1){
			ObjPrim_SetVertexPosition(obj, i*2, 490*cos((360/v)*i-90), 490*sin((360/v)*i-90), 0	);
			ObjPrim_SetVertexPosition(obj, i*2+1, 0, 0, 0);
		}

		x+=2;
		yield;
	
if (bSpell==false&&alpha<255){alpha+=35;}
if (bSpell==true&&alpha>0){alpha-=5;}
ObjRender_SetAlpha(obj, alpha-180);

yield;}
		}


}

	
task TSpellBackground
{
let obj=ObjPrim_Create(OBJ_SPRITE_2D);


ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"/img/cdbg06b.png");


ObjRender_SetPosition(obj, GetScreenWidth/2, GetScreenHeight/2, 0);


ObjSprite2D_SetSourceRect(obj, 0, 0, 512, 512);


ObjRender_SetScaleXYZ(obj, 1.5, 1.5, 1.5);

ObjSprite2D_SetDestCenter(obj);



Obj_SetRenderPriorityI(obj, 20);

let t=0;
let angle=0;


while(!Obj_IsDeleted(obj)){

t+=1;
ObjSprite2D_SetSourceRect(obj, 0, t, 1280, 1280+t);
if (bSpell==true&&alpha<200){alpha+=5;}
if (bSpell==false&&alpha>0){alpha-=15;}
ObjRender_SetAlpha(obj, alpha);

yield;}
}


task TSpellBackground2{
	
	let v = 384;
	let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	
	ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"/img/spellbg2.png");
	ObjPrim_SetVertexCount(obj, (v+1)*2);
	ObjRender_SetPosition(obj, GetStgFrameWidth/2, GetStgFrameHeight/2-90, 0);
	Obj_SetRenderPriorityI(obj, 20);
	ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
	ObjSprite2D_SetDestCenter(obj);
	let x = 0;

		while(!Obj_IsDeleted(obj)){

loop{
		ascent(i in 0..v+1){
			ObjPrim_SetVertexUVT(obj, i*2, 64+x, 0+(512/v)*i);
			ObjPrim_SetVertexUVT(obj, i*2+1, 448+x, 0+(512/v)*i);
		}

		ascent(i in 0..v+1){
			ObjPrim_SetVertexPosition(obj, i*2, 450*cos((360/v)*i-90), 450*sin((360/v)*i-90), 0	);
			ObjPrim_SetVertexPosition(obj, i*2+1, 0, 0, 0);
		}

		x+=2;
		yield;
if (bSpell==true&&alpha<200){alpha+=5;}
if (bSpell==false&&alpha>0){alpha-=15;}
ObjRender_SetAlpha(obj, alpha-150);

yield;}
		}


}
