#e
#Title[{ue匃{v]
#Text[]
#ScriptVersion[2]

script_enemy_main {
	let name	   = "{ue匃{v";

	// ʒu
	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);
	}

	// C^XN
	task TMain {
		yield;

		TRate;
		standBy;

		//e
		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));
			}
		}
	}
	
	//oԂƗ
	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);

		//Ԃƃ[U[
		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;
		}

	}
	
	//ʊO猝U@@_
	// kabe=1: 2=E 3= 4= ȊOƑłȂ
	// kabe_x, kabe_y _*ꎞ~*WBkabeŐݒ肵Ǖt߂Œ~
	function shot_kobushi(speed, kabe, kabe_x, kabe_y)
	{
		let x = 0; let y = 0;
		let angle = 0;
		let FLAG = 2;

		while (FLAG > 0)	//px}ɂȂ߂ꍇ͔ˈʒu𒲐
		{
			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);
	}
}


//e
//ʒ[GetArgumentsNZȏ㗣ꂽꎞ~AЕ̉ʒ[Œ~
//tF[hAEgA폜
script_shot Kobushi
{
	let SPEED = GetSpeed();		//Xs[h
	let SIZE = 50;			//e̔a

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

	let ERACE_FLAG = 0;
	let g_alpha = 0;
	let FALED = 1;		//ˎs
	let SE_FLAG = 0;

	//TEhf[^̍ݏ
	#include_function ".\lib\lib_se.txt"

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

		SetBombResist();	//{ŏȂ
		SetSpeed(6);		//speedl
	}
	@MainLoop
	{
		let angle = GetAngle%360;

		if(OnDelay()){
			//ʒuɓꎞ~
			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);	//蔻聨ςtȂ
		}
		else if (ERACE_FLAG == 1)
		{
			//XɔA폜
			g_alpha -= 10;
			if (g_alpha <= 0) { End(); }
			else if (g_alpha > 150) { SetCollisionB(GetX, GetY, 60); }	//蔻
		}
		else
		{
			//˂Ɏsꍇ͑Xɍ폜
			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
	{
	}
}

//_R̊
//GetArgument=Ŏԁi~ォ琔āj
script_shot Unzan
{
	let MUKI = GetArgument();	//̌
	if (GetSpeed() < 0) { MUKI = -1; }	//E
	else { MUKI = 1; }			//
	let TIMER = GetArgument();		//o

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

	let X1;		//蔻蒲p
	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);	//łI

		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);	//ʊO

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