From a39735c62712d65535583521e3ec54f5c929ccc6 Mon Sep 17 00:00:00 2001 From: fdai7736 Date: Thu, 8 Feb 2024 22:49:25 +0100 Subject: [PATCH] fix that wish card works in GUI --- .../onses/controllers/CardController.java | 13 ++++++++ .../onses/controllers/GameController.java | 33 ++++++++++++++++++- .../de/hsfulda/onses/views/game.fxml | 4 +-- 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/hsfulda/onses/controllers/CardController.java b/src/main/java/de/hsfulda/onses/controllers/CardController.java index 3d05811..f8ed28a 100644 --- a/src/main/java/de/hsfulda/onses/controllers/CardController.java +++ b/src/main/java/de/hsfulda/onses/controllers/CardController.java @@ -18,6 +18,7 @@ public class CardController implements Controller { private final Player player; private PropertyChangeListener cardSelectedChangeListener; + private PropertyChangeListener cardColorChangeListener; public CardController(Card card, Player player) { this.card = card; @@ -83,6 +84,17 @@ public class CardController implements Controller { }; card.listeners().addPropertyChangeListener(Card.PROPERTY_SELECTED, cardSelectedChangeListener); + cardColorChangeListener = e -> { + switch((Card.Color)e.getNewValue()) { + case RED -> mainPane.setStyle(addStyle(mainPane.getStyle(), "-fx-background-color: red")); + case BLUE -> mainPane.setStyle(addStyle(mainPane.getStyle(), "-fx-background-color: blue")); + case GREEN -> mainPane.setStyle(addStyle(mainPane.getStyle(), "-fx-background-color: green")); + case YELLOW -> mainPane.setStyle(addStyle(mainPane.getStyle(), "-fx-background-color: yellow")); + default -> mainPane.setStyle(addStyle(mainPane.getStyle(), "-fx-background-color: black")); + } + }; + card.listeners().addPropertyChangeListener(Card.PROPERTY_COLOR, cardColorChangeListener); + return parent; } @@ -98,5 +110,6 @@ public class CardController implements Controller { @Override public void destroy() { card.listeners().removePropertyChangeListener(Card.PROPERTY_SELECTED, cardSelectedChangeListener); + card.listeners().removePropertyChangeListener(Card.PROPERTY_COLOR, cardColorChangeListener); } } diff --git a/src/main/java/de/hsfulda/onses/controllers/GameController.java b/src/main/java/de/hsfulda/onses/controllers/GameController.java index e9146bc..3246d95 100644 --- a/src/main/java/de/hsfulda/onses/controllers/GameController.java +++ b/src/main/java/de/hsfulda/onses/controllers/GameController.java @@ -39,6 +39,10 @@ public class GameController implements Controller { final Button playButton = (Button) parent.lookup("#playCardBtn"); final Button drawCardButton = (Button) parent.lookup("#drawCardBtn"); final Button exitGameButton = (Button) parent.lookup("#exitBtn"); + final Button wishRedButton = (Button) parent.lookup("#btnWishRed"); + final Button wishBlueButton = (Button) parent.lookup("#btnWishBlue"); + final Button wishGreenButton = (Button) parent.lookup("#btnWishGreen"); + final Button wishYellowButton = (Button) parent.lookup("#btnWishYellow"); CardController lastPlayedCardController = new CardController(game.getLastPlayedCard(), null); PlayerController playerController = new PlayerController(gameService.getGame().getPlayerService().getPlayerList().getFirst()); @@ -48,6 +52,26 @@ public class GameController implements Controller { controllers.add(playerController); controllers.add(enemyController); + wishRedButton.setOnAction(e -> { + Card currentSelectedCard = game.getPlayerService().getPlayerList().getFirst().getCurrentCard(); + currentSelectedCard.setColor(Card.Color.RED); + }); + + wishBlueButton.setOnAction(e -> { + Card currentSelectedCard = game.getPlayerService().getPlayerList().getFirst().getCurrentCard(); + currentSelectedCard.setColor(Card.Color.BLUE); + }); + + wishGreenButton.setOnAction(e -> { + Card currentSelectedCard = game.getPlayerService().getPlayerList().getFirst().getCurrentCard(); + currentSelectedCard.setColor(Card.Color.GREEN); + }); + + wishYellowButton.setOnAction(e -> { + Card currentSelectedCard = game.getPlayerService().getPlayerList().getFirst().getCurrentCard(); + currentSelectedCard.setColor(Card.Color.YELLOW); + }); + lastPlayedCardPropertyChangeListener = e -> { lastPlayedCardPane.getChildren().removeAll(); try { @@ -71,7 +95,14 @@ public class GameController implements Controller { playButton.setOnAction(e -> { Card cardToPlay = game.getPlayerService().getPlayerList().getFirst().getCurrentCard(); - if(gameService.legalMove(cardToPlay)) { + if(cardToPlay.getValue().equals(Card.Value.CHOOSE) || cardToPlay.getValue().equals(Card.Value.CHOOSEDRAW)) { + if(!cardToPlay.getColor().equals(Card.Color.BLACK)) { + if(cardToPlay.getPlayer() != null) { + cardToPlay.getPlayer().removeCardFromPlayerDeck(cardToPlay); + gameService.playCard(cardToPlay); + } + } + } else if(gameService.legalMove(cardToPlay)){ if(cardToPlay.getPlayer() != null) { cardToPlay.getPlayer().removeCardFromPlayerDeck(cardToPlay); gameService.playCard(cardToPlay); diff --git a/src/main/resources/de/hsfulda/onses/views/game.fxml b/src/main/resources/de/hsfulda/onses/views/game.fxml index 8ec0469..c7f1d37 100644 --- a/src/main/resources/de/hsfulda/onses/views/game.fxml +++ b/src/main/resources/de/hsfulda/onses/views/game.fxml @@ -13,12 +13,12 @@ - -