#TouhouDanmakufu[Stage]
#Title[The Lily Fairies' Secret Garden]
#Text[The player stumbles onto the secret (squared-off) place of Lily White and Lily Black. They don't seem happy to see her, though.]
#PlayLevel[Normal]
#Player[FREE]
#BGM[.\Sounds\12 Banjo-Kazooie Music- Click Clock Wood (Spring).mp3]
#ScriptVersion[2]

script_stage_main
{
	let imgdir = GetCurrentScriptDirectory~"Images/";
	let bg1 = imgdir~"bg1.png";
	let bg2 = imgdir~"bg2.png";
	let bg3 = imgdir~"bg3.png";
	let bg4 = imgdir~"bg4.png";
	let bg5 = imgdir~"bg5.png";
	let bg6 = imgdir~"bg6.png";
	let bg7 = imgdir~"bg7.png";
	let bg8 = imgdir~"bg8.png";
	let bg9 = imgdir~"bg9.png";
	let imgpetal = imgdir~"petal.png";
	let spellimg = imgdir~"world13d.png";
	
	let spellBG_XScale = 10;
	let spellBG_YScale = 10;
	
	LoadGraphic(bg1);
	LoadGraphic(bg2);
	LoadGraphic(bg3);
	LoadGraphic(bg4);
	LoadGraphic(bg5);
	LoadGraphic(bg6);
	LoadGraphic(bg7);
	LoadGraphic(bg8);
	LoadGraphic(bg9);
	LoadGraphic(imgpetal);
	LoadGraphic(spellimg);
	
	let rotation = 0;
	let elevationangle = 20;
	let boundaryangle = 0;
	let frame = 0;
	let spawned = false;
	
	let LilyBoss = GetCurrentScriptDirectory~"LilyBattle.txt";
	SetCommonData("SpellActivate", false);
	
	@Initialize
	{
		PetalRain;
	}
	
	@MainLoop
	{
		if(GetKeyState(VK_LEFT) == KEY_HOLD && GetPlayerX < 50)
		{
			rotation += 0.2;
		}
		else if(GetKeyState(VK_RIGHT) == KEY_HOLD && GetPlayerX > 390)
		{
			rotation -= 0.2;
		}
		
		if(frame == 60)
		{
			CreateEnemyBossFromFile(LilyBoss, GetClipMinX - 60, GetCenterY, 0, 0, 0);
			spawned = true;
		}
		if(spawned && !IsBossExisting)
		{
			loop(60){
				yield;
			}
			Clear;
		}
		frame ++;
		yield;
	}
	
	@BackGround
	{
		WriteZBuffer(true);
		UseZBuffer(true);
		if(spellBG_YScale > 1){
			SetViewTo(0, 0, 0);
			SetViewFrom(400, rotation, elevationangle);
			
			SetGraphicScale(1, 1);
			DrawGrass;
			DrawBoundary;
			
			DrawBush(0, 480, 128, 128);
			DrawBush(224, 269, 128, 160);
			DrawBush(320, 210, 160, 128);
			DrawBush(448, 32, 128, 128);
			DrawBush(-448, 96, 128, 128);
			DrawBush(-312, -224, 128, 256);
			DrawBush(-217, -360, 128, 128);
			DrawBush(382, -329, 128, 300);
			DrawBush(30, -387, 128, 96);
			DrawBush(-469, 387, 96, 152);

			DrawFlowers(300, 126);
			DrawFlowers(140, 228);
			DrawFlowers(350, -420);
			DrawFlowers(32, 80);
			DrawFlowers(-58, 12);
			rotation += 0.1;
		}
		
		if(GetCommonData("SpellActivate") == true)
		{
			DrawSpellBG;
		} else {
			spellBG_XScale = 10;
			spellBG_YScale = 10;
		}
	}
	
	@Finalize
	{
		DeleteGraphic(bg1);
		DeleteGraphic(bg2);
		DeleteGraphic(bg3);
		DeleteGraphic(bg4);
		DeleteGraphic(bg5);
		DeleteGraphic(bg6);
		DeleteGraphic(bg7);
		DeleteGraphic(bg8);
		DeleteGraphic(bg9);
		DeleteGraphic(imgpetal);
		DeleteGraphic(spellimg);
	}
	
	task PetalRain
	{
		while(true)
		{
			loop(3)
			{
				yield;
			}
			CreatePetal;
		}
	}
	
	task CreatePetal
	{
		let X = rand(GetClipMinX, GetClipMaxX + 40);
		let Y = GetClipMinY - 30;
		let fallang = rand(0, 180);
		let fallspeed = rand(0.5, 2);
		let scale = rand(0.02, 0.1);
		let petrotate = 0;
		let rotatestep = rand(3, 5);
		
		let pet = Obj_Create(OBJ_EFFECT);
		ObjEffect_SetLayer(pet, 2);
		ObjEffect_SetPrimitiveType(pet, PRIMITIVE_TRIANGLESTRIP);
		ObjEffect_CreateVertex(pet, 4);
		ObjEffect_SetTexture(pet, imgpetal);
		ObjEffect_SetVertexUV(pet, 0, 0, 0);
		ObjEffect_SetVertexUV(pet, 1, 0, 162);
		ObjEffect_SetVertexUV(pet, 2, 115, 0);
		ObjEffect_SetVertexUV(pet, 3, 115, 162);
		ObjEffect_SetVertexXY(pet, 0, -58, -81);
		ObjEffect_SetVertexXY(pet, 1, -58, 81);
		ObjEffect_SetVertexXY(pet, 2, 58, -81);
		ObjEffect_SetVertexXY(pet, 3, 58, 81);
		ObjEffect_SetScale(pet, scale, scale);
		
		Obj_SetPosition(pet, X, Y);
		while(Obj_GetX(pet) > GetClipMinX - 40 && Obj_GetY(pet) < GetClipMaxY + 30)
		{
			Obj_SetPosition(pet, Obj_GetX(pet) + fallspeed*cos(fallang), Obj_GetY(pet) + fallspeed*sin(fallang));
			ObjEffect_SetAngle(pet, 180, petrotate/3, -fallang);
			petrotate += rotatestep;
			yield;
		}
	}
	
	function DrawBoundary
	{
		let ang = 0;
		let xdist = -540;
		let zdist = -540;
		let mult = -1;
		SetTexture(bg2);
		SetGraphicRect(0, 0, 128, 128);
		loop(11)
		{
			loop(2){
				SetAlpha(255);
				SetGraphicAngle(180, 0, 0);
				loop(2){
					DrawGraphic3D(xdist, 30, zdist);
					zdist += 10*mult;
					xdist += 540/10;
				}
				xdist -= 540/5;
				zdist -= 20*mult;
				zdist *= -1;
				mult *= -1;
			}
			zdist = 540;
			xdist += 540/5;
		}
		xdist = 540;
		zdist = -540;
		loop(11)
		{
			loop(2){
				SetAlpha(255);
				SetGraphicAngle(180, 90, 0);
				loop(2){
					DrawGraphic3D(xdist, 30, zdist);
					xdist += 10*mult;
					zdist += 540/10;
				}
				zdist -= 540/5;
				xdist -= 20*mult;
				xdist *= -1;
				mult *= -1;
			}
			xdist = 540;
			zdist += 540/5;
		}
		SetTexture(bg7);
		SetGraphicRect(0, 0, 1088, 512);
		SetAlpha(255);
		loop(4)
		{
			SetGraphicAngle(180, ang + 90, 0);
			DrawGraphic3D(544*cos(ang), 256, 544*sin(ang));
			ang += 360/4;
		}
	}
	
	function DrawFlowers(let X, let Z)
	{
		SetTexture(bg5);
		SetGraphicScale(0.5, 0.5);
		SetGraphicRect(0, 0, 155, 124);
		SetAlpha(255);
		SetGraphicAngle(180, -rotation+90, 0);
		DrawGraphic3D(X, 0, Z);
	}
	
	function DrawBush(let X, let Z, let width, let height)
	{
		SetTexture(bg2);
		SetGraphicRect(0, 0, 128, 128);
		SetGraphicScale(width/128, height/128);
		SetAlpha(255);
		let ang = 0;
		loop(6){
			SetGraphicAngle(180, ang, 0);
			DrawGraphic3D(X, 20, Z);
			ang += 360/6;
		}
	}
	
	function DrawTree(let X, let Z)
	{
		SetTexture(bg8);
		SetGraphicRect(0, 0, 17, 512);
		SetGraphicAngle(180, atan2(Z, X) + 90, 0);
		DrawGraphic3D(X, -200, Z);
	}

	function DrawGrass
	{
		SetTexture(bg1);
		SetGraphicRect(0, 0, 1280, 1280);
		SetGraphicAngle(90, 0, 0);
		SetAlpha(255);
		DrawGraphic3D(0, 0, 0);

		SetTexture(bg3);
		SetGraphicRect(0, 0, 1280, 1280);
		SetAlpha(120);
		SetGraphicAngle(90, 0, 0);
		DrawGraphic3D(0, 0, 0);
		SetGraphicAngle(90, 90, 0);
		DrawGraphic3D(0, 0, 0);
		SetGraphicRect(0, 0, 1152, 1152);
		SetGraphicAngle(90, 0, 0);
		DrawGraphic3D(0, 0, 0);
		SetGraphicAngle(90, 90, 0);
		DrawGraphic3D(0, 0, 0);
		
		SetTexture(bg4);
		SetGraphicRect(0, 0, 1280, 1280);
		SetAlpha(120);
		SetGraphicAngle(90, 0, 0);
		DrawGraphic3D(32, 0, 32);
		SetGraphicAngle(90, 90, 0);
		DrawGraphic3D(32, 0, 32);
		SetGraphicRect(0, 0, 1152, 1152);
		SetGraphicAngle(90, 0, 0);
		DrawGraphic3D(0, 0, 0);
		SetGraphicAngle(90, 90, 0);
		DrawGraphic3D(0, 0, 0);
	}

	function DrawSpellBG{
		SetTexture(bg1);
		SetGraphicRect(0, 0, GetClipMaxX - GetClipMinX + 20, GetClipMaxY - GetClipMinY + 20);
		SetGraphicScale(1/spellBG_XScale, 1/spellBG_YScale);
		SetGraphicAngle(0, 0, 0);
		DrawGraphic(GetCenterX, GetCenterY);
		SetTexture(spellimg);
		SetGraphicRect(0, 0, 512, 512);
		SetGraphicAngle(0, 0, 0);
		SetGraphicScale(spellBG_XScale, spellBG_YScale);
		DrawGraphic(GetCenterX, GetCenterY - 50);
		if(spellBG_XScale > 1)
		{
			spellBG_XScale --;
		} else if(spellBG_YScale > 1){
			spellBG_YScale --;
		}
	}
}