private void giveCommand(MouseState input, View gameView, Controller testGameController) { 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 testGameController.scenario.getPlayer().SelectedEntities) { switch (currentPlayerCommand) { // Move command case PlayerCommand.MOVE: testGameController.giveActionCommand(entity, new ZRTSLogic.Action.MoveAction(commandX, commandY, testGameController.gameWorld, entity)); break; // Cancel command case PlayerCommand.CANCEL: // TODO: Cancel current action break; // Attack command case PlayerCommand.ATTACK: if (entity.entityType == ZRTSModel.Entities.Entity.EntityType.Unit) { ZRTSModel.Entities.Entity temp = testGameController.scenario.getUnit((int)commandX, (int)commandY); if (temp != null) { System.Console.Out.WriteLine("Selected Attack Unit at " + commandX + ":" + commandY); testGameController.giveActionCommand(entity, new ZRTSLogic.Action.SimpleAttackAction((ZRTSModel.Entities.Unit)entity, temp)); } } break; // Build command case PlayerCommand.BUILD: if (testGameController.makeUnitBuild(entity, new ZRTSModel.Entities.Building(testGameController.scenario.getPlayer(), new ZRTSModel.Entities.BuildingStats()), testGameController.gameWorld.map.getCell((int)commandX, (int)commandY))) { System.Console.Out.WriteLine("Building at " + commandX + ":" + commandY); } else { System.Console.Out.WriteLine("Can't place a building at " + commandX + ":" + commandY); } break; } } } }
/// <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> /// Display debug information /// </summary> private void DrawDebugScreen() { //spriteBatch.DrawString(Font1, "Clicked at game Location : " + commandX + "," + commandY, new Vector2(500, 0), Color.Black); spriteBatch.DrawString(Font1, "Coverted game Location : " + gameView.convertScreenLocToGameLoc(input.X, input.Y).X + "," + gameView.convertScreenLocToGameLoc(input.X, input.Y).Y, new Vector2(500, 100), Color.Black); spriteBatch.DrawString(Font1, "Mouse Location : " + input.X + "," + input.Y, new Vector2(500, 150), Color.Black); spriteBatch.DrawString(Font1, "Unit Location : " + this.testScenario.getGameWorld().getUnits()[0].x + "," + this.testScenario.getGameWorld().getUnits()[0].y, new Vector2(500, 250), Color.Black); }
/// <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 (isWithInBound(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 (isWithInBound(selectX, selectY)) { selectX = gameView.convertScreenLocToGameLoc(input.X, input.Y).X; selectY = gameView.convertScreenLocToGameLoc(input.X, input.Y).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 (isWithInBound(releaseX, releaseY) && isWithInBound(pressX, pressY)) { List <ZRTSModel.Entities.Entity> entityList = 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)) ); 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) + ")"); if (entityList.Count != 0) { this.testGameController.scenario.getPlayer().selectEntities(entityList); } } } this.testGameController.updateWorld(); prevInput = this.input; base.Update(gameTime); }
/// <summary> /// Give a command when click at icons /// </summary> /// <param name="input"></param> /// <param name="gameView"></param> /// <param name="testGameController"></param> private void giveCommand(MouseState input, View gameView, Controller testGameController) { 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 testGameController.scenario.getPlayer().SelectedEntities) { ZRTSModel.GameWorld.Cell cell = testGameController.gameWorld.map.getCell((int)commandX, (int)commandY); if (currentPlayerCommand == PlayerCommand.BUILD) { ZRTSModel.Entities.Building building = new ZRTSModel.Entities.Building(testGameController.scenario.getPlayer(), new ZRTSModel.Entities.BuildingStats()); if (testGameController.makeUnitBuild(entity,building, cell)) { System.Console.Out.WriteLine("Building at " + commandX + ":" + commandY); } else { System.Console.Out.WriteLine("Can't place a building at " + commandX + ":" + commandY); } } else { if (cell.entity != null) { // TODO: Check that the Entity belongs to an enemy. If not, Move to the entity instead, // TODO: Check if a Resource was clicked, should be harvested or ignored instead. // TODO: Check if Entity is a frienfly building, if so and the unit can build buildings, have unit repair // the building instead. if (entity.entityType == ZRTSModel.Entities.Entity.EntityType.Unit) { // Right-Clicked on an Entity, Attack the Entity. System.Console.Out.WriteLine("Selected Attack Entity at " + commandX + ":" + commandY); testGameController.giveActionCommand(entity, new ZRTSLogic.Action.SimpleAttackAction((ZRTSModel.Entities.Unit)entity, cell.entity)); } } else if (cell.getUnit() != null) { // Right-Clicked on a Unit, attack the Unit. // TODO: Check that the enemy is an entity. if (entity.entityType == ZRTSModel.Entities.Entity.EntityType.Unit) { System.Console.Out.WriteLine("Selected Attack Unit at " + commandX + ":" + commandY); ZRTSLogic.Action.AttackAction attackAction = new ZRTSLogic.Action.AttackAction((ZRTSModel.Entities.Unit)entity, cell.getUnit(), testGameController.gameWorld); testGameController.giveActionCommand(entity, attackAction); //testGameController.giveActionCommand(entity, //new ZRTSLogic.Action.SimpleAttackAction((ZRTSModel.Entities.Unit)entity, cell.getUnit())); } } else { // Clicked Cell is empty, have the Unit move to the location. testGameController.giveActionCommand(entity, new ZRTSLogic.Action.MoveAction(commandX, commandY, testGameController.gameWorld, entity)); } } } } }
/// <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); }
/// <summary> /// Give a command when click at icons /// </summary> /// <param name="input"></param> /// <param name="gameView"></param> /// <param name="testGameController"></param> private void giveCommand(MouseState input, View gameView, Controller testGameController) { 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 testGameController.scenario.getPlayer().SelectedEntities) { ZRTSModel.GameWorld.Cell cell = testGameController.gameWorld.map.getCell((int)commandX, (int)commandY); if (currentPlayerCommand == PlayerCommand.BUILD) { ZRTSModel.Entities.Building building = new ZRTSModel.Entities.Building(testGameController.scenario.getPlayer(), new ZRTSModel.Entities.BuildingStats()); if (testGameController.makeUnitBuild(entity, building, cell)) { System.Console.Out.WriteLine("Building at " + commandX + ":" + commandY); } else { System.Console.Out.WriteLine("Can't place a building at " + commandX + ":" + commandY); } } else { if (cell.entity != null) { // TODO: Check that the Entity belongs to an enemy. If not, Move to the entity instead, // TODO: Check if a Resource was clicked, should be harvested or ignored instead. // TODO: Check if Entity is a frienfly building, if so and the unit can build buildings, have unit repair // the building instead. if (entity.entityType == ZRTSModel.Entities.Entity.EntityType.Unit) { // Right-Clicked on an Entity, Attack the Entity. System.Console.Out.WriteLine("Selected Attack Entity at " + commandX + ":" + commandY); testGameController.giveActionCommand(entity, new ZRTSLogic.Action.SimpleAttackAction((ZRTSModel.Entities.Unit)entity, cell.entity)); } } else if (cell.getUnit() != null) { // Right-Clicked on a Unit, attack the Unit. // TODO: Check that the enemy is an entity. if (entity.entityType == ZRTSModel.Entities.Entity.EntityType.Unit) { System.Console.Out.WriteLine("Selected Attack Unit at " + commandX + ":" + commandY); ZRTSLogic.Action.AttackAction attackAction = new ZRTSLogic.Action.AttackAction((ZRTSModel.Entities.Unit)entity, cell.getUnit(), testGameController.gameWorld); testGameController.giveActionCommand(entity, attackAction); //testGameController.giveActionCommand(entity, //new ZRTSLogic.Action.SimpleAttackAction((ZRTSModel.Entities.Unit)entity, cell.getUnit())); } } else { // Clicked Cell is empty, have the Unit move to the location. testGameController.giveActionCommand(entity, new ZRTSLogic.Action.MoveAction(commandX, commandY, testGameController.gameWorld, entity)); } } } } }