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

public class CollectableManager : MonoBehaviour
{
    public Vector3 startPosition;
    public Vector3 endPosition;
    public float speed;

    private Vector3 nextPosition;
    // Start is called before the first frame update
    void Start()
    {
        nextPosition = endPosition;
    }

    // Update is called once per frame
    void Update()
    {
        MoveCollectable();
        if (Vector3.Distance(transform.localPosition, nextPosition) <= 0.01f)
        {
            SwitchDestination();
        }
    }

    private void MoveCollectable()
    {
        transform.localPosition = Vector3.MoveTowards(transform.localPosition, nextPosition, speed * Time.deltaTime);
    }

    private void SwitchDestination()
    {
        nextPosition = nextPosition != startPosition ? startPosition : endPosition;
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.tag == "Player") 
        {
            gameObject.SetActive(false);
        }    
    }
}
