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 Alpha=155;
		let VAlpha=50;
		let PowerPlus;
		let CounterPlus;
		let CounterPlusOnBurst;
		let ScorePlus;
		let FragmentPlus=1;
		let amp=1;
		let DifficultChargeAmp=1;
		let DifficultCounterAmp=1;
		let DifficultScoreAmp=1;
		let DifficultFragmentAmp=1;


		alternative(GetCommonDataDefault("SELECTEDDIFFICULT","None"))
		case("Moderate")
		{
			DifficultChargeAmp=1;
			DifficultCounterAmp=1;
			DifficultScoreAmp=1;
			DifficultFragmentAmp=1;
		}
		case("Extream")
		{
			DifficultChargeAmp=1.5;
			DifficultCounterAmp=1.5;
			DifficultScoreAmp=1.5;
			DifficultFragmentAmp=1.5;
		}
		case("Apocalypse")
		{
			DifficultChargeAmp=2.0;
			DifficultCounterAmp=2.0;
			DifficultScoreAmp=2;
			DifficultFragmentAmp=2;
		}
		others{}

		if(type==1)//緑アイテム
		{
			Scale=1.5;
			DiScale=0.015;
			R=127;
			G=255;
			B=255;
			PowerPlus=0;
			CounterPlus=25;
			CounterPlusOnBurst=25;
			ScorePlus=MagicCounter;
		}
		if(type==2)//青アイテム
		{
			Scale=1;
			DiScale=0.005;
			R=127;
			G=127;
			B=255;
			PowerPlus=2;
			CounterPlus=10;
			CounterPlusOnBurst=10;
			ScorePlus=MagicCounter;
		}
		if(type==3)//大赤アイテム
		{
			Scale=2;
			DiScale=0.020;
			R=255;
			G=127;
			B=127;
			PowerPlus=6;
			CounterPlus=5;
			CounterPlusOnBurst=5;
			ScorePlus=MagicCounter;
		}
		if(type==4)//小赤アイテム
		{
			Alpha=100;
			VAlpha=25;
			Scale=1;
			DiScale=0.010;
			R=255;
			G=127;
			B=127;
			PowerPlus=1.25*DifficultChargeAmp;
			CounterPlus=3*DifficultCounterAmp;
			CounterPlusOnBurst=1*DifficultCounterAmp;
			ScorePlus=MagicCounter*DifficultScoreAmp;
			FragmentPlus=1*DifficultFragmentAmp;
		}
		if(type==41)//小赤アイテム
		{
			Alpha=100;
			VAlpha=25;
			Scale=1.2;
			DiScale=0.012;
			R=255;
			G=127;
			B=127;
			PowerPlus=1.25*DifficultChargeAmp*2;
			CounterPlus=3*DifficultCounterAmp*2;
			CounterPlusOnBurst=1*DifficultCounterAmp*2;
			ScorePlus=MagicCounter*DifficultScoreAmp*2;
			FragmentPlus=1*DifficultFragmentAmp*2;
		}
		if(type==42)//小赤アイテム
		{
			Alpha=100;
			VAlpha=25;
			Scale=1.5;
			DiScale=0.015;
			R=255;
			G=127;
			B=127;
			PowerPlus=1.25*DifficultChargeAmp*3;
			CounterPlus=3*DifficultCounterAmp*3;
			CounterPlusOnBurst=1*DifficultCounterAmp*3;
			ScorePlus=MagicCounter*DifficultScoreAmp*3;
			FragmentPlus=1*DifficultFragmentAmp*3;
		}
		if(type==5)//大白アイテム
		{
			Scale=2;
			DiScale=0.020;
			R=255;
			G=255;
			B=255;
			PowerPlus=3;
			CounterPlus=30;
			CounterPlusOnBurst=50;
			ScorePlus=MagicCounter*100;
		}
		if(type==6)//小白アイテム1
		{
			Alpha=100;
			VAlpha=25;
			Scale=1;
			DiScale=0.010;
			R=255;
			G=255;
			B=255;
			PowerPlus=0.625*DifficultChargeAmp;
			CounterPlus=6*DifficultCounterAmp;
			CounterPlusOnBurst=10*DifficultCounterAmp;
			ScorePlus=MagicCounter*10*DifficultScoreAmp;
			FragmentPlus=1*DifficultFragmentAmp;
		}
		if(type==61)//小白アイテム2
		{
			Alpha=100;
			VAlpha=25;
			Scale=1.2;
			DiScale=0.012;
			R=255;
			G=255;
			B=255;
			PowerPlus=0.625*DifficultChargeAmp*2;
			CounterPlus=6*DifficultCounterAmp*2;
			CounterPlusOnBurst=10*DifficultCounterAmp*2;
			ScorePlus=MagicCounter*10*DifficultScoreAmp*2;
			FragmentPlus=1*DifficultFragmentAmp*2;
		}
		if(type==62)//小白アイテム3
		{
			Alpha=100;
			VAlpha=25;
			Scale=1.5;
			DiScale=0.015;
			R=255;
			G=255;
			B=255;
			PowerPlus=0.625*DifficultChargeAmp*3;
			CounterPlus=6*DifficultCounterAmp*3;
			CounterPlusOnBurst=10*DifficultCounterAmp*3;
			ScorePlus=MagicCounter*10*DifficultScoreAmp*3;
			FragmentPlus=1*DifficultFragmentAmp*3;
		}
		if(type==7)//小紫アイテム
		{
			Scale=1;
			DiScale=0.010;
			R=160;
			G=32;
			B=240;
			PowerPlus=1.0*0.2;
			CounterPlus=10*0.2;
			CounterPlusOnBurst=0;
			ScorePlus=MagicCounter*0.2;
			FragmentPlus=0.2;
		}
		if(type==8)//小白アイテム
		{
			Scale=1;
			DiScale=0.010;
			R=255;
			G=255;
			B=255;
			PowerPlus=0.625*DifficultChargeAmp;
			CounterPlus=6*DifficultCounterAmp;
			CounterPlusOnBurst=10*DifficultCounterAmp;
			ScorePlus=MagicCounter*10*DifficultScoreAmp;
			FragmentPlus=1*DifficultFragmentAmp;
		}
		if(type==9)//暗青アイテム
		{
			Scale=1;
			DiScale=0.010;
			R=0;
			G=255;
			B=255;
			PowerPlus=1;
			CounterPlus=10;
			CounterPlusOnBurst=10;
			ScorePlus=MagicCounter;
			FragmentPlus=1;
		}
			DiScale=0.00;

		if(type<=3 || type==5 || type==7 || type==8 || type==9)
		{
		loop(5)
		{
			lightangle+=12;
			ascent(let i in 0..4)
			{
				ObjEffect_SetVertexColor(obj,i,Alpha+VAlpha*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);
		}
		}
		else
		{
		loop(30)
		{
			lightangle+=12;
			ascent(let i in 0..4)
			{
				ObjEffect_SetVertexColor(obj,i,155+50*cos(lightangle),R,G,B);
			}
			Obj_SetPosition(obj,Obj_GetX(obj)+speed*cos(angle),Obj_GetY(obj)+speed*sin(angle));
			speed-=StandardSpeed/30;
			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,155+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 && BurstMode==0)
				{
					Power+=PowerPlus;
				}
				if(MagicCounter<10000 && BurstMode==0)
				{
					MagicCounter+=CounterPlus;
				}
				if(BurstMode==1)
				{
					MagicCounter+=CounterPlusOnBurst;
				}

				AddScore(ScorePlus);
				MagicFragment+=FragmentPlus;
				SectionMagicFragment+=FragmentPlus;
				//PlaySE(SE[5]);
				Obj_Delete(obj);
			}
		yield;
		}
}