#TouhouDanmakufu[Stage]
#ScriptVersion[3]
#Title["Yuyuko"]
#System["./../../default_system/default_system.txt"]
#Text["Yuyuko Stage"]

#include"./../ZUNShot_Const.txt"
#include"./../../StageFunctions.txt"
let dir = GetCurrentScriptDirectory;
let diff;
	@Initialize
	{
		SetCommonData("Difficulty",3);
		diff = GetCommonData("difficulty", 0);
		TStage();
		SetFogEnable(true);
		SetFogParam(700, 2000, 100, 0, 100);
		SetPlayerLife(8);
	}
	@MainLoop
	{
		yield;
	}

	@Finalize
	{
	}

	task PoCCheck{
		let frame = 0;
		loop{
			if(GetPlayerY<120){
				if(frame%60==30){
					PIVItem(GetPlayerX,GetPlayerY,rand(0,360),1.5);
				}
			CollectAllItems;
			}
		frame++;
		yield;
		}
	}
	task TBackground{
		let objBack = ObjPrim_Create(OBJ_SPRITE_3D);
		//let image = GetCurrentScriptDirectory~"img/stgbg.png";

		/*ObjPrim_SetTexture(objBack,image);
		Obj_SetRenderPriorityI(objBack,21);
		ObjSprite3D_SetDestRect(objBack,-256,-1024,256,1024);
		ObjRender_SetPosition(objBack,0,0,0);
		ObjRender_SetAlpha(objBack,255);
		ObjSprite3D_SetSourceRect(objBack,0,0,1024,2048);
		ObjRender_SetZTest(objBack, true);
		ObjSprite3D_SetBillboard(objBack, true);*/
		let c = 0;
		let mesh = ObjMesh_Create();
		ObjMesh_Load(mesh, GetCurrentScriptDirectory() ~ "img/chery.mqo");
		Obj_SetRenderPriorityI(mesh,20);
		ObjRender_SetPosition(mesh,0,0,0);
		while(!Obj_IsDeleted(objBack)){
			//ObjRender_SetAngleXYZ(objBack,30,0,0);
			SetCameraElevationAngle(-10 + 5*sin(c/3));
			SetCameraAzimuthAngle(180*sin(c/30));
			SetCameraRadius(700);
			//ObjSprite3D_SetSourceRect(objBack,0,0,1024,2048);
			c++;
			yield;
		}
}
	task TRing(xstart,ystart,zstart,xplus,yplus,zplus,scale){
		let mesh = ObjMesh_Create();
		ObjMesh_Load(mesh, GetCurrentScriptDirectory() ~ "img/ring.mqo");
		Obj_SetRenderPriorityI(mesh,20);
		ObjRender_SetPosition(mesh,0,0,0);
		ObjRender_SetScaleXYZ(mesh,scale,scale,scale);
	     ObjRender_SetAngleXYZ(mesh,xstart,ystart,zstart);

		while(!Obj_IsDeleted(mesh)){
	        ObjRender_SetAngleXYZ(mesh,ObjRender_GetAngleX(mesh)+xplus,ObjRender_GetAngleY(mesh)+yplus,ObjRender_GetAngleX(mesh)+zplus);

			yield;
		}
			Obj_Delete(mesh);
}

task TRandomCall{
		TRing(0,0,90,1,0,0,1);
		TRing(0,90,0,1,0,0,0.8);
		TRing(90,0,0,1,0,0,1.2);
	}
task lifeGet(){
	let pivGet = 200000;
	let bombGet = 166000;
	loop{
		if(GetCommonData("PIValue",1)>=pivGet){
			DelaySE(GetCurrentScriptDirectory ~ "ZUN/se_extend.wav",0,0,0);
			SetPlayerLife(GetPlayerLife+1);
			pivGet+=100000;
		}
		if(GetCommonData("PIValue",1)>=bombGet){
			DelaySE(GetCurrentScriptDirectory ~ "ZUN/se_extend2.wav",0,0,0);
			SetPlayerSpell(GetPlayerSpell+1);
			bombGet+=66000;
		}
		yield;
	}
}

	task TStage
	{
		let dir = GetCurrentScriptDirectory();
		//lifeGet;
		TBackground;
		//TRandomCall;
		//Creates the boss
		let path = dir ~ "midplural.txt";
		let idScript = LoadScript(path);
		loop(1){yield;}//Assuming that compiling with finish in about a second.
		let a = 0;
		let b = 0;
		PlayMusic("MBoss1");
		loop(120){
			yield;
		}
		//〜〜〜Creating the enemy, or looping the creation of an enemy
		DeleteShotAll(TYPE_ALL, TYPE_ITEM);
		StartScript(idScript);
		//Stands by until the boss scene is finished
		while(!IsCloseScript(idScript))
		{
			if(GetPlayerLife<0){
				CloseScript(idScript); 
			}
			yield;
		}
	    //End of the stage
	    /*if(!(GetPlayerLife<0)){loop(120){yield;}
	    SetAreaCommonData("SpellData","HiScore" ~ IntToString(diff), max(GetScore,GetAreaCommonData("SpellData","HiScore" ~ IntToString(diff),0 )));
		}*/
		CloseStgScene();
	}
