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

public class AlimentController : MonoBehaviour
{
    public float speed = 1f;
    GameObject nextPos;
    GameObject movingPos;
    float distanceToArrive = 0.05f;
    float distanceToDetectArround = 0.05f;
    Collider2D collider;


    public void SetNutrient(GameObject next, Color color)
    {
        movingPos = next;
        nextPos = movingPos.GetComponent<Nodo>().m_Parent.gameObject;
        GetComponent<SpriteRenderer>().color = color;
        collider = GetComponent<Collider2D>();
    }

    void Update()
    {
        transform.position = Vector3.MoveTowards(transform.position, movingPos.transform.position, speed * Time.deltaTime);

        if (Vector2.Distance(transform.position, movingPos.transform.position) < distanceToArrive)
        {
            movingPos = nextPos;
            if (movingPos.GetComponent<Nodo>().m_Parent != null)
            {
                nextPos = movingPos.GetComponent<Nodo>().m_Parent.gameObject;
            }
            Vector3 movement = (movingPos.transform.position - transform.position).normalized;
            int rot = 0;
            if (Mathf.Abs(movement.x) > Mathf.Abs(movement.y)) rot = 90;
            transform.eulerAngles = new Vector3(0, 0, rot);
        }
    }


    public Color GetColor()
    {
        return GetComponent<SpriteRenderer>().color;
    }
}
