#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["YukariS5"]
#Text["Fantasy 'Perpetual Motion Machine of the First Kind', Yukari's IaMP Spell Card."]
#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(80);
	}
		case(EV_REQUEST_SPELL_SCORE)
	{
		SetScriptResult(50000000);
	}
}

//----------------------------------------------------
task mainTask{
	StartCountDown;
	//ObjPlayer_ClearIntersection(GetPlayerObjectID);
	YukariCircle(objBoss,1,-1);
	ObjMove_SetDestAtWeight(objBoss,stgx,stgy-100,10,7);
	Cutin("Fantasy 'Perpetual Motion Machine of the First Kind'",12,"SHINKI");
	ObjEnemyBossScene_StartSpell(objScene);
	Sound(GCSD ~ "Butterfly.mp3");
	wait(105);
	TShot1;
	wait(1200);
	}
//----------------------------------------------------

task TShot1
{
	attack( 5000 );
	let r = 70;
	let angle = rand(0,80);
	let dir = 0;
	let s = 1;
	maple;
	wait( 80 );
	PatternMachine;
	wait( 600 );
	DamageRateAtFrame(24,10,0);

}

              task PatternMachine{ 
              loop( 1 ){

              			let angle = rand( 0, 360 );
	                                	let way = 8;
                                                	let shote = 858;
			let delay = 0;
			let speed = 0;
			let sht1;

			loop( way ){

                      	                	let sx = ObjMove_GetX( objBoss );
                        		let sy = ObjMove_GetY( objBoss );
				
				sht1 = TShotA2( sx, sy, speed, angle, 0, speed, shote, delay );
				ObjShot_SetSpellResist( sht1, true );
				ObjShot_SetAutoDelete( sht1, false );

				PatternMachineSub1( sht1 );
				PatternMachineSub2( sht1 );
				PatternMachineSub3( sht1 );

				ObjectRotation( sht1, GetPlayerObjectID, 500, 130, angle, 0.6, 2.2, 0, 0 );

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

				angle += 360 / way;

			}

			PlaySound("LenenShot1",92,0);
                       		yield;


	}

   }

              task PatternMachineSub1( sht1 ){ 
              yield;
              while( !Obj_IsDeleted( sht1 ) ){

              			let angle = GetDotAngle( ObjMove_GetX( sht1 ), ObjMove_GetY( sht1 ), GetPlayerX, GetPlayerY );
	                                	let way = 1;
                                                	let shote = 810;
			let delay = 0;
			let speed = 4;

			loop( way ){

                      	                	let sx = ObjMove_GetX( sht1 );
                        		let sy = ObjMove_GetY( sht1 );
				
				let sht2 = TShotA2( sx, sy, speed, angle, 0, speed, shote, delay );

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

				angle += 360 / way;

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


	}

   }

              task PatternMachineSub2( sht1 ){ 
              wait( 600 );
              while( !Obj_IsDeleted( sht1 ) ){

              			let angle = rand( 0, 360 );
	                                	let way = 12;
                                                	let shote = 539;
			let delay = 30;

			loop( way ){

			let speed = rand( 0.3, 1 );
                      	                	let sx = ObjMove_GetX( sht1 );
                        		let sy = ObjMove_GetY( sht1 );
				
				let sht2 = TShotA2( sx, sy, speed, angle, 0, speed, shote, delay );

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

				angle += 360 / way;

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


	}

   }

              task PatternMachineSub3( sht1 ){ 
              wait( 1040 );
              while( !Obj_IsDeleted( sht1 ) ){

              			let angle = GetDotAngle( GetPlayerX, GetPlayerY, ObjMove_GetX( sht1 ), ObjMove_GetY( sht1 ) );
	                                	let way = 1;
                                                	let shote = 824;
			let delay = 30;
			let speed = 0.8;

			loop( way ){

                      	                	let sx = ObjMove_GetX( sht1 );
                        		let sy = ObjMove_GetY( sht1 );
				
				let sht2 = TShotA2( sx, sy, 0, angle, 0.007, speed, shote, delay );

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

				angle += 360 / way;

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


	}

   }


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

task ObjectRotation( obj, target, rstart, rmax, angle, ad, av, aad, aav ){

   let ox;
   let oy;
   let r = rstart;
   while(!Obj_IsDeleted(obj)){

      ox = ObjMove_GetX( target ) + r*cos( angle );
      oy = ObjMove_GetY( target ) + r*sin( angle );

      ObjMove_SetPosition(obj,ox,oy);
      ObjStLaser_SetAngle(obj,angle);

      if(r < rmax){
      r += ad;
      ad += aad; }

      if(r > rmax){
      r -= ad;
      ad += aad; }

      angle += av;
      av += aav;

      yield;
   }
}

	task StartCountDown
	{
		wait((80*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());
	}