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

let maxRadius = 250;		//ring radius
let color = [255,255,255]; 	//ring color

@Initialize{
	MagicCircle();
}

@MainLoop{
	color = GetCommonData("MCircleColor",[255,255,255]);
	t++;
	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/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);
	Obj_SetRenderPriorityI(obj, 28);
	ObjPrim_SetTexture(obj, path);
	ascent(iVert in 0..countVertex / 2){
		let left = iVert * 128 / 2;
		let indexVert = iVert * 2;
		ObjPrim_SetVertexUVT(obj, indexVert + 0, left, 0);
		ObjPrim_SetVertexUVT(obj, indexVert + 1, left, 16);	
	}

	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 = ObjMove_GetX(objBoss);
	let cy = ObjMove_GetY(objBoss);
	//let maxRadius = 256 * 1.2;
	let alpha = 0;
	let frame = 0;
	let angleRender = 0;
	let t = 0;

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

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

	function UpdateVertex(){
		t++;
		ObjRender_SetColor(obj, color[0] * alpha, color[1] * alpha, color[2] * alpha);
		ObjRender_SetPosition(obj, cx, cy,0);
		ObjRender_SetAngleZ(obj, t*5);
	}

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

    	let moveRate = 120;
	
    	while(typeEnd == 0){
		if(moveRate>8){moveRate -= (moveRate - 8)/90;}
		if(!Obj_IsDeleted(objBoss)){
			cx = cx * 0.95 + ObjRender_GetX(objBoss) * 0.05;
			cy = cy * 0.95 + ObjRender_GetY(objBoss) * 0.05;
		}

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

		let timer = ObjEnemyBossScene_GetInfo(objScene, INFO_CURRENT_LIFE);
		let rRate = timer / timerOrg + 0.06;
		let bMiss = ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SHOOTDOWN_COUNT) > 0 ||
					 ObjEnemyBossScene_GetInfo(objScene, INFO_PLAYER_SPELL_COUNT) > 0 ;
					 
		let rateMulti = 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 rRateIn = rRate - 0.15;
			if(bMiss){
				rRateIn = rRate - 0.13;
			}
			let dr1 = (maxRadius * rRate - listRadius[indexVert]) / moveRate;
			listRadius[indexVert] = listRadius[indexVert] + dr1;
			let dr2 = (maxRadius * rRateIn - listRadius[indexVert + 1]) / moveRate;
			listRadius[indexVert + 1] = listRadius[indexVert + 1] + dr2;
		}
		if(rateMulti<1){rateMulti+=1/40;}
		UpdateVertex();
		frame++;
		yield;
	}

		if(typeEnd == END_FAILED){
			Obj_Delete(obj);
   			CloseScript(GetOwnScriptID());
			return;
		}

		if(typeEnd == END_SUCCESS){
			loop( 190 ){ 
			Obj_SetVisible( obj, false ); 
			ObjRender_SetPosition( obj, ObjMove_GetX( objBoss ), ObjMove_GetY( objBoss ), 0);
			yield; 
			}
			Obj_SetVisible( obj, true ); 
		}

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

	let rRate = 0;
	frame = 0;
	alpha = 1;

	let muki = 1;
	let va = 80;
	maxRadius = 1200;
	let add_rate = 1;
	
	loop(50){
		angleRender += 360 / countVertex / 4;
		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 rRateIn = max(0,rRate-0.2);
			let dr1 = (maxRadius * rRate + (va * (cos(angle*5))) - listRadius[indexVert]);
			listRadius[indexVert] = listRadius[indexVert] + dr1;
			let dr2 = (maxRadius * rRateIn + (va * (cos(angle*5))) - listRadius[indexVert + 1]);
			listRadius[indexVert + 1] = listRadius[indexVert + 1] + dr2;
		}
		//maxRadius += add_rate;
		//add_rate += 0.8;
		rRate += 1/40;
		if(va>0){va-=80/30;}
		UpdateVertex;
		frame++;
		yield;
	}
	//alpha = 0.6;
	maxRadius = 1200;
	rRate = 1;
	cx = GetPlayerX;
	cy = GetPlayerY;
	va = 80;
	loop(70){
		cx = GetPlayerX;
		cy = GetPlayerY;
		angleRender += 360 / countVertex / 4;
		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 rRateIn = max(rRate - 0.2,0);
			let dr1 = (maxRadius * rRate + (va * (cos(angle*5)))) - listRadius[indexVert];
			listRadius[indexVert] = listRadius[indexVert] + dr1;
			let dr2 = (maxRadius * rRateIn + (va * (cos(angle*5)))) - listRadius[indexVert + 1];
			listRadius[indexVert + 1] = listRadius[indexVert + 1] + dr2;
		}
		//maxRadius -= 1500/90;
		rRate -= 1/70;
		if(va>0){va-=80/50;}
		UpdateVertex;
		frame++;
		yield;
	}
	Obj_Delete(obj);
	CloseScript(GetOwnScriptID());
}