#東方弾幕風
#Title[妖艶剣「花魁の落とし」]
#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"	//ここに指定のキャラをインクルード
	#include_function ".\INCLUDE\BOSS_INI.txt"

	let name        = "妖艶剣「花魁の落とし」";

	let wIni    = 100;	//攻撃移行まで
	let r;

	let SlowFlag = false;    //スローがかかっているか


	let arrayLaser = [];	//レーザー判定

		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);		//ライフ
		SetTimer(75);		//タイマー
		SetScore(4000000);	//ボーナススコア

		LoadGraphicthM;
		SetTexture(imgBoss);

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

		InitializeAction();	//アニメーション初期化
		TMain;
	}

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

		yield; 
	}

	@DrawLoop {
		DrawBoss(imgBoss);


	}

	@Finalize {
		SpellClear;

		DeleteGraphic(imgBoss);
	}

	@BackGround {
		//bgDraw;
	}

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

    // メインタスク（普通はここ以降から編集してください）
	task TMain {
		yield;

		loop(wIni) { yield; }

		
		tShot01;
		//tShot02;
		//Idou;

		//何個設置してもＯＫ
	}



	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){
		//弾タスク初期化

		let count = 0;

	
		let obj = CreateObjShot(x, y, speed, angle, color, delay);//弾オブジェクトを作成

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


		while( !Obj_BeDeleted(obj) ){ //削除されるまでループを実行

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

	}


////スロー管理//////////////////////////////////////////////////////////

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

	task SlowTime(let pow, let w){  //２で1/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%の前後誤差
	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);
    }



	//==============================================================================
	//	異なる位置にある2つの点AB間の距離を取得する
	//------------------------------------------------------------------------------
	function GetGapLength(
		let xA,		// 点Aのx座標
		let yA,		// 点Aのy座標
		let xB,		// 点Bのx座標
		let yB		// 点Bのy座標
	){
		return ( ( xB - xA ) ^ 2 + ( yB - yA ) ^ 2 ) ^ 0.5;
	}
	
	//------------------------------------------------------------------------------
	//	点Aから異なる位置にある点Bへの絶対角度を取得する
	//------------------------------------------------------------------------------
	function GetGapAngle(
		let xA,		// 点Aのx座標
		let yA,		// 点Aのy座標
		let xB,		// 点Bのx座標
		let yB		// 点Bのy座標
	){
		return atan2( yB - yA, xB - xA );
	}
	
	//------------------------------------------------------------------------------
	//	点Aからある距離、絶対角度にある点Bのx座標を取得する
	//------------------------------------------------------------------------------
	function GetGapX(
		let xA,			// 点Aのx座標
		let gapLength,		// 点Bまでの距離
		let gapAngle		// 点Bへの絶対角度
	){
		return xA + gapLength * cos( gapAngle );
	}
	
	//------------------------------------------------------------------------------
	//	点Aからある距離、絶対角度にある点Bのy座標を取得する
	//------------------------------------------------------------------------------
	function GetGapY(
		let yA,			// 点Aのy座標
		let gapLength,		// 点Bまでの距離
		let gapAngle		// 点Bへの絶対角度
	){
		return yA + gapLength * sin( gapAngle );
	}
	




	//オブジェクト弾の作成
	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;
	}

	//オブジェクトレーザー(設置)の作成
	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;
	}

	//のびーるれーざー(設置用)
	task ExpandLaser01(obj, v, _length) {
		let NowLength = 0;

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

			yield;
		}
	}

	//オブジェクトレーザー(射出)の作成
	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;
	}

	//のびーるれーざー(射出用)
	task ExpandLaser02(obj, ExSpeed, MoveSpeed, _length) {
		let NowLength = 0;

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

			yield;
		}

		MoveLaser(obj, MoveSpeed);
	}

	//うごーくれーざー(射出用)
	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;
		}
	}

	//レーザーのディレイエフェクト
	//組み込みのエフェクトが死ぬほど嫌いなのでこのような形に
	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点への角度を取得する関数
	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 フレームだけ待つ
	function wait(w) {
		loop(w) { yield; }
	}

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




}