Commit b35cad66 authored by Quentin DRUAULT-AUBIN's avatar Quentin DRUAULT-AUBIN
Browse files

[Ship] Attack destroyed ship

parent e374b140
...@@ -447,9 +447,11 @@ public class InGameGUIController { ...@@ -447,9 +447,11 @@ public class InGameGUIController {
} }
/** /**
* Function to place a ship on the grid with its corresponding image * Function to place a ship on the grid with its corresponding image
* @param ship * @param ship : the ship to place
* @param grid : the grid to place the ship on
* @return : the imageview of the ship placed
*/ */
void putShipOnBoard(Ship ship) private ImageView putShipOnBoard(Ship ship, GridPane grid)
{ {
// We assume the ship has the right coordinates. // We assume the ship has the right coordinates.
// Moreover, this function won't update the shipboard! // Moreover, this function won't update the shipboard!
...@@ -460,26 +462,27 @@ public class InGameGUIController { ...@@ -460,26 +462,27 @@ public class InGameGUIController {
if (ship.getListCoord().get(0).getY() == ship.getListCoord().get(1).getY()) { if (ship.getListCoord().get(0).getY() == ship.getListCoord().get(1).getY()) {
// Horizontal. // Horizontal.
// Set the size. // Set the size.
shipOnTheGrid.setFitWidth(playerGrid.getWidth()/10.0 * ship.getSize()); shipOnTheGrid.setFitWidth(grid.getWidth()/10.0 * ship.getSize());
shipOnTheGrid.setFitHeight(playerGrid.getHeight()/10.0); shipOnTheGrid.setFitHeight(grid.getHeight()/10.0);
// Place on the grid. // Place on the grid.
playerGrid.add(shipOnTheGrid, Math.min(ship.getListCoord().get(0).getX(), ship.getListCoord().get(1).getX()), ship.getListCoord().get(0).getY(), ship.getSize(), 1); grid.add(shipOnTheGrid, Math.min(ship.getListCoord().get(0).getX(), ship.getListCoord().get(1).getX()), ship.getListCoord().get(0).getY(), ship.getSize(), 1);
} else { } else {
// Vertical // Vertical
// Set the size. // Set the size.
shipOnTheGrid.setFitHeight(playerGrid.getWidth()/10.0); shipOnTheGrid.setFitHeight(grid.getWidth()/10.0);
shipOnTheGrid.setFitWidth(playerGrid.getHeight()/10.0 * ship.getSize()); shipOnTheGrid.setFitWidth(grid.getHeight()/10.0 * ship.getSize());
// Rotate the image. // Rotate the image.
shipOnTheGrid.setRotate(90); shipOnTheGrid.setRotate(90);
// Place on the grid. // Place on the grid.
playerGrid.add(shipOnTheGrid, ship.getListCoord().get(0).getX(), Math.min(ship.getListCoord().get(0).getY(), ship.getListCoord().get(1).getY()), 1, ship.getSize()); grid.add(shipOnTheGrid, ship.getListCoord().get(0).getX(), Math.min(ship.getListCoord().get(0).getY(), ship.getListCoord().get(1).getY()), 1, ship.getSize());
} }
return shipOnTheGrid;
} catch (Exception ex) { } catch (Exception ex) {
Logger.getLogger(InGameGUIController.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(InGameGUIController.class.getName()).log(Level.SEVERE, null, ex);
return null;
} }
} }
/** /**
* Update the label on the ship button by adding the specified value * Update the label on the ship button by adding the specified value
...@@ -600,45 +603,49 @@ public class InGameGUIController { ...@@ -600,45 +603,49 @@ public class InGameGUIController {
/** /**
* Generic method for placing a mine on the grid just with the coordinates * Generic method for placing a mine on the grid just with the coordinates
* @param c * @param coord : where to place the mine
* @param p * @param player : who places the mine
*/ */
private void placeMine(Coordinate c, Player p) private void placeMine(Coordinate coord, Player player)
{ {
// Select the right grid which depends on the player (TODO spectateur?) // Select the right grid which depends on the player (TODO spectateur?)
GridPane grid; GridPane grid;
if(p.getLightPublicUser().getId().equals(facade.getFacadeData().getMyPublicUserProfile().getId())) { if(player.getLightPublicUser().getId().equals(facade.getFacadeData().getMyPublicUserProfile().getId())) {
grid = playerGrid; grid = playerGrid;
} else { } else {
grid = opponentGrid; grid = opponentGrid;
} }
//Select the node in the grid and disable it when we lanch the attack // Select the node in the grid and disable it when we lanch the attack
Node n = getNodeByRowColumnIndex(c.getX(), c.getY(), grid); Node hitCell = getNodeByRowColumnIndex(coord.getX(), coord.getY(), grid);
n.setDisable(true); hitCell.setDisable(true);
Pair<Integer, Ship> attack_result = facade.getFacadeData().attack(c, false); Pair<Integer, Ship> attack_result = facade.getFacadeData().attack(coord, false);
//TODO: Voir si il faut demander à data une méthode "attack" neutralisée, // TODO: Voir si il faut demander à data une méthode "attack" neutralisée,
// on a besoin de pouvoir tester si une mine placée à un endroit provoque // on a besoin de pouvoir tester si une mine placée à un endroit provoque
// une explosion sans aucun autre effet (ou dire à data de tester si le joueur // une explosion sans aucun autre effet (ou dire à data de tester si le joueur
// en local est le currentPlayer) // en local est le currentPlayer)
if (attack_result.getKey() == 1) { if (attack_result.getKey() == 1) {
// Ship Touched! // Ship Touched!
n.getStyleClass().add("inGameGUI_touched_cell"); hitCell.getStyleClass().add("inGameGUI_touched_cell");
// TODO: check if the ship is destroyed. // Check if the ship is destroyed.
if(attack_result.getValue() != null) //ship destroyed here Ship destroyedShip = attack_result.getValue();
{ if(destroyedShip != null) {
//change opacity of the ship if (grid == opponentGrid) {
Ship destroyedShip = attack_result.getValue(); // Add ship picture on the opponent grid.
ImageView img = listOfShipsOnTheGrid.get(destroyedShip); ImageView destroyedShipImage = putShipOnBoard(destroyedShip, opponentGrid);
img.setOpacity(0.5); listOfShipsOnTheGrid.put(destroyedShip, destroyedShipImage);
}
// Change the CSS class of the cells.
destroyShip(destroyedShip, opponentGrid);
} }
// Reset the number of turns passed. // Reset the number of turns passed.
nbPassedTurns = 0; nbPassedTurns = 0;
} else { } else {
// Ship missed! // Ship missed!
n.getStyleClass().add("inGameGUI_missed_cell"); hitCell.getStyleClass().add("inGameGUI_missed_cell");
} }
} }
...@@ -746,7 +753,7 @@ public class InGameGUIController { ...@@ -746,7 +753,7 @@ public class InGameGUIController {
facade.getFacadeData().leaveGame(); facade.getFacadeData().leaveGame();
} else { } else {
// Fake an attack. // Fake an attack.
placeMine(new Coordinate(-1, -1), currentPlayer); facade.getFacadeData().attack(new Coordinate(-1, -1), true);
// Increase the number of turns passed. // Increase the number of turns passed.
nbPassedTurns++; nbPassedTurns++;
...@@ -963,9 +970,9 @@ public class InGameGUIController { ...@@ -963,9 +970,9 @@ public class InGameGUIController {
ImageView shipOnTheGrid = new ImageView(shipsPictures.get(ship.getType())); ImageView shipOnTheGrid = new ImageView(shipsPictures.get(ship.getType()));
//associate ship with its image on the grid //associate ship with its image on the grid
listOfShipsOnTheGrid.put(ship, shipOnTheGrid); listOfShipsOnTheGrid.put(ship, shipOnTheGrid);
// Put the image on the board // Put the image on the board
putShipOnBoard(ship); putShipOnBoard(ship, playerGrid);
// ATTENTION! Grid size is out of control! // ATTENTION! Grid size is out of control!
// setShip didn't return any exception so the ship is correctly placed -> Update the label on the left panel // setShip didn't return any exception so the ship is correctly placed -> Update the label on the left panel
...@@ -1017,7 +1024,7 @@ public class InGameGUIController { ...@@ -1017,7 +1024,7 @@ public class InGameGUIController {
} }
} }
} }
/** /**
* Display the opponent attack on the player grid. * Display the opponent attack on the player grid.
* @param coord : attacked cell * @param coord : attacked cell
...@@ -1026,7 +1033,7 @@ public class InGameGUIController { ...@@ -1026,7 +1033,7 @@ public class InGameGUIController {
*/ */
public void displayOpponentAttack(Coordinate coord, boolean touched, Ship destroyedShip) { public void displayOpponentAttack(Coordinate coord, boolean touched, Ship destroyedShip) {
// Get the cell. // Get the cell.
Node cell = getNodeByRowColumnIndex(coord.getX(), coord.getY(), playerGrid); Node cell = getNodeByRowColumnIndex(coord.getX(), coord.getY(), playerGrid);
// The opponent has touched my ship. // The opponent has touched my ship.
if (touched) { if (touched) {
// Add the CSS class. // Add the CSS class.
...@@ -1035,17 +1042,29 @@ public class InGameGUIController { ...@@ -1035,17 +1042,29 @@ public class InGameGUIController {
// The opponent has missed. // The opponent has missed.
cell.getStyleClass().add("inGameGUI_missed_cell"); cell.getStyleClass().add("inGameGUI_missed_cell");
} }
// Ship destroyed. // Ship destroyed.
if (destroyedShip != null) { if (destroyedShip != null) {
// Change the opacity.
listOfShipsOnTheGrid.get(destroyedShip).setOpacity(0.5);
// Change the CSS class of the cells. // Change the CSS class of the cells.
for (Coordinate coordinate : destroyedShip.getListCoord()) { destroyShip(destroyedShip, playerGrid);
Node node = getNodeByRowColumnIndex(coordinate.getY(), coordinate.getX(), playerGrid); }
node.getStyleClass().removeAll("inGameGUI_touched_cell"); }
node.getStyleClass().add("inGameGUI_destroyed_cell");
} /**
* Update the CSS of the cell where the destroyed ship is.
* @param ship : the destroyed ship
* @param grid : the player or opponent grid
*/
private void destroyShip(Ship ship, GridPane grid)
{
// Change the opacity.
listOfShipsOnTheGrid.get(ship).setOpacity(0.5);
// Change the CSS class of the cells.
for (Coordinate coordinate : ship.getListCoord()) {
Node node = getNodeByRowColumnIndex(coordinate.getY(), coordinate.getX(), grid);
node.getStyleClass().removeAll("inGameGUI_touched_cell");
node.getStyleClass().add("inGameGUI_destroyed_cell");
} }
} }
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment