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

SetCommonData("POWERMAX",Power);

if(GetCommonDataDefault("SELECTEDDIFFICULT","None")=="Extra" && overheaton==false &&GetCommonDataDefault("STAGECLEAR",false)==false)
{
Power+=1.5;
}

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);
		if(GetCommonDataDefault("SELECTEDDIFFICULT","None")=="Extra")
		{
			Power+=1.5;
		}
		if(Power>5000){Power=5000;}
		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;
       SetPlayerInvincibility(250);
	if(Power<3000)
	{
		Power=3000;
	}
	heat-=1000;
}
yield;
}
}
///////////////////////////////////////////////////////////////////////
let heat=0;
let overheaton=false;

task overheat
{
OverHeatdraw;
Barrierheat;
loop
{		
	if(heat>0)
	{
		heat-=1.5;
		if(overheaton==true)
		{
			heat-=1.0;
		}
		if(OnBomb)
		{
			heat-=2.0;
		}
	}
	if(heat>=5000)
	{
		overheaton=true;
	}
	if(heat<=0)
	{
		heat=0;
		overheaton=false;
	}
yield;
}
}

task Barrierheat
{
loop
{
	while(KekkaiOn==0){yield}
	heat+=1000;
	while(KekkaiOn==1){yield}	
	yield;
}
}
//////////////////////////////////////////////////////////////////////////////
task GetPowerItem//ステージ側でPアイテムを取った時アイテム生成
{
GetChangePowerItem;//タスク起動
let ItemCheck=0;//共通データの配列の長さをチェックする変数
let XY=[];//共通データの書き換えにワンクッション置くための変数
let ItemPerFrame=15;//１フレームあたりの最大アイテム生成数（処理落ち対策）
loop
{
	ItemCheck=length(GetCommonDataDefault("POWER",[]));//毎フレーム共通データの長さチェック
	if(ItemCheck>0)
	{//配列に中身がある（アイテム発生フラグ立っている）場合の処理
		loop(ItemPerFrame)
		{
			XY=GetCommonDataDefault("POWER",[]);//共通データ内容を変数に渡す
			Power+=10;
			if(GetCommonDataDefault("SELECTEDDIFFICULT","None")=="Extra")
			{
			Power+=10;
			heat-=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 GetChangePowerItem//ステージ側でCheangePアイテムを取った時アイテム生成
{
let ItemCheck=0;//共通データの配列の長さをチェックする変数
let XY=[];//共通データの書き換えにワンクッション置くための変数
let ItemPerFrame=50;//１フレームあたりの最大アイテム生成数（処理落ち対策）
loop
{
	ItemCheck=length(GetCommonDataDefault("CHANGEPOWER",[]));//毎フレーム共通データの長さチェック
	if(ItemCheck>0)
	{//配列に中身がある（アイテム発生フラグ立っている）場合の処理
		loop(ItemPerFrame)
		{
			XY=GetCommonDataDefault("CHANGEPOWER",[]);//共通データ内容を変数に渡す
			Power+=1;
			heat-=0.5;
			bonuspoint+=10;
			SetCommonData("BONUSPOINT",bonuspoint);
			if(Power>=5000)
			{
				Power=5000;
			}
			loop(1)
			{
				XY=erase(XY, 0);
			}//生成したのでその要素を配列から削除
			if(length(XY)==0)
			{//削除後の配列が空になった場合
				SetCommonData("CHANGEPOWER",[]);//共通データを空にして
				break;//アイテム生成ループ脱出
		
			}
			else
			{
				SetCommonData("CHANGEPOWER",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(12);
		}
		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 //収縮時のタスク
{
	SetPlayerInvincibility(12);
	SetCommonData("BarrirShrinkRemoveBullet",true);
/*	ascent(let i in 0..30)
	{
		DeleteEnemyShotImmediatelyInCircle(SHOT,GetPlayerX,GetPlayerY,48+i*2);
		wait(1);
	}
	DeleteEnemyShotImmediatelyInCircle(SHOT,GetPlayerX,GetPlayerY,138);
*/	ascent(let i in 0..90)
	{
		DeleteEnemyShotImmediatelyInCircle(SHOT,GetPlayerX,GetPlayerY,100+i*8);
		wait(1);
	}
	SetCommonData("BarrirShrinkRemoveBullet",false);
}

task parin//バリア割りのタスク。
{
	wait(3);
	loop
	{
		if(GetKeyState(VK_USER)==KEY_PUSH)
		{
			yield;
			PlaySE(SE[1]);
			KekkaiOn=0;
			bonus=0;
			DeleteShot;
			alternative(GetCommonDataDefault("SELECTEDDIFFICULT","None"))
			case("Easy")
			{
				Power+=250;
			}
			case("Normal")
			{
				Power+=200;
			}
			case("Hard")
			{
				Power+=150;
			}
			case("Lunatic")
			{
				Power+=100;
			}
			case("Extra")
			{
				Power+=0;
			}
			others
			{
				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);
	if(GetKeyState(VK_SHOT)==KEY_PUSH || GetKeyState(VK_SHOT)==KEY_HOLD)
	{
	SetCommonData("BarrirBreakRemoveBullet",true);
	}
	else
	{
	SetCommonData("BarrirBreakRemoveBulletNoShot",true);
	}
	ascent(let i in 0..30)
	{
		DeleteEnemyShotImmediatelyInCircle(SHOT,GetPlayerX,GetPlayerY,48+i*3);
		wait(1);
	}
	SetCommonData("BarrirBreakRemoveBullet",false);
	SetCommonData("BarrirBreakRemoveBulletNoShot",false);
}