using UnityEngine;
using System.Collections;

public class Laser : BaseShipComponent 
{
	public GameObject LaserPrefab;
	public bool canFireLaser = true;

	void Update()
	{
		if (inputController.playersActiveKey.Length == 0 || buildController.Building)
			return;

		if(inputController.playersActiveKey[(int) PlayerID] && canFireLaser)
		{
			canFireLaser = false;
			StartCoroutine(FireLaser());
		}
	}

	IEnumerator FireLaser ()
	{
		if (energyBar.CanFireLaser())
		{
			Instantiate (LaserPrefab, transform.position, transform.rotation);
			PlayAudio();
		} else {
			canFireLaser = true;
			return false;
		}

		yield return new WaitForSeconds (0.5f);
		canFireLaser = true;
	}
}
