#東方弾幕風
#Title[財宝「ゴールデンバブル」]
#Text[test]
#ScriptVersion[2]

script_enemy_main {
	let name	= "財宝「ゴールデンバブル」";

	// shot_all.png
	#include_function ".\lib_usershot.txt"
	// 星さん初期設定
	#include_function ".\lib\setting_tora.txt"

	// 初期位置
	let xIni	= GetCenterX;
	let yIni	= GetCenterY-16;
	
	let D_RATE = 28;
	let D_WAIT = 600;

	@Initialize {
		CutIn(YOUMU, name, "", 0, 0, 0, 0);

		SetLife(1350);
		SetTimer(65);
		SetScore(1500000);
		SetDamageRate(5, 5);

		Ini_Graphic;
		LoadGraphic(imgHoutou);

		//星の座標を記録
		SetCommonData("ToraX", xIni);
		SetCommonData("ToraY", yIni);

		TMain;
	}

	@MainLoop {
		//ナズ＆星の当たり判定を取得
		SetCollisionA(GetCommonDataDefault("NazuX", -100), GetCommonDataDefault("NazuY", -100), 32);
		SetCollisionB(GetCommonDataDefault("NazuX", -100), GetCommonDataDefault("NazuY", -100), 13);
		SetCollisionA(GetX, GetY, 32);
		SetCollisionB(GetX, GetY, 13);

		yield;
	}

	@DrawLoop {
		Draw_Toramaru;
	}

	@Finalize {
		if (!IsTimeOut()) { makeTokutenItem(10); }

		ClearCommonDataEx("NazuX");
		ClearCommonDataEx("NazuY");
		ClearCommonDataEx("ToraX");
		ClearCommonDataEx("ToraY");

		finish_bakuhatu;
//		DeleteGraphic(imgBoss);
	}

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

		//ナズーリン
		CreateEnemyFromScript("Nazu", GetCommonDataDefault("NazuX", -100), GetCommonDataDefault("NazuY", -100), 0, 90, 0);

		TRate;
		standBy;
		effect_toramaru;

		TShot;
	}

	//弾射出タスク
	task TShot()
	{
		wait(30);
		ATTACK_FLAG = 1;
		
		let wt = 180;
		let t = 0;
		loop
		{
			let gra = [US_COIN_SILVER, US_COIN_COPPER][t%2];
			let gra2 = [US_BALL_F_YELLOW, US_BALL_SS_YELLOW][t%2];
			shot_from_kabe(0, 5, 6, 0, 60, gra, gra2);
			shot_from_kabe(1, 5, 6, 0, 60, gra, gra2);
			shot_from_kabe(2, 5, 6, 0, 60, gra, gra2);
			shot_from_kabe(3, 5, 6, 0, 60, gra, gra2);
			t++;
			if (GetLife < 400) { wt = 90; }
			else if (GetLife < 800) { wt = 135; }
			wait(wt);
		}
	}

	//弾射出タスク
	task shot_from_kabe(kabe, way, way2, sukima, wt, graphic, graphic2)
	{
		let WAY_TMP  = 0; let WAY_TMP2 = 0;
		let x = 0; let y = 0; let CLIP_MAX = 0;
		
		if (kabe == 0 || kabe == 1)	//左右
		{
			WAY_TMP = (GetClipMaxY-GetClipMinY)/way;
			WAY_TMP2 = (WAY_TMP*(1-sukima))/way2;
			CLIP_MAX = (GetClipMaxX-GetClipMinX);
			if (kabe == 0)  { x = GetClipMinX-5; }	//左
			else  { x = GetClipMaxX+5; }	//右
		}
		else
		{
			WAY_TMP = (GetClipMaxX-GetClipMinX)/way;
			WAY_TMP2 = (WAY_TMP*(1-sukima))/way2;
			CLIP_MAX = (GetClipMaxY-GetClipMinY);
			if (kabe == 2)  { y = GetClipMinY-5; }	//上
			else { y = GetClipMaxY+5; }	//下
		}
		
		ascent(let n in 0 .. way2)
		{
			ascent(let m in 0 .. way)
			{
				let tmp = 0;
				if (kabe == 0 || kabe == 3) { tmp = WAY_TMP*n + WAY_TMP2*m; }
				else { tmp = CLIP_MAX - (WAY_TMP*n + WAY_TMP2*m); }
				if (kabe == 0 || kabe == 1) { y = tmp; }
				else { x = tmp; }
				shot_bubble(x, y, graphic, 0, 2, graphic2);
			}
			StopSE(SE_BELL);
			PlaySE(SE_BELL);
			wait(wt);
		}
	}
	
	
	//バブル弾
	//回転を交えつつ中心へ収束→中心で泡に変化、放出
	//弾幕キメラの実弾のような弾道
	task shot_bubble(x, y, graphic, delay, speed2, graphic2)
	{
		let POWER = 1/100;
		
		//オブジェクト弾の作成
		let obj = Obj_Create(OBJ_SHOT);
		
		//パラメータの設定
		Obj_SetPosition(obj, x, y);
		Obj_SetSpeed(obj, 0);
		Obj_SetAngle(obj, 0);
		ObjShot_SetGraphic(obj, graphic);
		ObjShot_SetDelay(obj, delay);
		Obj_SetAutoDelete(obj,false);
		ObjShot_SetBombResist(obj, true);
		
		let s_angle = atan2(GetY-y, GetX-x);
		
		loop(delay){ yield; }
		
		while(!Obj_BeDeleted(obj)) {
			let ox = Obj_GetX(obj)+(GetX-Obj_GetX(obj))*POWER;
			let oy = Obj_GetY(obj)+(GetY-Obj_GetY(obj))*POWER;
			Obj_SetPosition(obj, ox, oy);
			
			if (absolute(ox-GetX) < 8 && absolute(oy-GetY) < 8) {
				CreateShot01(GetX, GetY, speed2, 360-s_angle, graphic2, 5);
				ObjShot_FadeDelete(obj);
				break;
			}
			yield;
		}
	}
}

