using UnityEngine;
using System.Collections;

// This script is used by the players.
// It handles firing projectiles in a
// semi-straight semi-homing trajectory.
public class FireProjectile : MonoBehaviour
{
	public GameObject projectile;

	public EnemyMovement enemy;

	public Transform launchPoint;

	private float shotCounter;

	public float fireDelay;

	// Use this for initialization
	void Start ()
	{
		if (FindObjectOfType<EnemyMovement> () != null)
		{
			enemy = FindObjectOfType<EnemyMovement> ();

			shotCounter = 0.1f;
		}
	}

	// Update is called once per frame
	void Update()
	{
		if (FindObjectOfType<EnemyMovement> () != null)
		{
			shotCounter -= Time.deltaTime;

			if (enemy.transform.position.x > transform.position.x && shotCounter < 0)
			{
				Instantiate (projectile, launchPoint.position, launchPoint.rotation);
				shotCounter = fireDelay;
			}

			if (enemy.transform.position.x < transform.position.x && shotCounter < 0)
			{
				Instantiate (projectile, launchPoint.position, launchPoint.rotation);
				shotCounter = fireDelay;
			}
		}
	}
}
