using UnityEngine;
using System.Collections;

public class LazyCamera : MonoBehaviour {

	public GameObject playerObj;
	public float deadPercX;
	public float deadPercY;
	Vector3 camPos;
	Vector2 oldPlayerPos;
	float pos;
	float camWidth;
	float camHeight;
	// Use this for initialization
	void Start () {
		//camWidth = Camera.main.pixelWidth;
		//pos = 0;
		
		camWidth = Camera.main.orthographicSize * 2f;
		camHeight = camWidth * Camera.main.aspect;
	}
	
	// Update is called once per frame
	void Update () {

		Vector2 newPlayerPos = playerObj.transform.position;
		camPos = transform.position;
		float playerDistanceX = Mathf.Abs(camPos.x - newPlayerPos.x);
		float playerDistanceY = Mathf.Abs(camPos.y - newPlayerPos.y);
		
		Vector2 playerDelta = new Vector2(newPlayerPos.x - oldPlayerPos.x, newPlayerPos.y - oldPlayerPos.y);
		
		Debug.DrawLine( new Vector2(camPos.x - (camWidth * deadPercX / 2), newPlayerPos.y + 1f), new Vector2(camPos.x - (camWidth * deadPercX / 2), newPlayerPos.y - 1f) );
		Debug.DrawLine( new Vector2(camPos.x + (camWidth * deadPercX / 2), newPlayerPos.y + 1f), new Vector2(camPos.x + (camWidth * deadPercX / 2), newPlayerPos.y - 1f) );
		Debug.DrawLine( new Vector2(camPos.x + 1f, camPos.y - (camHeight * deadPercY / 2)), new Vector2(camPos.x - 1f, camPos.y - (camHeight * deadPercY / 2)) );
		Debug.DrawLine( new Vector2(camPos.x + 1f, camPos.y + (camHeight * deadPercY / 2)), new Vector2(camPos.x - 1f, camPos.y + (camHeight * deadPercY / 2)) );
		
		if (playerDistanceX > (camWidth * deadPercX / 2)){
			camPos.x += playerDelta.x;
		}
		
		if (playerDistanceY > (camHeight * deadPercY / 2)){
			camPos.y += playerDelta.y;
		}
		
		transform.position = camPos;
		oldPlayerPos = playerObj.transform.position;
	}
}
