東方弾幕風
#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() + 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(), 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(55, 2000000, "奇符「迷い家の幻想迷路」");
		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, 60);
			MoveActOwn(100, 140);
			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, 80)));
		y = max(ymin, min(ymax, GetY() + rand(-30, 30)));
		SetMovePositionDC(x, y, 60);
	}

	task T_Shot()
	{
		let angle = 0;
		let count = -10;

		loop{
			let wid = 0;
			let v_angle = 0;
			if( count >= 0 ){
				wid = 0.2 * (1 - cos((count * 5) % 360));
				v_angle = wid * sin((count * 47) % 360);
				SetShotColor(255 - wid * 100, 255 - wid * 500, 255);
			}
			let d_angle = 10 * (wid + 0.4) * sin((count * 47 + 360) % 360);
			PlaySE(se_shot1);
			ascent( let i in 0..18 ){
				let s_angle = angle + d_angle + i * 360 / 18;
				CreateCurveShot(GetX() + 30 * cos(s_angle), GetY() + 30 * sin(s_angle), s_angle, v_angle, [53, 54, 58][i % 3]);
			}
			SetShotColor(255, 255, 255);
			Wait(5);
			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.9;
		let a = 0.030;
		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"
}
