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

public class Game13Bullet : MonoBehaviour{

    public GameObject death;
    public GameObject deathParticles;
    public GameObject killParticles;
    public float speed;
    bool canMove;

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

    // Update is called once per frame
    void Update(){
        if (canMove){
            transform.Translate(Vector2.left * speed * Time.deltaTime);
        }

        if (Input.GetKeyDown(KeyCode.Space) && !canMove){
            AudioManager.instance.Play("Shoot");
            canMove = true;
        }
    }

    void OnCollisionEnter2D(Collision2D col){
        if(col.gameObject.tag == "Finish"){
            GetComponent<SpriteRenderer>().enabled = false;
            deathParticles.SetActive(true);
            Time.timeScale = 0;
            StartCoroutine(Death());
            AudioManager.instance.Play("Explosion");
        }else{
            col.gameObject.SetActive(false);
            killParticles.transform.position = col.transform.position;
            killParticles.SetActive(true);
            StartCoroutine(Win());
            Time.timeScale = 0;
            AudioManager.instance.Play("Explosion");
        }
    }

    IEnumerator Win(){
        yield return new WaitForSecondsRealtime(1f);
        FindObjectOfType<GameController>().CompleteLevel();
    }

    IEnumerator Death(){
        yield return new WaitForSecondsRealtime(1f);
        death.SetActive(true);
        yield return new WaitForSecondsRealtime(4f);
        FindObjectOfType<GameController>().LoseGame();
    }
}
