#e
#Title[XyJ]
#Text[pjbNEgx[^[]
#ScriptVersion[2]


script_enemy_main {
	let name	= "@BupjbNEgx[^[v";
	let imgBoss	= "script\img\ExRumia.png";

	// ʒu
	let xIni	= GetCenterX;
	let yIni	= GetClipMinY+160;
	
	let TORA_SPEED = 1.5;
	let TORA_UE    = -48;
	let TORA_SHITA =  48;

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

		PlaySE(GetCurrentScriptDirectory ~ "/se/eco00_e.wav");
		Concentration01(60);
		wait(120);
		PlaySE(GetCurrentScriptDirectory ~ "/se/hit82.wav");
		shot_haguruma(GetClipMaxX+20, 0, 16, 48, 0, 1, ORANGE21, 60);
		shot_haguruma(GetClipMinX-20, 0, 16, 48, 0, 1, ORANGE21, 60);
		shot_konbea;

		wait(60);
		shot_torabe;
	
	
		wait(60);
		PlaySE(GetCurrentScriptDirectory ~ "/se/eco00_e.wav");
		Concentration01(60);
	
	}

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

	task shot_torabe
	{
		let WT = 120;
		let STP = 14;
		loop
		{
			let y = GetClipMinY;
			while (y < GetClipMaxY)
			{
				if (y > TORA_SHITA+GetPlayerY)
					{ shot_torabe_sub(GetClipMaxX+10, y, RED01); }
				else if (y < TORA_UE+GetPlayerY)
					{ shot_torabe_sub(GetClipMinX-10, y, RED01); }
				y += STP;
			}

			wait(WT);
		}
	}

	task shot_torabe_tate_sub(start_x, start_y, count, STP, speed, wt)
	{
		let x = start_x+GetClipMinX;
		
		let angle = 90;
		if (start_y > GetCenterY) { angle += 180; }
		loop(count)
		{
			CreateShot01(x, start_y, speed, angle, BLUE01, 0);
			x += STP;
			wait(wt);
		}
	}

	task shot_torabe_tate
	{
		let WT = 135;
		let STEP = 10;
		let GAMEN4 = (GetClipMaxX-GetClipMinX)/4+1;
		let LENG = GAMEN4/STEP;
		loop
		{
			shot_torabe_tate_sub(GAMEN4*2+rand(-2,2), GetClipMaxY+10, LENG*1.2, STEP, 0.75, 8);
			shot_torabe_tate_sub(GAMEN4*2+rand(-2,2), GetClipMaxY+10, LENG*1.2, -STEP, 0.75, 8);

			shot_torabe_tate_sub(GAMEN4*1.1+rand(-2,2), GetClipMinY-10, LENG*1.1, -STEP, 1.3, 4);
			shot_torabe_tate_sub(GAMEN4*2.9+rand(-2,2), GetClipMinY-10, LENG*1.1, STEP, 1.3, 4);
			wait(WT);
		}
	}

	task shot_torabe_sub(x, y, graphic)
	{
		//IuWFNge̍쐬
		let obj = Obj_Create(OBJ_SHOT);
		
		//p[^̐ݒ
		Obj_SetPosition(obj, x, y);
		ObjShot_SetGraphic(obj, graphic);
		
		while(!Obj_BeDeleted(obj)) {
			if (GetPlayerY+TORA_UE > Obj_GetY(obj)) { x += TORA_SPEED; }
			else if (GetPlayerY+TORA_SHITA < Obj_GetY(obj)) { x -= TORA_SPEED; }

			Obj_SetPosition(obj, x, y);
			yield;
		}

		Obj_Delete(obj);
	}

	function shot_haguruma(x_offset, y_offset, way, size, a_plus, c_angle, graphic, wt)
	{
		let TIMER = 100;
		ascent(let i in 0 .. way)
		{
			shot_haguruma_sub(x_offset, y_offset, i*360/way, size, a_plus, c_angle, graphic, wt, TIMER, int(i*TIMER/way)+120);
		}
	}

	task shot_haguruma_sub(x_offset, y_offset, angle, size, a_plus, c_angle, graphic, wt, timer, sub_timer)
	{
		//IuWFNge̍쐬
		let obj = Obj_Create(OBJ_SHOT);
		
		//p[^̐ݒ
		Obj_SetPosition(obj, x_offset, GetPlayerY+y_offset);
		ObjShot_SetGraphic(obj, graphic);
		Obj_SetAutoDelete(obj,false);
		ObjShot_SetBombResist(obj, true);
//		Obj_SetAlpha(obj, 128);
		
		let tmp_r = 0;
		let tmp_p = size/wt;
		
		let t = timer + sub_timer;
		
		while(!Obj_BeDeleted(obj)) {
			if (t < 1)
			{
				t = timer;

				let sx = GetClipMinX;
				if (GetCenterX > Obj_GetX(obj)) { sx = GetClipMaxX; }
				CreateShot02(Obj_GetX(obj), Obj_GetY(obj), 4, atan2(GetPlayerY-Obj_GetY(obj), sx-Obj_GetX(obj)), -0.025, 1, BLUE01, 20);
			}
			
			if (size > tmp_r) { tmp_r += tmp_p;}
			angle += c_angle;
			
			Obj_SetPosition(obj, x_offset+cos(angle)*tmp_r, GetPlayerY+y_offset+sin(angle)*tmp_r);
			Obj_SetAngle(obj, angle+a_plus);
			t--;
			yield;
		}

		Obj_Delete(obj);
	}

	task shot_konbea
	{
		loop
		{
			shot_konbea_sub(GetClipMinX, TORA_UE, 1.5,  0, ORANGE11);
			shot_konbea_sub(GetClipMaxX, TORA_SHITA,-1.5, 180, ORANGE11);
			wait(8);
		}
	}

	task shot_konbea_sub(x, y_offset, speed, angle, graphic)
	{
		//IuWFNge̍쐬
		let obj = Obj_Create(OBJ_SHOT);
		
		//p[^̐ݒ
		Obj_SetPosition(obj, x, GetPlayerY+y_offset);
		Obj_SetAngle(obj, angle);
		ObjShot_SetGraphic(obj, graphic);
		ObjShot_SetBombResist(obj, true);
		
		while(!Obj_BeDeleted(obj)) {
			x += speed;
			Obj_SetPosition(obj, x, GetPlayerY+y_offset);
			yield;
		}

		Obj_Delete(obj);
	}

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

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