Example #1
0
        /// <summary> Calculate the player's non-normalized desired velocity. </summary>
        private Vector3 CheckMovementInput()
        {
            var desiredVelocity = new DesiredVelocity();

            if (MoveForward.IsDown)
            {
                desiredVelocity.Forward();
            }

            if (MoveBackward.IsDown)
            {
                desiredVelocity.Backward();
            }

            if (MoveLeft.IsDown)
            {
                desiredVelocity.Left();
            }

            if (MoveRight.IsDown)
            {
                desiredVelocity.Right();
            }

            return(desiredVelocity.Velocity);
        }
Example #2
0
        public override void AddExternalForces(float dt)
        {
            ClearForces();

            if (isJumping)
            {
                foreach (CollisionInfo info in CollisionSkin.Collisions)
                {
                    Vector3 N = info.DirToBody0;
                    if (this == info.SkinInfo.Skin1.Owner)
                    {
                        Vector3.Negate(ref N, out N);
                    }

                    if (Vector3.Dot(N, Orientation.Up) > 0.7f)
                    {
                        Vector3 vel = Velocity;
                        vel.Y    = jumpHeight;
                        Velocity = vel;
                        break;
                    }
                }
            }

            Vector3 deltaVel = DesiredVelocity - Velocity;

            bool running = true;

            if (DesiredVelocity.LengthSquared() < JiggleMath.Epsilon)
            {
                running = false;
            }
            else
            {
                deltaVel.Normalize();
            }

            deltaVel.Y = -2.0f;

            // start fast, slow down slower
            if (running)
            {
                deltaVel *= accelerationRate; //acceleration multiplier
            }
            else
            {
                deltaVel *= decelerationRate;  //deceleration multiplier
            }

            float forceFactor = 500.0f;

            AddBodyForce(deltaVel * Mass * dt * forceFactor);
            isJumping = false;
            AddGravityToExternalForce();
        }
Example #3
0
        public void Start()
        {
            _transform       = Owner.GetComponent <Transform>();
            _inventory       = Owner.GetComponent <InventoryBehavior>();
            _cameraTransform = Camera.main.GetComponent <Transform>();
            _playerCursor    = GetComponentInChildren <CursorBehavior>().PlayerCursor;
            _desiredVelocity = new DesiredVelocity();

            SetMouseLock(true);

            _gameControls = new InputActionCollection()
            {
                { KeyboardEventType.ToggleDown, KeyCode.F5, ReloadLevel },
                { KeyboardEventType.ToggleDown, KeyCode.F1, PauseGame },
                { KeyboardEventType.ToggleDown, KeyCode.F2, UnpauseGame },
            };

            _inputAction = new InputActionCollection()
            {
                { KeyboardEventType.Down, KeyCode.W, () => _desiredVelocity.Forward() },
                { KeyboardEventType.Down, KeyCode.S, () => _desiredVelocity.Backward() },
                { KeyboardEventType.Down, KeyCode.A, () => _desiredVelocity.Left() },
                { KeyboardEventType.Down, KeyCode.D, () => _desiredVelocity.Right() },
                { KeyboardEventType.Down, KeyCode.Alpha1, () => _inventory.TrySwitchTo(0) },
                { KeyboardEventType.Down, KeyCode.Alpha2, () => _inventory.TrySwitchTo(1) },
                { KeyboardEventType.Down, KeyCode.Alpha3, () => _inventory.TrySwitchTo(2) },
                { KeyboardEventType.Down, KeyCode.Alpha4, () => _inventory.TrySwitchTo(3) },
                { KeyboardEventType.Down, KeyCode.Alpha5, () => _inventory.TrySwitchTo(4) },
                { KeyboardEventType.Down, KeyCode.Alpha6, () => _inventory.TrySwitchTo(5) },
                { KeyboardEventType.Down, KeyCode.Alpha7, () => _inventory.TrySwitchTo(6) },
                { KeyboardEventType.Down, KeyCode.Alpha8, () => _inventory.TrySwitchTo(7) },
                { KeyboardEventType.Down, KeyCode.Alpha9, () => _inventory.TrySwitchTo(8) },
                { KeyboardEventType.Down, KeyCode.Alpha0, () => _inventory.TrySwitchTo(9) },
                { KeyboardEventType.Down, KeyCode.R, () => _inventory.TryReload() },
                { MouseEventType.ButtonDown, MouseButton.LeftButton, () => _inventory.TryTrigger1() },
                { MouseEventType.ButtonDown, MouseButton.RightButton, () => _inventory.TryTrigger2() },
            };
        }
Example #4
0
        public override MovementOutput GetMovement()
        {
            DesiredOutput = DesiredMovement.GetMovement();

            DesiredVelocity = this.Character.velocity + DesiredOutput.linear;

            if (DesiredVelocity.magnitude > MaxSpeed)
            {
                DesiredVelocity.Normalize();
                DesiredVelocity *= MaxSpeed;
            }
            samples.Add(DesiredVelocity);

            for (int i = 0; i < numSamples; i++)
            {
                angle          = Random.Range(0, MathConstants.MATH_2PI);
                magnitude      = Random.Range(0, MaxSpeed);
                velocitySample = MathHelper.ConvertOrientationToVector(angle) * magnitude;
                samples.Add(velocitySample);
            }
            base.Target.velocity = GetBestSample(samples, DesiredVelocity);
            return(base.GetMovement());
        }