#東方弾幕風
#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 Level = "L";
	let timeL = 10;

		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(3000);		//ライフ
		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 tShot02(){
			let angle = rand360;
		let time = 0;
		wait(30);
		loop{
			//let angle = rand360;
			let r = 100;
			ascent(i in 0+time..25+time){
				ascent(n in 0..5){
					ZanzouShot(GetX + r * cos(angle + i*3), GetY + r * sin(angle + i*3), 2 - n*0.1, angle+i*-9+180 + n*72, US_LIGHT2_RED, 10);
					ZanzouShot(GetX + r * cos(angle + i*3 + 180), GetY + r * sin(angle + i*3 + 180), 2 - n*0.1, angle+i*-9+180 + n*72 + 180, US_LIGHT2_RED, 10);
				}
				//ZanzouShot(GetX + r * cos(angle + i), GetY + r * sin(angle + i), 2, angle+i+180, US_LIGHT2_RED, 10);
	
				r += 120/150;
				wait(3);
			}
			angle += 91.7;
			time+=3;
			wait(145);

			let r = 100;
			ascent(i in 0+time..25+time){
				ascent(n in 0..5){
					ZanzouShot(GetX + r * cos(angle + i*3), GetY + r * sin(angle + i*3), 2 - n*0.1, angle+i*-9+180 + n*72, US_LIGHT2_BLUE, 10);
					ZanzouShot(GetX + r * cos(angle + i*3 + 180), GetY + r * sin(angle + i*3 + 180), 2 - n*0.1, angle+i*-9+180 + n*72 + 180, US_LIGHT2_BLUE, 10);
				}
				//ZanzouShot(GetX + r * cos(angle + i), GetY + r * sin(angle + i), 2, angle+i+180, US_LIGHT2_RED, 10);
	
				r += 120/150;
				wait(3);
			}
			angle += 91.7;
			time+=3;
			wait(175);
		}
	}

	task ZanzouShot(x, y, speed, angle, color, delay){
		CreateShot01(x, y, speed, angle, color, delay);
		ascent(i in 0..1){
			wait(1);
			Shot01(x, y, speed, angle, color, delay, 60*(5-1-i)/5);
		}
	}


	task tShot01{
		loop{
			ascent(i in 0..6){
				YakuLaser(i*60 + GetAngleToPlayer - 90 +10, 1);
			}
			wait(220);
			ascent(i in 0..6){
				YakuLaser(i*60 + GetAngleToPlayer - 90 -10, -1);
			}
			wait(250);
		}
	}


	task YakuLaser(angle, flag){	// flagは正か負　1 -1   220frame
		let x = GetX; let y = GetY;
		let v = 10;
		//let angle = 0;
		let type = US_BEAM_PURPLE;
		let delay = 60;
		let _length = v*delay;
		let width = 15;

		DelayEff(1, x, y, 0, 0, US_BEAM_WHITE, 10, 190);
		let obj = CreateObjLaser01(x, y, v, angle, type, delay, _length, width);

		wait(delay);
		loop(120){
			Obj_SetAngle(obj, Obj_GetAngle(obj) + 30/120 * flag);
			wait(1);
		}
		ObjDeleteSelf(obj, 30);
	}





	task Shot01(let x, let y, let speed, let angle, let color, let delay, alpha){
		//弾タスク初期化

		let count = 0;

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

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


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

			count++;
			yield;
		}
		
		//ObjShot_FadeDelete(obj);		
	}


	task ObjSyosoku(obj, Syo, speed, frame){
		ascent(i in 0..frame){
			Obj_SetSpeed(obj, Syo * (frame - 1 - i)/10 + speed);
			wait(1);
		}
	}


////オブジェクトを徐々に透明にして消す//////////////////////////////////

	function ObjDeleteSelf(obj, frame){
		if(frame <= 0){return;}
		Obj_SetCollisionToPlayer(obj, false);
		ascent(i in 0..frame){
			Obj_SetAlpha(obj, 255-i*255/frame);
			ObjLaser_SetWidth(obj, ObjLaser_GetWidth(obj)* (1 - i/frame));
			yield;
		}
		Obj_Delete(obj);
	}

///////////////////////////////////////////////////////////////////////

//演出用ディレイ弾(小さくなるか：0=通常 1=消えるまで小さくならない、レーザー用など
//				   X座標、Y座標、移動速度、移動方向、色
//				   大きさ：弾の遅延時間で設定
//				   消えるまでの時間)
	task DelayEff(type, x, y, speed, angle, color, scale, deltime) {
		if(OnPlayerMissed == true) { return; }

		let Obj  = Obj_Create(OBJ_SHOT);
		if(scale < 3){scale = 3;}
		if(scale <= deltime && type != 1){deltime = scale-1;}

		Obj_SetPosition(Obj, x, y);
		ObjShot_SetGraphic(Obj, color);
		ObjShot_SetDelay(Obj, scale);
		ObjShot_SetBombResist(Obj, true);
		ObjShot_ToItem(Obj, false);
		Obj_SetCollisionToPlayer(Obj, false);

		ascent(i in 0..deltime) {
			Obj_SetPosition(Obj, Obj_GetX(Obj) + speed*cos(angle), Obj_GetY(Obj) + speed*sin(angle));
			if(type == 1 && i < deltime - (scale-2)) { ObjShot_SetDelay(Obj, scale); }

			yield;
		}

		Obj_Delete(Obj);
	}


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

    




}