
task Powor	//powerの値を変化させるタスク。バリア発動やP回復。
{
loop
{

SetCommonData("POWERMAX",Power);

if(Power>5000){Power=5000;}
	if(GetKeyState(VK_USER)==KEY_PUSH && Power>=1000 && !OnBomb && Kekkaicount==0 && IsForbidBomb==false && !OnMissed)
	{
		PlaySE(SE[0]);
		Barrier;
		Power-=1000;
		loop
		{
		if(KekkaiOn==0)
		{
		break;
		}
		SetItemCollectLine(500);//アイテム回収されるy座標
		SetCommonData("KEKKAI",true);
		yield;
		}
	Kekkaicount=10;
	}
if(Kekkaicount>0)
{
	Kekkaicount-=1;
}

SetItemCollectLine(160);//アイテム回収されるy座標
SetCommonData("KEKKAI",false);
if(GetCommonData("STARTPOWER")==5000)
{
	Power=5000;
}
if(GetCommonData("STARTPOWER")==2000)
{
	Power=2000;
}
if(miss<GetMissCount)
{
	miss+=1;
	if(Power<3000)
	{
		Power=3000;
	}
}
yield;
}
}

task GetPowerItem//ステージ側でPアイテムを取った時アイテム生成
{
let ItemCheck=0;//共通データの配列の長さをチェックする変数
let XY=[];//共通データの書き換えにワンクッション置くための変数
let ItemPerFrame=5;//１フレームあたりの最大アイテム生成数（処理落ち対策）
loop
{
	ItemCheck=length(GetCommonDataDefault("POWER",[]));//毎フレーム共通データの長さチェック
	if(ItemCheck>0)
	{//配列に中身がある（アイテム発生フラグ立っている）場合の処理
		loop(ItemPerFrame)
		{
			XY=GetCommonDataDefault("POWER",[]);//共通データ内容を変数に渡す
			Power+=10;
			if(Power>=5000)
			{
				Power=5000;
			}
			loop(1)
			{
				XY=erase(XY, 0);
			}//生成したのでその要素を配列から削除
			if(length(XY)==0)
			{//削除後の配列が空になった場合
				SetCommonData("POWER",[]);//共通データを空にして
				break;//アイテム生成ループ脱出
		
			}
			else
			{
				SetCommonData("POWER",XY);
			}//配列が空でない場合共通データに変更後の配列を渡す
		}
	}
yield;
}
}

task Barrier//バリアのタスク。収縮と手動割りの２種。
{
		BarrierEffect;
		bonus=1;
		KekkaiOn=1;
		let scax=0;
		let scay=0;
		let angle=0;
  		let obj = Obj_Create(OBJ_EFFECT);
		Obj_SetPosition(obj,GetPlayerX,GetPlayerY);
		ObjEffect_SetTexture(obj, imgKekkai);
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjEffect_SetRenderState(obj, ADD);
		ObjEffect_SetScale(obj, scax,scay);
		ObjEffect_CreateVertex(obj, 4);
		ObjEffect_SetVertexXY(obj, 0, -128, -128);
		ObjEffect_SetVertexXY(obj, 1,  128, -128);
		ObjEffect_SetVertexXY(obj, 2,  128,  128);
		ObjEffect_SetVertexXY(obj, 3, -128,  128);
		
		ObjEffect_SetVertexUV(obj, 0,  256,  0);
		ObjEffect_SetVertexUV(obj, 1,  512, 0);
		ObjEffect_SetVertexUV(obj, 2, 512,  256);
		ObjEffect_SetVertexUV(obj, 3, 256, 256);

		ObjEffect_SetVertexColor(obj,0,150,255,255,255);
		ObjEffect_SetVertexColor(obj,1,150,255,255,255);
		ObjEffect_SetVertexColor(obj,2,150,255,255,255);
		ObjEffect_SetVertexColor(obj,3,150,255,255,255);

		
		ObjEffect_SetLayer(obj, 3);
		ObjEffect_SetAngle(obj, 0,0,angle);
		loop(5){
			Obj_SetPosition(obj,GetPlayerX,GetPlayerY);
			scax+=0.1;
			scay+=0.1;
			ObjEffect_SetScale(obj, scax,scay);
			yield;
			}
		parin;
		loop(300){
			if(KekkaiOn==0)
			{
				break;
			}
			Obj_SetPosition(obj,GetPlayerX,GetPlayerY);
			//scax-=0.5/360;
			//scay-=0.5/360;
			angle+=3;
			ObjEffect_SetScale(obj, scax,scay);
			ObjEffect_SetAngle(obj, 0,0,angle);
			yield;
			}
		loop(60){
			if(KekkaiOn==0)
			{
				break;
			}
			Obj_SetPosition(obj,GetPlayerX,GetPlayerY);
			scax-=0.5/60;
			scay-=0.5/60;
			angle+=3;
			ObjEffect_SetScale(obj, scax,scay);
			ObjEffect_SetAngle(obj, 0,0,angle);
			yield;
			}
		if(KekkaiOn==0 && !OnBomb)
		{
		loop(5)
		{
			scax+=0.5;
			scay+=0.5;
			ObjEffect_SetScale(obj, scax,scay);
			yield;
		}
		}
		Obj_Delete(obj);
		if(bonus==1)
		{
			PlaySE(SE[1]);
			Bonus;
			SetPlayerInvincibility(30);
		}
		KekkaiOn=0;
}

