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

public class Game12Ball : MonoBehaviour{

    public GameObject particles;
    public GameObject death;
    public float speed;
    public Rigidbody2D rb2d;
    bool launched;

    // Start is called before the first frame update
    void Start(){

    }

    // Update is called once per frame
    void Update(){
        if(Time.timeScale > 0 && !launched){
            Launch();
            launched = true;
        }
    }

    void Launch(){
        float x = Random.Range(0, 2) == 0 ? -1 : 1;
        float y = Random.Range(0, 2) == 0 ? -1 : 1;
        rb2d.velocity = new Vector2(speed * x, speed * y);
    }

    void OnCollisionEnter2D(Collision2D col){
        if(col.gameObject.tag == "Finish"){
            StartCoroutine(Death());
        }else{
            AudioManager.instance.Play("Paddle");
        }
    }

    IEnumerator Death(){
        Time.timeScale = 0;
        particles.SetActive(true);
        GetComponent<SpriteRenderer>().enabled = false;
        yield return new WaitForSecondsRealtime(1f);
        death.SetActive(true);
        yield return new WaitForSecondsRealtime(5f);
        FindObjectOfType<GameController>().LoseGame();
    }
}
