public var BulletPrefab : GameObject;
private var GunStatusScript : GunStatus;
private var DisparoSelectivoScript : DisparoSelectivo;

function Start () {
	DisparoSelectivoScript = this.gameObject.GetComponent(DisparoSelectivo);
}

function Update () {
	GunStatusScript = DisparoSelectivoScript.Armas[DisparoSelectivoScript.NumeroArma].GetComponent(GunStatus);
	
	if(Input.GetButtonDown ("Reload") && !FPCStatus.isRunning && !FPCStatus.isCrouched && 
									 	 !FPCStatus.IsAiming  && !FPCStatus.IsJumping){
		
		if(GunStatusScript.BalasActualesEnCargador == GunStatusScript.BalasTotalesPorCargador)
			return;
		ReloadWeapon();	
	}

	/*if(GunStatusScript.AnimacionesArma != null){*/
		if(FPCStatus.isShooting)
			PlayFireAnimation();
		/*else
		if(!FPCStatus.isReloading && !FPCStatus.isShooting && !GunStatusScript.AnimacionesArma.animation.isPlaying){
			//GunStatusScript.AnimacionesArma.animation.CrossFade("disparo");
			GunStatusScript.AnimacionesArma.animation["disparo"].speed = 0;
		}*/
	/*}*/
}

function SetSpeed(n:float){
	for(var state : AnimationState in GunStatusScript.AnimacionesArma.animation) {
    	state.speed = n;
	}
}

function PlayFireAnimation(){
	if(GunStatusScript.AnimacionesArma != null){
		GunStatusScript.AnimacionesArma.animation["disparo"].speed = 2;
		GunStatusScript.AnimacionesArma.animation.Play("disparo");
		PlayBullets();
		//yield WaitForSeconds(1);
	}
}

function PlayBullets(){
	if(GunStatusScript.PuntoCasquillo == null)
		return;
		
	var Casquillo: GameObject = Instantiate(BulletPrefab, GunStatusScript.PuntoCasquillo.position , GunStatusScript.PuntoCasquillo.rotation);
	var direction : Vector3 = transform.TransformDirection(Vector3.right);
	var Bulletdirection : Vector3 = SprayDirection(direction);
	Bulletdirection.x += GunStatusScript.Aceleracion*2;
	Casquillo.rigidbody.AddRelativeForce(Bulletdirection, ForceMode.VelocityChange);
}

function SprayDirection(direction : Vector3) : Vector3 {
	direction.x += (1 - 2 * Random.value) * 0.5f;
	direction.y += (1 - 2 * Random.value) * 0.5f;
	//var direction = 1.0;
	//return transform.TransformDirection(Vector3(vx,vy,vz));
	return direction;
}


function ReloadWeapon(){
	FPCStatus.isReloading = true;
	
	if(GunStatusScript.AnimacionesArma != null){
		//GunStatusScript.AnimacionesArma.animation["recargar"].speed = 1;
		GunStatusScript.AnimacionesArma.animation["recargar"].speed = 1;
		GunStatusScript.AnimacionesArma.animation.Play("recargar");
	}
		
	yield WaitForSeconds(3);
	if(GunStatusScript.CargadoresActuales > 0){
		GunStatusScript.BalasActualesEnCargador = GunStatusScript.BalasTotalesPorCargador;
		GunStatusScript.CargadoresActuales--;
	}
	DisparoSelectivoScript.GUICargadores.text = GunStatusScript.CargadoresActuales.ToString();
	DisparoSelectivoScript.GUIBalas.text = GunStatusScript.BalasActualesEnCargador.ToString();
	FPCStatus.isReloading = false;
}
	
   