#東方弾幕風[Player]
#ScriptVersion[2]
#Menu[Moonbeam Cathwright]
#Text[Moonbeam Cathwright

Shot:
Unfocused: Chakram Tilt (misdirection)
Focused: Chakram Compass (aimed)

Ars:
"Edge of Emotion"
"Gathering of Emotion"]
#Image[.\sakuya_select.png]
#ReplayName[Moonbeam]

script_player_main
{
	#include_function "lib\SHOT_REPLACE\playershot_replace.dnh"
	let img_sakuya = GetCurrentScriptDirectory()~"sakuya.png";
	let img_cutin = GetCurrentScriptDirectory()~"sakuya_select.png";
	let count=-1;
	let turna = 270;
	let optionxpos=16;
	let optionypos=0;
	let i=0;
	let bNextShot = false;
	task Option(position){
		let objoption=Obj_Create(OBJ_EFFECT);
		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,145,1);
		ObjEffect_SetVertexUV(objoption,1,159,1);
		ObjEffect_SetVertexUV(objoption,2,159,15);
		ObjEffect_SetVertexUV(objoption,3,145,15);
		if(position=="LEFT"){
			while(!Obj_BeDeleted(objoption)){
				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);
				if(GetKeyState(VK_SLOWMOVE)==KEY_PUSH || GetKeyState(VK_SLOWMOVE)==KEY_HOLD){
					if(count%6 == 3){
						ChakramCompass(GetPlayerX-optionxpos-1,GetPlayerY+optionypos-8,WHITE51);
					}
				}else{
					if(count%6 == 3) {
						CreatePlayerShot01(GetPlayerX()-optionxpos-1, GetPlayerY()+optionypos-8, 10, turna, 5, 1, WHITE51);
					}
				}
				yield;
			}
		}else{
			while(!Obj_BeDeleted(objoption)){
				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);
				if(GetKeyState(VK_SLOWMOVE)==KEY_PUSH || GetKeyState(VK_SLOWMOVE)==KEY_HOLD){
					if(count%6 == 3){	
						ChakramCompass(GetPlayerX+optionxpos,GetPlayerY+optionypos-8,WHITE51);
					}
				}else{
					if(count%6 == 3){
						CreatePlayerShot01(GetPlayerX()+optionxpos, GetPlayerY()+optionypos-8, 10, turna, 5, 1, WHITE51);
					}
				}
				yield;
			}
		}
	}
	task ChakramCompass(x,y,graphic){
		let enemy_target=-1;
		let target_angle=270;
		let best_center=1000;
		let best_target=-1;
		if(IsBossExisting){
			target_angle=atan2(GetEnemyY-GetPlayerY, GetEnemyX-GetPlayerX);
			if(target_angle<-60 && target_angle>-120){
				target_angle=atan2(GetEnemyY-GetPlayerY, GetEnemyX-GetPlayerX);
				CreatePlayerShot01(x,y,15,target_angle,4.5,1,graphic);
			}else{
				target_angle=270;
			}
		}
		if(target_angle==270){
			ascent(i in EnumEnemyBegin..EnumEnemyEnd) {
				enemy_target=EnumEnemyGetID(i);
				if((|GetEnemyInfo(enemy_target,ENEMY_X)-GetPlayerX|)<best_center){
					best_center=(|GetEnemyInfo(enemy_target,ENEMY_X)-GetPlayerX|);
					best_target=enemy_target;
				}
			}
			if(best_target!=-1){
				target_angle=atan2(GetEnemyInfo(best_target,ENEMY_Y)-GetPlayerY, GetEnemyInfo(best_target,ENEMY_X)-GetPlayerX);
				if(target_angle>-60 || target_angle<-120){
					best_target=-1;
				}
			}
			if(best_target!=-1){
				target_angle=atan2(GetEnemyInfo(best_target,ENEMY_Y)-y, GetEnemyInfo(best_target,ENEMY_X)-x);
				CreatePlayerShot01(x,y,15,target_angle,4.5,1,graphic);
			}else{
				CreatePlayerShot01(x,y,15,270,4.5,1,graphic);
			}
		}
	}
	@Initialize
	{	
		LoadGraphic(img_sakuya);
		LoadGraphic(img_cutin);
		LoadPlayerShotData(GetCurrentScriptDirectory()~"sakuya_shotdata.txt");
		SetPlayerLifeImage(img_sakuya, 96, 48, 143, 95);
		SetInitialBombCount(3);
		SetSpeed(5, 2);
		Option("LEFT");
		Option("RIGHT");
		shotinit;
	}
	function IsPushed(button) {
		return GetKeyState(button) == KEY_PUSH || GetKeyState(button) == KEY_HOLD;
	}
	@MainLoop
	{	
		if((GetKeyState(VK_SHOT)==KEY_PUSH || GetKeyState(VK_SHOT)==KEY_HOLD) && count==-1){
			count = 0;
		}
		if(GetKeyState(VK_SLOWMOVE)==KEY_PUSH || GetKeyState(VK_SLOWMOVE)==KEY_HOLD){
			turna = 270;
			if(count%6 == 0){
				ChakramCompass(GetPlayerX-8,GetPlayerY-8,ORANGE55 + rand_int(0, 1));
				ChakramCompass(GetPlayerX+8,GetPlayerY-8,ORANGE55 + rand_int(0, 1));
			}
			if(optionxpos>12){optionxpos--;}
			if(optionypos>-12){optionypos--;}
		}else{
			if(IsPushed(VK_RIGHT) && turna < 300) {
				turna++;
			}
			else if(IsPushed(VK_LEFT) && turna > 240) {
				turna--;
			}
			if(count%6 == 0){
				CreatePlayerShot01(GetPlayerX(), GetPlayerY()-8, 10, turna, 5, 1, WHITE51);
			}
			if(optionxpos<20){optionxpos++;}
			if(optionypos<0){optionypos++;}
			
		}		

		if(count >= 0){
			count++;
		}
		if(count >= 6 && (GetKeyState(VK_SLOWMOVE)==KEY_PUSH || GetKeyState(VK_SLOWMOVE)==KEY_HOLD)){
			count=-1;
		}
		if(count >= 6){
			count=-1;
		}
		SetIntersectionCircle(GetPlayerX, GetPlayerY, 0);
		yield;
	}
	@Missed{}
	@SpellCard{	
		SetSpeed(1, 1);
		if(GetKeyState(VK_SLOWMOVE)==KEY_PUSH || GetKeyState(VK_SLOWMOVE)==KEY_HOLD){
			UseSpellCard("KillerDoll", 0);
			CutIn(KOUMA,"「Gathering of Emotion」", img_cutin);
		}else{
			UseSpellCard("Indiscriminate", 0);
			CutIn(KOUMA,"「Edge of Emotion」", img_cutin);
		}
	}
	@DrawLoop{
		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 Indiscriminate{
	let spellcount=0;
	let spellangle=0;
	let img_spell = GetCurrentScriptDirectory()~"sakuya_spell.png";
	task Spell_Knife(x,y,angle,type){
		let objknife=Obj_Create(OBJ_SPELL);
		let knifexpos=GetPlayerX;
		let knifeypos=GetPlayerY;
		let knifeangle=angle;
		let knifecount=0;
		Obj_SetAlpha(objknife,200);
		ObjEffect_SetTexture(objknife,img_spell);
		ObjEffect_SetRenderState(objknife,ADD);
		ObjEffect_CreateVertex(objknife,4);
		ObjEffect_SetPrimitiveType(objknife,PRIMITIVE_TRIANGLEFAN);
		if(type==1){
			ObjEffect_SetVertexUV(objknife,0,1,1);
			ObjEffect_SetVertexUV(objknife,1,46,1);
			ObjEffect_SetVertexUV(objknife,2,46,46);
			ObjEffect_SetVertexUV(objknife,3,1,46);
		}else{
			ObjEffect_SetVertexUV(objknife,0,49,1);
			ObjEffect_SetVertexUV(objknife,1,94,1);
			ObjEffect_SetVertexUV(objknife,2,94,46);
			ObjEffect_SetVertexUV(objknife,3,49,46);
		}
		while(!Obj_BeDeleted(objknife)){
			if(type==1){
				knifeangle-=1;	
			}else{
				knifeangle+=1;
			}
			knifexpos=knifexpos+(8*cos(180+knifeangle));
			knifeypos=knifeypos+(8*sin(180+knifeangle));
			ObjEffect_SetVertexXY(objknife,0,knifexpos+(45*cos(135+knifeangle)),knifeypos+(45*sin(135+knifeangle)));
			ObjEffect_SetVertexXY(objknife,1,knifexpos+(45*cos(225+knifeangle)),knifeypos+(45*sin(225+knifeangle)));
			ObjEffect_SetVertexXY(objknife,2,knifexpos+(45*cos(315+knifeangle)),knifeypos+(45*sin(315+knifeangle)));
			ObjEffect_SetVertexXY(objknife,3,knifexpos+(45*cos(45+knifeangle)),knifeypos+(45*sin(45+knifeangle)));
			if(knifecount>3){ObjSpell_SetIntersecrionCircle(objknife,knifexpos,knifeypos,30,5+(knifecount*0.35),true);}
			knifecount++;
			yield;
		}
	}
	task run{
		SetPlayerInvincibility(240);
		ForbidShot(true);
		loop(45){yield;}
		while(spellcount<90){
			if(spellcount%5==0){
				Spell_Knife(GetPlayerX,GetPlayerY,spellangle,1);
				Spell_Knife(GetPlayerX,GetPlayerY,spellangle+90,2);
				Spell_Knife(GetPlayerX,GetPlayerY,spellangle+180,1);
				Spell_Knife(GetPlayerX,GetPlayerY,spellangle+270,2);
				spellangle+=40;
				PlaySE(GetCurrentScriptDirectory()~"knife.wav");
			}
			spellcount++;
			yield;
		}
		ForbidShot(false);
		SetSpeed(3.5, 2.5);
		loop(90){yield;}
		End();
	}
	@Initialize{
		LoadGraphic(img_spell);
		run();
	}
	@MainLoop{
		CollectItems();
		yield;
	}
	@Finalize{
		DeleteGraphic(img_spell);
	}
}

script_spell KillerDoll{
	let spellcount=0;
	let spellangle=0;
	let img_spell = GetCurrentScriptDirectory()~"sakuya_spell.png";
	task Spell_Knife(x,y,angle,type){
		let objknife=Obj_Create(OBJ_SPELL);
		let knifexpos=GetPlayerX;
		let knifeypos=GetPlayerY;
		let knifexpos2=GetPlayerX;
		let knifeypos2=GetPlayerY;
		let knifeangle=angle;
		let knifespin=angle+180;
		let knifelength=0;
		let knifecount=0;
		Obj_SetAlpha(objknife,200);
		ObjEffect_SetTexture(objknife,img_spell);
		ObjEffect_SetRenderState(objknife,ADD);
		ObjEffect_CreateVertex(objknife,4);
		ObjEffect_SetPrimitiveType(objknife,PRIMITIVE_TRIANGLEFAN);
		if(type==1){
			ObjEffect_SetVertexUV(objknife,0,1,1);
			ObjEffect_SetVertexUV(objknife,1,46,1);
			ObjEffect_SetVertexUV(objknife,2,46,46);
			ObjEffect_SetVertexUV(objknife,3,1,46);
		}else{
			ObjEffect_SetVertexUV(objknife,0,49,1);
			ObjEffect_SetVertexUV(objknife,1,94,1);
			ObjEffect_SetVertexUV(objknife,2,94,46);
			ObjEffect_SetVertexUV(objknife,3,49,46);
		}
		while(!Obj_BeDeleted(objknife)){
			if(knifecount<60){
				knifespin+=4;
				knifexpos=knifexpos+(cos(180+knifeangle));
				knifeypos=knifeypos+(sin(180+knifeangle));
				knifexpos2=knifexpos;
				knifeypos2=knifeypos;
				ObjSpell_SetIntersecrionCircle(objknife,knifexpos,knifeypos,30,0.1,true);
			}else{
				knifexpos=knifexpos+(20*cos(180+knifeangle));
				knifeypos=knifeypos+(20*sin(180+knifeangle));
				knifexpos2=knifexpos2+(5*cos(180+knifeangle));
				knifeypos2=knifeypos2+(5*sin(180+knifeangle));
				ObjSpell_SetIntersecrionCircle(objknife,knifexpos,knifeypos,30,6.5,true);
			}
			if(knifecount==60){
				if(IsBossExisting){
					knifeangle=180+atan2(GetEnemyY-knifeypos, GetEnemyX-knifexpos);
					knifespin=knifeangle;
				}else{
					let enemy_target=-1;
					ascent(i in EnumEnemyBegin..EnumEnemyEnd) {
						enemy_target=EnumEnemyGetID(i);
						i=EnumEnemyEnd;
					}
					if(enemy_target!=-1){
						knifeangle=180+atan2(GetEnemyInfo(enemy_target,ENEMY_Y)-knifeypos, GetEnemyInfo(enemy_target,ENEMY_X)-knifexpos);
						knifespin=knifeangle;
					}
				}
				PlaySE(GetCurrentScriptDirectory()~"knife.wav");
			}
			ObjEffect_SetVertexXY(objknife,0,knifexpos+(45*cos(135+knifespin)),knifeypos+(45*sin(135+knifespin)));
			ObjEffect_SetVertexXY(objknife,1,knifexpos+(45*cos(225+knifespin)),knifeypos+(45*sin(225+knifespin)));
			ObjEffect_SetVertexXY(objknife,2,knifexpos2+(45*cos(315+knifespin)),knifeypos2+(45*sin(315+knifespin)));
			ObjEffect_SetVertexXY(objknife,3,knifexpos2+(45*cos(45+knifespin)),knifeypos2+(45*sin(45+knifespin)));
			knifecount++;
			if(knifecount>120){
				Obj_Delete(objknife);
			}
			yield;
		}
	}
	task run{
		
		ForbidShot(true);
		loop(45){yield;}
		while(spellcount<90){
			if(spellcount%8==0){
				Spell_Knife(GetPlayerX,GetPlayerY,spellangle,1);
				Spell_Knife(GetPlayerX,GetPlayerY,spellangle+180,1);
				spellangle+=20;
			}
			if(spellcount%8==4){
				Spell_Knife(GetPlayerX,GetPlayerY,spellangle,2);
				Spell_Knife(GetPlayerX,GetPlayerY,spellangle+180,2);
				spellangle+=20;
			}
			spellcount++;
			yield;
		}
		ForbidShot(false);
		SetSpeed(3.5, 2.5);
		loop(180){yield;}
		End();
	}
	@Initialize{
		SetPlayerInvincibility(240);
		LoadGraphic(img_spell);
		run();
	}
	@MainLoop{
		CollectItems();
		yield;
	}
	@Finalize{
		DeleteGraphic(img_spell);
	}
}