diff --git a/Projektgruppe_175/src/gui/components/MapPanel.java b/Projektgruppe_175/src/gui/components/MapPanel.java index 34f1a67..f1e2370 100644 --- a/Projektgruppe_175/src/gui/components/MapPanel.java +++ b/Projektgruppe_175/src/gui/components/MapPanel.java @@ -4,6 +4,7 @@ import java.awt.*; import java.awt.event.*; import java.awt.geom.Line2D; import java.awt.image.BufferedImage; +import java.util.ArrayList; import java.util.List; import javax.swing.*; @@ -30,6 +31,9 @@ public class MapPanel extends JScrollPane { ATTACKING } + private boolean isChoosingJoker = false; + + private static final int CASTLE_SIZE = 50; private static final int ICON_SIZE = 20; private final GameView gameView; @@ -112,6 +116,9 @@ public class MapPanel extends JScrollPane { public void mouseReleased(MouseEvent mouseEvent) { super.mousePressed(mouseEvent); + if(isChoosingJoker) + return; + if(getCursor().getType() == Cursor.MOVE_CURSOR) setCursor(Cursor.getDefaultCursor()); } @@ -154,6 +161,61 @@ public class MapPanel extends JScrollPane { boolean selectNew = true; Action lastAction = currentAction; + if(isChoosingJoker) { + // Verscheuchen-Joker funktionalität + Castle scared = getRegion(mousePos); + if(scared == null) + return; + if(scared.getOwner() == game.getCurrentPlayer()) { + return; + } + System.out.println("Ausgewählt: " + scared.getName()); + List> castleEdges = map.getEdges(); + List adjacentCastles = new ArrayList<>(); + for(Edge edge : castleEdges) { + if(edge.getNodeA().getValue().equals(scared)) { + if(edge.getNodeB().getValue().getOwner() != game.getCurrentPlayer() && !edge.getNodeB().getValue().equals(scared)) { + adjacentCastles.add(edge.getNodeB().getValue()); + System.out.print("added: " + edge.getNodeB().getValue().getName()); + } + } else if(edge.getNodeB().getValue().equals(scared)) { + if(edge.getNodeA().getValue().getOwner() != game.getCurrentPlayer() && !edge.getNodeA().getValue().equals(scared)) { + adjacentCastles.add(edge.getNodeA().getValue()); + System.out.print("added: " + edge.getNodeA().getValue().getName()); + } + } + } + + int troopsToDistribute = scared.getTroopCount() - 1; + System.out.println(troopsToDistribute); + scared.removeTroops(troopsToDistribute); + System.out.println(adjacentCastles.size()); + for(Castle c : adjacentCastles) { + System.out.print(c.getName() + " "); + } + + if(adjacentCastles.isEmpty()) { + gameView.revalidate(); + gameView.repaint(); + isChoosingJoker = false; + setCursor(Cursor.getDefaultCursor()); + return; + } + + Random random = new Random(); + for(int i = 0; i < troopsToDistribute; i++) { + int castleNumber = random.nextInt(adjacentCastles.size()); + System.out.print(castleNumber + " "); + adjacentCastles.get(castleNumber).addTroops(1); + } + + gameView.revalidate(); + gameView.repaint(); + isChoosingJoker = false; + setCursor(Cursor.getDefaultCursor()); + return; + } + if (selectedCastle != null && canPerformAction()) { Point castlePos = selectedCastle.getLocationOnMap(); @@ -553,6 +615,11 @@ public class MapPanel extends JScrollPane { this.selectedCastle = null; repaint(); } + + public void setIsChoosingJoker(boolean b) { + this.isChoosingJoker = b; + setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR)); + } public void reset() { currentAction = MapPanel.Action.NONE;