#include"./function.txt"

let objScene 		= GetEnemyBossSceneObjectID;
let BossSpell 		= false;

@Initialize
{
	SetCameraFocusX(0);
	SetCameraFocusY(0);
	SetCameraFocusZ(0);
	SetCameraAzimuthAngle(90);
    SetCameraElevationAngle(330);
	TCallBg;
	TSpell;
}

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

task TCallBg
{
	bgFloor1;
	bgFloor2;
}

task bgFloor1
{
	let sr = [1280,2560*3];
	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	Obj_SetRenderPriority		(obj,0);
	ObjPrim_SetTexture			(obj,bgTile);
	ObjSprite3D_SetSourceRect	(obj,0,0,sr[0],sr[1]);
	ObjSprite3D_SetDestRect		(obj,-sr[0]/2,-sr[1]/2,sr[0]/2,sr[1]/2);
	ObjRender_SetBlendType		(obj,BLEND_ALPHA);
	ObjRender_SetPosition		(obj,0,0,0);
	ObjRender_SetScaleXYZ		(obj,90/1280,90/1280,0);
	let c = 0;
	let spd = 0;
	let rgb = [90,90,90];
	while(!Obj_IsDeleted(obj))
	{
		if(c < 0.6){c += 0.6/750;}
		if(GetCommonData("bgMode",0) == 0)
		{
			if(rgb[0] < 90){rgb[0] = rgb[0] + 255/GetCommonData("bgModeTime",0);}
			if(rgb[0] > 90){rgb[0] = rgb[0] - 255/GetCommonData("bgModeTime",0);}
			if(rgb[1] < 180){rgb[1] = rgb[1] + 255/GetCommonData("bgModeTime",0);}
			if(rgb[1] > 180){rgb[1] = rgb[1] - 255/GetCommonData("bgModeTime",0);}
			if(rgb[2] < 90){rgb[2] = rgb[2] + 255/GetCommonData("bgModeTime",0);}
			if(rgb[2] > 90){rgb[2] = rgb[2] - 255/GetCommonData("bgModeTime",0);}
		}
		if(GetCommonData("bgMode",0) == 1)
		{
			if(rgb[0] < 105){rgb[0] = rgb[0] + 255/GetCommonData("bgModeTime",0);}
			if(rgb[0] > 105){rgb[0] = rgb[0] - 255/GetCommonData("bgModeTime",0);}
			if(rgb[1] < 210){rgb[1] = rgb[1] + 255/GetCommonData("bgModeTime",0);}
			if(rgb[1] > 210){rgb[1] = rgb[1] - 255/GetCommonData("bgModeTime",0);}
			if(rgb[2] < 90){rgb[2] = rgb[2] + 255/GetCommonData("bgModeTime",0);}
			if(rgb[2] > 90){rgb[2] = rgb[2] - 255/GetCommonData("bgModeTime",0);}
			//if(rgb[0] < 255){rgb[0] = rgb[0] + 255/GetCommonData("bgModeTime",0);}
			//if(rgb[0] > 255){rgb[0] = rgb[0] - 255/GetCommonData("bgModeTime",0);}
			//if(rgb[1] < 75){rgb[1] = rgb[1] + 255/GetCommonData("bgModeTime",0);}
			//if(rgb[1] > 75){rgb[1] = rgb[1] - 255/GetCommonData("bgModeTime",0);}
			//if(rgb[2] < 75){rgb[2] = rgb[2] + 255/GetCommonData("bgModeTime",0);}
			//if(rgb[2] > 75){rgb[2] = rgb[2] - 255/GetCommonData("bgModeTime",0);}
		}
		if(GetCommonData("bgMode",0) == 2)
		{
			if(rgb[0] < 240){rgb[0] = rgb[0] + 255/GetCommonData("bgModeTime",0);}
			if(rgb[0] > 240){rgb[0] = rgb[0] - 255/GetCommonData("bgModeTime",0);}
			if(rgb[1] < 150){rgb[1] = rgb[1] + 255/GetCommonData("bgModeTime",0);}
			if(rgb[1] > 150){rgb[1] = rgb[1] - 255/GetCommonData("bgModeTime",0);}
			if(rgb[2] < 60){rgb[2] = rgb[2] + 255/GetCommonData("bgModeTime",0);}
			if(rgb[2] > 60){rgb[2] = rgb[2] - 255/GetCommonData("bgModeTime",0);}
		}
		if(GetCommonData("bgMode",0) == 3)
		{
			if(rgb[0] < 165){rgb[0] = rgb[0] + 255/GetCommonData("bgModeTime",0);}
			if(rgb[0] > 165){rgb[0] = rgb[0] - 255/GetCommonData("bgModeTime",0);}
			if(rgb[1] < 165){rgb[1] = rgb[1] + 255/GetCommonData("bgModeTime",0);}
			if(rgb[1] > 165){rgb[1] = rgb[1] - 255/GetCommonData("bgModeTime",0);}
			if(rgb[2] < 165){rgb[2] = rgb[2] + 255/GetCommonData("bgModeTime",0);}
			if(rgb[2] > 165){rgb[2] = rgb[2] - 255/GetCommonData("bgModeTime",0);}
		}
		if(GetCommonData("bgMode",0) == 4)
		{
			if(rgb[0] < 210){rgb[0] = rgb[0] + 255/GetCommonData("bgModeTime",0);}
			if(rgb[0] > 210){rgb[0] = rgb[0] - 255/GetCommonData("bgModeTime",0);}
			if(rgb[1] < 135){rgb[1] = rgb[1] + 255/GetCommonData("bgModeTime",0);}
			if(rgb[1] > 135){rgb[1] = rgb[1] - 255/GetCommonData("bgModeTime",0);}
			if(rgb[2] < 195){rgb[2] = rgb[2] + 255/GetCommonData("bgModeTime",0);}
			if(rgb[2] > 195){rgb[2] = rgb[2] - 255/GetCommonData("bgModeTime",0);}
		}
		ObjRender_SetColor			(obj,rgb[0]*c,rgb[1]*c,rgb[2]*c);
		ObjSprite3D_SetSourceRect	(obj,0,spd,sr[0],sr[1]+spd);
		spd += GetCommonData("bgSpeed",0.5);
		yield;
	}
	Obj_Delete(obj);
}

