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

public class Enemy : MonoBehaviour
{
    public float speed = 4;
    public float dist = 4;
    public Player player;
    private SpriteRenderer render;
    private Collider2D collideE;
    private Collider2D collideA;
    // Start is called before the first frame update
    void Start()
    {
        player = FindObjectOfType<Player>();
        collideE = GetComponent<Collider2D>();
        collideA = player.GetComponent<Collider2D>();
        render = GetComponent<SpriteRenderer>();
    }

    // Update is called once per frame
    void Update()
    {
        if (dist <= 0 || transform.position.x < 1.79)
        {
            speed = -speed;
            dist = 4;
            render.flipX = !render.flipX;
        }
        Vector3 tmp = Vector3.left * speed * Time.deltaTime;
        transform.position += tmp;
        dist -= Mathf.Abs(tmp.x);
    }

    void OnTriggerEnter2D(Collider2D collideA)
    {
        if (collideA.gameObject.tag == "Player")
        {
            player.Kill();
        }
    }
}
