e
#Title[u~V~[Vv]
#Text[]
#BackGround[DEFAULT]
#Player[FREE]
#ScriptVersion[2]

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

	@Initialize
	{
		SetLife(800);
		StartSetting(60, 1000000, "u~V~[Vv");
		SetInvincibility(240);
		SetExMode();
		SetHitState(0);

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

		LoadGraphic(img_range);
		LoadGraphic(img_magic);

		LoadUserShotData(shot_00);
		SetShotAutoDeleteClip(128, 64, 128, 128);

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

		T_Main();
		T_DamageRate(20, 20, 300, -1);
		T_HitTest(48);
	}

	@Finalize
	{
		CreateItemCircle(GetX(), GetY(), 20, 50);
		CreateItem(ITEM_BOMB, GetX(), GetY());
		DeleteGraphic(img_range);
		DeleteGraphic(img_magic);
		DeleteSE(se_shot1);
		DeleteSE(se_shot2);
		ClearBG();
	}

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

	task T_Main()
	{
		yield;

		SetAction(ACT_SPELL, 60);

		Wait(180);
		SetHitState(1);

		T_Snipe();
		T_Circle();
	}

	task T_Snipe()
	{
		Wait(60);
		loop{
			PlaySE(se_shot1);
			CreateShot01(GetX(), GetY(), 2.2, GetAngleToPlayer(), 208, 0);
			Wait(15);
		}
	}

	task T_Circle()
	{

		loop{
			PlaySE(se_shot2);
			SetAction(ACT_SHOT_B, 60);
			CreateEnemyFromScript("magic", GetX(), GetY(), 0, 0, [GetClipMinX() - 32 + rand(-24, 24), GetClipMinY() + 32, 0, 0, 300, -200, 0]);
			Wait(360);
			PlaySE(se_shot2);
			SetAction(ACT_SHOT_B, 60);
			CreateEnemyFromScript("magic", GetX(), GetY(), 0, 0, [GetClipMaxX() + 32 + rand(-24, 24), GetClipMinY() + 32, 180, 0, 300, 200, 0]);
			Wait(360);
		}
	}

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

script_enemy magic
{
	#include_function ".\init.txt"

	let arg;
	let rot = rand(0, 360);
	let f_turn = 0;
	let f_stop = 0;

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

		arg = GetArgument();

		T_Main();
	}

	@Finalize
	{
	}

	@DrawLoop
	{
		SetTexture(img_magic);
		SetGraphicRect(64, 64, 128, 128);
		SetAlpha(128);
		SetGraphicAngle(0, 0, rot);
		DrawGraphic(GetX(), GetY());
		SetGraphicAngle(0, 0, 0);
	}

	task T_Main()
	{
		yield;

		T_Move();
		T_Shot();

		loop{
			if( IsOutOfClipRange(GetX(), GetY(), 64) ){
				f_stop = 1;
				Wait(80);
				VanishEnemy();
			}
			rot += 2;
			yield;
		}
	}

	task T_Move()
	{
		let x = GetX();
		let y = GetY();
		let tx = arg[0];
		let ty = arg[1];
		let v = hypot(ty - GetY(), tx - GetX()) / 30;
		let angle = atan2(ty - GetY(), tx - GetX());
		let a = -v / 60;
		v += a / 2;

		loop(60){
			x += v * cos(angle);
			y += v * sin(angle);
			v += a;
			SetX(x);
			SetY(y);
			yield;
		}

		v = 2.0;
		angle = arg[2];

		while( !f_stop ){
			let radx = cos(angle);
			x += v * radx;
			y += v * sin(angle);
			SetX(x);
			SetY(y);
			if( radx < 0 && x <= GetClipMinX() + 32 ){
				angle = 90;
				y += GetClipMinX() + 32 - x;
				x = GetClipMinX() + 32;
				f_turn = 1;
			}
			else if( radx > 0 && x >= GetClipMaxX() - 32 ){
				angle = 90;
				y += x - (GetClipMaxX() - 32);
				x = GetClipMaxX() - 32;
				f_turn = 1;
			}
			yield;
		}
	}

	task T_Shot()
	{
		Wait(60);
		while( !f_stop ){
			if( !f_turn ){
				CreateCircle(GetX(), GetY(), arg[3], arg[4], 54);
			}
			else{
				CreateCircle(GetX(), GetY(), arg[5], arg[6], 54);
			}
			Wait(24);
		}
	}

	task CreateCircle(cx, cy, tdx, tdy, type)
	{
		let angle = rand(0, 360);
		let tx = cx + tdx;
		let ty = cy + tdy;
		let v = 1.6;
		loop(18){
			CreateCircleShot(cx, cy, v, angle, tx, ty, type);
			angle += 360 / 18;
		}
	}

	task CreateCircleShot(x, y, tv, angle, tx, ty, type)
	{
		let obj = Obj_Create(OBJ_SHOT);
		let v = 4.0;
		let a = -v / 40;

		Obj_SetPosition(obj, x, y);
		Obj_SetAngle(obj, angle);
		ObjShot_SetGraphic(obj, type);
		ObjShot_SetDelay(obj, 10);
		Wait(10);
		loop(40){
			v += a;
			Obj_SetSpeed(obj, v);
			yield;
		}
		Wait(30);

		Obj_SetAngle(obj, atan2(ty - Obj_GetY(obj), tx - Obj_GetX(obj)));
		Obj_SetSpeed(obj, tv);
	}

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