
#ScriptVersion[3]
#Title["YaorochiN2"]
#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",14);
mainTask;
//MusicPlay;
}

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

let musicplayer = false;
task MusicPlay
{
	loop
	{
		let music1 = ObjSound_Create;
		music1 = ObjSound_Create;
		ObjSound_Load(music1, GetCurrentScriptDirectory ~ "LenenBGM/BOSS_Namari.ogg");
		ObjSound_SetSoundDivision(music1, SOUND_BGM);
		ObjSound_SetRestartEnable(music1, true);
		ObjSound_SetLoopEnable(music1, true);	
		ObjSound_SetLoopSampleCount(music1,1499633,5631073);		
		ObjSound_Play(music1);
		
		ObjSound_SetVolumeRate(music1,50);
		while(!musicplayer)
		{
	
			yield;
		}
		Obj_Delete(music1);
		while(musicplayer)
		{
			yield;
		}
	}
}

//----------------------------------------------------
task mainTask{
	SetCommonData("ForceLifebar",1);
	StartCountDown;
	ObjMove_SetDestAtWeight(objBoss,GetStgFrameWidth/2 + 27/2,130 + 15/2,20,20);
	wait(180);
	SetCommonData("ForceLifebar",0);
	DamageRateAtFrame(28,50,0);
	TShot1;
	}
	
