Browse Source

add remove all Controllers by destroy

main^2
fdai7736 11 months ago
parent
commit
6d417a6a57
  1. 11
      src/main/java/de/hsfulda/onses/controllers/AppController.java
  2. 29
      src/main/java/de/hsfulda/onses/controllers/GameController.java
  3. 11
      src/main/java/de/hsfulda/onses/controllers/PlayerController.java

11
src/main/java/de/hsfulda/onses/controllers/AppController.java

@ -8,6 +8,7 @@ import javafx.scene.Parent;
import javafx.scene.control.Button; import javafx.scene.control.Button;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.Objects; import java.util.Objects;
public class AppController implements Controller { public class AppController implements Controller {
@ -15,13 +16,16 @@ public class AppController implements Controller {
private final GameService gameService; private final GameService gameService;
private final App app; private final App app;
private final ArrayList<Controller> controllers = new ArrayList<>();
public AppController(App app, GameService gameService) { public AppController(App app, GameService gameService) {
this.app = app; this.app = app;
this.gameService = gameService; this.gameService = gameService;
} }
@Override @Override
public Parent render() throws IOException { 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"))); final Parent parent = FXMLLoader.load(Objects.requireNonNull(Main.class.getResource("views/app.fxml")));
Button button = (Button) parent.lookup("#startGameBtn"); Button button = (Button) parent.lookup("#startGameBtn");
@ -38,6 +42,11 @@ public class AppController implements Controller {
@Override @Override
public void destroy() { public void destroy() {
int i = 0;
while(i < controllers.size()) {
Controller controller = controllers.get(i++);
controller.destroy();
}
} }
} }

29
src/main/java/de/hsfulda/onses/controllers/GameController.java

@ -1,5 +1,6 @@
package de.hsfulda.onses.controllers; package de.hsfulda.onses.controllers;
import de.hsfulda.onses.App;
import de.hsfulda.onses.Main; import de.hsfulda.onses.Main;
import de.hsfulda.onses.models.Card; import de.hsfulda.onses.models.Card;
import de.hsfulda.onses.models.Game; import de.hsfulda.onses.models.Game;
@ -9,13 +10,21 @@ import javafx.scene.Parent;
import javafx.scene.control.Button; import javafx.scene.control.Button;
import javafx.scene.layout.Pane; import javafx.scene.layout.Pane;
import java.beans.PropertyChangeEvent;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.Objects; import java.util.Objects;
public class GameController implements Controller { public class GameController implements Controller {
private final GameService gameService; private final GameService gameService;
private final Game game; private final Game game;
public GameController(GameService gameService) {
private final App app;
private PropertyChangeEvent lastPlayedCardPropertyChangeEvent;
private final ArrayList<Controller> controllers = new ArrayList<>();
public GameController(App app, GameService gameService) {
this.app = app;
this.gameService = gameService; this.gameService = gameService;
this.game = gameService.getGame(); this.game = gameService.getGame();
} }
@ -27,20 +36,31 @@ public class GameController implements Controller {
final Pane playerPane = (Pane) parent.lookup("#playerPane"); final Pane playerPane = (Pane) parent.lookup("#playerPane");
final Button playButton = (Button) parent.lookup("#playCardBtn"); final Button playButton = (Button) parent.lookup("#playCardBtn");
final Button drawCardButton = (Button) parent.lookup("#drawCardBtn"); final Button drawCardButton = (Button) parent.lookup("#drawCardBtn");
final Button exitGameButton = (Button) parent.lookup("#exitBtn");
CardController lastPlayedCardController = new CardController(game.getLastPlayedCard(), null); CardController lastPlayedCardController = new CardController(game.getLastPlayedCard(), null);
PlayerController playerController = new PlayerController(gameService.getGame().getPlayerService().getPlayerList().getFirst()); PlayerController playerController = new PlayerController(gameService.getGame().getPlayerService().getPlayerList().getFirst());
PlayerController enemyController = new PlayerController(gameService.getGame().getPlayerService().getPlayerList().getLast()); 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 -> { game.listeners().addPropertyChangeListener(Game.PROPERTY_LAST_PLAYED_CARD, e -> {
lastPlayedCardPane.getChildren().removeAll(); lastPlayedCardPane.getChildren().removeAll();
try { 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) { } catch (IOException ex) {
throw new RuntimeException(ex); throw new RuntimeException(ex);
} }
}); });
exitGameButton.setOnAction(e -> {
app.show(new AppController(app, new GameService()));
});
playButton.setOnAction(e -> { playButton.setOnAction(e -> {
Card cardToPlay = game.getPlayerService().getPlayerList().getFirst().getCurrentCard(); Card cardToPlay = game.getPlayerService().getPlayerList().getFirst().getCurrentCard();
if(gameService.legalMove(cardToPlay)) { if(gameService.legalMove(cardToPlay)) {
@ -70,6 +90,11 @@ public class GameController implements Controller {
@Override @Override
public void destroy() { public void destroy() {
int i = 0;
while(i < controllers.size()) {
Controller controller = controllers.get(i++);
controller.destroy();
}
} }
} }

11
src/main/java/de/hsfulda/onses/controllers/PlayerController.java

@ -9,11 +9,13 @@ import javafx.scene.control.Label;
import javafx.scene.layout.HBox; import javafx.scene.layout.HBox;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.Objects; import java.util.Objects;
public class PlayerController implements Controller { public class PlayerController implements Controller {
private final Player player; private final Player player;
private final ArrayList<Controller> controllers = new ArrayList<>();
public PlayerController(Player player) { public PlayerController(Player player) {
this.player = player; this.player = player;
@ -25,7 +27,9 @@ public class PlayerController implements Controller {
final HBox cards = (HBox) parent.lookup("#cardsHBox"); final HBox cards = (HBox) parent.lookup("#cardsHBox");
for(Card card : player.getPlayerDeck()) { 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 -> { player.listeners().addPropertyChangeListener(Player.PROPERTY_PLAYER_DECK, e -> {
@ -51,6 +55,11 @@ public class PlayerController implements Controller {
@Override @Override
public void destroy() { public void destroy() {
int i = 0;
while(i < controllers.size()) {
Controller controller = controllers.get(i++);
controller.destroy();
}
} }
} }
Loading…
Cancel
Save