e
#Title[}uvv\qv]
#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(800);
		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(), 20, 40);
				CreateItemCircle(GetX() + 40, GetY(), 20, 40);
			}
			else{
				CreateItemCircle(GetX(), GetY(), 20, 40);
			}
		}
		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(60, 2000000, "}uvv\qv");
		SetInvincibility(150);
		T_DamageRate(20, 0, 240, -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()
	{
		loop{
			PlaySE(se_concent);
			SetColor(64, 255, 64);
			Concentration01(120);
			SetColor(255, 192, 64);
			Concentration01(120);
			SetColor(255, 255, 255);
			Wait(120);
			PlaySE(se_shot1);
			let angle = GetAngleToPlayer();
			SetAction(ACT_SHOT_A, 30);
			ascent( let i in 0..2 ){
				CreateSplitShot(GetX(), GetY(), angle + i * 180, 2);
			}
			Wait(180);
			SetAction(ACT_MOVE, 60);
			MoveActOwn(GetClipMinY() + 60, GetClipMinY() + 140);
			Wait(90);
		}
	}

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

		Obj_SetPosition(obj, x, y);
		Obj_SetAngle(obj, angle);
		ObjShot_SetGraphic(obj, [54, 172, 192][split]);

		if( split <= 1 ){
			Wait(60);
			PlaySE(se_shot1);
		}

		let v = 0.0;
		let a = [0.015, 0.1, 0.2][split];
		let count = 0;
		let max_v = [1.8, 4.0, 6.0][split];
		let wait = [999, 6, 5][split];

		while( !Obj_BeDeleted(obj) ){
			v = min(v + a, max_v);
			if( split > 0 && count >= 5 ){
				if( count % (wait * 2) == 0 ){
					CreateSplitShot(Obj_GetX(obj), Obj_GetY(obj), angle + 90 + rand(-15, 15), split - 1);
				}
				else if( count % (wait * 2) == wait ){
					CreateSplitShot(Obj_GetX(obj), Obj_GetY(obj), angle - 90 + rand(-15, 15), split - 1);
				}
			}
			count ++;

			Obj_SetSpeed(obj, v);
			yield;
		}
	}

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