let dirCurrent = GetCurrentScriptDirectory();
let typeEnd = 0;
let END_FAILED = 1;
let END_SUCCESS = 2;
let END_TIMEOUT = 3;
let Effect = GetCommonData("EFFECT",0);

let objScene = GetEnemyBossSceneObjectID();
let objBoss = GetEnemyBossObjectID();
let bLastSpell = ObjEnemyBossScene_GetInfo(objScene, INFO_IS_LAST_SPELL);
let bSurvivalSpell = ObjEnemyBossScene_GetInfo(objScene, INFO_IS_DURABLE_SPELL);

#include "./Sound.txt"

@Initialize
{
}

@MainLoop
{
	yield;
}

@Event
{
	alternative(GetEventType())
	case(EV_START_BOSS_STEP)
		{
		typeEnd = END_SUCCESS;
		}
	case(EV_PLAYER_SHOOTDOWN)
		{
		if(typeEnd == END_SUCCESS)
			{
			typeEnd = END_FAILED;
			}
		}
	case(EV_TIMEOUT)
		{
		typeEnd = END_TIMEOUT;
		}
	case(EV_END_BOSS_STEP)
		{
		if(typeEnd == END_TIMEOUT && bSurvivalSpell == false)
			{
			PlaySound("se_Laugh",110);
			}
		else if(typeEnd == END_FAILED)
			{
			PlaySound("se_SpellFail",100);
			PlaySound("se_SpellBreak",90);
			}
		else
			{
			PlaySound("se_SpellEnd",110);
			}
		if(Effect < 1)
			{
			MagicCircle;
			}
		else
			{
			TDel;
			}
		}
}

task MagicCircle()
{
	if(typeEnd == END_TIMEOUT && bSurvivalSpell == false)
		{
		loop(90){yield;}
		CloseScript(GetOwnScriptID());
		return;
		}
	if(typeEnd == END_FAILED)
		{
		loop(90){yield;}
		CloseScript(GetOwnScriptID());
		return;
		}
	let angleM = 180;
	let countVertex = 18;
	let listRadius = [];
	loop(countVertex)
		{
		listRadius = listRadius ~ [0];
		}

	let path = dirCurrent ~ "img/Default_MagicCircle.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.30);
	ObjPrim_SetTexture(obj, path);
	ascent(iVert in 0..countVertex / 2)
		{
		let left = iVert * 128;
		let indexVert = iVert * 2;
		ObjPrim_SetVertexUVT(obj, indexVert + 0, left, 0);
		ObjPrim_SetVertexUVT(obj, indexVert + 1, left, 64);		
		}

	Obj_SetVisible(obj,false);

	let cx = GetStgFrameWidth/2;
	let cy = GetStgFrameHeight/2;
	let maxRadius = 256 * 1.2;
	let alpha = 0;
	let frame = 0;
	let angleRender = 0;
	let angleI = rand(0,360);

	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, 255 * alpha, 192 * alpha, 192 * alpha);
		}
		else
		{
			ObjRender_SetColor(obj, 192 * alpha, 192 * alpha, 255 * alpha);
		}

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

	Obj_SetVisible(obj,true);
	let rRate = 1.0;
	frame = 0;
	alpha = 1;
	loop(105)
		{
		angleRender += 360 / countVertex/1.5;
		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+angleRender;

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

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

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

				let rRateIn = rRateOut - 0.08;
				if(rRateIn<0){rRateIn=0;}
				drIn = (maxRadius * rRateIn - listRadius[indexVert+1]) / 8;
				}
			else
				{
				cx = GetPlayerX();
				cy = GetPlayerY();

				rRate -= 1.0 / 60.0;
				let rRateOut = rRate * sin(angle % 60);
				drOut = (maxRadius * rRateOut - listRadius[indexVert]) / 16;

				let rRateIn = rRate * sin(angle % 60)-0.08;
				if(rRateIn<0){rRateIn=0;}
				drIn=(maxRadius * rRateIn - listRadius[indexVert+1])/16;
				}
			if(frame == 75)
				{
				PlaySound("se_SpellBreak",95);
				}	
			listRadius[indexVert] = listRadius[indexVert] + drOut;
			listRadius[indexVert + 1] = listRadius[indexVert + 1] + drIn;
			}

		UpdateVertex();
		frame++;
		yield;
		}

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

task TDel
	{
	loop(105){yield;}
	CloseScript(GetOwnScriptID());
	}