示例#1
0
        /// <summary>
        /// Handles the input and movement of the character.
        /// </summary>
        /// <param name="dt">Time since last frame in simulation seconds.</param>
        /// <param name="previousKeyboardInput">The last frame's keyboard state.</param>
        /// <param name="keyboardInput">The current frame's keyboard state.</param>
        /// <param name="previousGamePadInput">The last frame's gamepad state.</param>
        /// <param name="gamePadInput">The current frame's keyboard state.</param>
        public void Update(float dt, KeyboardState previousKeyboardInput, KeyboardState keyboardInput, GamePadState previousGamePadInput, GamePadState gamePadInput)
        {
            if (IsActive)
            {
                CameraControlScheme.Update(dt);

                Vector2 totalMovement = Vector2.Zero;

#if XBOX360
                totalMovement += new Vector2(gamePadInput.ThumbSticks.Left.X, gamePadInput.ThumbSticks.Left.Y);

                CharacterController.HorizontalMotionConstraint.SpeedScale        = Math.Min(totalMovement.Length(), 1); //Don't trust the game pad to output perfectly normalized values.
                CharacterController.HorizontalMotionConstraint.MovementDirection = totalMovement;

                CharacterController.StanceManager.DesiredStance = gamePadInput.IsButtonDown(Buttons.RightStick) ? Stance.Crouching : Stance.Standing;

                //Jumping
                if (previousGamePadInput.IsButtonUp(Buttons.LeftStick) && gamePadInput.IsButtonDown(Buttons.LeftStick))
                {
                    CharacterController.Jump();
                }
#else
                //Collect the movement impulses.

                if (keyboardInput.IsKeyDown(Keys.E))
                {
                    totalMovement += new Vector2(0, 1);
                }
                if (keyboardInput.IsKeyDown(Keys.D))
                {
                    totalMovement += new Vector2(0, -1);
                }
                if (keyboardInput.IsKeyDown(Keys.S))
                {
                    totalMovement += new Vector2(-1, 0);
                }
                if (keyboardInput.IsKeyDown(Keys.F))
                {
                    totalMovement += new Vector2(1, 0);
                }
                if (totalMovement == Vector2.Zero)
                {
                    CharacterController.HorizontalMotionConstraint.MovementDirection = Vector2.Zero;
                }
                else
                {
                    CharacterController.HorizontalMotionConstraint.MovementDirection = Vector2.Normalize(totalMovement);
                }


                //Jumping
                if (previousKeyboardInput.IsKeyUp(Keys.A) && keyboardInput.IsKeyDown(Keys.A))
                {
                    CharacterController.Jump();
                }
#endif
                CharacterController.ViewDirection = Camera.WorldMatrix.Forward;
            }
        }
        /// <summary>
        /// Handles the input and movement of the character.
        /// </summary>
        /// <param name="dt">Time since last frame in simulation seconds.</param>
        /// <param name="previousKeyboardInput">The last frame's keyboard state.</param>
        /// <param name="keyboardInput">The current frame's keyboard state.</param>
        /// <param name="previousGamePadInput">The last frame's gamepad state.</param>
        /// <param name="gamePadInput">The current frame's keyboard state.</param>
        public override void Update(float dt, KeyboardState previousKeyboardInput, KeyboardState keyboardInput, GamePadState previousGamePadInput, GamePadState gamePadInput)
        {
            if (IsActive)
            {
                //Note that the character controller's update method is not called here; this is because it is handled within its owning space.
                //This method's job is simply to tell the character to move around.

                CameraControlScheme.Update(dt);

                Vector2 totalMovement = Vector2.Zero;

#if XBOX360
                totalMovement += new Vector2(gamePadInput.ThumbSticks.Left.X, gamePadInput.ThumbSticks.Left.Y);

                CharacterController.HorizontalMotionConstraint.SpeedScale        = Math.Min(totalMovement.Length(), 1); //Don't trust the game pad to output perfectly normalized values.
                CharacterController.HorizontalMotionConstraint.MovementDirection = totalMovement;

                CharacterController.StanceManager.DesiredStance = gamePadInput.IsButtonDown(Buttons.RightStick) ? Stance.Crouching : Stance.Standing;

                //Jumping
                if (previousGamePadInput.IsButtonUp(Buttons.LeftStick) && gamePadInput.IsButtonDown(Buttons.LeftStick))
                {
                    CharacterController.Jump();
                }
#else
                //Collect the movement impulses.



                if (keyboardInput.IsKeyDown(Keys.E))
                {
                    totalMovement += new Vector2(0, 1);
                }
                if (keyboardInput.IsKeyDown(Keys.D))
                {
                    totalMovement += new Vector2(0, -1);
                }
                if (keyboardInput.IsKeyDown(Keys.S))
                {
                    totalMovement += new Vector2(-1, 0);
                }
                if (keyboardInput.IsKeyDown(Keys.F))
                {
                    totalMovement += new Vector2(1, 0);
                }
                if (totalMovement == Vector2.Zero)
                {
                    CharacterController.HorizontalMotionConstraint.MovementDirection = Vector2.Zero;
                }
                else
                {
                    CharacterController.HorizontalMotionConstraint.MovementDirection = Vector2.Normalize(totalMovement);
                }


                if (keyboardInput.IsKeyDown(Keys.X))
                {
                    CharacterController.StanceManager.DesiredStance = Stance.Prone;
                }
                else if (keyboardInput.IsKeyDown(Keys.Z))
                {
                    CharacterController.StanceManager.DesiredStance = Stance.Crouching;
                }
                else
                {
                    CharacterController.StanceManager.DesiredStance = Stance.Standing;
                }

                //Jumping
                if (previousKeyboardInput.IsKeyUp(Keys.A) && keyboardInput.IsKeyDown(Keys.A))
                {
                    CharacterController.Jump();
                }
#endif
                CharacterController.ViewDirection = Camera.WorldMatrix.Forward;

                Console.WriteLine(CharacterController.Body.Position);
            }
        }
