#include"./function.txt"
#include"./mod.txt"

let objScene = GetEnemyBossSceneObjectID;

let BossSpell = false;

let mapscale = 1;
let scrolling = 1.5;
let stagetilt = 0;

let fogalpha = 256;

let shadowalpha = 256;

@Initialize
{
	SetCameraFocusX(0);
	SetCameraFocusY(0);
	SetCameraFocusZ(0);
	SetCameraRadius(384);
	SetCameraAzimuthAngle(90);
	TCallBg;
}

@MainLoop
{
    SetCameraElevationAngle(300);
	stagetilt=0;
	let objScene = GetEnemyBossSceneObjectID();
	if(objScene != ID_INVALID && ObjEnemyBossScene_GetInfo(objScene, INFO_IS_SPELL))
	{
		SetFogEnable(false);
		BossSpell = true;
	}
	else
	{
		BossSpell = false;
	}
	yield;
}

task TCallBg
{
	Floor;
	Shadow;
	SpellBG;
	ThickFog;
}

task ThickFog
{
	let scale=1;
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(obj,0.24);
	ObjPrim_SetTexture(obj,dir ~ "BG/thickfog.png");
	ObjSprite2D_SetSourceRect(obj,0,0,512,512);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj,1,scale,0);
	ObjRender_SetAlpha(obj,fogalpha);
	while(!Obj_IsDeleted(obj))
	{
		ObjRender_SetColor(obj,128,128,128);
		ObjRender_SetPosition(obj,GetStgFrameWidth/2,GetStgFrameHeight/2-512*scale+512*scale,0);
		yield;
	}
}

task Floor
{
	let xx=512*3;
	let yy=512*3;
	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	Obj_SetRenderPriority(obj,0.21);
	ObjPrim_SetTexture(obj,dir ~ "bg/tile.png");
	ObjSprite3D_SetSourceRect(obj,0,0,xx,yy);
	ObjSprite3D_SetDestRect(obj,-xx/2,-yy/2,xx/2,yy/2);
	let sr=0;
	while(!Obj_IsDeleted(obj))
	{
		ObjRender_SetAngleZ(obj,stagetilt);
		ObjRender_SetPosition(obj,0,0,0);
		ObjSprite3D_SetSourceRect(obj,0,sr,xx,yy+sr);
		ObjRender_SetScaleXYZ(obj,mapscale,mapscale,0);
		sr+=scrolling;
		yield;
	}
	Obj_Delete(obj);
}

task Shadow
{
	let objx = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(objx,0.28);
	ObjPrim_SetTexture(objx,dir ~ "BG/spellbg.png");
	ObjSprite2D_SetSourceRect(objx,0,0,480,480);
	ObjSprite2D_SetDestCenter(objx);
	ObjRender_SetPosition(objx,GetStgFrameWidth/2,GetStgFrameHeight/2,0);
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(obj,0.29);
	ObjPrim_SetTexture(obj,dir ~ "BG/shadow.png");
	ObjSprite2D_SetSourceRect(obj,0,0,512,512);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetPosition(obj,GetStgFrameWidth/2,GetStgFrameHeight/2,0);
	let al=shadowalpha;
	while(!Obj_IsDeleted(obj))
	{
		al=shadowalpha;
		ObjRender_SetAlpha(objx,al/8);
		ObjRender_SetScaleXYZ(objx,GetStgFrameWidth/480,GetStgFrameHeight/480,1);
		ObjRender_SetAlpha(obj,al);
		ObjRender_SetScaleXYZ(obj,GetStgFrameWidth/512,GetStgFrameHeight/512,1);
		yield;
	}
	Obj_Delete(obj);
	Obj_Delete(objx);
}

task SpellBG
{
	let obj=ObjPrim_Create(OBJ_SPRITE_2D);
	let objt1=ObjPrim_Create(OBJ_SPRITE_2D);
	let objt2=ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriority(objt1,0.25);
	Obj_SetRenderPriority(objt2,0.26);
	Obj_SetRenderPriority(obj,0.27);
	ObjPrim_SetTexture(objt1,dir ~ "BG/spellbgt1.png");
	ObjPrim_SetTexture(objt2,dir ~ "BG/spellbgt2.png");
	ObjPrim_SetTexture(obj,dir ~ "BG/spellbg.png");
	ObjSprite2D_SetSourceRect(objt1,0,0,512,512);
	ObjSprite2D_SetSourceRect(objt2,0,0,1024,1024);
	ObjSprite2D_SetSourceRect(obj,0,0,480,480);
	ObjSprite2D_SetDestCenter(objt1);
	ObjSprite2D_SetDestCenter(objt2);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetPosition(objt1,GetStgFrameWidth/2,GetStgFrameHeight/2,0);
	ObjRender_SetPosition(objt2,GetStgFrameWidth/2,GetStgFrameHeight/2,0);
	ObjRender_SetPosition(obj,GetStgFrameWidth/2,GetStgFrameHeight/2,0);
	let al=0;
	let sr=0;
	let z=0;
	let c=0;
	loop
	{
		if(BossSpell==true && al<256){al+=256/60;}
		if(BossSpell==false && al>0){al-=256/60;}
		if(GetCommonData("spellcolor","grey")=="white"){ObjRender_SetColor(obj,64,64,64);}
		if(GetCommonData("spellcolor","grey")=="black"){ObjRender_SetColor(obj,192,192,192);}
		if(GetCommonData("spellcolor","grey")=="grey"){ObjRender_SetColor(obj,128,128,128);}
		if(GetCommonData("bossspell","both")=="tenkai" || GetCommonData("bossspell","both")=="both")
		{
			ObjRender_SetAlpha(objt1,al/2);
			ObjRender_SetAlpha(objt2,al/2);
		}
		ObjRender_SetAlpha(obj,al/2);
		ObjSprite2D_SetSourceRect(objt1,0,-sr,512,512-sr);
		ObjSprite2D_SetSourceRect(objt2,0,sr,1024,1024+sr);
		ObjRender_SetAngleZ(objt1,-z/2+180);
		ObjRender_SetAngleZ(objt2,z);
		z+=0.1;
		sr--;
		c++;
		yield;
		if(GetCommonData("bossspell","both")=="senri" || GetCommonData("bossspell","both")=="both")
		{
			if(c%30==0){Cards(rand(-240,240));}
		}
		
		task Cards(yy)
		{
			let objs=ObjPrim_Create(OBJ_SPRITE_2D);
			Obj_SetRenderPriority(objs,0.24);
			ObjPrim_SetTexture(objs,dir ~ "BG/spellbgs.png");
			ObjSprite2D_SetSourceRect(objs,0,0,256,128);
			ObjSprite2D_SetDestCenter(objs);
			let ww=-360;
			let sp=rand(1,2);
			while(ww<360)
			{
				if(GetCommonData("bossspell","both")=="senri" || GetCommonData("bossspell","both")=="both"){ObjRender_SetAlpha(objs,al/2);}
				ObjRender_SetPosition(objs,GetStgFrameWidth/2+ww,GetStgFrameHeight/2+yy,0);
				ww+=sp;
				yield;
			}
			Obj_Delete(objs);
		}
	}
	Obj_Delete(obj);
	Obj_Delete(objt1);
	Obj_Delete(objt2);
}