
let bSpell = false;
let current = GetCurrentScriptDirectory();
let cc = 0;
let y = 600;
let ea = -10;

let triggerBGFade = false;
@Initialize{
	SetCameraFocusX(0);
	SetCameraFocusY(90);
	SetCameraFocusZ(350);
	SetCameraRadius(250);
	SetCameraElevationAngle(5);
	SetCameraAzimuthAngle(90);
	SetFogParam(200, 850, 160, 160, 160);
	SetCameraPerspectiveClip(10, 2000);

	let fadeDelay = 360;
	let fadeT = 120;
	ascent(i in -2 .. 5){
		TNormalBackground1(i, 3, fadeDelay, fadeT, 300, 0); //gray background
		TNormalBackground1(i, 0, fadeDelay, fadeT, 0, 255); //black background
		TClouds(i);
	}

	TNormalBackground2();
	TSpellBackground_Hibaru();
	Rain();
}

@MainLoop{

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

	if(GetVirtualKeyState(VK_SHOT) == KEY_PUSH && !triggerBGFade){
		triggerBGFade = true;
	}


	cc++;
	yield;
}

@Loading{
	ascent(i in 0 .. 5){
		LoadTexture(GetCurrentScriptDirectory ~ "Resource/rain/rain_0" ~ IntToString(i) ~ ".png");
	}
}

