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

public class Shooter : MonoBehaviour
{

    [SerializeField]
    public Transform[] waypoints;
    public int waypointIndex;
    public float moveSpeed, distance;
    public int hitCounter;
    Animator animcont;

    // Start is called before the first frame update
    void Start()
    {
        waypoints = new Transform[2];
        waypoints[0] = gameObject.transform;
        waypoints[1] = GameObject.Find("PainKiller").transform;
        hitCounter = 0;
        waypointIndex = 0;
        moveSpeed = 0.5f;
        distance = 0;
        animcont = gameObject.GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        waypoints[1] = GameObject.Find("PainKiller").transform;
        if (waypoints[1].position.y > transform.position.y)
        {
            animcont.SetInteger("up", 1);
        }
        if (waypoints[1].position.y < transform.position.y)
        {
            animcont.SetInteger("up", 0);
        }
        distance = Vector2.Distance(transform.position, waypoints[1].position);
        if (distance > 2.75f)
        {
            Move();
        }

        if (hitCounter > 4)
        {
            GameObject.Find("Functions").GetComponent<Functions>().killCount++;
            GameObject.Find("Functions").GetComponent<Functions>().healt += 0.1f; 
            Destroy(gameObject);
        }
    }
    private void Move()
    {
        if (waypointIndex < waypoints.Length)
        {
            transform.position = Vector3.MoveTowards(transform.position, waypoints[waypointIndex].transform.position, moveSpeed * Time.deltaTime);
            if (transform.position == waypoints[waypointIndex].transform.position)
            {
                waypointIndex++;
            }
        }
        else
        {

        }
    }
}
