예제 #1
0
        /// <summary>
        /// Read keyboard and gamepad input
        /// </summary>
        private void HandleInput(float elapsedTime)
        {
            previousGamePadState  = currentGamePadState;
            previousKeyboardState = currentKeyboardState;
            previousMouseState    = currentMouseState;
            currentGamePadState   = GamePad.GetState(PlayerIndex.One);
            currentKeyboardState  = Keyboard.GetState();
            currentMouseState     = Mouse.GetState();

            currentTouchCollection = TouchPanel.GetState();

            //bool touched = false;
            int touchCount = 0;

            // tap the screen to select
            foreach (TouchLocation location in currentTouchCollection)
            {
                switch (location.State)
                {
                case TouchLocationState.Pressed:
                    //touched = true;
                    touchCount++;
                    cursorLocation = location.Position;
                    break;

                case TouchLocationState.Moved:
                    break;

                case TouchLocationState.Released:
                    break;
                }
            }

            if (currentMouseState.LeftButton == ButtonState.Released && previousMouseState.LeftButton == ButtonState.Pressed)
            {
                cursorLocation.X = currentMouseState.X;
                cursorLocation.Y = currentMouseState.Y;
                touchCount       = 1;
            }

            if (currentMouseState.MiddleButton == ButtonState.Released && previousMouseState.MiddleButton == ButtonState.Pressed)
            {
                touchCount = 2;
            }

            if (currentMouseState.RightButton == ButtonState.Released && previousMouseState.RightButton == ButtonState.Pressed)
            {
                touchCount = 3;
            }

            // Allows the game to exit
            if (currentGamePadState.Buttons.Back == ButtonState.Pressed ||
                currentKeyboardState.IsKeyDown(Keys.Escape))
            {
                this.Exit();
            }

            // Update the cursor location by listening for left thumbstick input on
            // the GamePad and direction key input on the Keyboard, making sure to
            // keep the cursor inside the screen boundary
            cursorLocation.X +=
                currentGamePadState.ThumbSticks.Left.X * cursorMoveSpeed * elapsedTime;
            cursorLocation.Y -=
                currentGamePadState.ThumbSticks.Left.Y * cursorMoveSpeed * elapsedTime;

            if (currentKeyboardState.IsKeyDown(Keys.Up))
            {
                cursorLocation.Y -= elapsedTime * cursorMoveSpeed;
            }
            if (currentKeyboardState.IsKeyDown(Keys.Down))
            {
                cursorLocation.Y += elapsedTime * cursorMoveSpeed;
            }
            if (currentKeyboardState.IsKeyDown(Keys.Left))
            {
                cursorLocation.X -= elapsedTime * cursorMoveSpeed;
            }
            if (currentKeyboardState.IsKeyDown(Keys.Right))
            {
                cursorLocation.X += elapsedTime * cursorMoveSpeed;
            }
            cursorLocation.X = MathHelper.Clamp(cursorLocation.X, 0f, screenWidth);
            cursorLocation.Y = MathHelper.Clamp(cursorLocation.Y, 0f, screenHeight);

            // Change the tank move behavior if the user pressed B on
            // the GamePad or on the Keyboard.
            if ((previousGamePadState.Buttons.B == ButtonState.Released &&
                 currentGamePadState.Buttons.B == ButtonState.Pressed) ||
                (previousKeyboardState.IsKeyUp(Keys.B) &&
                 currentKeyboardState.IsKeyDown(Keys.B)) || (touchCount == 2))
            {
                tank.CycleBehaviorType();
            }

            // Add the cursor's location to the WaypointList if the user pressed A on
            // the GamePad or on the Keyboard.
            if ((previousGamePadState.Buttons.A == ButtonState.Released &&
                 currentGamePadState.Buttons.A == ButtonState.Pressed) ||
                (previousKeyboardState.IsKeyUp(Keys.A) &&
                 currentKeyboardState.IsKeyDown(Keys.A)) || (touchCount == 1))
            {
                tank.Waypoints.Enqueue(cursorLocation);
            }

            // Delete all the current waypoints and reset the tanks’ location if
            // the user pressed X on the GamePad or on the Keyboard.
            if ((previousGamePadState.Buttons.X == ButtonState.Released &&
                 currentGamePadState.Buttons.X == ButtonState.Pressed) ||
                (previousKeyboardState.IsKeyUp(Keys.X) &&
                 currentKeyboardState.IsKeyDown(Keys.X)) || (touchCount == 3))
            {
                tank.Reset(
                    new Vector2((float)screenWidth / 4, (float)screenHeight / 4));
            }
        }
