
#ScriptVersion[3]
#Title["YaorochiS4"]
#Text[""]
#Player[DEFAULT]
#System["./system/Default_System.txt"]
#Background["./bg-klong.txt"]
#include"./Functions/Index.txt"
#include"./SetBoss.txt"
#include"./Shot/JINXShot_Const.txt"
#include"./system/Default_Effect.Txt"

let objBoss;
//let objBossIyo;
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 dir=GetCurrentScriptDirectory();
let obj=ObjEnemyBossScene_Create();
let color = [276,277,278];
let color2 = [51,53,55];
let dcolor = [308,309,310];
@Initialize
{
SetBoss("Doremy",true);
//SetBossIyo("Doremy",true);
GetCommonData("SPback",0);
SetCommonData("LenenSpellcut",[255,255,255]);
SetCommonData("SpellBarcl",[255,255,255]);
SetCommonData("Boss",0);
SetCommonData("CutChar",GCSD ~ "./Functions/booss/senri_09.png");
mainTask;
}

@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(2500);
	}
	case(EV_REQUEST_TIMER)
	{
		SetScriptResult(52);
	}
		case(EV_REQUEST_SPELL_SCORE)
	{
		SetScriptResult(500000);
	}
}

//----------------------------------------------------
task mainTask{
	//SetCommonData("FORCE_HITBOX",true);
	StartCountDown;
	PlaySound("SpellStart",100,0);
	ObjMove_SetDestAtWeight(objBoss,GetStgFrameWidth/2,80,10,20);
	wait( 80 );
	SetCommonData("ForceLifebar",0);
	Cutin("\"Kusanagi Galatine\"",20,"SHINKI");
	ObjEnemyBossScene_StartSpell(objScene);
	wait( 210 );
	TShot1;
	DamageRateAtFrame(16,50,0);
	}
	
//----------------------------------------------------
task TShot1
{
	ScreenShake2( 0, 125, 2 );
	PatternBrush;
}

	task sun1{ 
		let way = 22;
		let angle = GetAngleToPlayer( objBoss ) + 360/way/2;
		let speed = 2.5;
		loop( 12 ){
			let shote = 89;
			let delay = 0;
			//let speed = 0.5;
			let sx = ObjMove_GetX( objBoss );
			let sy = ObjMove_GetY( objBoss );

			loop( way ){
				
				let sht = TShotA2( sx, sy, speed, angle, speed/50, 10, shote, delay );
				//suwako( sht, 0.3, 300 );
				eff_scale02( sht );
				eff_alpha02( sht );
				//ObjShot_SetSpellResist( sht, true );
				//ObjShot_SetAutoDelete( sht, false );

				angle += 360 / way;
			}
			speed -= 0.15;
			//PlaySound("LenenShot1",92,0);
			PlaySound("BulletWave",92,0);
			wait( 5 );
		}
	}
	
	task eff_scale02( obj ){
		descent( i in 4..6*4 ){ 
			ObjRender_SetScaleXYZ( obj, i/4, i/4, 0 );
			yield;
		}
		ObjRender_SetScaleXYZ( obj, 1, 1, 0 );
	}
	
	task eff_alpha02( obj ){
		ascent( i in 0..255/10 ){
			ObjRender_SetAlpha( obj, i*10 );
			yield;
		}
		ObjRender_SetAlpha( obj, 255 );
	}
	
	task suwako( obj, ang, t ){
		ObjMove_SetAngularVelocity( obj, ang );
		wait( t );
		ObjMove_SetAngularVelocity( obj, 0 );
	}
	
	task GetP( objt, obj ){ 
	while( !Obj_IsDeleted( objt )){
	ObjRender_SetPosition( obj, ObjMove_GetX( objt ), ObjMove_GetY( objt ), 0);
	yield;
	}
	}

	task DelaySound( t ){
	wait( t );
	PlaySound("Laser7",100,0);
	}

	task DelaySound2( t ){
	wait( t );
	PlaySound("preblast",100,0);
	}

