#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["YukariS4"]
#Text["Fantasy Nest 'Flying Insect's Nest', Yukari's Spell Card which Satori copied and use in SA."]
#Player["./Hecatia/Hecatia.txt"]
#System["./system/Default_System.txt"]
#Background["./BG.txt"]
#include"./Functions/Index.txt"
#include"./SetBoss.txt"
#include"./Shot/AllStarShot_Const.dnh"
#include"./system/Default_Effect.Txt"


let objBoss;
let GCSD=GetCurrentScriptDirectory~"";
let enemy;
let bSpell=false;
let stgx=GetStgFrameWidth()/2;
let stgy=GetStgFrameHeight()/2;
let idle = true;
let atk = false;
let fog = 0;
let action = false;
let objScene=GetEnemyBossSceneObjectID();
let color = [276,277,278];
let color2 = [51,53,55];
let dcolor = [308,309,310];
@Initialize
{
SetBoss("Yukari",true);
}

@MainLoop
{
	ObjEnemy_SetIntersectionCircleToPlayer(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),18);
	ObjEnemy_SetIntersectionCircleToShot(objBoss,ObjMove_GetX(objBoss),ObjMove_GetY(objBoss),26);
	yield;

}

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
	{
		SetScriptResult(1250);
	}
	case(EV_REQUEST_TIMER)
	{
		SetScriptResult(90);
	}
		case(EV_REQUEST_SPELL_SCORE)
	{
		SetScriptResult(45000000);
	}
}

//----------------------------------------------------
task mainTask{
	StartCountDown;
	//ObjPlayer_ClearIntersection(GetPlayerObjectID);
	YukariCircle(objBoss,1,-1);
	ObjMove_SetDestAtWeight(objBoss,stgx,140,10,7);
	Cutin("Fantasy Nest 'Flying Insect's Nest'",15,"SHINKI");
	ObjEnemyBossScene_StartSpell(objScene);
	Sound(GCSD ~ "Butterfly.mp3");
	wait( 120 );
	TShot1;
	}
//----------------------------------------------------

