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(500);
		SetExMode();
		SetHitState(0);

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

		LoadGraphic(img_chen);

		LoadUserShotData(shot_00);
		SetShotAutoDeleteClip(64, 64, 64, 64);

		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, "\u֑唭v");
		SetInvincibility(150);
		T_DamageRate(20, 0, 240, -1);

		SetAction(ACT_SPELL, 60);

		Wait(120);
		SetHitState(1);

		T_Move();
		T_Winder();
		T_Circle();
	}

	task T_Move()
	{
		Wait(80);
		loop{
			SetAction(ACT_MOVE, 60);
			SetMovePositionDC(GetClipMinX() + rand(120, 135), GetClipMinY() + rand(90, 110), 60);
			Wait(90);
			SetAction(ACT_MOVE, 60);
			SetMovePositionDC(GetClipMaxX() - rand(120, 135), GetClipMinY() + rand(90, 110), 60);
			Wait(90);
		}
	}

	task T_Winder()
	{
		let count = -40;
		let width = 300;
		let way = 13;

		loop{
			let v_angle = 9 * sin(count * 18.5);

			if( count < -20 ){
				width -= (300 - 43) / 20;
			}
			PlaySE(se_shot1);
			ascent( let i in 0..way ){
				let angle = 90 + width / 2 + v_angle + i * (360 - width) / (way - 1);
				CreateShot02(GetX() + 40 * cos(angle), GetY() + 40 * sin(angle), 6.0, angle, 0, 0, 87, 6);
			}
			Wait(2);
			count ++;
		}
	}

	task T_Circle()
	{
		loop{
			PlaySE(se_shot2);
			CreateRotateCircle(GetX(), GetY(), -0.4);
			Wait(60);
			PlaySE(se_shot2);
			CreateRotateCircle(GetX(), GetY(), 0.4);
			Wait(60);
		}
	}

	task CreateRotateCircle(x, y, d_angle)
	{
		let w = 19;
		let c = 6;
		let n = w * c;
		let obj_s = [];

		ascent( let i in 0..n ){
			obj_s = obj_s ~ [Obj_Create(OBJ_SHOT)];
			ObjShot_SetGraphic(obj_s[i], 201);
			Obj_SetAutoDelete(obj_s[i], true);
		}

		let r = 0;
		let angle_s = rand(0, 360);
		let v = 1.8;
		let a = 0.03;
		let max_v = 3.0;
		let del_num = 0;

		while( del_num < n ){
			r += v;
			v = min(v + a, 3.0);

			let idx = 0;

			del_num = 0;
			ascent( let j in 0..w ){
				let angle_sb = angle_s + j * 360 / w;
				ascent( let i in 0..c ){
					if( Obj_BeDeleted(obj_s[idx]) ){
						del_num ++;
					}
					else{
						let angle_st = angle_sb + i * 2.2;
						Obj_SetPosition(obj_s[idx], x + r * cos(angle_st), y + r * sin(angle_st));
					}
					idx ++;
				}
			}
			angle_s += d_angle;
			yield;
		}
	}

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