using UnityEngine;
using System.Collections;

public class FollowCamera : MonoBehaviour {

    public float xMargin = 1.5f;
    public float yMargin = 1.5f;

    public float xSmooth = 1.0f;
    public float ySmooth = 1.0f;

    public Vector2 maxXandY;
    public Vector2 minXandY;

    public Transform player;
    // Use this for initialization
    void Start () {
	
	}

    void Awake()
    {
        player = GameObject.Find("Player").transform;
        if (player == null)
            Debug.LogError("Player doesn't exist");
    }
	
    bool CheckXMargin()
    {
        return Mathf.Abs(transform.position.x - player.position.x) > xMargin;
    }

    bool CheckYMargin()
    {
        return Mathf.Abs(transform.position.y - player.position.y) > yMargin;
    }

	// Update is called once per frame
	void FixedUpdate()
    {
        float TargetX = transform.position.x;
        float TargetY = transform.position.y;

        if (CheckXMargin())
        {
            TargetX = Mathf.Lerp(transform.position.x, player.position.x, xSmooth * Time.fixedDeltaTime);
        }
        if (CheckYMargin())
        {
            TargetY = Mathf.Lerp(transform.position.y, player.position.y, ySmooth * Time.fixedDeltaTime);
        }

        TargetX = Mathf.Clamp(TargetX,minXandY.x,maxXandY.x);
        TargetY = Mathf.Clamp(TargetY, minXandY.y, maxXandY.y);

        transform.position = new Vector3(TargetX,TargetY,transform.position.z);
    }
}
