#東方弾幕風
#Title[威光「弾幕神判」]
#Text[test]
#ScriptVersion[2]

script_enemy_main {
	let name	= "威光「弾幕神判」";
	
	let IMG_CIRCLE	= GetCurrentScriptDirectory~"img\circle.png";

	// shot_all.png
	#include_function ".\lib_usershot.txt"
	// 星さん初期設定
	#include_function ".\lib\setting_tora.txt"

	//現在入っている円の色
	// 0x01 ... R
	// 0x02 ... G
	// 0x04 ... B
	let CIRCLE_COLOR	= 0;
	
	LoadSE(GetCurrentScriptDirectory~"se\se002-0001.wav");
	LoadSE(GetCurrentScriptDirectory~"se\se011-0013.wav");
	

	// 初期位置
	let xIni	   = GetCenterX;
	let yIni	   = GetCenterY-16;

	let D_RATE = 27;
	let D_WAIT = 240;

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

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

		Ini_Graphic;
		LoadGraphic(IMG_CIRCLE);
		LoadGraphic(imgHoutou);

		TMain;
	}

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

		CIRCLE_COLOR	=0;		//処理移行前に変数をリセット
		yield;
//		OutputDebugString(0,"CIRCLE_COLOR",CIRCLE_COLOR);
	}

	@DrawLoop {
		Draw_Toramaru;
	}

	@Finalize {
		if (!IsTimeOut()) { makeTokutenItem(15); }

//		DeleteGraphic(imgBoss);
	}

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

		TRate;
		standBy;
		effect_toramaru;

		//弾
		ATTACK_FLAG = 1;
		TCircle;
		TShot;
		TLaser;
	}


	//弾射出タスク
	task TShot()
	{
		loop(120) { yield; }

		let i = 0;
		loop{
			ascent(let n in 0 .. 12)
			{
				CreateShot01(GetX, GetY, 2, GetAngleToPlayer()+(n+(i%2)/2)*360/12, WHITE02, 20);
			}
			i++;
			PlaySE(SE_SHOT);

			loop(60) { yield; }
		}
	}

	//弾射出タスク
	task TCircle()
	{
		let i = 0; let j = 0;
		loop{
			loop(12)
			{
				let angle =30*(j%12);
				let c1 = [1, 0, 0][i%3];
				let c2 = [0, 0, 1][i%3];
				let c3 = [0, 1, 0][i%3];
				effect_light(GetX, GetY, 0.5, angle, 3, 120, 600, 92, c1, c2, c3);
				//効果音
				i++; j++;
			}
			loop(300) { yield; }
			i++;
		}
	}
	
	task TLaser()
	{
		let j = 0; let i = 0; //and_int(0,2);

		loop(60) { yield; }
		let col = [1, 2, 3, 4, 1, 5, 2, 4, 6][i%9];
		lazer_colorful(0, 2, 60, 400, col);
		i++;

		loop
		{
			PlaySE(SE_LASER);

			loop(210) { yield; }
			let ltype = [1, 0, 2, 0, 1, 0, 2, 0][j%8];
			let col = [1, 2, 3, 4, 1, 5, 2, 4, 6][i%9];
			let speed = [2, 1.5, 1.5, 2][ltype];
			lazer_colorful(ltype, speed, 60, 800, col);
			i++; j++;
		}
	}

	//CIRCLE_COLORの値によって当たり判定が消えるレーザー
	//type  ... 0=上 1=左 2=右 3=下
	//cname ... 1-7の範囲 CIRCLE_COLORと同じ色
	task lazer_colorful(type, speed, delay, timer, cname)
	{
		let graphic = [NULL, US_BALL_F_RED, US_BALL_F_GREEN, US_BALL_F_YELLOW, US_BALL_F_BLUE, US_BALL_F_PURPLE, US_BALL_F_AQUA, US_BALL_F_WHITE][cname];
		let KYORI = 24;
		//オブジェクト弾の作成
		let obj = Obj_Create(OBJ_LASER);
		
		//パラメータの設定
		Obj_SetAngle(obj, [0, 90, 90, 0][type]);
		Obj_SetPosition(obj, [GetClipMinX-16, GetClipMinX, GetClipMaxX, GetClipMinX-16][type], [GetClipMinY, GetClipMinY-32, GetClipMinY-32, GetClipMaxY][type]);
		ObjLaser_SetLength(obj, [445, 580, 580, 445][type]);
		ObjLaser_SetWidth(obj, 18);
		ObjLaser_SetSource(obj, false);
		ObjShot_SetDelay(obj, delay);
		ObjShot_SetGraphic(obj, graphic);
		ObjShot_SetBombResist(obj, true);
		
		loop(delay){ yield; }
		
		loop(timer){
			if (type == 0)
				{ Obj_SetY(obj, Obj_GetY(obj)+speed); }
			else if (type == 1)
				{ Obj_SetX(obj, Obj_GetX(obj)+speed); }
			else if (type == 2)
				{ Obj_SetX(obj, Obj_GetX(obj)-speed); }
			else
				{ Obj_SetY(obj, Obj_GetY(obj)-speed); }

			//当たり判定
			if (CIRCLE_COLOR == cname) { Obj_SetCollisionToPlayer(obj, false); }
			else { Obj_SetCollisionToPlayer(obj, true); }

			yield;
		}

		ObjShot_FadeDelete(obj);
	}
	
	//効果音
	sub click_sound()
	{
		PlaySE(GetCurrentScriptDirectory~"se\se011-0013.wav");	//効果音
	}

	//円形エフェクト
	//gain_fadeより大きくなるとsize_speedの加算幅が減る
	//最終的なサイズは、大体 gain_fade+size_speed*10
	task effect_light(x, y, speed, angle, size_speed, gain_fade, timer, c_alpha, cr, cg, cb)
	{
		let c_red = cr * 255;
		let c_green = cg * 255;
		let c_blue = cb * 255;
		let FADE_TIME = 60;
		
		let obj = Obj_Create(OBJ_EFFECT);
		ObjEffect_SetTexture(obj, IMG_CIRCLE);
		ObjEffect_SetRenderState(obj, ADD);
		ObjEffect_SetLayer(obj, 1);
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjEffect_CreateVertex(obj, 4);
		ObjEffect_SetVertexUV(obj, 0,   0,   0);
		ObjEffect_SetVertexUV(obj, 1, 255,   0);
		ObjEffect_SetVertexUV(obj, 2, 255, 255);
		ObjEffect_SetVertexUV(obj, 3,   0, 255);
		ObjEffect_SetVertexXY(obj, 0, -50, -50);
		ObjEffect_SetVertexXY(obj, 1,  50, -50);
		ObjEffect_SetVertexXY(obj, 2,  50,  50);
		ObjEffect_SetVertexXY(obj, 3, -50,  50);
		ObjEffect_SetVertexColor(obj, 0, c_alpha, c_red, c_green, c_blue);
		ObjEffect_SetVertexColor(obj, 1, c_alpha, c_red, c_green, c_blue);
		ObjEffect_SetVertexColor(obj, 2, c_alpha, c_red, c_green, c_blue);
		ObjEffect_SetVertexColor(obj, 3, c_alpha, c_red, c_green, c_blue);

		//初期設定
		let scale = 0;
		ObjEffect_SetScale(obj, scale, scale);
		Obj_SetPosition(obj, x, y);
		Obj_SetSpeed(obj, speed);
		Obj_SetAngle(obj, angle);
		ObjEffect_SetAngle(obj, 0, 0, -180);

		loop(timer)
		{
			//当たり判定用の変数に記録
			player_in_circle(cr, cg, cb, Obj_GetX(obj), Obj_GetY(obj), scale*50);
			yield;

			//サイズ変更
			if (gain_fade/100 <= scale) { size_speed *= 0.9; }
			scale += size_speed/100;
			
			ObjEffect_SetScale(obj, scale, scale);
		}
		
		//サイズ縮小（安全地帯判定も最後まで残す）
		ascent (let n in 0..FADE_TIME)
		{
			player_in_circle(cr, cg, cb, Obj_GetX(obj), Obj_GetY(obj), scale*50);
			yield;

			ObjEffect_SetVertexColor(obj, 0, c_alpha*(FADE_TIME-n)/FADE_TIME, c_red, c_green, c_blue);
			ObjEffect_SetVertexColor(obj, 1, c_alpha*(FADE_TIME-n)/FADE_TIME, c_red, c_green, c_blue);
			ObjEffect_SetVertexColor(obj, 2, c_alpha*(FADE_TIME-n)/FADE_TIME, c_red, c_green, c_blue);
			ObjEffect_SetVertexColor(obj, 3, c_alpha*(FADE_TIME-n)/FADE_TIME, c_red, c_green, c_blue);
		}

		Obj_Delete(obj);
	}
	
	//色判定チェック
	function player_in_circle(cr, cg, cb, x, y, size)
	{
		if ((x-GetPlayerX)^2+(y-GetPlayerY)^2 <= size*size)
		{
			if (cr == 1 && CIRCLE_COLOR%2 == 0) { CIRCLE_COLOR += 1; }
			if (cg == 1 && int((CIRCLE_COLOR%4)/2) == 0) { CIRCLE_COLOR += 2; }
			if (cb == 1 && int((CIRCLE_COLOR%8)/4) == 0) { CIRCLE_COLOR += 4; }
		}
	}

}
