// 白蓮さんの基本設定＆関数まとめ


let imgBoss	= GetCurrentScriptDirectory() ~ "\img\mychara.png";
let imgEffect = GetCurrentScriptDirectory() ~ "\img\eff_byakuren.png";
let imgBlankCircle	= GetCurrentScriptDirectory() ~ "\img\mcircle_blank.png";

//魔方陣
let CC_RED	= -1;	//表示しない
let CC_GREEN	= 0;
let CC_BLUE	= 0;

let ANIME_FLAME = 3;		//アニメーションするフレーム数
let ANIME_FLAG = ANIME_FLAME;	//アニメーション用フラグ

// 白蓮さん初期設定
#include_function ".\lib\common.txt"


// グラフィックの設定
sub setGraphicStop   { SetGraphicRect(128, 160, 192, 256); }
sub setGraphicAttack { SetGraphicRect(  0,  32,  64, 128); }
sub setGraphicAttack2{ SetGraphicRect( 64,  32, 128, 128); }
sub setGraphicAttack3{ SetGraphicRect(128,  32, 192, 128); }
sub setGraphicLeft   { SetGraphicRect(  0, 160,  64, 256); }
sub setGraphicRight  { SetGraphicRect( 64, 160, 128, 256); }

sub setGraphicMove {
	if(GetSpeedX == 0) {
		setGraphicStop;
	} else if(GetSpeedX < 0) {
		setGraphicLeft;
	} else {
		setGraphicRight;
	}
}

sub Draw_Byakuren
{
	if(GetSpeedX == 0) {
		if (ATTACK_FLAG)
		{
			ANIME_FLAG--;
			if (ANIME_FLAG <= ANIME_FLAME) { setGraphicAttack; }
			else { setGraphicAttack2; }
			
			if (ANIME_FLAG <= 0) { ANIME_FLAG = ANIME_FLAME*2; }
		}
		else
		{
			setGraphicAttack3;
		}
	} else if(GetSpeedX < 0) {
		setGraphicLeft;
	} else {
		setGraphicRight;
	}
	
	DrawGraphic(GetX, GetY);
}

// ===========================================================
// 白蓮さん用エフェクト
// ===========================================================

// 白蓮さんの強化魔法（？）エフェクト用タスク
// 自機の方角を見つめる目玉を描く
// offset_x&y ... 敵機からの距離
// X1~Y2 ... 画像のトリミング範囲  WIDTH ... 横幅の比率(1以上で横長)
// BASE_SCALE ... 貼り付けサイズ   R_State ... 描画方法
task effect_byakuren03(offset_x, offset_y, X1, Y1, X2, Y2, WIDTH, BASE_SCALE, R_State, Kidou)
{
	let EYE_SIZE = 2;	//眼？の動く範囲
	
	//初期設定
	let obj=Obj_Create(OBJ_EFFECT);
	ObjEffect_SetTexture(obj, imgEffect);
	ObjEffect_SetRenderState(obj, R_State);
	ObjEffect_SetLayer(obj, 1);
	ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
	ObjEffect_CreateVertex(obj, 4);
	ObjEffect_SetVertexUV(obj, 0, X1, Y1);
	ObjEffect_SetVertexUV(obj, 1, X2, Y1);
	ObjEffect_SetVertexUV(obj, 2, X2, Y2);
	ObjEffect_SetVertexUV(obj, 3, X1, Y2);
	ObjEffect_SetVertexXY(obj, 0, -100, -100);
	ObjEffect_SetVertexXY(obj, 1,  100, -100);
	ObjEffect_SetVertexXY(obj, 2,  100,  100);
	ObjEffect_SetVertexXY(obj, 3, -100,  100);

	ObjEffect_SetScale(obj, BASE_SCALE*WIDTH, BASE_SCALE);

	//ずっとループのターン
	let t = 0;
	let angle = 0; let gx = 0; let gy = 0;
	loop
	{
		if (t%10 == 0) {	//少し処理をケチる
			angle = atan2(offset_y+GetY-GetPlayerY, offset_x+GetX-GetPlayerX)+180;
			gx = EYE_SIZE*cos(angle);
			gy = EYE_SIZE*sin(angle);
		}
		
		Obj_SetPosition(obj, GetX+offset_x+gx, GetY+offset_y+gy);
		yield;
	}

	Obj_Delete(obj);
}

// 白蓮さんの強化魔法（？）エフェクト用タスク
// 花を描く
task effect_byakuren02(offset_x, offset_y, X1, Y1, X2, Y2, WIDTH, BASE_SCALE, BASE_ANGLE, MOVE_ANGLE, MOVE_TIMER, R_State, Kidou)
{
	let MOVE_TIMER_WAIT = 20;
	
	//初期設定
	let obj=Obj_Create(OBJ_EFFECT);
	ObjEffect_SetTexture(obj, imgEffect);
	ObjEffect_SetRenderState(obj, R_State);
	ObjEffect_SetLayer(obj, 1);
	ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
	ObjEffect_CreateVertex(obj, 4);
	ObjEffect_SetVertexUV(obj, 0, X1, Y1);
	ObjEffect_SetVertexUV(obj, 1, X2, Y1);
	ObjEffect_SetVertexUV(obj, 2, X2, Y2);
	ObjEffect_SetVertexUV(obj, 3, X1, Y2);
	ObjEffect_SetVertexXY(obj, 0, -100, -100);
	ObjEffect_SetVertexXY(obj, 1,  100, -100);
	ObjEffect_SetVertexXY(obj, 2,  100,  100);
	ObjEffect_SetVertexXY(obj, 3, -100,  100);

	ObjEffect_SetScale(obj, BASE_SCALE*WIDTH, BASE_SCALE);

	//起動
	if (Kidou)
	{
		ascent(let t in 0 .. 20)
		{
			let scale = BASE_SCALE*t/20;
			ObjEffect_SetScale(obj, scale*WIDTH, scale);
			Obj_SetPosition(obj, GetX+offset_x, GetY+offset_y);
			yield;
		}
	}

	//目玉（？）を描くタスクを呼ぶ
	effect_byakuren03(offset_x, offset_y, 96, 0, 105, 9, 1, 0.06, ALPHA, Kidou);
	
	//しばらく待つ
	let angle = BASE_ANGLE;
	let t = MOVE_TIMER;
	let gain_angle = MOVE_ANGLE;
	
	//ずっとループのターン
	loop
	{
		ObjEffect_SetAngle(obj, 0, 0, angle);
		Obj_SetPosition(obj, GetX+offset_x, GetY+offset_y);

		if (t <= 0) { t = MOVE_TIMER+MOVE_TIMER_WAIT; gain_angle = -gain_angle; }
		else if (t <= MOVE_TIMER) { angle += gain_angle; }
		t--;
		
		yield;
	}

	Obj_Delete(obj);
}
	
// 白蓮さんの強化魔法（？）エフェクト用タスク
// うにゃうにゃ動くピクチャーを貼り付ける
task effect_byakuren01(offset_x, offset_y , X1, Y1, X2, Y2, WIDTH, BASE_SCALE, MOVE_COUNT, MOVE_SIZE, R_State, Kidou)
{
	//初期設定
	let obj=Obj_Create(OBJ_EFFECT);
	ObjEffect_SetTexture(obj, imgEffect);
	ObjEffect_SetRenderState(obj, R_State);
	ObjEffect_SetLayer(obj, 1);
	ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
	ObjEffect_CreateVertex(obj, 4);
	ObjEffect_SetVertexUV(obj, 0, X1, Y1);
	ObjEffect_SetVertexUV(obj, 1, X2, Y1);
	ObjEffect_SetVertexUV(obj, 2, X2, Y2);
	ObjEffect_SetVertexUV(obj, 3, X1, Y2);
	ObjEffect_SetVertexXY(obj, 0, -100, -100);
	ObjEffect_SetVertexXY(obj, 1,  100, -100);
	ObjEffect_SetVertexXY(obj, 2,  100,  100);
	ObjEffect_SetVertexXY(obj, 3, -100,  100);

	//起動
	if (Kidou)
	{
		ascent(let t in 0 .. 30)
		{
			let scale = BASE_SCALE*t/30;
			ObjEffect_SetScale(obj, scale*WIDTH, scale);
			Obj_SetPosition(obj, GetX+offset_x, GetY+offset_y);
			yield;
		}
	}

	//ずっとループのターン
	let t = 0;
	loop
	{
		let scale = BASE_SCALE*(1+sin(t*MOVE_COUNT)*MOVE_SIZE);
		ObjEffect_SetScale(obj, scale*WIDTH, scale);
		Obj_SetPosition(obj, GetX+offset_x, GetY+offset_y);

		t++;
		yield;
	}
	Obj_Delete(obj);
}

// ===========================================================
// 白蓮さん通常弾幕用空魔方陣
// ===========================================================

task effect_blank_circle()
{
	//初期設定
	let obj = Obj_Create(OBJ_EFFECT);
	ObjEffect_SetTexture(obj, imgBlankCircle);
	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, 256,   0);
	ObjEffect_SetVertexUV(obj, 2, 256, 256);
	ObjEffect_SetVertexUV(obj, 3,   0, 256);
	ObjEffect_SetVertexXY(obj, 0,-85,-85);
	ObjEffect_SetVertexXY(obj, 1, 85,-85);
	ObjEffect_SetVertexXY(obj, 2, 85, 85);
	ObjEffect_SetVertexXY(obj, 3,-85, 85);
	ascent (let n in 0 .. 4)
	{
		ObjEffect_SetVertexColor(obj, n, 128, 255, 255, 255);
	}
	Obj_SetPosition(obj, GetCenterX, GetCenterY);

	let scale = 0;
	loop(25)
	{
		scale += 0.1;
		ObjEffect_SetScale(obj, scale, scale);
		yield;
	}

	
	//ずっとループのターン
	let t = 0;
	let angle = 0;
	
	loop
	{
		ObjEffect_SetScale(obj, scale, scale);
		ObjEffect_SetAngle(obj, 0, 0, angle);
		
		let c = sin(t);
		scale += c/200;
		angle -= (3+c);
		
		t+=3; angle %= 360;
		
		yield;
	}

	Obj_Delete(obj);
}


// ===========================================================
// 白蓮さんスペカ用魔方陣
// ===========================================================

task effect_guruguru_circle(cr, cg, cb)
{
	//初期設定
	let obj = Obj_Create(OBJ_EFFECT);
	ObjEffect_SetTexture(obj, imgBlankCircle);
	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, 256,   0);
	ObjEffect_SetVertexUV(obj, 2, 256, 256);
	ObjEffect_SetVertexUV(obj, 3,   0, 256);
	ObjEffect_SetVertexXY(obj, 0,-85,-85);
	ObjEffect_SetVertexXY(obj, 1, 85,-85);
	ObjEffect_SetVertexXY(obj, 2, 85, 85);
	ObjEffect_SetVertexXY(obj, 3,-85, 85);
	ascent (let n in 0 .. 4)
	{
		ObjEffect_SetVertexColor(obj, n, 128, cr, cg, cb);
	}
	Obj_SetPosition(obj, GetCenterX, GetCenterY);

	
	let scale = 0;
	loop(30)
	{
		scale += 0.05;
		ObjEffect_SetScale(obj, scale, scale);
		Obj_SetPosition(obj, GetX, GetY);
		yield;
	}

	//ずっとループのターン
	let angle = [0, 0, 0];
	let gain = [rand(0.5, 1.5), rand(0.5, 1.5), rand(2,3)];
	let flg = [0, 0, 0];
	
	loop
	{
		ObjEffect_SetAngle(obj, angle[0], angle[1], angle[2]);
		Obj_SetPosition(obj, GetX, GetY);
		
		ascent (let n in 0 .. 3)
		{
			angle[n] = angle[n]+gain[n];
			flg[n] = flg[n]+gain[n];
			if (flg[n] > 360)
			{
				flg[n] = 0;
				if (n == 2) { gain[n] = rand(2,3); }
				else { gain[n] = rand(0.5, 1.5); }
			}
		}
		
		yield;
	}

	Obj_Delete(obj);
}


// 白蓮さんのエフェクトひとまとめ
sub sub_effects
{
	effect_byakuren01(0, 0,0, 96,352,320,11/7,0.9,3,-0.025,ADD, 0);
	effect_byakuren01(0,10,0,320,220,512,   1,0.7,5,  0.05,ADD, 0);

	effect_byakuren02(-90, 30,0,0,96,96,1,0.4,  0, 0.3,40,ALPHA, 0);
	effect_byakuren02( 90, 30,0,0,96,96,1,0.4,180,-0.3,40,ALPHA, 0);
	effect_byakuren02( 45,-60,0,0,96,96,1,0.4,210, 0.3,40,ALPHA, 0);
	effect_byakuren02(-45,-60,0,0,96,96,1,0.4, 30,-0.3,40,ALPHA, 0);
}

// 白蓮起動
sub sub_effects_start
{
	PlaySE(SE_LASER2);
	effect_byakuren01(0,10,0,320,220,512,   1,0.7,5,  0.05,ADD, 1);
	wait(30);
	PlaySE(SE_LASER2);
	effect_byakuren01(0, 0,0, 96,352,320,11/7,0.9,3,-0.025,ADD, 1);
	wait(45);
	PlaySE(SE_SHA);
	effect_byakuren02(-90, 30,0,0,96,96,1,0.4,  0, 0.3,40,ALPHA, 1);
	effect_byakuren02( 90, 30,0,0,96,96,1,0.4,180,-0.3,40,ALPHA, 1);
	effect_byakuren02( 45,-60,0,0,96,96,1,0.4,210, 0.3,40,ALPHA, 1);
	effect_byakuren02(-45,-60,0,0,96,96,1,0.4, 30,-0.3,40,ALPHA, 1);
}