task bgFloor2
{
	let sr = [1280,2560*3];
	let dimY = 1280;
	let obj = ObjPrim_Create(OBJ_SPRITE_3D);
	Obj_SetRenderPriority		(obj,0);
	ObjPrim_SetTexture			(obj,bgTile);
	ObjSprite3D_SetSourceRect	(obj,0,dimY,sr[0],sr[1]+dimY);
	ObjSprite3D_SetDestRect		(obj,-sr[0]/2,-sr[1]/2,sr[0]/2,sr[1]/2);
	ObjRender_SetBlendType		(obj,BLEND_ADD_ARGB);
	ObjRender_SetPosition		(obj,0,0,0);
	ObjRender_SetColor			(obj,30,120,30);
	ObjRender_SetScaleXYZ		(obj,60/1280,60/1280,0);
	let c = 0;
	let spd = dimY;
	let rgb = [90,90,90];
	while(!Obj_IsDeleted(obj))
	{
		if(c < 0.2){c += 0.2/750;}
		if(GetCommonData("bgMode",0) == 0)
		{
			if(rgb[0] < 105){rgb[0] = rgb[0] + 255/GetCommonData("bgModeTime",0);}
			if(rgb[0] > 105){rgb[0] = rgb[0] - 255/GetCommonData("bgModeTime",0);}
			if(rgb[1] < 105){rgb[1] = rgb[1] + 255/GetCommonData("bgModeTime",0);}
			if(rgb[1] > 105){rgb[1] = rgb[1] - 255/GetCommonData("bgModeTime",0);}
			if(rgb[2] < 150){rgb[2] = rgb[2] + 255/GetCommonData("bgModeTime",0);}
			if(rgb[2] > 150){rgb[2] = rgb[2] - 255/GetCommonData("bgModeTime",0);}
		}
		if(GetCommonData("bgMode",0) == 1)
		{
			if(rgb[0] < 150){rgb[0] = rgb[0] + 255/GetCommonData("bgModeTime",0);}
			if(rgb[0] > 150){rgb[0] = rgb[0] - 255/GetCommonData("bgModeTime",0);}
			if(rgb[1] < 135){rgb[1] = rgb[1] + 255/GetCommonData("bgModeTime",0);}
			if(rgb[1] > 135){rgb[1] = rgb[1] - 255/GetCommonData("bgModeTime",0);}
			if(rgb[2] < 120){rgb[2] = rgb[2] + 255/GetCommonData("bgModeTime",0);}
			if(rgb[2] > 120){rgb[2] = rgb[2] - 255/GetCommonData("bgModeTime",0);}
			//if(rgb[0] < 165){rgb[0] = rgb[0] + 255/GetCommonData("bgModeTime",0);}
			//if(rgb[0] > 165){rgb[0] = rgb[0] - 255/GetCommonData("bgModeTime",0);}
			//if(rgb[1] < 180){rgb[1] = rgb[1] + 255/GetCommonData("bgModeTime",0);}
			//if(rgb[1] > 180){rgb[1] = rgb[1] - 255/GetCommonData("bgModeTime",0);}
			//if(rgb[2] < 60){rgb[2] = rgb[2] + 255/GetCommonData("bgModeTime",0);}
			//if(rgb[2] > 60){rgb[2] = rgb[2] - 255/GetCommonData("bgModeTime",0);}
		}
		if(GetCommonData("bgMode",0) == 2)
		{
			if(rgb[0] < 195){rgb[0] = rgb[0] + 255/GetCommonData("bgModeTime",0);;}
			if(rgb[0] > 195){rgb[0] = rgb[0] - 255/GetCommonData("bgModeTime",0);;}
			if(rgb[1] < 195){rgb[1] = rgb[1] + 255/GetCommonData("bgModeTime",0);;}
			if(rgb[1] > 195){rgb[1] = rgb[1] - 255/GetCommonData("bgModeTime",0);;}
			if(rgb[2] < 60){rgb[2] = rgb[2] + 255/GetCommonData("bgModeTime",0);;}
			if(rgb[2] > 60){rgb[2] = rgb[2] - 255/GetCommonData("bgModeTime",0);;}
		}
		if(GetCommonData("bgMode",0) == 3)
		{
			if(rgb[0] < 150){rgb[0] = rgb[0] + 255/GetCommonData("bgModeTime",0);;}
			if(rgb[0] > 150){rgb[0] = rgb[0] - 255/GetCommonData("bgModeTime",0);;}
			if(rgb[1] < 150){rgb[1] = rgb[1] + 255/GetCommonData("bgModeTime",0);;}
			if(rgb[1] > 150){rgb[1] = rgb[1] - 255/GetCommonData("bgModeTime",0);;}
			if(rgb[2] < 195){rgb[2] = rgb[2] + 255/GetCommonData("bgModeTime",0);;}
			if(rgb[2] > 195){rgb[2] = rgb[2] - 255/GetCommonData("bgModeTime",0);;}
		}
		if(GetCommonData("bgMode",0) == 4)
		{
			if(rgb[0] < 195){rgb[0] = rgb[0] + 255/GetCommonData("bgModeTime",0);;}
			if(rgb[0] > 195){rgb[0] = rgb[0] - 255/GetCommonData("bgModeTime",0);;}
			if(rgb[1] < 165){rgb[1] = rgb[1] + 255/GetCommonData("bgModeTime",0);;}
			if(rgb[1] > 165){rgb[1] = rgb[1] - 255/GetCommonData("bgModeTime",0);;}
			if(rgb[2] < 180){rgb[2] = rgb[2] + 255/GetCommonData("bgModeTime",0);;}
			if(rgb[2] > 180){rgb[2] = rgb[2] - 255/GetCommonData("bgModeTime",0);;}
		}
		ObjRender_SetColor			(obj,rgb[0]*c,rgb[1]*c,rgb[2]*c);
		ObjSprite3D_SetSourceRect	(obj,0,spd,sr[0],sr[1]+spd);
		spd += GetCommonData("bgSpeed",0.5)*0.6;
		yield;
	}
	Obj_Delete(obj);
}

