Beispiel #1
0
        public GameInput ConsumeInput()
        {
            GameInput result = Input;

            Input.Reset();
            return(result);
        }
Beispiel #2
0
        public void Update(float deltaSeconds)
        {
            KeyboardState newKeyboard = Keyboard.GetState();
            MouseState    newMouse    = Mouse.GetState();

            GamePadState[] newGamepad = new GamePadState[NUM_SUPPORTED_GAMEPADS];
            for (int gamepadIndex = 0; gamepadIndex < NUM_SUPPORTED_GAMEPADS; gamepadIndex++)
            {
                newGamepad[gamepadIndex] = GamePad.GetState(gamepadIndex);
            }

            Vector2 mouseDelta         = Vector2.Zero;
            Vector2 timelessMouseDelta = Vector2.Zero;

            if (deltaSeconds > 0)
            {
                mouseDelta         = (newMouse.Position - _prevMouse.Position).ToVector2() * MouseSensitivity;
                timelessMouseDelta = mouseDelta / deltaSeconds;
            }

            //
            // Character input
            //
            {
                // Reset
                CharacterInput.Reset();
                CharacterMovement = Vector2.Zero;

                // Mouse
                Vector2 mouseMovement = Vector2.Zero;

                // Keyboard
                Vector2 keyboardMovement = new Vector2();
                if (newKeyboard.IsKeyDown(Keys.Left))
                {
                    keyboardMovement.X -= 1.0f;
                }
                if (newKeyboard.IsKeyDown(Keys.Right))
                {
                    keyboardMovement.X += 1.0f;
                }
                if (newKeyboard.IsKeyDown(Keys.Up))
                {
                    keyboardMovement.Y -= 1.0f;
                }
                if (newKeyboard.IsKeyDown(Keys.Down))
                {
                    keyboardMovement.Y += 1.0f;
                }
                if (newKeyboard.WasKeyPressed(Keys.Space, ref _prevKeyboard))
                {
                    CharacterInput.WantsAttack = true;
                }
                if (newKeyboard.WasKeyPressed(Keys.Enter, ref _prevKeyboard))
                {
                    CharacterInput.WantsInteraction = true;
                }
                if (newKeyboard.WasKeyPressed(Keys.Escape, ref _prevKeyboard))
                {
                    CharacterInput.WantsPause = true;
                }

                // Gamepad
                const int padIndex        = 0;
                Vector2   gamepadMovement = newGamepad[padIndex].ThumbSticks.Left * LeftThumbstickSensitivity[padIndex];
                if (newGamepad[padIndex].WasButtonPressed(Buttons.X, ref _prevGamepad[padIndex]))
                {
                    CharacterInput.WantsAttack = true;
                }
                if (newGamepad[padIndex].WasButtonPressed(Buttons.A, ref _prevGamepad[padIndex]))
                {
                    CharacterInput.WantsInteraction = true;
                }
                if (newGamepad[padIndex].WasButtonPressed(Buttons.Start, ref _prevGamepad[padIndex]))
                {
                    CharacterInput.WantsInteraction = true;
                }

                // Finalize
                CharacterMovement = (keyboardMovement + gamepadMovement).GetClampedTo(1.0f) + mouseMovement;
            }

            //
            // Companion input
            //
            {
                CompanionInput.Reset();
                CompanionMovement = Vector2.Zero;

                // Mouse
                Vector2 mouseMovement = Vector2.Zero;

                // Keyboard
                Vector2 keyboardMovement = new Vector2();

                // Gamepad
                const int padIndex        = 1;
                Vector2   gamepadMovement = newGamepad[padIndex].ThumbSticks.Left * LeftThumbstickSensitivity[padIndex];
                if (newGamepad[padIndex].IsButtonDown(Buttons.Y) && _prevGamepad[padIndex].IsButtonUp(Buttons.Y))
                {
                    CompanionInput.WantsAttack = true;
                }
                if (newGamepad[padIndex].IsButtonDown(Buttons.A) && _prevGamepad[padIndex].IsButtonUp(Buttons.A))
                {
                    CompanionInput.WantsInteraction = true;
                }
                if (newGamepad[padIndex].IsButtonDown(Buttons.Start) && _prevGamepad[padIndex].IsButtonUp(Buttons.Start))
                {
                    CompanionInput.WantsInteraction = true;
                }

                // Finalize
                CompanionMovement = (keyboardMovement + gamepadMovement).GetClampedTo(1.0f) + mouseMovement;
            }

            //
            // Debug input
            //
            {
                DebugInput.Reset();
                DebugMovement = Vector2.Zero;

                // Mouse
                Vector2 mouseMovement = Vector2.Zero;
                //mouseMovement = timelessMouseDelta;

                // Keyboard
                Vector2 keyboardMovement = new Vector2();
                if (newKeyboard.IsKeyDown(Keys.A))
                {
                    keyboardMovement.X -= 1.0f;
                }
                if (newKeyboard.IsKeyDown(Keys.D))
                {
                    keyboardMovement.X += 1.0f;
                }
                if (newKeyboard.IsKeyDown(Keys.W))
                {
                    keyboardMovement.Y -= 1.0f;
                }
                if (newKeyboard.IsKeyDown(Keys.S))
                {
                    keyboardMovement.Y += 1.0f;
                }

                if (newKeyboard.WasKeyPressed(Keys.F1, ref _prevKeyboard))
                {
                    DebugInput.ToggleMainDrawing = true;
                }
                if (newKeyboard.WasKeyPressed(Keys.F2, ref _prevKeyboard))
                {
                    DebugInput.ToggleDebugDrawing = true;
                }
                if (newKeyboard.WasKeyPressed(Keys.F3, ref _prevKeyboard))
                {
                    DebugInput.TogglePhysicsDebugView = true;
                }
                if (newKeyboard.WasKeyPressed(Keys.F4, ref _prevKeyboard))
                {
                    DebugInput.ToggleCameraVisibilityBounds = true;
                }

                if (newKeyboard.WasKeyPressed(Keys.D1, ref _prevKeyboard))
                {
                    DebugInput.SpeedMultiplier -= 0.5f;
                }
                if (newKeyboard.WasKeyPressed(Keys.D2, ref _prevKeyboard))
                {
                    DebugInput.SpeedMultiplier += 0.5f;
                }
                if (newKeyboard.WasKeyPressed(Keys.D3, ref _prevKeyboard))
                {
                    DebugInput.SpeedMultiplier = 1.0f;
                }
                else
                {
                    DebugInput.SpeedMultiplier = MathHelper.Clamp(DebugInput.SpeedMultiplier, min: 0.1f, max: 10.0f);
                }
                if (newKeyboard.WasKeyPressed(Keys.D4, ref _prevKeyboard))
                {
                    DebugInput.ResetCameraPosition = true;
                }


                // Gamepad
                const int padIndex        = 0;
                Vector2   gamepadMovement = newGamepad[padIndex].ThumbSticks.Right * RightThumbstickSensitivity[padIndex];

                // Finalize
                DebugMovement = (keyboardMovement + gamepadMovement).GetClampedTo(1.0f) + mouseMovement;
            }

            //
            // Platform input
            //
            PlatformInput.Reset();

            bool isAltDown = newKeyboard.IsKeyDown(Keys.LeftAlt) || newKeyboard.IsKeyDown(Keys.RightAlt);

            if (newKeyboard.WasKeyPressed(Keys.Escape, ref _prevKeyboard))
            {
                PlatformInput.WantsExit = true;
            }
            if (isAltDown && newKeyboard.WasKeyPressed(Keys.F4, ref _prevKeyboard))
            {
                PlatformInput.WantsExit = true;
            }

            if (isAltDown && newKeyboard.WasKeyPressed(Keys.Enter, ref _prevKeyboard))
            {
                PlatformInput.ToggleFullscreen = true;
            }


            _prevKeyboard = newKeyboard;
            _prevGamepad  = newGamepad;
            _prevMouse    = newMouse;
        }