task TNormalBackground1(i, type, delay, fadeT, al_start, al_end){
	let road = ObjPrim_Create(OBJ_SPRITE_3D);
	Obj_SetRenderPriorityI(road, 22);
	ObjPrim_SetTexture(road, GetCurrentScriptDirectory ~ "Resource/road_0" ~ IntToString(type) ~ ".png");
	ObjSprite3D_SetSourceRect(road, 0, 0, 512, 512);
	ObjSprite3D_SetDestRect(road, -256, -256, 256, 256);
	ObjRender_SetPosition(road, 0, 0, i*128);
	ObjRender_SetAngleX(road, 90);
	ObjRender_SetScaleXYZ(road, 0.25, 0.25, 0);
	ObjRender_SetAlpha(road, al_start);

	let ground1 = ObjPrim_Create(OBJ_SPRITE_3D);
	Obj_SetRenderPriorityI(ground1, 22);
	ObjPrim_SetTexture(ground1, GetCurrentScriptDirectory ~ "Resource/road_0" ~ IntToString(type + 1) ~ ".png");
	ObjSprite3D_SetSourceRect(ground1, 0, 0, 2048, 512);
	ObjSprite3D_SetDestRect(ground1, -512, -256, 512, 256);
	ObjRender_SetPosition(ground1, -320, 0, i*128);
	ObjRender_SetAngleX(ground1, 90);
	ObjRender_SetScaleXYZ(ground1, 0.25, 0.25, 0);
	ObjRender_SetAlpha(ground1, al_start);

	let ground2 = ObjPrim_Create(OBJ_SPRITE_3D);
	Obj_SetRenderPriorityI(ground2, 22);
	ObjPrim_SetTexture(ground2, GetCurrentScriptDirectory ~ "Resource/road_0" ~ IntToString(type + 1) ~ ".png");
	ObjSprite3D_SetSourceRect(ground2, 0, 0, 2048, 512);
	ObjSprite3D_SetDestRect(ground2, -1024, -256, 1024, 256);
	ObjRender_SetPosition(ground2, 320, 0, i*128);
	ObjRender_SetAngleX(ground2, 90);
	ObjRender_SetScaleXYZ(ground2, 0.25, 0.25, 0);
	ObjRender_SetAlpha(ground2, al_start);

	let tree1 = ObjPrim_Create(OBJ_SPRITE_3D);
	Obj_SetRenderPriorityI(tree1, 23);
	ObjPrim_SetTexture(tree1, GetCurrentScriptDirectory ~ "Resource/road_0" ~ IntToString(type + 2) ~ ".png");
	ObjSprite3D_SetSourceRect(tree1, 0, 0, 512, 512);
	ObjSprite3D_SetDestRect(tree1, -256, -512, 256, 0);
	ObjRender_SetPosition(tree1, -96, 0, i*128 - 64);
	ObjRender_SetAngleX(tree1, 180);
	ObjRender_SetScaleXYZ(tree1, 0.25, 0.25, 0);
	ObjRender_SetAlpha(tree1, al_start);

	let tree2 = ObjPrim_Create(OBJ_SPRITE_3D);
	Obj_SetRenderPriorityI(tree2, 23);
	ObjPrim_SetTexture(tree2, GetCurrentScriptDirectory ~ "Resource/road_0" ~ IntToString(type + 2) ~ ".png");
	ObjSprite3D_SetSourceRect(tree2, 0, 0, 512, 512);
	ObjSprite3D_SetDestRect(tree2, -256, -512, 256, 0);
	ObjRender_SetPosition(tree2, 96, 0, i*128 - 64);
	ObjRender_SetAngleX(tree2, 180);
	ObjRender_SetScaleXYZ(tree2, 0.25, 0.25, 0);
	ObjRender_SetAlpha(tree2, al_start);

	let tree3 = ObjPrim_Create(OBJ_SPRITE_3D);
	Obj_SetRenderPriorityI(tree3, 23);
	ObjPrim_SetTexture(tree3, GetCurrentScriptDirectory ~ "Resource/road_0" ~ IntToString(type + 2) ~ ".png");
	ObjSprite3D_SetSourceRect(tree3, 0, 0, 512, 512);
	ObjSprite3D_SetDestRect(tree3, -256, -512, 256, 0);
	ObjRender_SetPosition(tree3, -192, 0, i*128);
	ObjRender_SetAngleX(tree3, 180);
	ObjRender_SetScaleXYZ(tree3, 0.25, 0.25, 0);
	ObjRender_SetAlpha(tree3, al_start);

	let tree4 = ObjPrim_Create(OBJ_SPRITE_3D);
	Obj_SetRenderPriorityI(tree4, 23);
	ObjPrim_SetTexture(tree4, GetCurrentScriptDirectory ~ "Resource/road_0" ~ IntToString(type + 2) ~ ".png");
	ObjSprite3D_SetSourceRect(tree4, 0, 0, 512, 512);
	ObjSprite3D_SetDestRect(tree4, -256, -512, 256, 0);
	ObjRender_SetPosition(tree4, 192, 0, i*128);
	ObjRender_SetAngleX(tree4, 180);
	ObjRender_SetScaleXYZ(tree4, 0.25, 0.25, 0);
	ObjRender_SetAlpha(tree4, al_start);

	let tree5 = ObjPrim_Create(OBJ_SPRITE_3D);
	Obj_SetRenderPriorityI(tree5, 23);
	ObjPrim_SetTexture(tree5, GetCurrentScriptDirectory ~ "Resource/road_0" ~ IntToString(type + 2) ~ ".png");
	ObjSprite3D_SetSourceRect(tree5, 0, 0, 512, 512);
	ObjSprite3D_SetDestRect(tree5, -256, -512, 256, 0);
	ObjRender_SetPosition(tree5, -288, 0, i*128 + 64);
	ObjRender_SetAngleX(tree5, 180);
	ObjRender_SetScaleXYZ(tree5, 0.25, 0.25, 0);
	ObjRender_SetAlpha(tree5, al_start);

	let tree6 = ObjPrim_Create(OBJ_SPRITE_3D);
	Obj_SetRenderPriorityI(tree6, 23);
	ObjPrim_SetTexture(tree6, GetCurrentScriptDirectory ~ "Resource/road_0" ~ IntToString(type + 2) ~ ".png");
	ObjSprite3D_SetSourceRect(tree6, 0, 0, 512, 512);
	ObjSprite3D_SetDestRect(tree6, -256, -512, 256, 0);
	ObjRender_SetPosition(tree6, 288, 0, i*128 + 64);
	ObjRender_SetAngleX(tree6, 180);
	ObjRender_SetScaleXYZ(tree6, 0.25, 0.25, 0);
	ObjRender_SetAlpha(tree6, al_start);

	let pos = 0;
	let posTree = 0;

	let c = 0;

	let alpha = al_start;
	loop{
		if(posTree >= 128){posTree = 0;}

		ObjSprite3D_SetSourceRect(road, 0, -pos, 512, 512 - pos);
		ObjSprite3D_SetDestRect(road, -256, -256, 256, 256);

		ObjSprite3D_SetSourceRect(ground1, 0, -pos, 2048, 512 - pos);
		ObjSprite3D_SetDestRect(ground1, -1024, -256, 1024, 256);

		ObjSprite3D_SetSourceRect(ground2, 0, -pos, 2048, 512 - pos);
		ObjSprite3D_SetDestRect(ground2, -1024, -256, 1024, 256);

		ObjRender_SetZ(tree1, i*128 + posTree - 64);
		ObjRender_SetZ(tree2, i*128 + posTree - 64);

		ObjRender_SetZ(tree3, i*128 + posTree);
		ObjRender_SetZ(tree4, i*128 + posTree);

		ObjRender_SetZ(tree5, i*128 + posTree + 64);
		ObjRender_SetZ(tree6, i*128 + posTree + 64);

		if(c >= delay){
			alpha = al_start + ((c - delay)/fadeT) * (al_end - al_start);

			ObjRender_SetAlpha(road, alpha);
			ObjRender_SetAlpha(ground1, alpha);
			ObjRender_SetAlpha(ground2, alpha);
			ObjRender_SetAlpha(tree1, alpha);
			ObjRender_SetAlpha(tree2, alpha);
			ObjRender_SetAlpha(tree3, alpha);
			ObjRender_SetAlpha(tree4, alpha);
			ObjRender_SetAlpha(tree5, alpha);
			ObjRender_SetAlpha(tree6, alpha);

			if(c >= (delay + fadeT)){
				if(al_end < al_start){
					Obj_Delete(road);
					Obj_Delete(ground1);
					Obj_Delete(ground2);
					Obj_Delete(tree1);
					Obj_Delete(tree2);
					Obj_Delete(tree3);
					Obj_Delete(tree4);
					Obj_Delete(tree5);
					Obj_Delete(tree6);
					return;
				}
			}
		}


		pos += 4;
		posTree += 4 * 128/512;

		if(triggerBGFade){c++;}
		yield;
	}
}

