diff --git a/src/main/java/de/hsfulda/onses/controllers/AppController.java b/src/main/java/de/hsfulda/onses/controllers/AppController.java index b5de34e..03a6e0a 100644 --- a/src/main/java/de/hsfulda/onses/controllers/AppController.java +++ b/src/main/java/de/hsfulda/onses/controllers/AppController.java @@ -8,6 +8,7 @@ import javafx.scene.Parent; import javafx.scene.control.Button; import java.io.IOException; +import java.util.ArrayList; import java.util.Objects; public class AppController implements Controller { @@ -15,13 +16,16 @@ public class AppController implements Controller { private final GameService gameService; private final App app; + private final ArrayList controllers = new ArrayList<>(); + public AppController(App app, GameService gameService) { this.app = app; - this.gameService = gameService; + this.gameService = gameService; } @Override public Parent render() throws IOException { - GameController gameController = new GameController(gameService); + GameController gameController = new GameController(app, gameService); + controllers.add(gameController); final Parent parent = FXMLLoader.load(Objects.requireNonNull(Main.class.getResource("views/app.fxml"))); Button button = (Button) parent.lookup("#startGameBtn"); @@ -38,6 +42,11 @@ public class AppController implements Controller { @Override public void destroy() { + int i = 0; + while(i < controllers.size()) { + Controller controller = controllers.get(i++); + controller.destroy(); + } } } diff --git a/src/main/java/de/hsfulda/onses/controllers/GameController.java b/src/main/java/de/hsfulda/onses/controllers/GameController.java index 2f7ba5c..d173009 100644 --- a/src/main/java/de/hsfulda/onses/controllers/GameController.java +++ b/src/main/java/de/hsfulda/onses/controllers/GameController.java @@ -1,5 +1,6 @@ package de.hsfulda.onses.controllers; +import de.hsfulda.onses.App; import de.hsfulda.onses.Main; import de.hsfulda.onses.models.Card; import de.hsfulda.onses.models.Game; @@ -9,13 +10,21 @@ import javafx.scene.Parent; import javafx.scene.control.Button; import javafx.scene.layout.Pane; +import java.beans.PropertyChangeEvent; import java.io.IOException; +import java.util.ArrayList; import java.util.Objects; public class GameController implements Controller { private final GameService gameService; private final Game game; - public GameController(GameService gameService) { + private final App app; + + private PropertyChangeEvent lastPlayedCardPropertyChangeEvent; + + private final ArrayList controllers = new ArrayList<>(); + public GameController(App app, GameService gameService) { + this.app = app; this.gameService = gameService; this.game = gameService.getGame(); } @@ -27,20 +36,31 @@ public class GameController implements Controller { final Pane playerPane = (Pane) parent.lookup("#playerPane"); final Button playButton = (Button) parent.lookup("#playCardBtn"); final Button drawCardButton = (Button) parent.lookup("#drawCardBtn"); + final Button exitGameButton = (Button) parent.lookup("#exitBtn"); CardController lastPlayedCardController = new CardController(game.getLastPlayedCard(), null); PlayerController playerController = new PlayerController(gameService.getGame().getPlayerService().getPlayerList().getFirst()); PlayerController enemyController = new PlayerController(gameService.getGame().getPlayerService().getPlayerList().getLast()); + controllers.add(lastPlayedCardController); + controllers.add(playerController); + controllers.add(enemyController); + game.listeners().addPropertyChangeListener(Game.PROPERTY_LAST_PLAYED_CARD, e -> { lastPlayedCardPane.getChildren().removeAll(); try { - lastPlayedCardPane.getChildren().add(new CardController((Card) e.getNewValue(), null).render()); + CardController tmp = new CardController((Card) e.getNewValue(), null); + controllers.add(tmp); + lastPlayedCardPane.getChildren().add(tmp.render()); } catch (IOException ex) { throw new RuntimeException(ex); } }); + exitGameButton.setOnAction(e -> { + app.show(new AppController(app, new GameService())); + }); + playButton.setOnAction(e -> { Card cardToPlay = game.getPlayerService().getPlayerList().getFirst().getCurrentCard(); if(gameService.legalMove(cardToPlay)) { @@ -70,6 +90,11 @@ public class GameController implements Controller { @Override public void destroy() { + int i = 0; + while(i < controllers.size()) { + Controller controller = controllers.get(i++); + controller.destroy(); + } } } diff --git a/src/main/java/de/hsfulda/onses/controllers/PlayerController.java b/src/main/java/de/hsfulda/onses/controllers/PlayerController.java index 6742b09..57d7078 100644 --- a/src/main/java/de/hsfulda/onses/controllers/PlayerController.java +++ b/src/main/java/de/hsfulda/onses/controllers/PlayerController.java @@ -9,11 +9,13 @@ import javafx.scene.control.Label; import javafx.scene.layout.HBox; import java.io.IOException; +import java.util.ArrayList; import java.util.Objects; public class PlayerController implements Controller { private final Player player; + private final ArrayList controllers = new ArrayList<>(); public PlayerController(Player player) { this.player = player; @@ -25,7 +27,9 @@ public class PlayerController implements Controller { final HBox cards = (HBox) parent.lookup("#cardsHBox"); for(Card card : player.getPlayerDeck()) { - cards.getChildren().add(new CardController(card, player).render()); + CardController newCardController = new CardController(card, player); + controllers.add(newCardController); + cards.getChildren().add(newCardController.render()); } player.listeners().addPropertyChangeListener(Player.PROPERTY_PLAYER_DECK, e -> { @@ -51,6 +55,11 @@ public class PlayerController implements Controller { @Override public void destroy() { + int i = 0; + while(i < controllers.size()) { + Controller controller = controllers.get(i++); + controller.destroy(); + } } }