task TShot1
{
	if( ObjEnemy_GetInfo( objBoss, INFO_LIFE ) == 1 ){ DeleteShotAll( TYPE_ALL, TYPE_IMMEDIATE ); }
	attack( 300 );
	let r = 70;
	let angle = rand(0,80);
	let dir = 0;
	let s = 1;
	maple;
	wait(80);
	PatternInsectNest;
	wait( 300 );
	PatternInsectNest2;
	DamageRateAtFrame(42,10,0);
}

              task PatternInsectNest2{
              PatternInsectSub;
              while( !Obj_IsDeleted( objBoss )){
              TMovement(40,10,1);
              wait( 270 );
	}
	}

              task PatternInsectNest{
              while( !Obj_IsDeleted( objBoss ) ){
	PatternInsect1;
	wait( 90 );
	PatternInsect2;
	wait( 90 );
	}
	}

              task PatternInsect1{ 
              let sx = 0;
              let a = 0;
              loop( 30 ){

              			let speedu = 10;
	                                	let way = 1;
                                                	let shote = 53;
              			let angle = 90;
			let delay = 0;
			let length = 160;
			let width = 18;
			let acc = -0.25;
			let speedv = 3;
			let shotdelay = 703;
			let shotf = 745;

			if( ObjMove_GetY( GetPlayerObjectID ) < ObjMove_GetY( objBoss ) ){ angle = 270; }

			loop( way ){

                        			let sy = ObjMove_GetY( objBoss ) + rand( -50, 10 );
				
				let sht0 = TShotA2( sx, sy, 0, 90, 0, 0, shotdelay, 0 );
				let sht2 = TShotA2( sx, sy, speedu, angle, acc, speedv, shotf, delay );
				let sht1 = CreateLooseLaserA1( sx, sy, speedu, angle, length, width, shote, delay );
				ObjMove_AddPatternA2( sht1, 0, speedu, angle, acc, 0, speedv );

				ObjLaser_SetGrazeInvalidFrame( sht1, 1 );
				ObjLaser_SetIntersectionWidth( sht1, 6 );

				ObjShot_SetSpellResist( sht0, true );
				ObjShot_SetSpellResist( sht2, true );

				DelayDeletes( sht0, 10 );

				NotifyEventAll( EV_USER + 40, [ sht1, delay ] );

				angle += 360 / way;

			}
			sx += a;
			a += rand( 1, 8 );
			PlaySound("Laser2",92,0);
                        		wait( 8 );

	}

   }

              task PatternInsect2{ 
              let sx = GetStgFrameWidth;
              let a = 0;
              loop( 30 ){

              			let speedu = 10;
	                                	let way = 1;
                                                	let shote = 53;
              			let angle = 90;
			let delay = 0;
			let length = 160;
			let width = 18;
			let acc = -0.25;
			let speedv = 3;
			let shotdelay = 703;
			let shotf = 745;

			if( ObjMove_GetY( GetPlayerObjectID ) < ObjMove_GetY( objBoss ) ){ angle = 270; }

			loop( way ){

                        			let sy = ObjMove_GetY( objBoss ) + rand( -50, 10 );
				
				let sht0 = TShotA2( sx, sy, 0, 90, 0, 0, shotdelay, 0 );
				let sht2 = TShotA2( sx, sy, speedu, angle, acc, speedv, shotf, delay );
				let sht1 = CreateLooseLaserA1( sx, sy, speedu, angle, length, width, shote, delay );
				ObjMove_AddPatternA2( sht1, 0, speedu, angle, acc, 0, speedv );

				ObjLaser_SetGrazeInvalidFrame( sht1, 4 );
				ObjLaser_SetIntersectionWidth( sht1, 6 );

				ObjShot_SetSpellResist( sht0, true );
				ObjShot_SetSpellResist( sht2, true );

				DelayDeletes( sht0, 10 );

				NotifyEventAll( EV_USER + 40, [ sht1, delay ] );

				angle += 360 / way;

			}
			sx -= a;
			a += rand( 1, 8 );
			PlaySound("Laser2",92,0);
                        		wait( 8 );

	}

   }

              task PatternInsectSub{ 
              while( !Obj_IsDeleted( objBoss )){
              loop( 6 ){

              			let speed = 1.1;
	                                	let way = 1;
                                                	let shote = 293;
              			let angle = GetAngleToPlayer( objBoss ) + 180;
			let delay = 20;

			loop( way ){

                      	                	let sx = ObjMove_GetX( objBoss );
                        		let sy = ObjMove_GetY( objBoss );
				
				let sht1 = TShotA2( sx, sy, speed, angle + 22.5*5, 0, speed, shote, delay );
				let sht2 = TShotA2( sx, sy, speed, angle + 22.5*3, 0, speed, shote, delay );
				let sht3 = TShotA2( sx, sy, speed, angle + 22.5*1, 0, speed, shote, delay );
				let sht4 = TShotA2( sx, sy, speed, angle - 22.5*1, 0, speed, shote, delay );
				let sht5 = TShotA2( sx, sy, speed, angle - 22.5*3, 0, speed, shote, delay );
				let sht6 = TShotA2( sx, sy, speed, angle - 22.5*5, 0, speed, shote, delay );

				ReflectAndAim( sht1, 0.03, 0, 4 );
				ReflectAndAim( sht2, 0.03, 0, 4 );
				ReflectAndAim( sht3, 0.03, 0, 4 );
				ReflectAndAim( sht4, 0.03, 0, 4 );
				ReflectAndAim( sht5, 0.03, 0, 4 );
				ReflectAndAim( sht6, 0.03, 0, 4 );

				NotifyEventAll( EV_USER + 40, [ sht1, delay ] );

				angle += 360 / way;

			}
			PlaySound("LenenShot1",92,0);
                       		wait( 15 );

	}

                        wait( 180 );

	}

   }

	task ReflectAndAim(obj, acceleration, angular, v )
{
while( !Obj_IsDeleted( obj ) && ObjMove_GetX( obj ) > 0 && ObjMove_GetX( obj ) < GetStgFrameWidth && ObjMove_GetY( obj ) > 0 ){ yield; }

   	if(ObjMove_GetX( obj )<0 || ObjMove_GetX( obj ) > GetStgFrameWidth )
   	{ ObjMove_AddPatternA2( obj, 0, NO_CHANGE, GetAngleToPlayer( obj ), acceleration, angular, v );}
   	if( ObjMove_GetY( obj ) < 0 )
   	{ ObjMove_AddPatternA2( obj, 0, NO_CHANGE, GetAngleToPlayer( obj ), acceleration, angular, v );}

	PlaySound("LenenBulletWave",70,0);

}

	task DelayDeletes( obj, t ){
	wait( t );
	Obj_Delete( obj );
	}

	function GetDotAngle(x1,y1,x2,y2){
	let angle=atan2(y1-y2,x1-x2);
	return angle;
	}

	task StartCountDown
	{
		wait((90*60) - (10*60));
		loop(5)
		{
			PlaySound("time1",100,0);
			wait(60);
		}
		loop(5)
		{
			PlaySound("time2",100,0);
			wait(60);
		}
	}


//----------------------------------------------------

task TEnd{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){yield;}
		EndBoss(1);
		yield;
		CloseScript(GetOwnScriptID());
	}