#東方弾幕風
#Title[憤怒「雷親父大激怒」]
#Text[]
#ScriptVersion[2]

script_enemy_main {
	let name	   = "憤怒「雷親父大激怒」";

	// 初期位置
	let xIni	   = GetCenterX;
	let yIni	   = GetClipMinY + 120;
	
	//shotall.png
	#include_function ".\lib_usershot.txt"
	//一輪の基本設定
	#include_function ".\lib\setting_itirin.txt"

	let imgShotKobushi	= GetCurrentScriptDirectory() ~ "\img\genkotu.png";
	let imgUnzan		= GetCurrentScriptDirectory() ~ "\img\unzan.png";

	let D_RATE = 30;
	let D_WAIT = 300;

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

		SetLife(1100);
		SetTimer(50);
		SetScore(1500000);
		SetDamageRate(5, 5);

		LoadGraphic(imgShotKobushi);
		LoadGraphic(imgUnzan);
		Ini_Graphic;

		LoadSE(SE_DON19);
		LoadSE(SE_SHA);
		LoadSE(SE_THUNDER);

		TMain;
	}

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

		yield;
	}

	@DrawLoop {
		Draw_Itirin;
	}

	@Finalize {
		if (!IsTimeOut()) { makeTokutenItem(25); }
		finish_bakuhatu;
//		DeleteGraphic(imgBoss);
	}

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

		TRate;
		standBy;

		//弾
		TShot;
	}

	task TShot()
	{
		let move_positionX = [GetClipMaxX-100, GetClipMinX+100, GetCenterX-30, GetCenterX, GetCenterX+10];
		let move_positionY = [GetClipMinY+135, GetClipMinY+135, GetClipMinY+100, GetClipMinY+55, GetClipMinY+90];

		wait(60);
		loop
		{
			func_thunder(true);
			SetMovePosition03(move_positionX[0], move_positionY[0], 20, 2);
			wait(270);
			func_thunder(false);
			SetMovePosition03(move_positionX[1], move_positionY[1], 20, 2);
			wait(205);
			SetMovePosition03(move_positionX[2], move_positionY[2], 20, 2);
			wait(200);
			func_thunder(false);
			func_thunder(true);
			SetMovePosition03(move_positionX[3], move_positionY[3], 20, 2);
			wait(200);
			SetMovePosition03(move_positionX[4], move_positionY[4], 20, 2);
			wait(205);
		}
	}
	
	task func_thunder(type)
	{
		if (type)
		{
			CreateShotFromScript("Unzan", 0, 0,  1, 0, 0, 190);

			wait(40);

			let angle = atan2(GetPlayerY-100,GetPlayerX-95);
			shot_thunder(55, 85, angle);
			shot_thunder(145, 110, angle);

			wait(30);
			ascent(let n in 0 .. 3)
			{
				wait(10);
				shot_kobushi(10, 1, GetClipMinX+(150-50*n), GetClipMaxY-(200-50*n));
			}
		}
		else
		{
			CreateShotFromScript("Unzan", 0, 0, -1, 0, 0, 190);
			
			wait(40);
			
			let angle = atan2(GetPlayerY-100,GetPlayerX-345);
			shot_thunder(390, 85, angle);
			shot_thunder(300, 110, angle);
			
			wait(30);
			ascent(let n in 0 .. 3)
			{
				wait(10);
				shot_kobushi(10, 2, GetClipMaxX-(150-50*n), GetClipMaxY-(200-50*n));
			}
		}
	}
	
	//顔出現→ごんぶと雷
	task shot_thunder(x, y, angle)
	{
		//予告線
		CreateLaserA(0, x, y, 500, 20, US_BEAM_YELLOW, 5);
		SetLaserDataA(0, 0, angle, 0, 0, 0, 0);
		SetShotKillTime(0, 5);
		FireShot(0);
		PlaySE(SE_SHA);	//SE

		wait(60);

		//ごんぶとレーザー
		CreateLaserA(0, x, y, 500, 50, US_BEAM_YELLOW, 0);
		SetLaserDataA(0, 0, angle, 0, 0, 0, 0);
		SetShotKillTime(0, 90);
		FireShot(0);
		
		//ぴしゃーん
		ascent(let n in 0 .. 5)
		{
			let an = angle + rand(-15,15);
			let fl = 0; let tmp = 4;
			CreateLaserC(0, x, y, 6, 24, US_BALL_F_YELLOW, n*3);
			while (fl < 30)
			{
				SetLaserDataC(0, fl, 12, an, 0, 0, 0);
				if (an > angle) { an -= rand(15,30); }
				else { an += rand(15,30); }
				fl += rand_int(tmp,tmp*2);
				tmp += 2;
			}
			SetShotKillTime(0, 120);
			FireShot(0);
		}

		PlaySE(SE_THUNDER);	//SE
		
		wait(20);
		
		let tmp = getKyoriToKabe(x, y, angle);
		let tx = x+cos(angle)*tmp; let ty = y+sin(angle)*tmp;
		let t = 0;
		loop(66)
		{
			CreateShot01(tx, ty, rand(0.5, 1.5), rand(0,360), US_BALL_S_A_YELLOW, 0);
			t++;
			yield;
		}

	}
	
	//画面外から拳を振る　自機狙い
	// kabe=1:左 2=右 3=上 4=下 それ以外だと打たない
	// kabe_x, kabe_y 拳が_*一時停止する*座標。kabeで設定した壁付近で停止する
	function shot_kobushi(speed, kabe, kabe_x, kabe_y)
	{
		let x = 0; let y = 0;
		let angle = 0;
		let FLAG = 2;

		while (FLAG > 0)	//角度が急になり過ぎる場合は発射位置を調整
		{
			alternative (kabe)
			case (1)
			{
				angle = (atan2(GetPlayerY-kabe_y ,GetPlayerX-2)+360)%360;
				x = GetClipMinX-60; y = kabe_y-tan(angle)*60;
				if (angle <= 90 && angle > 70) { kabe = 3; }
				else if (angle >= 270 && angle < 290) { kabe = 4; }
				else { FLAG = 0; }
			}
			case (2)
			{
				angle = (atan2(GetPlayerY-kabe_y ,GetPlayerX-(GetClipMaxX-2))+360)%360;
				x = GetClipMaxX+60; y = kabe_y+tan(angle)*60;
				if (angle < 110) { kabe = 3; }
				else if (angle > 250) { kabe = 4; }
				else { FLAG = 0; }
			}
			case (3)
			{
				angle = (atan2(GetPlayerY-2 ,GetPlayerX-kabe_x)+360)%360;
				x = kabe_x-60/tan(angle); y = GetClipMinY-60;
				if (angle > 160) { kabe = 2; }
				else if (angle < 20) { kabe = 1; }
				else { FLAG = 0; }
			}
			case (4)
			{
				angle = (atan2(GetPlayerY-(GetClipMaxY-2) ,GetPlayerX-kabe_x)+360)%360;
				x = kabe_x+60/tan(angle); y = GetClipMaxY+60;
				if (angle < 200) { kabe = 2; }
				else if (angle > 340) { kabe = 1; }
				else { FLAG = 0; }
			}
			others
			{
				return (false);
			}
			FLAG--;
		}
		
		CreateShotFromScript("Kobushi", x, y, speed, angle, 75, 2);
	}
}


