搶曽抏枊晽
#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(3000);
		SetExMode();
		SetHitState(0);

		InitPosition(GetCenterX(), GetClipMinY() - 60);

		LoadGraphic(img_chen);

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

		InitializeAction();
		InitializeBG();
		LoadSE(se_shot1);
		LoadSE(se_shot2);

		T_Main();
		T_HitTest(32);
	}

	@Finalize
	{
		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(90);
		StartSetting(79, 1000000, "乽崟擫抏枊戭媫曋乿");
		T_DamageRate(20, 0, 300, -1);
		SetDurableSpellCard();
		MagicCircle(false);
		Wait(120);
		SetHitState(1);

		T_Pattern();
	}

	task T_Pattern()
	{
		//仧峌寕侾仧
		SetX(GetCenterX());
		SetY(GetClipMinY() - 60);
		PlaySE(se_concent);
		Concentration01(120);
		Concentration01(120);
		Wait(120);
		DeleteEnemyShotToItem(ALL);
		T_Move(78, 0, GetClipMinY() + 20, 0);
		let n = 13;
		ascent( let j in 0..n ){
			let angle = 180 - 180 * (j % 2);
			ascent( let i in 0..3 ){
				let s_angle = angle + (i - 1) * 30;
				ascent( let k in 0..4 ){
					CreateAccelShot2(GetX(), GetY(), 0.20 - k * 0.05, 0.030 - 0.003 * k, s_angle, 81, (n - j) * 15);
				}
			}
			Wait(15);
		}
		Wait(120);

		//仧峌寕俀仧
		SetX(GetCenterX());
		SetY(GetClipMinY() - 60);
		PlaySE(se_concent);
		Concentration01(120);
		Concentration01(120);
		Wait(120);
		DeleteEnemyShotToItem(ALL);
		T_Move(52, 1, GetClipMinY() + 20, 0);
		let n = 19;
		ascent( let j in 0..n ){
			let angle = 180 * (j % 2);
			ascent( let i in 0..3 ){
				let s_angle = angle + (i - 1) * 30;
				ascent( let k in 0..4 ){
					CreateAccelShot2(GetX(), GetY(), 0.20 - k * 0.05, 0.030 - 0.003 * k, s_angle, 81, (n - j) * 15);
				}
			}
			Wait(15);
		}
		Wait(120);

		//仧峌寕俁仧
		SetX(GetCenterX());
		SetY(GetClipMinY() - 60);
		PlaySE(se_concent);
		Concentration01(120);
		Concentration01(120);
		Wait(120);
		DeleteEnemyShotToItem(ALL);
		T_Move(83, 1, GetClipMinY() + 30, 0);
		let n = 13;
		ascent( let j in 0..n ){
			let angle = GetAngleToPlayer();
			ascent( let i in 0..5 ){
				let s_angle = angle + (i - 1) * 20;
				ascent( let k in 0..4 ){
					CreateAccelShot2(GetX(), GetY(), 0.20 - k * 0.05, 0.030 - 0.003 * k, s_angle, 83, (n - j) * 15);
				}
			}
			Wait(15);
		}
		Wait(120);

		//仧峌寕係仧
		SetX(GetCenterX());
		SetY(GetClipMinY() - 60);
		PlaySE(se_concent);
		Concentration01(120);
		Concentration01(120);
		Wait(120);
		DeleteEnemyShotToItem(ALL);
		T_Move(69, 0, GetClipMinY() + 25, 0);
		let n = 15;
		ascent( let j in 0..n ){
			let angle = GetAngleToPlayer();
			ascent( let i in 0..7 ){
				CreateShot01(GetX(), GetY(), 2.0 + i * 0.3, angle, 47, 0);
			}
			Wait(15);
		}
		Wait(120);

		//仧峌寕俆仧
		SetX(GetCenterX());
		SetY(GetClipMinY() - 60);
		PlaySE(se_concent);
		Concentration01(120);
		Concentration01(120);
		Wait(120);
		DeleteEnemyShotToItem(ALL);
		T_Move(52, 1, GetClipMinY() + 20, 0);
		let n = 19;
		let wait = 1;
		let k = 0;
		let num = trunc(n * 15 / wait);
		ascent( let j in 0..num ){
			CreateShot01(GetX(), GetY(), 4.0, GetAngleToPlayer(), 56, 0);
			Wait(wait);
			k = 1 - k;
		}
		Wait(120);

		//仧峌寕俇仧
		SetX(GetCenterX());
		SetY(GetClipMinY() - 60);
		PlaySE(se_concent);
		Concentration01(120);
		Concentration01(120);
		Wait(120);
		DeleteEnemyShotToItem(ALL);
		T_Move(82, 0, GetClipMinY() + 20, 0);
		let n = 13;
		let wait = 1;
		let num = trunc(n * 15 / wait);
		let angle = rand(0, 360);
		ascent( let j in 0..num ){
			ascent( let i in 0..3 ){
				CreateAccelShot2(GetX(), GetY(), 0.0, 0.006, angle + i * 120, 95, (num - j) * wait);
			}
			angle += 48.0;
			Wait(wait);
		}
		Wait(240);

		//仧峌寕俈仧
		SetX(GetCenterX());
		SetY(GetClipMinY() - 60);
		PlaySE(se_concent);
		Concentration01(120);
		Concentration01(120);
		Wait(120);
		DeleteEnemyShotToItem(ALL);
		T_Move(89.6, 0, GetClipMinY() + 44.8, 1);
		let n = 60;
		let wait = 2;
		let num = trunc(n * 15 / wait);
		ascent( let j in 0..num ){
			CreatePutShot(GetX(), GetY(), 1.0, GetAngleToPlayer(), 97);
			Wait(2);
		}
		DeleteEnemyShotToItem(ALL);
		Wait(30);
		let angle = rand(0, 360);
		loop{
			ascent( let i in 0..2 ){
				CreatePutShot(GetX(), GetY(), 1.4, angle + i * 180, 96);
			}
			angle -= 17.0;
			Wait(1);
		}
	}

	task CreateAccelShot1(x, y, angle, type, wait)
	{
		let obj = Obj_Create(OBJ_SHOT);

		Obj_SetPosition(obj, x, y);
		ObjShot_SetGraphic(obj, type);

		let v = 0.4;
		let a = 0.030;
		let count = 0;

		while( !Obj_BeDeleted(obj) ){
			if( count >= wait ){
				v += a;
			}
			count ++;

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

	task CreateAccelShot2(x, y, v, a, angle, type, wait)
	{
		let obj = Obj_Create(OBJ_SHOT);

		Obj_SetPosition(obj, x, y);
		ObjShot_SetGraphic(obj, type);

		let count = 0;

		while( !Obj_BeDeleted(obj) ){
			if( count >= wait ){
				v += a;
			}
			count ++;

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

	task CreateAccelShot3(x, y, angle, type, wait)
	{
		let obj = Obj_Create(OBJ_SHOT);

		Obj_SetPosition(obj, x, y);
		ObjShot_SetGraphic(obj, type);

		let v = 0.4;
		let a = 0.015;
		let count = 0;

		while( !Obj_BeDeleted(obj) ){
			if( count >= wait && count < wait + 120 ){
				v += a;
			}
			count ++;

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

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

		Obj_SetPosition(obj, x, y);
		ObjShot_SetGraphic(obj, type);

		let a = v / 60;
		let count = 0;
		let v = 0;

		while( !Obj_BeDeleted(obj) ){
			if( count >= 60 && count < 120 ){
				v += a;
			}
			count ++;

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

	task T_Move(dy, k, ty, f_topref)
	{
		SetHitState(1);

		loop{
			let f_reflect = 0;
			let tx = [GetClipMinX() + 16, GetClipMaxX() - 16][k];

			if( dy > 0 && ty > GetClipMaxY() ){
				dy = -dy;
				f_reflect = 1;
			}
			else if( f_topref && dy < 0 && ty < GetClipMinY() ){
				dy = -dy;
				f_reflect = 2;
			}
			PlaySE(se_shot1);
			SetMovePositionDC_Ref(tx, ty, 15, f_topref);
			SetAction(ACT_MOVE, 15);
			CreateOrbitLaser(GetX(), GetY(), tx, ty);
			if( f_reflect ){
				CreateOrbitLaser(GetX(), 2 * GetClipMaxY() - GetY(), tx, 2 * GetClipMaxY() - ty);
				if( f_topref == 1 ){
					CreateOrbitLaser(GetX(), 2 * GetClipMinY() - GetY(), tx, 2 * GetClipMinY() - ty);
				}
			}
			Wait(15);

			if( dy < 0 && GetY() < GetClipMinY() - 48 ){
				break;
			}
			k = 1 - k;
			if( f_reflect == 1 ){
				ty = 2 * GetClipMaxY() - ty + dy;
			}
			else if( f_reflect == 2 ){
				ty = 2 * GetClipMinY() - ty + dy;
			}
			else{
				ty += dy;
			}
		}
		SetHitState(0);
	}

	task SetMovePositionDC_Ref(tx, ty, count, f_topref)
	{
		if( count <= 0 ){
			return;
		}
		let x = GetX();
		let y = GetY();
		let vx = (tx - x) * 2 / count;
		let vy = (ty - y) * 2 / count;
		let v = hypot(vy, vx);
		let a = -v / count;
		let angle = atan2(vy, vx);
		v += a / 2;
		loop(count){
			let nvy = sin(angle);
			if( nvy > 0 && GetY() > GetClipMaxY() ){
				angle *= -1;
			}
			if( f_topref && nvy < 0 && GetY() < GetClipMinY() ){
				angle *= -1;
			}
			SetSpeed(v);
			SetAngle(angle);
			v += a;
			yield;
		}
		SetSpeed(0);
	}

	task CreateOrbitLaser(sx, sy, tx, ty)
	{
		let obj = Obj_Create(OBJ_LASER);
		let d = 2 * hypot(ty - sy, tx - sx);
		let x = 3 * sx - 2 * tx;
		let y = 3 * sy - 2 * ty;
		let vx = (tx - sx) / 15;
		let vy = (ty - sy) / 15;
		let angle = atan2(vy, vx);

		Obj_SetAngle(obj, angle);
		ObjShot_SetGraphic(obj, 222);
		Obj_SetAutoDelete(obj, false);
		ObjLaser_SetSource(obj, false);
		ObjLaser_SetLength(obj, d);
		ObjLaser_SetWidth(obj, 16);

		ascent( let i in 0..15 ){
			if( i == 10 ){
				ObjShot_FadeDelete(obj);
			}
			x += vx;
			y += vy;
			Obj_SetPosition(obj, x, y);
			yield;
		}
	}

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