東方弾幕風
#Title[交錯「事故多発インターチェンジ」]
#Text[]
#BackGround[DEFAULT]
#Player[FREE]
#ScriptVersion[2]

script_enemy_main
{
	#include_function ".\init.txt"
	#include_function ".\lib\lib_anime_Yukari.txt"

	@Initialize
	{
		SetLife(550);
		SetHitState(0);

		InitPosition(GetCenterX(), GetClipMinY() + 60);

		LoadGraphic(img_yukari);

		LoadUserShotData(shot_00);
		SetShotAutoDeleteClip(24, 24, 24, 24);

		InitializeAction();
		InitializeBG();
		DeleteEnemyShot(ALL);
		LoadSE(se_shot1);
		LoadSE(se_shot2);

		T_Main();
		T_HitTest(48);
	}

	@Finalize
	{
		if( GotSpellCardBonus() ){
			if( hypot(GetX() - GetPlayerX(), GetY() - GetPlayerY()) < 100 ){
				CreateItemCircle(GetX() - 40, GetY(), 40, 40);
				CreateItemCircle(GetX() + 40, GetY(), 40, 40);
			}
			else{
				CreateItemCircle(GetX(), GetY(), 40, 40);
			}
		}
		DeleteGraphic(img_yukari);
		DeleteSE(se_shot1);
		DeleteSE(se_shot2);
		ClearBG();
	}

	@DrawLoop
	{
		SetAlpha([255, 128][IsExMode() && OnBomb()]);
		DrawBoss(img_yukari);
	}

	task T_Main()
	{
		yield;

		StartPlural();

		SetAction(ACT_MOVE, 60);
		Wait(60);
		StartSetting(40, 2000000, "交錯「事故多発インターチェンジ」");
		SetInvincibility(150);
		T_DamageRate(20, 20, 180, -1);

		SetAction(ACT_SPELL, 60);

		Wait(150);
		SetHitState(1);

		T_ShotA();
		T_ShotB();
	}

	function MoveActOwn(ymin, ymax)
	{
		let x;
		let y;
		let k;
		let min_x = 100;
		let xmin = GetClipMinX() + min_x;
		let xmax = GetClipMaxX() - min_x;

		if( GetX() < xmin + 60 ){
			k = 1;
		}
		else if( GetX() > xmax - 60 ){
			k = -1;
		}
		else if( GetX() < GetPlayerX() ){
			k = 1;
		}
		else{
			k = -1;
		}
		x = max(xmin, min(xmax, GetX() + k * rand(35, 60)));
		y = max(ymin, min(ymax, GetY() + rand(-30, 30)));
		SetMovePositionDC(x, y, 60);
	}

	task T_ShotA()
	{
		let n = 24;

		Wait(60);
		loop{
			PlaySE(se_shot1);
			SetAction(ACT_SHOT_A, 40);
			let angle = rand(0, 360);
			ascent( let i in 0..n ){
				CreateAccelShot(GetX(), GetY(), angle + i * 360 / n);
			}
			Wait(15);

			PlaySE(se_shot1);
			angle += 180 / n;
			ascent( let i in 0..n ){
				CreateAccelShot(GetX(), GetY(), angle + i * 360 / n);
			}
			Wait(15);
			SetAction(ACT_MOVE, 60);
			MoveActOwn(GetClipMinY() + 50, GetClipMinY() + 60);
			Wait(75);
		}
	}

	task T_ShotB()
	{
		let k = 0;
		loop{
			let wait = [105, 150, 105, 150][k];
			let type1 = [201, 205, 201, 205][k];
			let type2 = [207, 204, 207, 204][k];
			let rpos = [0.05, 0.50, 0.95, 0.50][k];
			let rx = rpos * (GetClipMaxX() - GetClipMinX());
			let ry = rpos * (GetClipMaxY() - GetClipMinY());
			PlaySE(se_shot2);
			CreateSearchShot(GetClipMinX() - 24, GetClipMinY() + ry,  3.0, 0, type1, type2);
			CreateSearchShot(GetClipMaxX() + 24, GetClipMaxY() - ry, -3.0, 0, type1, type2);
			CreateSearchShot(GetClipMaxX() - rx, GetClipMinY() - 24, 0,  3.0, type1, type2);
			CreateSearchShot(GetClipMinX() + rx, GetClipMaxY() + 24, 0, -3.0, type1, type2);
			Wait(wait);
			k += 1;
			if( k > 3 ){
				k = 0;
			}
		}
	}

	task CreateAccelShot(x, y, angle)
	{
		let obj = Obj_Create(OBJ_SHOT);

		Obj_SetPosition(obj, x, y);
		ObjShot_SetGraphic(obj, 11);

		let v = 7.0;
		let a1 = -v / 15;
		let a2 = 0.02;
		let count = 0;

		while( !Obj_BeDeleted(obj) ){
			if( count < 15 ){
				v += a1;
			}
			else if( count < 180 ){
				v += a2;
			}
			count ++;

			Obj_SetSpeed(obj, v);
			Obj_SetAngle(obj, angle);
			yield;
		}
	}

	task CreateSearchShot(x, y, vx, vy, type1, type2)
	{
		let n = 15;
		let inter = 2;
		let obj = [];
		let obj_x = [];
		let obj_y = [];
		let obj_grap = [];

		ascent( let i in 0..n ){
			obj = obj ~ [Obj_Create(OBJ_SHOT)];
		}
		ascent( let i in 0..(n * inter) ){
			obj_x = obj_x ~ [x];
			obj_y = obj_y ~ [y];
			obj_grap = obj_grap ~ [type1];
		}

		let f_reflect = 0;

		loop{
			let del_num = 0;
			ascent( let i in 0..n ){
				if( Obj_BeDeleted(obj[i]) ){
					del_num ++;
				}
			}
			if( del_num == n ){
				break;
			}
			descent( let i in 1..(n * inter) ){
				obj_x[i] = obj_x[i - 1];
				obj_y[i] = obj_y[i - 1];
				obj_grap[i] = obj_grap[i - 1];
			}
			obj_x[0] = obj_x[0] + vx;
			obj_y[0] = obj_y[0] + vy;
			ascent( let i in 0..n ){
				Obj_SetPosition(obj[i], obj_x[i * inter], obj_y[i * inter]);
				ObjShot_SetGraphic(obj[i], obj_grap[i * inter]);
			}

			if( !f_reflect ){
				let f_none = 0;
				if( vx < -0.5 && obj_x[0] <= GetPlayerX() ){
					if( obj_y[0] < GetPlayerY() ){
						vy = -vx;
					}
					else{
						vy = vx;
					}
					vx = 0;
				}
				else if( vx > 0.5 && obj_x[0] >= GetPlayerX() ){
					if( obj_y[0] < GetPlayerY() ){
						vy = vx;
					}
					else{
						vy = -vx;
					}
					vx = 0;
				}
				else if( vy < -0.5 && obj_y[0] <= GetPlayerY() ){
					if( obj_x[0] < GetPlayerX() ){
						vx = -vy;
					}
					else{
						vx = vy;
					}
					vy = 0;
				}
				else if( vy > 0.5 && obj_y[0] >= GetPlayerY() ){
					if( obj_x[0] < GetPlayerX() ){
						vx = vy;
					}
					else{
						vx = -vy;
					}
					vy = 0;
				}
				else{
					f_none = 1;
				}
				if( !f_none ){
					obj_grap[0] = type2;
					ObjShot_SetGraphic(obj[0], obj_grap[0]);
					f_reflect = 1;
				}
			}
			yield;
		}
	}

	#include_function ".\f_base.txt"
	#include_function ".\f_enemy.txt"
	#include_function ".\f_boss.txt"
	#include_function ".\f_bg_yukari.txt"
}
