#TouhouDanmakufu[Single]
#ScriptVersion[3]

#include"./functions.txt"

@Initialize
{
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	TXY;

	TEnemyWork;
}

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
		{
		SetScriptResult(0);
		}
	case(EV_REQUEST_TIMER)
		{
		SetScriptResult(0);
		}
	case(EV_REQUEST_SPELL_SCORE)
		{
		SetScriptResult(30000);
		}
}

task TEnemyWork
{
	PlayMusic(0);
	PlayMusic(1);
	VolumeMusic(1,0);
	SetForbidPlayerShot(true);
	SetForbidPlayerSpell(true);
	ObjEnemy_SetDamageRate(objEnemy,0,0);
	SetPlayerClip(GetPlayerX,GetPlayerY,GetPlayerX,GetPlayerY);
	loop(120){yield;}

	ObjMove_SetPosition(objEnemy,GetStgFrameWidth*0.5,GetStgFrameHeight*0.25);
	ObjEnemy_SetRender(objEnemy,FLOW_RISE);
	ObjEnemy_RenderFlow(objEnemy);
	loop(75){yield;}
		
	let bubble = TBubble(enmx+128,enmy-12,[1,1]);
	loop(15){yield;}
	FDialogue(enmx+128,enmy-12,"Howdy!_____ It's_ me,________                          AJS!",0,3);
	FDialogue(enmx+128,enmy-12,"AJS_ the. . .",0,3);
	ObjEnemy_SetRender(objEnemy,FLOW_SMUG1);
	FDialogue(enmx+128,enmy-12,"AJS_ the. . .",0,5);
	ObjEnemy_SetRender(objEnemy,FLOW_SAD2);
	FDialogue(enmx+128,enmy-12,"The_. _. _. ____ uh__. __. __. ________                  scripter___. ___. ___. ___?",0,5);
	PlaySound("se_CuteShotShort",85);
	ObjEnemy_SetRender(objEnemy,FLOW_WINK);
	loop(30){yield;}
	ObjEnemy_SetRender(objEnemy,FLOW_MAIN);
	FDialogue(enmx+128,enmy-12,"Anyway.",0,3);
	FDialogue(enmx+128,enmy-12,"I'm_ sure_ you've_               seen_ enough_                  Undertale...",0,2);
	ObjEnemy_SetRender(objEnemy,FLOW_SMUG2);
	FDialogue(enmx+128,enmy-12,"To_ know_ where_               this_ is_ going.",0,2);


	task TDia(text)
		{
		FDialogue(enmx+128,enmy-12,text,0,2);
		}
	let shotS = 0;
	ascent(i in 0..5)
		{
		i = i-5/2+0.5;
		TShot(270+i*105/5,2);
		}

	TDia("So let's just get to it!");
	AddVirtualKey(VK_SHOT,KEY_INVALID,KEY_INVALID);
	loop(60){yield;}
	AddVirtualKey(VK_SHOT,KEY_Z,KEY_INVALID);
	while(IsOkPush == false){yield;}

	SetPlayerClip(5,20,GetStgFrameWidth-5,GetStgFrameHeight-5);
	shotS = 1;
	TDia("Try_ to_ catch_ all_               the_ Friendliness_             Pellets!");
	AddVirtualKey(VK_SHOT,KEY_INVALID,KEY_INVALID);
	loop(300){yield;}	
	shotS = 0;
	AddVirtualKey(VK_SHOT,KEY_Z,KEY_INVALID);
	while(IsOkPush == false){yield;}

	let dumDeath = false;
	let medDeath = false;
	
	if(IsDead)
		{
		EndMusic(0);
		ObjEnemy_SetRender(objEnemy,FLOW_MAD);
		FDialogue(enmx+128,enmy-12,"You_ idiot!",1,7);
		ObjEnemy_SetRender(objEnemy,FLOW_SMUG1);
		FDialogue(enmx+128,enmy-12,"Who_ dies_ to_                     something_ like_               that?!?",1,2);
		ObjEnemy_SetRender(objEnemy,FLOW_SMUG2);
		FDialogue(enmx+128,enmy-12,"You'd_ better_ be_             happy_ you_ have_            infinite_ lives.",1,2);
		ObjEnemy_SetRender(objEnemy,FLOW_MAD);
		FDialogue(enmx+128,enmy-12,"Because_ clearly_            you'll_ need_ them!",1,2);
		}
	else	
		{
		loop(1)
			{	
			FDialogue(enmx+128,enmy-12,"You...",0,6);
			if(IsDead){dumDeath = true;break;}
			EndMusic(0);
			VolumeMusic(1,100);
			ObjEnemy_SetRender(objEnemy,FLOW_EVIL);
			FDialogue(enmx+128,enmy-12,"You_ messed_ up.",0,2);
			if(IsDead){dumDeath = true;break;}
			ascent(i in 0..5)
				{
				i = i-5/2+0.5;
				TShot(270+i*120/5,2.5);
				}
			TDia("Let's_ try_ this_ again.");
			if(IsDead){dumDeath = true;break;}
			AddVirtualKey(VK_SHOT,KEY_INVALID,KEY_INVALID);
			loop(60){yield;if(IsDead){break;}}
			AddVirtualKey(VK_SHOT,KEY_Z,KEY_INVALID);
			if(IsDead){break;}
			while(IsOkPush == false){if(IsDead){dumDeath = true;}yield;}
			if(dumDeath==true){break;}
			shotS = 1;
			TDia("Run_ into_ the_                    Pellets!");
			AddVirtualKey(VK_SHOT,KEY_INVALID,KEY_INVALID);
			loop(75){if(dumDeath == true){break;}yield;}
			let dir = 1;
			if(GetPlayerX > GetStgFrameWidth*0.5)
				{
				dir = -1;		
				}
			CreateTrain(GetStgFrameWidth*(0.5-dir*0.5),GetPlayerY,8.5,dir,1);
			if(IsDead == true)
				{
				medDeath = true;
				AddVirtualKey(VK_SHOT,KEY_Z,KEY_INVALID);
				SetVirtualKeyState(VK_SHOT,KEY_PUSH);
				yield;
				}
			else
				{		
				PlaySound("se_Train",85);
				loop(165){yield;}
				AddVirtualKey(VK_SHOT,KEY_Z,KEY_INVALID);
				while(IsOkPush == false){yield;}		
				}
			}
		EndMusic(0);
		EndMusic(1);
		if(medDeath == true)
			{
			ObjEnemy_SetRender(objEnemy,FLOW_SAD1);
			loop(120){yield;}
			shotS = 0;
			FDialogue(enmx+128,enmy-12,"Uh...",0,4);
			ObjEnemy_SetRender(objEnemy,FLOW_SAD2);
			FDialogue(enmx+128,enmy-12,"Why_ would_ you_...",1,2);
			ObjEnemy_SetRender(objEnemy,FLOW_SAD1);
			FDialogue(enmx+128,enmy-12,"You're_ really_ a_               weird_ one.",1,2);
			FDialogue(enmx+128,enmy-12,"Let's_ just_ get_ on_              with_ this.",1,2);
			}
		else if(dumDeath == true)
			{
			ObjEnemy_SetRender(objEnemy,FLOW_SAD1);
			FDialogue(enmx+128,enmy-12,"._____._____._____",1,5);
			shotS = 2;
			FDialogue(enmx+128,enmy-12,"Wow.",0,9);
			FDialogue(enmx+128,enmy-12,"That_ was_ a_ fail.",1,2);
			ObjEnemy_SetRender(objEnemy,FLOW_SAD2);
			FDialogue(enmx+128,enmy-12,"This_ is_ going_ to_ be_         a_ long_ day_ isn't_ it...",1,2);
			}
		else if(IsDead)
			{
			ObjEnemy_SetRender(objEnemy,FLOW_MAD);
			FDialogue(enmx+128,enmy-12,"You_ idiot.",1,7);
			ObjEnemy_SetRender(objEnemy,FLOW_SMUG2);
			FDialogue(enmx+128,enmy-12,"What_ kind_ of_ a_                script_ did_ you_                 think_ this_ would_ be?",1,2);
			ObjEnemy_SetRender(objEnemy,FLOW_MAD);
			FDialogue(enmx+128,enmy-12,"We're_ going_ to_               have_ a_ lot_ of_                   fun_ together!",1,3);
			}
		else
			{
			ObjEnemy_SetRender(objEnemy,FLOW_MAD);
			FDialogue(enmx+128,enmy-12,"You_ must_ think_                you're_ pretty_                   clever.",1,2);
			ObjEnemy_SetRender(objEnemy,FLOW_SMUG2);
			FDialogue(enmx+128,enmy-12,"Or_ maybe_ you_              alreay_ died_ to_ it.",1,2);
			ObjEnemy_SetRender(objEnemy,FLOW_MAD);
			FDialogue(enmx+128,enmy-12,"But_ let's_ see_ just_             how_ far_ you_ can_            get!",1,2);
			}
		}
	ObjEnemy_SetRender(objEnemy,FLOW_SAD2);
	FDialogue(enmx+128,enmy-12,"Oh uh...",0,2);
	ObjEnemy_SetRender(objEnemy,FLOW_SAD1);
	FDialogue(enmx+128,enmy-12,"That_ was_ the_                   obligatory_                       nonspell_                           by_ the_ way.",0,2);
	DeleteBubble(bubble);
	ObjEnemy_SetRender(objEnemy,FLOW_FALL);

	loop(45){yield;}
	SetPlayerClip(5,20,GetStgFrameWidth-5,GetStgFrameHeight-5);
	ObjEnemy_SetLife(objEnemy,0);
	Obj_Delete(objEnemy);
	yield;
	PlayMusic(2);
	VolumeMusic(2,70);
	SetForbidPlayerShot(false);
	SetForbidPlayerSpell(false);
	CloseScript(GetOwnScriptID);

	task TShot(angle,speed)
		{
		let shot = CreateShotA1(enmx,enmy,0,0,PELLET_WHITE,0);
		ObjShot_SetSpellResist(shot,true);

		let numb = 0;
		let counter = -1;

		while(!Obj_IsDeleted(shot))
			{
			numb+=0.25;
			ObjRender_SetAngleZ(shot,30+floor(numb)*90);
			if(counter < 45)
				{
				counter+=1;
				ObjMove_SetPosition(shot,ObjMove_GetX(shot)+1.5*cos(angle),ObjMove_GetY(shot)+1.5*sin(angle));
				}
			else if(shotS == 1)
				{
				if(counter == 45)
					{
					angle = GetAngleToPlayer(shot);
					}
				counter+=1;
				ObjMove_SetPosition(shot,ObjMove_GetX(shot)+speed*cos(angle),ObjMove_GetY(shot)+speed*sin(angle));
				}
			else if(shotS == 2)
				{
				if(counter == 45)
					{
					angle+=180;
					Obj_SetRenderPriority(shot,0.3);
					}
				else if(counter == 90)
					{
					Obj_Delete(shot);
					}
				counter+=1;
				ObjMove_SetPosition(shot,ObjMove_GetX(shot)+1.5*cos(angle),ObjMove_GetY(shot)+1.5*sin(angle));
				}
			yield;
			}
		}

	function IsDead
		{
		if(GetCommonData("DEATH_COUNT",0) > 0)
			{
			return true;
			}
		else
			{
			return false;
			}
		}

}	
