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

[RequireComponent(typeof(Camera))]
public class CameraLeash : MonoBehaviour
{
	[SerializeField]
	private Camera targetCamera;

	[SerializeField]
	private Vector3Asset targetPositionAsset;

	[SerializeField, Range(0f, 1f)]
	private float smoothTime = 0.01f;

	[SerializeField]
	private float cameraZPosition;

	private Vector3 velocity;

	private void Start()
	{
		Initialize();
	}

	private void Initialize()
	{
		targetCamera = GetComponent<Camera>();
	}

	private void LateUpdate()
	{
		UpdateCameraPosition();
	}

	private void UpdateCameraPosition()
	{
		targetCamera.transform.position = Vector3.SmoothDamp(GetCameraPosition(), GetTargetPosition(), ref velocity, smoothTime);
	}

	private Vector3 GetTargetPosition()
	{
		if(targetPositionAsset != null)
		{
			return new Vector3(targetPositionAsset.GetVector().x, targetPositionAsset.GetVector().y, cameraZPosition);
		}
		else return GetCameraPosition();
	}

	private Vector3 GetCameraPosition()
	{
		return targetCamera.transform.position;
	}
}