// ナズーリン
script_enemy Nazu {
	let shot_speed = GetArgument();

	// ナズ寅初期設定
	#include_function ".\lib\setting_nazutora.txt"

	let GEKIHA_FLAG = 0;
	
	let D_RATE = 0;
	let D_WAIT = 999;

	// 初期位置
	let xIni	= 0;
	let yIni	= GetCenterY-40;
	if (GetX > GetCenterX)
		{ xIni = GetCenterX+100; }
	else
		{ xIni = GetCenterX-100; }

	@Initialize {
		SetLife(9999);
		SetScore(1000);
		SetDamageRate(0, 0);		//ダメージ無効化

		SetTexture(imgBoss);
		setGraphicStop;

		TMain;
	}

	@MainLoop {
		//位置を記録
		SetCommonData("NazuX", GetX);
		SetCommonData("NazuY", GetY);
		yield;
	}

	@DrawLoop {
		Draw_Nazu;
	}

	@Finalize {
		if (!IsTimeOut()) { makeTokutenItem(8); }
	}

	task TMain {
		yield;
		standBy;
		
		TShot;
	}

	task TShot {
		wait(60);
		let WAY = 16;

		loop
		{
			let base_angle = rand(0,360);
			ascent(let n in 0 .. WAY)
			{
				shot_bubble(GetX+cos(n*360/WAY+base_angle)*32, GetY+sin(n*360/WAY+base_angle)*32, US_COIN_GOLD, 15, 1.5, US_BALL_L_YELLOW, n%2);
				StopSE(SE_SHOT2);
				PlaySE(SE_SHOT2);
				wait(2);
			}
			wait(30);
			moveBoss(120, 240, 40, 120, 40, 3, 64, 20, 240);
			wait(50);
		}
	}

	//バブル弾
	//回転を交えつつ中心へ収束→中心で泡に変化、放出
	//弾幕キメラの実弾のような弾道
	//→星を動かす場合はスクリプトをいじる必要あり
	task shot_bubble(x, y, graphic, delay, speed2, graphic2, bomb)
	{
		let POWER = 1/100;
		let T_X = GetCommonDataDefault("ToraX", 0);
		let T_Y = GetCommonDataDefault("ToraY", 0);
		
		//オブジェクト弾の作成
		let obj = Obj_Create(OBJ_SHOT);
		
		//パラメータの設定
		Obj_SetPosition(obj, x, y);
		Obj_SetSpeed(obj, 0);
		Obj_SetAngle(obj, 0);
		ObjShot_SetGraphic(obj, graphic);
		ObjShot_SetDelay(obj, delay);
		Obj_SetAutoDelete(obj,false);
		
		loop(delay){ yield; }
		
		while(!Obj_BeDeleted(obj)) {
			let ox = Obj_GetX(obj)+(T_X-Obj_GetX(obj))*POWER;
			let oy = Obj_GetY(obj)+(T_Y-Obj_GetY(obj))*POWER;
			Obj_SetPosition(obj, ox, oy);
			
			if (absolute(ox-T_X) < 8 && absolute(oy-T_Y) < 8) {
				if (bomb) { CreateShot01(T_X, T_Y, speed2, rand(0,360), graphic2, 5); }
				ObjShot_FadeDelete(obj);
				break;
			}
			yield;
		}
	}
}
