e
#Title[uދyo[jOv]
#Text[]
#BackGround[DEFAULT]
#Player[FREE]
#ScriptVersion[2]

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

	let draw_rot = 0;

	@Initialize
	{
		SetLife(450);
		SetExMode();
		SetHitState(0);

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

		LoadGraphic(img_chen);

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

		InitializeAction();
		InitializeBG();
		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);
			}
		}
		CreateItem(ITEM_1UP, GetX(), GetY());
		DeleteGraphic(img_chen);
		DeleteSE(se_shot1);
		DeleteSE(se_shot2);
		ClearBG();
	}

	@DrawLoop
	{
		if( action == ACT_MOVE && absolute(GetSpeed()) >= 0.1 ){
			if( cos( GetAngle() ) <= 0 ){
				draw_rot -= 30;
			}
			else{
				draw_rot += 30;
			}
		}
		else{
			draw_rot = 0;
		}
		SetGraphicAngle(0, 0, draw_rot);
		SetAlpha([255, 128][IsExMode() && OnBomb()]);
		DrawBoss(img_chen);
		SetGraphicAngle(0, 0, 0);
	}

	task T_Main()
	{
		yield;

		SetAction(ACT_MOVE, 60);
		Wait(60);
		StartSetting(99, 2000000, "uދyo[jOv");
		SetInvincibility(150);
		T_DamageRate(20, 0, 300, -1);

		SetAction(ACT_SPELL, 60);

		Wait(120);
		SetHitState(1);

		T_Snipe();
	}

	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 + 20 ){
			k = 1;
		}
		else if( GetX() > xmax - 20 ){
			k = -1;
		}
		else if( GetX() < GetPlayerX() ){
			k = 1;
		}
		else{
			k = -1;
		}
		x = max(xmin, min(xmax, GetX() + k * rand(40, 80)));
		y = max(ymin, min(ymax, GetY() + rand(-30, 30)));
		SetMovePositionDC(x, y, 60);
	}

	task T_Snipe()
	{
		let angle = 0;
		let inter = 13.5;
		let count = 0;
		let k = 1;
		loop{
			PlaySE(se_concent);
			SetColor(255, 64, 64);
			Concentration01(120);
			SetColor(255, 255, 255);
			Wait(120);
			PlaySE(se_shot1);

			let at_ii = atan2(GetClipMinY() - GetY(), GetClipMinX() - GetX());
			let at_ai = atan2(GetClipMinY() - GetY(), GetClipMaxX() - GetX());
			let at_aa = atan2(GetClipMaxY() - GetY(), GetClipMaxX() - GetX());
			let at_ia = atan2(GetClipMaxY() - GetY(), GetClipMinX() - GetX());
			let at_tr = GetSnipeAngle(GetX(), GetY());
			let tx;
			let ty;
			let d = 12;

			if( at_tr < at_ii ){
				tx = GetClipMinX() + d;
				ty = GetY() + sin(at_tr) / cos(at_tr) * (GetClipMinX() + d - GetX());
			}
			else if( at_tr < at_ai ){
				tx = GetX() + cos(at_tr) / sin(at_tr) * (GetClipMinY() + d - GetY());
				ty = GetClipMinY() + d;
			}
			else if( at_tr < at_aa ){
				tx = GetClipMaxX() - d;
				ty = GetY() + sin(at_tr) / cos(at_tr) * (GetClipMaxX() - d - GetX());
			}
			else if( at_tr < at_ia ){
				tx = GetX() + cos(at_tr) / sin(at_tr) * (GetClipMaxY() - d - GetY());
				ty = GetClipMaxY() - d;
			}
			else{
				tx = GetClipMinX() + d;
				ty = GetY() + sin(at_tr) / cos(at_tr) * (GetClipMinX() + d - GetX());
			}

			SetAction(ACT_MOVE, 72);
			SetMovePositionDC(tx, ty, 72);

			let angle = rand(0, 360);
			loop(9){
				CreatePutShot(GetX(), GetY(), angle);
				angle += 360 / 22 / 2;
				Wait(8);
			}
			Wait(60);
		}
	}

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

		Obj_SetPosition(obj, x, y);
		Obj_SetAngle(obj, angle);
		ObjShot_SetGraphic(obj, 41);

		Wait(60);

		ascent( let i in 0..24 ){
			CreateShot02(x, y, 0.0, angle + i * 360 / 24, 0.04, 2.0, 201, 0);
		}
		Obj_Delete(obj);
	}

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