#東方弾幕風
#Title[水符「幻想郷の海」]
#Text[test]
#ScriptVersion[2]

script_enemy_main {
	let name	   = "水符「幻想郷の海」";

	// 初期位置
	let xIni	   = GetCenterX;
	let yIni	   = GetClipMinY + 120;
	
	// ムラサ初期設定
	#include_function ".\lib\setting_murasa.txt"
	// shot_all.png
	#include_function ".\lib_usershot.txt"

	let D_RATE = 100;
	let D_WAIT = 240;

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

		SetLife(3000);
		SetTimer(50);
		SetScore(1500000);
		SetDamageRate(5, 5);

		Ini_Graphic;

		TMain;
	}

	@MainLoop {
		//位置を記録
		SetCommonData("MurasaX", GetX);
		SetCommonData("MurasaY", GetY);

		SetCollisionA(GetX, GetY, 32);
		SetCollisionB(GetX, GetY, 16);

		yield;
	}

	@DrawLoop {
		Draw_Murasa;
	}

	@Finalize {
		finish_bakuhatu;
		if (!IsTimeOut()) { makeTokutenItem(15); }

		ClearCommonDataEx("MurasaX");
		ClearCommonDataEx("MurasaY");

//		DeleteGraphic(imgBoss);
	}

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

		TRate;
		standBy;

		//弾
		SetShotDirectionType(ABSOLUTE);
		TShot_Wave;
		wait(60);
		TShot_Ikari;
	}

	//波ワインダー
	task TShot_Wave()
	{
		let i = 0;
		loop
		{
			let x = rand(GetClipMinX,GetClipMaxX);
			if (rand_int(0,2) == 0)
			{
				shot_seatama(x, 0, 2+sin(x+i)+cos(x+i/4), SHOT_WATER_AQUA, 20, 1);
				shot_seatama(x, 0, 2+cos(x-i)+sin(x-i/3), SHOT_WATER_AQUA, 20, 0);
			}
			else
			{
				shot_seatama(x, 0, 2+sin(x+i)+cos(x+i/4), SHOT_WATER_BLUE, 20, 1);
				shot_seatama(x, 0, 2+cos(x-i)+sin(x-i/3), SHOT_WATER_BLUE, 20, 0);
			}
			if (i%8 == 0)
			{
				StopSE(SE_WATER);
				PlaySE(SE_WATER);
			}
			i++;
			yield;
		}
	}

	//イカリ投げ
	task TShot_Ikari()
	{
		loop
		{
			splash_hishaku(1);
			CreateShotFromScript("Ikari",GetX(),GetY(),6,GetAngleToPlayer(),60,0);
			loop(60) { yield; }
			splash_hishaku(2);
			loop(60) { yield; }
			splash_hishaku(0);

			moveBoss(40, 90, 15, 40, 30, 2, 64, 60, 150);

			loop(75) { yield; }
		}
	}
	
	//波っぽいワインダー、単発分
	function shot_seatama(x, x_spd, y_spd, graphic, wt, type)
	{
		if (type == 0)
			{ CreateShot12(x, GetClipMinY, x_spd, y_spd, 0, -0.033, 0, -y_spd, graphic, wt); }
		else
			{ CreateShot12(x, GetClipMaxY, x_spd, -y_spd, 0, 0.033, 0, y_spd, graphic, wt); }
	}
}

//イカリ弾
script_shot Ikari
{
	let SPEED_MAX = GetSpeed();	//スピード最大値
	let SPEED_REVERCE = -3;		//戻り速度
	let STOPPER = 90;		//停止時間
	let SPEED_GAIN = SPEED_MAX/80;	//加速度
	let CHILD_TIMER = 9;		//小弾を産む間隔
	let DELAY = GetDelayTime();
	let SIZE = 50;
	let reverse_flag = 0;		//進行方向管理用

	let sx = GetX(); let sy = GetY();
	let speed = 0;
	let scale = 0;
	let ct = CHILD_TIMER/3;
	let t = 0;
	let SOUND_FLAG = 0;

	let imgShot	= GetCurrentScriptDirectory() ~ "\img\ikari.png";

	//応急処置
	let US_BALL_M_A_BLUE	= 46;

	#include_function ".\lib\lib_se.txt"

	@Initialize
	{
		LoadGraphic(imgShot);
		SetTexture(imgShot);
		SetGraphicRect(  0,   0, 128, 111);
	}
	
	@MainLoop
	{
		if(OnDelay()){
			scale += 2/DELAY; if (scale > 1) { scale = 1; }
			SetSpeed(0);

			if (SOUND_FLAG <= 0)
			{
				PlaySE(SE_KITEKI);
				SOUND_FLAG = 1;
			}
		}
		else
		{
			SetCollisionB(GetX, GetY, 50);	//当たり判定
			
			//バックオーライ
			if (reverse_flag == 1)
			{
				SetAngle(atan2(GetY-GetCommonDataDefault("MurasaY", 0), GetX-GetCommonDataDefault("MurasaX", 0)));
				SetSpeed(SPEED_REVERCE);
				//発射位置に近づいたら消す
				if (absolute(GetX()-GetCommonDataDefault("MurasaX", 0)) <= 12 && absolute(GetY()-GetCommonDataDefault("MurasaY", 0)) <= 12) { End(); }
			}
			//画面端で停止
			else if (GetX() >= GetClipMaxX-SIZE || GetY() >= GetClipMaxY-SIZE || GetX() <= GetClipMinX+SIZE || GetY() <= GetClipMinY+SIZE)
			{
				if (SOUND_FLAG <= 1)
				{
					PlaySE(SE_WATER2);
					PlaySE(SE_DON19);
					SOUND_FLAG = 2;
				}

				SetSpeed(0);
				STOPPER--;
				if (STOPPER <= 0) {
					reverse_flag = 1;
				}
			}
			//そのまま
			else
			{
				speed += SPEED_GAIN; if (speed > SPEED_MAX) { speed = SPEED_MAX; }
				SetSpeed(speed);

				//停止するまで弾を撃つ
				if (ct <= 0)
				{
					let tmp_s = -180+GetSpeed()/SPEED_MAX*150;
					ascent(let tmp_n in 0..3)
					{
						let tmp_muki = 1;
						loop(2)
						{
							CreateShot02(GetX, GetY, 0, GetAngle+tmp_s*tmp_muki, 1/(40*(1+tmp_n)), 2, US_BALL_M_A_BLUE, 10);
							tmp_muki = -tmp_muki;
						}
					}

					ascent(let tmp_n in 0..2)
					{
						let tmp_muki = 1;
						loop(2)
						{
							CreateShot01(GetX, GetY, 0.5+tmp_n*0.3, GetAngle+tmp_s*tmp_muki, US_BALL_M_A_BLUE, 10);
							tmp_muki = -tmp_muki;
						}
					}
					ct = CHILD_TIMER;
				}
				ct--;
			}
		}
	}
	@DrawLoop
	{
		if(OnDelay()){ SetGraphicScale(scale, scale); }
		SetRenderState(ADD);
		SetGraphicAngle(0, 0, 270+GetAngle);
		DrawGraphic(GetX, GetY);
	}
	@Finalize
	{
	}
}