東方弾幕風
#Title[枝符「久久能智大植林」]
#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(1000);
		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);
			}
		}
		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, "枝符「久久能智大植林」");
		SetInvincibility(150);
		T_DamageRate(20, 0, 480, -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()
	{
		PlaySE(se_concent);
		SetColor(64, 255, 64);
		Concentration01(120);
		SetColor(255, 192, 64);
		Concentration01(120);
		SetColor(255, 255, 255);
		Wait(120);

		loop{
			PlaySE(se_shot1);
			let angle = GetAngleToPlayer();
			SetAction(ACT_SHOT_A, 30);
			ascent( let i in 0..2 ){
				CreateSplitShot(GetX(), GetY(), angle + i * 180, 2, 0);
			}
			Wait(120);
			SetAction(ACT_MOVE, 60);
			MoveActOwn(GetClipMinY() + 60, GetClipMinY() + 140);
			Wait(90);

			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 - 36 + i * 180, 2, 1);
				CreateSplitShot(GetX(), GetY(), angle + 36 + i * 180, 2, 1);
			}
			Wait(120);
		}
	}

	task CreateSplitShot(x, y, angle, split, pattern)
	{
		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);
		}

		//angle = GetSnipeAngle(x, y);
		Obj_SetAngle(obj, angle);

		let v = 0.0;
		let a = [0.015, 0.1, 0.2][split];
		let count = 0;
		let max_v = [2.6, 5.0, 6.0][split];
		let wait = [999, 7, 6][split];
		let r_angle;

		if( pattern == 0 ){
			r_angle = [0, 5, 0][split];
		}
		else{
			r_angle = [0, 0, 0][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(-r_angle, r_angle), split - 1, pattern);
				}
				else if( count % (wait * 2) == wait ){
					CreateSplitShot(Obj_GetX(obj), Obj_GetY(obj), angle - 90 + rand(-r_angle, r_angle), split - 1, pattern);
				}
			}
			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"
}