//----------------------------------------------------
task TShot1
{
	snake;
	TrapOperate;
}

	task TrapOperate{
		let barrier = MakeBarrierEffect_Ring( objBoss, 80, 200, 140 );
		ObjBarrier_SetOuterRadius( barrier, 120 );
		ObjBarrier_SetInnerRadius( barrier, 0 );
		wait( 80 );
		Trap;
	}
	
	    task Trap{ 
              while( !Obj_IsDeleted( objBoss )){
              while( GetDistance( GetPlayerX, GetPlayerY, ObjMove_GetX( objBoss ), ObjMove_GetY( objBoss )) > 130 ){ yield; }
              while( GetDistance( GetPlayerX, GetPlayerY, ObjMove_GetX( objBoss ), ObjMove_GetY( objBoss )) < 130 ){

			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 snake{
		//let angle = rand( 0, 360 );
		let sht = TShotA2( ObjMove_GetX( objBoss ) - 27, ObjMove_GetY( objBoss ) - 15, 0, 90, 0, 0, 1, 0 );
		ObjShot_SetSpellResist( sht, true );
		ObjShot_SetAutoDelete( sht, false );
		ObjShot_SetIntersectionEnable( sht, false );
		ObjRender_SetAlpha( sht, 0 );
		while( !Obj_IsDeleted( objBoss )){
			let angle = GetDotAngle( GetPlayerX, GetPlayerY, ObjMove_GetX( objBoss ) - 27, ObjMove_GetY( objBoss ) - 15 );;
			snake01( angle, sht );
			//angle += 40;
			wait( 80 );
		}
	}
	
	
	task sub_1{ 
		let angle = GetDotAngle( GetPlayerX, GetPlayerY, ObjMove_GetX( objBoss ) - 27, ObjMove_GetY( objBoss ) - 15 );
		while( !Obj_IsDeleted( objBoss )){
			let x = GetPlayerX;
			let y = GetPlayerY;
			sub_slash( x, y, angle );
			angle += 18;
			wait( 120 );
		}
	}	
	
		task sub_slash( x, y, angle ){ 
	        let way = 2;
            let shote = 145;
			let delay = 50;

			loop( way ){
				let r = -60;
				let sx = x + r*cos( angle );
				let sy = y + r*sin( angle );
				
				HLaser( sx, sy, angle, 900, 20, 40, shote, delay );

				angle += 360 / way;
			}
			PlaySound("LenenShot1",92,0);
		}
	
	task change1( obj ){
		while( ObjMove_GetSpeed( obj ) > 0 ){ yield; }
		PlaySound("BulletWave",92,0);
		ObjMove_AddPatternA2( obj, 0, 2.35, ObjMove_GetAngle( obj ) + 80, 0, 0, 2.35 );
	}
	
	task change2( obj ){
		while( ObjMove_GetSpeed( obj ) > 0 ){ yield; }
		wait( 20 );
		PlaySound("BulletWave",92,0);
		ObjMove_AddPatternA2( obj, 0, 2, ObjMove_GetAngle( obj ) - 120, 0, 0, 2 );
	}
		
	task suwako( obj, ang, time ){
		ObjMove_SetAngularVelocity( obj, ang );
		wait( time );
		ObjMove_SetAngularVelocity( obj, 0 );
	}
	
	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 snake01( angle, sht ){
		//let angle = rand( 0, 360 );
		let a = round( rand( 0, 1 ));
		let select = [ 55, -55 ];
		let diff = rand( -25, 25 );
		ascent( i in 0..16 ){
			let way = 1;
			let diff_ang = 40;
			let w_time = 65;
			loop( way ){
				PatternSnake( sht, angle + 90, select[ a ], 3, diff_ang + diff, w_time, i );
				PatternSnake( sht, angle + 30, select[ a ], 3, diff_ang + diff, w_time, i );
				PatternSnake( sht, angle - 30, select[ a ], 3, diff_ang + diff, w_time, i );
				PatternSnake( sht, angle - 90, select[ a ], 3, diff_ang + diff, w_time, i );
				angle += 360/way;
			}
			//angle += 0.5;
			wait( 4 );
		}
	}
				

    task PatternSnake( obj, angle, side, time, diffang, wtime, index ){ 
		if( time > 0 && !Obj_IsDeleted( obj ) ){
        loop( 1 ){
		
            let speed = 2;
	        let way = 1;
            let shote = 17;
			let delay = 0;
			let sx = ObjMove_GetX( obj );
			let sy = ObjMove_GetY( obj );

			loop( way ){
				
				let sht1 = TShotA2( sx, sy, speed, angle, 0, speed, shote, delay );
				del_split( sht1, angle, side, time, diffang, wtime, index );
				ObjRender_SetBlendType( sht1, BLEND_ADD_ARGB );
				//SpeedUn( sht1, speed );
				//ObjShot_SetSpellResist( sht1, true );
				SnakeWave( sht1, angle, side, 30 );

				angle += 360 / way;
			
			}
			PlaySound("LenenShot1",92,0);
			wait( 1 );
		}
	}
   }
   
	task del_split( obj, angle, side, time, diffang, wtime, index ){
		wait( wtime );
		time--;
		if( !Obj_IsDeleted( obj )){
			PatternSnake( obj, angle + diffang, side, time, diffang, wtime, index );
			PatternSnake( obj, angle - diffang, side, time, diffang, wtime, index );
		}
		if( time > 0 && !Obj_IsDeleted( obj ) ){
			if( index == 0 ){
				let g = 0;
				loop( 1 ){
					let sht1 = TShotA2( ObjMove_GetX( obj ), ObjMove_GetY( obj ), 0, GetDotAngle( ObjMove_GetX( obj ), ObjMove_GetY( obj ), ObjMove_GetX( objBoss ) - 27, ObjMove_GetY( objBoss ) - 15 ) + g, 0.005, 14, 17, 0 );
					eff_scale022( sht1 );
					eff_alpha022( sht1 );
					//ObjRender_SetBlendType( sht1, BLEND_ADD_ARGB );
					g += 0;
				}
			}
			Obj_Delete( obj );
		}
	}
	
	task eff_scale022( obj ){
		descent( i in 4..6*4 ){ 
			ObjRender_SetScaleXYZ( obj, i/4, i/4, 0 );
			yield;
		}
		ObjRender_SetScaleXYZ( obj, 1, 1, 0 );
	}
	
	task eff_alpha022( obj ){
		ascent( i in 0..255/10 ){
			ObjRender_SetAlpha( obj, i*10 );
			yield;
		}
		ObjRender_SetAlpha( obj, 255 );
	}
	
	function GetDotAngle( x1, y1, x2, y2 ){
		let angle=atan2( y1 - y2, x1 - x2 );
		return angle;
	}
	
	
	task SpeedUn( obj, bspeed ){
		let a = 0;
		while( !Obj_IsDeleted( obj )){
			ObjMove_SetSpeed( obj, bspeed + sin( a )/4 );
			a += 6;
			yield;
		}
	}
	
	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 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 );
	}



	task StartCountDown
	{
		wait((45*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 TEnd{
	while(ObjEnemy_GetInfo(objBoss,INFO_LIFE)>0){yield;}
		EndBoss(0);
		DeleteShotAll(TYPE_ALL,TYPE_FADE);
		yield;
		CloseScript(GetOwnScriptID());
	}