#TouhouDanmakufu[Single]
#ScriptVersion[3]

#include"./functions.txt"

let koi1;
let koi;
let ran; 

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

	koi1 = ObjEnemy_Create(OBJ_ENEMY);
	ObjEnemy_Regist(koi1);
	TXY;
	ObjEnemy_RenderKoi(koi1);
	ObjMove_SetPosition(koi1,enmx,enmy);
	ObjMove_SetPosition(objEnemy,-999,-999);
	
	TEnemyWork;
	TEnd;
}

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
		{
		SetScriptResult(500);
		}
	case(EV_REQUEST_TIMER)
		{
		SetScriptResult(74);
		}
	case(EV_REQUEST_SPELL_SCORE)
		{
		SetScriptResult(30000);
		}
	case(EV_START_BOSS_SPELL)
		{
		SetScriptResult(true);
		}
	case(EV_REQUEST_IS_DURABLE_SPELL)
		{
		SetScriptResult(true);
		}
		
}

task TEnemyWork
{
	ObjEnemy_SetDamageRate(objEnemy,0,0);
	
	yield;

	ObjMove_SetDestAtWeight(koi1,GetStgFrameWidth*0.5,GetStgFrameHeight*0.5-120,15,5);
	loop(45){yield;}
	ObjMove_SetDestAtSpeed(koi1,GetStgFrameWidth*0.5,-120,2.5);
	loop(30){yield;}
	PlaySound("se_Gap",85);
	loop(30){yield;}
	Obj_Delete(koi1);
	koi = ObjEnemy_Create(OBJ_ENEMY);
	ObjEnemy_Regist(koi);
	Obj_SetRenderPriority(koi,0.35);
	ObjMove_SetPosition(koi,GetStgFrameWidth*0.5,GetStgFrameHeight+32);
	ObjEnemy_RenderKoiPlayer(koi);
	ObjMove_SetDestAtSpeed(koi,GetStgFrameWidth*0.5,GetStgFrameHeight-24,2.5);

	TCaution(0);
	loop(45){yield;}
	ObjMove_SetPosition(objEnemy,GetStgFrameWidth*0.5,GetStgFrameHeight*0.5-155);
	TGrow(objEnemy,20);
	ObjEnemy_RenderYuk(objEnemy);

	loop(105){yield;}
	TSpellName("Yakumo Game \"Abandoned Meme Nest\"");
	ObjEnemyBossScene_StartSpell(GetEnemyBossSceneObjectID());
	loop(45){yield;}

	let ranS = false;
	let enmLoss = false;
	TRan;
	loop(65){yield;}
	PlaySound("se_DeepShotHigh",60);
	let speed = 1;
	let counter = -1;
	let xMA = 0;
	let dir = 0;
	while(ranS == false)
		{
		counter+=1;
		if(speed < 4.5)
			{
			speed+=0.025;
			}
		else
			{
			speed = 4.5;
			}
		ObjMove_SetDestAtWeight(objEnemy,ObjMove_GetX(ran),enmy,5,speed);
		ObjMove_SetDestAtWeight(koi,ObjMove_GetX(ran),ObjMove_GetY(koi),5,speed);
		ObjEnemy_SetIntersectionCircleToPlayer(koi,ObjMove_GetX(koi),ObjMove_GetY(koi),16);

		if(counter%30 == 0 && counter > 180)		
			{
			PlaySound("se_DeepShotHigh",30);
			CreateShotA1(enmx,enmy,4,GetAngleToPlayer(objEnemy),LARGE_PURPLE,8);
			}
		if(counter >= 600 && counter%15 == 0)
			{
			let xA = xMA+rand(-1,1)*17*(15/10)*0.35;
			let y = -rand(0,64);
			PlaySound("se_LaserShort",35);
			ascent(i in 0..2)
				{
				TLaser(xA,i,y);
				}
			xMA+=17*15/10;
			}
		if(dir == 0 && counter >= 1680 && counter%330 == 0)
			{
			if(GetPlayerX < GetScreenWidth*0.5)
				{
				dir=-1;
				}
			else
				{
				dir=1;
				}
			}
		if(counter >= 1680)
			{
			if(counter%330 == 285)
				{
				PlaySound("se_Train",70);
				}
			else if(counter%330 == 0)
				{
				let shot = CreateTrain(GetStgFrameWidth*(0.5-dir*0.5),GetPlayerY,4.5,dir,0.825);
				Obj_SetRenderPriority(shot,0.32);
				dir *= -1;
				}
			}
		yield;
		}
	loop(45){yield;}
	PlaySound("se_PlayerDie",85);
	Obj_Delete(koi);
	loop(90){yield;}
	FadeMusic(2,90);	
	
	task TLaser(xA,i,y)
		{
		let speed = 150/30;
		let shot = CreateLaserA2(GetStgFrameWidth*(0.5+0.5*cos(xA)),enmy+y,speed,90+i*180,150,32,10^10,OVAL_BLUE,0);
		ObjShot_SetSpellResist(shot,false);
		loop(30){yield;}
		ObjMove_SetSpeed(shot,speed);
		ObjMove_SetAngle(shot,90+i*180);
		ObjMove_SetAcceleration(shot,(1-speed)/60);
		ObjMove_SetMaxSpeed(shot,1);
		}

	task TRan
		{
		ran = ObjEnemy_Create(OBJ_ENEMY);
		ObjEnemy_Regist(ran);
		ObjMove_SetPosition(ran,enmx+96,enmy);
		ObjPrim_SetTexture(ran,GetCurrentScriptDirectory ~ "img/ran.png");
		Obj_SetRenderPriority(ran,0.37);
		
		let rx;
		let ry;
		let rxA = 1024/8;
		let ryA = 512/4;
		
		let counter = 0;	
		let angleZ = 0;
	
		let angle = rand(0,360);
		TGrow(ran,20);

		while(!Obj_IsDeleted(objEnemy))
			{
			counter+=1;		
			if(counter == 65)
				{
				ObjMove_SetAngle(ran,45);
				ObjMove_SetSpeed(ran,2.75);
				ObjMove_SetAcceleration(ran,(1.75-ObjMove_GetSpeed(ran))/60);
				ObjMove_SetMaxSpeed(ran,1.75);
				ObjMove_AddPatternA2(ran,60,NO_CHANGE,NO_CHANGE,1/750,0,100);
				}
			if(counter >= 125 && ranS == false)
				{
				let shot = CreateShotA1(ObjMove_GetX(ran),ObjMove_GetY(ran),ObjMove_GetSpeed(ran)/20,angle,BL_SMALL_BLUE,0);
				ObjRender_SetBlendType(shot,BLEND_ADD_ARGB);
				Obj_DelayDelete(shot,180/ObjMove_GetSpeed(ran));
				angle+=7*ObjMove_GetSpeed(ran);
				}

			if(ObjMove_GetY(ran) < 24 && enmLoss == false)
				{
				ObjMove_SetSpeed(ran,0);
				ObjMove_SetMaxSpeed(ran,0);
				TShrink(ran,15);
				PlaySound("se_Gap",85);
				loop(10){angleZ = 0;yield;}
				TGrow(ran,10);
				ranS = true;
				ObjMove_SetPosition(ran,GetStgFrameWidth-48,ObjMove_GetY(koi)-24);
				loop(30){angleZ = 0;yield;}
				ObjMove_SetSpeed(ran,10);
				ObjMove_SetMaxSpeed(ran,10);
				ObjMove_SetAngle(ran,90);
				}
			else if(ObjMove_GetY(ran) > GetStgFrameHeight+16 && enmLoss == false)
				{
				enmLoss = true;
				ObjMove_SetSpeed(ran,0);
				ObjMove_SetMaxSpeed(ran,0);
				ObjEnemy_SetGlow(ran,false);
				ObjMove_SetPosition(ran,-999,-999);
				}
			if(GetObjectDistance(ran,objEnemy) < 32 && ObjMove_GetY(ran) > enmy)
				{
				if(ranS == false)
					{
					PlaySound("se_CuteShotLong",70);
					}
				let angle = atan2(ObjMove_GetY(ran)-enmy,ObjMove_GetX(ran)-enmx);
			 	let angleA = 60*cos(ObjMove_GetAngle(ran)+cos(angleZ)*10);
				ObjMove_SetAngle(ran,-ObjMove_GetAngle(ran));
				ObjMove_SetPosition(ran,enmx+32*cos(angle),enmy+32*sin(angle));
				}
			else if(GetObjectDistance(ran,koi) < 32 && ObjMove_GetY(ran) < ObjMove_GetY(koi))
				{
				if(ranS == false)
					{
					PlaySound("se_CuteShotLong",70);
					}
				let angle = atan2(ObjMove_GetY(ran)-ObjMove_GetY(koi),ObjMove_GetX(ran)-ObjMove_GetX(koi));
				let angleA = 60*cos(ObjMove_GetAngle(ran)+cos(angleZ)*10); 
				ObjMove_SetAngle(ran,-ObjMove_GetAngle(ran));
				ObjMove_SetPosition(ran,ObjMove_GetX(koi)+32*cos(angle),ObjMove_GetY(koi)+32*sin(angle));
				}
			if(ObjMove_GetX(ran) > GetStgFrameWidth-16)
				{
				if(ranS == false)
					{
					PlaySound("se_CuteShotLong",70);
					}
				ObjMove_SetAngle(ran,180-ObjMove_GetAngle(ran));
				ObjMove_SetX(ran,GetStgFrameWidth-16);
				}	
			else if(ObjMove_GetX(ran) <16)
				{
				if(ranS == false)
					{
					PlaySound("se_CuteShotLong",70);
					}
				ObjMove_SetAngle(ran,180-ObjMove_GetAngle(ran));
				ObjMove_SetX(ran,16);
				}			


			if(counter >= 45)
				{
				rx = 2;
				ry = 2+min(1,floor((counter-45)/8));
				if(counter >= 65)
					{
					if(ObjRender_GetScaleX(ran) < 0)
						{
						angleZ-=1;
						}
					else
						{
						angleZ+=1;
						}
					ObjRender_SetAngleZ(ran,angleZ*2);
					}
				}
			else
				{
				rx = 0;
				ry = 0;
				}
			if(cos(ObjMove_GetAngle(ran)) < 0)
				{
				ObjRender_SetScaleX(ran,-absolute(ObjRender_GetScaleX(ran)));
				}
			else
				{
				ObjRender_SetScaleX(ran,absolute(ObjRender_GetScaleX(ran)));
				}
			ObjSprite2D_SetSourceRect(ran,rx*rxA,ry*ryA,(rx+1)*rxA,(ry+1)*ryA);
			ObjSprite2D_SetDestCenter(ran);
			if(ObjEnemy_GetGlow(ran) == true && counter%4 == 0)
				{
				TGlow;
				}
			ObjEnemy_SetIntersectionCircleToPlayer(ran,ObjMove_GetX(ran),ObjMove_GetY(ran),20);
			yield;	
			}
		task TGlow
			{
			let glw = ObjPrim_Create(OBJ_SPRITE_2D);
			ObjPrim_SetTexture(glw,GetCurrentScriptDirectory ~ "img/ran.png");
			ObjSprite2D_SetSourceRect(glw,rx*rxA,ry*ryA,(rx+1)*rxA,(ry+1)*ryA);
			ObjSprite2D_SetDestCenter(glw);
			ObjRender_SetPosition(glw,ObjMove_GetX(ran),ObjMove_GetY(ran),0);
			Obj_SetRenderPriority(glw,0.28);
			ObjRender_SetAngleZ(glw,ObjRender_GetAngleZ(ran));
			ObjRender_SetBlendType(glw,BLEND_ADD_ARGB);
			let time = 20;
			let xS = ObjRender_GetScaleX(ran);
			let yS = ObjRender_GetScaleY(ran);
			ascent(t in 0..time)
				{
				ObjRender_SetColorHSV(glw,210+t*30/time,255,255);
				ObjRender_SetAlpha(glw,(time-t)*ObjRender_GetAlpha(ran)/time);
				ObjRender_SetScaleXYZ(glw,(1+t*1.25/time)*xS,(1+t*1.25/time)*yS,0);
				if(Obj_IsDeleted(ran) || Obj_IsDeleted(objEnemy))
					{
					Obj_Delete(glw);
					}
				yield;
				}
			Obj_Delete(glw);
			}
		}

	task TGrow(obj,time)
		{
		ascent(i in 0..time)
			{
			ObjRender_SetScaleX(obj,(i+1)*1/time);
			yield;
			}
		}
	task TShrink(obj,time)
		{
		descent(i in 0..time)
			{
			ObjRender_SetScaleX(obj,(i+1)*1/time);
			yield;
			}
		}
}