#e
#Title[XyJ]
#Text[ꂽ@Bv]
#ScriptVersion[2]


script_enemy_main {
	let name	= "@Buꂽ@Bvv";
	let imgBoss	= "script\img\ExRumia.png";

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

	@Initialize {
		CutIn(YOUMU, name, "", 0, 0, 0, 0);

		SetLife(1000);
		SetTimer(50);
		SetDamageRate(5, 5);
		SetScore(500000);

		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;
		TMove;
	}

	task TShot()
	{
		let s = 200;
		let w = int(s*0.15);
		let muki = 1;
		
		shot_clock_laser(2.73, 85, RED01, 60, 1, 6);
		shot_clock_laser(1.23, 70, RED01, 60, -2, 6);
		shot_haguruma(40, 80, 0,-1, ORANGE21, 40);
		shot_haguruma(40, 80, 0, 1, ORANGE21, 40);
	}

	task TMove {
		loop
		{
			wait(240);
			moveBoss(32, 60, 16, 32, 40, 1.5, 64, 48, 160);
		}
	}

	function shot_haguruma(way, size, a_plus, c_angle, graphic, wt)
	{
		ascent(let i in 0 .. way)
		{
			shot_haguruma_sub(0, 0, i*360/way, size, a_plus, c_angle, graphic, wt);
		}
	}

	task shot_haguruma_sub(x, y, angle, size, a_plus, c_angle, graphic, wt)
	{
		//IuWFNge̍쐬
		let obj = Obj_Create(OBJ_SHOT);
		
		//p[^̐ݒ
		Obj_SetPosition(obj, GetX+x, GetY+y);
		ObjShot_SetGraphic(obj, graphic);
		Obj_SetAutoDelete(obj,false);
		ObjShot_SetBombResist(obj, true);
		
		let tmp_r = 0;
		let tmp_timer = wt;
		let tmp_p = size/wt;
		let tmp_ap = 90;
		if (c_angle < 0) { tmp_ap = -90; }
		
		while(!Obj_BeDeleted(obj)) {
			if (size > tmp_r) { tmp_r += tmp_p;}
			angle += c_angle;
			
			Obj_SetPosition(obj, GetX+x+cos(angle)*tmp_r, GetY+y+sin(angle)*tmp_r);
			Obj_SetAngle(obj, angle+a_plus);
			tmp_timer--;
			yield;
		}

		Obj_Delete(obj);
	}

	//angle>0ɂ̂ݑΉ
	task shot_clock_laser(c_angle, size, graphic, wt, sang, timer)
	{
		let angle = 270;
		
		//IuWFNge̍쐬
		let obj = Obj_Create(OBJ_LASER);
		
		//p[^̐ݒ
		Obj_SetPosition(obj, GetX, GetY);
		Obj_SetAngle(obj, angle);
		ObjShot_SetGraphic(obj, graphic);
		Obj_SetAutoDelete(obj,false);
		ObjShot_SetBombResist(obj, true);
		ObjLaser_SetWidth(obj, 10);
		ObjLaser_SetSource(obj, false);
		
		let tmp_r = 0;
		let tmp_p = size/wt;
		let tmp_wt = wt;
		
		let tmp_shot = timer;
		let tmp_delay = 30;
		
		while(!Obj_BeDeleted(obj) && tmp_wt > 0) {
			tmp_r += tmp_p;

			Obj_SetPosition(obj, GetX, GetY);
			ObjLaser_SetLength(obj, tmp_r);
			tmp_wt--;
			yield;
		}

		let before_pangle = GetAngleToPlayer;
		let ctmp = c_angle*timer/2;
		while(!Obj_BeDeleted(obj)) {
			if (tmp_shot < 1)
			{
				CreateShot01(GetX+size*0.95*cos(angle), GetY+size*0.95*sin(angle),
					2, angle, RED21, 15);
				CreateShot01(GetX+size*0.95*cos(angle-ctmp), GetY+size*0.95*sin(angle-ctmp),
					1, angle, RED21, 15);
				tmp_shot = timer;
			}
			if (tmp_delay < 1 && (before_pangle > angle) && (GetAngleToPlayer < angle+c_angle))
			{
				PlaySE(GetCurrentScriptDirectory ~ "/se/hit82.wav");
				shot_clock(GetX, GetY, angle, sang, size, size*2, RED01, 5);
				tmp_delay = 30;
			}
			angle += c_angle;
			angle %= 360;
			Obj_SetPosition(obj, GetX, GetY);
			Obj_SetAngle(obj, angle);
			
			tmp_shot--; tmp_delay--;
			before_pangle = GetAngleToPlayer;
			yield;
		}

		Obj_Delete(obj);
	}

	task shot_clock(x, y, angle, c_angle, size, maxsize, graphic, delay)
	{
		//IuWFNge̍쐬
		let obj_main = Obj_Create(OBJ_SHOT);
		
		//p[^̐ݒ
		Obj_SetPosition(obj_main, x, y);
		Obj_SetAngle(obj_main, angle);
		Obj_SetSpeed(obj_main, 1);
		ObjShot_SetGraphic(obj_main, RED03);
		Obj_SetAutoDelete(obj_main, true);
		ObjShot_SetBombResist(obj_main, true);
		ObjShot_SetDelay(obj_main, delay);

		//IuWFNge̍쐬
		let obj = Obj_Create(OBJ_LASER);
		//p[^̐ݒ
		Obj_SetPosition(obj, x, y);
		Obj_SetAngle(obj, angle);
		ObjShot_SetGraphic(obj, graphic);
		Obj_SetAutoDelete(obj,false);
		ObjShot_SetBombResist(obj, true);
		ObjLaser_SetWidth(obj, 10);
		ObjLaser_SetSource(obj, false);
		ObjLaser_SetLength(obj, size);
		ObjShot_SetDelay(obj, delay);
		
		wait(delay);
		
		while(!Obj_BeDeleted(obj_main)) {
			if (size < maxsize)
			{
				size += 1;
				ObjLaser_SetLength(obj, size);
			}
			angle += c_angle;
			Obj_SetPosition(obj, Obj_GetX(obj_main), Obj_GetY(obj_main));
			Obj_SetAngle(obj, angle);
			yield;
		}

		Obj_Delete(obj_main);
		ObjShot_FadeDelete(obj);
	}


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

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