//Returns a list of tiles containing a valid target in range of the given ability //That list is stored on the ability object and is cleared with each new (non-recursive) call to this method public List<Tile> checkRange(Tile origin, Ability ability, int range, Piece user) { //Clears the list if this is the initial call because the parameter "range" will only equal ability.range if this is the first call if (ability.getRange() == range) { validTargets = new List<Tile>(); } List<Tile> frontier = getNeighbors(origin, new List<Tile>()); if (!origin.isEmpty()) { //If ability targets enemies & the tile contains a unit from the other faction & if the tile is not already in the list THEN the tile is added if (ability.getTargetType().Equals("Enemy") && !(origin.getResident().getFaction().Equals(user.getFaction())) && !validTargets.Contains(origin)) { validTargets.Add(origin); } //If ability targets friendlies & the tile contains a unit from the same faction & if the tile is not already in the list THEN the tile is added else if (ability.getTargetType().Equals("Friendly") && (origin.getResident().getFaction().Equals(user.getFaction())) && !validTargets.Contains(origin)) { validTargets.Add(origin); } } //decrement range for the next round of checks int nRange = range - 1; if (nRange >= 0) { for (int i = 0; i < frontier.Count(); i++) { checkRange(frontier[i], ability, nRange, user); } } return validTargets; }
public void update() //gathers input, updates positions, and handles combat { //update inputs oldMouse = mouseState; mouseState = Mouse.GetState(); oldKeyboardState = keyboardState; keyboardState = Keyboard.GetState(); Rectangle endTurn = new Rectangle(918, 818, 268, 69); Rectangle toggleGrid = new Rectangle(1076, 70, 115, 20); Rectangle mouse = new Rectangle(mouseState.X, mouseState.Y, 1, 1); //TODO find unspent unit //click toggle grid button if (mouse.Intersects(toggleGrid) && mouseState.LeftButton == ButtonState.Pressed && oldMouse.LeftButton != ButtonState.Pressed) gridOn = !gridOn; if (controlState == ControlState.selectUnit) { //change scroll-over display based on mouse coordinates scrolledOver = mouseOverTile(); //if click and scrolled over friendly unit set unit to selected and change state to choose option if (mouseState.LeftButton == ButtonState.Pressed) { if (mouse.Intersects(endTurn) && oldMouse.LeftButton != ButtonState.Pressed) { //if end turn is clicked then refresh armies and switch players controlState = ControlState.endTurn; } else { if (mouseState.X < 900 && mouseState.Y < 900 && scrolledOver != null && !scrolledOver.isEmpty()) { if((scrolledOver.getResident().getFaction().Equals("RED") && isPlayer1Turn) || (scrolledOver.getResident().getFaction().Equals("BLUE") && !isPlayer1Turn)) { if (!scrolledOver.getResident().hasPieceAttacked()) { selectedTile = scrolledOver; selectedUnit = scrolledOver.getResident(); selectionTiles = map.getValidMoves(); controlState = ControlState.chooseOption; //hide mouse mouseVisible = false; } else status = "Unit is spent for this turn"; } } } } if (keyboardState.IsKeyDown(Keys.K)) //test win screen { status = "Nobody wins! YAY!"; controlState = ControlState.endGame; } } else if (controlState == ControlState.chooseOption) { //if keyboardState has a button pressed coressponding to the options then change state and show mouse TODO if (keyboardState.IsKeyDown(Keys.W) && !selectedUnit.hasPieceMoved()) //move { mouseVisible = true; selectionTiles = map.findValidMoves(selectedUnit); controlState = ControlState.move; } else if (keyboardState.IsKeyDown(Keys.A) && !selectedUnit.hasPieceAttacked()) //attack { mouseVisible = true; currentAbility = new Attack(selectedUnit); selectionTiles = map.checkRange(selectedTile, currentAbility, currentAbility.getRange(), selectedUnit); //if no targets in range status "says so" and don't change state controlState = ControlState.selectTarget; } else if (keyboardState.IsKeyDown(Keys.T)) //end turn { //selectedUnit.move(); selectedUnit.attack(); //TODO mouseVisible = true; controlState = ControlState.selectUnit; } else if (keyboardState.IsKeyDown(Keys.Space) && !oldKeyboardState.IsKeyDown(Keys.Space)) { mouseVisible = true; controlState = ControlState.selectUnit; } //space = cancel, go back to selectUnit } else if (controlState == ControlState.move) { //change scroll-over display based on mouse coordinates scrolledOver = mouseOverTile(); //if click and mouse XY corresponds to valid tile then move unit and return to chooseoption TODO if (selectionTiles.Contains(mouseOverTile()) && mouseState.LeftButton == ButtonState.Pressed) { map.movePiece(selectedTile, scrolledOver, selectedUnit); selectedTile = scrolledOver; selectedUnit.move(); status = selectedUnit.getName() + " has moved."; mouseVisible = false; controlState = ControlState.chooseOption; } else if (keyboardState.IsKeyDown(Keys.Space) && !oldKeyboardState.IsKeyDown(Keys.Space)) { mouseVisible = false; controlState = ControlState.chooseOption; } //space = cancel, go back to chooseOption } else if (controlState == ControlState.selectTarget) { //change scroll-over display based on mouse coordinates scrolledOver = mouseOverTile(); //if click and tile is in target list than initiate combat between selected unit and target, show mouse, return to select unit TODO if (mouseState.LeftButton == ButtonState.Pressed) { //if scrolled over tile is within targetTiles then initiate combat if(selectionTiles.Contains(scrolledOver)) { //change status to combat result string status = currentAbility.execute(selectedUnit, scrolledOver.getResident()); selectedUnit.attack(); //remove dead units if (selectedUnit.getHealth() < 1) selectedTile.removePiece(); if (scrolledOver.getResident().getHealth() < 1) scrolledOver.removePiece(); controlState = ControlState.selectUnit; //status = "attack"; } } else if (keyboardState.IsKeyDown(Keys.Space) && !oldKeyboardState.IsKeyDown(Keys.Space)) { currentAbility = null; mouseVisible = false; controlState = ControlState.chooseOption; } //space = cancel, go back to chooseOption } else if (controlState == ControlState.endTurn) { if (keyboardState.IsKeyDown(Keys.Y)) { refreshArmy(); isPlayer1Turn = !isPlayer1Turn; //refreshArmy(); status = "Turn ended, next player's turn begins"; controlState = ControlState.selectUnit; } else if (keyboardState.IsKeyDown(Keys.Space)) { controlState = ControlState.selectUnit; } } else if(controlState == ControlState.endGame) { //continue if (keyboardState.IsKeyDown(Keys.Space)) { endBattle(); } } if (isArmyDead(player1Army)) { status = "Blue Player 2 Wins!"; } else if (isArmyDead(player2Army)) { status = "Red Player 1 Wins!"; } }