let dirCurrent = GetCurrentScriptDirectory();
let typeEnd = 0;
let END_FAILED = 1;
let END_SUCCESS = 2;
let t = 0;
let mul = 1;
let primcount = [50,50];

let maxRadius = 250;		//ring radius
let color = [255,255,255]; 	//ring color
let thickness = [0.18,0.18];	//thickness
let texture = [1, 128/3, 16];
@Initialize
{
	MagicCircle3();
}

@MainLoop
{
	t++;
	yield;
}

@Event
{
	alternative(GetEventType())
	case(EV_END_BOSS_STEP)
	{
		if(typeEnd == 0)
		{
			typeEnd = END_FAILED;
		}
	}
	case(EV_GAIN_SPELL)
	{
		typeEnd = END_SUCCESS;
	}
}

task MagicCircle3()
{
	let countVertex = 70;
	let listRadius = [];
	let path;
	loop(countVertex)
	{
		listRadius = listRadius ~ [0];
	}
	if(texture[0]==0){
		path = dirCurrent ~ "img/neppa_line.png";
	}
	else if(texture[0]==1){
		path = dirCurrent ~ "img/spell_eff_00.png";
	}
	let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetVertexCount(obj, countVertex);
	ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
	ObjRender_SetAlpha(obj, 200 );
	Obj_SetRenderPriorityI(obj, 40);
	ObjPrim_SetTexture(obj, path);
	ascent(iVert in 0..countVertex / 2)
	{
		let left = iVert * texture[1];
		let indexVert = iVert * 2;
		ObjPrim_SetVertexUVT(obj, indexVert + 0, left, 0);
		ObjPrim_SetVertexUVT(obj, indexVert + 1, left, texture[2]);	
	}

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

	let cx = 0;
	let cy = 0;
	let alpha = 0;
	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()
	{
		if(bLastSpell)
		{
			ObjRender_SetColor(obj, color[0] * alpha, color[1] * alpha, color[2] * alpha);
		}
		else
		{
			ObjRender_SetColor(obj, color[0] * alpha, color[1] * alpha, color[2] * alpha);
		}

		ObjRender_SetPosition(obj, cx, cy,0);
		ObjRender_SetAngleZ(obj, t*3);
	}

	let pathUseSpell = dirCurrent ~ "se/seUseSpellCard.wav";
	LoadSound(pathUseSpell);
	PlaySE(pathUseSpell);
	let rate = 0;
	while(typeEnd == 0)
	{
		if(rate<1){rate+=1/90;}
		if(!Obj_IsDeleted(objBoss))
		{
			//cx = cx * 0.95 + ObjRender_GetX(objBoss) * 0.05;
			//cy = cy * 0.95 + ObjRender_GetY(objBoss) * 0.05;
			cx = ObjRender_GetX(objBoss);
			cy = ObjRender_GetY(objBoss);
		}

		alpha += 1 / 120;
		alpha = min(alpha, 1);
		angleRender += 360 / countVertex / 4;

		let timer = ObjEnemyBossScene_GetInfo(objScene, INFO_CURRENT_LIFE);
		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]) / 8;
				listRadius[indexVert] = listRadius[indexVert] + dr;
			}
			if(frame > 45)
			{
				let rRateIn = rRate - thickness[0];
				if(bMiss)
				{
					rRateIn = rRate - thickness[1];
				}
				if(rRateIn < 0){rRateIn=0;}
				let dr= (maxRadius * rRateIn - listRadius[indexVert + 1]) / 16;
				listRadius[indexVert + 1] = listRadius[indexVert + 1] + dr;
			}

		}

		UpdateVertex();
		frame++;

		yield;
	}

	let pathGainSpell = dirCurrent ~ "se/seGetSpellCardBonus.wav";
	LoadSound(pathGainSpell);
	PlaySE(pathGainSpell);

	let rRate = 1.0;
	frame = 0;
	alpha = 1;
	let muki = 1;
	let m = 2;
	let sinmul = 0;
	let sinsub = 3;
	let radiusmul = 0.8;
	loop(105)
	{
		angleRender += 360 / countVertex / 4;
		let dx = (GetPlayerX() - cx) / 16;
		let dy = (GetPlayerY() - cy) / 16;
		cx += dx;
		cy += dy;
		if(frame >= 45)
		{
			//alpha -= 1 / 45;
			alpha = max(alpha, 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);

			let drOut = 0;
			let drIn = 0;
			if(frame <= 35)
			{
				let rRateOut = 1.0;
				drOut = (maxRadius*m * rRateOut - listRadius[indexVert]) / 4;

				let rRateIn = rRateOut - 0.08;
				if(rRateIn<0){rRateIn=0;}
				drIn = (maxRadius*m * rRateIn - listRadius[indexVert+1]) / 4;
			}
			else
			{
				if(sinmul<5){sinmul+=5/60;}
				sinsub-=3/600;
				radiusmul+=0.1;
				cx = GetPlayerX();
				cy = GetPlayerY();

				rRate -= 1.0 / 100.0;
				let rRateOut = rRate;
				drOut = (maxRadius * m * (sinsub-sin(angle*sinmul)) - listRadius[indexVert]) / 8;

				let rRateIn = rRate - radiusmul;
				if(rRateIn<0){rRateIn=0;}
				drIn=(maxRadius * (m*0.8) * (sinsub-sin(angle*sinmul)) - listRadius[indexVert+1])/8;
			}
			listRadius[indexVert] = listRadius[indexVert] + drOut;
			listRadius[indexVert + 1] = listRadius[indexVert + 1] + drIn;
		}
		m -= 2/96;
		UpdateVertex();
		frame++;
		yield;
	}
	Obj_Delete(obj);
	CloseScript(GetOwnScriptID());
}