﻿using UnityEngine;
using System.Collections;

public class gun : MonoBehaviour {

	public float			amoReloadTime = 1f;
	public float			gunDamage = 34;

	private bool			isShooting = false;
	private AudioSource		audioSource;
	private ParticleSystem	particle;

	public void awake() {
		isShooting = false;
	}
	
	public void shoot () {
		//		Debug.Log("shoots");
		if (!isShooting) {
			//			Debug.Log("isNotShooting");
			isShooting = true;
			StartCoroutine(doesShoot());
		}
	}

	void Start () {
		audioSource = GetComponent<AudioSource>();
		particle = GetComponentInChildren<ParticleSystem> ();
		particle.startLifetime = amoReloadTime;
//		particle.enableEmission = false;
	}

//	public void activate() {
//		gameObject.Set	Active (true);
//	}
//	
//	public void desactivate() {
//		gameObject.SetActive (false);
//	}

	private IEnumerator doesShoot() {
//		Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
//		RaycastHit hit;
//		if (Physics.Raycast (ray, out hit)) {
			//throw beeming ray
//		particle.enableEmission = true;
//		yield return new WaitForEndOfFrame ();
//		particle.loop = false;
//		Debug.Log ("particle.loop = " + particle.loop);
//		Debug.Log ("particle.enableEmission = " + particle.enableEmission);
//		Debug.Log ("particle.Play() = " + particle.Play());
		//playMusic
			particle.Play ();
			audioSource.Play();
			//move gun
			//throw explosion - which deals damages itself
			yield return new WaitForSeconds(amoReloadTime);
			isShooting = false;
//		}
	}

//	void OnParticleCollision(GameObject other) {
//		Debug.Log ("OnParticleCollisionFromGun");
//		hpManager lifes = other.GetComponent<hpManager> ();
//		if (lifes != null) {
//			lifes.getHit (gunDamage);
//		}
//		//play damage audio clip
//	}
}
