task MigicAbsorb
{
let ShortCount=0;
let LongCount=0;
loop
{
	ascent(let enemy in EnumEnemyBegin .. EnumEnemyEnd)
	{
		let enemyID = EnumEnemyGetID(enemy);
		if(((GetEnemyInfo(enemyID,ENEMY_X)-GetPlayerX)^2+(GetEnemyInfo(enemyID,ENEMY_Y)-GetPlayerY)^2)^0.5<=MagicAbsorbShortRange)
		{
			if(enemyID==0 && GetEnemyLife==0)
			{
			}
			else if(GetEnemyInfo(enemyID,ENEMY_X)>GetClipMinX && GetEnemyInfo(enemyID,ENEMY_X)<GetClipMaxX && GetEnemyInfo(enemyID,ENEMY_Y)>GetClipMinY && GetEnemyInfo(enemyID,ENEMY_Y)<GetClipMaxY)
			{
				if(ShortCount>=5)
				{
				CreateMigicItem(GetEnemyInfo(enemyID,ENEMY_X),GetEnemyInfo(enemyID,ENEMY_Y),1);
				ShortCount=0;
				}
			ShortCount+=1;
			}
		}
		else if(((GetEnemyInfo(enemyID,ENEMY_X)-GetPlayerX)^2+(GetEnemyInfo(enemyID,ENEMY_Y)-GetPlayerY)^2)^0.5<=MagicAbsorbLongRange)
		{
			if(enemyID==0 && GetEnemyLife==0)
			{
			}
			else if(GetEnemyInfo(enemyID,ENEMY_X)>GetClipMinX && GetEnemyInfo(enemyID,ENEMY_X)<GetClipMaxX && GetEnemyInfo(enemyID,ENEMY_Y)>GetClipMinY && GetEnemyInfo(enemyID,ENEMY_Y)<GetClipMaxY)
			{
				if(LongCount>=5)
				{
				CreateMigicItem(GetEnemyInfo(enemyID,ENEMY_X),GetEnemyInfo(enemyID,ENEMY_Y),2);
				LongCount=0;
				}
			LongCount+=1;
			}
		}
	}

yield;
}
}

task CreateMigicItem(x,y,type)
{
		let obj=Obj_Create(OBJ_EFFECT);
		ObjEffect_SetTexture(obj, imgItem);
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjEffect_SetRenderState(obj, ADD); 
		ObjEffect_CreateVertex(obj, 4);
		ObjEffect_SetVertexXY(obj, 0, -12, -12);
		ObjEffect_SetVertexXY(obj, 1, 12,  -12);
		ObjEffect_SetVertexXY(obj, 2, 12, 12);
		ObjEffect_SetVertexXY(obj, 3,  -12,  12);
		
		ObjEffect_SetVertexUV(obj, 0,  0,  0);
		ObjEffect_SetVertexUV(obj, 1,  64, 0);
		ObjEffect_SetVertexUV(obj, 2, 64,  64);
		ObjEffect_SetVertexUV(obj, 3, 0, 64);
		ObjEffect_SetScale(obj,1.2,1.2);

			ascent(let i in 0..4)
			{
				ObjEffect_SetVertexColor(obj,i,155,255,255,255);
			}	

		ObjEffect_SetLayer(obj,3);
		Obj_SetPosition(obj,x,y);
		let StandardSpeed=rand(3,9);
		let speed=StandardSpeed;
		let angle=rand(0,360);

		let lightangle=rand(0,360);
		let Scale;
		let DiScale;
		let R;
		let G;
		let B;
		let PowerPlus;
		let amp=1;
		let DifficultChargeAmp=1;
		let DifficultCounterAmp=1;


		alternative(GetCommonDataDefault("SELECTEDDIFFICULT","None"))
		case("Moderate")
		{
			DifficultChargeAmp=1-BurstRank*0.1;
			DifficultCounterAmp=1;
		}
		case("Extream")
		{
			DifficultChargeAmp=1.0-BurstRank*0.10;
			DifficultCounterAmp=2;
		}
		case("Apocalypse")
		{
			DifficultChargeAmp=1.0-BurstRank*0.10;
			DifficultCounterAmp=4;
		}
		others{}

		if(type==1)
		{
			Scale=1.5;
			DiScale=0.015;
			R=127;
			G=255;
			B=255;
			PowerPlus=7.5;
		}
		if(type==2)
		{
			Scale=1;
			DiScale=0.005;
			R=127;
			G=127;
			B=255;
			PowerPlus=1.0;
		}
		if(type==3)
		{
			Scale=2;
			DiScale=0.020;
			R=255;
			G=127;
			B=127;
			PowerPlus=1;
		}
		if(type==4)
		{
			Scale=2;
			DiScale=0.020;
			R=255;
			G=255;
			B=255;
			PowerPlus=1;
		}
			DiScale=0.00;

		loop(5)
		{
			lightangle+=12;
			ascent(let i in 0..4)
			{
				ObjEffect_SetVertexColor(obj,i,205+50*cos(lightangle),R,G,B);
			}
			Obj_SetPosition(obj,Obj_GetX(obj)+speed*cos(angle),Obj_GetY(obj)+speed*sin(angle));
			speed-=StandardSpeed/15;
			Scale-=DiScale*2;
			ObjEffect_SetScale(obj,Scale,Scale);
			wait(1);
		}
		while(!Obj_BeDeleted(obj))
		{
			Scale-=DiScale;
			ObjEffect_SetScale(obj,Scale,Scale);
			if(speed<10)
			{
			speed+=0.25;
			}
			lightangle+=12;
			ascent(let i in 0..4)
			{
				ObjEffect_SetVertexColor(obj,i,205+50*cos(lightangle),R,G,B);
			}
			let sangle=atan2(GetPlayerY-Obj_GetY(obj),GetPlayerX-Obj_GetX(obj));
			Obj_SetPosition(obj,Obj_GetX(obj)+speed*cos(sangle),Obj_GetY(obj)+speed*sin(sangle));
			if(((Obj_GetX(obj)-GetPlayerX)^2+(Obj_GetY(obj)-GetPlayerY)^2)^0.5<10)
			{
				if(Power<=1000){amp=1;}
				else if(Power<=2000){amp=2/3;}
				else if(Power<=3000){amp=1/3;}
				if(Power<3000)
				{
				ReChargePower+=PowerPlus*amp*DifficultChargeAmp;
				}
				if(type==3)
				{
					MagicCounter+=PowerPlus*3*DifficultCounterAmp;
					BurstRank-=0.001*(1+BurstRank/10);
				}
				else
				{
					MagicCounter+=PowerPlus*DifficultCounterAmp;
				}
				if(type==4)
				{
				AddScore(MagicCounter*10);
				BurstRank-=0.001*(1+floor(BurstRank)/2);
				}
				else
				{
				AddScore(trunc(MagicCounter*PowerPlus/10));
				}
				//PlaySE(SE[3]);
				Obj_Delete(obj);
			}
		yield;
		}
}