示例#3
0
        /// <summary>
        /// Handles the input and movement of the character.
        /// </summary>
        /// <param name="dt">Time since last frame in simulation seconds.</param>
        /// <param name="previousKeyboardInput">The last frame's keyboard state.</param>
        /// <param name="keyboardInput">The current frame's keyboard state.</param>
        /// <param name="previousGamePadInput">The last frame's gamepad state.</param>
        /// <param name="gamePadInput">The current frame's keyboard state.</param>
        public void Update(float dt)
        {
            if (IsActive)
            {
                //Note that the character controller's update method is not called here; this is because it is handled within its owning space.
                //This method's job is simply to tell the character to move around.

                CameraControlScheme.Update(dt);

                Vector2 totalMovement = Vector2.Zero;


                if (Input.ControlScheme == ControlScheme.XboxController)
                {
                    totalMovement += new Vector2(Input.CurrentPad.ThumbSticks.Left.X, Input.CurrentPad.ThumbSticks.Left.Y);

                    CharacterController.HorizontalMotionConstraint.SpeedScale        = Math.Min(totalMovement.Length(), 1); //Don't trust the game pad to output perfectly normalized values.
                    CharacterController.HorizontalMotionConstraint.MovementDirection = totalMovement;

                    CharacterController.StanceManager.DesiredStance = Input.CurrentPad.IsButtonDown(Buttons.B) ? Stance.Crouching : Stance.Standing;

                    //Jumping
                    if (Input.CurrentPadLastFrame.IsButtonUp(Buttons.A) && Input.CurrentPad.IsButtonDown(Buttons.A))
                    {
                        CharacterController.Jump();
                    }
                }
                else if (Input.ControlScheme == ControlScheme.Keyboard)
                {
                    //Collect the movement impulses.

                    if (Input.KeyboardState.IsKeyDown(Keys.W))
                    {
                        totalMovement += new Vector2(0, 1);
                    }
                    if (Input.KeyboardState.IsKeyDown(Keys.S))
                    {
                        totalMovement += new Vector2(0, -1);
                    }
                    if (Input.KeyboardState.IsKeyDown(Keys.A))
                    {
                        totalMovement += new Vector2(-1, 0);
                    }
                    if (Input.KeyboardState.IsKeyDown(Keys.D))
                    {
                        totalMovement += new Vector2(1, 0);
                    }
                    if (totalMovement == Vector2.Zero)
                    {
                        CharacterController.HorizontalMotionConstraint.MovementDirection = Vector2.Zero;
                    }
                    else
                    {
                        CharacterController.HorizontalMotionConstraint.MovementDirection = Vector2.Normalize(totalMovement);
                    }


                    CharacterController.StanceManager.DesiredStance = Input.KeyboardState.IsKeyDown(Keys.LeftShift) ? Stance.Crouching : Stance.Standing;

                    //Jumping
                    if (Input.KeyboardLastFrame.IsKeyUp(Keys.Space) && Input.KeyboardState.IsKeyDown(Keys.Space))
                    {
                        CharacterController.Jump();
                    }
                }
                CharacterController.ViewDirection = Camera.WorldMatrix.Forward;

                #region Grabber Input

                //Update grabber

                if (((Input.ControlScheme == ControlScheme.XboxController && Input.CheckXboxJustPressed(Buttons.X)) ||
                     (Input.ControlScheme == ControlScheme.Keyboard && Input.CheckKeyboardPress(Keys.E))) && !grabber.IsUpdating)
                {
                    //Find the earliest ray hit
                    RayCastResult raycastResult;
                    if (Space.RayCast(new Ray(Renderer.Camera.Position, Renderer.Camera.WorldMatrix.Forward), 6, RayCastFilter, out raycastResult))
                    {
                        var entityCollision = raycastResult.HitObject as EntityCollidable;
                        //If there's a valid ray hit, then grab the connected object!
                        if (entityCollision != null)
                        {
                            var tag = entityCollision.Entity.Tag as GameModel;
                            if (tag != null)
                            {
                                tag.Texture.GameProperties.RayCastHit(tag.Texture.GameProperties);
                                if (!tag.Texture.Wireframe && tag.Texture.GameProperties.Grabbable)
                                {
                                    grabber.Setup(entityCollision.Entity, raycastResult.HitData.Location);
                                    try
                                    {
                                        CollisionRules.AddRule(entityCollision.Entity, CharacterController.Body, CollisionRule.NoBroadPhase);
                                    }
                                    catch { }
                                    grabDistance = raycastResult.HitData.T;
                                }
                                else
                                {
                                    Program.Game.Loader.InvalidApplicationRemovalGrab.Play();
                                }
                            }
                            else
                            {
                                Program.Game.Loader.InvalidApplicationRemovalGrab.Play();
                            }
                        }
                    }
                    else
                    {
                        Program.Game.Loader.InvalidApplicationRemovalGrab.Play();
                    }
                }
                else if (grabber.IsUpdating)
                {
                    if ((Input.ControlScheme == ControlScheme.XboxController && Input.CheckXboxJustPressed(Buttons.X)) ||
                        (Input.ControlScheme == ControlScheme.Keyboard && Input.CheckKeyboardPress(Keys.E)))
                    {
                        try
                        {
                            CollisionRules.RemoveRule(grabber.Entity, CharacterController.Body);
                        }
                        catch { }
                        grabber.Release();
                    }
                    grabber.GoalPosition = Renderer.Camera.Position + Renderer.Camera.WorldMatrix.Forward * grabDistance;
                }
                #endregion
            }
        }