#TouhouDanmakufu[Plural]
#ScriptVersion[3]
#System["./../../../system/Default_System.txt"]

#include "./../system/functions.txt"
#include "./functions/functions_stage.txt"
#include "./Enemies/Stage.txt"

let Dif = GetCommonData("DIFFICULTY",0);
let Stage = GetWorldY+1;
let Practice = GetCommonData("PRACTICE",false);
let PlayerHit = false;
let stgHeight = 2+Stage*3;	

@Initialize
{
	DeleteCommonData("BACKGROUND_END");
	if(GetCommonData("STAGE_TYPE","") == "TYPE_SPELL_PRACTICE")
		{
		Practice = true;
		}
	TMain;
	SetAutoDeleteObject(true);
}

@MainLoop
{
	if(GetPlayerLife < 0)
		{	
		EndCurrentMusic;
		CloseStgScene;
		}	
	yield;
}

@Event
{
	alternative(GetEventType())

		case(EV_PAUSE_ENTER)
			{
			PauseMusic;
			}
		case(EV_PAUSE_LEAVE)
			{
			ResumeMusic;
			}
	case(EV_PLAYER_SHOOTDOWN)
		{
		PlayerHit = true;
		}
	case(EV_START_BOSS_STEP)
		{
		PlayerHit = false;
		}
	case(EV_END_BOSS_STEP)
		{
		let scene = GetEnemyBossSceneObjectID;
		if(scene != ID_INVALID && Obj_GetValueD(scene,"INIT",false) == false && Practice == false)
			{	
			let x = Obj_GetValueD(scene,"ENMX",GetStgFrameWidth/2);
			let y = Obj_GetValueD(scene,"ENMY",GetStgFrameHeight/2-110);
			if(PlayerHit == false)
				{
				if(ObjEnemyBossScene_GetInfo(scene,INFO_IS_SPELL) == true)
					{
					CreateItem1(x,y,TYPE_LIFE);
					}
				else
					{
					CreateItem1(x,y,TYPE_SPELL);
					}
				}
			ascent(i in 0..50)
				{
				let angle = (i+rand(0,1))*360/50;
				let dist = rand(10,50);
				CreateItem1(x+dist*cos(angle),y+dist*sin(angle),TYPE_POINT);
				}
			}
		}

}


task TMain
{
	let texture = [GetCurrentScriptDirectory ~ "boss/boss/img/Back_Main.png"];
	LoadTexture(texture[0]);
	let back = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(back,texture[0]);
	ObjSprite2D_SetSourceRect(back,0,0,640,480);
	ObjRender_SetPosition(back,GetStgFrameWidth*0.5,GetStgFrameHeight*0.5,0);
	Obj_SetRenderPriority(back,0.2);
	ObjSprite2D_SetDestCenter(back);
	loop(60){yield;}

	let path = GetCurrentScriptDirectory ~ "boss/";
	let songN;
	let bossN;
	let song;
	let objScene = ObjEnemyBossScene_Create();

	LoadBoss;

	ObjEnemyBossScene_LoadInThread(objScene);
	ObjEnemyBossScene_Regist(objScene);
	TBossName(bossN);
	IsBossStart = true;

	MoveEnable = false;
	if(Practice == false)
		{
		while(Obj_GetValueD(GetPlayerObjectID,"MUSIC",true) == true){yield;}
		EndMusic(100);
		while(Obj_GetValueD(GetPlayerObjectID,"MUSIC",false) == false)
			{
			yield;
			}
		}
	else
		{
		yield;
		}
	PlayMusic(song);
	TSongName(songN);

	while(!Obj_IsDeleted(objScene)){yield;}	

	SetCommonData("BACKGROUND_END",2);
	SetForbidPlayerShot(true);
	SetForbidPlayerSpell(true);	
	FadeCurrentMusic(150);
	loop(150){yield;}
	EndMusic(song);
	SetCommonData("PLAYER_WIN",true);
	Obj_Delete(objScene);
	Obj_Delete(back);
	ascent(i in 0..length(texture))
		{
		RemoveTexture(texture[i]);
		}
	CloseScript(GetOwnScriptID);


	task LoadBoss
		{
		path = path ~ "boss/";
		bossN = "Featherine Augustus Aurora";
		songN = "Liberated Liberator";	
		song = 110;
		ObjEnemyBossScene_Add(objScene, 0, path ~ "Init.txt");
		ObjEnemyBossScene_Add(objScene, 0, path ~ "Base1.txt");
		ObjEnemyBossScene_Add(objScene, 0, path ~ "Spell1.txt");
		ObjEnemyBossScene_Add(objScene, 1, path ~ "Base2.txt");
		ObjEnemyBossScene_Add(objScene, 1, path ~ "Spell2.txt");
		ObjEnemyBossScene_Add(objScene, 2, path ~ "Spell3.txt");
		ObjEnemyBossScene_Add(objScene, 3, path ~ "Spell4.txt");
		ObjEnemyBossScene_Add(objScene, 3, path ~ "End.txt");
		}
}
