public void HandleInput(InputState input, Sprite selectedSprite) { MouseState mouse = input.CurrentMouseState; Rectangle mouseRec = new Rectangle(mouse.X, mouse.Y, 1, 1); if (mouseRec.Intersects(_miniMap)) { Vector2 mousePositionOnMiniMap = new Vector2((mouse.X - _miniMap.X)/_miniMapRatio.X, (mouse.Y - _miniMap.Y)/_miniMapRatio.Y); if (input.CurrentMouseState.LeftButton == ButtonState.Pressed) { World.Camera.Focus(mousePositionOnMiniMap); } if (input.CurrentMouseState.RightButton == ButtonState.Pressed) { var unit = selectedSprite as Unit; if (unit != null) { unit.Destination = mousePositionOnMiniMap; } } } }
private static void _HandleUnitControlSelection(InputState input, Control control, ContentManager content) { if (Selection.IsUnit) { var unit = Selection.SelectedSprite as Unit; if (control.Name == "Move"){ if (input.CurrentMouseState.Y < (Camera.ViewSize.Y) && input.IsNewLeftMouseClick()) { unit.Destination = new Vector2(Mouse.X - unit.Bounds.Width/2, Mouse.Y - unit.Bounds.Height/2); control.IsSelected = false; } } else if (control.Name == "BuildSolarPanel") { if (!_isBuildingSolarPanel) { unit = Selection.SelectedSprite as Truck; unit.IsBuilding = true; _isBuildingSolarPanel = true; _solarPanelTexture = content.Load<Texture2D>(@"Textures\Buildings\SolarPlant"); } if (_isBuildingSolarPanel && input.IsNewLeftMouseClick()) { var solarPanel = new StaticBuilding(Mouse, @"Textures\Buildings\SolarPlant", 30); solarPanel.LoadContent(content); Sprites.Add(solarPanel); ResourcesManager.Money -= Settings.SolarPanelCost; ResourcesManager.MaxEnergy += Settings.SolarPanelEnergy; control.IsSelected = false; _isBuildingSolarPanel = false; } if (input.CurrentMouseState.Y < (Camera.ViewSize.Y) && input.IsNewRightMouseClick()) { _isBuildingSolarPanel = false; unit.IsBuilding = false; control.IsSelected = false; } } else if (control.Name == "BuildWindTurbine") { unit = Selection.SelectedSprite as Truck; unit.IsBuilding = true; _isBuildingWindTurbine = true; _windTurbineTexture = content.Load<Texture2D>(@"Textures\Buildings\WindTurbineSingleSprite"); if ((Vector2.Distance(Mouse, unit.Position) <= unit.Range) && input.IsNewLeftMouseClick()) { var windTurbine = new AnimatedBuilding(Mouse, @"Textures\Buildings\WindTurbine", 30); windTurbine.LoadContent(content); Sprites.Add(windTurbine); ResourcesManager.Money -= Settings.WindTurbineCost; ResourcesManager.MaxEnergy += Settings.WindTurbineEnergy; control.IsSelected = false; _isBuildingWindTurbine = false; } if (input.CurrentMouseState.Y < (Camera.ViewSize.Y) && input.IsNewRightMouseClick()) { unit.IsBuilding = false; _isBuildingWindTurbine = false; control.IsSelected = false; } } else if (!control.Name.ToLower().Contains("load")) { unit.HandleEnergyUpgrade(ResourcesManager.Money); control.IsSelected = false; } } }
private static void _HandleResourceMechanism(InputState input) { if (Selection.IsUnit) { var unit = Selection.SelectedSprite as Unit; ResourceBuilding resourceBuilding = (from sprite in Sprites where sprite is ResourceBuilding && Vector2.Distance(unit.Position, sprite.Position) < sprite.Range select sprite as ResourceBuilding).FirstOrDefault(); unit.HandleResource(input, resourceBuilding); /*if (unit.Controls.Selection.Name.Contains("Load")) { EvaFrontier.load.Play(); } else if (unit.Controls.Selection.Name.Contains("Unload")) { EvaFrontier.unload.Play(); }*/ } }
public static void HandleInput(InputState input, ContentManager content) { _screenMouse = new Vector2(input.CurrentMouseState.X, input.CurrentMouseState.Y); _gridTexture = content.Load<Texture2D>(@"Maps\Grid"); HUD.HandleInput(input, Selection.SelectedSprite); if (Selection.IsNull) { foreach (var sprite in Sprites) { if (sprite.Intersects(Mouse) && input.IsNewLeftMouseClick() && !Selection.IsUnit) { Selection.Set(sprite); break; } } } else { if (Selection.HasControls) { Control control = Selection.SelectedSprite.Controls.Selection; _HandleResourceMechanism(input); if (control == null) { if (input.CurrentMouseState.Y < (Camera.ViewSize.Y)) { if (input.IsNewLeftMouseClick()) { Selection.UnSet(); } else if (Selection.IsUnit) { var unit = Selection.SelectedSprite as Unit; unit.HandleWayPoint(input, Mouse); } } else { Selection.SelectedSprite.Controls.HandleInput(input); } } else { // some control is selected if (Selection.IsUnit) { _HandleUnitControlSelection(input, control, content); } else if (Selection.SelectedSprite is HeadQuarter) { if (control.Name.Contains("Deploy")) { _HandleDeployCommandAtHeadQuarter(control, content); } else { var hq = Selection.SelectedSprite as HeadQuarter; hq.HandleBuildCommand(control, ResourcesManager); } } else if (Selection.SelectedSprite is Village) { var village = Selection.SelectedSprite as Village; if (control.Name == "BuildFarm") { village.HandleBuildFarm(control, ResourcesManager); } } } } else { // sprite with no control if (input.CurrentMouseState.Y < (Camera.ViewSize.Y)) { if (input.IsNewLeftMouseClick()) { Selection.UnSet(); } } } } }
/// <summary> /// Responds to user input, changing the selected entry and accepting /// or cancelling the menu. /// </summary> public override void HandleInput(InputState input) { // Move to the previous menu entry? if (input.IsMenuUp(ControllingPlayer)) { selectedEntry--; if (selectedEntry < 0) selectedEntry = menuEntries.Count - 1; EvaFrontier.buttonOver.Play(); } // Move to the next menu entry? if (input.IsMenuDown(ControllingPlayer)) { selectedEntry++; if (selectedEntry >= menuEntries.Count) selectedEntry = 0; EvaFrontier.buttonOver.Play(); } // Accept or cancel the menu? 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 // OnSelectEntry and OnCancel, so they can tell which player triggered them. PlayerIndex playerIndex; if (input.IsMenuSelect(ControllingPlayer, out playerIndex) || input.IsNewLeftMouseClick()) { OnSelectEntry(selectedEntry, playerIndex); } else if (input.IsMenuCancel(ControllingPlayer, out playerIndex)) { OnCancel(playerIndex); } }
private void _HandleMouse(InputState input, PlayerIndex? ControllingPlayer) { Boolean isMouseMoved = (_mouse != _pMouse); if (isMouseMoved) { _mouseRec = new Rectangle(_mouse.X, _mouse.Y, 1, 1); World.Mouse = World.ScreenToWorldPosition(new Vector2(_mouse.X, _mouse.Y)); } if (_mouse.LeftButton == ButtonState.Pressed) { if (rMouse.X == -1) { rMouse.X = _mouse.X; rMouse.Y = _mouse.Y; } if (_pMouse.LeftButton == ButtonState.Released && _mouse.Y <= World.Camera.ViewArea.Height) { World.Selection.Box = new Rectangle(_mouse.X, _mouse.Y, 0, 0); } if (rMouse.X != -1) if (World.Selection.Box.HasValue) { if (_mouse.Y <= World.Camera.ViewArea.Height) { //_selection.Box = new Rectangle(_selection.Box.Value.X, _selection.Box.Value.Y, // _mouse.X - _selection.Box.Value.X, _mouse.Y - _selection.Box.Value.Y); int currentX = (int)rMouse.X; int currentY = (int)rMouse.Y; int newX = _mouse.X; int newY = _mouse.Y; int width = Math.Abs(currentX - newX); int height = Math.Abs(currentY - newY); World.Selection.Box = new Rectangle(Math.Min(currentX, newX), Math.Min(currentY, newY), width, height); //} else { // _selection.Box = new Rectangle(_selection.Box.Value.X, _selection.Box.Value.Y, // _mouse.X - _selection.Box.Value.X, World.Camera.ViewArea.Height - _selection.Box.Value.Y); //World.Camera.Focus(new Vector2(World.Camera.Position.X, World.Camera.Position.Y + (_mouse.Y - World.Camera.ViewArea.Height))); } } } if (_mouse.LeftButton == ButtonState.Released) { World.Selection.Box = null; rMouse.X = -1; rMouse.Y = -1; } _pMouse = _mouse; }
/// <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(InputState input) { if (input == null) throw new ArgumentNullException("input"); int playerIndex = (int)ControllingPlayer.Value; KeyboardState keyboardState = input.CurrentKeyboardStates[playerIndex]; GamePadState gamePadState = input.CurrentGamePadStates[playerIndex]; _mouse = input.CurrentMouseState; _pMouse = input.PreviousMouseState; // The game pauses either if the user presses the pause button, or if // they unplug the active gamepad. This requires us to keep track of // whether a gamepad was ever plugged in, because we don't want to pause // on PC if they are playing with a keyboard and have no gamepad at all! bool gamePadDisconnected = !gamePadState.IsConnected && input.GamePadWasConnected[playerIndex]; if (input.IsPauseGame(ControllingPlayer) || gamePadDisconnected) { ScreenManager.AddScreen(new PauseMenuScreen(), ControllingPlayer); } else if (DidLose) { ScreenManager.AddScreen(new GameOverScreen(), ControllingPlayer); } else if (DidWin) { ScreenManager.AddScreen(new GameWonScreen(), ControllingPlayer); } else { if (keyboardState.IsKeyDown(Keys.H)) { World.Camera.Focus(World.HeadQuarter.Position); } World.Camera.HandleInput(input, ControllingPlayer); World.HandleInput(input, _content); _HandleMouse(input, ControllingPlayer); } }
/// <summary> /// Responds to user input, accepting or cancelling the message box. /// </summary> public override void HandleInput(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(); } }
public void HandleInput(InputState input, PlayerIndex? controllingPlayer) { int playerIndex = (int)controllingPlayer; if (input.CurrentKeyboardStates[playerIndex].IsKeyDown(Keys.S) || input.CurrentKeyboardStates[playerIndex].IsKeyDown(Keys.Down)) _Move(new Vector2(0, _scrollValue)); else if (input.CurrentKeyboardStates[playerIndex].IsKeyDown(Keys.W) || input.CurrentKeyboardStates[playerIndex].IsKeyDown(Keys.Up)) _Move(new Vector2(0, -_scrollValue)); if (input.CurrentKeyboardStates[playerIndex].IsKeyDown(Keys.D) || input.CurrentKeyboardStates[playerIndex].IsKeyDown(Keys.Right)) _Move(new Vector2(_scrollValue, 0)); else if (input.CurrentKeyboardStates[playerIndex].IsKeyDown(Keys.A) || input.CurrentKeyboardStates[playerIndex].IsKeyDown(Keys.Left)) _Move(new Vector2(-_scrollValue, 0)); float zoomValueFromMouseWheel = input.CurrentMouseState.ScrollWheelValue - input.PreviousMouseState.ScrollWheelValue; if (zoomValueFromMouseWheel >= 10) { _ZoomIn(_zoomValue * 5); } else if (zoomValueFromMouseWheel <= -10) { _ZoomOut(_zoomValue * 5); } if (input.CurrentKeyboardStates[playerIndex].IsKeyDown(Keys.OemPlus)) _ZoomIn(_zoomValue); else if (input.CurrentKeyboardStates[playerIndex].IsKeyDown(Keys.OemMinus)) _ZoomOut(_zoomValue); else if (input.CurrentKeyboardStates[playerIndex].IsKeyDown(Keys.Back)) _ZoomOriginal(); }