Browse Source

Implemented first move 'animation' for GUI

Lorenz Hohmann 3 years ago
parent
commit
78fed5e20e
  1. 43
      src/main/java/de/tims/fleetstorm/gui/GameLogic.java

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

@ -23,7 +23,7 @@ public class GameLogic extends JPanel {
private Matchfield matchfield;
private Matchfield enemyMatchfield;
private int matchfieldSize = 10;
private boolean playerMove = true;
private boolean playerMove;
private Logic aiLogic;
public static final int PREPARATION = 1;
@ -248,18 +248,32 @@ public class GameLogic extends JPanel {
return enemy5Ship;
}
private void updateField(boolean player, Matchfield matchfield) {
private void updateField(boolean playerMove, Matchfield matchfield) {
for (JPanel coordinatePanel : this.fields) {
String panelName = coordinatePanel.getName();
String[] panelNameSplit = panelName.split(":");
int coordX = Integer.parseInt(panelNameSplit[0]);
int coordY = Integer.parseInt(panelNameSplit[1]);
matchfield.getField(coordX, coordY).print();
// reset field
coordinatePanel.setBackground(null);
coordinatePanel.removeAll();
int state = matchfield.getField(coordX, coordY).getState();
if (state == Coordinate.SHIP)
coordinatePanel.setBackground(Color.GREEN);
switch (state) {
case Coordinate.SHIP:
if (playerMove)
coordinatePanel.setBackground(new Color(91, 58, 41));
break;
case Coordinate.SHOT:
coordinatePanel.setBackground(new Color(0, 94, 184));
break;
case Coordinate.HIT:
coordinatePanel.setBackground(new Color(0, 94, 184));
coordinatePanel.add(new JLabel("x"));
break;
}
coordinatePanel.revalidate();
coordinatePanel.repaint();
@ -283,6 +297,7 @@ public class GameLogic extends JPanel {
}
public void start() {
this.playerMove = true;
this.gameState = GameLogic.PREPARATION;
// create player matchfield
@ -304,11 +319,27 @@ public class GameLogic extends JPanel {
this.aiLogic.setMatchfield(this.matchfield);
// show player own matchfield
gui.updateField(true, this.matchfield);
gui.updateField(this.playerMove, this.matchfield);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// enter game loop
this.nextMove();
}
public void nextMove() {
this.playerMove = !this.playerMove;
if (this.playerMove) {
gui.updateField(this.playerMove, this.enemyMatchfield);
} else {
gui.updateField(this.playerMove, this.matchfield);
}
}
public int getGameState() {

Loading…
Cancel
Save