task TClouds(i){
	let clouds = ObjPrim_Create(OBJ_SPRITE_3D);
	Obj_SetRenderPriorityI(clouds, 23);
	ObjPrim_SetTexture(clouds, GetCurrentScriptDirectory ~ "Resource/clouds_00.png");
	ObjSprite3D_SetSourceRect(clouds, 0, 0, 1024, 255);
	ObjSprite3D_SetDestRect(clouds, -512, -128, 512, 128);
	ObjRender_SetPosition(clouds, 0, 350, i*128);
	ObjRender_SetAngleX(clouds, 180);
	ObjRender_SetScaleXYZ(clouds, 2.5, 2.5, 0);
	ObjRender_SetColor(clouds, 192, 192, 192);

	let clouds2 = ObjPrim_Create(OBJ_SPRITE_3D);
	Obj_SetRenderPriorityI(clouds2, 23);
	ObjPrim_SetTexture(clouds2, GetCurrentScriptDirectory ~ "Resource/clouds_00.png");
	ObjSprite3D_SetSourceRect(clouds2, 0, 0, 1024, 255);
	ObjSprite3D_SetDestRect(clouds2, -512, -128, 512, 128);
	ObjRender_SetPosition(clouds2, 0, 450, i*128);
	ObjRender_SetAngleX(clouds2, 180);
	ObjRender_SetScaleXYZ(clouds2, 1.5, 1.5, 0);

	let posTree = 0;
	loop{
		if(posTree >= 128){posTree = 0;}
		ObjRender_SetZ(clouds, i*128 + posTree);
		ObjRender_SetZ(clouds, i*128 + posTree);

		posTree += 4 * 128/512;
		yield;
	}
}

