e
#Title[uƂ̖v]
#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() + 120);

		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(55, 2000000, "uƂ̖v");
		SetInvincibility(150);
		T_DamageRate(20, 0, 300, -1);

		SetAction(ACT_SPELL, 60);

		Wait(150);
		SetHitState(1);

		T_Move();
		T_Shot();
	}

	task T_Move()
	{
		Wait(240);
		loop{
			SetAction(ACT_MOVE, 75);
			MoveActOwn(115, 125);
			Wait(120);
		}
	}

	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, 60)));
		y = max(ymin, min(ymax, GetY() + rand(-30, 30)));
		SetMovePositionDC(x, y, 75);
	}

	task T_Shot()
	{
		let angle = -90;
		let count = -20;

		loop{
			let wid = 0;
			let v_angle = 0;
			if( count >= 0 ){
				wid = 0.22 * (1 - cos((count * 6) % 360));
				v_angle = wid * sin((count * 45) % 360);
			}
			PlaySE(se_shot1);
			ascent( let i in 0..15 ){
				let s_angle = angle + v_angle * 20 + i * 360 / 15;
				CreateCurveShot(GetX() + 30 * cos(s_angle), GetY() + 30 * sin(s_angle), s_angle, v_angle, [53, 54, 58][i % 3]);
			}
			Wait(4);
			count ++;
		}
	}

	task CreateCurveShot(x, y, angle, v_angle, type)
	{
		let obj = Obj_Create(OBJ_SHOT);

		Obj_SetPosition(obj, x, y);
		ObjShot_SetGraphic(obj, type);
		ObjShot_SetDelay(obj, 5);
		Wait(5);
		//ObjShot_SetBombResist(obj, true);

		let v = 0.8;
		let a = 0.017;
		let a_angle = -v_angle / 120;
		let count = 0;

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

			Obj_SetSpeed(obj, v);
			Obj_SetAngle(obj, angle);
			yield;
		}
	}

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