#東方弾幕風
#Title[捜符「モーションセンサー」]
#Text[]
#ScriptVersion[2]

script_enemy_main {
	let name	= "捜符「モーションセンサー」";

	#include_function ".\lib_usershot.txt"
	#include_function ".\lib\setting_nazu.txt"

	// 初期位置
	let xIni	= GetCenterX;
	let yIni	= GetClipMinY + 80;
	
	let D_RATE = 30;
	let D_WAIT = 300;

	@Initialize {
		CutIn(YOUMU, name, "", 0, 0, 0, 0);

		SetLife(850);
		SetTimer(45);
		SetScore(1500000);
		SetDamageRate(5, 5);

		Ini_Graphic;

		TMain;
	}

	@MainLoop {
		SetCollisionA(GetX, GetY, 32);
		SetCollisionB(GetX, GetY, 16);

		yield;
	}

	@DrawLoop {
		Draw_Nazu;
	}

	@Finalize {
		if (!IsTimeOut()) { makeTokutenItem(12); }

//		DeleteGraphic(imgBoss);
	}

	// メインタスク
	task TMain {
		yield;

		TRate;
		standBy;

		//弾
		ATTACK_FLAG = 1;
		TShot;
	}

	//弾射出タスク
	task TShot()
	{
		let move_positionX = [GetClipMinX+64, GetCenterX, GetClipMaxX-64, GetCenterX];
		let move_positionY = [GetClipMinY+135, GetClipMinY+150, GetClipMinY+110, GetClipMinY+80];

		wait(40);
		let t = 0;
		loop
		{
			PlaySE(SE_LASER2);
			laser_motion(GetAngleToPlayer-60, -210,  1, 360);
			laser_motion(GetAngleToPlayer+60,  210, -1, 360);
			wait(60);
			shot_from_ue(60);
			SetMovePosition03(move_positionX[t%4], move_positionY[t%4], 20, 4);
			shot_jikansa(8);
			wait(120);
			t++;
		}
	}

	//上から降る弾
	task shot_from_ue(count)
	{
		loop(count/3)
		{
			loop(3)
			{
				let spd = rand(1,3);
				CreateShot12(rand(GetClipMinX,GetClipMaxX), GetClipMinY,
					rand(-0.2,0.2), 0, 0, spd/60, 0, spd, US_RICE_S_YELLOW, 15);
			}
			yield;
		}
	}

	//時間差自機狙い弾
	task shot_jikansa(count)
	{
		loop(count)
		{
			SetShotDirectionType(PLAYER);
			CreateShotA(0, GetX, GetY, 15);
			SetShotDataA(0,  0, 0.2, 0, 0, 0, 0, US_SCALE_PURPLE);
			SetShotDataA(0, 40,NULL, 0, 0, 0.1, 4, US_SCALE_PURPLE);
			FireShot(0);
			SetShotDirectionType(ABSOLUTE);

			StopSE(SE_SHOT);
			PlaySE(SE_SHOT);
			wait(5);
		}
	}
	
	//プレイヤーの動きに反応して角度が変わるレーザー
	task laser_motion(angle, angle2, a_change, timer)
	{
		//オブジェクト弾の作成
		let obj = Obj_Create(OBJ_LASER);
		
		//パラメータの設定
		Obj_SetAngle(obj, angle+angle2);
		Obj_SetPosition(obj, GetX+cos(angle)*32, GetY+sin(angle)*32);
		ObjLaser_SetLength(obj, 600);
		ObjLaser_SetWidth(obj, 24);
		ObjShot_SetDelay(obj, 10);
		ObjShot_SetGraphic(obj, US_BEAM_RED);
		ObjShot_SetBombResist(obj, true);
		
		loop(10){ yield; }

		let tmp = angle2/40;
		loop(40){
			angle2 -= tmp;
			Obj_SetAngle(obj, angle+angle2);
			yield;
		}

		let px = GetPlayerX;
		let py = GetPlayerY;
		loop(timer){
			yield;
			
			if (px != GetPlayerX || py != GetPlayerY)
			{
				px = GetPlayerX;
				py = GetPlayerY;

				if (GetPlayerMoveState == MOVE_SLOW)
				{
					angle += a_change;
					Obj_SetAngle(obj, angle);
				}
				else if (GetPlayerMoveState == MOVE_NORMAL)
				{
					angle += a_change*2;
					Obj_SetAngle(obj, angle);
				}
			}
		}

		ObjShot_FadeDelete(obj);
	}
}