task TSpell
{
	bgSpell;
	bgCrack;
	bgStarry;
	task bgSpell
	{
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		Obj_SetRenderPriority		(obj,0);
		ObjPrim_SetTexture			(obj,sfxSquare);
		ObjSprite2D_SetSourceRect	(obj,0,0,640,480);
		ObjSprite2D_SetDestCenter	(obj);
		ObjRender_SetColor			(obj,0,0,0);
		ObjRender_SetPosition		(obj,320,240,0);
		let c = 0;
		let time = 30;
		while(!Obj_IsDeleted(obj))
		{
			if(BossSpell == true && c < 1)	{c += 1 / time;}
			if(BossSpell == false && c > 0)	{c -= 1 / time;}
			ObjRender_SetAlpha				(obj,c * 150);
			yield;
		}
	}
	task bgCrack
	{
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		Obj_SetRenderPriority		(obj,0);
		ObjPrim_SetTexture			(obj,sfxCrack);
		ObjSprite2D_SetSourceRect	(obj,0,0,512,512);
		ObjSprite2D_SetDestCenter	(obj);
		ObjRender_SetPosition		(obj,320,240,0);
		ObjRender_SetBlendType		(obj,BLEND_ADD_ARGB);
		ObjRender_SetScaleXYZ		(obj,720/512,720/512,0);
		let c = 0;
		let time = 60;
		let rgb = [90,90,90];
		let z = rand(0,360);
		while(!Obj_IsDeleted(obj))
		{
			if(BossSpell == true && c < 1)	{c += 1 / time;}
			if(BossSpell == false && c > 0)		{c -= 1 / time;}
			if(GetCommonData("bgMode",0) == 0)
			{
				if(rgb[0] < 120){rgb[0] = rgb[0] + 255/GetCommonData("bgModeTime",0);}
				if(rgb[0] > 120){rgb[0] = rgb[0] - 255/GetCommonData("bgModeTime",0);}
				if(rgb[1] < 120){rgb[1] = rgb[1] + 255/GetCommonData("bgModeTime",0);}
				if(rgb[1] > 120){rgb[1] = rgb[1] - 255/GetCommonData("bgModeTime",0);}
				if(rgb[2] < 120){rgb[2] = rgb[2] + 255/GetCommonData("bgModeTime",0);}
				if(rgb[2] > 120){rgb[2] = rgb[2] - 255/GetCommonData("bgModeTime",0);}
			}
			if(GetCommonData("bgMode",0) == 1)
			{
				//if(rgb[0] < 105){rgb[0] = rgb[0] + 255/GetCommonData("bgModeTime",0);}
				//if(rgb[0] > 105){rgb[0] = rgb[0] - 255/GetCommonData("bgModeTime",0);}
				//if(rgb[1] < 210){rgb[1] = rgb[1] + 255/GetCommonData("bgModeTime",0);}
				//if(rgb[1] > 210){rgb[1] = rgb[1] - 255/GetCommonData("bgModeTime",0);}
				//if(rgb[2] < 90){rgb[2] = rgb[2] + 255/GetCommonData("bgModeTime",0);}
				//if(rgb[2] > 90){rgb[2] = rgb[2] - 255/GetCommonData("bgModeTime",0);}
				if(rgb[0] < 255){rgb[0] = rgb[0] + 255/GetCommonData("bgModeTime",0);}
				if(rgb[0] > 255){rgb[0] = rgb[0] - 255/GetCommonData("bgModeTime",0);}
				if(rgb[1] < 75){rgb[1] = rgb[1] + 255/GetCommonData("bgModeTime",0);}
				if(rgb[1] > 75){rgb[1] = rgb[1] - 255/GetCommonData("bgModeTime",0);}
				if(rgb[2] < 75){rgb[2] = rgb[2] + 255/GetCommonData("bgModeTime",0);}
				if(rgb[2] > 75){rgb[2] = rgb[2] - 255/GetCommonData("bgModeTime",0);}
			}
			if(GetCommonData("bgMode",0) == 2)
			{
				if(rgb[0] < 240){rgb[0] = rgb[0] + 255/GetCommonData("bgModeTime",0);}
				if(rgb[0] > 240){rgb[0] = rgb[0] - 255/GetCommonData("bgModeTime",0);}
				if(rgb[1] < 150){rgb[1] = rgb[1] + 255/GetCommonData("bgModeTime",0);}
				if(rgb[1] > 150){rgb[1] = rgb[1] - 255/GetCommonData("bgModeTime",0);}
				if(rgb[2] < 60){rgb[2] = rgb[2] + 255/GetCommonData("bgModeTime",0);}
				if(rgb[2] > 60){rgb[2] = rgb[2] - 255/GetCommonData("bgModeTime",0);}
			}
			if(GetCommonData("bgMode",0) == 3)
			{
				if(rgb[0] < 150){rgb[0] = rgb[0] + 255/GetCommonData("bgModeTime",0);}
				if(rgb[0] > 150){rgb[0] = rgb[0] - 255/GetCommonData("bgModeTime",0);}
				if(rgb[1] < 150){rgb[1] = rgb[1] + 255/GetCommonData("bgModeTime",0);}
				if(rgb[1] > 150){rgb[1] = rgb[1] - 255/GetCommonData("bgModeTime",0);}
				if(rgb[2] < 195){rgb[2] = rgb[2] + 255/GetCommonData("bgModeTime",0);}
				if(rgb[2] > 195){rgb[2] = rgb[2] - 255/GetCommonData("bgModeTime",0);}
			}
			if(GetCommonData("bgMode",0) == 4)
			{
				if(rgb[0] < 210){rgb[0] = rgb[0] + 255/GetCommonData("bgModeTime",0);}
				if(rgb[0] > 210){rgb[0] = rgb[0] - 255/GetCommonData("bgModeTime",0);}
				if(rgb[1] < 135){rgb[1] = rgb[1] + 255/GetCommonData("bgModeTime",0);}
				if(rgb[1] > 135){rgb[1] = rgb[1] - 255/GetCommonData("bgModeTime",0);}
				if(rgb[2] < 195){rgb[2] = rgb[2] + 255/GetCommonData("bgModeTime",0);}
				if(rgb[2] > 195){rgb[2] = rgb[2] - 255/GetCommonData("bgModeTime",0);}
			}
			ObjRender_SetColor				(obj,rgb[0]*c,rgb[1]*c,rgb[2]*c);
			ObjRender_SetAngleZ				(obj,z);
			z += 0.1;
			yield;
		}
	}
	task bgStarry
	{
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		Obj_SetRenderPriority		(obj,0);
		ObjPrim_SetTexture			(obj,sfxStarry);
		ObjSprite2D_SetSourceRect	(obj,0,0,512,512);
		ObjSprite2D_SetDestCenter	(obj);
		ObjRender_SetPosition		(obj,320,240,0);
		ObjRender_SetScaleXYZ		(obj,720/512,720/512,0);
		let c = 0;
		let time = 30;
		let z = rand(0,360);
		while(!Obj_IsDeleted(obj))
		{
			if(BossSpell == true && c < 1)	{c += 1 / time;}
			if(BossSpell == false && c > 0)	{c -= 1 / time;}
			ObjRender_SetAlpha				(obj,c * 120);
			ObjRender_SetAngleZ				(obj,z);
			z -= 0.05;
			yield;
		}
	}
}