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

#include "../system/functions.txt"

let Dif = GetCommonData("DIFFICULTY",1);
let Stage = GetCommonData("STAGE",1);
let Practice = GetCommonData("PRACTICE",false);
let mid = false;
let PlayerHit = false;

@Initialize
{
	if(GetCommonData("CURRENT_SPELL",0) < 6)
		{
		mid = true;
		}
	DeleteCommonData("BACKGROUND_END");
	SetCommonData("MIDBOSS",mid);
	TMain;
}

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

@Event
{
	alternative(GetEventType())

	case(EV_PAUSE_ENTER)
		{
		ObjSound_Stop(musicObj);
		}
	case(EV_PAUSE_LEAVE)
		{
		ObjSound_Play(musicObj);
		}
	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 path = GetCurrentScriptDirectory ~ "Background/";
	let Background = LoadScript(path ~ "Background.txt");
	StartScript(Background);
	TPOC;
	if(mid == true)
		{
		PlayMusic(path ~ "bgm_Stage",13,175.815);
		TSongName("Romantic Tour ~ Dan-no-Ura");
		if(Practice == false)
			{
			TBoss;
			SetCommonData("MIDBOSS",false);
			mid = false;
			}
		}
	TBoss;
	SetCommonData("PLAYER_LIFE",GetPlayerHealth);
	SetCommonData("PLAYER_BOMB",GetPlayerSpell);
	SetCommonData("PLAYER_FLASH",GetPlayerFlash);

	CloseScript(Background);
	CloseScript(GetOwnScriptID);
}

function TBoss
{
	let path = GetCurrentScriptDirectory ~ "Boss/Boss/";
	let BackPath = path ~ "Background.txt";
	let BossPath = path ~ "Plural.txt";
	if(mid == true)
		{
		BackPath = path ~ "BackgroundM.txt";
		}
	let Background = LoadScript(BackPath);

	StartScript(Background);
	if(Practice == true)
		{
		BossPath = path ~ "PracticePlural.txt";
		if(mid == false)
			{
			ObjSound_Stop(musicObj);
			}
		}

	let Boss = LoadScriptInThread(BossPath);
	StartScript(Boss);
	if(mid == false)
		{
		if(Practice == false)
			{
			while(Obj_GetValueD(GetPlayerObjectID,"MUSIC",true) == true){yield;}
			while(Obj_GetValueD(GetPlayerObjectID,"MUSIC",false) == false)
				{
				ObjSound_Stop(musicObj);
				yield;
				}
			}
		PlayMusic(path ~ "bgm_Boss",17.3,154.83);
		TSongName("Ancient Children's Song ~ Firefly");
		}
	while(!IsCloseScript(Boss)){yield;}
	if((mid == false && Practice == false) || Practice == true)	
		{
		SetCommonData("BACKGROUND_END",2);
		SetForbidPlayerShot(true);
		SetForbidPlayerSpell(true);
		let vol = ObjSound_GetVolumeRate(musicObj);
		descent(i in -1..149)
			{
			ObjSound_SetVolumeRate(musicObj,i*vol/150);
			yield;
			}
		SetCommonData("PLAYER_WIN",true);
		ObjSound_Stop(musicObj);
		Obj_Delete(musicObj);
		}
	CloseScript(Background);
	return;
}

task TPOC
	{
	loop(30){yield;}
	let height = GetStgFrameHeight/2-110;
	let text = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(text,GetCurrentScriptDirectory ~ "../system/img/Default_System.png");
	ObjSprite2D_SetSourceRect(text,18,130,174,152);
	ObjSprite2D_SetDestRect(text,-78*1.5,-6*1.5,78*1.5,6*1.5);
	ObjRender_SetPosition(text,GetStgFrameWidth/2,height-20,0);
	Obj_SetRenderPriority(text,0.29);

	let spread = 3;
	let obj = [];
	ascent(i in 0..spread)
		{
		obj = obj ~ [ObjPrim_Create(OBJ_SPRITE_2D)];
		ObjPrim_SetTexture(obj[i],GetCurrentScriptDirectory ~ "../system/img/Default_System.png");
		ObjSprite2D_SetSourceRect(obj[i],0,158,168,166);
		ObjRender_SetColorHSV(obj[i],(spread-i)*135/spread,255,255);
		Obj_SetRenderPriority(obj[i],0.29);
		}
	let blend = BLEND_ALPHA;

	ascent(t in 0..135)
		{
		let alpha = min(255,t*255/30);
		if(t > 105)
			{
			alpha = (135-t)*255/30;
			}
		let dist = min(1,t*1/30);
		ascent(i in 0..spread)
			{
			ObjSprite2D_SetDestRect(obj[i],GetStgFrameWidth/2-t*GetStgFrameWidth/2,height-1.5+(i*i)*2/(spread*spread),GetStgFrameWidth/2+t*GetStgFrameWidth/2,height+1.5+(i*i)*2/(spread*spread));
			ObjRender_SetAlpha(obj[i],alpha);
			ObjRender_SetBlendType(obj[i],blend);
			}
		ObjRender_SetAlpha(text,alpha);
		ObjRender_SetBlendType(text,blend);
		if(t%10 == 0)
			{
			blend = BLEND_ALPHA;
			}
		else if(t%10 == 5)
			{
			blend = BLEND_ADD_ARGB;
			}
		yield;
		}
	Obj_Delete(text);
	ascent(i in 0..spread)	
		{
		Obj_Delete(obj[i]);
		}
	}