#東方弾幕風
#Title[火炎「蜥蜴の尻尾」]
#Text[test]
#ScriptVersion[2]

script_enemy_main {
	let name	= "火炎「蜥蜴の尻尾」";

	// shot_all.png
	#include_function ".\lib_usershot.txt"
	// 白蓮さん初期設定
	#include_function ".\lib\setting_byakuren.txt"
	
	imgBlankCircle = GetCurrentScriptDirectory() ~ "\img\tokage.png";

	// 初期位置
	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);
	}

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

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

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

	//自動弾発生機
	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)
	{
		//オブジェクト弾の作成
		let obj = Obj_Create(OBJ_SHOT);
		let delay=0;
		//パラメータの設定
		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);	//自機狙いロックオン

		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);	//ボム耐性もいらない
	}

	//火の玉単発
	//白蓮の周りを周回→発射時に直進
	task shot_smallfire(parent, r, angle, kidou, graphic, delay)
	{
		//オブジェクト弾の作成
		let obj = Obj_Create(OBJ_SHOT);
		
		let t = 0;
		let kaiten_c = cos(angle);
		let kaiten_s = sin(angle);

		//パラメータの設定
		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)){
			//楕円軌道を回転
			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);
			//角度
			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;

			//中心が動き出したら向き変更
			if (Obj_GetSpeed(parent) != 0) { break; }
		}

		ObjShot_SetBombResist(obj,true);	//ボム耐性消去
		
		//親とほぼ同方向に進む
		Obj_SetAngle(obj,Obj_GetAngle(parent)+rand(-3, 3));
		Obj_SetSpeed(obj,Obj_GetSpeed(parent)*rand(0.5^3, 1.5^3)^(1/3));
		
		//以降は親と無関係。壁で反射
		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関数
	// 　指定座標が画面の内側か外側か判定する関数です。
	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 }
	}
}
