#東方弾幕風
#Title[白蓮さん通常２]
#Text[火蜥蜴の尻尾準備]
#ScriptVersion[2]

script_enemy_main {
	let name	= "";

	// shot_all.png
	#include_function ".\lib_usershot.txt"
	// 白蓮さん初期設定
	#include_function ".\lib\setting_byakuren.txt"

	// 初期位置
	let xIni	= GetCenterX;
	let yIni	= GetClipMinY + 120;

	let D_RATE = 80;
	let D_WAIT = 260;

	@Initialize {

		SetLife(2000);
		SetTimer(40);
		SetDamageRate(5, 5);

		Ini_Graphic;
		LoadGraphic(imgBlankCircle);

		TMain;
	}

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

		yield;
	}

	@DrawLoop {
		Draw_Byakuren;
	}

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

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

		TRate;
		standBy;

		//エフェクト
		effect_blank_circle();
		powerSave;

		//弾
		TShot;
	}

	//弾射出タスク
	task TShot()
	{
		ATTACK_FLAG = 1;
		lazer_tokage(10, 8, 100, US_BALL_F_RED, 2.5, 9);

		loop
		{
			ascent (let t in 0 .. 5)
			{
				shot_sakuretu(270+t*25, 5, US_BALL_F_BLUE, 4, 3, 4, 3);
				shot_sakuretu(270-t*25, 5, US_BALL_F_BLUE, 4, 3, 4, 3);
				if (t == 2) { moveBoss(20, 60, 10, 35, 20, 2, 64, 40, 150); }
				StopSE(SE_LASER2);
				PlaySE(SE_LASER2);
				wait(90);
			}
			moveBoss(20, 60, 10, 35, 20, 2, 64, 40, 150);
			wait(120);
		}
	}

	task lazer_tokage(speed, leng, width, graphic, speed2, wt)
	{
		PlaySE(SE_LASER);
		loop
		{
			lazer_tokage1(speed, leng, width, graphic, speed2);
			lazer_tokage2(speed, leng, width, graphic, speed2);
			wait(wt);
		}
	}

	//自機Y座標と同じ位置に着弾するレーザー
	//→レーザー着弾後に弾発生
	task lazer_tokage1(speed, leng, width, graphic, speed2)
	{
		let angle = atan2(GetPlayerY-GetY, GetClipMaxX-GetX);
		let old_y = GetPlayerY;
		CreateLaser01(GetX, GetY, speed, angle, width, leng, graphic, 4);
		
		let timer = ceil(((GetPlayerY-GetY)^2+(GetX-GetClipMaxX)^2)^0.5 / speed) + 5;
		wait(timer);
		CreateShot01(GetClipMaxX, old_y, speed2, 180, US_FIRE_RED,0);
	}
	task lazer_tokage2(speed, leng, width, graphic, speed2)
	{
		let angle = atan2(GetPlayerY-GetY, GetClipMinX-GetX);
		let old_y = GetPlayerY;
		CreateLaser01(GetX, GetY, speed, angle, width, leng, graphic, 4);
		
		let timer = ceil(((GetPlayerY-GetY)^2+(GetX-GetClipMinX)^2)^0.5 / speed) + 5;
		wait(timer);
		CreateShot01(GetClipMinX, old_y, speed2, 0, US_FIRE_RED, 0);
	}

	//炸裂ショット
	//speed > 0でないと動作がおかしくなる
	task shot_sakuretu(angle, speed, graphic, t_speed, t1, t2, t3)
	{
		let RAND_CHILD = 60;
		CreateLaser01(GetX, GetY, speed, angle, 150, 8, graphic, 15);

		let kyori = getKyoriToKabe(GetX, GetY, angle);	//壁までの距離
		let timer = ceil(kyori/speed);	//当たるまでの時間
		let tmpx = GetX+cos(angle)*kyori;
		let tmpy = GetY+sin(angle)*kyori;

		let timer2 = ceil((kyori+150)/speed);	//当たり終えるまでの時間
		wait(timer+15);

		//炸裂 画像固定
		let t = 0;
		loop(timer2-timer)
		{
			if (t%2 == 0)
			{
				let ts = rand(t_speed/5, t_speed/2);
				let ta = angle+180+rand(RAND_CHILD,-RAND_CHILD);
				CreateShot12(tmpx, tmpy,
					cos(ta)*ts, sin(ta)*ts, 0, 0.02, 0, 2, US_STAR_S_BLUE, 0);
			}
			t++;
			yield;
		}
	}
}
