
let bSpell = false;
let current = GetCurrentScriptDirectory();
let cc = 0;
let y = 600;
let ea = -10;
@Initialize{
	SetCameraFocusX(0);
	SetCameraFocusY(180);
	SetCameraFocusZ(500);
	SetCameraRadius(250);
	SetCameraElevationAngle(15);
	SetCameraAzimuthAngle(90);
	SetFogParam(1800, 2200, 32, 0, 32);
	SetCameraPerspectiveClip(10, 2000);

	TNormalBackground2();

	TSpellBackground_Doremy;
	TSpellBackground_Sumireko;
	TSpellBackground_Last;
}

@MainLoop{

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


	//SetCameraPitch(15*sin(cc*0.25));
	SetCameraYaw(10*sin(cc*0.125));
	SetCameraRoll(-25*sin(cc*0.125));
	cc++;
	yield;
}

task TNormalBackground2{
	let ground = ObjPrim_Create(OBJ_SPRITE_3D);
	Obj_SetRenderPriorityI(ground, 22);
	ObjPrim_SetTexture(ground, GetCurrentScriptDirectory ~ "Resource/st02a.png");
	ObjSprite3D_SetSourceRect(ground, 0, 0, 2048, 4096);
	ObjSprite3D_SetDestRect(ground, -1024, -2048, 1024, 2048);
	ObjRender_SetPosition(ground, 0, 0, 0);
	ObjRender_SetAngleX(ground, 90);

	let fog = ObjPrim_Create(OBJ_SPRITE_3D);
	Obj_SetRenderPriorityI(fog, 22);
	ObjPrim_SetTexture(fog, GetCurrentScriptDirectory ~ "Resource/st02b.png");
	ObjSprite3D_SetSourceRect(fog, 0, 0, 2048, 4096);
	ObjSprite3D_SetDestRect(fog, -1024, -2048, 1024, 2048);
	ObjRender_SetAlpha(fog, 128);
	ObjRender_SetPosition(fog, 0, 1, 0);
	ObjRender_SetAngleX(fog, 90);

	let fog2 = ObjPrim_Create(OBJ_SPRITE_3D);
	Obj_SetRenderPriorityI(fog2, 22);
	ObjPrim_SetTexture(fog2, GetCurrentScriptDirectory ~ "Resource/st02b.png");
	ObjSprite3D_SetSourceRect(fog, 0, 0, 2048, 4096);
	ObjSprite3D_SetDestRect(fog, -1024, -2048, 1024, 2048);
	ObjRender_SetAlpha(fog2, 128);
	ObjRender_SetPosition(fog2, 0, 2, 0);
	ObjRender_SetAngleX(fog2, 90);

	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, 32, 0, 32);

	let mountains = ObjPrim_Create(OBJ_SPRITE_3D);
	Obj_SetRenderPriorityI(mountains, 21);
	ObjPrim_SetTexture(mountains, GetCurrentScriptDirectory ~ "Resource/st02d.png");
	ObjSprite3D_SetSourceRect(mountains, 0, 0, 1024, 256);
	ObjSprite3D_SetDestRect(mountains, -512, -128, 512, 128);
	ObjRender_SetPosition(mountains, 0, 300, -500);
	ObjRender_SetFogEnable(mountains, false);
	ObjRender_SetAngleX(mountains, 180);
	ObjRender_SetScaleXYZ(mountains, 2, 2, 0);

	let clouds = ObjPrim_Create(OBJ_SPRITE_3D);
	Obj_SetRenderPriorityI(clouds, 22);
	ObjPrim_SetTexture(clouds, GetCurrentScriptDirectory ~ "Resource/st02c.png");
	ObjSprite3D_SetSourceRect(clouds, 0, 0, 1024, 256);
	ObjSprite3D_SetDestRect(clouds, -512, -128, 512, 128);
	ObjRender_SetPosition(clouds, 0, 450, -500);
	ObjRender_SetFogEnable(clouds, false);
	ObjRender_SetAngleX(clouds, 180);
	ObjRender_SetScaleXYZ(clouds, 1.5, 1.5, 0);

	let clouds2 = ObjPrim_Create(OBJ_SPRITE_3D);
	Obj_SetRenderPriorityI(clouds2, 22);
	ObjPrim_SetTexture(clouds2, GetCurrentScriptDirectory ~ "Resource/st02c.png");
	ObjSprite3D_SetSourceRect(clouds2, 0, 0, 1024, 256);
	ObjSprite3D_SetDestRect(clouds2, -512, -128, 512, 128);
	ObjRender_SetPosition(clouds2, 0, 400, -500);
	ObjRender_SetFogEnable(clouds2, false);
	ObjRender_SetAngleX(clouds2, 180);
	ObjRender_SetScaleXYZ(clouds2, 2, 2, 0);

	let pos = 0;
	loop{
		ObjSprite3D_SetSourceRect(ground, 0, -pos, 2048, 4096 - pos);
		ObjSprite3D_SetDestRect(ground, -1024, -2048, 1024, 2048);

		ObjSprite3D_SetSourceRect(fog, -pos*0.5, -pos, 2048 - pos*0.5, 4096 - pos);
		ObjSprite3D_SetDestRect(fog, -1024, -2048, 1024, 2048);

		ObjSprite3D_SetSourceRect(fog2, -pos*0.25, -pos, 2048 - pos*0.25, 4096 - pos);
		ObjSprite3D_SetDestRect(fog2, -1024, -2048, 1024, 2048);

		ObjSprite3D_SetSourceRect(clouds, -pos*0.125, 0, 1024 - pos*0.125, 256);
		ObjSprite3D_SetDestRect(clouds, -512, -128, 512, 128);

		ObjSprite3D_SetSourceRect(clouds2, -pos*0.15 + 256, 0, 1024 - pos*0.15 + 256, 256);
		ObjSprite3D_SetDestRect(clouds2, -512, -128, 512, 128);

		pos += 2;
		yield;
	}
}

