#ScriptVersion[2]

script_enemy_main
{
	#include_function ".\init.txt"

	let sub_id = GetArgument();
	let f_shot = 1;

	@Initialize
	{
		SetLife(9999);
		T_Main();
	}

	@Finalize
	{
		DeleteSubEnemy(sub_id);
	}

	@DrawLoop
	{
	}

	task T_Main()
	{
		yield;

		T_ShotA();
		//T_ShotB();

		loop{
			let label = ToString(sub_id);
			SetX(GetCommonData("sub_x" ~ label));
			SetY(GetCommonData("sub_y" ~ label));
			if( GetCommonData("sub_state" ~ label) == 3 ){
				f_shot = 0;
				break;
			}
			yield;
		}
		Wait(300);
		VanishEnemy();
	}

	task T_ShotA()
	{
		let angle = rand(0, 360);
		loop(40){
			PlaySE(se_shot1);
			ascent( let i in 0..10 ){
				CreateRotateShot(GetX(), GetY(), angle + i * 360 / 10, 1);
			}
			angle -= 7.3;
			Wait(8);
			if( !f_shot ){
				break;
			}
		}
	}

	task T_ShotB()
	{
		Wait(60);
		loop(10){
			PlaySE(se_shot2);
			ascent( let i in 0..4 ){
				let angle = GetAngleToPlayer();
				ascent( let j in 0..3 ){
					CreateShot02(GetX(), GetY(), 4.0, angle + (j - 1) * 20, 0.0, 10.0, 178, 0);
				}
			}
			Wait(30);
			if( !f_shot ){
				break;
			}
		}
	}

	task CreateRotateShot(x, y, angle, k)
	{
		let obj = Obj_Create(OBJ_SHOT);
		let v = 1.0;
		let a = 0.04;
		let v_angle = 1.0 * k;
		let a_angle = -v_angle / 120;
		let count = 0;

		Obj_SetPosition(obj, x, y);
		ObjShot_SetGraphic(obj, 55);
		ObjShot_SetDelay(obj, 5);
		Wait(5);

		while( !Obj_BeDeleted(obj) ){
			v += a;
			if( count < 120 ){
				angle += v_angle;
				v_angle += a_angle;
			}
			Obj_SetSpeed(obj, v);
			Obj_SetAngle(obj, angle);
			count ++;
			yield;
		}
	}

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