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

public class GameOver : MonoBehaviour {

    public Transform[] enemy;
    public Transform player;
    public playercontroller playernya;
    public detectWifi deteksi;
    bool _mati;
    public GameObject darah;
    public bool pause;
    public GameObject kalah;
    GameManager GM;

    public Wifi1 W1;
    public Wifi2 W2;
    public Wifi3 W3;


    void Start()
    {
        GM = FindObjectOfType<GameManager>();
    }
	// Update is called once per frame
	void Update () {
        if (!pause)
        {
            if (!_mati && GM.state.Equals(GameState.GAMEPLAY))
            {
                if (Vector3.Distance(player.transform.position, enemy[0].transform.position) <= 1 || Vector3.Distance(player.transform.position, enemy[1].transform.position) <= 1 || Vector3.Distance(player.transform.position, enemy[2].transform.position) <= 1)
                {
                    _mati = true;
                    playernya.kematian = true;
                    deteksi.kematian = true;
                    
                    GM.state = GameState.GAMEOVER;
					player.GetComponent<Animator> ().SetTrigger ("death");
					player.GetComponent<AudioSource>().Stop();
					player.GetComponent<AudioSource>().PlayOneShot(player.GetComponent<playercontroller>().death);
                    W1.backtoasal = true;
                    W2.backtoasal = true;
                    W3.backtoasal = true;
//                    print("mati");
                    darah.SetActive(true);
					StartCoroutine (waitDeath());
                }
            }
        }
    }
	IEnumerator waitDeath(){
		yield return new WaitForSeconds(4f);
		kalah.SetActive(true);
	}
}
