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

public class CameraController : MonoBehaviour {

    [Header("Camera Object Ref.")]
    [Space]

    [SerializeField]
    private Camera _camera;

    [Space]
    [Header("Camera's Target")]
    [Space]

    public Transform _target;

    private Vector3 _targetCamPos;

    [Space]
    [Header("Camera Settings")]
    [Space]

    [SerializeField]
    private Vector3 _offset;

    [SerializeField]
    private int pixelScale = 1;

    [SerializeField]
    private int pixelsPerUnit = 100;

    [SerializeField]
    private float halfScreen = 0.50f;

    [SerializeField]
    private float minY = 0f;

    public float smoothingFactor;

    void Awake() {
        _camera = GetComponent<Camera>();
    }

    void Start () { 
        _offset = transform.position - _target.position;
        CameraSetUp();
    }

	void FixedUpdate () {
        TrackPlayer();
    }

    private void TrackPlayer() {

        _targetCamPos = _target.position + _offset;

        transform.position = Vector3.Lerp(transform.position , _targetCamPos , smoothingFactor * Time.deltaTime);

        if(transform.position.y < minY)
            transform.position = new Vector3(transform.position.x , minY , transform.position.z);
    }

    private void CameraSetUp() {
        _camera.orthographic = true;
        _camera.orthographicSize = Screen.height * ((halfScreen / pixelsPerUnit) / pixelScale);
    }
}
