////////////////////////////////////////////////////////
/////////////////////Variables//////////////////////////
////////////////////////////////////////////////////////

let count=-1;
let optionxpos=16;
let optionypos=0;
let i=0;
let H = false;
let BitX     = 0;   
let BitY     = 0; 
let frame2 = 0;

////////////////////////////////////////////////////////
/////////////////////Imagenes///////////////////////////
////////////////////////////////////////////////////////

//===General===

let img_yui = GetCurrentScriptDirectory~"Mamizou.png";
let cut = GetCurrentScriptDirectory~"cut1.png";
let declaration = GetCurrentScriptDirectory~"cutin_spell.png";
let imgeff = GetCurrentScriptDirectory ~"eff.png";
let perne = GetCurrentScriptDirectory() ~ "slow_dot.png"; 
let julos = GetCurrentScriptDirectory() ~ "slow_border.png"; 
let Names = GetCurrentScriptDirectory~"Spellname.png";

//===Yui A===
let estelaeff = GetCurrentScriptDirectory ~"Estela.png";
let imgEffect = GetCurrentScriptDirectory ~ "Spell.png";
let imgBack = GetCurrentScriptDirectory() ~ "Back.png";

//===Yui B===

LoadGraphic(img_yui);
LoadGraphic(cut);
LoadGraphic(declaration);
LoadGraphic(imgeff);
LoadGraphic(estelaeff);
LoadGraphic(imgEffect);
LoadGraphic(imgBack);
LoadGraphic(perne);
LoadGraphic(julos);
LoadGraphic(Names);

////////////////////////////////////////////////////////
//////////////////////////SE////////////////////////////
////////////////////////////////////////////////////////

let disparo=GetCurrentScriptDirectory()~"DISHparo.wav";
let sespell=GetCurrentScriptDirectory()~"se_cat00.wav";


LoadSE(disparo);
LoadSE(sespell);



task focuspocus
{
	let obj1;
	let obj2;
	let obj3;
	let obj_alpha1=0;
	let obj_scale1=0;
	let obj_alpha23=0;
	let obj_scale23=1.25;
	let obj_angle=0;
	let obj_timer = 10800;

	EffectOn;

	loop
	{
		Obj_SetPosition(obj1,GetPlayerX,GetPlayerY);
		ObjEffect_SetAngle(obj1,0,0,obj_angle);
		ObjEffect_SetScale(obj1,obj_scale1,obj_scale1);
		ascent(n in 0..4){ObjEffect_SetVertexColor(obj1,n,obj_alpha1,255,255,255);}
		
		Obj_SetPosition(obj2,GetPlayerX,GetPlayerY);
		ObjEffect_SetAngle(obj2,0,0,0-obj_angle);
		ObjEffect_SetScale(obj2,obj_scale23,obj_scale23);
		ascent(n in 0..4){ObjEffect_SetVertexColor(obj2,n,obj_alpha23,255,255,255);}
		
		Obj_SetPosition(obj3,GetPlayerX,GetPlayerY);
		ObjEffect_SetAngle(obj3,0,0,obj_angle);
		ObjEffect_SetScale(obj3,obj_scale23,obj_scale23);
		ascent(n in 0..4){ObjEffect_SetVertexColor(obj3,n,obj_alpha23,255,255,255);}
		
		
		if(GetKeyState(VK_SLOWMOVE)==KEY_HOLD && H==false){
			obj_angle+=2;
			if(obj_alpha1<255) { obj_alpha1=255; }
			if(obj_scale1<1) { obj_scale1+=1/10 }
			if(obj_alpha23<162) { obj_alpha23+=162/10; }
			if(obj_scale23>0.75) { obj_scale23-=(1.25-0.75)/10 }
		}
		else {
			obj_angle+=6;
			if(obj_alpha1>0) { obj_alpha1-=255/10; }
			if(obj_scale1>0) { obj_scale1-=1/10 }
			if(obj_alpha23>0) { obj_alpha23-=162/10; }
			if(obj_scale23<1.25) { obj_scale23+=(1.25-0.75)/10 }
		}
		
		obj_timer--;
		if(obj_timer < 0 || Obj_BeDeleted(obj1) || Obj_BeDeleted(obj2) || Obj_BeDeleted(obj3)){
			Obj_Delete(obj1);
			Obj_Delete(obj2);
			Obj_Delete(obj3);
			obj_timer=10800;
			EffectOn;
		}
		
		yield;
	}
	sub EffectOn{
		obj1=Obj_Create(OBJ_EFFECT);
		obj2=Obj_Create(OBJ_EFFECT);
		obj3=Obj_Create(OBJ_EFFECT);
		ObjEffect_SetTexture(obj1,perne);
		ObjEffect_SetLayer(obj1,4);
		ObjEffect_SetPrimitiveType(obj1,PRIMITIVE_TRIANGLEFAN);
		ObjEffect_CreateVertex(obj1,4);
		ObjEffect_SetScale(obj1,1,1);
		ObjEffect_SetVertexUV(obj1,0,0,0);
		ObjEffect_SetVertexUV(obj1,1,16,0);
		ObjEffect_SetVertexUV(obj1,2,16,16);
		ObjEffect_SetVertexUV(obj1,3,0,16);
		ObjEffect_SetVertexXY(obj1,0,-7,-7);
		ObjEffect_SetVertexXY(obj1,1,8,-7);
		ObjEffect_SetVertexXY(obj1,2,8,8);
		ObjEffect_SetVertexXY(obj1,3,-7,8);
		
		ObjEffect_SetTexture(obj2,julos);
		ObjEffect_SetLayer(obj2,2);
		ObjEffect_SetPrimitiveType(obj2,PRIMITIVE_TRIANGLEFAN);
		ObjEffect_CreateVertex(obj2,4);
		ObjEffect_SetVertexUV(obj2,0,0,0);
		ObjEffect_SetVertexUV(obj2,1,63,0);
		ObjEffect_SetVertexUV(obj2,2,63,63);
		ObjEffect_SetVertexUV(obj2,3,63,0);
		ObjEffect_SetVertexXY(obj2,0,-46,-46);
		ObjEffect_SetVertexXY(obj2,1,46,-46);
		ObjEffect_SetVertexXY(obj2,2,46,46);
		ObjEffect_SetVertexXY(obj2,3,-46,46);
		
		ObjEffect_SetTexture(obj3,julos);
		ObjEffect_SetLayer(obj3,4);
		ObjEffect_SetPrimitiveType(obj3,PRIMITIVE_TRIANGLEFAN);
		ObjEffect_CreateVertex(obj3,2);
		ObjEffect_SetAngle(obj3,0,0,90);
		ObjEffect_SetScale(obj3,0.75,0.75);
		ObjEffect_SetVertexUV(obj3,0,0,0);
		ObjEffect_SetVertexUV(obj3,1,63,0);
		ObjEffect_SetVertexUV(obj3,2,63,63);
		ObjEffect_SetVertexUV(obj3,3,63,0);
		ObjEffect_SetVertexXY(obj3,0,-46,-46);
		ObjEffect_SetVertexXY(obj3,1,46,-46);
		ObjEffect_SetVertexXY(obj3,2,46,46);
		ObjEffect_SetVertexXY(obj3,3,-46,46);
	}
}

