Browse Source

Merge pull request #41 from borrillis/feature/MouseSupport4HoneycombRush

Feature/mouse support4 honeycomb rush
Dominique Louis 12 years ago
parent
commit
2a25285834

+ 97 - 1
HoneycombRush/ScreenManager/InputState.cs

@@ -24,14 +24,22 @@ namespace HoneycombRush
     /// </summary>
     public class InputState
     {
+		public enum MouseButton
+		{
+			Left,
+			Middle,
+			Right
+		}
         #region Fields
 
         public const int MaxInputs = 4;
 
         public readonly KeyboardState[] CurrentKeyboardStates;
+		public readonly MouseState[] CurrentMouseStates;
         public readonly GamePadState[] CurrentGamePadStates;
 
         public readonly KeyboardState[] LastKeyboardStates;
+		public readonly MouseState[] LastMouseStates;
         public readonly GamePadState[] LastGamePadStates;
 
         public readonly bool[] GamePadWasConnected;
@@ -51,9 +59,11 @@ namespace HoneycombRush
         public InputState()
         {
             CurrentKeyboardStates = new KeyboardState[MaxInputs];
+			CurrentMouseStates = new MouseState[MaxInputs];
             CurrentGamePadStates = new GamePadState[MaxInputs];
 
             LastKeyboardStates = new KeyboardState[MaxInputs];
+			LastMouseStates = new MouseState[MaxInputs];
             LastGamePadStates = new GamePadState[MaxInputs];
 
             GamePadWasConnected = new bool[MaxInputs];
@@ -73,9 +83,11 @@ namespace HoneycombRush
             for (int i = 0; i < MaxInputs; i++)
             {
                 LastKeyboardStates[i] = CurrentKeyboardStates[i];
+				LastMouseStates[i] = CurrentMouseStates[i];
                 LastGamePadStates[i] = CurrentGamePadStates[i];
 
                 CurrentKeyboardStates[i] = Keyboard.GetState((PlayerIndex)i);
+				CurrentMouseStates[i] = Mouse.GetState();
                 CurrentGamePadStates[i] = GamePad.GetState((PlayerIndex)i);
 
                 // Keep track of whether a gamepad has ever been
@@ -153,7 +165,90 @@ namespace HoneycombRush
             }
         }
 
-        /// <summary>
+		/// <summary>
+		/// Helper for checking if a mouse button was newly pressed during this update. The
+		/// controllingPlayer parameter specifies which player to read input for.
+		/// If this is null, it will accept input from any player. When a click
+		/// is detected, the output playerIndex reports which player pressed it.
+		/// </summary>
+		public bool IsNewMouseClick(MouseButton mouseButton , PlayerIndex? controllingPlayer,
+		                          out PlayerIndex playerIndex)
+		{
+			if (controllingPlayer.HasValue)
+			{
+				// Read input from the specified player.
+				playerIndex = controllingPlayer.Value;
+
+				int i = (int)playerIndex;
+				switch( mouseButton){
+					case MouseButton.Left:
+						return (CurrentMouseStates[i].LeftButton == ButtonState.Pressed && 
+					            LastMouseStates[i].LeftButton == ButtonState.Released);
+					case MouseButton.Right:
+						return (CurrentMouseStates[i].RightButton == ButtonState.Pressed && 
+						        LastMouseStates[i].RightButton == ButtonState.Released);
+					case MouseButton.Middle:
+						return (CurrentMouseStates[i].MiddleButton == ButtonState.Pressed && 
+						        LastMouseStates[i].MiddleButton == ButtonState.Released);
+					default:
+						return IsNewMouseClick(MouseButton.Left, controllingPlayer, out playerIndex) ||
+							   IsNewMouseClick(MouseButton.Middle, controllingPlayer, out playerIndex) ||
+							   IsNewMouseClick(MouseButton.Right, controllingPlayer, out playerIndex);
+				}
+
+			}
+			else
+			{
+				// Accept input from any player.
+				return (IsNewMouseClick(mouseButton, PlayerIndex.One, out playerIndex) ||
+				        IsNewMouseClick(mouseButton, PlayerIndex.Two, out playerIndex) ||
+				        IsNewMouseClick(mouseButton, PlayerIndex.Three, out playerIndex) ||
+				        IsNewMouseClick(mouseButton, PlayerIndex.Four, out playerIndex));
+			}
+
+		}
+
+		/// <summary>
+		/// Helper for checking if a mouse button is currently pressed during the update. The
+		/// controllingPlayer parameter specifies which player to read input for.
+		/// If this is null, it will accept input from any player. When a pressed button
+		/// is detected, the output playerIndex reports which player pressed it.
+		/// </summary>
+		public bool IsMouseDown(MouseButton mouseButton , PlayerIndex? controllingPlayer,
+		                            out PlayerIndex playerIndex)
+		{
+			if (controllingPlayer.HasValue)
+			{
+				// Read input from the specified player.
+				playerIndex = controllingPlayer.Value;
+
+				int i = (int)playerIndex;
+				switch( mouseButton  ) {
+					case MouseButton.Left:
+						return CurrentMouseStates[i].LeftButton == ButtonState.Pressed;
+					case MouseButton.Right:
+						return CurrentMouseStates[i].RightButton == ButtonState.Pressed;
+					case MouseButton.Middle:
+						return CurrentMouseStates[i].MiddleButton == ButtonState.Pressed;
+					default:
+						return IsMouseDown(MouseButton.Left, controllingPlayer, out playerIndex) ||
+							   IsMouseDown(MouseButton.Middle, controllingPlayer, out playerIndex) ||
+							   IsMouseDown(MouseButton.Right, controllingPlayer, out playerIndex);
+				}
+
+			}
+			else
+			{
+				// Accept input from any player.
+				return (IsMouseDown(mouseButton, PlayerIndex.One, out playerIndex) ||
+				        IsMouseDown(mouseButton, PlayerIndex.Two, out playerIndex) ||
+				        IsMouseDown(mouseButton, PlayerIndex.Three, out playerIndex) ||
+				        IsMouseDown(mouseButton, PlayerIndex.Four, out playerIndex));
+			}
+
+		}
+
+		/// <summary>
         /// Helper for checking if a button was newly pressed during this update.
         /// The controllingPlayer parameter specifies which player to read input for.
         /// If this is null, it will accept input from any player. When a button press
@@ -194,6 +289,7 @@ namespace HoneycombRush
         {
             return IsNewKeyPress(Keys.Space, controllingPlayer, out playerIndex) ||
                    IsNewKeyPress(Keys.Enter, controllingPlayer, out playerIndex) ||
+			   	   IsNewMouseClick(MouseButton.Left, controllingPlayer, out playerIndex) ||
                    IsNewButtonPress(Buttons.A, controllingPlayer, out playerIndex) ||
                    IsNewButtonPress(Buttons.Start, controllingPlayer, out playerIndex);
         }

+ 2 - 1
HoneycombRush/ScreenManager/MenuScreen.cs

@@ -109,7 +109,8 @@ namespace HoneycombRush
                     selectedEntry = 0;
             }
             else if (input.IsNewKeyPress(Keys.Enter, ControllingPlayer, out player) ||
-                input.IsNewKeyPress(Keys.Space, ControllingPlayer, out player))
+                input.IsNewKeyPress(Keys.Space, ControllingPlayer, out player) ||
+			    input.IsNewMouseClick(InputState.MouseButton.Left, ControllingPlayer, out player) )
             {
                 OnSelectEntry(selectedEntry, player);
             }

