
#ScriptVersion[3]
#Title["YaorochiS1"]
#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);
SetCommonData("Bomb",1);
GetCommonData("SPback",0);
SetCommonData("LenenSpellcut",[70,100,70]);
SetCommonData("SpellBarcl",[70,100,70]);
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);
	/*ObjEnemy_SetIntersectionCircleToPlayer(objBossIyo,ObjMove_GetX(objBossIyo),ObjMove_GetY(objBossIyo),18);
	ObjEnemy_SetIntersectionCircleToShot(objBossIyo,ObjMove_GetX(objBossIyo),ObjMove_GetY(objBossIyo),26);*/
	yield;

}

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE)
	{
		SetScriptResult(600);
	}
	case(EV_REQUEST_TIMER)
	{
		SetScriptResult(46);
	}
		case(EV_REQUEST_SPELL_SCORE)
	{
		SetScriptResult(500000);
	}
}

//----------------------------------------------------
task mainTask{
	//SetCommonData("FORCE_HITBOX",true);
	StartCountDown;
	PlaySound("SpellStart",100,0);
	ObjMove_SetDestAtWeight(objBoss,GetStgFrameWidth/2,+GetStgFrameHeight/2-150,20,20);
	Cutin("Hopeless Sign \"Childhood's End\"",20,"SHINKI");
	ObjEnemyBossScene_StartSpell(objScene);
	wait(210);
	//Cutin2("'sample text'",15,"SHINKI",202);
	//PlaySound("SpellStart",50,0);
	//wait(220);
	DamageRateAtFrame(5.5,50,0);
	TShot1;
	}
	
//----------------------------------------------------
let t = 0;
let t2 = 0;

task TShot1
{
	OrbSnake;
	TrapOperate;
	wait( 60 );
	sold1;
	wait( 300 );
	Snake;
}

	task OrbSnake{ 
		while( !Obj_IsDeleted( objBoss )){
			let way = 1;
			let shote = 129;
			let delay = 0;
			let angle = rand( 180, 360 );

			loop( way ){
		
				let ux = rand( 1, 2 )*cos( angle );
				let uy = -rand( 1, 2 )*sin( angle );

				let sx = ObjMove_GetX( objBoss ) + 100*cos( angle );
				let sy = ObjMove_GetY( objBoss ) + 100*sin( angle );
				
				let sht = TShotA2( sx, sy, rand( 6, 10 ), angle, -0.5, 1, shote, delay );
				//ObjRender_SetColor( sht, 0, 0, 0 );
				eff_scale02( sht );
				eff_alpha02( sht );
				ObjShot_SetSpellResist( sht, true );
				ObjMove_AddPatternB2( sht, 0 , ux, uy, 0, rand( -0.01, -0.015 ), 9, -9 );

				angle += rand( -360, 360 ) / way;
			}
			PlaySound("LenenShot3",92,0);
			wait( 4 );
		}
	}
	
	task Snake{
		while( !Obj_IsDeleted( objBoss )){
			PatternSnake;
			wait( rand( 150, 200 ) );
		}
	}

    task PatternSnake{ 
        let sx = rand( 10, GetStgFrameWidth - 10 );
        let sy = -5;
        let angle = GetDotAngle( GetPlayerX, GetPlayerY, sx, sy );
		let a = round( rand( 0, 1 ));
		let select = [ 55, -55 ];
        loop( 15 ){
		
            let speed = 3.5;
	        let way = 1;
            let shote = 113;
			let delay = 10;

			loop( way ){
				
				let sht1 = TShotA2( sx, sy, speed, angle, 0, speed, shote, delay );
				SpeedUn( sht1 );
				ObjShot_SetSpellResist( sht1, true );
				SnakeWave( sht1, angle, select[ a ], 10 );

				angle += 360 / way;
			
			}
			PlaySound("LenenShot1",92,0);
			wait( 6 );
	}
   }
   
	task SpeedUn( obj ){
		let a = 0;
		while( !Obj_IsDeleted( obj )){
			ObjMove_SetSpeed( obj, 3.5 + sin( a ) );
			a += 6;
			yield;
		}
	}

