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


@Initialize
{
	//MagicCircle();
}

@MainLoop
{
	yield;
}

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

task MagicCircle()
{
	//頂点奇数：偶数：円の外側、円の内側
	let countVertex = 64;
	let listRadius = [];
	loop(countVertex)
	{
		listRadius = listRadius ~ [0];
	}

	let path = dirCurrent ~ "img/eff_line.png";
	let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetVertexCount(obj, countVertex);
	ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
	Obj_SetRenderPriority(obj, 0.3);
	ObjPrim_SetTexture(obj, path);
	let y = 0;
	ascent(iVert in 0..countVertex / 2)
	{	

		y += 5;
		let indexVert = iVert * 2;
		ObjPrim_SetVertexUVT(obj, indexVert + 0, 16, y);
		ObjPrim_SetVertexUVT(obj, indexVert + 1, 32, y);		
	}

	let objScene = GetEnemyBossSceneObjectID();
	let objBoss = GetEnemyBossObjectID()[0];
	let timerOrg = ObjEnemyBossScene_GetInfo(objScene, INFO_ORGTIMERF);
	let bLastSpell = ObjEnemyBossScene_GetInfo(objScene, INFO_IS_LAST_SPELL);

	let cx = 0;
	let cy = 0;
	let maxRadius = 256;
	let frame = 0;
	let angleRender = 0;

	function GetPlayerX()
	{
		let objPlayer = GetPlayerObjectID();
		return ObjRender_GetX(objPlayer);
	}

	function GetPlayerY()
	{
		let objPlayer = GetPlayerObjectID();
		return ObjRender_GetY(objPlayer);
	}

	function UpdateVertex()
	{

		ObjRender_SetPosition(obj, cx, cy, 0);
		ObjRender_SetAngleZ(obj, angleRender);
	}

	/*let pathUseSpell = dirCurrent ~ "se/seUseSpellCard.wav";
	LoadSound(pathUseSpell);
	PlaySE(pathUseSpell);*/

	while(typeEnd == 0)
	{
		if(!Obj_IsDeleted(objBoss))
		{
			cx = ObjRender_GetX(objBoss);
			cy = ObjRender_GetY(objBoss);
		}

		angleRender += 360 / countVertex / 4;

		let timer = ObjEnemyBossScene_GetInfo(objScene, INFO_TIMERF);
		let rRate = timer / timerOrg;
		let bMiss = ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SHOOTDOWN_COUNT) > 0 ||
					 ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SPELL_COUNT) > 0 ;

		ascent(iVert in 0..countVertex / 2)
		{
			let indexVert = iVert * 2;
			let angle = 360 / (countVertex / 2 - 1) * iVert;

			let vx1 = listRadius[indexVert] * cos(angle);
			let vy1 = listRadius[indexVert] * sin(angle);
			ObjPrim_SetVertexPosition(obj, indexVert + 0, vx1, vy1, 0);

			let vx2 = listRadius[indexVert+1] * cos(angle);
			let vy2 = listRadius[indexVert+1] * sin(angle);
			ObjPrim_SetVertexPosition(obj, indexVert + 1, vx2, vy2, 0);

			//魔方陣拡大
			if(frame >= 0)
			{//外側
				let dr = (maxRadius * rRate - listRadius[indexVert]) / 32;
				listRadius[indexVert] = listRadius[indexVert] + dr;
				let rRateIn = rRate - 0.08;
				if(bMiss)
				{
					rRateIn = rRate - 0.04;
				}
				if(rRateIn < 0){rRateIn=0;}
				let dr2= (maxRadius * rRateIn - listRadius[indexVert + 1]) / 64;
				listRadius[indexVert + 1] = listRadius[indexVert + 1] + dr2;
				Obj_SetVisible(obj, false);
			}
			if(frame > 1)
			{//内側
				Obj_SetVisible(obj, true);
				let rRateIn = rRate - 0.08;
				if(bMiss)
				{
					rRateIn = rRate - 0.04;
				}
				if(rRateIn < 0){rRateIn=0;}
				let dr= (maxRadius * rRateIn - listRadius[indexVert + 1]) / 100;
				listRadius[indexVert + 1] = listRadius[indexVert + 1] + dr;
			}

		}

		UpdateVertex();
		frame++;

		yield;
	}

	//スペルカード取得失敗
	if(typeEnd == END_FAILED)
	{
		Obj_Delete(obj);
		CloseScript(GetOwnScriptID());
		return;
	}

	let rRate = 1.0;
	frame = 0;
	//スペルカード取得成功

	Obj_Delete(obj);
	CloseScript(GetOwnScriptID());
	
}


