Browse Source

GUI Refactoring

fleetstorm
Lorenz Hohmann 3 years ago
parent
commit
5d039543b7
  1. 42
      src/main/java/de/tims/fleetstorm/gui/GameLogic.java

42
src/main/java/de/tims/fleetstorm/gui/GameLogic.java

@ -35,6 +35,7 @@ public class GameLogic extends JPanel {
public GameLogic(int gapToFrameBorderX, int gapToFrameBorderY, int fieldWidth, int spaceBetween) {
this.playerFields = new ArrayList<>();
this.enemyFields = new ArrayList<>();
setSize(640, 480);
JPanel fieldWrapper = new JPanel();
@ -64,8 +65,9 @@ public class GameLogic extends JPanel {
private void generateFieldsForGUI(int gapToFrameBorderX, int gapToFrameBorderY, int fieldWidth, int spaceBetween,
JPanel panel, ArrayList<JPanel> fields, boolean registerMouseListener) {
for (int x = 0; x < 10; x++) {
for (int y = 0; y < 10; y++) {
for (int x = 0; x < this.matchfieldSize; x++) {
for (int y = 0; y < this.matchfieldSize; y++) {
JPanel field = new JPanel();
int xPos = gapToFrameBorderX + x * fieldWidth + (x * spaceBetween);
@ -74,17 +76,16 @@ public class GameLogic extends JPanel {
field.setBounds(xPos, yPos, fieldWidth, fieldWidth);
field.setBorder(new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 0, 0)));
field.setName(x + ":" + y);
fields.add(field);
panel.add(field);
if (registerMouseListener) {
if (!registerMouseListener)
continue;
field.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
System.out.println("mouse clicked");
if (!playerMove)
return;
int[] coords = getCoordinateFromLabel(field);
int coordX = coords[0];
int coordY = coords[1];
@ -92,7 +93,6 @@ public class GameLogic extends JPanel {
Coordinate chosenField = enemyMatchfield.getField(coordX, coordY);
boolean shotSuccess = chosenField.shoot();
gui.updateFields();
if (shotSuccess && chosenField.getState() != Coordinate.HIT) {
nextMove(false);
@ -102,19 +102,14 @@ public class GameLogic extends JPanel {
}
});
}
}
}
}
public ArrayList<JPanel> getFields() {
return playerFields;
}
private JLabel getXLabel() {
JLabel x = new JLabel("x");
x.setForeground(Color.WHITE);
return x;
}
@ -185,7 +180,7 @@ public class GameLogic extends JPanel {
this.gameState = GameLogic.PREPARATION;
this.playerMove = true;
// create player matchfield
// create player matchfield and set ships
this.matchfield = new Matchfield(matchfieldSize);
this.matchfield.createMatchfield();
this.matchfield.setShip(new Coordinate(4, 6), 2, 0);
@ -193,15 +188,17 @@ public class GameLogic extends JPanel {
this.matchfield.setShip(new Coordinate(9, 1), 4, 1);
this.matchfield.setShip(new Coordinate(0, 0), 5, 0);
// create enemy matchfield and initialize AI (with player's matchfield)
this.aiLogic = new Logic();
this.aiLogic.setMatchfield(this.matchfield);
// create enemy matchfield, set ships and initialize AI (with player's
// matchfield)
this.enemyMatchfield = new Matchfield(matchfieldSize);
this.enemyMatchfield.createMatchfield();
this.enemyMatchfield.setShip(new Coordinate(8, 9), 2, 0);
this.enemyMatchfield.setShip(new Coordinate(0, 2), 3, 1);
this.enemyMatchfield.setShip(new Coordinate(7, 0), 4, 1);
this.enemyMatchfield.setShip(new Coordinate(3, 6), 5, 0);
this.aiLogic = new Logic();
this.aiLogic.setMatchfield(this.matchfield);
// enter game loop
this.gameState = GameLogic.RUNNING;
@ -213,7 +210,7 @@ public class GameLogic extends JPanel {
if (this.playerMove) {
gui.updateFields();
// waiting for mouse click event....
// waiting here for mouse click event....
} else {
Coordinate aiChosenField = null;
@ -221,12 +218,11 @@ public class GameLogic extends JPanel {
aiChosenField = this.aiLogic.chooseField();
}
aiChosenField.print();
aiChosenField.shoot();
gui.updateFields();
boolean aiHasNextMove = aiChosenField.getState() != Coordinate.HIT;
this.nextMove(aiHasNextMove);
}
@ -235,8 +231,4 @@ public class GameLogic extends JPanel {
public int getGameState() {
return gameState;
}
public boolean isPlayerMove() {
return playerMove;
}
}
Loading…
Cancel
Save