Пример #1
0
        /// <summary>
        /// Handle thumbstick logic
        /// </summary>
        private void HandleThumbStick()
        {
            // Calculate the rectangle of the outer circle of the thumbstick
            Rectangle outerControlstick = new Rectangle(0, (int)controlstickBoundaryPosition.Y - 35,
                                                        controlstickBoundary.Width + 60, controlstickBoundary.Height + 60);

            // Reset the thumbstick position when it is idle
            if (VirtualThumbsticks.LeftThumbstick == Vector2.Zero)
            {
                IsInMotion = false;
                beeKeeper.SetMovement(Vector2.Zero);
                controlstickStartupPosition = new Vector2(55, 369);
            }
            else
            {
                // If not in motion and the touch point is not in the control bounds - there is no movement
                Rectangle touchRectangle = new Rectangle((int)lastTouchPosition.X, (int)lastTouchPosition.Y, 1, 1);

                if (!outerControlstick.Contains(touchRectangle))
                {
                    controlstickStartupPosition = new Vector2(55, 369);
                    IsInMotion = false;
                    return;
                }

                // Move the beekeeper
                SetMotion();

                // Moves the thumbstick's inner circle
                float radius = controlstick.Width / 2 + 35;
                controlstickStartupPosition = new Vector2(55, 369) + (VirtualThumbsticks.LeftThumbstick * radius);
            }
        }
Пример #2
0
        /// <summary>
        /// Moves the beekeeper.
        /// </summary>
        /// <returns>Returns a vector indicating the beekeeper's movement direction.
        /// </returns>
        private Vector2 SetMotion(InputState input)
        {
            // Calculate the beekeeper location, if allow moving
            Rectangle safeArea = SafeArea;

            PlayerIndex playerIndex;

            Vector2 leftThumbstick = VirtualThumbsticks.LeftThumbstick;

            // If we have no phone directional input, try the gamepad
            if (leftThumbstick == Vector2.Zero)
            {
                leftThumbstick = input.LeftThumbstick(ControllingPlayer, out playerIndex) * new Vector2(1, -1);
            }

            // Move on to keyboard input if we still have nothing
            if (leftThumbstick == Vector2.Zero)
            {
                float vecX = 0;
                float vecY = 0;

                if (input.IsKeyDown(Keys.Left, ControllingPlayer, out playerIndex))
                {
                    vecX--;
                }
                if (input.IsKeyDown(Keys.Right, ControllingPlayer, out playerIndex))
                {
                    vecX++;
                }
                if (input.IsKeyDown(Keys.Up, ControllingPlayer, out playerIndex))
                {
                    vecY--;
                }
                if (input.IsKeyDown(Keys.Down, ControllingPlayer, out playerIndex))
                {
                    vecY++;
                }

                leftThumbstick = new Vector2(vecX, vecY);
            }

            Vector2 movementVector = leftThumbstick * 12f * ScreenManager.SpriteBatch.ScaleVector;

            Rectangle futureBounds = beeKeeper.Bounds;

            futureBounds.X += (int)movementVector.X;
            futureBounds.Y += (int)movementVector.Y;

            if (futureBounds.Left <= safeArea.Left || futureBounds.Right >= safeArea.Right)
            {
                movementVector.X = 0;
            }
            if (futureBounds.Top <= safeArea.Top || futureBounds.Bottom >= safeArea.Bottom)
            {
                movementVector.Y = 0;
            }

            if (movementVector == Vector2.Zero)
            {
                IsInMotion = false;
                beeKeeper.SetMovement(Vector2.Zero);
            }
            else
            {
                Vector2 beekeeperCalculatedPosition =
                    new Vector2(beeKeeper.CentralCollisionArea.X, beeKeeper.CentralCollisionArea.Y) + movementVector;

                if (!CheckBeehiveCollision(beekeeperCalculatedPosition))
                {
                    beeKeeper.SetMovement(movementVector);
                    IsInMotion = true;
                }
            }

            return(movementVector);
        }