/// <summary> /// Handle the player's input. /// </summary> /// <param name="input"></param> public override void HandleInput(GameTime gameTime, InputState input) { if (IsActive) { if (input == null) { throw new ArgumentNullException("input"); } VirtualThumbsticks.Update(input); } if (input.TouchState.Count > 0) { foreach (TouchLocation touch in input.TouchState) { lastTouchPosition = touch.Position; } } isSmokebuttonClicked = false; PlayerIndex player; // If there was any touch if (VirtualThumbsticks.RightThumbstickCenter.HasValue) { // Button Bounds Rectangle buttonRectangle = new Rectangle((int)smokeButtonPosition.X, (int)smokeButtonPosition.Y, smokeButton.Width / 2, smokeButton.Height); // Touch Bounds Rectangle touchRectangle = new Rectangle((int)VirtualThumbsticks.RightThumbstickCenter.Value.X, (int)VirtualThumbsticks.RightThumbstickCenter.Value.Y, 1, 1); // If the touch is in the button if (buttonRectangle.Contains(touchRectangle) && !beeKeeper.IsCollectingHoney && !beeKeeper.IsStung) { isSmokebuttonClicked = true; } } if (input.IsKeyDown(Keys.Space, ControllingPlayer, out player) && !beeKeeper.IsCollectingHoney && !beeKeeper.IsStung) { isSmokebuttonClicked = true; } if (input.Gestures.Count > 0) { if (isLevelEnd) { if (input.Gestures[0].GestureType == GestureType.Tap) { ScreenManager.RemoveScreen(this); ScreenManager.AddScreen(new GameplayScreen(DifficultyMode.Easy), null); } } } }
/// <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; // 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; }
/// <summary> /// Handle the player's input. /// </summary> /// <param name="input"></param> public override void HandleInput(GameTime gameTime, InputState input) { if (IsActive) { if (input == null) { throw new ArgumentNullException("input"); } if (input.IsPauseGame(null)) { PauseCurrentGame(); } } if (input.TouchState.Count > 0) { foreach (TouchLocation touch in input.TouchState) { lastTouchPosition = touch.Position; } } isSmokebuttonClicked = false; PlayerIndex player; VirtualThumbsticks.Update(input); if (input.Gestures.Count > 0) { GestureSample topGesture = input.Gestures[0]; if (topGesture.GestureType == GestureType.Tap && deviceUpperRightCorner.Contains(new Point((int)topGesture.Position.X, (int)topGesture.Position.Y))) { showDebugInfo = !showDebugInfo; } } if (isLevelEnd) { if (input.Gestures.Count > 0) { if (input.Gestures[0].GestureType == GestureType.Tap) { userInputToExit = true; } } if (input.IsNewKeyPress(Keys.Enter, ControllingPlayer, out player) || input.IsNewKeyPress(Keys.Space, ControllingPlayer, out player)) { userInputToExit = true; } } if (!IsStarted) { return; } // If there was any touch if (VirtualThumbsticks.RightThumbstickCenter.HasValue) { // Button Bounds Rectangle buttonRectangle = new Rectangle((int)smokeButtonPosition.X, (int)smokeButtonPosition.Y, smokeButton.Width / 2, smokeButton.Height); // Touch Bounds Rectangle touchRectangle = new Rectangle((int)VirtualThumbsticks.RightThumbstickCenter.Value.X, (int)VirtualThumbsticks.RightThumbstickCenter.Value.Y, 1, 1); // If the touch is in the button if (buttonRectangle.Contains(touchRectangle) && !beeKeeper.IsCollectingHoney && !beeKeeper.IsStung) { isSmokebuttonClicked = true; } } // Handle keyboard if (input.IsNewKeyPress(Keys.Y, ControllingPlayer, out player)) { showDebugInfo = !showDebugInfo; } if (input.IsKeyDown(Keys.Space, ControllingPlayer, out player) && !beeKeeper.IsCollectingHoney && !beeKeeper.IsStung) { isSmokebuttonClicked = true; } movementVector = SetMotion(input); beeKeeper.SetDirection(movementVector); }
/// <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); }
/// <summary> /// Handle the player's input. /// </summary> /// <param name="input"></param> public override void HandleInput(GameTime gameTime, InputState input) { if (IsActive) { if (input == null) { throw new ArgumentNullException("input"); } if (input.IsPauseGame(null)) { PauseCurrentGame(); } } if (input.TouchState.Count > 0) { foreach (TouchLocation touch in input.TouchState) { lastTouchPosition = touch.Position; } } isSmokebuttonClicked = false; PlayerIndex player; VirtualThumbsticks.Update(input); if (input.Gestures.Count > 0) { GestureSample topGesture = input.Gestures[0]; if (topGesture.GestureType == GestureType.Tap && deviceUpperRightCorner.Contains(new Point((int)topGesture.Position.X, (int)topGesture.Position.Y))) { showDebugInfo = !showDebugInfo; } } if (isLevelEnd) { if (input.Gestures.Count > 0) { if (input.Gestures[0].GestureType == GestureType.Tap) { userInputToExit = true; } } else if (input.IsNewButtonPress(Buttons.A, ControllingPlayer, out player)) { userInputToExit = true; } if (input.IsNewKeyPress(Keys.Enter, ControllingPlayer, out player) || input.IsNewKeyPress(Keys.Space, ControllingPlayer, out player)) { userInputToExit = true; } } if (!IsStarted) { return; } // If there was any touch if (VirtualThumbsticks.RightThumbstickCenter.HasValue) { // Button Bounds Rectangle buttonRectangle = new Rectangle((int)smokeButtonPosition.X, (int)smokeButtonPosition.Y, smokeButton.Width / 2, smokeButton.Height); // Touch Bounds Rectangle touchRectangle = new Rectangle((int)VirtualThumbsticks.RightThumbstickCenter.Value.X, (int)VirtualThumbsticks.RightThumbstickCenter.Value.Y, 1, 1); // If the touch is in the button if (buttonRectangle.Contains(touchRectangle) && !beeKeeper.IsCollectingHoney && !beeKeeper.IsStung) { isSmokebuttonClicked = true; } } if (input.IsNewButtonPress(Buttons.Y, ControllingPlayer, out player)) { showDebugInfo = !showDebugInfo; } else if (input.IsButtonDown(Buttons.A, ControllingPlayer, out player) && !beeKeeper.IsCollectingHoney && !beeKeeper.IsStung) { isSmokebuttonClicked = true; } // Handle keyboard if (input.IsNewKeyPress(Keys.Y, ControllingPlayer, out player)) { showDebugInfo = !showDebugInfo; } if (input.IsKeyDown(Keys.Space, ControllingPlayer, out player) && !beeKeeper.IsCollectingHoney && !beeKeeper.IsStung) { isSmokebuttonClicked = true; } movementVector = SetMotion(input); beeKeeper.SetDirection(movementVector); }