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

public class MoveScript : MonoBehaviour {

    public Transform camTransform;

    Rigidbody rb;

    float speed = 1000;

    public bool isJumping = false;

    private void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag == "Ground")
        {
            isJumping = false;
        }
    }

    void FixedUpdate () {

        if (transform.position.y < -10)
        {
            //SceneManager.LoadScene(0);
            GameControllerScript.shared.Die();
            //print("du er død");
        }

        Vector3 velo = rb.velocity;
        velo.x *= 0.95f;
        velo.z *= 0.95f;
        rb.velocity = velo;


        if (Input.GetKey(KeyCode.W))
        {
            rb.AddForce(camTransform.forward * Time.fixedDeltaTime * speed);
        }
        if (Input.GetKey(KeyCode.S))
        {
            rb.AddForce(-camTransform.forward * Time.fixedDeltaTime * speed);
        }
        if (Input.GetKey(KeyCode.A))
        {
            rb.AddForce(-camTransform.right * Time.fixedDeltaTime * speed);
        }
        if (Input.GetKey(KeyCode.D))
        {
            rb.AddForce(camTransform.right * Time.fixedDeltaTime * speed);
        }

    }

    private void Update()
    {
        if (isJumping == false)
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                isJumping = true;
                GetComponent<Rigidbody>().AddForce(0, 400, 0);
            }
        }
    }
}
