using UnityEngine;
using System.Collections;

public class Spore : MonoBehaviour {


    public float moveSpeed=3f;

    public float bobSpeed = 3f;
    public float bobHeight=.5f;
    public float bobOffset = 0;

    private float bottom;
    public float visionDistance=5;
    private float Distance;
    public GameObject playerObject;
	void Awake ()
    {
        playerObject = GameObject.FindGameObjectWithTag("Player");
        bottom = transform.position.y;
        
	}
	
	// Update is called once per frame
	void Update () 
    {


        Distance = Vector2.Distance(playerObject.transform.position, this.transform.position);

        if (Distance < visionDistance)
        {
            transform.position = Vector2.Lerp(transform.position, playerObject.transform.position, moveSpeed * Time.deltaTime);
        }
        else 
        {
            float y = bottom + (((Mathf.Cos((Time.time + bobOffset) * bobSpeed) + 1) / 2) * bobHeight);
            transform.position = new Vector2(transform.position.x,y);
        }
	}
    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.CompareTag("Player"))
        {
            Destroy(gameObject);
            NotificationCenter.DefaultCenter().PostNotification(this,"PlayerIsDead");
           
        }
    }
    
}
