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

public class Enemy : MonoBehaviour
{
    [Header("Movement Stats")]
    public Vector3 walkingDistance = new Vector3(5f, 0f, 0f);
    public float waitingTime = 1f;
    //public float timeFromSideToSide = 2f;
    [Header("Attack Stats")]
    public Transform attackpoint;
    public float attackrange = 0.5f;
    GameObject player;


    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine(walkRight());
        player = GameObject.FindObjectOfType<PlayerMovement>().gameObject;
    }

    IEnumerator walkRight()
	{
        float startTime = Time.time;

        Vector3 start = transform.position;
        Vector3 end = transform.position + walkingDistance;

        while (Time.time - startTime <= 1)
        { // until one second passed
            transform.position = Vector3.Lerp(start, end, Time.time - startTime); // lerp from A to B
            yield return 1; // wait for next frame
        }
        yield return new WaitForSeconds(waitingTime);
        StartCoroutine(walkLeft());
    }

    IEnumerator walkLeft()
    {
        float startTime = Time.time;

        Vector3 start = transform.position;
        Vector3 end = transform.position - walkingDistance;

        while (Time.time - startTime <= 1)
        { // until one second passed
            transform.position = Vector3.Lerp(start, end, Time.time - startTime); // lerp from A to B 
            yield return 1; // wait for next frame
        }
        yield return new WaitForSeconds(waitingTime);
        StartCoroutine(walkRight());
    }

    void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject == player)
            collision.gameObject.GetComponent<PlayerRespawn>().Respawn();
    }
}