task DelayRotation( obj, time, target, rstart, rmax, angle, ad, av, aad, aav, avmax ){
wait( time - 25 );
ObjectRotation3( obj, target, rstart, rmax, angle, ad, av, aad, aav, avmax );
}

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

   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; }

      if( av < avmax ){
      angle += av;
      av += aav;
}

      if( av > avmax ){
      angle += avmax;
      }

      yield;

      
 
   }
}

task ClosingCurtain( obj, t, width ){
wait( t );
ObjShot_SetIntersectionEnable( obj, false );
descent(i in 0..width/10){
ObjLaser_SetRenderWidth( obj, i*10 );
yield;
}
Obj_Delete( obj );
}

task OpeningCurtain( obj, t, length ){
wait( t );
ascent(i in 0..length/6){
ObjLaser_SetLength( obj, i*6 );
yield;
}
}

task ScreenShake2( t, time, size ){
wait( t );

        loop( time/2 ){

                Set2DCameraFocusX( Get2DCameraX + rand( -size, size ));
                Set2DCameraFocusY( Get2DCameraY + rand( -size, size ));

                yield;
                Reset2DCamera;
                yield;

        }
        Reset2DCamera;
}

              task PatternBrush{ 
			  let c = 0;
              while( !Obj_IsDeleted( objBoss )){

			ScreenShake2( 125, 280, 4 );
			//ScreenShake2( 125 + 481, 120, 1 );
			DelaySound( 125 + 10 );
			DelaySound2( 65 + 10 );
              			let angle = [ GetAngleToPlayer( objBoss ) + 135, GetAngleToPlayer( objBoss ) - 135 ];
			let aaaaa = angle[ c%length( angle )];//angle[ round( rand( 0, 1 ))];
			let rotate = [ 1, 1 ];
			let rmax = [ -1.5, 1.5 ];
			let plus = [ -100, 100 ];
			let s = round( rand( 0, 1 ));
	                                	let way = 1;
                                                	let shote = 169;
			let delay = 135;

			loop( way ){

                      	                	let sx = ObjMove_GetX( objBoss );
                        		let sy = ObjMove_GetY( objBoss );
				
				let a = CreateStraightLaserA1( sx, sy, aaaaa, 800, 120, 550, shote, delay );
				setalpha( a, 130 );
				ObjShot_SetIntersectionEnable( a, false );
				ObjLaser_SetIntersectionWidth( a, 0 );
				GetPP( objBoss, a );
				PatternBrushSub( a, 135 );
				PatternTrail( a, plus[ c%length( plus ) ] );
				Obj_SetRenderPriorityI( a, 60 );
				DelayRotation( a, 160, objBoss, - 25, 0, aaaaa, 0, 0, 0, rotate[ c%length( rotate ) ], rmax[ c%length( rmax )] );
				OpeningCurtain( a, 0, 800 );
				ClosingCurtain( a, 350 + 10, 120 );
				//PatternTrailStar( a );

				let b = CreateStraightLaserA1( sx, sy, angle, 80, 10, 500, shote, delay );
				GetPP( objBoss, b );
				ObjRender_SetAlpha( b, 0 );
				DelayRotation( b, 160, objBoss, 0, 0, aaaaa, 0, 0, 0, rotate[ c%length( rotate ) ], rmax[ c%length( rmax )] );
				ClosingCurtain( b, 510, 10 );

			}
			c++;
			PlaySound("LenenLaserDelayLong",100,0);
                        		wait( 160 );
								let distance = GetDistance( GetPlayerX, GetPlayerY, ObjMove_GetX( objBoss ), ObjMove_GetY( objBoss ))/4;
								let angler = GetDotAngle( ObjMove_GetX( objBoss ), ObjMove_GetY( objBoss ), GetPlayerX, GetPlayerY );
			ObjMove_SetDestAtWeight( objBoss, GetPlayerX + distance*cos( angler ), GetPlayerY + distance*sin( angler ), 70, 2 );
			wait( 300 );
			sun1;
			wait( 150 );
	}
   }
   
	task setalpha( a, t ){
		ObjRender_SetAlpha( a, 255 );
		wait( t );
		ObjRender_SetAlpha( a, 0 );
	}

	task GetPP( objt, obj ){ 
	while( !Obj_IsDeleted( objt )){
	ObjMove_SetPosition( obj, ObjMove_GetX( objt ), ObjMove_GetY( objt ));
	yield;
	}
	}

              task PatternBrushSub( obj, delayed ){ 
              wait( delayed );
              while( !Obj_IsDeleted( obj )){

              			let angle = ObjStLaser_GetAngle(obj);
	                                	let way = 1;
                                                	let shote = 169;
			let delay = 0;

			loop( way ){

                      	                	let sx = ObjMove_GetX( objBoss );
                        		let sy = ObjMove_GetY( objBoss );
				
				let a = CreateStraightLaserA1( sx, sy, angle, 800, 120, 70, shote, delay );
				ObjRender_SetAlpha( a, 255/50 );
				ObjShot_SetIntersectionEnable( a, false );
				DelayRotation( a, 0, objBoss, -25, 0, angle, 0, 0, 0, 0.002, 1 );
				Obj_SetRenderPriorityI( a, 60 );
				ClosingCurtain( a, 120, 120 );

				angle += 360 / way;

			}
			PlaySound("LenenShot1",100,0);
                        		if( wait( 1 ) ){ return; }
	}
   }

	task PatternTrail( obj, plus ){ 
	wait( 135 );
	let t = 2;
		let a = rand( 0, 15 );
	while( !Obj_IsDeleted( obj )){
	let r = a;
                    let shote = 17;	
	loop( 12 ){
	                                	let way = 1;			
			let angle = ObjStLaser_GetAngle( obj );
			let x = ObjMove_GetX( objBoss );
			let y = ObjMove_GetY( objBoss );
                        		let sx = x + r*cos( angle );
                        		let sy = y + r*sin( angle );
			loop( way ){
				//plus = rand( -60, 60 );
				
				let sht1 = TShotA2( sx, sy, 0, angle, 0, 0, shote, 90 );
				ObjMove_AddPatternA2( sht1, rand( 140, 400 ), 0, angle - plus, 0.005, 0, 6 );
				ObjShot_SetSpellResist( sht1, true );

				angle += 360 / way;

			}
			//t += 0.1;
			r += 52;
			PlaySound("LenenShot3",92,0);
			}
			r += 1;
			wait( 6 );
			}
   }

	task PatternTrailStar( obj ){ 
	wait( 135 );
	while( !Obj_IsDeleted( obj )){
	let r = 0;
	loop( 5 ){

                    		let shote = 89;	
	                                	let way = 1;			
			let x = ObjMove_GetX( objBoss );
			let y = ObjMove_GetY( objBoss );
			let angla = ObjStLaser_GetAngle( obj );
                        		let sx = x + r*cos( angla );
                        		let sy = y + r*sin( angla );
			let angle = GetDotAngle( ObjMove_GetX( objBoss ), ObjMove_GetY( objBoss ), sx, sy );
			let speed = 4;
			loop( way ){
				
				let sht1 = TShotA2( sx, sy, 0, angle, 0, 0, shote, 60 );
				ObjMove_AddPatternA2( sht1, 240, 0, angle, 0.007, 0, speed );
				ObjShot_SetSpellResist( sht1, true );

				angle += 360 / way;

			}
			r += 30;
			}
			PlaySound("LenenShot3",92,0);
			wait( 1 );
			}
   }


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

	task StartCountDown
	{
		wait((52*60) - (10*60));
		loop(10)
		{
			PlaySound("time",100,0);
			wait(60);
		}

	}

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

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