////////////////////////////////////////////////////////
///////////////////////Estela///////////////////////////
////////////////////////////////////////////////////////

task Estela(let x,let y){
			let scale=1.3;
			let alp=150;
			let obj2 = Obj_Create(OBJ_EFFECT);
			Obj_SetPosition(obj2,x,y);
			ObjEffect_SetTexture(obj2, estelaeff);
			ObjEffect_SetPrimitiveType(obj2, PRIMITIVE_TRIANGLESTRIP);
			ObjEffect_SetRenderState(obj2, ADD);
			ObjEffect_SetScale(obj2, scale,scale);
	                ObjEffect_SetLayer(obj2,4);
			ObjEffect_CreateVertex(obj2, 4);
			Obj_SetAlpha(obj2, alp);
			ObjEffect_SetLayer(obj2, 2);
			ObjEffect_SetVertexXY(obj2, 0, -7, -7);
			ObjEffect_SetVertexXY(obj2, 1, -7,  7);
			ObjEffect_SetVertexXY(obj2, 2,  7, -7);
			ObjEffect_SetVertexXY(obj2, 3,  7,  7);
		
			ObjEffect_SetVertexUV(obj2, 0,  149,  16);
			ObjEffect_SetVertexUV(obj2, 1,  149, 30);
			ObjEffect_SetVertexUV(obj2, 2, 163,  16);
			ObjEffect_SetVertexUV(obj2, 3, 163, 30);

ObjEffect_SetVertexColor(obj2, 0,  alp, rand(100,255), 255, 255);
ObjEffect_SetVertexColor(obj2, 1,  alp, rand(100,255), 255, 255);
ObjEffect_SetVertexColor(obj2, 2,  alp, rand(100,255), 255, 255);
ObjEffect_SetVertexColor(obj2, 3,  alp, rand(100,255), 255, 255);

			loop(35){
				scale-=1.3/35;
				alp-=255/35;
				Obj_SetAlpha(obj2, alp);
				ObjEffect_SetScale(obj2,scale,scale);
				Obj_SetPosition(obj2,Obj_GetX(obj2),Obj_GetY(obj2));
				yield;
				}
			Obj_Delete(obj2);
			}
	

////////////////////////////////////////////////////////
//////////////////////Disparos//////////////////////////
////////////////////////////////////////////////////////

//==General==

