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

public class PlayerController03 : MonoBehaviour{

    Rigidbody2D rb2d;
    public float flapForce;
    public GameObject explosion;
    public GameObject death;

    bool jump;

    // Start is called before the first frame update
    void Start(){
        rb2d = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update(){
        if (Input.GetKeyDown(KeyCode.Space)){
            jump = true;
        }
    }

    void FixedUpdate(){
        if (jump){
            rb2d.velocity = Vector2.zero;
            rb2d.AddForce(Vector2.up * flapForce, ForceMode2D.Impulse);
            jump = false;
        }
    }

    void OnCollisionEnter2D(Collision2D col){
        AudioManager.instance.Play("Hit");
        Time.timeScale = 0f;
        GetComponent<SpriteRenderer>().enabled = false;
        explosion.SetActive(true);
        StartCoroutine(Death());
    }

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