let DIR = GetCurrentScriptDirectory();
let typeEnd = 0;
let END_FAILED = 1;
let END_SUCCESS = 2;

let BossDf = 90;

@Initialize
{
	MagicCircle();
	SpellCardAttackEffect();
	SetAutoDeleteObject(true);
}

@MainLoop
{
	yield;
}

@Event
{
	alternative(GetEventType())
	case(EV_END_BOSS_STEP)
	{
		//次の敵動作へ
		if(typeEnd == 0)
		{
			//スペルカード取得失敗
			typeEnd = END_FAILED;
		}
		CloseScript(GetOwnScriptID());
	}
	case(EV_GAIN_SPELL)
	{
		//スペルカード取得
		typeEnd = END_SUCCESS;
	}
}

let imgEffLine = DIR ~ "img/eff_line.png";

// ===========================================================
// 風神録以降スペルカード魔法陣エフェクト
// ===========================================================

task MagicCircle{
	let objScene = GetEnemyBossSceneObjectID();
	let GetTimers = ObjEnemyBossScene_GetInfo(objScene, INFO_TIMERF);
	let objEnemy = GetEnemyBossObjectID()[0];
	let S_Circle_r = 180;		//初期半径
	let way = 54;
	let way1 = 3;
	let way2 = 9;
	let close_f = BossDf-1;
	let c_Alpha_Rate = 0.55;
	
	//外円「Border of Life」
	task Spell_CircleA(layer){
		let e_r_flag = true;
		let e_r = 0;
		let e_r_d = 15;
		let e_r_dd = -0.3;
		let e_ang = 0;
		let e_ang_d = 3.5;
		let e_ang_sub = 360/24;
		let alpha = 255;
		let cx = ObjMove_GetX(objEnemy);
		let cy = ObjMove_GetY(objEnemy);
	
		let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
		ObjPrim_SetVertexCount(obj, way);
		ObjRender_SetFogEnable(obj,false);
		ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
		Obj_SetRenderPriorityI(obj, layer);
		ObjPrim_SetTexture(obj, imgEffLine);
	
		ascent( i in 0..way1){
			ascent( j in 0..way2){
				ObjPrim_SetVertexUVT(obj, i*18+2*j, j/8*256,161);
				ObjPrim_SetVertexUVT(obj, i*18+2*j+1, j/8*256,191);
	
				ObjPrim_SetVertexPosition(obj, i*18+2*j, (e_r+16+2)*cos(120*i+e_ang_sub*j), (e_r+16+2)*sin(120*i+e_ang_sub*j),0);
				ObjPrim_SetVertexPosition(obj, i*18+2*j+1, (e_r+0+2)*cos(120*i+e_ang_sub*j), (e_r+0+2)*sin(120*i+e_ang_sub*j),0);
			}
		}
		ObjRender_SetColor(obj, alpha*c_Alpha_Rate, alpha*c_Alpha_Rate, alpha*c_Alpha_Rate);
	
		while(typeEnd == 0){
			GetTimers = ObjEnemyBossScene_GetInfo(objScene, INFO_TIMERF);
			e_r += e_r_d;
			e_r_d += e_r_dd;
			if((e_r <= S_Circle_r)&&(e_r_d < 0)&&(e_r_flag)){
				e_r = S_Circle_r;
				e_r_d = -(S_Circle_r-16)/(GetTimers-90);
				e_r_dd = 0;
				e_r_flag = false;
			}
			e_r = absolute(e_r);
			ascent( i in 0..way1){
				ascent( j in 0..way2){
					ObjPrim_SetVertexPosition(obj, i*18+2*j, (e_r+16+2)*cos(120*i+e_ang_sub*j), (e_r+16+2)*sin(120*i+e_ang_sub*j),0);
					ObjPrim_SetVertexPosition(obj, i*18+2*j+1, (e_r+0+2)*cos(120*i+e_ang_sub*j), (e_r+0+2)*sin(120*i+e_ang_sub*j),0);
				}
			}
			e_ang += e_ang_d;
			if(e_ang > 360){e_ang += -360;}
			if(e_ang < -360){e_ang += 360;}
			ObjRender_SetAngleZ(obj,e_ang);
			if(!Obj_IsDeleted(objEnemy))
			{
				cx = cx * 0.95 + ObjMove_GetX(objEnemy) * 0.05;
				cy = cy * 0.95 + ObjMove_GetY(objEnemy) * 0.05;
			}
			ObjRender_SetPosition(obj, cx, cy, 0);
			ObjRender_SetColor(obj, alpha*c_Alpha_Rate, alpha*c_Alpha_Rate, alpha*c_Alpha_Rate);
			yield;
		}
		task Ring_SpGet{
			S_Circle_r = 180/2;
			e_r = S_Circle_r;
			e_r_d = 15*2;
			e_r_dd = -0.3*2;
			loop(90/2){
				e_r -= e_r_d;
				e_r_d -= e_r_dd;
				e_r = absolute(e_r);
				descent( i in 0..way1){
					descent( j in 0..way2){
						ObjPrim_SetVertexUVT(obj, i*18+2*j, j/8*256,65);
						ObjPrim_SetVertexUVT(obj, i*18+2*j+1, j/8*256,95);
						ObjPrim_SetVertexPosition(obj, i*18+2*j, (e_r+16+2)*cos(120*i+e_ang_sub*j), (e_r+16+2)*sin(120*i+e_ang_sub*j),0);
						ObjPrim_SetVertexPosition(obj, i*18+2*j+1, (e_r+0+2)*cos(120*i+e_ang_sub*j), (e_r+0+2)*sin(120*i+e_ang_sub*j),0);
					}
				}
				yield;
			}
		}
		function SpellCircleMoveAnima
		{
			let frame = 0;
			let alp = 1;
			let cx = ObjMove_GetX(objEnemy);
			let cy = ObjMove_GetY(objEnemy);
	
			S_Circle_r = 180;
			Ring_SpGet;
			loop(close_f)
			{
				cx = GetPlayerX();
				cy = GetPlayerY();
		
				if(frame >= 45)
				{
					alp -= 1 / 45;
					//alpha = max(alpha, 0);
				}
				e_ang += e_ang_d;
				if(e_ang > 360){e_ang += -360;}
				if(e_ang < -360){e_ang += 360;}
				ObjRender_SetAngleZ(obj,e_ang);

				
				ObjRender_SetPosition(obj, cx, cy, 0);
				ObjRender_SetColor(obj, alpha*alp*c_Alpha_Rate, alpha*alp*c_Alpha_Rate, alpha*alp*c_Alpha_Rate);
				frame++;
				yield;
			}
		}
	
		if(typeEnd == END_SUCCESS){SpellCircleMoveAnima;}
		Obj_Delete(obj);
	}

	//内円「Spell Card Attack of Little Girl...」
	task Spell_CircleB(layer){
		let e_r = 0;
		let e_ang_d = -3.5;
		let e_ang = 0;
		let e_ang_sub = 360/24;
		let e_alp = 255;
		let alpha = 255;
	
		let cx = ObjMove_GetX(objEnemy);
		let cy = ObjMove_GetY(objEnemy);
	
		let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
		ObjPrim_SetTexture(obj, imgEffLine);
	
		ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
		ObjPrim_SetVertexCount(obj, way);
		ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
		ObjRender_SetFogEnable(obj,false);
		Obj_SetRenderPriorityI(obj, layer);
	
		ascent( i in 0..way1){
			ascent( j in 0..way2){
				ObjPrim_SetVertexUVT(obj, i*18+2*j, j/8*256,97);
				ObjPrim_SetVertexUVT(obj, i*18+2*j+1, j/8*256,127);
				ObjPrim_SetVertexPosition(obj, i*18+2*j, e_r*cos(120*i+e_ang_sub*j), e_r*sin(120*i+e_ang_sub*j),0);
				ObjPrim_SetVertexPosition(obj, i*18+2*j+1, e_r*cos(120*i+e_ang_sub*j), e_r*sin(120*i+e_ang_sub*j),0);
			}
		}
		ObjRender_SetColor(obj, alpha*c_Alpha_Rate, alpha*c_Alpha_Rate, alpha*c_Alpha_Rate);
	
	
		//外周の頂点操作
		task e_vt_out{
			let e_r_sub = 0;
	
			let e_r_sub_ang = 0;
			let e_r_sub_ang_d = 180/90;
			loop(90){
				e_r_sub_ang += e_r_sub_ang_d;
				e_r_sub = (S_Circle_r+6)/2 - (S_Circle_r+6)/2*cos(e_r_sub_ang);
				ascent( i in 0..way1){
					ascent( j in 0..9){
						ObjPrim_SetVertexPosition(obj, i*18+2*j, e_r_sub*cos(120*i+e_ang_sub*j), e_r_sub*sin(120*i+e_ang_sub*j),0);
					}
				}
				yield;
			}
		}
	
		//内周の頂点操作
		task e_vt_in{
			let e_r_sub = -90;
			let e_r_sub_ang = 0;
			let e_r_sub_ang_d = 180/30;
	
			loop(60){
				e_r_sub_ang += e_r_sub_ang_d;
				e_r_sub += 90/60;
				ascent( i in 0..way1){
					ascent( j in 0..9){
						ObjPrim_SetVertexPosition(obj, i*18+2*j+1, e_r_sub*cos(120*i+e_ang_sub*j), e_r_sub*sin(120*i+e_ang_sub*j),0);
					}
				}
				yield;
			}
			loop(30){
				e_r_sub_ang += e_r_sub_ang_d;
				e_r_sub = (S_Circle_r-10)/2 - (S_Circle_r-10)/2*cos(e_r_sub_ang);
				ascent( i in 0..way1){
					ascent( j in 0..9){
						ObjPrim_SetVertexPosition(obj, i*18+2*j+1, e_r_sub*cos(120*i+e_ang_sub*j), e_r_sub*sin(120*i+e_ang_sub*j),0);
					}
				}
				yield;
			}
		}
		//後は徐々に半径を小さく。
		task e_vt_reduce{
			e_r = S_Circle_r+6;
			let e_r_d = -(S_Circle_r-16)/(GetTimers-90);
	
			loop(90){yield;}
			while(typeEnd == 0){
				e_r += e_r_d;
				e_r = absolute(e_r);
				ascent( i in 0..way1){
					ascent( j in 0..9){
						ObjPrim_SetVertexPosition(obj, i*18+2*j, e_r*cos(120*i+e_ang_sub*j), e_r*sin(120*i+e_ang_sub*j),0);
						ObjPrim_SetVertexPosition(obj, i*18+2*j+1, (e_r-16)*cos(120*i+e_ang_sub*j), (e_r-16)*sin(120*i+e_ang_sub*j),0);
					}
				}
				yield;
			}
		}
		task e_vt_out_spGet{
			S_Circle_r = 180/2;
			let e_r_sub = (S_Circle_r+6)/2;
			let e_r_sub_ang = 180;
			let e_r_sub_ang_d = 180/90*2;
			loop(90/2){
				e_r_sub_ang -= e_r_sub_ang_d;
				e_r_sub = (S_Circle_r+6) - (S_Circle_r+6)/2*cos(e_r_sub_ang*2);
				descent( i in 0..way1){
					descent( j in 0..9){
						ObjPrim_SetVertexPosition(obj, i*18+2*j, e_r_sub*cos(120*i+e_ang_sub*j), e_r_sub*sin(120*i+e_ang_sub*j),0);
					}
				}
				yield;
			}
		}
	
		//内周の頂点操作
		task e_vt_in_spGet{
			S_Circle_r = 180/2;
			let e_r_sub = (S_Circle_r-10)/2;
			let e_r_sub_ang = 180;
			let e_r_sub_ang_d = 180/30*2;
			loop(30/2){
				e_r_sub_ang -= e_r_sub_ang_d;
				e_r_sub = 0 - (S_Circle_r-10)/2*cos(e_r_sub_ang);
				descent( i in 0..way1){
					descent( j in 0..9){
						ObjPrim_SetVertexPosition(obj, i*18+2*j+1, e_r_sub*cos(120*i+e_ang_sub*j), e_r_sub*sin(120*i+e_ang_sub*j),0);
					}
				}
				yield;
			}
		}
	
	function SpellCircleMoveAnima
	{
		let rRate = 1.0;
		let frame = 0;
		let alp = 1;
		let cx = ObjMove_GetX(obj);
		let cy = ObjMove_GetY(obj);
		let rRateOut = 1.0;
		let rRateIn = 1.0;
		let er = e_r;
		S_Circle_r = 180;
		e_vt_out_spGet;
		e_vt_in_spGet;
		loop(close_f)
		{
			cx = GetPlayerX();
			cy = GetPlayerY();
			if(frame >= 45)
			{
				alp -= 1 / 45;
			}
			if(frame <= 45)
			{
				//魔方陣拡大
				S_Circle_r += (180-16)/45;
			}
			else
			{
				S_Circle_r -= (180-16)/(close_f-45-30);
			}
	
			e_ang += e_ang_d;
			if(e_ang > 360){e_ang += -360;}
			if(e_ang < -360){e_ang += 360;}
			ObjRender_SetAngleZ(obj,e_ang);
			
			ObjRender_SetPosition(obj, cx, cy, 0);
			ObjRender_SetColor(obj, alpha*alp*c_Alpha_Rate, alpha*alp*c_Alpha_Rate, alpha*alp*c_Alpha_Rate);
			frame++;
			yield;
		}
	}
	
	
		//頂点操作
		e_vt_in;
		e_vt_out;
		e_vt_reduce;
	
		//回転・移動
		while(typeEnd == 0){
			e_ang += e_ang_d;
			if(e_ang > 360){e_ang += -360;}
			if(e_ang < -360){e_ang += 360;}
			ObjRender_SetAngleZ(obj,e_ang);
			e_r = absolute(e_r);
	
			if(!Obj_IsDeleted(objEnemy))
			{
				cx = cx * 0.95 + ObjMove_GetX(objEnemy) * 0.05;
				cy = cy * 0.95 + ObjMove_GetY(objEnemy) * 0.05;
			}
			ObjRender_SetPosition(obj, cx, cy, 0);
			yield;
		}
		if(typeEnd == END_SUCCESS){SpellCircleMoveAnima;}
		Obj_Delete(obj);
	}

	Spell_CircleA(23);
	Spell_CircleB(23);

}
// ===========================================================
// 発動時の「SpellCardAttack!!」のエフェクト
// ===========================================================
task SpellCardAttackEffect{
	let x    = GetStgFrameWidth/2;
	let y    = GetStgFrameHeight/2 - 155;
	let dirL = -1;
	ascent( i in 0..10){
		AttackEffLine(x,y+(30*i), dirL);
		dirL *= -1;
	}

	AttackEffCircle(0 + 60, 0 + 60, 120,  1);
	AttackEffCircle(GetStgFrameWidth - 60, GetStgFrameHeight - 60, 130, -1);
	AttackEffCircle(GetStgFrameWidth - 60, GetStgFrameHeight - 60, 160,  1);
	AttackEffCircle(GetStgFrameWidth - 60, GetStgFrameHeight - 60, 190,  1);
//}
}

