
#ScriptVersion[3]
#Title["YaorochiN1"]
#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(65);
	}
}

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{
	ObjRender_SetPosition(objBoss,-200,0,0);
	StartCountDown;
	SetCommonData("ForceLifebar",1);
	NotifyEventAll(EV_SET_SONG,1);
	MusicName(GCSD~"./musicname/Yao.png");
	wait(100);
	ObjMove_SetDestAtWeight(objBoss,GetStgFrameWidth/2,180,20,20);
	wait(130);
	SetCommonData("ForceLifebar",0);
	DamageRateAtFrame(26,50,0);
	TShot1;
	}
	
//----------------------------------------------------
let t = 0;
let t2 = 0;

	task TShot1{
	
		ObjMove_SetDestAtWeight(objBoss,GetStgFrameWidth/2,120,10,10);
		
		PlaySound("LenenLaserDelayLong",92,0);
		TrapOperate;
	
		while( !Obj_IsDeleted( objBoss )){
	
			loop( 2 ){
				let x = rand( 0 + 50, 384 - 50 );
				KnifeSlash( x, 0, GetDotAngle( GetPlayerX, GetPlayerY, x, 0 ));
				wait( 100 );
				loop( 5 ){
					let x = rand( 80, GetStgFrameWidth - 80 );
					let y = rand( 100, GetStgFrameHeight - 150 );
					let angle = rand( 0, 360 );
					sub_slash( x, y, angle );
					wait( 8 );
				}
				wait( 80 );
			}
			slash_02;
			wait( 330 );
		}
	}
	
	task TrapOperate{
		let barrier = MakeBarrierEffect_Ring( objBoss, 80, 200, 140 );
		ObjBarrier_SetOuterRadius( barrier, 130 );
		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 slash_02{
			let angle = rand( -10, 10 ) - 60;
			let r = 130;
			let way = 20;
			let lop = 2.1;
			let hind = 6;
			loop( way*lop ){
				sub_slash( ObjMove_GetX( objBoss ) + r*cos( angle ), ObjMove_GetY( objBoss ) + r*sin( angle ), (angle + 90) );
				hind_1( ObjMove_GetX( objBoss ) + r*cos( angle ), ObjMove_GetY( objBoss ) + r*sin( angle ) );
				angle += 16.7;
				wait( 6 );
			}
		}
	
        task hind_1( x, y ){ 
            wait( 50 );
            loop( 4 ){
				let speed = rand( 0.8, 2 );
				let angle = rand( 0, 360 );
				let shote = 17;
	            let way = 1;
				let delay = 0;

				loop( way ){

					let sx = x;
                    let sy = y;
				
					let sht1 = TShotA2( sx, sy, speed, angle, 0, speed, shote, delay );
					//ObjShot_SetAutoDelete( sht1, false );
					eff_scale02( sht1 );
					eff_alpha02( sht1 );
					//r_slash( sht1, 130 );
					//ObjMove_AddPatternA2( sht1, 0, NO_CHANGE, NO_CHANGE, 0.01, 0, 10 );

					angle += 360 / way;

				}
				PlaySound("LenenShot3",92,0);
				wait( 0 );
			}
		}
		
		task r_slash( obj, r ){
			while( GetDistance( ObjMove_GetX( obj ), ObjMove_GetY( obj ), ObjMove_GetX( objBoss ), ObjMove_GetY( objBoss ) ) >= r ){ yield; }
			eff_scale01( obj );
			eff_alpha01( 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 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 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 KnifeSlash( x, y, angle ){ 
				PlaySound("Boon1",92,0);
				let sx1;
				let sy1;
				let rad_weff = 60;
				loop( 10 ){
					let way = 1;
					let shote = 153;
					let delay = 0;
					let length = 1600;
					let width = 4;
					loop( way ){
						let r = -length/2;
						
              			sx1 = x + r*cos( angle ) + rad_weff*cos( angle + 90 );
              			sy1 = y + r*sin( angle ) + rad_weff*sin( angle + 90 );
						let sx2 = x + r*cos( angle ) + rad_weff*cos( angle - 90 );
              			let sy2 = y + r*sin( angle ) + rad_weff*sin( angle - 90 );
				
						let sht1 = CreateStraightLaserA1( sx1, sy1, angle, length, width, 100, shote, delay );
						slash_eff01( sht1 );
						ObjMove_AddPatternA2( sht1, 0, 0, angle + 90 + 180, 0.185, 0, 20 );
						ObjMove_AddPatternA2( sht1, 25, 0, NO_CHANGE, 0, 0, 0 );
						ObjShot_SetIntersectionEnable( sht1, false );
						ObjShot_SetAutoDelete( sht1, false );
						
						let sht2 = CreateStraightLaserA1( sx2, sy2, angle, length, width, 100, shote, delay );
						slash_eff01( sht2 );
						ObjMove_AddPatternA2( sht2, 0, 0, angle + 90, 0.185, 0, 20 );
						ObjMove_AddPatternA2( sht2, 25, 0, NO_CHANGE, 0, 0, 0 );
						ObjShot_SetIntersectionEnable( sht2, false );
						ObjShot_SetAutoDelete( sht2, false );

						angle += 360 / way;
				}
				wait( 6 );
			}
			realslash( sx1 - rad_weff*cos( angle + 90 ), sy1 - rad_weff*sin( angle + 90 ), angle );
		}
		
		task realslash( sx, sy, angle ){
			wait( 2 );
			delaysoundeff02;
			let uspeed = 40;
			let slash = CreateLooseLaserA1( sx, sy, uspeed, angle, 760, 25*1.8, 89, 0 );
			ObjShot_SetAutoDelete( slash, false );
			DelayDelete( slash, 120 );
			wait( 16 );
			let slash02 = TShotA2( sx, sy, uspeed, angle, 0, uspeed, 1, 0 );
			SlashSub( slash02, angle );
			ObjShot_SetAutoDelete( slash02, false );
			DelayDelete( slash02, 120 );
			ObjShot_SetSpellResist( slash02, true );
			ObjShot_SetIntersectionEnable( slash02, false );
			ObjRender_SetScaleXYZ( slash02, 5, 5, 0 );
			
			let len = 60;
			let slash3 = CreateStraightLaserA1( sx, sy, 180 + angle, 3500, len, 200, 145, 0 );
			close_eff( slash3, len );
			//ObjRender_SetBlendType( slash3, BLEND_ADD_ARGB );
			ObjShot_SetAutoDelete( slash3, false );
			ObjMove_AddPatternA2( slash3, 0, uspeed, angle, 0, 0, uspeed );
		}
		
		task delaysoundeff02{
		wait( 25 );
		PlaySound("Slash",92,0);
		}
		
	task close_eff( obj, len ){
		wait( 90 );
		ObjShot_SetIntersectionEnable( obj, false );
		descent( i in 0..len*(18/len)){
			ObjLaser_SetRenderWidth( obj, i/(18/len) );
			yield;
		}
		Obj_Delete( obj );
	}
		
	task slash_eff01( obj ){
		ascent( i in 0..255/9 ){
			ObjRender_SetAlpha( obj, i*9 );
			yield;
		}
	}
			
			
			task delayadd( obj ){
				wait( 60 );
				PlaySound("BulletWave",92,0);
				ObjMove_AddPatternA2( obj, 0, NO_CHANGE, NO_CHANGE, 0.05, 0, 20 );
			}
			
		task SlashSub( obj, w_angle ){ 
		let u_d = [ 0, 180 ];
		let select = round( rand( 0, 1 ));
		wait( 5 );
		let a = 4.2;
		let b = 3.8;
            while( !Obj_IsDeleted( obj )){
				let speed = absolute( rand( a, b ) );
				loop( 1 ){
	                let way = 1;
                    let shote = 49;
					let delay = 0;
					loop( way ){
					
              			let sx = ObjMove_GetX( obj );
              			let sy = ObjMove_GetY( obj );
				
						let w_r = 0;
						loop( 7 ){
							let angle = ObjMove_GetAngle( obj ) + rand( -90, 90 );
							let sht1 = TShotA2( sx + w_r*cos( w_angle ), sy + w_r*sin( w_angle ), speed, angle, 0, speed, shote, delay + w_r/20 );
							ObjShot_SetSpellResist( sht1, true );
							eff_scale02( sht1 );
							eff_alpha02( sht1 );
							w_r += 7.5;
						}

						//angle += 360 / way;
				}
			}
			a -= 0.13;
			b -= 0.13;
			select++;
			wait( 1 );
		}
		}
		
		task auto_dist( obj ){
			ObjShot_SetAutoDelete( obj, false );
			wait( 100 );
			ObjShot_SetAutoDelete( obj, true );
		}
		
	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 StartCountDown
	{
		wait((65*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());
	}