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

public class Bomb : MonoBehaviour {


	public AudioClip explosion;
	public AudioClip tictic;
	public GameObject explosionFX;
	bool activated = false;

	public void Activate () {
		if (!activated) {
			StartCoroutine (TicTac ());
			activated = true;
		}
	}

	IEnumerator TicTac() {
		float time = 2.5f;
		while(time > 0) {
			yield return new WaitForSeconds (0.25f);
			GameManager.instance.GetComponent<AudioSource> ().PlayOneShot (tictic);
			time -= 0.25f;
		}
		GameManager.instance.GetComponent<AudioSource> ().PlayOneShot (explosion);
		if (transform.GetChild(0).GetComponent<Collider> ().bounds.Intersects (GameObject.Find ("Player1").GetComponent<Collider> ().bounds)) {
			yield return new WaitForSeconds (1f);
			GameManager.WinP2 ("random bomb in a random crate");
		}
		if (transform.GetChild(0).GetComponent<Collider> ().bounds.Intersects (GameObject.Find ("Player2").GetComponent<Collider> ().bounds)) {
			yield return new WaitForSeconds (1f);
			GameManager.WinP1 ("random bomb in a random crate");
		}
		Destroy (gameObject, 0.1f);
		Instantiate (explosionFX, new Vector3(transform.position.x, 0.01f, transform.position.z), Quaternion.Euler(-90, 0, 0));
	}
}
