東方弾幕風
#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(750);
		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);
			}
		}
		CreateItem(ITEM_1UP, GetX(), GetY());
		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, "嵐符「暴風ときどき雷警報」");
		SetInvincibility(150);
		T_DamageRate(20, 0, 300, -1);

		SetAction(ACT_SPELL, 60);

		Wait(150);
		SetHitState(1);

		T_Snipe();
		T_Shot();
	}

	task T_Snipe()
	{
		loop{
			PlaySE(se_shot1);
			CreateShot01(GetX(), GetY(), 2.6, GetAngleToPlayer(), 43, 0);
			Wait(30);
		}
	}

	task T_Shot()
	{
		let k = 0;
		loop{
			CreateWind(1, GetClipMaxX());
			Wait(80);
			SetAction(ACT_MOVE, 60);
			SetMovePositionDC(GetClipMinX() + rand(80, 130), GetClipMinY() + 80, 60);
			Wait(60);
			SetAction(ACT_SHOT_B, 100);
			Wait(10);
			PlaySE(se_shot2);
			let num = 10;
			let inter = (GetClipMaxX() - GetClipMinX()) / (num - 1);
			let lx = GetClipMaxX() + rand(-inter / 2, inter / 2);
			ascent( let i in 0..num ){
				CreateThunderLaser(lx - inter * i);
				Wait(3);
			}
			Wait(60);

			CreateWind(-1, GetClipMinX());
			Wait(80);
			SetAction(ACT_MOVE, 60);
			SetMovePositionDC(GetClipMaxX() - rand(80, 130), GetClipMinY() + 80, 60);
			Wait(60);
			SetAction(ACT_SHOT_B, 100);
			Wait(10);
			PlaySE(se_shot2);
			let num = 10;
			let inter = (GetClipMaxX() - GetClipMinX()) / (num - 1);
			let lx = GetClipMinX() + rand(-inter / 2, inter / 2);
			ascent( let i in 0..num ){
				CreateThunderLaser(lx + inter * i);
				Wait(3);
			}
			Wait(60);
		}
	}

	task CreateWind(k, bx)
	{
		PlaySE(se_concent);
		let shot = 0;
		ascent( let j in 0..7 ){
			let num = 16;
			let dt = 620 / num;
			let t = -240 + (j % 2) * dt / 2;
			ascent( let i in 0..num ){
				let x = bx + k * t * cos(75);
				let y = GetClipMinY() + t * sin(75);
				CreateWindShot(x, y, k, 54);
				t += dt;
			}
			Wait(20);
		}
		/*ascent( let j in 0..220 ){
			let rate = 140 - abs(j - 110);
			shot += rate;
			let num = trunc(shot / 140);
			shot -= num * 120;
			ascent( let i in 0..num ){
				let t = rand(-240, 380);
				let x = bx + k * t * cos(75);
				let y = GetClipMinY() + t * sin(75);
				CreateWindShot(x, y, k, [54, 24][((j * 2 + i) % 7 == 0)]);
			}
			Wait(1);
		}*/
	}

	task CreateWindShot(x, y, k, type)
	{
		let obj = Obj_Create(OBJ_SHOT);

		Obj_SetPosition(obj, x, y);
		Obj_SetAutoDelete(obj, false);
		ObjShot_SetGraphic(obj, type);
		ObjShot_SetDelay(obj, 5);
		Wait(5);

		let v = 1.8;
		let a = 0.010;
		let angle = 90 + k * 45;
		let v_angle = k * rand(0.20, 0.22);
		let a_angle = -v_angle / 180;
		let count = 0;

		while( !Obj_BeDeleted(obj) ){
			v += a;
			if( count < 180 ){
				angle += v_angle;
				v_angle += a_angle;
			}
			if( count == 210 ){
				Obj_SetAutoDelete(obj, true);
			}
			count ++;

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

	task CreateThunderLaser(x)
	{
		let obj_e = Obj_Create(OBJ_LASER);

		Obj_SetPosition(obj_e, x, GetClipMinY());
		Obj_SetAngle(obj_e, 90);
		ObjLaser_SetSource(obj_e, false);
		ObjLaser_SetLength(obj_e, 480);
		ObjLaser_SetWidth(obj_e, 32);
		ObjShot_SetGraphic(obj_e, 188);
		ObjShot_SetDelay(obj_e, 999);
		//ObjShot_SetBombResist(obj_e, true);
		Obj_SetAutoDelete(obj_e, false);
		Wait(30);

		if( OnPlayerMissed() || OnBomb() ){
			Obj_Delete(obj_e);
			return;
		}

		let obj = Obj_Create(OBJ_LASER);
		let len = 800;
		let y = GetClipMinY() - 50 - len;
		let v = len / 20;

		Obj_SetPosition(obj, x, y);
		Obj_SetAngle(obj, 90);
		ObjLaser_SetSource(obj, false);
		ObjLaser_SetLength(obj, len);
		ObjLaser_SetWidth(obj, 32);
		ObjShot_SetGraphic(obj, 229);
		//ObjShot_SetBombResist(obj, false);
		Obj_SetAutoDelete(obj, false);

		loop(20){
			if( Obj_BeDeleted(obj) ){
				break;
			}
			y += v;
			Obj_SetPosition(obj, x, y);
			ObjLaser_SetLength(obj, len);
			yield;
		}
		Wait(20);

		Obj_Delete(obj);
		Obj_Delete(obj_e);
	}

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