#TouhouDanmakufu[Player]
#ScriptVersion[2]
#Menu[Gusano The Worm Launcher]
#Text[Gusano Comes from the outside world
now he enters Gensokyo to prove he is the strongest!
Shot: Worm Launcher -Spread-
Focused: Worm Launcher -focus-]
#Image[.\Gusano_portrait.png]
#ReplayName[GUSANO]


script_player_main
{
	let img_sakuya = GetCurrentScriptDirectory()~"gusano.png";
	let img_cutin = GetCurrentScriptDirectory()~"Gusano_portrait.png";
	let count=-1;
	let optionxpos=16;
	let optionypos=0;
	let i=0;
let angulP = 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){	
						JackTheRipper(GetPlayerX-optionxpos-1,GetPlayerY+optionypos-8,12);
					}
				}else{
					if(count%8 == 4){	
						i=-1;
						while(i<=0){
							CreatePlayerShot01(GetPlayerX()-optionxpos-1, GetPlayerY()+optionypos-8, 10, 254-sin(angulP)*10, 7.5, 1, 2);
							i++;
angulP +=5;
						}
					}
				}
				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){	
						JackTheRipper(GetPlayerX+optionxpos,GetPlayerY+optionypos-8,12);
					}
				}else{
					if(count%8 == 4){	
						i=-1;
						while(i<=0){
							CreatePlayerShot01(GetPlayerX()+optionxpos, GetPlayerY()+optionypos-8, 10, 286+sin(angulP)*10, 7.5, 1, 2);
							i++;
                                                                                                                                          angulP += 5;
						}
					}
				}
				yield;
			}
		}
	}
let objHitbox = NULL;
  let objHitboxCopy = NULL;
  let objHitboxGraphic = GetCurrentScriptDirectory~"Hitbox.png";
  let objHitboxScale = 0;
  let objHitboxAngle = 0;
  let objHitboxAngleVel = 103;
  let objHitboxCopyScale = 1.45;
  sub CreateHitbox{
    LoadGraphic(objHitboxGraphic);
    objHitbox = Obj_Create(OBJ_EFFECT);
    ObjEffect_SetTexture(objHitbox, objHitboxGraphic);
    ObjEffect_SetRenderState(objHitbox, ALPHA);
    ObjEffect_SetLayer(objHitbox, 4);
    ObjEffect_SetPrimitiveType(objHitbox, PRIMITIVE_TRIANGLEFAN);
    ObjEffect_CreateVertex(objHitbox, 4);
    ObjEffect_SetVertexUV(objHitbox, 0, 0, 0);
    ObjEffect_SetVertexUV(objHitbox, 1, 63, 0);
    ObjEffect_SetVertexUV(objHitbox, 2, 63, 63);
    ObjEffect_SetVertexUV(objHitbox, 3, 0, 63);
    ObjEffect_SetVertexXY(objHitbox, 0, -32, -32);
    ObjEffect_SetVertexXY(objHitbox, 1, 32, -32);
    ObjEffect_SetVertexXY(objHitbox, 2, 32, 32);
    ObjEffect_SetVertexXY(objHitbox, 3, -32, 32);
    objHitboxCopy = Obj_Create(OBJ_EFFECT);
    ObjEffect_SetTexture(objHitboxCopy, objHitboxGraphic);
    ObjEffect_SetRenderState(objHitboxCopy, ALPHA);
    ObjEffect_SetLayer(objHitboxCopy, 4);
    ObjEffect_SetPrimitiveType(objHitboxCopy, PRIMITIVE_TRIANGLEFAN);
    ObjEffect_CreateVertex(objHitboxCopy, 4);
    ObjEffect_SetVertexUV(objHitboxCopy, 0, 0, 0);
    ObjEffect_SetVertexUV(objHitboxCopy, 1, 63, 0);
    ObjEffect_SetVertexUV(objHitboxCopy, 2, 63, 63);
    ObjEffect_SetVertexUV(objHitboxCopy, 3, 0, 63);
    ObjEffect_SetVertexXY(objHitboxCopy, 0, -32, -32);
    ObjEffect_SetVertexXY(objHitboxCopy, 1, 32, -32);
    ObjEffect_SetVertexXY(objHitboxCopy, 2, 32, 32);
    ObjEffect_SetVertexXY(objHitboxCopy, 3, -32, 32);
  }
  sub DrawHitbox{
    if(GetKeyState(VK_SLOWMOVE)==KEY_PUSH || GetKeyState(VK_SLOWMOVE)==KEY_HOLD){
      if(GetKeyState(VK_SLOWMOVE)==KEY_PUSH){
 objHitboxCopyScale = 1.45;
      }
      if(objHitboxAngleVel>3){
 objHitboxAngleVel -= 5;
      }
      if(objHitboxScale<1){
 objHitboxScale += 0.2;
      }
      if(objHitboxCopyScale>1){
 objHitboxCopyScale-=0.05;
      }
      objHitboxAngle += objHitboxAngleVel;
      ObjEffect_SetAngle(objHitbox, 0, 0, objHitboxAngle);
      ObjEffect_SetScale(objHitbox, objHitboxScale, objHitboxScale);
      ObjEffect_SetAngle(objHitboxCopy, 0, 0, -objHitboxAngle);
      ObjEffect_SetScale(objHitboxCopy, objHitboxCopyScale, objHitboxCopyScale);
      Obj_SetPosition(objHitbox, GetPlayerX, GetPlayerY);
      Obj_SetPosition(objHitboxCopy, GetPlayerX, GetPlayerY);
    }else{
      if(objHitboxScale>0){
 objHitboxScale -= 0.2;
      }else{
 objHitboxScale = 0;
      }
      if(objHitboxCopyScale>0){
 objHitboxCopyScale -= 0.2;
      }else{
 objHitboxCopyScale = 0;
      }
      objHitboxAngleVel = 103;
      ObjEffect_SetAngle(objHitbox, 0, 0, objHitboxAngle);
      ObjEffect_SetScale(objHitbox, objHitboxScale, objHitboxScale);
      ObjEffect_SetAngle(objHitboxCopy, 0, 0, -objHitboxAngle);
      ObjEffect_SetScale(objHitboxCopy, objHitboxCopyScale, objHitboxCopyScale);
    }
  }
	task JackTheRipper(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-y, GetEnemyX-x);
				CreatePlayerShot01(x,y,15,270,1.5,1,graphic);