task TSky{
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj, 20);
	ObjPrim_SetTexture(obj, GetCurrentScriptDirectory ~ "Resource/Field.png");
	ObjSprite2D_SetSourceRect(obj, 0, 0, 384, 448);
	ObjSprite2D_SetDestRect(obj, 0, 0, 384, 448);
	ObjRender_SetColor(obj, 0, 0, 0);
	while(GetCommonData("SkyPhase", 0) < 1){yield;}
	let c = 0;
	loop(180){
		ObjRender_SetColor(obj, c, c, c);
		SetFogParam(400, 1600, c, c, c);

		c += 160/180;
		yield;
	}
	ObjRender_SetColor(obj, 160, 160, 160);
	SetFogParam(400, 1600, 160, 160, 160);
}

task NoFog(obj){
	loop(70){yield;}
	ObjRender_SetFogEnable(obj, false);
}

task TSpellBackground_Doremy{
	let path1 = current ~ "./Resource/SpellBG/cdbg02a.png";
	let path2 = current ~ "./Resource/SpellBG/cdbg02b.png";

	let obj1 = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj1, 22);
	ObjPrim_SetTexture(obj1, path1);
	ObjSprite2D_SetSourceRect(obj1, 0, 0, 512, 512);
	ObjSprite2D_SetDestCenter(obj1);
	ObjRender_SetPosition(obj1, GetStgFrameWidth/2, GetStgFrameHeight/2, 0);

	let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj2, 22);
	ObjPrim_SetTexture(obj2, path2);
	ObjSprite2D_SetSourceRect(obj2, 0, 0, 256, 256);
	ObjSprite2D_SetDestCenter(obj2);
	ObjRender_SetScaleXYZ(obj2, 2, 2, 0);
	ObjRender_SetBlendType(obj2, BLEND_ADD_ARGB);
	ObjRender_SetPosition(obj2, 32, 32, 0);

	let obj3 = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj3, 22);
	ObjPrim_SetTexture(obj3, path2);
	ObjSprite2D_SetSourceRect(obj3, 0, 0, 256, 256);
	ObjSprite2D_SetDestCenter(obj3);
	ObjRender_SetScaleXYZ(obj3, 2, 2, 0);
	ObjRender_SetBlendType(obj3, BLEND_ADD_ARGB);
	ObjRender_SetPosition(obj3, GetStgFrameWidth - 32, GetStgFrameHeight - 32, 0);

	let alpha = 0;
	let frame = 0;
	let pos = 0;
	loop{
		if(pos < -512){pos = 0;}
		if(bSpell && GetCommonData("Boss", "usami") == "doremy" && !GetCommonData("LastSpell", false)){
			if(alpha <= 256){alpha += 4;}
		}else{
			if(alpha >= 0){alpha -= 4;}
		}

		ObjRender_SetAlpha(obj1, alpha);
		Obj_SetVisible(obj1, alpha > 0);
		ObjSprite2D_SetSourceRect(obj1, 0, -pos, 512, 512 - pos);

		ObjRender_SetAlpha(obj2, alpha/3);
		Obj_SetVisible(obj2, alpha > 0);
		ObjRender_SetAngleZ(obj2, -frame);

		ObjRender_SetAlpha(obj3, alpha/3);
		Obj_SetVisible(obj3, alpha > 0);
		ObjRender_SetAngleZ(obj3, frame);

		frame -= 0.5;
		pos -= 0.5;
		yield;
	}

}

