#e
#Title[Ήu厂̐Kv]
#Text[test]
#ScriptVersion[2]

script_enemy_main {
	let name	= "Ήu厂̐Kv";

	// shot_all.png
	#include_function ".\lib_usershot.txt"
	// @񏉊ݒ
	#include_function ".\lib\setting_byakuren.txt"
	
	imgBlankCircle = GetCurrentScriptDirectory() ~ "\img\tokage.png";

	// ʒu
	let xIni	= GetCenterX;
	let yIni	= GetClipMinY + 120;

	let D_RATE = 50;
	let D_WAIT = 210;
	
	@Initialize {
		CutIn(YOUMU, name, "", 0, 0, 0, 0);

		SetLife(1000);
		SetTimer(60);
		SetScore(1500000);
		SetDamageRate(5, 5);

		Ini_Graphic;
		LoadGraphic(imgEffect);
		LoadGraphic(imgBlankCircle);

		TMain;
	}

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

		yield;
	}

	@DrawLoop {
		Draw_Byakuren;
	}

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

	// C^XN
	task TMain {
		yield;

		TRate;
		standBy;
		effect_guruguru_circle(255,128,128);

		//e
		TShot;
	}

	//@֋߂Â
	task TShot()
	{
		wait(40);
		TShot_moveshot;
		loop
		{
			move_power;
			shot_bigfire(-10, -16, 40, 5, 60, 5, US_FIRE_RED);
			wait(210);
		}
	}

	//
	task move_power()
	{
		let KYORI = 250;

		PlaySE(SE_POWER);
		Concentration01(80);
		ATTACK_FLAG = 1;

		wait(20);

		let lx = GetX + cos(GetAngleToPlayer)*KYORI;
		let ly = GetY + sin(GetAngleToPlayer)*KYORI;

		if (lx > GetClipMaxX) { lx = GetClipMaxX; }
		else if (lx < GetClipMinX) { lx = GetClipMinX; }
		if (lx > GetClipMaxY) { lx = GetClipMaxY; }
		else if (ly < GetClipMinY) { ly = GetClipMinY; }


		wait(50);

		ATTACK_FLAG = 0;
		SetMovePosition03(lx, ly, 2, 6);
	}

	//e@
	task TShot_moveshot
	{
		let t = 0;
		loop
		{
			if (GetSpeed() > 2) {
				CreateShotA(0, GetX, GetY, 20);
				SetShotDataA(0,   0, 0, t*30, 0, 0, 0, US_BALL_S_A_WHITE);
				SetShotDataA(0, 160, NULL, NULL, 0, 0.03, 1.5, US_BALL_S_A_WHITE);
				FireShot(0);
				t++;
			}
			yield;
		}
	}
	
	//ł΂̋
	task shot_bigfire(offset_x, offset_y, count, wt, wt2, speed, graphic)
	{
		//IuWFNge̍쐬
		let obj = Obj_Create(OBJ_SHOT);
		let delay=0;
		//p[^̐ݒ
		Obj_SetPosition(obj, GetX+offset_x, GetY+offset_y);
		Obj_SetSpeed(obj, 0);
		Obj_SetAngle(obj, 0);
		ObjShot_SetGraphic(obj, NULL);
		ObjShot_SetDelay(obj, delay);
		Obj_SetAutoDelete(obj,false);
		ObjShot_SetBombResist(obj,true);
		
		loop(delay){ yield; }
		

		let t = 0; let rand_max = 24;
		while(!Obj_BeDeleted(obj)) {
			if (t%wt == 0)
			{
				shot_smallfire(obj, rand(18,rand_max), rand(0,360), rand(4,10), graphic, 3);
				count--; rand_max++;
				if (count <= 0) { break; }
			}
			Obj_SetPosition(obj, GetX+offset_x, GetY+offset_y);
			yield;
		}
		
		Obj_SetAngle(obj, GetAngleToPlayer);	//@_bNI

		t = 0;
		while(!Obj_BeDeleted(obj)) {
			Obj_SetPosition(obj, GetX+offset_x, GetY+offset_y);
			if (t > wt2) { break; }
			t++;
			yield;
		}

		Obj_SetSpeed(obj, speed);

		Obj_SetAutoDelete(obj,true);		//Ă
		ObjShot_SetBombResist(obj,false);	//{ϐȂ
	}

	//΂̋ʒP
	//@̎񁨔ˎɒi
	task shot_smallfire(parent, r, angle, kidou, graphic, delay)
	{
		//IuWFNge̍쐬
		let obj = Obj_Create(OBJ_SHOT);
		
		let t = 0;
		let kaiten_c = cos(angle);
		let kaiten_s = sin(angle);

		//p[^̐ݒ
		Obj_SetPosition(obj, Obj_GetX(parent) + (cos(t*kidou)*kaiten_c+sin(t*kidou)*kaiten_s/3)*r, Obj_GetY(parent) + (-cos(t*kidou)*kaiten_s+sin(t*kidou)/3*kaiten_c)*r);
		Obj_SetSpeed(obj, 0);
		Obj_SetAngle(obj, 0);
		Obj_SetAutoDelete(obj, false);
		ObjShot_SetGraphic(obj, graphic);
		ObjShot_SetDelay(obj, delay);
		ObjShot_SetBombResist(obj,true);
		
		loop(delay){ yield; }
		
		while(!Obj_BeDeleted(parent)){
			//ȉ~O]
			Obj_SetPosition(obj, Obj_GetX(parent) + (cos(t*kidou)*kaiten_c+sin(t*kidou)*kaiten_s/3)*r,
						Obj_GetY(parent) + (-cos(t*kidou)*kaiten_s+sin(t*kidou)/3*kaiten_c)*r);
			//px
			if (sin(t*kidou) > 0) { Obj_SetAngle(obj, atan(-1/(3*tan(t*kidou)))-angle+180); }
			else { Obj_SetAngle(obj, atan(-1/(3*tan(t*kidou)))-angle); }

			t++;
			yield;

			//SoύX
			if (Obj_GetSpeed(parent) != 0) { break; }
		}

		ObjShot_SetBombResist(obj,true);	//{ϐ
		
		//eƂقړɐi
		Obj_SetAngle(obj,Obj_GetAngle(parent)+rand(-3, 3));
		Obj_SetSpeed(obj,Obj_GetSpeed(parent)*rand(0.5^3, 1.5^3)^(1/3));
		
		//ȍ~͐eƖ֌WBǂŔ
		let hansha = 0;
		while(!Obj_BeDeleted(obj)){
			hansha = GetBorderLine(Obj_GetX(obj), Obj_GetY(obj), 5);
			if (hansha != 0) { break; }
			yield;
		}
		
		Obj_SetSpeed(obj, Obj_GetSpeed(obj)^0.5);
		if (hansha == 1)
			{ Obj_SetAngle(obj, rand(-90, 90)); }
		else if (hansha == 2)
			{ Obj_SetAngle(obj, rand(0, 180)); }
		else if (hansha == 2)
			{ Obj_SetAngle(obj, rand(90, 270)); }
		else
			{ Obj_SetAngle(obj, rand(180, 360)); }

		Obj_SetAutoDelete(obj,true);		//Ă
	}

	// GetBorderLine֐
	// @wWʂ̓O肷֐łB
	function GetBorderLine(PosX,PosY,difference) {
		if(PosX <= GetClipMinX + difference) { return 1 }
		else if(PosY <= GetClipMinY + difference) { return 2 }
		else if(PosX >= GetClipMaxX - difference) { return 3 }
		else if(PosY >= GetClipMaxY - difference) { return 4 }
		else { return 0 }
	}
}