task BarrierEffect
{
		let scax=0;
		let scay=0;
		let angle=0;
  		let obj = Obj_Create(OBJ_EFFECT);
		Obj_SetPosition(obj,GetPlayerX,GetPlayerY);
		ObjEffect_SetTexture(obj, imgKekkai);
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjEffect_SetRenderState(obj, ADD);
		ObjEffect_SetScale(obj, scax,scay);
		ObjEffect_CreateVertex(obj, 4);
		ObjEffect_SetVertexXY(obj, 0, -128, -128);
		ObjEffect_SetVertexXY(obj, 1,  128, -128);
		ObjEffect_SetVertexXY(obj, 2,  128,  128);
		ObjEffect_SetVertexXY(obj, 3, -128,  128);
		
		ObjEffect_SetVertexUV(obj, 0,  256,  0);
		ObjEffect_SetVertexUV(obj, 1,  512, 0);
		ObjEffect_SetVertexUV(obj, 2, 512,  256);
		ObjEffect_SetVertexUV(obj, 3, 256, 256);

		ObjEffect_SetVertexColor(obj,0,150,255,255,255);
		ObjEffect_SetVertexColor(obj,1,150,255,255,255);
		ObjEffect_SetVertexColor(obj,2,150,255,255,255);
		ObjEffect_SetVertexColor(obj,3,150,255,255,255);
		loop(5){
			Obj_SetPosition(obj,GetPlayerX,GetPlayerY);
			scax+=0.1;
			scay+=0.1;
			ObjEffect_SetScale(obj, scax,scay);
			yield;
			}
		loop(300){
			if(KekkaiOn==0)
			{
				break;
			}
			Obj_SetPosition(obj,GetPlayerX,GetPlayerY);
			angle-=3;
			ObjEffect_SetScale(obj, scax,scay);
			ObjEffect_SetAngle(obj, 0,0,angle);
			yield;
			}
		loop(60){
			if(KekkaiOn==0)
			{
				break;
			}
			Obj_SetPosition(obj,GetPlayerX,GetPlayerY);
			scax-=0.5/60;
			scay-=0.5/60;
			angle-=3;
			ObjEffect_SetScale(obj, scax,scay);
			ObjEffect_SetAngle(obj, 0,0,angle);
			yield;
			}
		if(KekkaiOn==0 && !OnBomb)
		{
		loop(5)
		{
			scax+=0.5;
			scay+=0.5;
			ObjEffect_SetScale(obj, scax,scay);
			yield;
		}
		}
		Obj_Delete(obj);
}

task Bonus //収縮時のタスク
{
	BombBonus+=1;
	SetCommonData("BombBonus",BombBonus);
}

task parin//バリア割りのタスク。
{
	wait(3);
	loop
	{
		if(GetKeyState(VK_USER)==KEY_PUSH)
		{
			yield;
			PlaySE(SE[1]);
			KekkaiOn=0;
			bonus=0;
			DeleteShot;
			Power+=250;
			break;
		}
		if( GetKeyState(VK_BOMB)==KEY_PUSH && GetPlayerBomb>=1)
		{
			yield;
			KekkaiOn=0;
			bonus=0;
			break;
		}
	if(KekkaiOn==0)
	{
	break;
	}

	yield;
	}
}

task DeleteShot
{
	SetPlayerInvincibility(12);
	ascent(let i in 0..30)
	{
		DeleteEnemyShotToItemInCircle(SHOT,GetPlayerX,GetPlayerY,48+i*3);
		wait(1);
	}

}