+ 11 - 4
HoneycombRush/Screens/GameplayScreen.cs

@@ -308,8 +308,10 @@ namespace HoneycombRush
             {
                 showDebugInfo = !showDebugInfo;
             }
-            if (input.IsKeyDown(Keys.Space, ControllingPlayer, out player) && !beeKeeper.IsCollectingHoney &&
-                !beeKeeper.IsStung)
+			if ( (input.IsKeyDown(Keys.Space, ControllingPlayer, out player) ||
+			      input.IsNewMouseClick(InputState.MouseButton.Right, ControllingPlayer, out player))
+			    && !beeKeeper.IsCollectingHoney 
+			    && !beeKeeper.IsStung)
             {
                 isSmokebuttonClicked = true;
             }
@@ -836,8 +838,13 @@ namespace HoneycombRush
                 {
                     vecY++;
                 }
-
-                leftThumbstick = new Vector2(vecX, vecY);
+				if (input.IsMouseDown( InputState.MouseButton.Left, ControllingPlayer, out playerIndex ) )
+				{
+					vecX = input.CurrentMouseStates[(int)playerIndex].X - beeKeeper.Bounds.X ;
+					vecY = input.CurrentMouseStates[(int)playerIndex].Y - beeKeeper.Bounds.Y;
+				}
+				leftThumbstick = new Vector2 (vecX, vecY);
+				leftThumbstick.Normalize();
             }
 
             Vector2 movementVector = leftThumbstick * 12f * ScreenManager.SpriteBatch.ScaleVector;

+ 2 - 1
HoneycombRush/Screens/HighScoreScreen.cs

@@ -156,7 +156,8 @@ namespace HoneycombRush
             PlayerIndex player;
             // Handle keyboard input
             if (input.IsNewKeyPress(Keys.Enter, ControllingPlayer, out player) ||
-                input.IsNewKeyPress(Keys.Space, ControllingPlayer, out player))
+			    input.IsNewKeyPress(Keys.Space, ControllingPlayer, out player) ||
+			    input.IsNewMouseClick(InputState.MouseButton.Left, ControllingPlayer, out player))
             {
                 Exit();
             }

+ 2 - 1
HoneycombRush/Screens/LevelOverScreen.cs

@@ -159,7 +159,8 @@ namespace HoneycombRush
             }
             // Handle keyboard
             else if (input.IsNewKeyPress(Keys.Enter, ControllingPlayer, out player) ||
-                input.IsNewKeyPress(Keys.Space, ControllingPlayer, out player))
+			         input.IsNewKeyPress(Keys.Space, ControllingPlayer, out player) ||
+			         input.IsNewMouseClick(InputState.MouseButton.Left, ControllingPlayer, out player))
             {
                 StartNewLevelOrExit(input);
             }

+ 2 - 1
HoneycombRush/Screens/LoadingAndInstructionScreen.cs

@@ -94,7 +94,8 @@ namespace HoneycombRush
                     ScreenManager.AddScreen(new MainMenuScreen(), PlayerIndex.One);
                 }
                 else if (input.IsNewKeyPress(Keys.Enter, ControllingPlayer, out player) ||
-                    input.IsNewKeyPress(Keys.Space, ControllingPlayer, out player))
+				         input.IsNewKeyPress(Keys.Space, ControllingPlayer, out player) ||
+				         input.IsNewMouseClick(InputState.MouseButton.Left, ControllingPlayer, out player))
                 {
                     LoadResources();
                 }