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

public class PatrollingEnemy : Enemy
{
    public Transform[] path;
    public int currentPoint;
    public Transform currentGoal;
    public float roundingDistance;

    private Rigidbody2D myRigidbody;
    //public Transform target;
    public Animator anim;

    //isKilled = 0 - mob ALIVE
    //isKilled = 1 - mob DEAD

    // Start is called before the first frame update
    void Start()
    {
        isKilled = PlayerPrefs.GetInt(enemyName);
        if(isKilled == 1)
        {
            gameObject.SetActive(false);
        }
        myRigidbody = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Vector3.Distance(transform.position, path[currentPoint].position) > roundingDistance)
        {
            Vector3 temp = Vector3.MoveTowards(transform.position, path[currentPoint].position, moveSpeed * Time.deltaTime);
            //changeAnim(temp - transform.position);
            myRigidbody.MovePosition(temp);
        }
        else
        {
            changeGoal();
        }
        //killing
        if(gameObject.active == false)
        {
            isKilled = 1;
            PlayerPrefs.SetInt(enemyName, isKilled);
        }
    }

    private void changeGoal()
    {
        if (currentPoint == path.Length - 1)
        {
            currentPoint = 0;
            currentGoal = path[0];
        }
        else
        {
            currentPoint++;
            currentGoal = path[currentPoint];
        }
    }
}