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

/// <summary>
/// Simple Camera!
/// </summary>
public class SimpleCamera : MonoBehaviour
{
    public Vector3 min;
    public Vector3 max;
    public Vector3 offset;
    public Transform target;
    public float dampener = 1;
    
    [ContextMenu("Set Offset")]
    void GetOffsetFromTarget()
    {
        offset = transform.position - target.position;
    }

    [ContextMenu("Go To Offset")]
    void GoToOffset()
    {
        if(target != null)
        {
            transform.position = target.position + offset;
        }
    }

    [ContextMenu("Set Min")]
    void SetMin()
    {
        min = transform.position;
    }

    [ContextMenu("Set Max")]
    void SetMax()
    {
        max = transform.position;
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        if (target == null)
            return;

        Vector3 goal = target.position + offset;
        goal.x = Mathf.Clamp(goal.x, min.x, max.x);
        goal.y = Mathf.Clamp(goal.y, min.y, max.y);
        goal.z = Mathf.Clamp(goal.z, min.z, max.z);

        transform.position = Vector3.Lerp(transform.position, goal, Time.deltaTime * dampener);
    }

    private void OnDrawGizmosSelected()
    {
        Bounds b = new Bounds(min, Vector3.zero);
        b.Encapsulate(max);

        Gizmos.DrawWireCube(b.center, b.size);
    }
}
