#e
#Title[duԊ@̗Ƃv]
#Text[]
#ScriptVersion[2]
#BackGround[User()]

script_enemy_main {
	//ʏݒ
	let csd = GetCurrentScriptDirectory;
	let imgBoss	=csd ~ "img\dot_youmu.png";
	#include_function ".\lib\lib_anime_youmu.txt"	//Ɏw̃LCN[h
	#include_function ".\INCLUDE\BOSS_INI.txt"

	let name        = "duԊ@̗Ƃv";

	let wIni    = 100;	//Uڍs܂
	let r;

	let SlowFlag = false;    //X[Ă邩


	let arrayLaser = [];	//[U[

		let mX = GetClipMinX;
		let mY = GetClipMinY;
		let MX = GetClipMaxX;
		let MY = GetClipMaxY;
		let mMGapX = MX - mX;
		let mMGapY = MY - mY;

	@Initialize {
		SetMovePosition02(GetCenterX, GetClipMinY + 120, wIni/2);
		//SetX(GetCenterX);
		//SetY(GetClipMinY + 120);

		thMCutIn(1,name);
		SetLife(4000);		//Ct
		SetTimer(75);		//^C}[
		SetScore(4000000);	//{[iXXRA

		LoadGraphicthM;
		SetTexture(imgBoss);

		SetInvincibility(wIni);
		SetDamageRate(100, 50);

		InitializeAction();	//Aj[V
		TMain;
	}

	@MainLoop {
		SetCollisionA(GetX, GetY, 24);
		SetCollisionB(GetX, GetY, 15);

		yield; 
	}

	@DrawLoop {
		DrawBoss(imgBoss);


	}

	@Finalize {
		SpellClear;

		DeleteGraphic(imgBoss);
	}

	@BackGround {
		//bgDraw;
	}

//==========================================================================================

    // C^XNiʂ͂ȍ~ҏWĂj
	task TMain {
		yield;

		loop(wIni) { yield; }

		
		tShot01;
		//tShot02;
		//Idou;

		//ݒuĂnj
	}



	task tShot01{
		loop{
			ascent(i in 0..30){
				Shot01(GetX, GetY, 5, 90 + i*12, US_BALL_L_BLUE, 10, 2.5);
				Shot01(GetX, GetY, 5, 90 + i*12, US_BALL_L_BLUE, 10, -2.5);
			}
			wait(90);
			//MotionBlurEx(ALL, 90, 75, ADD);
			SlowTime(2, 90);
			Concentration01(60);
			SetAction(ACT_SHOT_A, 60);
			wait(30);
			let angle = GetAngleToPlayer;
			SlashLaser(GetX, GetY, 7, 10, angle+15, US_SCALE_WHITE, 500, 15);
			SlashLaser(GetX, GetY, 7, 10, angle-15, US_SCALE_WHITE, 500, 15);
			wait(30);
			SetAction(ACT_SLASH_V, 60);
			wait(70);
			BossMoveEX_Beta(100);
			SetAction(ACT_MOVE, 60);
			wait(50);
		}
	}




	task SlashLaser(x, y, ExSpeed, MoveSpeed, angle, type, _length, width){
		
		let obj = CreateObjLaser02(x, y, ExSpeed, MoveSpeed, angle, type, _length, width);
		arrayLaser = arrayLaser~[obj];
	}


	task Shot01(let x, let y, let speed, let angle, let color, let delay, rot){
		//e^XN

		let count = 0;

	
		let obj = CreateObjShot(x, y, speed, angle, color, delay);//eIuWFNg쐬

		//Obj_SetAlpha(obj, alpha);
		//Obj_SetCollisionToPlayer(obj, false);
		//ObjShot_SetBombResist(obj, true);
		Obj_SetAutoDelete(obj,false);
		wait(delay);


		while( !Obj_BeDeleted(obj) ){ //폜܂Ń[vs

			if(SlowFlag == true){ObjShot_SetGraphic(obj, US_BALL_L_PURPLE);}
			else{ObjShot_SetGraphic(obj, color);}

			if(count >= 180){
				rot = 0;
				Obj_SetAutoDelete(obj,true);
			}
			else if(count >= 120){
				rot = 1.5 * (absolute(rot)/rot);
			}


			ascent(i in 0..length(arrayLaser)){
				if(Collision_Obj_Obj(obj, arrayLaser[i])){
					CrashShot(obj);

					Obj_Delete(obj);
					break;
				}

			}


			Obj_SetAngle(obj, Obj_GetAngle(obj) + rot);
			count++;
			yield;
		}
		
		//ObjShot_FadeDelete(obj);		
	}


	task CrashShot(obj){
		let x = Obj_GetX(obj);
		let y = Obj_GetY(obj);
		//let speed = 2;
		let color = US_BALL_S_RED;
		let color2 = US_BALL_M_RED;
		let color3 = US_BALL_SS_RED;
		let num = 3;
		if(SlowFlag == false){num = 5;}
		loop(num){
			let speed = rand(1, 2.5);
			if(SlowFlag == true){
				speed = rand(2, 4);
				color = US_BALL_S_PURPLE;
				color2 = US_BALL_M_PURPLE;
				color3 = US_BALL_SS_PURPLE;
			}
			FreeFall(x, y, speed, rand(-60, -120), 0.025, color, 10);
			FreeFall(x, y, speed, rand(-60, -120), 0.025, color2, 10);
			FreeFall(x, y, speed, rand(-60, -120), 0.025, color3, 10);
			//CreateShot01(x, y, rand(1,3), rand360, color, 10);
		}
	}


	task FreeFall(x, y, speed, angle, accY, color, delay){
		let obj = CreateObjShot(x, y, speed, angle, color, delay);

		while( !Obj_BeDeleted(obj) ){

			//if(SlowFlag == true){ObjShot_SetGraphic(obj, color+7);}
			//else{ObjShot_SetGraphic(obj, color);}

			let speedX = SAtoS_X(speed, angle);
			let speedY = SAtoS_Y(speed, angle);
			speedY += accY;
			speed = (speedX^2 + speedY^2)^0.5;
			angle = GetGapAngle(0, 0, speedX, speedY);
			Obj_SetSpeed(obj, speed);
			Obj_SetAngle(obj, angle);

			yield;
		}

	}


////X[Ǘ//////////////////////////////////////////////////////////

	task SlowTask(pow, SL, SW){
		
		loop(1){
			SlowTime(pow, SL);
			wait(SW);
		}
	}

	task SlowTime(let pow, let w){  //Q1/2{ > pow
		Slow(pow - 1);
		SlowFlag = true;
		//PlaySE(SE_SHOT2);
		wait(w);
		Slow(0);
		SlowFlag = false;
	}



    // ړ
    // dist	:ړ͈
    function BossMoveEX_Beta(let dist){
	let dist2 = dist + rand(-dist/10, dist/10);	//ړ = ړɈړ10%̑O덷
	let theta = rand(0,360);
	
	let nX = cos(theta) * dist2;
	let nY = sin(theta) * dist2;
	
	BossMoveEXtoPlayer(absolute(nX), nY, GetClipMinX + 32, GetClipMinY + 48, GetClipMaxX - 32, GetClipMinY + 140);
    }



	//==============================================================================
	//	قȂʒuɂ2̓_ABԂ̋擾
	//------------------------------------------------------------------------------
	function GetGapLength(
		let xA,		// _AxW
		let yA,		// _AyW
		let xB,		// _BxW
		let yB		// _ByW
	){
		return ( ( xB - xA ) ^ 2 + ( yB - yA ) ^ 2 ) ^ 0.5;
	}
	
	//------------------------------------------------------------------------------
	//	_AقȂʒuɂ_Bւ̐Ίpx擾
	//------------------------------------------------------------------------------
	function GetGapAngle(
		let xA,		// _AxW
		let yA,		// _AyW
		let xB,		// _BxW
		let yB		// _ByW
	){
		return atan2( yB - yA, xB - xA );
	}
	
	//------------------------------------------------------------------------------
	//	_A炠鋗AΊpxɂ_BxW擾
	//------------------------------------------------------------------------------
	function GetGapX(
		let xA,			// _AxW
		let gapLength,		// _B܂ł̋
		let gapAngle		// _Bւ̐Ίpx
	){
		return xA + gapLength * cos( gapAngle );
	}
	
	//------------------------------------------------------------------------------
	//	_A炠鋗AΊpxɂ_ByW擾
	//------------------------------------------------------------------------------
	function GetGapY(
		let yA,			// _AyW
		let gapLength,		// _B܂ł̋
		let gapAngle		// _Bւ̐Ίpx
	){
		return yA + gapLength * sin( gapAngle );
	}
	




	//IuWFNge̍쐬
	function CreateObjShot(x, y, v, angle, type, delay) {
		let obj = Obj_Create(OBJ_SHOT);

		Obj_SetPosition(obj, x, y);
		Obj_SetSpeed(obj, v);
		Obj_SetAngle(obj, angle);
		ObjShot_SetGraphic(obj, type);
		ObjShot_SetDelay(obj, delay);

		return obj;
	}

	//IuWFNg[U[(ݒu)̍쐬
	function CreateObjLaser01(x, y, v, angle, type, delay, _length, width) {
		let obj = Obj_Create(OBJ_LASER);

		Obj_SetPosition(obj, x, y);
		Obj_SetAngle(obj, angle);
		ObjShot_SetGraphic(obj, type);
		ObjShot_SetDelay(obj, delay);

		ObjLaser_SetLength(obj, 0);
		ObjLaser_SetWidth(obj, width);
		ObjLaser_SetSource(obj, false);

		ExpandLaser01(obj, v, _length);

		return obj;
	}

	//̂с[[[(ݒup)
	task ExpandLaser01(obj, v, _length) {
		let NowLength = 0;

		while(NowLength < _length) {
			NowLength += v; 
			ObjLaser_SetLength(obj, NowLength);

			yield;
		}
	}

	//IuWFNg[U[(ˏo)̍쐬
	function CreateObjLaser02(x, y, ExSpeed, MoveSpeed, angle, type, _length, width) {
		let obj = Obj_Create(OBJ_LASER);

		Obj_SetPosition(obj, x, y);
		Obj_SetAngle(obj, angle);
		ObjShot_SetGraphic(obj, type);
		ObjShot_SetDelay(obj, 0);

		ObjLaser_SetLength(obj, 0);
		ObjLaser_SetWidth(obj, width);
		ObjLaser_SetSource(obj, false);

		ExpandLaser02(obj, ExSpeed, MoveSpeed, _length);

		return obj;
	}

	//̂с[[[(ˏop)
	task ExpandLaser02(obj, ExSpeed, MoveSpeed, _length) {
		let NowLength = 0;

		while(NowLength < _length) {
			NowLength += ExSpeed; 
			ObjLaser_SetLength(obj, NowLength);

			yield;
		}

		MoveLaser(obj, MoveSpeed);
	}

	//[[[(ˏop)
	task MoveLaser(obj, MoveSpeed) {
		let x     = Obj_GetX(obj);
		let y     = Obj_GetY(obj);
		let angle = Obj_GetAngle(obj);

		let xv;
		let yv;
		while(!Obj_BeDeleted(obj)) {
			xv = MoveSpeed*cos(angle);
			yv = MoveSpeed*sin(angle);

			Obj_SetPosition(obj, x + xv, y + yv);
			x = Obj_GetX(obj);
			y = Obj_GetY(obj);

			yield;
		}
	}

	//[U[̃fBCGtFNg
	//gݍ݂̃GtFNgʂقǌȂ̂ł̂悤Ȍ`
	function DelayEffect(x, y, angle, delay) {
		let v = 50;
		let type = US_BEAM_WHITE;
		let _length = 500;
		let width = 2;

		let obj = CreateObjLaser01(x, y, v, angle, type, 0, _length, width);
		Obj_SetAlpha(obj, 64);
		Obj_SetCollisionToPlayer(obj, false);

		__wait(delay);
		ObjShot_FadeDelete(obj);
	}

	//A_B_ւ̊px擾֐
	function GetAngle_fromAtoB(x1, y1, x2, y2) {
		let angle = atan2(y2 - y1, x2 - x1);
		return angle;
	}

	//A_B_ւ̋擾֐
	function GetLength_fromAtoB(x1, y1, x2, y2) {
		let _length = ( (x2 - x1)^2 + (y2 - y1)^2 )^0.5;
		return _length;
	}



	// w t[҂
	function wait(w) {
		loop(w) { yield; }
	}

	function __wait(w) {
		loop(w) { yield; }
	}




}