using UnityEngine;
using System.Collections;

public class Shield : BaseShipComponent 
{
	private bool canUseShield = true;
	public GameObject ShieldObject;

	private bool keyDepressed = false;

	void Start()
	{
		ShieldObject.SetActive(false);
	}

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

		if(inputController.playersActiveKey[(int) PlayerID] && canUseShield && !keyDepressed)
		{
			keyDepressed = true;

			canUseShield = false;
			StartCoroutine(UseShield());
		}

		if(!inputController.playersActiveKey[(int) PlayerID])
		{
			keyDepressed = false;

		}
	}

	private IEnumerator UseShield ()
	{
		if (energyBar.CanUseShield())
		{
			ShieldObject.SetActive(true);
			PlayAudio();
		} 
		else 
		{
			ShieldObject.SetActive(false);
			canUseShield = true;
			return false;
		}
		yield return new WaitForSeconds(0.5f);

		ShieldObject.SetActive(false);

		yield return new WaitForSeconds(0.3f);
		canUseShield = true;
	}
}
