#東方弾幕風
#Title[魔法「夢の回転木馬」]
#Text[test]
#ScriptVersion[2]

script_enemy_main {
	let name	= "魔法「夢の回転木馬」";

	// shot_all.png
	#include_function ".\lib_usershot.txt"
	// 白蓮さん初期設定
	#include_function ".\lib\setting_byakuren.txt"

	imgBlankCircle = GetCurrentScriptDirectory() ~ "\img\yume.png";

	// 初期位置
	let xIni	= GetCenterX;
	let yIni	= GetClipMinY + 64+32;
	
	let MOKUBA_SPEED = 0;	//回転木馬の広がるスピード

	let D_RATE = 20;
	let D_WAIT = 320;

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

		SetLife(900);
		SetTimer(60);
		SetScore(1500000);
		SetDamageRate(5, 5);

		Ini_Graphic;
		LoadGraphic(imgEffect);
		LoadGraphic(imgBlankCircle);

		TMain;
	}

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

		yield;
	}

	@DrawLoop {
		Draw_Byakuren;
	}

	@Finalize {
		if (!IsTimeOut()) { makeTokutenItem(15); }
		else { makeTokutenItem(30); }
//		DeleteGraphic(imgBoss);
	}

	// メインタスク
	task TMain {
		yield;

		TRate;
		standBy;
		effect_guruguru_circle(255,128,192);

		//弾
		wait(20);
		TShot;
	}

	//弾射出タスク
	task TShot()
	{
		MOKUBA_SPEED = 1;
		let speed = 2;
		let kuzure = 0;
		let wt = 75;
		let t = 0;

		ATTACK_FLAG = 1;
		loop{
			//スピードアップ！
			if (GetLife() < 350) { MOKUBA_SPEED = 1.66; speed = 2.6; wt = 40; kuzure = 0.06; }
			else if (GetLife() < 700) { MOKUBA_SPEED = 1.5; speed = 2.4; wt = 50; kuzure = 0.04; }
			else if (GetLife() < 850) { MOKUBA_SPEED = 1.33; speed = 2.2; wt = 60; kuzure = 0.02; }

			shot_circle_yume([speed, -speed][t%2], rand(0,360), 360/16, 12, 5,
				[US_BUTTERFLY_BLUE, US_BUTTERFLY_PURPLE][t%2], kuzure);
			t++;
			PlaySE(SE_BELL);
			loop(wt) {yield;}
		}

	}

	//全方位弾　崩れる
	function shot_circle_yume(speed, angle, angle2, way, way2, graphic, kuzure)
	{
		let LARGEWAY = 360/way;
		let SMALLWAY = angle2/way2;
		ascent (let m in 0 .. way2)
		{
			ascent (let n in 0 .. way)
			{
				let stmp = speed*(1 + rand(-kuzure,kuzure));
				shot_merrygoround(GetX, GetY, stmp, angle+n*LARGEWAY+m*SMALLWAY, graphic, 30, 400);
			}
		}
	}

	
	//弾幕キメラの実弾のような弾道
	task shot_merrygoround(x, y, d_speed, angle, graphic, delay, max_r)
	{
		let PI = 3.1415926535;	//π
		let MAX_GAIN = 5;	//角度増分の最大値
		
		//オブジェクト弾の作成
		let obj = Obj_Create(OBJ_SHOT);
		
		//パラメータの設定
		Obj_SetPosition(obj, x, y);
		Obj_SetAngle(obj, angle);
		ObjShot_SetGraphic(obj, graphic);
		ObjShot_SetDelay(obj, delay);
		Obj_SetAutoDelete(obj,false);

		
		loop(delay){ yield; }
		
		let r = 0;
		let muki = 1;
		if (d_speed < 0) { muki = -1; }
		d_speed = absolute(d_speed);
		
		while(r < max_r && !Obj_BeDeleted(obj)) {
			r += MOKUBA_SPEED;
			
			let ag = (d_speed/r)*180/PI;
			if (ag > MAX_GAIN) { ag = MAX_GAIN; }
			angle += (ag*muki);
			Obj_SetPosition(obj, x+cos(angle)*r, y+sin(angle)*r);
			Obj_SetAngle(obj, angle+90*muki);
			yield;
		}

		Obj_Delete(obj);
	}
}
