using UnityEngine;
using System.Collections;
using System;

public class Enemy : MonoBehaviour
{
	//variavel criada para identificar o player a se seguido
	public Transform Player;

    private SpriteRenderer sr;

    private float speed;
    public bool guardar;

    // Use this for initialization
    void Start () 
	{
        sr = this.GetComponent<SpriteRenderer>();
        speed = 2f;

	}

	// Update is called once per frame
	void Update () 
	{
        distanceControl();

    }

    void OnTriggerEnter2D(Collider2D col)
    {
        if (col.gameObject.tag == "limite")
        {
            this.speed *= -1;
        }
    }


    public void distanceControl()
	{
		// calcula a distancia do inimigo para o player para começar a andar
		float distanceToPlayer = Vector3.Distance(transform.position, Player.position);
		if( distanceToPlayer >= 0.5f)
		{
			followPlayer();
		}
		else
		{
            Destroy( gameObject );
            Application.LoadLevel("gameover");

		}

	}

	public void followPlayer()
    {
        Vector3 Direcao = (Player.position - transform.position).normalized;
		transform.position += Direcao * Time.deltaTime * speed;

	}

    void OnCollisionEnter2D(Collision2D c)
    {
        if (c.gameObject.tag == "Finish")
        {
            Destroy(gameObject);
        }

    }

}