/// <summary> /// Handle selecting units and buildings by the user /// </summary> /// <param name="input"></param> /// <param name="testGameController"></param> /// <param name="gameView"></param> /// <param name="gamePlayMenu"></param> private void handleSelecting(MouseState input, Controller testGameController, View gameView, ViewGamePlayMenu gamePlayMenu) { /* Left click to select units */ // Store the first corner of the "drag box" if (input.LeftButton == ButtonState.Pressed && prevInput.LeftButton == ButtonState.Released) { if (testGameController.isWithinGameBound(selectX, selectY)) { selectX = gameView.convertScreenLocToGameLoc(input.X, input.Y).X; selectY = gameView.convertScreenLocToGameLoc(input.X, input.Y).Y; gameView.setFirstCornerOfDragBox(input.X, input.Y); gameView.IsDragging = true; } } // While dragging, update the view to draw the box if (input.LeftButton == ButtonState.Pressed) { gameView.setDragBox(input.X, input.Y); } // "Drag box" is created, select all units within box if (input.LeftButton == ButtonState.Released && prevInput.LeftButton == ButtonState.Pressed) { float releaseX = gameView.convertScreenLocToGameLoc(input.X, input.Y).X; // coords of release location float releaseY = gameView.convertScreenLocToGameLoc(input.X, input.Y).Y; float pressX = selectX; // coords of press location float pressY = selectY; if (testGameController.isWithinGameBound(releaseX, releaseY) && testGameController.isWithinGameBound(pressX, pressY)) { /* * Retrieve all units within the drag box - Use Min and Max to find the topleft and * bottomright corner */ testGameController.scenario.selectUnits( (int)Math.Min(pressX, releaseX), (int)Math.Min(pressY, releaseY), (int)(Math.Max(pressX, releaseX) - Math.Min(pressX, releaseX)), (int)(Math.Max(pressY, releaseY) - Math.Min(pressY, releaseY)) ); } gameView.IsDragging = false; //gameView.resetDragBox(); } }
/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { this.input = Mouse.GetState(); // Receive input from mouse // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { this.Exit(); } // Right click to give a command if (input.RightButton == ButtonState.Pressed && prevInput.RightButton == ButtonState.Released) { commandX = gameView.convertScreenLocToGameLoc(input.X, input.Y).X; commandY = gameView.convertScreenLocToGameLoc(input.X, input.Y).Y; if (testGameController.isWithinGameBound(commandX, commandY)) { foreach (ZRTSModel.Entities.Entity entity in this.testGameController.scenario.getPlayer().SelectedEntities) { this.testGameController.giveActionCommand( entity, new ZRTSLogic.Action.MoveAction(commandX, commandY, this.testGameController.gameWorld, entity) ); } } } /* Left click to select units */ // Store the first corner of the "drag box" if (input.LeftButton == ButtonState.Pressed && prevInput.LeftButton == ButtonState.Released) { if (testGameController.isWithinGameBound(selectX, selectY)) { selectX = gameView.convertScreenLocToGameLoc(input.X, input.Y).X; selectY = gameView.convertScreenLocToGameLoc(input.X, input.Y).Y; gameView.setFirstCornerOfDragBox(input.X, input.Y); gameView.IsDragging = true; } } // While dragging, update the view to draw the box if (input.LeftButton == ButtonState.Pressed) { gameView.setDragBox(input.X, input.Y); } // "Drag box" is created, select all units within box if (input.LeftButton == ButtonState.Released && prevInput.LeftButton == ButtonState.Pressed) { float releaseX = gameView.convertScreenLocToGameLoc(input.X, input.Y).X; // coords of release location float releaseY = gameView.convertScreenLocToGameLoc(input.X, input.Y).Y; float pressX = selectX; // coords of press location float pressY = selectY; if (testGameController.isWithinGameBound(releaseX, releaseY) && testGameController.isWithinGameBound(pressX, pressY)) { /* * Retrieve all units within the drag box - Use Min and Max to find the topleft and * bottomright corner */ this.testGameController.scenario.getUnits( (int)Math.Min(pressX, releaseX), (int)Math.Min(pressY, releaseY), (int)(Math.Max(pressX, releaseX) - Math.Min(pressX, releaseX)), (int)(Math.Max(pressY, releaseY) - Math.Min(pressY, releaseY)) ); // TODO: remove later (DEBUGGING INFORMATION ONLY) Console.WriteLine("(pressX, pressY) = (" + pressX + "," + pressY + ")"); Console.WriteLine("topleft = (" + (int)Math.Min(pressX, releaseX) + "," + (int)Math.Min(pressY, releaseY) + ")"); Console.WriteLine("(releaseX, releaseY) = (" + releaseX + "," + releaseY + ")"); Console.WriteLine("bottomright = (" + (int)Math.Max(pressX, releaseX) + "," + (int)Math.Max(pressY, releaseY) + ")"); } gameView.IsDragging = false; } this.testGameController.updateWorld(); prevInput = this.input; base.Update(gameTime); }