task TNormalBackground2{
	let orangeBG = ObjPrim_Create(OBJ_SPRITE_3D);
	Obj_SetRenderPriorityI(orangeBG, 21);
	ObjPrim_SetTexture(orangeBG, GetCurrentScriptDirectory ~ "Resource/Field.png");
	ObjSprite3D_SetSourceRect(orangeBG, 0, 0, 2048, 2048);
	ObjSprite3D_SetDestRect(orangeBG, -1024, -1024, 1024, 1024);
	ObjRender_SetPosition(orangeBG, 0, 200, -600);
	ObjRender_SetColor(orangeBG, 160, 160, 160);
}

task Rain{
	let rain = ObjPrim_Create(OBJ_SPRITE_3D);
	Obj_SetRenderPriorityI(rain, 22);
	ObjPrim_SetTexture(rain, GetCurrentScriptDirectory ~ "Resource/rain/rain_00.png");
	ObjSprite3D_SetSourceRect(rain, 0, 0, 2048, 2048);
	ObjSprite3D_SetDestRect(rain, -1024, -1024, 1024, 1024);
	ObjSprite3D_SetBillboard(rain, true);
	ObjRender_SetPosition(rain, 0, 200, 0);
	ObjRender_SetScaleXYZ(rain, 0.5, 0.5, 0.5);

	let rain2 = ObjPrim_Create(OBJ_SPRITE_3D);
	Obj_SetRenderPriorityI(rain2, 23);
	ObjPrim_SetTexture(rain2, GetCurrentScriptDirectory ~ "Resource/rain/rain_00.png");
	ObjSprite3D_SetSourceRect(rain2, 0, 0, 2048, 2048);
	ObjSprite3D_SetDestRect(rain2, -1024, -1024, 1024, 1024);
	ObjSprite3D_SetBillboard(rain2, true);
	ObjRender_SetPosition(rain2, 128, 200, 0);
	ObjRender_SetScaleXYZ(rain2, 0.5, 0.5, 0.5);

	let i = 0;
	let i2 = 0;
	loop{
		if(i >= 20){i = 0;}
		if(i2 >= 40){i2 = 0;}

		SplashEffect(rand(-128, 128), rand(-256, 512));
		SplashEffect(rand(-128, 128), rand(-256, 512));

		if(i % 4 == 0){
			ObjPrim_SetTexture(rain, GetCurrentScriptDirectory ~ "Resource/rain/rain_0" ~ IntToString(i/4) ~ ".png");
		}

		if(i2 % 8 == 0){
			ObjPrim_SetTexture(rain2, GetCurrentScriptDirectory ~ "Resource/rain/rain_0" ~ IntToString(i/8) ~ ".png");
		}

		i++;
		i2++;
		yield;
	}

	task SplashEffect(x, z){
		let splash = ObjPrim_Create(OBJ_SPRITE_3D);
		Obj_SetRenderPriorityI(splash, 22);
		ObjPrim_SetTexture(splash, GetCurrentScriptDirectory ~ "Resource/rain/splash.png");
		ObjSprite3D_SetSourceRect(splash, 0, 0, 64, 64);
		ObjSprite3D_SetDestRect(splash, -32, -32, 32, 32);
		ObjRender_SetPosition(splash, x, 2, z);
		ObjRender_SetAngleX(splash, 90);
		ObjRender_SetScaleXYZ(splash, 0, 0, 0);

		let pos = 0;
		ascent(c in 0 .. 41){
			ObjRender_SetAlpha(splash, 256 * (40 - c)/40);
			ObjRender_SetScaleXYZ(splash, c/40 * 0.35, c/40 * 0.35, c/40 * 0.35);
			ObjRender_SetZ(splash, z + pos);


			pos += 4 * 128/512;
			yield;
		}
		Obj_Delete(splash);
	}
}