//拳弾
//画面端からGetArgumentピクセル以上離れたら一時停止、もう片方の画面端で停止
//フェードアウト後、削除
script_shot Kobushi
{
	let SPEED = GetSpeed();		//スピード
	let SIZE = 50;			//弾の半径

	let imgShot	= GetCurrentScriptDirectory() ~ "\img\genkotu.png";

	let ERACE_FLAG = 0;
	let g_alpha = 0;
	let FALED = 1;		//拳骨発射失敗
	let SE_FLAG = 0;

	//サウンドデータの在処
	#include_function ".\lib\lib_se.txt"

	@Initialize
	{	
//		LoadGraphic(imgShot);
		SetTexture(imgShot);
		SetGraphicRect(  0,   0, 128, 128);

		SetBombResist();	//ボムで消さない
		SetSpeed(6);		//speed初期値
	}
	@MainLoop
	{
		let angle = GetAngle%360;

		if(OnDelay()){
			//一定位置より内側に入ったら一時停止
			if (angle < 90) { if (GetX >= GetClipMinX+GetArgument && GetY >= GetClipMinY+GetArgument) { SetSpeed(0); FALED=0; } }
			else if (angle < 180) { if (GetX <= GetClipMaxX-GetArgument && GetY >= GetClipMinY+GetArgument) { SetSpeed(0); FALED=0; } }
			else if (angle < 270) { if (GetX <= GetClipMaxX-GetArgument && GetY <= GetClipMaxY-GetArgument) { SetSpeed(0); FALED=0; } }
			else { if (GetX >= GetClipMinX+GetArgument && GetY <= GetClipMaxY-GetArgument) { SetSpeed(0); FALED=0; } }

			if (FALED == 0)
			{
				if (SE_FLAG <= 0) { StopSE(SE_WARP); PlaySE(SE_WARP); SE_FLAG = 1; }
			}

		//	SetCollisionB(GetX, GetY, 60);	//当たり判定→やっぱり付けない
		}
		else if (ERACE_FLAG == 1)
		{
			//徐々に薄く、消えたら削除
			g_alpha -= 10;
			if (g_alpha <= 0) { End(); }
			else if (g_alpha > 150) { SetCollisionB(GetX, GetY, 60); }	//当たり判定
		}
		else
		{
			//発射に失敗した場合は早々に削除
			if (FALED == 1) { End(); }
			
			//壁にぶつかったら停止
			if (angle < 90)
			{
				if (GetX >= GetClipMaxX-SIZE || GetY >= GetClipMaxY-SIZE) { g_alpha = 255; ERACE_FLAG = 1; SetSpeed(0); }
				else { SetSpeed(SPEED); }
			}
			else if (angle < 180)
			{
				if (GetX <= GetClipMinX+SIZE || GetY >= GetClipMaxY-SIZE) { g_alpha = 255; ERACE_FLAG = 1; SetSpeed(0); }
				else { SetSpeed(SPEED); }
			}
			else if (angle < 270)
			{
				if (GetX <= GetClipMinX+SIZE || GetY <= GetClipMinY+SIZE) { g_alpha = 255; ERACE_FLAG = 1; SetSpeed(0); }
				else { SetSpeed(SPEED); }
			}
			else
			{
				if (GetX >= GetClipMaxX-SIZE || GetY <= GetClipMinY+SIZE) { g_alpha = 255; ERACE_FLAG = 1; SetSpeed(0); }
				else { SetSpeed(SPEED); }
			}

			SetCollisionB(GetX, GetY, 60);	//当たり判定
		}
	}
	@DrawLoop
	{
		if (g_alpha > 0) { SetAlpha(g_alpha); }
		SetGraphicAngle(0, 0, 180+GetAngle);
		DrawGraphic(GetX, GetY);
	}
	@Finalize
	{
	}
}

