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

public class player : MonoBehaviour
{
    public GameObject fail;
    float distance;
    float lastdist;
    public hole holeThatIWillControll;
    int losing;
    int i;
    float timer;
    float timer2;
    bool stillGoing = true;
    public bool falling =false;
     sound so;
    // Start is called before the first frame update
    void Start()
    {
        Time.timeScale = 1;
        so = GetComponent<sound>();
        lastdist = Mathf.Infinity;
        so.gameplay_sound();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            if (holeThatIWillControll)
            stillGoing = holeThatIWillControll.pressit();
            soundofclick(stillGoing);
        }
        changeDif();
        transform.Translate(new Vector3(0,0, GameState.speed * Time.deltaTime * ((GameState.speed-1)*5f)));

        Ray ray = new Ray(transform.position,Vector3.forward);
        if (Physics.Raycast(ray, 1))
        {
            failThegame(stillGoing);
            
        }
        if (falling)
        {
            so.fall_sound();
            transform.Translate(0, -10 * Time.deltaTime, 0);
            timer2 += Time.deltaTime;
            if(timer2 > 2)
            {
                failThegame(false);
            }
        }
    }
    void changeDif()
    {
        timer += Time.deltaTime;
        GameState.speed += Time.deltaTime * Time.deltaTime * Time.deltaTime;

        GameState.defc = (int)Mathf.Clamp(GameState.speed, 2,5);
    }

    public void soundofclick(bool Yes)
    {
        if (Yes)
        {
            so.click_sound();
        }
        else
        {
        }
    }
    public void failThegame(bool Yes)
    {
        if (!Yes)
        {
            fail.SetActive(true);
            Time.timeScale = 0;
        }
    }
    public void RS()
    {
        Time.timeScale = 1;
        SceneManager.LoadScene("1");
        
    }
    public void pause()
    {
        if(Time.timeScale == 0)
            Time.timeScale = 1;
        else
            Time.timeScale = 0;
    }
}
