#東方弾幕風[Player]
#ScriptVersion[2]
#Menu[Emily B]
#Text[Shot: Negation Field
Bomb 1: Purge Field
Bomb 2: Annihilation Field]
#Image[.\emily_select.png]
#ReplayName[EmilyB]

script_player_main
{
	#include_function "lib\SHOT_REPLACE\playershot_replace.dnh"
	#include_function "p_config.txt"
	let img_sakuya = GetCurrentScriptDirectory()~"sakuya.png";
	let img_cutin = GetCurrentScriptDirectory()~"sakuya_select.png";
	let count=-1;
	let optionr = 33;
	let i=0;
	let bNextShot = false;
	let objShot = NULL;
	let pointcheck = 0;
	let TwoOptIsSet = true;
	let ThreeOptIsSet = true;
	let mframe = 0;
	task slowShot()
	{	//低速移動時の誘導弾
		//敵を追尾してダメージを与える
		if(!Obj_BeDeleted(objShot)){return;}//1個しか存在しない
		objShot = Obj_Create(OBJ_SHOT);
		let isLatched = false;
		Obj_SetAlpha(objShot, 182);
		Obj_SetX(objShot, GetPlayerX);//x座標設定
		Obj_SetY(objShot, GetPlayerY);//y座標設定;
		Obj_SetSpeed(objShot, 4);//速度設定
		ObjShot_SetGraphic(objShot, WHITE03);//画像設定
		ObjShot_SetDamage(objShot, 1.375);//ダメージを設定
		ObjShot_SetPenetration(objShot, 1000000);//貫通力は適当に大きく
		while( !Obj_BeDeleted(objShot) )
		{//削除されるまでループを実行		
			//ホーミング
			let toAngle=atan2(GetEnemyY()-Obj_GetY(objShot), GetEnemyX()-Obj_GetX(objShot));
			Obj_SetAngle(objShot, toAngle);
			if(Obj_GetX(objShot) <= GetEnemyX + 10 && Obj_GetX(objShot) >= GetEnemyX - 10) {
				isLatched = true;
			}
			else {
				isLatched = false;
			}
			yield;
		}

	}
	task Option(position){
		let objoption=Obj_Create(OBJ_EFFECT);
		let optiontrig = 0;
		let optionxpos = 0;
		let optionypos = 0;
		Obj_SetAlpha(objoption,200);
		ObjEffect_SetTexture(objoption,img_sakuya);
		ObjEffect_SetRenderState(objoption,ALPHA);
		ObjEffect_SetPrimitiveType(objoption,PRIMITIVE_TRIANGLEFAN);
		ObjEffect_CreateVertex(objoption,4);
		ObjEffect_SetVertexUV(objoption,0,143,4);
		ObjEffect_SetVertexUV(objoption,1,160,4);
		ObjEffect_SetVertexUV(objoption,2,160,19);
		ObjEffect_SetVertexUV(objoption,3,143,19);
		if(position=="LEFT"){
			while(!Obj_BeDeleted(objoption)){
				if(GetCommonDataDefault("power", 128) >= 96 || GetCommonDataDefault("power", 128) < 16) {
					Obj_Delete(objoption);
				}
				TwoOptIsSet = true;
				ObjEffect_SetVertexXY(objoption,0,GetPlayerX-optionxpos-8,GetPlayerY+optionypos-7);
				ObjEffect_SetVertexXY(objoption,1,GetPlayerX-optionxpos+6,GetPlayerY+optionypos-7);
				ObjEffect_SetVertexXY(objoption,2,GetPlayerX-optionxpos+6,GetPlayerY+optionypos+7);
				ObjEffect_SetVertexXY(objoption,3,GetPlayerX-optionxpos-8,GetPlayerY+optionypos+7);
				optionxpos = cos(optiontrig) * optionr;
				optionypos = sin(optiontrig) * optionr;
				if(GetKeyState(VK_SLOWMOVE)==KEY_PUSH || GetKeyState(VK_SLOWMOVE)==KEY_HOLD) {
					optiontrig += 2;
				}
				else {
					optiontrig -= 2;
				}
				if(GetKeyState(VK_SHOT) == KEY_PUSH || GetKeyState(VK_SHOT) == KEY_HOLD) {
					if(GetKeyState(VK_SLOWMOVE)==KEY_PUSH || GetKeyState(VK_SLOWMOVE)==KEY_HOLD){
						PlayerLaser(GetPlayerX-optionxpos-1, GetPlayerY()+optionypos, 600, 50, 270, 0.35, BLUE60);
					}else{
						PlayerLaser(GetPlayerX()-optionxpos-1, GetPlayerY()+optionypos, 600, 50, 270, 0.35, BLUE60);
					}
				}
				yield;
			}
		}else{
			while(!Obj_BeDeleted(objoption)) {
				if(GetCommonDataDefault("power", 128) >= 96 || GetCommonDataDefault("power", 128) < 16) {
					Obj_Delete(objoption);
				}
				TwoOptIsSet = true;
				ObjEffect_SetVertexXY(objoption,0,GetPlayerX+optionxpos-7,GetPlayerY+optionypos-7);
				ObjEffect_SetVertexXY(objoption,1,GetPlayerX+optionxpos+7,GetPlayerY+optionypos-7);
				ObjEffect_SetVertexXY(objoption,2,GetPlayerX+optionxpos+7,GetPlayerY+optionypos+7);
				ObjEffect_SetVertexXY(objoption,3,GetPlayerX+optionxpos-7,GetPlayerY+optionypos+7);
				optionxpos = cos(optiontrig) * optionr;
				optionypos = sin(optiontrig) * optionr;
				if(GetKeyState(VK_SLOWMOVE)==KEY_PUSH || GetKeyState(VK_SLOWMOVE)==KEY_HOLD) {
					optiontrig -= 2;
				}
				else {
					optiontrig += 2;
				}
				if(GetKeyState(VK_SHOT) == KEY_PUSH || GetKeyState(VK_SHOT) == KEY_HOLD) {
					if(GetKeyState(VK_SLOWMOVE)==KEY_PUSH || GetKeyState(VK_SLOWMOVE)==KEY_HOLD){
						PlayerLaser(GetPlayerX+optionxpos, GetPlayerY()+optionypos, 600, 50, 270, 0.35, BLUE60);
					}else{
						PlayerLaser(GetPlayerX()+optionxpos, GetPlayerY()+optionypos, 600, 50, 270, 0.35, BLUE60);
					}
				}
				yield;
			}
		}
	}
	task ThreeOption(angle){
		let objoption=Obj_Create(OBJ_EFFECT);
		let optiontrig = angle;
		let optionxpos = 0;
		let optionypos = 0;
		Obj_SetAlpha(objoption,200);
		ObjEffect_SetTexture(objoption,img_sakuya);
		ObjEffect_SetRenderState(objoption,ALPHA);
		ObjEffect_SetPrimitiveType(objoption,PRIMITIVE_TRIANGLEFAN);
		ObjEffect_CreateVertex(objoption,4);
		ObjEffect_SetVertexUV(objoption,0,143,4);
		ObjEffect_SetVertexUV(objoption,1,160,4);
		ObjEffect_SetVertexUV(objoption,2,160,19);
		ObjEffect_SetVertexUV(objoption,3,143,19);
		while(!Obj_BeDeleted(objoption)){
			if(GetCommonDataDefault("power", 128) < 96) {
				Obj_Delete(objoption);
			}
			ThreeOptIsSet = true;
			ObjEffect_SetVertexXY(objoption,0,GetPlayerX-optionxpos-8,GetPlayerY+optionypos-7);
			ObjEffect_SetVertexXY(objoption,1,GetPlayerX-optionxpos+6,GetPlayerY+optionypos-7);
			ObjEffect_SetVertexXY(objoption,2,GetPlayerX-optionxpos+6,GetPlayerY+optionypos+7);
			ObjEffect_SetVertexXY(objoption,3,GetPlayerX-optionxpos-8,GetPlayerY+optionypos+7);
			optionxpos = cos(optiontrig) * optionr;
			optionypos = sin(optiontrig) * optionr;
			if(GetKeyState(VK_SLOWMOVE)==KEY_PUSH || GetKeyState(VK_SLOWMOVE)==KEY_HOLD) {
				optiontrig += 2;
			}
			else {
				optiontrig -= 2;
			}
			if(GetKeyState(VK_SHOT) == KEY_PUSH || GetKeyState(VK_SHOT) == KEY_HOLD) {
				if(GetKeyState(VK_SLOWMOVE)==KEY_PUSH || GetKeyState(VK_SLOWMOVE)==KEY_HOLD){
					PlayerLaser(GetPlayerX-optionxpos-1, GetPlayerY()+optionypos, 600, 50, 270, 0.25, BLUE60);
				}else{
					PlayerLaser(GetPlayerX()-optionxpos-1, GetPlayerY()+optionypos, 600, 50, 270, 0.25, BLUE60);
				}
			}
			yield;
		}
	}
	task PlayerLaser(x, y, l, w, angle, damage, graphic) {
		let obj = Obj_Create(OBJ_LASER);
		Obj_SetAlpha(obj, 182);
		Obj_SetPosition(obj, x, y);
		Obj_SetAngle(obj, angle);
		ObjShot_SetGraphic(obj, graphic);
		ObjShot_SetDamage(obj, damage);
		ObjShot_SetPenetration(obj, 1);
		ObjLaser_SetLength(obj, l);
		ObjLaser_SetWidth(obj, w);
		ObjLaser_SetSource(obj, false);
		yield;
		Obj_Delete(obj);
	}
	task PowerUp {
		loop {
			yield;
			if(GetPoint > pointcheck && GetCommonDataDefault("power", 128) < 128) {
				SetCommonData("power", GetCommonDataDefault("power", 128) + (GetPoint - pointcheck));
			}
			pointcheck = GetPoint;
		}
	}
	@Initialize
	{	
		LoadGraphic(img_sakuya);
		LoadGraphic(img_cutin);
		SetPlayerLifeImage(img_sakuya, 96, 48, 143, 95);
		if(bombs > 3) { bombs = 3; }
		if(bombs < 0) { bombs = 0; }
		SetInitialBombCount(bombs);
		SetSpeed(3.3, 1.6);
		PowerUp;
		shotinit;
	}
	
	@MainLoop
	{
		if((GetKeyState(VK_SHOT)==KEY_PUSH || GetKeyState(VK_SHOT)==KEY_HOLD) && count==-1){
			count = 0;
		}
		if(!OnMissed) {
			mframe = 0;
		}
		if(GetCommonDataDefault("power", 128) > 128) {
			SetCommonData("power", 128);
		}
		if(!ThreeOptIsSet) {
			ascent(a in 0..3) {
				ThreeOption(a * 120 + 30);
			}
		}
		else if(!TwoOptIsSet) {
			Option("LEFT");
			Option("RIGHT");
		}
		if(GetKeyState(VK_SLOWMOVE)==KEY_PUSH || GetKeyState(VK_SLOWMOVE)==KEY_HOLD){
			if(count != -1 && GetCommonDataDefault("power", 128) >= 16) {
				slowShot;
			}
			if(count%6 == 0) {
				if(GetCommonDataDefault("power", 128) < 32) {
					ascent(a in 0..2) {
						CreatePlayerShot01(GetPlayerX() + (a%2 * 2 - 1) * 2.5, GetPlayerY()-8, 10, 270, 1.5, 2, BLUE12);
					}
				}
				else if(GetCommonDataDefault("power", 128) < 64) {
					ascent(a in -1..2) {
						CreatePlayerShot01(GetPlayerX(), GetPlayerY()-8, 10, 270+(a*2.5), 1.25, 2, BLUE12);
					}
				}
				else if(GetCommonDataDefault("power", 128) < 128) {
					ascent(a in -2..2) {
						CreatePlayerShot01(GetPlayerX(), GetPlayerY()-8, 10, 270+(a*2.5) + 1.25, 1.25, 2, BLUE12);
					}
				}
				else {
					ascent(a in -2..3){
						CreatePlayerShot01(GetPlayerX(), GetPlayerY()-8, 10, 270+(a*2.5), 1, 2, BLUE12);
						i++;
					}
				}
			}
			if(optionr>12){optionr--;}
		}else{	
			Obj_Delete(objShot);
			if(optionr<33){optionr++;}
			if(count%6 == 0){
				if(GetCommonDataDefault("power", 128) < 32) {
					ascent(a in -1..2) {
						CreatePlayerShot01(GetPlayerX(), GetPlayerY()-8, 10, 270 + a * 3, 1.5, 2, BLUE12);
					}
				}
				else if(GetCommonDataDefault("power", 128) < 64) {
					ascent(a in -2..2) {
						CreatePlayerShot01(GetPlayerX(), GetPlayerY()-8, 10, 270+(a*5) + 2.5, 1.25, 2, BLUE12);
					}
				}
				else if(GetCommonDataDefault("power", 128) < 128) {
					ascent(a in -2..3){
						CreatePlayerShot01(GetPlayerX(), GetPlayerY()-8, 10, 270 + a * 5, 0.9, 2, BLUE12);
					}
				}
				else {
					ascent(a in -3..4){
						CreatePlayerShot01(GetPlayerX(), GetPlayerY()-8, 10, 270+(a * 10 / 3), 0.9, 2, BLUE12);
					}
				}
			}			
		}		

		if(count >= 0){
			count++;
		}
		if(count >= 13 && (GetKeyState(VK_SLOWMOVE)==KEY_PUSH || GetKeyState(VK_SLOWMOVE)==KEY_HOLD)){
			count=-1;
		}
		if(count >= 18){
			count=-1;
		}
		SetIntersectionCircle(GetPlayerX, GetPlayerY, 0);
		yield;
	}
	@Missed{
		if(mframe == GetRebirthFrame + 1 && !OnBomb) {
			SetCommonData("power", GetCommonDataDefault("power", 128) - 16);
		}
		mframe++;
	}
	@SpellCard{	
		if(GetKeyState(VK_SLOWMOVE)==KEY_PUSH || GetKeyState(VK_SLOWMOVE)==KEY_HOLD){
			UseSpellCard("PurgeField", true);
			CutIn(KOUMA,"「Annihilation Field」", img_cutin);
		}else{
			UseSpellCard("PurgeField", false);
			CutIn(KOUMA,"「Purge Field」", img_cutin);
		}
	}
	@DrawLoop{
		DrawText("Power: "~ToString(GetCommonDataDefault("power", 128)), GetClipMinX, GetClipMaxY - 36, 12, 255);
		SetTexture(img_sakuya);
		if(GetKeyState(VK_LEFT)==KEY_PUSH || GetKeyState(VK_LEFT)==KEY_HOLD){
			SetGraphicRect(49.5, 1.5, 95.5, 47.5);
		}else if(GetKeyState(VK_RIGHT)==KEY_PUSH || GetKeyState(VK_RIGHT)==KEY_HOLD){
			SetGraphicRect(97.5, 1.5, 143.5, 47.5);
		}else{
			SetGraphicRect(1.5, 1.5, 47.5, 47.5);
		}
		DrawGraphic(GetPlayerX(), GetPlayerY());
	}
	@Finalize{
		DeleteGraphic(img_sakuya);
		DeleteGraphic(img_cutin);
	}
}
script_spell PurgeField {
	let purgefield = GetCurrentScriptDirectory ~ "purgefield.png";
	@Initialize {
		LoadGraphic(purgefield);
		SetPlayerInvincibility(300);
		run;
		effect;
	}
	@MainLoop {
		CollectItems;
		yield;
	}
	task run {
		loop(45) { yield; }
		if(GetArgument == true) {
			ascent(a in 0..60) {
				if(a%10 == 0) {
					LaserHagalaz(a * 6, 1);
					LaserHagalaz(a * 6, -1);
				}
				AnnihilationField(a * 6, 1);
			}
		}
		else {
			ascent(a in 0..180) {
				DeletingShot(a * 2);
			}
		}
		loop(300) { yield; }
		End;
	}
	task effect {
		loop(45) { yield; }
		let obj = Obj_Create(OBJ_SPELL);
		ObjEffect_SetTexture(obj, purgefield);
		ObjEffect_SetRenderState(obj, ADD);
		ObjEffect_SetPrimitiveType(obj, PRIMITIVE_TRIANGLEFAN);
		ObjEffect_CreateVertex(obj, 4);
		ObjEffect_SetVertexUV(obj, 0, 0, 0);
		ObjEffect_SetVertexUV(obj, 1, 512, 0);
		ObjEffect_SetVertexUV(obj, 2, 512, 512);
		ObjEffect_SetVertexUV(obj, 3, 0, 512);
		if(GetArgument == true) {
			Obj_SetPosition(obj, GetEnemyX, GetEnemyY);
		}
		else {
			Obj_SetPosition(obj, GetPlayerX, GetPlayerY);
		}
		let px = GetPlayerX;
		let py = GetPlayerY;
		let r = 0;
		loop(300) {
			if(GetArgument == true) {
				Obj_SetPosition(obj, GetEnemyX, GetEnemyY);
				ObjEffect_SetVertexXY(obj, 0, -90, -90);
				ObjEffect_SetVertexXY(obj, 1, 90, -90);
				ObjEffect_SetVertexXY(obj, 2, 90, 90);
				ObjEffect_SetVertexXY(obj, 3, -90, 90);
				ObjSpell_SetIntersecrionCircle(obj, GetEnemyX, GetEnemyY, 90, 7, true);
			}
			else {
				ObjEffect_SetVertexXY(obj, 0, -r, -r);
				ObjEffect_SetVertexXY(obj, 1, r, -r);
				ObjEffect_SetVertexXY(obj, 2, r, r);
				ObjEffect_SetVertexXY(obj, 3, -r, r);
				ObjSpell_SetIntersecrionCircle(obj, px, py, r, 4, false);
				r++;
			}
			yield;
		}
	}
	task LaserHagalaz(angle, turndir) {
		let obj = Obj_Create(OBJ_LASER);
		Obj_SetPosition(obj, GetEnemyX + cos(angle) * 90, GetEnemyY + sin(angle) * 90);
		Obj_SetAngle(obj, angle + 180);
		ObjShot_SetGraphic(obj, 160);
		ObjShot_SetDamage(obj, 0);
		Obj_SetAutoDelete(obj, false);
		ObjLaser_SetLength(obj, 90);
		ObjLaser_SetWidth(obj, 15);
		loop(300) {
			angle += turndir;
			Obj_SetPosition(obj, GetEnemyX + cos(angle) * 90, GetEnemyY + sin(angle) * 90);
			Obj_SetAngle(obj, angle + 180);
			yield;
		}
		Obj_Delete(obj);
	}
	task AnnihilationField(angle, turndir) {
		let obj = Obj_Create(OBJ_SHOT);
		Obj_SetPosition(obj, GetPlayerX, GetPlayerY);
		Obj_SetAlpha(obj, 128);
		Obj_SetAutoDelete(obj, false);
		Obj_SetAngle(obj, angle - 90);
		ObjShot_SetGraphic(obj, 46);
		ObjShot_SetDamage(obj, 0);
		ObjShot_SetPenetration(obj, 999);
		loop(300) {
			if(GetEnemyNum > 0) {
				Obj_SetPosition(obj, GetEnemyX + cos(angle) * 90, GetEnemyY + sin(angle) * 90);
			}	
			else {
				Obj_SetPosition(obj, GetCenterX + cos(angle) * 90, GetCenterY + sin(angle) * 90);
			}
			yield;
		}
		Obj_Delete(obj);
	}
	task DeletingShot(angle) {
		let obj = Obj_Create(OBJ_SHOT);
		Obj_SetPosition(obj, GetPlayerX, GetPlayerY);
		Obj_SetAlpha(obj, 128);
		Obj_SetAngle(obj, angle);
		Obj_SetSpeed(obj, 1);
		ObjShot_SetGraphic(obj, 46);
		ObjShot_SetDamage(obj, 0);
		ObjShot_SetPenetration(obj, 999);
		while(Obj_BeDeleted(obj) == false) {
			DeleteEnemyShotToItemInCircle(SHOT, Obj_GetX(obj), Obj_GetY(obj), 0);
			yield;
		}
	}
	@DrawLoop {
	}
	@Finalize {
		DeleteGraphic(purgefield);
	}
}