using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using UnityStandardAssets.Characters.FirstPerson;

public class deathScript : MonoBehaviour {

    Rigidbody rb;
    CapsuleCollider cc;
    RigidbodyFirstPersonController rbfpc;
    AudioSource aud;
    private bool isDeaded;
    private float timer;
    private int score;
    private float iscore;

	// Use this for initialization
	void Start () {
        isDeaded = false;
        rb = GetComponent<Rigidbody>();
        cc = GetComponent<CapsuleCollider>();
        rbfpc = GetComponent<RigidbodyFirstPersonController>();
        aud = GetComponent<AudioSource>();
        timer = Time.time;
        score = 0;
	}
	
	// Update is called once per frame
	void Update () {
        if (isDeaded)
        {
            rb.useGravity = false;
            rb.velocity = new Vector3(0, 0, 0);
            cc.enabled = false;
            rbfpc.enabled = false;
            aud.enabled = false;
            Cursor.visible = true;
            Cursor.lockState = CursorLockMode.None;
        }
    }

    void OnCollisionEnter (Collision col)
    {
        if ((col.transform.tag == "Kill") || (col.transform.tag == "Terrain"))
        {
            isDeaded = true;
            iscore = Time.time - timer;
            score = (int) iscore;
        }
    }

    void OnGUI()
    {
        if (isDeaded)
        {
            GUI.Box(new Rect(10, 10, 200, 90), "You survived " + score.ToString() + " seconds");
            if (GUI.Button(new Rect(20, 40, 80, 50), "Reload"))
            {
                SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
            }
            if (GUI.Button(new Rect(120, 40, 80, 50), "Main Menu"))
            {
                SceneManager.LoadScene(0);
            }
        }
    }

}
