From 78fed5e20ef184e82ec5f8a6d8f2c7494b8ca515 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Sat, 15 Jan 2022 14:18:50 +0100 Subject: [PATCH] Implemented first move 'animation' for GUI --- .../de/tims/fleetstorm/gui/GameLogic.java | 43 ++++++++++++++++--- 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/gui/GameLogic.java b/src/main/java/de/tims/fleetstorm/gui/GameLogic.java index 0d6d14f..f8ba163 100644 --- a/src/main/java/de/tims/fleetstorm/gui/GameLogic.java +++ b/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() {