using UnityEngine;
using System.Collections;

public class RocketLauncher : MonoBehaviour {
	
	public ParticleSystem smoke;
	public ConstantForce rocket;
	public float speed = 10f;
	public int ammoCount = 20;

	public bool isAI = false;
	
	float lastShot = 0.0f;
	RocketUI _rocketUI;
	
	void Start () {
		_rocketUI = GameObject.FindObjectOfType<RocketUI>();
	}
	
	public void Fire( float reloadTime) {
		if (Time.time > (reloadTime + lastShot) && ammoCount > 0) {
			ConstantForce rocketPrefab = ConstantForce.Instantiate(rocket, transform.position, transform.rotation) as ConstantForce;
			rocketPrefab.relativeForce = new Vector3(0, 0, speed);
			
			smoke.Play();
			
			//We ignore the collision between rocket and character
			if (!isAI) {
				Physics.IgnoreCollision(rocketPrefab.GetComponent<Collider>(), transform.root.GetComponent<Collider>());
			} else {
				Physics.IgnoreCollision(rocketPrefab.GetComponent<Collider>(), transform.parent.GetComponent<Collider>());
			}
			//Get the last shot time		
			lastShot = Time.time;

			if (!isAI) {
				//Decrease the bullet
				ammoCount--;
				_rocketUI.UpdateUI(ammoCount);
			}
		}
	}
	
	public void Reload () {
		ammoCount = 20;
		_rocketUI.UpdateUI(ammoCount);
	}
}

