Exemple #1
0
        private void Update()
        {
            // FPS counting
            fpsDeltaTime += (Time.deltaTime - fpsDeltaTime) * 0.1f;

            if (Input.GetKeyDown(KeyCode.F10))
            {
                _showFPS = !_showFPS;
            }

            if (Input.GetKeyDown(KeyCode.F9))
            {
                _showVel = !_showVel;
            }

            if (!Loader.HasLoaded)
            {
                return;
            }

            if (!_player.enableFlying && !_player.IsInVehicle && Input.GetKeyDown(KeyCode.T))
            {
                _player.enableFlying = true;
                _player.Movement     = new Vector3(0f, 0f, 0f);                                   // disable current movement
                PlayerModel.PlayAnim(AnimGroup.WalkCycle, AnimIndex.RoadCross, PlayMode.StopAll); // play 'flying' animation
            }
            else if (_player.enableFlying && Input.GetKeyDown(KeyCode.T))
            {
                _player.enableFlying = false;
            }

            if (!_player.IsInVehicle && Input.GetKeyDown(KeyCode.R))
            {
                _player.enableNoclip = !_player.enableNoclip;
                _player.characterController.detectCollisions = !_player.enableNoclip;
                if (_player.enableNoclip && !_player.enableFlying)
                {
                    _player.Movement = new Vector3(0f, 0f, 0f);                                       // disable current movement
                    PlayerModel.PlayAnim(AnimGroup.WalkCycle, AnimIndex.RoadCross, PlayMode.StopAll); // play 'flying' animation
                }
            }

            // Fix cursor state if it has been 'broken', happens eg. with zoom gestures in the editor in macOS
            if (CursorLocked && ((Cursor.lockState != CursorLockMode.Locked) || (Cursor.visible)))
            {
                Cursor.lockState = CursorLockMode.Locked;
                Cursor.visible   = false;
            }

            if (Input.GetKeyDown(KeyCode.F1))
            {
                _showMenu = !_showMenu;
            }

            bool isConsoleStateChanged = Console.Instance.m_openKey != Console.Instance.m_closeKey ?
                                         Input.GetKeyDown(Console.Instance.m_openKey) || Input.GetKeyDown(Console.Instance.m_closeKey) :
                                         Input.GetKeyDown(Console.Instance.m_openKey);

            if (!_showMenu && (Input.GetKeyDown(KeyCode.Escape) || isConsoleStateChanged || Input.GetKeyDown(KeyCode.F1)))
            {
                ChangeCursorState(!CursorLocked);
            }

            if (CursorLocked)
            {
                var cursorDelta = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));

                Yaw   += cursorDelta.x * CursorSensitivity.x;
                Pitch -= cursorDelta.y * CursorSensitivity.y;
            }

            Camera.transform.rotation = Quaternion.AngleAxis(Yaw, Vector3.up)
                                        * Quaternion.AngleAxis(Pitch, Vector3.right);

            float   distance;
            Vector3 castFrom;

            float scrollValue = Input.mouseScrollDelta.y;

            if (Console.Instance.IsOpened)
            {
                scrollValue = 0;
            }

            if (_player.IsInVehicle)
            {
                CarCameraDistance = Mathf.Clamp(CarCameraDistance - scrollValue, 2.0f, 32.0f);
                distance          = CarCameraDistance;
                castFrom          = _player.CurrentVehicle.transform.position;
            }
            else
            {
                PlayerCameraDistance = Mathf.Clamp(PlayerCameraDistance - scrollValue, 2.0f, 32.0f);
                distance             = PlayerCameraDistance;
                castFrom             = transform.position + Vector3.up * .5f;
            }

            var castRay = new Ray(castFrom, -Camera.transform.forward);

            RaycastHit hitInfo;

            if (Physics.SphereCast(castRay, 0.25f, out hitInfo, distance,
                                   -1 ^ (1 << MapObject.BreakableLayer) ^ (1 << Vehicle.Layer)))
            {
                distance = hitInfo.distance;
            }

            Camera.transform.position = castRay.GetPoint(distance);

            if (!CursorLocked)
            {
                return;
            }

            if (Input.GetButtonDown("Use") && _player.IsInVehicle)
            {
                _player.ExitVehicle();

                return;
            }

            if (_player.IsInVehicle)
            {
                return;
            }

            if (_player.enableFlying || _player.enableNoclip)
            {
                var up_down = 0.0f;

                if (Input.GetKey(KeyCode.Backspace))
                {
                    up_down = 1.0f;
                }
                else if (Input.GetKey(KeyCode.Delete))
                {
                    up_down = -1.0f;
                }

                var inputMove = new Vector3(Input.GetAxis("Horizontal"), up_down, Input.GetAxis("Vertical"));

                _player.Movement = Vector3.Scale(Camera.transform.TransformVector(inputMove),
                                                 new Vector3(1f, 1f, 1f)).normalized;

                _player.Movement *= 10.0f;

                if (Input.GetKey(KeyCode.LeftShift))
                {
                    _player.Movement *= 10.0f;
                }
                else if (Input.GetKey(KeyCode.Z))
                {
                    _player.Movement *= 100.0f;
                }

                return;
            }

            if (_player.currentWeaponSlot > 0 && Input.GetMouseButton(1))
            {
                // right click is on
                // aim with weapon
                //	this.Play2Animations (new int[]{ 41, 51 }, new int[]{ 2 }, AnimGroup.MyWalkCycle,
                //		AnimGroup.MyWalkCycle, AnimIndex.IdleArmed, AnimIndex.GUN_STAND);
                PlayerModel.PlayAnim(AnimGroup.MyWalkCycle, AnimIndex.GUN_STAND, PlayMode.StopAll);
            }
            else
            {
                var inputMove = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));

                if (inputMove.sqrMagnitude > 0f)
                {
                    inputMove.Normalize();

                    if (Input.GetKey(KeyCode.LeftShift))
                    {
                        if (_player.currentWeaponSlot > 0)
                        {
                            // player is holding a weapon

                            Play2Animations(new int[] { 41, 51 }, new int[] { 2 }, AnimGroup.WalkCycle,
                                            AnimGroup.MyWalkCycle, AnimIndex.Run, AnimIndex.IdleArmed);
                        }
                        else
                        {
                            // player is not holding a weapon
                            PlayerModel.PlayAnim(AnimGroup.WalkCycle,
                                                 AnimIndex.Run, PlayMode.StopAll);
                        }
                        //    PlayerModel.Running = true;
                    }
                    else
                    {
                        // player is walking
                        if (_player.currentWeaponSlot > 0)
                        {
                            Play2Animations(new int[] { 41, 51 }, new int[] { 2 }, AnimGroup.WalkCycle,
                                            AnimGroup.MyWalkCycle, AnimIndex.Walk, AnimIndex.IdleArmed);
                        }
                        else
                        {
                            PlayerModel.PlayAnim(AnimGroup.WalkCycle, AnimIndex.Walk, PlayMode.StopAll);
                        }
                        //    PlayerModel.Walking = true;
                    }
                }
                else
                {
                    // player is standing
                    if (_player.currentWeaponSlot > 0)
                    {
                        Play2Animations(new int[] { 41, 51 }, new int[] { 2 }, AnimGroup.MyWalkCycle,
                                        AnimGroup.MyWalkCycle, AnimIndex.IdleArmed, AnimIndex.IdleArmed);
                        //	PlayerModel.PlayAnim (AnimGroup.MyWalkCycle, AnimIndex.IdleArmed, PlayMode.StopAll);
                    }
                    else
                    {
                        PlayerModel.PlayAnim(AnimGroup.WalkCycle, AnimIndex.Idle, PlayMode.StopAll);
                    }
                    //    PlayerModel.Walking = false;
                }

                _player.Movement = Vector3.Scale(Camera.transform.TransformVector(inputMove),
                                                 new Vector3(1f, 0f, 1f)).normalized;
            }

            if (!Input.GetButtonDown("Use"))
            {
                return;
            }

            // find any vehicles that have a seat inside the checking radius and sort by closest seat
            var vehicles = FindObjectsOfType <Vehicle>()
                           .Where(x => Vector3.Distance(transform.position, x.FindClosestSeatTransform(transform.position).position) < EnterVehicleRadius)
                           .OrderBy(x => Vector3.Distance(transform.position, x.FindClosestSeatTransform(transform.position).position)).ToArray();

            foreach (var vehicle in vehicles)
            {
                var seat = vehicle.FindClosestSeat(transform.position);

                _player.EnterVehicle(vehicle, seat);

                break;
            }
        }
        private void Update()
        {
            if (Input.GetKeyDown(KeyCode.F9))
            {
                _showVel = !_showVel;
            }

            if (!Loader.HasLoaded)
            {
                return;
            }


            // reset player input
            _player.IsWalking = _player.IsRunning = _player.IsSprinting = false;
            _player.Movement  = Vector3.zero;


            if (!_player.enableFlying && !_player.IsInVehicle && Input.GetKeyDown(KeyCode.T))
            {
                _player.enableFlying = true;
                _player.Movement     = new Vector3(0f, 0f, 0f);                                   // disable current movement
                PlayerModel.PlayAnim(AnimGroup.WalkCycle, AnimIndex.RoadCross, PlayMode.StopAll); // play 'flying' animation
            }
            else if (_player.enableFlying && Input.GetKeyDown(KeyCode.T))
            {
                _player.enableFlying = false;
            }

            if (!_player.IsInVehicle && Input.GetKeyDown(KeyCode.R))
            {
                _player.enableNoclip = !_player.enableNoclip;
                _player.characterController.detectCollisions = !_player.enableNoclip;
                if (_player.enableNoclip && !_player.enableFlying)
                {
                    _player.Movement = new Vector3(0f, 0f, 0f);                                       // disable current movement
                    PlayerModel.PlayAnim(AnimGroup.WalkCycle, AnimIndex.RoadCross, PlayMode.StopAll); // play 'flying' animation
                }
            }


            this.UpdateCamera();


            if (!GameManager.CanPlayerReadInput())
            {
                return;
            }


            if (Input.GetButtonDown("Use") && _player.IsInVehicle)
            {
                _player.ExitVehicle();

                return;
            }


            if (_player.IsInVehicle)
            {
                return;
            }


            if (_player.enableFlying || _player.enableNoclip)
            {
                var up_down = 0.0f;

                if (Input.GetKey(KeyCode.Backspace))
                {
                    up_down = 1.0f;
                }
                else if (Input.GetKey(KeyCode.Delete))
                {
                    up_down = -1.0f;
                }

                var inputMove = new Vector3(Input.GetAxis("Horizontal"), up_down, Input.GetAxis("Vertical"));

                _player.Movement = Vector3.Scale(Camera.transform.TransformVector(inputMove),
                                                 new Vector3(1f, 1f, 1f)).normalized;

                _player.Movement *= 10.0f;

                if (Input.GetKey(KeyCode.LeftShift))
                {
                    _player.Movement *= 10.0f;
                }
                else if (Input.GetKey(KeyCode.Z))
                {
                    _player.Movement *= 100.0f;
                }

                return;
            }


            if (_player.WeaponHolder.IsHoldingWeapon && Input.GetMouseButton(1))
            {
                // player is holding a weapon, and right click is on => aim with weapon
                _player.WeaponHolder.IsAimOn = true;
            }
            else
            {
                // player is not aiming

                _player.WeaponHolder.IsAimOn = false;

                // give input to player

                Vector3 inputMove = Vector3.zero;
                if (m_smoothMovement)
                {
                    inputMove = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
                }
                else
                {
                    inputMove = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical"));
                }

                if (inputMove.sqrMagnitude > 0f)
                {
                    inputMove.Normalize();

                    if (Input.GetKey(KeyCode.LeftAlt))
                    {
                        _player.IsWalking = true;
                    }
                    else if (Input.GetKey(KeyCode.Space))
                    {
                        _player.IsSprinting = true;
                    }
                    else
                    {
                        _player.IsRunning = true;
                    }
                }

                _player.Movement = Vector3.Scale(Camera.transform.TransformVector(inputMove),
                                                 new Vector3(1f, 0f, 1f)).normalized;
            }


            if (!Input.GetButtonDown("Use"))
            {
                return;
            }

            // find any vehicles that have a seat inside the checking radius and sort by closest seat
            var vehicles = FindObjectsOfType <Vehicle>()
                           .Where(x => Vector3.Distance(transform.position, x.FindClosestSeatTransform(transform.position).position) < EnterVehicleRadius)
                           .OrderBy(x => Vector3.Distance(transform.position, x.FindClosestSeatTransform(transform.position).position)).ToArray();

            foreach (var vehicle in vehicles)
            {
                var seat = vehicle.FindClosestSeat(transform.position);

                _player.EnterVehicle(vehicle, seat);

                break;
            }
        }
        private void Update()
        {
            if (!_lockedCursor && Input.GetMouseButtonDown(0))
            {
                _lockedCursor    = true;
                Cursor.lockState = CursorLockMode.Locked;
                Cursor.visible   = false;
            }
            else if (_lockedCursor && Input.GetKeyDown(KeyCode.Escape))
            {
                _lockedCursor    = false;
                Cursor.lockState = CursorLockMode.None;
                Cursor.visible   = true;
            }

            if (_lockedCursor)
            {
                var cursorDelta = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));

                Yaw   += cursorDelta.x * CursorSensitivity.x;
                Pitch -= cursorDelta.y * CursorSensitivity.y;
            }

            Camera.transform.rotation = Quaternion.AngleAxis(Yaw, Vector3.up)
                                        * Quaternion.AngleAxis(Pitch, Vector3.right);

            float   distance;
            Vector3 castFrom;

            if (_player.IsInVehicle)
            {
                CarCameraDistance = Mathf.Clamp(CarCameraDistance - Input.mouseScrollDelta.y, 2.0f, 32.0f);
                distance          = CarCameraDistance;
                castFrom          = _player.CurrentVehicle.transform.position;
            }
            else
            {
                PlayerCameraDistance = Mathf.Clamp(PlayerCameraDistance - Input.mouseScrollDelta.y, 2.0f, 32.0f);
                distance             = PlayerCameraDistance;
                castFrom             = transform.position + Vector3.up * .5f;
            }

            var castRay = new Ray(castFrom, -Camera.transform.forward);

            RaycastHit hitInfo;

            if (Physics.SphereCast(castRay, 0.25f, out hitInfo, distance,
                                   -1 ^ (1 << MapObject.BreakableLayer) ^ (1 << Vehicle.Layer)))
            {
                distance = hitInfo.distance;
            }

            Camera.transform.position = castRay.GetPoint(distance);

            if (Input.GetButtonDown("Use") && _player.IsInVehicle)
            {
                _player.ExitVehicle();

                return;
            }

            if (_player.IsInVehicle)
            {
                return;
            }
            if (!_lockedCursor)
            {
                return;
            }


            if (_player.currentWeaponSlot > 0 && Input.GetMouseButton(1))
            {
                // right click is on
                // aim with weapon
                //	this.Play2Animations (new int[]{ 41, 51 }, new int[]{ 2 }, AnimGroup.MyWalkCycle,
                //		AnimGroup.MyWalkCycle, AnimIndex.IdleArmed, AnimIndex.GUN_STAND);
                PlayerModel.PlayAnim(AnimGroup.MyWalkCycle, AnimIndex.GUN_STAND, PlayMode.StopAll);
            }
            else
            {
                var inputMove = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));

                if (inputMove.sqrMagnitude > 0f)
                {
                    inputMove.Normalize();

                    if (Input.GetKey(KeyCode.LeftShift))
                    {
                        if (_player.currentWeaponSlot > 0)
                        {
                            // player is holding a weapon

                            this.Play2Animations(new int[] { 41, 51 }, new int[] { 2 }, AnimGroup.WalkCycle,
                                                 AnimGroup.MyWalkCycle, AnimIndex.Run, AnimIndex.IdleArmed);
                        }
                        else
                        {
                            // player is not holding a weapon
                            PlayerModel.PlayAnim(AnimGroup.WalkCycle,
                                                 AnimIndex.Run, PlayMode.StopAll);
                        }
                        //    PlayerModel.Running = true;
                    }
                    else
                    {
                        // player is walking
                        if (_player.currentWeaponSlot > 0)
                        {
                            this.Play2Animations(new int[] { 41, 51 }, new int[] { 2 }, AnimGroup.WalkCycle,
                                                 AnimGroup.MyWalkCycle, AnimIndex.Walk, AnimIndex.IdleArmed);
                        }
                        else
                        {
                            PlayerModel.PlayAnim(AnimGroup.WalkCycle, AnimIndex.Walk, PlayMode.StopAll);
                        }
                        //    PlayerModel.Walking = true;
                    }
                }
                else
                {
                    // player is standing
                    if (_player.currentWeaponSlot > 0)
                    {
                        this.Play2Animations(new int[] { 41, 51 }, new int[] { 2 }, AnimGroup.MyWalkCycle,
                                             AnimGroup.MyWalkCycle, AnimIndex.IdleArmed, AnimIndex.IdleArmed);
                        //	PlayerModel.PlayAnim (AnimGroup.MyWalkCycle, AnimIndex.IdleArmed, PlayMode.StopAll);
                    }
                    else
                    {
                        PlayerModel.PlayAnim(AnimGroup.WalkCycle, AnimIndex.Idle, PlayMode.StopAll);
                    }
                    //    PlayerModel.Walking = false;
                }

                _player.Movement = Vector3.Scale(Camera.transform.TransformVector(inputMove),
                                                 new Vector3(1f, 0f, 1f)).normalized;
            }


            if (!Input.GetButtonDown("Use"))
            {
                return;
            }

            // find any vehicles that have a seat inside the checking radius and sort by closest seat
            var vehicles = FindObjectsOfType <Vehicle>()
                           .Where(x => Vector3.Distance(transform.position, x.FindClosestSeatTransform(transform.position).position) < EnterVehicleRadius)
                           .OrderBy(x => Vector3.Distance(transform.position, x.FindClosestSeatTransform(transform.position).position)).ToArray();

            foreach (var vehicle in vehicles)
            {
                var seat = vehicle.FindClosestSeat(transform.position);

                _player.EnterVehicle(vehicle, seat);

                break;
            }
        }