#東方弾幕風
#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 = 100;
	let D_WAIT = 300;

	@Initialize {
		SetLife(4000);
		SetTimer(63);
		SetDamageRate(5, 5);

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

		TMain;
	}

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

		yield;
	}

	@DrawLoop {
		Draw_Byakuren;
	}

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

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

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

		TRate;
		standBy;
		powerSave;

		//弾
		TShot;
	}

	//弾射出タスク
	task TShot()
	{
		let byaku_optionX = [-90,90,-45,45];
		let byaku_optionY = [30,30,-60,-60];

		ATTACK_FLAG = 1;
		shot_kyokusen(GetX, GetY, 10, 0, 1.2, -1, US_BILL_PURPLE, 2, 72, 40, 0.5);
		shot_kyokusen(GetX, GetY, 10, 0, 1.2,  1,   US_BILL_AQUA, 2, 72, 40, 0.5);

		let t = 0;
		loop
		{
			shot_guruguru_oodama(GetX+byaku_optionX[t%4], GetY+byaku_optionY[t%4],
				4, 0.05, GetAngleToPlayer, US_BALL_L_BLUE, 20,
				3, US_STAR_S_YELLOW);
			t++;
			wait(180);
		}
	}

	task shot_guruguru_oodama(x, y, speed, s_gain, angle, graphic, delay, speed2, graphic2)
	{
		if (s_gain <= 0) { break; }

		//オブジェクト弾の作成
		let obj = Obj_Create(OBJ_SHOT);
		
		//パラメータの設定
		Obj_SetPosition(obj, x, y);
		Obj_SetSpeed(obj, speed);
		Obj_SetAngle(obj, angle);
		ObjShot_SetGraphic(obj, graphic);
		ObjShot_SetDelay(obj, delay);
		ObjShot_SetBombResist(obj, true);
		
		loop(delay){ yield; }
		
		PlaySE(SE_SHOT3);

		while(Obj_GetSpeed(obj) > 0) {
			Obj_SetSpeed(obj, Obj_GetSpeed(obj) - s_gain);
			yield;
		}
		
		Obj_SetSpeed(obj, 0);
		yield;
		
		let ox = Obj_GetX(obj); let oy = Obj_GetY(obj);

		loop(60) { yield; }
		ObjShot_FadeDelete(obj);
		if (ox < GetClipMinX || ox > GetClipMaxX || oy < GetClipMinY || oy > GetClipMaxY)
			{ break; }
		
		//自動発射ショット
		PlaySE(SE_BELL);
		let ang = atan2(GetPlayerY-oy, GetPlayerX-ox);
		loop(48)
		{
			CreateShot01(ox, oy, speed2, ang, graphic2, 0);
			ang += 20;
			yield;
		}

	}

	task shot_kyokusen(x, y, a_gain, a_offset, speed, muki, graphic, wt, kyori, c_flame, a_change)
	{
		let t = 0;
		loop
		{
			let dx = x+cos(t)*kyori*muki;
			let dy = y+(sin(t)+cos(t*2/3))*kyori/3;
			let angle = atan( -(cos(t)-sin(t*2/3)*2/3) / (sin(t)*muki) / 3 );	//微分→角度へ
			if (sin(t)*muki > 0) { angle += 180; }	//進行方向を反映
			angle += a_offset;
			
			CreateShotA(1, dx, dy, 10);//shot1作製
			SetShotDataA(1, 0,  speed, angle,       0, 0, 0, graphic);
			SetShotDataA(1, c_flame,  NULL, NULL,-a_change, 0, 0, graphic);
			SetShotDataA(1, c_flame*3, NULL, NULL,        0, 0, 0, graphic);
			
			CreateShotA(2, 0, 0, 0);
			SetShotDataA(2, 0, speed, angle,   a_change, 0, 0, graphic);
			SetShotDataA(2, c_flame, speed, angle, -a_change, 0, 0, graphic);
			SetShotDataA(2, c_flame*2, NULL, NULL,          0, 0, 0, graphic);

			CreateShotA(3, 0, 0, 0);
			SetShotDataA(3, 0, speed, angle-a_change*c_flame, a_change, 0, 0, graphic);
			SetShotDataA(3, c_flame, NULL,              NULL,        0, 0, 0, graphic);
			AddShot(c_flame*2, 1, 3, 0);

			CreateShotA(4, 0, 0, 0);
			SetShotDataA(4, 0, speed, angle+a_change*c_flame, a_change, 0, 0, graphic);
			SetShotDataA(4, 60, NULL,              NULL,        0, 0, 0, graphic);
			AddShot(c_flame, 2, 4, 0);

			AddShot(60,1,2,0);
			FireShot(1);

			t += a_gain;
			wait(wt);
		}
	}
}
