using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AutoCam : MonoBehaviour {

	public float currentSize = 10.0f;

	public float minTriggerFactor = 0.8f;
	public float maxTriggerFactor = 1.0f;

	private float sizeTarget = 10.0f;

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		Gameplay gameplay = gameObject.GetComponentInParent<Gameplay>();

		Vector3 playerPos = gameplay.playerShip.transform.position - GetComponent<Camera>().transform.position;

		float playerDistance = Mathf.Max(Mathf.Abs(playerPos.x), Mathf.Abs(playerPos.y));

		if (playerDistance / currentSize < minTriggerFactor)
			{
				//shrink the camera
				sizeTarget = playerDistance;
			}
		else if (playerDistance / currentSize > maxTriggerFactor)
			{
				//grow the camera
				sizeTarget = playerDistance * 1.2f;
			}

		float camSize = GetComponent<Camera>().orthographicSize;
		camSize = Mathf.Max(playerDistance, (sizeTarget - camSize) * Time.deltaTime + camSize);
		GetComponent<Camera>().orthographicSize = camSize;
	}
}