CreatePlayerShot01(x,y,15,275,1.5,1,graphic);
CreatePlayerShot01(x,y,15,265,1.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,270,1.5,1,graphic);
CreatePlayerShot01(x,y,15,275,1.5,1,graphic);
CreatePlayerShot01(x,y,15,265,1.5,1,graphic);
			}else{
				CreatePlayerShot01(x,y,15,270,1.5,1,graphic);
CreatePlayerShot01(x,y,15,275,1.5,1,graphic);
CreatePlayerShot01(x,y,15,265,1.5,1,graphic);
			}
		}
	}
	@Initialize
	{	
		LoadGraphic(img_sakuya);
		LoadGraphic(img_cutin);
CreateHitbox;
		LoadPlayerShotData(GetCurrentScriptDirectory()~"shot.txt");
		SetPlayerLifeImage(img_sakuya, 96, 48, 143, 95);
		SetInitialBombCount(3);
		SetSpeed(3.5, 2.5);
		Option("LEFT");
		Option("RIGHT");

	}
	
	@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){
			if(count%6 == 0){	
				JackTheRipper(GetPlayerX-8,GetPlayerY-8,11);
				JackTheRipper(GetPlayerX+8,GetPlayerY-8,11);
			}
			if(optionxpos>12){optionxpos--;}
			if(optionypos>-12){optionypos--;}
		}else{	
			if(count%8 == 0){	
				i=-3;
				while(i<=3){
					CreatePlayerShot01(GetPlayerX(), GetPlayerY()-8, 10, 270+(i*5), 2.5, 1, 1);
					i++;
				}
			}
			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 >= 8){
			count=-1;
		}
		SetIntersectionCircle(GetPlayerX, GetPlayerY, 0);
		yield;
	}
	@Missed{}
	@SpellCard{	
		SetSpeed(1, 1);
			UseSpellCard("KillerDoll", 0);
			CutIn(KOUMA,"Worm Sign uHomming Wormsv", img_cutin);
		
	}
	@DrawLoop{
DrawHitbox;
		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()~"shot.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()~"shot - copia.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);
	}
}