task TSpellBackground_Hibaru{
	let path1 = current ~ "Resource/SpellBG/Hibaru_00.png";
	let obj1 = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj1, 25);
	ObjPrim_SetTexture(obj1, path1);
	ObjSprite2D_SetSourceRect(obj1, 0, 0, 1024, 1024);
	ObjSprite2D_SetDestCenter(obj1);
	ObjRender_SetPosition(obj1, GetStgFrameWidth/2, GetStgFrameHeight/2, 0);

	let path2 = current ~ "Resource/SpellBG/Hibaru_01.png";
	let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj2, 23);
	ObjPrim_SetTexture(obj2, path2);
	ObjSprite2D_SetSourceRect(obj2, 0, 0, 1024, 512);
	ObjSprite2D_SetDestRect(obj2, 0, 0, 1024, 512);
	ObjRender_SetPosition(obj2, 0, 0, 0);
	ObjRender_SetColor(obj2, 160, 80, 80);

	let path3 = current ~ "Resource/SpellBG/Hibaru_02.png";
	let obj4 = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj4, 23);
	ObjPrim_SetTexture(obj4, path3);
	ObjSprite2D_SetSourceRect(obj4, 0, 0, 1024, 1024);
	ObjSprite2D_SetDestCenter(obj4);
	ObjRender_SetPosition(obj4, GetStgFrameWidth/2, GetStgFrameHeight/2, 0);

	let obj3 = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj3, 23);
	ObjPrim_SetTexture(obj3, path2);
	ObjSprite2D_SetSourceRect(obj3, 0, 0, 1024, 512);
	ObjSprite2D_SetDestRect(obj3, 0, 0, 1024, 512);
	ObjRender_SetPosition(obj3, 0, 0, 0);
	ObjRender_SetColor(obj3, 160, 160, 160);

	let path5 = current ~ "Resource/SpellBG/Hibaru_03.png";
	let obj5 = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj5, 24);
	ObjPrim_SetTexture(obj5, path5);
	ObjSprite2D_SetSourceRect(obj5, 0, 0, 1024, 1024);
	ObjSprite2D_SetDestCenter(obj5);
	ObjRender_SetPosition(obj5, GetStgFrameWidth/2, GetStgFrameHeight/2, 0);
	ObjRender_SetBlendType(obj5,BLEND_SUBTRACT);
	ObjRender_SetScaleXYZ(obj5,2,2,1);
	ObjRender_SetColor(obj5, 240, 120, 120);

	let alpha = 0;
	let z = 0;
	let pos = 0;
	loop{
		if(bSpell){
			if(alpha < 256){alpha += 4;}
		}else{
			if(alpha > 0){alpha -= 4;}
		}

		ObjRender_SetAlpha(obj1, alpha * 0.4);
		Obj_SetVisible(obj1, alpha > 0);
		
		ObjRender_SetAlpha(obj2, alpha * 0.6);
		Obj_SetVisible(obj2, alpha > 0);
		ObjSprite2D_SetSourceRect(obj2, 0, -pos/2, 1024, 512 - pos/2);
		ObjSprite2D_SetDestRect(obj2, 0, 0, 1024, 512);

		ObjRender_SetAlpha(obj3, alpha * 0.6);
		Obj_SetVisible(obj3, alpha > 0);
		ObjSprite2D_SetSourceRect(obj3, 0, pos/2, 1024, 512 + pos/2);
		ObjSprite2D_SetDestRect(obj3, 0, 0, 1024, 512);

		ObjRender_SetAlpha(obj4, alpha);
		Obj_SetVisible(obj4, alpha > 0);
		ObjRender_SetAngleZ(obj4, z);

		ObjRender_SetAlpha(obj5, alpha * 0.2);
		Obj_SetVisible(obj5, alpha > 0);
		ObjSprite2D_SetSourceRect(obj5, 0, -pos/4, 1024, 1024 - pos/4);
		ObjSprite2D_SetDestCenter(obj5);

		z -= 0.15;
		pos++;
		yield;
	}
}