e
#Title[ucƉ̕ϊv]
#Text[]
#BackGround[DEFAULT]
#Player[FREE]
#ScriptVersion[2]

script_enemy_main
{
	#include_function ".\init.txt"
	#include_function ".\lib\lib_anime_Yukari.txt"

	@Initialize
	{
		SetLife(600);
		SetHitState(0);

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

		LoadGraphic(img_yukari);

		LoadUserShotData(shot_00);
		SetShotAutoDeleteClip(24, 24, 24, 24);

		InitializeAction();
		InitializeBG();
		DeleteEnemyShot(ALL);
		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_yukari);
		DeleteSE(se_shot1);
		DeleteSE(se_shot2);
		ClearBG();
	}

	@DrawLoop
	{
		SetAlpha([255, 128][IsExMode() && OnBomb()]);
		DrawBoss(img_yukari);
	}

	task T_Main()
	{
		yield;

		StartPlural();

		SetAction(ACT_MOVE, 60);
		Wait(60);
		StartSetting(40, 2000000, "ucƉ̕ϊv");
		SetInvincibility(150);
		T_DamageRate(20, 20, 180, -1);

		SetAction(ACT_SPELL, 60);

		Wait(150);
		SetHitState(1);

		T_ShotA();
		T_ShotB();
	}

	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 + 60 ){
			k = 1;
		}
		else if( GetX() > xmax - 60 ){
			k = -1;
		}
		else if( GetX() < GetPlayerX() ){
			k = 1;
		}
		else{
			k = -1;
		}
		x = max(xmin, min(xmax, GetX() + k * rand(35, 60)));
		y = max(ymin, min(ymax, GetY() + rand(-30, 30)));
		SetMovePositionDC(x, y, 60);
	}

	task T_ShotA()
	{
		let n = 24;

		Wait(60);
		loop{
			PlaySE(se_shot1);
			SetAction(ACT_SHOT_A, 40);
			let angle = rand(0, 360);
			ascent( let i in 0..n ){
				CreateAccelShot(GetX(), GetY(), angle + i * 360 / n);
			}
			Wait(15);

			PlaySE(se_shot1);
			angle += 180 / n;
			ascent( let i in 0..n ){
				CreateAccelShot(GetX(), GetY(), angle + i * 360 / n);
			}
			Wait(15);
			SetAction(ACT_MOVE, 60);
			MoveActOwn(GetClipMinY() + 50, GetClipMinY() + 60);
			Wait(90);
		}
	}

	task T_ShotB()
	{
		loop{
			let rpos = 0.1;//rand(0, 1);
			let rx = rpos * (GetClipMaxX() - GetClipMinX());
			let ry = rpos * (GetClipMaxY() - GetClipMinY());
			PlaySE(se_shot2);
			CreateSearchShot(GetClipMinX() - 24, GetClipMinY() + ry,  3.0, 0);
			CreateSearchShot(GetClipMaxX() + 24, GetClipMaxY() - ry, -3.0, 0);
			CreateSearchShot(GetClipMaxX() - rx, GetClipMinY() - 24, 0,  3.0);
			CreateSearchShot(GetClipMinX() + rx, GetClipMaxY() + 24, 0, -3.0);
			Wait(120);
			let rpos = 0.9;//rand(0, 1);
			let rx = rpos * (GetClipMaxX() - GetClipMinX());
			let ry = rpos * (GetClipMaxY() - GetClipMinY());
			PlaySE(se_shot2);
			CreateSearchShot(GetClipMinX() - 24, GetClipMinY() + ry,  3.0, 0);
			CreateSearchShot(GetClipMaxX() + 24, GetClipMaxY() - ry, -3.0, 0);
			CreateSearchShot(GetClipMaxX() - rx, GetClipMinY() - 24, 0,  3.0);
			CreateSearchShot(GetClipMinX() + rx, GetClipMaxY() + 24, 0, -3.0);
			Wait(120);
		}
	}

	task CreateAccelShot(x, y, angle)
	{
		let obj = Obj_Create(OBJ_SHOT);

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

		let v = 7.0;
		let a1 = -v / 15;
		let a2 = 0.02;
		let count = 0;

		while( !Obj_BeDeleted(obj) ){
			if( count < 15 ){
				v += a1;
			}
			else if( count < 180 ){
				v += a2;
			}
			count ++;

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

	task CreateSearchShot(x, y, vx, vy)
	{
		let n = 15;
		let inter = 2;
		let obj = [];
		let obj_x = [];
		let obj_y = [];
		let obj_grap = [];

		ascent( let i in 0..n ){
			obj = obj ~ [Obj_Create(OBJ_SHOT)];
		}
		ascent( let i in 0..(n * inter) ){
			obj_x = obj_x ~ [x];
			obj_y = obj_y ~ [y];
			obj_grap = obj_grap ~ [201];
		}

		let f_reflect = 0;

		loop{
			let del_num = 0;
			ascent( let i in 0..n ){
				if( Obj_BeDeleted(obj[i]) ){
					del_num ++;
				}
			}
			if( del_num == n ){
				break;
			}
			descent( let i in 1..(n * inter) ){
				obj_x[i] = obj_x[i - 1];
				obj_y[i] = obj_y[i - 1];
				obj_grap[i] = obj_grap[i - 1];
			}
			obj_x[0] = obj_x[0] + vx;
			obj_y[0] = obj_y[0] + vy;
			ascent( let i in 0..n ){
				Obj_SetPosition(obj[i], obj_x[i * inter], obj_y[i * inter]);
				ObjShot_SetGraphic(obj[i], obj_grap[i * inter]);
			}

			if( !f_reflect ){
				let f_none = 0;
				if( vx < -0.5 && obj_x[0] <= GetPlayerX() ){
					if( obj_y[0] < GetPlayerY() ){
						vy = -vx;
					}
					else{
						vy = vx;
					}
					vx = 0;
				}
				else if( vx > 0.5 && obj_x[0] >= GetPlayerX() ){
					if( obj_y[0] < GetPlayerY() ){
						vy = vx;
					}
					else{
						vy = -vx;
					}
					vx = 0;
				}
				else if( vy < -0.5 && obj_y[0] <= GetPlayerY() ){
					if( obj_x[0] < GetPlayerX() ){
						vx = -vy;
					}
					else{
						vx = vy;
					}
					vy = 0;
				}
				else if( vy > 0.5 && obj_y[0] >= GetPlayerY() ){
					if( obj_x[0] < GetPlayerX() ){
						vx = vy;
					}
					else{
						vx = -vy;
					}
					vy = 0;
				}
				else{
					f_none = 1;
				}
				if( !f_none ){
					obj_grap[0] = 207;
					ObjShot_SetGraphic(obj[0], obj_grap[0]);
					f_reflect = 1;
				}
			}
			yield;
		}
	}

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