/// <summary> /// Lets the game respond to player input. Unlike the Update method, /// this will only be called when the gameplay screen is active. /// </summary> public override void HandleInput(GameTime gameTime, InputState input) { if (input == null) throw new ArgumentNullException("input"); // Look up inputs for the active player profile. int playerIndex = 0; KeyboardState keyboardState = input.CurrentKeyboardStates[playerIndex]; GamePadState gamePadState = input.CurrentGamePadStates[playerIndex]; PlayerIndex player; if (input.IsPauseGame(ControllingPlayer)) { PauseBackgroundScreen pauseBG = new PauseBackgroundScreen(); ScreenManager.AddScreen(pauseBG, ControllingPlayer); ScreenManager.AddScreen(new PauseMenuScreen(pauseBG), ControllingPlayer); } if(IsActive) { if ((keyboardState.IsKeyDown(Keys.Tab) && !lastKeyboardState.IsKeyDown(Keys.Tab)) || gamePadState.IsButtonDown(Buttons.Back)) ScreenManager.AddScreen(new MapScreen(gameMap, mapFog, gameHero, mapIcons), null); //if (keyboardState.IsKeyDown(Keys.Space) && !lastKeyboardState.IsKeyDown(Keys.Space)) ThreadPool.QueueUserWorkItem(new WaitCallback(GenerateTerrainAsync)); if (keyboardState.IsKeyDown(Keys.D1) && !lastKeyboardState.IsKeyDown(Keys.D1)) gameHero.SelectWeapon(0, false); if (keyboardState.IsKeyDown(Keys.D2) && !lastKeyboardState.IsKeyDown(Keys.D2)) gameHero.SelectWeapon(1, false); if (keyboardState.IsKeyDown(Keys.D3) && !lastKeyboardState.IsKeyDown(Keys.D3)) gameHero.SelectWeapon(2, false); if (keyboardState.IsKeyDown(Keys.D4) && !lastKeyboardState.IsKeyDown(Keys.D4)) gameHero.SelectWeapon(3, false); if (keyboardState.IsKeyDown(Keys.D5) && !lastKeyboardState.IsKeyDown(Keys.D5)) gameHero.SelectWeapon(4, false); if (input.IsNewButtonPress(Buttons.Y, null, out player)) gameHero.SelectWeapon(1, true); if (input.IsNewButtonPress(Buttons.RightShoulder, null, out player) || input.IsNewButtonPress(Buttons.DPadDown, null, out player)) gameHero.SelectWeapon(1, true); if (input.IsNewButtonPress(Buttons.LeftShoulder, null, out player) || input.IsNewButtonPress(Buttons.DPadUp, null, out player)) gameHero.SelectWeapon(-1, true); if ((keyboardState.IsKeyDown(Keys.E) && !lastKeyboardState.IsKeyDown(Keys.E)) || input.IsNewButtonPress(Buttons.A, null, out player)) { gameHero.EnterVehicle(gameMap); } if (input.MouseDragging) { gameCamera.Target -= (input.MouseDelta/gameCamera.Zoom); } //if (input.CurrentMouseState.ScrollWheelValue < input.LastMouseState.ScrollWheelValue) gameCamera.Zoom -= (0.1f*gameCamera.Zoom); //if (input.CurrentMouseState.ScrollWheelValue > input.LastMouseState.ScrollWheelValue) gameCamera.Zoom += (0.1f*gameCamera.Zoom); if (input.CurrentMouseState.ScrollWheelValue < input.LastMouseState.ScrollWheelValue) gameHero.SelectWeapon(-1,true); if (input.CurrentMouseState.ScrollWheelValue > input.LastMouseState.ScrollWheelValue) gameHero.SelectWeapon(1, true); mousePos = new Vector2(input.LastMouseState.X, input.LastMouseState.Y); mousePos = Vector2.Transform(mousePos, Matrix.Invert(gameCamera.CameraMatrix)); if (gameHero.drivingVehicle == null) { if (input.CurrentGamePadStates[0].ThumbSticks.Left.Length() > 0.2f) { gameHero.Move(new Vector2(input.CurrentGamePadStates[0].ThumbSticks.Left.X, -input.CurrentGamePadStates[0].ThumbSticks.Left.Y)); } if (input.CurrentGamePadStates[0].ThumbSticks.Right.Length() > 0.2f) { gameHero.LookAt(Helper.PointOnCircle(ref gameHero.Position, 200, Helper.V2ToAngle(new Vector2(input.CurrentGamePadStates[0].ThumbSticks.Right.X, -input.CurrentGamePadStates[0].ThumbSticks.Right.Y)))); } else { if (input.CurrentGamePadStates[0].ThumbSticks.Left.Length() > 0.2f) { gameHero.LookAt(Helper.PointOnCircle(ref gameHero.Position, 200, Helper.V2ToAngle(new Vector2(input.CurrentGamePadStates[0].ThumbSticks.Left.X, -input.CurrentGamePadStates[0].ThumbSticks.Left.Y)))); } } crosshairPos = new Vector2(input.CurrentMouseState.X, input.CurrentMouseState.Y); Vector2 keyboardStick = Vector2.Zero; if (input.CurrentKeyboardStates[0].IsKeyDown(Keys.W)) keyboardStick.Y = -1f; if (input.CurrentKeyboardStates[0].IsKeyDown(Keys.S)) keyboardStick.Y = 1f; if (input.CurrentKeyboardStates[0].IsKeyDown(Keys.A)) keyboardStick.X = -1f; if (input.CurrentKeyboardStates[0].IsKeyDown(Keys.D)) keyboardStick.X = 1f; if (keyboardStick.Length() > 0f) gameHero.Move(keyboardStick); gameHero.Attack(gameTime, gameCamera.Position + (crosshairPos - new Vector2(gameCamera.Width / 2, gameCamera.Height / 2)), input.CurrentMouseState.LeftButton == ButtonState.Pressed, gameCamera, true); gameHero.LookAt(gameCamera.Position + (crosshairPos - new Vector2(gameCamera.Width / 2, gameCamera.Height / 2)));//Helper.PointOnCircle(ref gameHero.Position, 200, Helper.V2ToAngle(((gameHero.Position - gameCamera.Position) ) + (crosshairPos- new Vector2(gameCamera.Width / 2, gameCamera.Height / 2))))); } // driving controls if (gameHero.drivingVehicle != null) { if (input.CurrentKeyboardStates[0].IsKeyDown(Keys.W)) gameHero.drivingVehicle.Accelerate(1f); if (input.CurrentKeyboardStates[0].IsKeyDown(Keys.S)) gameHero.drivingVehicle.Brake(); if (input.CurrentKeyboardStates[0].IsKeyDown(Keys.A)) gameHero.drivingVehicle.Turn(-1f); if (input.CurrentKeyboardStates[0].IsKeyDown(Keys.D)) gameHero.drivingVehicle.Turn(1f); } } lastKeyboardState = keyboardState; }
public override void HandleInput(GameTime gameTime, InputState input) { PlayerIndex pi; if(input.TapPosition.HasValue || input.IsMenuCancel(null, out pi) || input.IsNewButtonPress(Buttons.Back, null, out pi)) ExitScreen(); base.HandleInput(gameTime, input); }
public override void HandleInput(GameTime gameTime, InputState input) { PlayerIndex pi; if (input.IsMenuCancel(null, out pi) || input.IsNewKeyPress(Keys.Tab, null, out pi) || input.IsNewButtonPress(Buttons.Back, null, out pi)) this.ExitScreen(); base.HandleInput(gameTime, input); }
/// <summary> /// Responds to user input, changing the selected entry and accepting /// or cancelling the menu. /// </summary> public override void HandleInput(GameTime gameTime, InputState input) { // we cancel the current menu screen if the user presses the back button PlayerIndex player; if (input.IsNewButtonPress(Buttons.Back, ControllingPlayer, out player) || input.IsMenuCancel(null, out player)) { OnCancel(player); } if (input.IsMenuUp(null)) selectedEntry -= 1; if (input.IsMenuDown(null)) selectedEntry += 1; if (input.IsMenuSelect(null, out player)) OnSelectEntry(selectedEntry, player); if (selectedEntry < 0) selectedEntry = menuEntries.Count - 1; if (selectedEntry >= menuEntries.Count) selectedEntry = 0; // look for any taps that occurred and select any entries that were tapped foreach (GestureSample gesture in input.Gestures) { if (gesture.GestureType == GestureType.Tap) { // convert the position to a Point that we can test against a Rectangle Point tapLocation = new Point((int)gesture.Position.X, (int)gesture.Position.Y); // iterate the entries to see if any were tapped for (int i = 0; i < menuEntries.Count; i++) { MenuEntry menuEntry = menuEntries[i]; if (GetMenuEntryHitBounds(menuEntry).Contains(tapLocation)) { // select the entry. since gestures are only available on Windows Phone, // we can safely pass PlayerIndex.One to all entries since there is only // one player on Windows Phone. OnSelectEntry(i, PlayerIndex.One); } } } } if (!ScreenManager.IsPhone) { Point mouseLocation = new Point(input.CurrentMouseState.X, input.CurrentMouseState.Y); for (int i = 0; i < menuEntries.Count; i++) { MenuEntry menuEntry = menuEntries[i]; if (GetMenuEntryHitBounds(menuEntry).Contains(mouseLocation)) { selectedEntry = i; // Mouse left click? if (input.CurrentMouseState.LeftButton == ButtonState.Released && input.LastMouseState.LeftButton == ButtonState.Pressed) { // select the entry. since gestures are only available on Windows Phone, // we can safely pass PlayerIndex.One to all entries since there is only // one player on Windows Phone. OnSelectEntry(i, PlayerIndex.One); } } } } }
/// <summary> /// Allows the screen to handle user input. Unlike Update, this method /// is only called when the screen is active, and not when some other /// screen has taken the focus. /// </summary> public virtual void HandleInput(GameTime gameTime, InputState input) { }
/// <summary> /// Responds to user input, accepting or cancelling the message box. /// </summary> public override void HandleInput(GameTime gameTime, InputState input) { PlayerIndex playerIndex; // We pass in our ControllingPlayer, which may either be null (to // accept input from any player) or a specific index. If we pass a null // controlling player, the InputState helper returns to us which player // actually provided the input. We pass that through to our Accepted and // Cancelled events, so they can tell which player triggered them. if (input.IsMenuSelect(ControllingPlayer, out playerIndex)) { // Raise the accepted event, then exit the message box. if (Accepted != null) Accepted(this, new PlayerIndexEventArgs(playerIndex)); ExitScreen(); } else if (input.IsMenuCancel(ControllingPlayer, out playerIndex)) { // Raise the cancelled event, then exit the message box. if (Cancelled != null) Cancelled(this, new PlayerIndexEventArgs(playerIndex)); ExitScreen(); } foreach (GestureSample gesture in input.Gestures) { if (gesture.GestureType == GestureType.Tap) { // convert the position to a Point that we can test against a Rectangle Point tapLocation = new Point((int)gesture.Position.X, (int)gesture.Position.Y); if (okRect.Contains(tapLocation)) { if (Accepted != null) Accepted(this, new PlayerIndexEventArgs(playerIndex)); ExitScreen(); } if (cancelRect.Contains(tapLocation)) { if (Cancelled != null) Cancelled(this, new PlayerIndexEventArgs(playerIndex)); ExitScreen(); } } } }