//雲山の顔
//GetArgument=消滅時間（停止後から数えて）
script_shot Unzan
{
	let MUKI = GetArgument();	//顔の向き
	if (GetSpeed() < 0) { MUKI = -1; }	//右
	else { MUKI = 1; }			//左
	let TIMER = GetArgument();		//出現時間

	let imgShot	= GetCurrentScriptDirectory() ~ "\img\unzan.png";
	#include_function ".\lib\lib_se.txt"

	let X1;		//当たり判定調整用
	let sx; let sy;
	let flame = 0;
	let timer = 0;

	@Initialize
	{	
//		LoadGraphic(imgShot);
		SetTexture(imgShot);
		SetGraphicRect(  0,   0, 100, 192);
		SetGraphicScale(1.75, 1.75);	//でけぇ！

		SetBombResist();	//ボムで消さない
		if (MUKI == 1)
			{ sx=GetClipMinX-100; X1=-15; }
		else
			{ sx=GetClipMaxX+100;
			  SetGraphicAngle(180,0,0); X1=15; }
		sy = -50;
		SetX(sx); SetY(sy);	//画面外

		SetSpeed(0);		//止める

		PlaySE(SE_DON19);
	}
	@MainLoop
	{
		if (timer > TIMER+90)
			{ End(); }
		else if (timer > TIMER)
			{ SetX(sx-180*(timer-TIMER)/90*MUKI); SetY(sy-(timer-TIMER)*flame/90); }	//画面内から消す
		else if (flame <= 30)
		{
			//画面内に表示
			SetX(sx+180*flame/30*MUKI);
			SetY(sy+150*flame/30);
			if (flame == 30)
			{
				timer = 1;
				sx = GetX();
				sy = GetY();
			}
		}
		flame++;
		if (timer > 0) { timer++; }
		
		SetCollisionB(GetX+X1, GetY-55, 80);	//当たり判定1
		SetCollisionB(GetX+X1, GetY+80, 75);	//当たり判定2
	}
	@DrawLoop
	{
		SetRenderState(ADD);
		DrawGraphic(GetX, GetY);
	}
	@Finalize
	{
	}
}