예제 #2
0
        /// <summary>
        /// Read keyboard and gamepad input
        /// </summary>
        private void HandleInput(float elapsedTime)
        {
            previousGamePadState  = currentGamePadState;
            previousKeyboardState = currentKeyboardState;
            currentGamePadState   = GamePad.GetState(PlayerIndex.One);
            currentKeyboardState  = Keyboard.GetState();

            // Allows the game to exit
            if (currentGamePadState.Buttons.Back == ButtonState.Pressed ||
                currentKeyboardState.IsKeyDown(Keys.Escape))
            {
                this.Exit();
            }

            // Update the cursor location by listening for left thumbstick input on
            // the GamePad and direction key input on the Keyboard, making sure to
            // keep the cursor inside the screen boundary
            cursorLocation.X +=
                currentGamePadState.ThumbSticks.Left.X * cursorMoveSpeed * elapsedTime;
            cursorLocation.Y -=
                currentGamePadState.ThumbSticks.Left.Y * cursorMoveSpeed * elapsedTime;

            if (currentKeyboardState.IsKeyDown(Keys.Up))
            {
                cursorLocation.Y -= elapsedTime * cursorMoveSpeed;
            }
            if (currentKeyboardState.IsKeyDown(Keys.Down))
            {
                cursorLocation.Y += elapsedTime * cursorMoveSpeed;
            }
            if (currentKeyboardState.IsKeyDown(Keys.Left))
            {
                cursorLocation.X -= elapsedTime * cursorMoveSpeed;
            }
            if (currentKeyboardState.IsKeyDown(Keys.Right))
            {
                cursorLocation.X += elapsedTime * cursorMoveSpeed;
            }

#if WINDOWS_PHONE
            bool isTouchDetected = false;
            bool isMenuBarUsed   = false;

            TouchCollection touches = TouchPanel.GetState();

            if (touches.Count == 1)
            {
                //Use only the single (first) touch point to make experience close to Windows/XBOX - Simulates move by mouse/gamepad & "A" button press
                TouchLocation touch = touches[0];
                if (touch.State != TouchLocationState.Invalid)
                {
                    //TODO: Check on device and swap X/Y + remove Y tweaking. Workaround for EMU:
                    double halfHeight = screenHeight / 2;
                    double delta      = halfHeight - touch.Position.X;

                    //Check "button click" on menuBar
                    if (halfHeight + delta < menuBar_Height)
                    {
                        Rectangle touchRect   = new Rectangle((int)touch.Position.Y, (int)(halfHeight + delta - 5) - 5, 10, 10);
                        Rectangle button1Rect = new Rectangle(menuBarButton1_Left, menuBarButtonTop, menuBarButtonWidth, menuBarButtonHeight);
                        Rectangle button2Rect = new Rectangle(menuBarButton2_Left, menuBarButtonTop, menuBarButtonWidth, menuBarButtonHeight);

                        bool button1Press, button2Press;
                        button1Rect.Intersects(ref touchRect, out button1Press);
                        button2Rect.Intersects(ref touchRect, out button2Press);

                        if (button1Press && touch.State == TouchLocationState.Released)
                        {
                            isClearRequested = true;
                        }
                        else if (button2Press && touch.State == TouchLocationState.Released)
                        {
                            isBehaviorChangeRequested = true;
                        }

                        isMenuBarUsed = true;
                    }
                    else
                    {
                        cursorLocation.X = touch.Position.Y;
                        cursorLocation.Y = (int)(halfHeight + delta);
                    }

                    //Don't let the cursor move under the MenuBar
                    if (cursorLocation.Y < menuBar_Height + (cursorTexture.Height / 2))
                    {
                        cursorLocation.Y = menuBar_Height + (cursorTexture.Height / 2);
                    }
                }

                if (touch.State == TouchLocationState.Released && !isMenuBarUsed)
                {
                    isTouchDetected = true;
                }
            }
            else if (touches.Count > 1 && touches[0].State == TouchLocationState.Released) //Multiple touch simulates pressing "B" button press to change the behavior
            {
                isBehaviorChangeRequested = true;
            }
#endif

            cursorLocation.X = MathHelper.Clamp(cursorLocation.X, 0f, screenWidth);
            cursorLocation.Y = MathHelper.Clamp(cursorLocation.Y, 0f, screenHeight);

            // Change the tank move behavior if the user pressed B on
            // the GamePad or on the Keyboard.
            if ((previousGamePadState.Buttons.B == ButtonState.Released &&
                 currentGamePadState.Buttons.B == ButtonState.Pressed) ||
                (previousKeyboardState.IsKeyUp(Keys.B) &&
                 currentKeyboardState.IsKeyDown(Keys.B))
#if WINDOWS_PHONE
                || isBehaviorChangeRequested
#endif
                )
            {
                tank.CycleBehaviorType();

#if WINDOWS_PHONE
                isBehaviorChangeRequested = false;
#endif
            }

            // Add the cursor's location to the WaypointList if the user pressed A on
            // the GamePad or on the Keyboard.
            if ((previousGamePadState.Buttons.A == ButtonState.Released &&
                 currentGamePadState.Buttons.A == ButtonState.Pressed) ||
                (previousKeyboardState.IsKeyUp(Keys.A) &&
                 currentKeyboardState.IsKeyDown(Keys.A))
#if WINDOWS_PHONE
                || isTouchDetected
#endif
                )
            {
                tank.Waypoints.Enqueue(cursorLocation);
            }

            // Delete all the current waypoints and reset the tanks’ location if
            // the user pressed X on the GamePad or on the Keyboard.
            if ((previousGamePadState.Buttons.X == ButtonState.Released &&
                 currentGamePadState.Buttons.X == ButtonState.Pressed) ||
                (previousKeyboardState.IsKeyUp(Keys.X) &&
                 currentKeyboardState.IsKeyDown(Keys.X))
#if WINDOWS_PHONE
                || isClearRequested
#endif
                )
            {
                tank.Reset(
                    new Vector2((float)screenWidth / 4, (float)screenHeight / 4));

#if WINDOWS_PHONE
                isClearRequested = false;
#endif
            }
        }