using UnityEngine;

namespace InfiniteWorld
{
    public class CameraFollow2D : MonoBehaviour
    {
        [SerializeField] private Transform target;
        [SerializeField] private float smoothTime = 0.18f;
        [SerializeField] private Vector3 offset = new Vector3(0f, 0f, -10f);

        private Vector3 velocity;

        private void LateUpdate()
        {
            if (target == null)
            {
                SimplePlayerInputMover player = FindFirstObjectByType<SimplePlayerInputMover>();
                if (player == null)
                {
                    return;
                }

                target = player.transform;
            }

            Vector3 desiredPosition = target.position + offset;
            desiredPosition.z = offset.z;
            transform.position = Vector3.SmoothDamp(transform.position, desiredPosition, ref velocity, smoothTime);
        }
    }
}