//直線描画制御
task AttackEffLine(x,y, dir) {
	let obj  = ObjPrim_Create(OBJ_PRIMITIVE_2D);

	let alpha = 0;
	let r     = 255;
	let g     = 255;
	let b     = 255;

	let u     = rand(0, 50);

	ObjRender_SetPosition(obj, x, y, 0);
	ObjPrim_SetTexture(obj, imgEffLine);
	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
	ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
	Obj_SetRenderPriorityI(obj, 23);

	ObjPrim_SetVertexCount(obj, 4);
	ObjPrim_SetVertexPosition(obj, 0, 400*cos(150) + 8*cos( 240), 400*sin(150) + 8*sin( 240),0);
	ObjPrim_SetVertexPosition(obj, 1, 400*cos(150) + 8*cos(  60), 400*sin(150) + 8*sin(  60),0);
	ObjPrim_SetVertexPosition(obj, 2, 400*cos(-30) + 8*cos(-120), 400*sin(-30) + 8*sin(-120),0);
	ObjPrim_SetVertexPosition(obj, 3, 400*cos(-30) + 8*cos(  60), 400*sin(-30) + 8*sin(  60),0);
	SetUV(0);


	function SetUV(u)
	{
		ObjPrim_SetVertexUVT(obj, 0,   0+u, 192);
		ObjPrim_SetVertexUVT(obj, 1,   0+u, 224);
		ObjPrim_SetVertexUVT(obj, 2,1600+u, 192);
		ObjPrim_SetVertexUVT(obj, 3,1600+u, 224);
		ObjRender_SetColor(obj,alpha,alpha,alpha);
	}


	loop(20) {
	  u     += 1.5 * dir;
	  alpha += 192/20;
	  SetUV(u);
	  yield;
	}

	loop(100) {
	  u += 1.5 * dir;
	  SetUV(u);
	  yield;
	}

	loop(30) {
	  u     += 1.5 * dir;
	  alpha -= 192/30;
	  SetUV(u);
	  yield;
	}

	Obj_Delete(obj);
}
//円形描画制御
task AttackEffCircle(x, y, radius, dir) {
	let obj  = ObjPrim_Create(OBJ_PRIMITIVE_2D);

	let angle = 0;

	let alpha = 0;
	let r     = 255;
	let g     = 255;
	let b     = 255;

	ObjRender_SetPosition(obj, x, y, 0);
	ObjPrim_SetTexture(obj, imgEffLine);
	ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
	ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
	Obj_SetRenderPriorityI(obj, 30);

	ObjPrim_SetVertexCount(obj, 32);
	ascent( i in 0..8) {
		ObjPrim_SetVertexPosition(obj, i*4,     (radius+8)*cos(angle+(45*i)),     (radius+8)*sin(angle+(45*i)),0);
		ObjPrim_SetVertexPosition(obj, (i*4)+1, (radius-8)*cos(angle+(45*i)),     (radius-8)*sin(angle+(45*i)),0);
		ObjPrim_SetVertexPosition(obj, (i*4)+2, (radius+8)*cos(angle+(45*(i+1))), (radius+8)*sin(angle+(45*(i+1))),0);
		ObjPrim_SetVertexPosition(obj, (i*4)+3, (radius-8)*cos(angle+(45*(i+1))), (radius-8)*sin(angle+(45*(i+1))),0);
	}

	ascent( i in 0..8) {
		ObjPrim_SetVertexUVT(obj, i*4,     256, 256);
		ObjPrim_SetVertexUVT(obj, (i*4)+1, 256, 224);
		ObjPrim_SetVertexUVT(obj, (i*4)+2,   0, 256);
		ObjPrim_SetVertexUVT(obj, (i*4)+3,   0, 224);
	}

	ObjRender_SetColor(obj,alpha,alpha,alpha);


	loop(20) {
	  angle += 1.5 * dir;
	  alpha += 192/20;

	  ascent( i in 0..8) {
		ObjPrim_SetVertexPosition(obj, i*4,     (radius+8)*cos(angle+(45*i)),     (radius+8)*sin(angle+(45*i)),0);
		ObjPrim_SetVertexPosition(obj, (i*4)+1, (radius-8)*cos(angle+(45*i)),     (radius-8)*sin(angle+(45*i)),0);
		ObjPrim_SetVertexPosition(obj, (i*4)+2, (radius+8)*cos(angle+(45*(i+1))), (radius+8)*sin(angle+(45*(i+1))),0);
		ObjPrim_SetVertexPosition(obj, (i*4)+3, (radius-8)*cos(angle+(45*(i+1))), (radius-8)*sin(angle+(45*(i+1))),0);
	  }
	  ObjRender_SetColor(obj,alpha,alpha,alpha);

	  yield;
	}

	loop(100) {
	  angle += 1.5 * dir;

	  ascent( i in 0..8) {
		ObjPrim_SetVertexPosition(obj, i*4,     (radius+8)*cos(angle+(45*i)),     (radius+8)*sin(angle+(45*i)),0);
		ObjPrim_SetVertexPosition(obj, (i*4)+1, (radius-8)*cos(angle+(45*i)),     (radius-8)*sin(angle+(45*i)),0);
		ObjPrim_SetVertexPosition(obj, (i*4)+2, (radius+8)*cos(angle+(45*(i+1))), (radius+8)*sin(angle+(45*(i+1))),0);
		ObjPrim_SetVertexPosition(obj, (i*4)+3, (radius-8)*cos(angle+(45*(i+1))), (radius-8)*sin(angle+(45*(i+1))),0);
	  }

	  yield;
	}

	loop(30) {
	  angle += 1.5 * dir;
	  alpha -= 192/30;

	  ascent( i in 0..8) {
		ObjPrim_SetVertexPosition(obj, i*4,     (radius+8)*cos(angle+(45*i)),     (radius+8)*sin(angle+(45*i)),0);
		ObjPrim_SetVertexPosition(obj, (i*4)+1, (radius-8)*cos(angle+(45*i)),     (radius-8)*sin(angle+(45*i)),0);
		ObjPrim_SetVertexPosition(obj, (i*4)+2, (radius+8)*cos(angle+(45*(i+1))), (radius+8)*sin(angle+(45*(i+1))),0);
		ObjPrim_SetVertexPosition(obj, (i*4)+3, (radius-8)*cos(angle+(45*(i+1))), (radius-8)*sin(angle+(45*(i+1))),0);
	  }
	  ObjRender_SetColor(obj,alpha,alpha,alpha);

	  yield;
	}

	Obj_Delete(obj);
}