task Mainshotu(pX, pY, speed, angle, Damage, pene, Graphic)
{
	let obj = Obj_Create(OBJ_SHOT);
	ObjShot_SetDamage(obj, Damage);
	ObjShot_SetPenetration(obj, pene);
	ObjShot_SetGraphic(obj, Graphic);
	Obj_SetSpeed(obj, speed);
	Obj_SetAngle(obj, angle);
	Obj_SetAlpha(obj, 200);
	Obj_SetPosition(obj, GetPlayerX+pX, GetPlayerY+pY);

	while(!Obj_BeDeleted(obj))
	{

		if(Obj_IsIntersected(obj))
		{
			
			Obj_Delete(obj);
		}
		yield;
	}
}



task Mainshota(pX, pY, speed, angle, Damage, pene, Graphic)
{
	let obj = Obj_Create(OBJ_SHOT);
	ObjShot_SetDamage(obj, Damage);
	ObjShot_SetPenetration(obj, pene);
	ObjShot_SetGraphic(obj, Graphic);
	Obj_SetSpeed(obj, speed-speed+1);
	Obj_SetAngle(obj, angle);
	Obj_SetAlpha(obj, 100);
	Obj_SetPosition(obj, GetPlayerX+pX, GetPlayerY+pY);

	while(!Obj_BeDeleted(obj))
	{
loop(20){yield;}
	Obj_SetSpeed(obj, speed);
	ObjShot_SetGraphic(obj, 5);
	Obj_SetAlpha(obj, 200);
		if(Obj_IsIntersected(obj))
		{
			Obj_Delete(obj);
		}
		yield;
	}
}



//===Yui A===

	task Homing(let x,let y,let fangle,let LorR){
		
		let objShot= Obj_Create(OBJ_SHOT);
		let dspeed=0;
		Obj_SetX(objShot, x);
		Obj_SetY(objShot, y);
		Obj_SetSpeed(objShot, 2);
        	ObjShot_SetGraphic(objShot, 2);
		ObjShot_SetDamage(objShot, 17.5);//17.5 op2, 35 1op
		Obj_SetAlpha(objShot, 200);     
		ObjShot_SetPenetration(objShot, 1);
		Obj_SetAngle(objShot, fangle);
		loop(18){
			dspeed+=2/18;
			fangle+=LorR*10;
			Obj_SetAngle(objShot, fangle);
			Obj_SetSpeed(objShot, 4-dspeed);
			yield;
			}
		loop(60){
			dspeed+=2/18;
			if(dspeed>12){dspeed=12;}
			Obj_SetSpeed(objShot, dspeed);
        	ObjShot_SetGraphic(objShot, 4);
			if(GetEnemyNum !=0){
				Obj_SetAngle(objShot, atan2(GetEnemyY-Obj_GetY(objShot),GetEnemyX-Obj_GetX(objShot)));
				}
			yield;
			}
		while( !Obj_BeDeleted(objShot) ){yield;}
		Obj_Delete(objShot);
		}

	
	


////////////////////////////////////////////////////////
//////////////////////Opciones//////////////////////////
////////////////////////////////////////////////////////

