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

public class Friend_script : MonoBehaviour
{
    public float speed = 3;
    public float shoutsNeeded = 3;
    public float minDistance = 1f;
    public GameObject head;

    private Transform target;
    private bool follow;


    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if(follow)
            FollowTarget();
    }

    void FollowTarget()
    {
        if (Vector2.Distance(transform.position, target.position) > minDistance)
        {
            transform.position = Vector2.MoveTowards(transform.position, target.position,
                                                     speed * Time.deltaTime);
        }
    }

    void GotHome()
    {
        GetComponent<Animator>().SetTrigger("GotHome");
    }

    void DestroyObj()
    {
        GameManager_script.current.UpdateScore();
        follow = false;
        Destroy(gameObject);
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.tag == "PlayerShout" && !follow)
        {
            shoutsNeeded -= 1;
            if (shoutsNeeded <= 0)
            {
                target = other.gameObject.transform;
                head.SetActive(true);
                follow = true;
            }
        }
    }

    private void OnTriggerStay2D(Collider2D other)
    {
        if (other.gameObject.tag == "Home")
        {
            GotHome();
        }
    }
}