#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
{
	EndMusic(3);
	SetForbidPlayerShot(true);
	SetForbidPlayerSpell(true);
	ObjEnemy_SetDamageRate(objEnemy,0,0);
	ObjMove_SetPosition(objEnemy,GetStgFrameWidth*0.5,GetStgFrameHeight*0.25);
	ObjEnemy_SetRender(objEnemy,FLOW_RISE);
	ObjEnemy_RenderFlow(objEnemy);

	let xP = GetPlayerX;
	let yP = GetPlayerY;
	let xM = GetStgFrameWidth*0.5;
	let yM = GetStgFrameHeight-32;
	ascent(t in 0..75)
		{
		t = (t+1)/75;
		let x = (1-t)*xP+t*xM;
		let y = (1-t)*yP+t*yM;
		SetPlayerClip(x,y,x,y);
		yield;
		}
	loop(30){yield;}
		
	let bubble1 = TBubble(enmx+128,enmy-12,[1,1]);
	loop(15){yield;}
	FDialogue(enmx+128,enmy-12,"Nice_ job_ getting_             this_ far.",0,3);
	ObjEnemy_SetRender(objEnemy,FLOW_SMUG1);
	FDialogue(enmx+128,enmy-12,"Even_ if_ you_ do_                have_ infinite_ lives.",0,5);
	ObjEnemy_SetRender(objEnemy,FLOW_SMUG2);
	FDialogue(enmx+128,enmy-12,"But_ let's_ see_ how_             you_ do_ against_              my_ final_ attack!",0,5);
	ObjEnemy_SetRender(objEnemy,FLOW_MAD);
	FDialogue(enmx+128,enmy-12,"I'm_ sure_ you'll_                have_ a_ great_ time!",1,3);
	ObjEnemy_SetRender(objEnemy,FLOW_SMUG2);
	FDialogue(enmx+128,enmy-12,"Are_ you_ read?",0,2);
	ObjEnemy_SetRender(objEnemy,FLOW_SMUG2);
	FDialogue(enmx+128,enmy-12,"Because_ here_",0,2);
	FDialogue(enmx+128,enmy-12,"It",0,2);
	FDialogue(enmx+128,enmy-12,"Comes!",0,2);
	loop(120){yield;}
	ObjEnemy_SetRender(objEnemy,FLOW_SMUG1);
	FDialogue(enmx+128,enmy-12,"...",0,30);
	FDialogue(enmx+128,enmy-12,"Yeah...",0,5);
	ObjEnemy_SetRender(objEnemy,FLOW_SAD1);
	FDialogue(enmx+128,enmy-12,"I_ got_ nothing.",0,2);
	FDialogue(enmx+128,enmy-12,"Sorry_ about_ that.",0,2);
	ObjEnemy_SetRender(objEnemy,FLOW_SAD2);
	FDialogue(enmx+128,enmy-12,"I_ guess_ you_                     should_ just...",0,2);
	FDialogue(enmx+128,enmy-12,"Quit_ or_ something?",0,2);
	DeleteBubble(bubble1);
	ObjEnemy_SetRender(objEnemy,FLOW_SAD1);

	let aX = 96;
	let aY = GetStgFrameHeight*0.5-30;
	let bubble2 = TBubble(aX,aY,[1,1]);
	FDialogue(aX,aY,"What_ the_ Hell?!?",0,2);
	DeleteBubble(bubble2);

	let obj = ObjEnemy_Create(OBJ_ENEMY);
	ObjEnemy_Regist(obj);
	ObjPrim_SetTexture(obj,GetCurrentScriptDirectory ~ "img/Boss/AJS.png");
	ObjSprite2D_SetSourceRect(obj,0,0,45,82);
	ObjMove_SetPosition(obj,-aX,aY);
	ObjSprite2D_SetDestCenter(obj);
	ObjMove_SetDestAtWeight(obj,aX,aY,30,5);
	ObjRender_SetScaleX(obj,-1);
	let angleZ = 0;
	while(ObjMove_GetSpeed(obj) > 0)
		{
		angleZ+=(720-angleZ)/25;
		ObjRender_SetAngleZ(obj,angleZ);
		yield;
		}
	loop(30){yield;}
	aX = ObjMove_GetX(obj)+116;
	aY = ObjMove_GetY(obj)+12;
	bubble2 = TBubble(aX,aY,[1,-1]);
	FDialogue(aX,aY,"Was_ this_ supposed_          to_ be_ a_ Christmas_         present_ or_                        something?",0,2);
	FDialogue(aX,aY,"Cause_ I_ really_                 hope_ you_ kept_               the_ receipt.",0,2);
	DeleteBubble(bubble2);
	
	bubble1 = TBubble(enmx+128,enmy-12,[1,1]);
	ObjEnemy_SetRender(objEnemy,FLOW_SAD2);
	FDialogue(enmx+128,enmy-12,"Oh_______ uh..._______",0,2);
	ObjEnemy_SetRender(objEnemy,FLOW_SMUG1);
	FDialogue(enmx+128,enmy-12,"What_ are_ you_                 doing_ here?",0,2);
	ObjEnemy_SetRender(objEnemy,FLOW_SAD1);
	DeleteBubble(bubble1);

	bubble2 = TBubble(aX,aY,[1,-1]);
	FDialogue(aX,aY,"Someone_ was_                 supposed_ to_ make_       a_ script_ based_               on_ me.",0,2);
	FDialogue(aX,aY,"But_ instead_ they_             just_ spammed_ a_             bunch_ of_ memes!",0,2);
	FDialogue(aX,aY,"Half_ of_ them_ didn't_        even_ have_                       anything_ to_ do_              with_ me!",0,2);
	FDialogue(aX,aY,"The_ patterns_                   weren't_ great_                  either...",0,2);
	DeleteBubble(bubble2);

	bubble1 = TBubble(enmx+128,enmy-12,[1,1]);
	FDialogue(enmx+128,enmy-12,"I_ think_ it_ was_                    pretty_ good.",0,2);
	FDialogue(enmx+128,enmy-12,"For_ the_ amount_ of_          time_ it_ was_ made_           in.",0,2);
	ObjEnemy_SetRender(objEnemy,FLOW_SMUG1);
	FDialogue(enmx+128,enmy-12,"Besides",0,2);
	ObjEnemy_SetRender(objEnemy,FLOW_SMUG2);
	FDialogue(enmx+128,enmy-12,"It's_ the_ though_                 that_ counts______, right?",0,2);
	DeleteBubble(bubble1);

	loop(90){yield;}
	
	local
		{
		let sprk = ObjPrim_Create(OBJ_SPRITE_2D);
		let img = GetCurrentScriptDirectory ~ "img/sprk.png";
		ObjPrim_SetTexture(sprk,img);
		ObjSprite2D_SetSourceRect(sprk,1,0,128,128);	
		Obj_SetRenderPriority(sprk,0.48);
		ObjRender_SetBlendType(sprk,BLEND_ADD_ARGB);
		let counter = 0;
		let scaleX = 5;
		let scaleY = 0;
		let speed = 1.5/105;
		let x = ObjMove_GetX(obj);	
		let y = ObjMove_GetY(obj)+8;
		let angle = atan2(enmy-y,enmx-x);
		let angleZ = 0;
		let end = false;
		loop
			{
			counter+=1;
			if(counter%10 == 0 && counter > 120)
				{
				TShot;
				}
			if(counter < 105)
				{
				speed*=(1+2/105);
				scaleY+=speed;
				}
			if(end == true)
				{
				if(scaleX > 0)	
					{
					scaleX-=(128*4)/20;
					}
				else
					{
					Obj_Delete(sprk);
					break;
					}
				}
			else if(counter >= 120 && scaleX < 124*3)
				{
				scaleX+=(128*4-3)/15;
				}		
			if(counter == 120)
				{
				PlaySound("se_MasterSpark",150);
				PlaySound("se_PlayerDie",90);
				ObjEnemy_SetRender(objEnemy,FLOW_SAD1);
				ObjMove_SetSpeed(objEnemy,12);
				ObjMove_SetAngle(objEnemy,angle);
				}
			if(counter >= 120)
				{
				angleZ+=4;
				ObjRender_SetAngleZ(objEnemy,angleZ);
				}
			if(counter == 360)
				{	
				end = true;
				}
			ObjRender_SetPosition(sprk,x+64*scaleY*cos(angle),y-2+64*scaleY*sin(angle),0);
			ObjRender_SetAngleZ(sprk,angle);
			ObjRender_SetScaleXYZ(sprk,scaleY,scaleX/128,0);
			ObjRender_SetAlpha(sprk,rand(155,255));
			ObjSprite2D_SetDestCenter(sprk);		
			yield;
			}
		task TShot
			{
			let shot = ObjPrim_Create(OBJ_SPRITE_2D);
			ObjPrim_SetTexture(shot,img);
			ObjSprite2D_SetSourceRect(shot,0,128,256,222);
			ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);
			Obj_SetRenderPriority(shot,0.54);
			ObjRender_SetAngleZ(shot,angle+90);
			ObjRender_SetColorHSV(shot,rand(0,360),75,255);
			let scalex = 0;
			let scaley = 0;
			let speed = 1;
			let dist = 0;
			loop(90)
				{
				speed+=1;
				dist+=speed;
				if(scalex < scaleX)
					{
					scalex+=16;
					}
				if(scaley < 1)
					{
					scaley+=1/15;
					}
				ObjRender_SetPosition(shot,x+dist*cos(angle),y+dist*sin(angle),0);
				ObjRender_SetScaleXYZ(shot,scalex/256,scaley,0);
				ObjSprite2D_SetDestCenter(shot);
				yield;
				}
			Obj_Delete(shot);
			}
		}
	loop(75){yield;}

	bubble2 = TBubble(aX,aY,[1,-1]);
	FDialogue(aX,aY,"Oh_ yeah,_ you.",0,2);
	FDialogue(aX,aY,"You_ wanted_ a_                 final_ attack.",0,2);
	FDialogue(aX,aY,"Uh...",0,7);
	FDialogue(aX,aY,"Here_____, I_ guess.",0,2);
	DeleteBubble(bubble2);

	local
		{
		let obj = ObjPrim_Create(OBJ_SPRITE_2D);
		ObjPrim_SetTexture(obj,GetCurrentScriptDirectory ~ "img/globe.png");
		ObjSprite2D_SetSourceRect(obj,0,0,248,248);
		Obj_SetRenderPriority(obj,0.32);
		let rad = 124;
		let x = GetStgFrameWidth+rad;
		let y = GetStgFrameHeight-rad*0.85;
		ObjRender_SetPosition(obj,x,y,0);
		ObjSprite2D_SetDestRect(obj,0,0,1,1);
		ObjRender_SetScaleXYZ(obj,rad*2,rad*2,0);
		let speed = 5;
		let angle = 0;
		let s = false;
		PlaySound("se_Shake",100);
		while(x > -rad)
			{
			x-=speed;
			angle-=speed;
			ObjRender_SetPosition(obj,x,y,0);
			ObjRender_SetAngleZ(obj,angle);
			if((((GetPlayerX-x)^2)+((GetPlayerY-y)^2))^0.5 <= rad)
				{
				let spam = CreateShotA1(GetPlayerX,GetPlayerY,0,0,BUBBLE_RED,0);
				ObjShot_SetSpellResist(spam,true);
				ObjShot_SetAutoDelete(spam,false);
				Obj_SetVisible(spam,false);
				Obj_DelayDelete(spam,2);
				s = true;
				}
			yield;
			if(s == true)
				{
				SetPlayerClip(999,999,999,999);
				}
			}
		Obj_Delete(obj);
		}
	loop(120){yield;}

	bubble2 = TBubble(aX,aY,[1,-1]);
	FDialogue(aX,aY,"Yeah.",0,2);
	FDialogue(aX,aY,"There's_ no_                        punchline.",0,2);
	DeleteBubble(bubble2);
	
	ObjMove_SetDestAtWeight(obj,-aX,aY,30,5);
	angleZ = 0;
	while(ObjMove_GetSpeed(obj) > 0)
		{
		angleZ+=(-720-angleZ)/25;
		ObjRender_SetAngleZ(obj,angleZ);
		yield;
		}
	loop(30){yield;}
	aX = 96;
	aY = GetStgFrameHeight*0.5-30;
	bubble2 = TBubble(aX,aY,[1,-1]);
	FDialogue(aX,aY,"Merry_ Christmas,_                I_ guess.",0,2);
	DeleteBubble(bubble2);

	loop(120){yield;}
	ObjEnemy_SetLife(objEnemy,0);
	Obj_Delete(objEnemy);
	yield;
	CloseScript(GetOwnScriptID);
}	
