let dirCurrent = GetCurrentScriptDirectory;
let path1 = dirCurrent ~ "img/effect3/effect35.png";
let path2 = dirCurrent ~ "img/effect3/effect36.png";
let typeEnd = 0;
let END_FAILED = 1;
let END_SUCCESS = 2;
let IsTimeStopped = false;
let TimeSpeed = 1;

@Initialize{
	MagicCircle(path1, 192 * 1.125, 192 * 1.125, -2.5);
	MagicCircle(path2, 192 * 1.225, 512, 2.5);
}

@MainLoop{
	if(GetCommonData("TimeStop", 0)>0){IsTimeStopped=true}else{IsTimeStopped=false}
	TimeSpeed = GetCommonData("TimeSlow", 1);
	yield;
}

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

@Finalize{
}

task MagicCircle(path, RealmaxRadius, radset, angturn){
	loop(3){yield}
	let countVertex = 64;
	let listRadius = [];
	loop(countVertex){
		listRadius = listRadius ~ [0];
	}

	let obj = ObjPrim_Create(OBJ_PRIMITIVE_2D);
	ObjPrim_SetPrimitiveType(obj, PRIMITIVE_TRIANGLESTRIP);
	ObjPrim_SetVertexCount(obj, countVertex);
	ObjRender_SetBlendType(obj, BLEND_ALPHA);
	ObjRender_SetAlpha(obj, 155);
	Obj_SetRenderPriority(obj, 0.28);
	ObjPrim_SetTexture(obj, path);
	ascent(iVert in 0..countVertex / 2){
		let left = iVert * 24;
		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_ORGTIMERF);

	let cx = ObjRender_GetY(objBoss);
	let cy = ObjRender_GetX(objBoss);
	let alpha = 155;
	let frame = 0;
	let angleRender = 0;
	let maxRadius = radset;

	while(typeEnd == 0){
		if(IsTimeStopped==false){
			if(!Obj_IsDeleted(objBoss)){
				let by = ObjRender_GetY(objBoss);
				let bx = ObjRender_GetX(objBoss);
				let my = ObjRender_GetY(obj);
				let mx = ObjRender_GetX(obj);				
				if(cx > bx+3 || cy > by+3 || cx < bx-3 || cy < by-3){ 
					cx = cx+(GetObjectDistance(obj, objBoss)/16)*cos(atan2(by-my, bx-mx));
					cy = cy+(GetObjectDistance(obj, objBoss)/16)*sin(atan2(by-my, bx-mx));
				}
			}

			if(frame>45){
				if(maxRadius>RealmaxRadius){
					maxRadius-=16/TimeSpeed;
				}
				if(maxRadius<RealmaxRadius){
					maxRadius+=2/TimeSpeed;
				}
			}
			
			alpha += 1/120;
			alpha = min(alpha, 1);
			angleRender += (360 / countVertex / angturn)/TimeSpeed;

			let timer = ObjEnemyBossScene_GetInfo(objScene, INFO_TIMERF);
			let rRate = timer/timerOrg+(angturn/200);
			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 left = iVert * truncate(8+maxRadius/16);
				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]) / 16;
					listRadius[indexVert] = listRadius[indexVert] + dr;
				}
				if(frame > 1){
					let rRateIn = rRate - 0.1;
					if(rRateIn < 0){rRateIn=0;}
					let dr= (maxRadius * rRateIn - listRadius[indexVert + 1]) / 12;
					listRadius[indexVert + 1] = listRadius[indexVert + 1] + dr;
				}
				
				ObjPrim_SetVertexUVT(obj, indexVert + 0, left, 0);
				ObjPrim_SetVertexUVT(obj, indexVert + 1, left, 16);		

			}

			UpdateVertex;
			frame++;
		}
		yield;
	}

	if(typeEnd == END_FAILED){
	let pathFailSpell = dirCurrent ~ "se/spellfail.wav";
	let sound = ObjSound_Create();
	ObjSound_Load(sound,pathFailSpell);
	ObjSound_SetVolumeRate(sound,90);
	ObjSound_Play(sound);
		FailedSpellCard;
	}else{
	let pathGainSpell = dirCurrent ~ "se/spellget.wav";
	let sound = ObjSound_Create();
	ObjSound_Load(sound,pathGainSpell);
	ObjSound_SetVolumeRate(sound,90);
	ObjSound_Play(sound);
	}

	Obj_Delete(obj);
	
	task UpdateVertex{
		ObjRender_SetPosition(obj, cx, cy, 0);
		ObjRender_SetAngleZ(obj, angleRender);
	}
}

task FailedSpellCard{
	let path = GetCurrentScriptDirectory ~ "img/frame2.png";
	let objText = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(objText, path);
	ObjSprite2D_SetSourceRect(objText, 0, 387, 264, 419);
	ObjSprite2D_SetDestRect(objText, 0, 0, 264, 32);
	Obj_SetRenderPriority(objText, 0.8);
	ObjRender_SetX(objText, 64);
	ObjRender_SetY(objText, 98);


	loop(120){
		yield;
	}
	let alpha = 255;
	while(alpha>0){
		alpha-=15;
		ObjRender_SetAlpha(objText, alpha);
		yield;
	}
	Obj_Delete(objText);
	loop(230){yield}
	CloseScript(GetOwnScriptID);
}