task TSpellBackground_Sumireko{
	let path = current ~ "Resource/SpellBG/cdbg01a.png";
	let path2 = current ~ "Resource/SpellBG/cdbg01b.png";
	let path3 = current ~ "Resource/SpellBG/cdbg01c.png";

	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj, 22);
	ObjPrim_SetTexture(obj, path);
	ObjSprite2D_SetSourceRect(obj, 0, 0, 384, 448);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetPosition(obj, GetStgFrameWidth/2, GetStgFrameHeight/2, 0);

	let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj2, 23);
	ObjPrim_SetTexture(obj2, path2);
	ObjSprite2D_SetSourceRect(obj2, 0, 0, 512, 512);
	ObjSprite2D_SetDestCenter(obj2);
	ObjRender_SetPosition(obj2, GetStgFrameWidth/2, GetStgFrameHeight/2, 0);
	//ObjRender_SetBlendType(obj2, BLEND_ADD_ARGB);

	let obj3 = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetPrimitiveType(obj3, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetVertexCount(obj3, 4);
	Obj_SetRenderPriorityI(obj3, 23);
	ObjPrim_SetTexture(obj3, path3);
	ObjRender_SetPosition(obj3, GetStgFrameWidth/2, GetStgFrameHeight/2, 0);
	ObjRender_SetBlendType(obj3, BLEND_SUBTRACT);
	ObjRender_SetScaleXYZ(obj3, (384/256) * 1.75, (448/256) * 1.75, 0);

	let obj4 = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetPrimitiveType(obj4, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetVertexCount(obj4, 4);
	Obj_SetRenderPriorityI(obj4, 23);
	ObjPrim_SetTexture(obj4, path3);
	ObjRender_SetPosition(obj4, GetStgFrameWidth/2, GetStgFrameHeight/2, 0);
	ObjRender_SetBlendType(obj4, BLEND_SUBTRACT);
	ObjRender_SetScaleXYZ(obj4, (384/256) * 1.25, (448/256) * 1.25, 0);

	let w = 256;
	let h = 256;
	let vx = [-w/2, -w/2, w/2, w/2];
	let vy = [-h/2, h/2, -h/2, h/2];
	ascent(i in 0 .. 4){
		ObjPrim_SetVertexPosition(obj3, i, vx[i], vy[i], 0);
		ObjPrim_SetVertexPosition(obj4, i, vx[i], vy[i], 0);
	}

	let alpha = 0;
	let pos = 0;
	let a = 0;
	loop{
		if(pos > 512){pos = 0;}
		if(bSpell && GetCommonData("Boss", "usami") == "usami" && !GetCommonData("LastSpell", false)){
			if(alpha < 256){alpha += 4;}
		}else{
			if(alpha > 0){alpha -= 4;}
		}

		ascent(i in 0 .. 4){
			let pos = Rotate2D_XY(vx[i], vy[i], a);
			ObjPrim_SetVertexUVT(obj3, i, 128+pos[0], 128+pos[1]);

			let pos2 = Rotate2D_XY(vx[i], vy[i], a*1.5);
			ObjPrim_SetVertexUVT(obj4, i, 128+pos2[0], 128+pos2[1]);
		}

		ObjRender_SetAlpha(obj, alpha);
		Obj_SetVisible(obj, alpha > 0);

		ObjSprite2D_SetSourceRect(obj2, pos, pos, 512 + pos, 512 + pos);
		ObjSprite2D_SetDestCenter(obj2);
		ObjRender_SetAlpha(obj2, alpha/4);
		Obj_SetVisible(obj2, alpha > 0);

		ObjRender_SetColor(obj3, alpha*0.35, alpha*0.35, alpha*0.35);
		Obj_SetVisible(obj3, alpha > 0);

		ObjRender_SetColor(obj4, alpha*0.15, alpha*0.15, alpha*0.15);
		Obj_SetVisible(obj4, alpha > 0);

		pos += 0.5;
		a++;
		yield;
	}
}

task TSpellBackground_Last{
	let path = current ~ "Resource/SpellBG/cdbg03a.png";
	let path2 = current ~ "Resource/SpellBG/cdbg03b.png";
	let path3 = current ~ "Resource/SpellBG/cdbg01c.png";

	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj, 22);
	ObjPrim_SetTexture(obj, path);
	ObjSprite2D_SetSourceRect(obj, 0, 0, 384, 448);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetPosition(obj, GetStgFrameWidth/2, GetStgFrameHeight/2, 0);

	let obj2 = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj2, 22);
	ObjPrim_SetTexture(obj2, path2);
	ObjSprite2D_SetSourceRect(obj2, 0, 0, 384, 448);
	ObjSprite2D_SetDestCenter(obj2);
	ObjRender_SetPosition(obj2, GetStgFrameWidth/2, GetStgFrameHeight/2, 0);

	let obj3 = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetPrimitiveType(obj3, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetVertexCount(obj3, 4);
	Obj_SetRenderPriorityI(obj3, 23);
	ObjPrim_SetTexture(obj3, path3);
	ObjRender_SetPosition(obj3, GetStgFrameWidth/2, GetStgFrameHeight/2, 0);
	ObjRender_SetBlendType(obj3, BLEND_SUBTRACT);
	ObjRender_SetScaleXYZ(obj3, (384/256) * 1.75, (448/256) * 1.75, 0);

	let obj4 = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetPrimitiveType(obj4, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetVertexCount(obj4, 4);
	Obj_SetRenderPriorityI(obj4, 23);
	ObjPrim_SetTexture(obj4, path3);
	ObjRender_SetPosition(obj4, GetStgFrameWidth/2, GetStgFrameHeight/2, 0);
	ObjRender_SetBlendType(obj4, BLEND_SUBTRACT);
	ObjRender_SetScaleXYZ(obj4, (384/256) * 1.25, (448/256) * 1.25, 0);

	let w = 256;
	let h = 256;
	let vx = [-w/2, -w/2, w/2, w/2];
	let vy = [-h/2, h/2, -h/2, h/2];
	ascent(i in 0 .. 4){
		ObjPrim_SetVertexPosition(obj3, i, vx[i], vy[i], 0);
		ObjPrim_SetVertexPosition(obj4, i, vx[i], vy[i], 0);
	}

	let alpha = 0;
	let al_doremy = 0;
	let al_usami = 0;
	let pos = 0;
	let a = 0;
	loop{
		if(pos > 512){pos = 0;}
		if(bSpell && GetCommonData("LastSpell", false)){
			if(alpha < 256){alpha += 4;}

			if(GetCommonData("Boss", "usami") == "usami"){
				if(al_usami < 256){
					al_usami += 4;
				}
			}else{
				if(al_usami > 0){al_usami -= 4;}
			}

			if(GetCommonData("Boss", "usami") == "doremy"){
				if(al_doremy < 256){
					al_doremy += 4;
				}
			}else{
				if(al_doremy > 0){al_doremy -= 4;}
			}
		}else{
			if(alpha > 0){alpha -= 4;}
			if(al_doremy > 0){al_doremy -= 4;}
			if(al_usami > 0){al_usami -= 4;}
		}

		ascent(i in 0 .. 4){
			let pos = Rotate2D_XY(vx[i], vy[i], a);
			ObjPrim_SetVertexUVT(obj3, i, 128+pos[0], 128+pos[1]);

			let pos2 = Rotate2D_XY(vx[i], vy[i], a*1.5);
			ObjPrim_SetVertexUVT(obj4, i, 128+pos2[0], 128+pos2[1]);
		}

		ObjRender_SetAlpha(obj, al_doremy);
		Obj_SetVisible(obj, al_doremy > 0);

		ObjRender_SetAlpha(obj2, al_usami);
		Obj_SetVisible(obj2, al_usami > 0);

		ObjRender_SetColor(obj3, alpha*0.35, alpha*0.35, alpha*0.35);
		Obj_SetVisible(obj3, alpha > 0);

		ObjRender_SetColor(obj4, alpha*0.15, alpha*0.15, alpha*0.15);
		Obj_SetVisible(obj4, alpha > 0);

		pos += 0.5;
		a++;
		yield;
	}
}

function linear(a,b,x) {
	return a + x * (b-a);
}

function Rotate2D_XY(x, y, angle){
	let c = cos(angle);
	let s = sin(angle);
	return [x*c - y*s, x*s + y*c];
}