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


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

	// ʒu
	let xIni	= GetCenterX;
	let yIni	= GetClipMinY + 120;
	
	@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
		wait(30);
		TRate;
		TShot;
	}

	//eˏo^XN
	task TShot
	{
		TMove;
		
		loop
		{
			let muki = rand_int(0,1)*2-1;
			loop(2)
			{
				let angle = GetAngleToPlayer();
				loop(12)
				{
					CreateEnemyFromScript("haguruma", GetX, GetY, 1, angle, 3*muki);
					angle += muki * 360/12;
					PlaySE(GetCurrentScriptDirectory ~ "/se/hit82.wav");
					wait(5);
				}
				muki = -muki;
				wait(30);
			}
			wait(120);
		}
	}

	task TMove {
		loop
		{
			wait(5*12*2+30*2);
			moveBoss(32, 64, 16, 32, 20, 2, 64, 48, 160);
			wait(120);
		}
	}

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

	// ʒ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; }
	}
}

// 
script_enemy haguruma {
	let imgEmeny	= GetCurrentScriptDirectory() ~ "\img\faily_red.png";

	@Initialize {
		SetLife(100);
		SetScore(0);
		SetDamageRate(100, 100);

		TMain;
	}

	@MainLoop {
		if (GetX < GetClipMinX-64 || GetX > GetClipMaxX+64 || GetY < GetClipMinY-64 || GetY > GetClipMaxY+64)
				{ VanishEnemy(); }

		SetCollisionA(GetX, GetY, 32);

		yield;
	}

//`悵Ȃ
//	@DrawLoop {
//	}

	@Finalize()
	{
		//Ԃ
		if (!BeVanished())
		{
			DeleteEnemyShotToItemInCircle(CHILD, GetX, GetY, 36);
		}
		else
		{
			DeleteEnemyShotImmediatelyInCircle(ALL, GetX, GetY, 36);
		}
	}

	task TMain {
		yield;

		TShot;
	}

	task TShot {
		let ca = GetArgument();
		let a = GetAngle();
		let ct = 140;
		let flg = 1;
		loop
		{
			if (flg > 0) { ct -= 4; }
			else { ct += 4; }

			if (ct > 140 || ct < 40) { flg = -flg; }

			shot_haguruma(a, 32, ca, YELLOW21, 2.5, RED21, ct);
			loop(10) { yield; }
		}
	}

	task shot_haguruma(angle, size, c_angle, graphic, speed, graphic2, COUNTER)
	{
		//IuWFNge̍쐬
		let obj = Obj_Create(OBJ_SHOT);
		
		//p[^̐ݒ
		Obj_SetPosition(obj, GetX, GetY);
		ObjShot_SetGraphic(obj, graphic);
		Obj_SetAutoDelete(obj,false);
		ObjShot_SetBombResist(obj, true);
		
		let tmp_r = 0;
		while(!Obj_BeDeleted(obj)) {
			if (COUNTER < 0) { break; }
			if (size > tmp_r) { tmp_r += 1;}
			angle += c_angle;
			
			Obj_SetPosition(obj, GetX+cos(angle)*tmp_r, GetY+sin(angle)*tmp_r);
			Obj_SetAngle(obj, angle);
			COUNTER--;
			yield;
		}

		ObjShot_SetGraphic(obj, graphic2);
		Obj_SetAutoDelete(obj,true);
		ObjShot_SetBombResist(obj, false);
		Obj_SetSpeed(obj, speed);
	}
}
