#e
#Title[ʏe]
#Text[]
#ScriptVersion[2]


script_enemy_main {
	let name	= "";
	let imgBoss	= "script\img\ExRumia.png";

	// ʒu
	let xIni	= GetCenterX;
	let yIni	= GetClipMinY + 140;

	@Initialize {
		SetLife(2000);
		SetTimer(35);
		SetDamageRate(5, 5);

		LoadGraphic(imgBoss);
		SetTexture(imgBoss);
		setGraphicStop;

		TMain;
	}

	@MainLoop {
		SetCollisionA(GetX, GetY, 32);
		SetCollisionB(GetX, GetY, 16);

		yield;
	}

	@DrawLoop {
		DrawGraphic(GetX, GetY);
	}

	@Finalize {
		DeleteGraphic(imgBoss);
	}

	// C^XN
	task TMain {
		yield;

		standBy;

		//e
		TRate;
		TShot;
	}

	//eˏo^XN
	task TShot
	{
		let WAY = 20;
		let WT = 45;

		TMove(WT);
		loop
		{
			let angle = rand(0,360);
			shot_guruguru(1, angle,WAY*3, 2-0.08, YELLOW01);
			ascent (let i in 0 .. 3)
			{
				shot_guruguru(1, angle, WAY, i*0.1+2, YELLOW01);
			}
			PlaySE(GetCurrentScriptDirectory ~ "/se/hit82.wav");
			wait(WT);

			let angle = rand(0,360);
			shot_guruguru(-1, angle, WAY*3, 2-0.08, ORANGE01);
			ascent (let i in 0 .. 3)
			{
				shot_guruguru(-1, angle, WAY, i*0.1+2, ORANGE01);
			}
			PlaySE(GetCurrentScriptDirectory ~ "/se/hit82.wav");
			wait(WT);
		}
	}

	task TMove(WT) {
		loop
		{
			wait(WT*3);
			moveBoss(48, 80, 16, 32, 40, 1.5, 64, 48, 160);
		}
	}
	
	function shot_guruguru(muki, angle, way, speed, graphic)
	{
		ascent (let i in 0 .. way)
		{
			CreateShotA(0, GetX, GetY, 0);
			SetShotDataA(0,  0, speed, angle+i*360/way, 2*muki, 0, speed, graphic);
			SetShotDataA(0, 90, NULL, NULL, 1*muki, 0, 0, graphic);
			SetShotDataA(0,120, NULL, NULL, 0, 0, 0, graphic);
			FireShot(0);
		}
	}

	// _[W[g̕ύX
	task TRate {
		wait(300);
		SetDamageRate(120, 120);
	}

	// ʒuֈړAUJn
	sub standBy {
		let wIni = 120;

		SetMovePosition02(xIni, yIni, wIni);
		setGraphicMove;

		SetInvincibility(wIni);
		wait(wIni);

		setGraphicPose;
	}

	// OtBbN̐ݒ
	sub setGraphicStop  { SetGraphicRect(  0,   0,  64,  64); }
	sub setGraphicPose  { SetGraphicRect( 64,   0, 128,  64); }
	sub setGraphicLeft  { SetGraphicRect(128,   0, 192,  64); }
	sub setGraphicRight { SetGraphicRect(192,   0, 256,  64); }

	sub setGraphicMove {
		if(GetSpeedX < 0) {
			setGraphicLeft;
		} else {
			setGraphicRight;
		}
	}
	

	function moveBoss(minX, maxX, minY, maxY, weight, speed, gamen_side, gamen_top, gamen_bottom)
	{
		moveToPlayer(rand(minX, maxX), rand(minY, maxY), weight, speed,
			GetClipMinX + gamen_side, GetClipMinY + gamen_top,
			GetClipMaxX - gamen_side, GetClipMinY + gamen_bottom);
	}

	// ȂׂvC[̕Ɉړ
	function moveToPlayer(xMove, yAdd, weight, max_speed, left, top, right, bottom) {
		let x;
		let y;

		if(GetPlayerX < GetX) {
			// vC[EɓG΁AG͍ɓ܂B
			x = GetX - xMove;

			// AAG̈̍[ɂ悤ȂAEɓ܂B
			if(x < left) {
				x = GetX + xMove;
			}
		} else {
			// Ȃ΁AG͉Eɓ܂B
			x = GetX + xMove;

			// AAG̈̉E[Eɂ悤ȂAɓ܂B
			if(right < x) {
				x = GetX - xMove;
			}
		}

		// ̈̊Oɍsꍇ́A[Ŏ~߂܂B
		let flag = rand_int(0, 1);
		
		if (GetY+yAdd < top) { flag = 0; }
		else if (GetY-yAdd > bottom) { flag = 1; }

		if (flag == 1) { y = GetY + yAdd; }
		else { y = GetY - yAdd; }

		if(y < top) {
			y = top;
		} else if(bottom < y) {
			y = bottom;
		}

		SetMovePosition03(x, y, weight, max_speed);
	}

	// w t[҂
	function wait(w) {
		loop(w) { yield; }
	}
}