task Option(position){
  let objoption=Obj_Create(OBJ_EFFECT);
  let optionxpos=30;
  let optionypos=-8;
  let side = 0;
  let frame2 = 0;

  let osc = 0;
	let an=0;
  let count2 = 0;
  Obj_SetAlpha(objoption,200);
  ObjEffect_SetTexture(objoption,img_yui);
  ObjEffect_SetRenderState(objoption,ALPHA);
  ObjEffect_SetPrimitiveType(objoption,PRIMITIVE_TRIANGLEFAN);
  ObjEffect_CreateVertex(objoption,4);
		ObjEffect_SetVertexUV(objoption, 0,  110,  7);
		ObjEffect_SetVertexUV(objoption, 1,  140, 7);
		ObjEffect_SetVertexUV(objoption, 2, 140,  37);
		ObjEffect_SetVertexUV(objoption, 3, 110, 37);
//if(GetCommonData("robado")==0){
  if(position=="LEFT"){
    while(!Obj_BeDeleted(objoption)){
	if(GetKeyState(VK_SLOWMOVE)==KEY_PUSH || GetKeyState(VK_SLOWMOVE)==KEY_HOLD){
		if(optionxpos>-12){
			optionypos+=3;
			optionxpos-=3;
		}
	}else{
		if(optionxpos<30){
			optionypos-=3;
			optionxpos+=3;
		}
	}
	count2++;
if(GetCommonData("robado")==0){
      ObjEffect_SetVertexXY(objoption,0,GetPlayerX-optionxpos-7 - osc,GetPlayerY+optionypos-7 - osc);
      ObjEffect_SetVertexXY(objoption,1,GetPlayerX-optionxpos+7 + osc,GetPlayerY+optionypos-7 - osc);
      ObjEffect_SetVertexXY(objoption,2,GetPlayerX-optionxpos+7 + osc,GetPlayerY+optionypos+7 + osc);
      ObjEffect_SetVertexXY(objoption,3,GetPlayerX-optionxpos-7 - osc,GetPlayerY+optionypos+7 + osc);}
else if(GetCommonData("robado")==1){
      ObjEffect_SetVertexXY(objoption,0,0,0);
      ObjEffect_SetVertexXY(objoption,1,0,0);
      ObjEffect_SetVertexXY(objoption,2,0,0);
      ObjEffect_SetVertexXY(objoption,3,0,0);}
else if(GetCommonData("robado")==2){
      ObjEffect_SetVertexXY(objoption,0,0,0);
      ObjEffect_SetVertexXY(objoption,1,0,0);
      ObjEffect_SetVertexXY(objoption,2,0,0);
      ObjEffect_SetVertexXY(objoption,3,0,0);}

	  if(count2%40<20){
		osc+=0.2;
		}
	if(count2%40>=20){
		osc-=0.2;
		}
 				if(GetKeyState(VK_SLOWMOVE)==KEY_PUSH || GetKeyState(VK_SLOWMOVE)==KEY_HOLD){
if((GetKeyState(VK_SHOT)==KEY_PUSH || GetKeyState(VK_SHOT)==KEY_HOLD)){
frame2++;}
if(frame2 == 1){
if(GetCommonData("robado")==0){

		Homing(GetPlayerX-optionxpos-1-BitX, GetPlayerY+optionypos-BitY,90,-1);

}
frame2++;
}


if(frame2 > 15){
frame2 = 0;
}
				}else{
					if(count%12 == 2){	
if(GetCommonData("robado")==0 ){
			Mainshota(0-optionxpos-1, 0+optionypos, 5, 290, 1.4,1,3);
			Mainshota(0-optionxpos-1, 0+optionypos, 5, 285, 1.4,1,3);
			Mainshota(0-optionxpos-1, 0+optionypos, 5, 280, 1.4,1,3);
}


					}
				}


	
      yield;
    }
  }else{
    while(!Obj_BeDeleted(objoption)){
an++;
	if(GetKeyState(VK_SLOWMOVE)==KEY_PUSH || GetKeyState(VK_SLOWMOVE)==KEY_HOLD){
		if(optionxpos>-12){
			optionypos+=3;
			optionxpos-=3;
		}
	}else{
		if(optionxpos<30){
			optionypos-=3;
			optionxpos+=3;
		}
	}
	count2++;
if(GetCommonData("robado")==0 || GetCommonData("robado")==1){
      ObjEffect_SetVertexXY(objoption,0,GetPlayerX+optionxpos-7 - osc,GetPlayerY+optionypos-7 - osc);
      ObjEffect_SetVertexXY(objoption,1,GetPlayerX+optionxpos+7 + osc,GetPlayerY+optionypos-7 - osc);
      ObjEffect_SetVertexXY(objoption,2,GetPlayerX+optionxpos+7 + osc,GetPlayerY+optionypos+7 + osc);
      ObjEffect_SetVertexXY(objoption,3,GetPlayerX+optionxpos-7 - osc,GetPlayerY+optionypos+7 + osc);} 
else if(GetCommonData("robado")==2){
      ObjEffect_SetVertexXY(objoption,0,0,0);
      ObjEffect_SetVertexXY(objoption,1,0,0);
      ObjEffect_SetVertexXY(objoption,2,0,0);
      ObjEffect_SetVertexXY(objoption,3,0,0);}
	if(count2%40<20){
		osc+=0.2;
		}
	if(count2%40>=20){
		osc-=0.2;
		}


 				if(GetKeyState(VK_SLOWMOVE)==KEY_PUSH || GetKeyState(VK_SLOWMOVE)==KEY_HOLD){
if((GetKeyState(VK_SHOT)==KEY_PUSH || GetKeyState(VK_SHOT)==KEY_HOLD)){
frame2++;}
if(frame2 == 1){
if(GetCommonData("robado")==0 || GetCommonData("robado")==1){
                Homing(GetPlayerX+optionxpos-1-BitX, GetPlayerY+optionypos-BitY,90,1);

}
frame2++;
}


if(frame2 > 15){
frame2 = 0;
}
				}else{
					if(count%12 == 2){	


if(GetCommonData("robado")==0 || GetCommonData("robado")==1){

			Mainshota(0+optionxpos-1, 0+optionypos, 5, 250, 1.4,1,3);
			Mainshota(0+optionxpos-1, 0+optionypos, 5, 255, 1.4,1,3);
			Mainshota(0+optionxpos-1, 0+optionypos, 5, 260, 1.4,1,3);

}

					}
				}


      yield;
    }
  }
//}

}