task SnakeWave2(obj,angle,amplitude,omega)
{
   let count = 0;
   while(!Obj_IsDeleted(obj))
   {
         ObjMove_SetAngle(obj,angle+amplitude*sin(count*omega));
         count += 1;
         wait( 2 );
   }
}

	task sold1{ 
		while( !Obj_IsDeleted( objBoss )){
		let angle = 0;
		let shote = 49;
	    let way = 1;
		let delay = 10;

		loop( way ){

			let speed = 0;
		
			let sx1 = 0;
            let sy1 = rand( 0, 448 );
				
			let sht1 = TShotA2( sx1, sy1, 0, angle, 0, speed, shote, delay );
			//ObjRender_SetColor( sht1, 0, 0, 0 );
			//ObjShot_SetAutoDelete( sht1, false );
			//eff_scale02( sht1 );
			//eff_alpha02( sht1 );
			ObjMove_AddPatternA2( sht1, 0, NO_CHANGE, NO_CHANGE, 0.02, rand( -0.3, -0.4 ), 10 );
			slashb( sht1 );
			
			speed = 0;
			
			let sx2 = 384;
            let sy2 = rand( 0, 448 );
				
			let sht2 = TShotA2( sx2, sy2, speed, angle + 180, 0, speed, shote, delay );
			//ObjRender_SetColor( sht2, 0, 0, 0 );
			//ObjShot_SetAutoDelete( sht2, false );
			//eff_scale02( sht2 );
			//eff_alpha02( sht2 );
			ObjMove_AddPatternA2( sht2, 0, NO_CHANGE, NO_CHANGE, 0.02, rand( 0.3, 0.4 ), 10 );
			slashb( sht2 );
			
			angle += 360 / way;

			}
			PlaySound("LenenShot3",92,0);
			wait( 3 );
		}
	}
	
	task slugr( obj, a ){ 
		if( a > 0 ){
		
			let way = 1;
			let shote = 500;
			let delay = 10;
			let angle = rand( 180, 360 );

			loop( way ){
		
				let ux = rand( 1, 2 )*cos( angle );
				let uy = rand( 1, 2 )*sin( angle );

				let sx = ObjMove_GetX( obj );
				let sy = ObjMove_GetY( obj );
				
				let sht = TShotA2( sx, sy, rand( 6, 10 ), angle, -0.5, 1, shote, delay );
				//ObjRender_SetColor( sht, 0, 0, 0 );
				//ObjRender_SetBlendType( sht, BLEND_MULTIPLY );
				ObjShot_SetSpellResist( sht, true );
				ObjMove_AddPatternB2( sht, 0 , ux, uy, 0, rand( 0.01, 0.015 ), 9, 9 );

				angle += rand( -360, 360 ) / way;
			}
			PlaySound("LenenShot1",92,0);
		}
	}
	
	task CreateSlash(s_x, s_y, s_angle, s_len, s_wid, s_graphic){
	let objLaser = CreateStraightLaserA1(0, 0, s_angle, 0, 0, 99999, s_graphic, 0);
	
	ObjShot_SetAutoDelete(objLaser, false);
	ObjShot_SetSpellResist(objLaser, true);
	
	ObjStLaser_SetSource(objLaser, false);
	
	let s_timer = 30;	//Starting phase duration (adjustable)
	let e_timer = 20;	//Ending phase duration (adjustable)
	
	let c = cos(s_angle);
	let s = sin(s_angle);
	
	ascent(i in 0..s_timer){
		let tmp = sin((i+1)/s_timer*90);
		
		Update(2.5 - tmp * 1.5, tmp);
	}
	descent(i in 0..e_timer){
		let tmp = (i + 1) / e_timer;
		
		Update(1, tmp);
	}
	
	Obj_Delete(objLaser);
	
	function Update(rl, rw){
		let tmp_l = rl * s_len;
		let tmp_w = rw * s_wid;
		
		let inv_l = max(0, rl - 1) * 50 + 10;
		
		ObjLaser_SetInvalidLength(objLaser, inv_l, inv_l);
		
		ObjMove_SetPosition(objLaser, s_x - c * tmp_l / 2, s_y - s * tmp_l / 2);
		
		ObjLaser_SetLength(objLaser, tmp_l);
		ObjLaser_SetRenderWidth(objLaser, tmp_w);
		ObjLaser_SetIntersectionWidth(objLaser, tmp_w * 0.8);
	
		yield;
	}
}

	
	task slashb( obj ){
		let a = 0;
		while( GetDistance( ObjMove_GetX( objBoss ), ObjMove_GetY( objBoss ), ObjMove_GetX( obj ), ObjMove_GetY( obj )) > GetCommonData( "radi", 0 ) ){ a += 1; yield; }
		CreateSlash( ObjMove_GetX( obj ), ObjMove_GetY( obj ), rand( 0, 360 ), 110, 9, 49 );
		slugr( obj, a );
		eff_scale01( obj );
		eff_alpha01( obj );
	}
	
	
	task eff_scale02( obj ){
		descent( i in 4..3*4 ){ 
			ObjRender_SetScaleXYZ( obj, i/4, i/4, 0 );
			yield;
		}
	}
	
	task eff_alpha02( obj ){
		ascent( i in 0..255/25 ){
			ObjRender_SetAlpha( obj, i*25 );
			yield;
			}
		//Obj_Delete( obj );
	}
	
	task eff_scale01( obj ){
	ascent( i in 10..3*10 ){
		if( Obj_IsDeleted( obj )){ break; }
		ObjRender_SetScaleXYZ( obj, i/10, i/10, 0 );
		yield;
		}
	}
	
	task eff_alpha01( obj ){
	descent( i in 0..255/25 ){
		ObjRender_SetAlpha( obj, i*25 );
		yield;
		}
	Obj_Delete( obj );
	}

	task TrapOperate{
		conrad;
		PlaySound("LenenLaserDelayLong",92,0);
		let barrier = MakeBarrierEffect_Ring( objBoss, 80, 200, 140 );
		ObjBarrier_SetInnerRadius( barrier, 0 );
		setrad( barrier );
		wait( 80 );
		Trap;
	}
	task setrad( barrier ){
		while( !Obj_IsDeleted( objBoss )){
			ObjBarrier_SetOuterRadius( barrier, GetCommonData( "radi", 0 ) );
			yield;
		}
	}
	
	task conrad{
		SetCommonData( "radi", 330 );
		wait( 80 );
		let a = 0.25;
		let b = 0;
		while( GetCommonData( "radi", 0 ) >= 180 ){
			SetCommonData( "radi", 330 - b );
			b += a;
			a -= 0.000208;
			yield;
		}
		SetCommonData( "radi", 180 );
		a = 0;
		while( !Obj_IsDeleted( objBoss )){
			SetCommonData( "radi", 180 + sin( a )*2 );
			a += 3;
			yield;
		}
	}
	
	task Trap{ 
        while( !Obj_IsDeleted( objBoss )){
			while( GetDistance( GetPlayerX, GetPlayerY, ObjMove_GetX( objBoss ), ObjMove_GetY( objBoss )) > GetCommonData( "radi", 0 ) ){ yield; }
			while( GetDistance( GetPlayerX, GetPlayerY, ObjMove_GetX( objBoss ), ObjMove_GetY( objBoss )) < GetCommonData( "radi", 0 ) ){

				let shote = 49;
				let way = 1;
				let delay = 5;
				let angle = rand( 0, 360 );
				let angle2 = angle + 90 + rand( -60, 60 );

				loop( way ){

					let r = -500;
					let r2 = -150;
					let sx = GetPlayerX + r*cos( angle );
					let sy = GetPlayerY + r*sin( angle );
					let sx2 = GetPlayerX + r2*cos( angle2 );
					let sy2 = GetPlayerY + r2*sin( angle2 );
				
					ProperLaser( sx, sy, angle, 1000, 20, 20, shote );
					ProperLaser( sx2, sy2, angle2, 300, 15, 20, shote );

					angle += 360 / way;

				}
				PlaySound("trap",92,0);
				wait( 30 );
			}
			yield;
		}
	}
	
	task HLaser( sx, sy, angle, length, width, duration, shote, delay ){
		ProperLaserDelay( sx, sy, angle, length, delay, shote );
		wait( delay );
		PlaySound("LenenLaserShort",92,0);
		ProperLaser( sx, sy, angle, length, width, duration, shote );
	}

	task ProperLaserDelay( sx, sy, angle, length, delay, shote ){
		let sht0 = CreateStraightLaserA1( sx, sy, angle, length, 4, delay, shote, 0 );
		Opening2( sht0, length );
		ObjShot_SetIntersectionEnable( sht0, false );
		wait( delay );
		Obj_Delete( sht0 );
	}

	task Opening2( obj, length ){
		ascent( i in 0..length/20 ){
			ObjLaser_SetLength( obj, i*20 );
			yield;
		}
	}

	task ProperLaser( sx, sy, angle, length, width, duration, shote ){
		let sht1 = CreateStraightLaserA1( sx, sy, angle, length, width, duration + 120, shote, 0 );
		//ObjRender_SetBlendType( sht1, BLEND_INV_DESTRGB );
		ObjLaser_SetGrazeInvalidFrame( sht1, 5 );
		wait( duration );

		ObjShot_SetIntersectionEnable( sht1, false );
		descent( i in 0..width/3 ){
			ObjLaser_SetRenderWidth( sht1, i*3 );
			wait( 2 );
		}
		Obj_Delete( sht1 );
	}
	   
   	function GetDotAngle( x1, y1, x2, y2 ){
		let angle=atan2( y1 - y2, x1 - x2 );
		return angle;
	}

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

	}

task SnakeWave(obj,angle,amplitude,omega)
{
   let count = 0;
   while(!Obj_IsDeleted(obj))
   {
         ObjMove_SetAngle(obj,angle+amplitude*sin(count*omega));
         count += 1;
         wait( 2 );
   }
}
	
	task Rotate( obj, ang, t ){
	ObjMove_SetAngularVelocity( obj, ang );
	wait( t );
	ObjMove_SetAngularVelocity( obj, 0 );
	}
	
	task RotateMT( obj, ang, ang2, t ){
	ObjMove_SetAngularVelocity( obj, ang );
	wait( t );
	ObjMove_SetAngularVelocity( obj, ang2 );
	}
	
//----------------------------------------------------

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