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

public class Infection : MonoBehaviour
{
    public GameObject Rusher;
    float SpawnTime, distance, HoldTime, Count;
    int SpawnPoint;
    bool CanSpawn, Heal;
    // Start is called before the first frame update
    void Start()
    {
        SpawnTime = Time.time;
        CanSpawn = true;
        HoldTime = Time.time;
        Count = 0;
        Heal = false;
    }

    // Update is called once per frame
    void Update()
    {
        distance = Vector2.Distance(transform.position, GameObject.Find("PainKiller").GetComponent<PainKiller>().transform.position);

        if (SpawnTime < Time.time && CanSpawn == true)
        {
            SpawnPoint = Random.Range(0, 5);
            GameObject rush = Instantiate(Rusher, transform.GetChild(SpawnPoint).transform.position, Quaternion.Euler(0, 0, 0));
            SpawnTime = Time.time+1f;
        }
        if(Heal == true && HoldTime < Time.time)
        {
            HoldTime = Time.time + 1;
            Count++;
        }
        if(Count > 4)
        {
            GameObject.Find("Functions").GetComponent<Functions>().killCount++;
            GameObject.Find("Functions").GetComponent<Functions>().healt += 0.1f;
            Destroy(gameObject);
        }

    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.name == "PainKiller")
        {
            CanSpawn = false;
            Heal = true;
        }

    }
    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.gameObject.name == "PainKiller")
        {
            CanSpawn = true;
            Heal = false;
            Count = 0;
        }
    }
}
