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


public class Movement : NetworkBehaviour 
{

    public GameObject player;
    public Rigidbody m_Rigidbody;
    public bool IsFlying = true;
    public int m_strenght = 5;

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

    // Update is called once per frame
    void Update()
    {
        UseGravity();
        if(isLocalPlayer) {

        
            if(Input.GetKey(KeyCode.W)) {
                m_Rigidbody.velocity = Vector3.forward * m_strenght;
            }

            if(Input.GetKey(KeyCode.S)) {
                m_Rigidbody.velocity = Vector3.back * m_strenght;
            }

            if(Input.GetKey(KeyCode.A)) {
                m_Rigidbody.velocity = Vector3.left * m_strenght;
            }

            if(Input.GetKey(KeyCode.D)) {
                m_Rigidbody.velocity = Vector3.right * m_strenght;
            }
            
            if(Input.GetKeyDown(KeyCode.Space) && !IsFlying) {
                m_Rigidbody.constraints = RigidbodyConstraints.None;
                m_Rigidbody.velocity = new Vector3(0, 20, 0);
                IsFlying = true;
            }
        }
    }

    public void UseGravity() {
        if(player.transform.position.y == 1) {
            m_Rigidbody.constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezeRotation;
            IsFlying = false;
        }
        else {
            m_Rigidbody.constraints = RigidbodyConstraints.FreezeRotation;
        }
    }
}

