From f5b7c26cf7fe413fa6202621b5489fc0457f1e17 Mon Sep 17 00:00:00 2001 From: fdai7736 Date: Wed, 7 Feb 2024 21:23:43 +0100 Subject: [PATCH] add selected Card player --- .../onses/controllers/CardController.java | 28 +++++++++++++++++-- .../onses/controllers/GameController.java | 4 +-- .../onses/controllers/PlayerController.java | 4 +-- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/main/java/de/hsfulda/onses/controllers/CardController.java b/src/main/java/de/hsfulda/onses/controllers/CardController.java index eeffa39..5e2aef6 100644 --- a/src/main/java/de/hsfulda/onses/controllers/CardController.java +++ b/src/main/java/de/hsfulda/onses/controllers/CardController.java @@ -2,6 +2,7 @@ package de.hsfulda.onses.controllers; import de.hsfulda.onses.Main; import de.hsfulda.onses.models.Card; +import de.hsfulda.onses.models.Player; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.control.Label; @@ -12,10 +13,12 @@ import java.util.Objects; public class CardController implements Controller { - private Card card; + private final Card card; + private final Player player; - public CardController(Card card) { + public CardController(Card card, Player player) { this.card = card; + this.player = player; } @Override public Parent render() throws IOException { @@ -53,6 +56,27 @@ public class CardController implements Controller { } } + if(player != null && !player.isEnemy()) { + mainPane.setOnMousePressed(e -> { + Card oldCard = player.getCurrentCard(); + if(oldCard != null) { + oldCard.setSelected(false); + } + player.setCurrentCard(card); + card.setSelected(true); + }); + } + + card.listeners().addPropertyChangeListener(Card.PROPERTY_SELECTED, e -> { + boolean oldValue = (boolean) e.getOldValue(); + if(oldValue) { + mainPane.setStyle(addStyle(mainPane.getStyle(), "-fx-border-color: black")); + } else { + mainPane.setStyle(addStyle(mainPane.getStyle(), "-fx-border-color: pink")); + } + + }); + return parent; } diff --git a/src/main/java/de/hsfulda/onses/controllers/GameController.java b/src/main/java/de/hsfulda/onses/controllers/GameController.java index 11ae0ae..70cc61b 100644 --- a/src/main/java/de/hsfulda/onses/controllers/GameController.java +++ b/src/main/java/de/hsfulda/onses/controllers/GameController.java @@ -27,14 +27,14 @@ public class GameController implements Controller { final Pane playerPane = (Pane) parent.lookup("#playerPane"); final Button playButton = (Button) parent.lookup("#playCardBtn"); - CardController lastPlayedCardController = new CardController(game.getLastPlayedCard()); + 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()); game.listeners().addPropertyChangeListener(Game.PROPERTY_LAST_PLAYED_CARD, e -> { lastPlayedCardPane.getChildren().removeAll(); try { - lastPlayedCardPane.getChildren().add(new CardController((Card) e.getNewValue()).render()); + lastPlayedCardPane.getChildren().add(new CardController((Card) e.getNewValue(), null).render()); } catch (IOException ex) { throw new RuntimeException(ex); } diff --git a/src/main/java/de/hsfulda/onses/controllers/PlayerController.java b/src/main/java/de/hsfulda/onses/controllers/PlayerController.java index 1757520..5a11d1c 100644 --- a/src/main/java/de/hsfulda/onses/controllers/PlayerController.java +++ b/src/main/java/de/hsfulda/onses/controllers/PlayerController.java @@ -26,14 +26,14 @@ public class PlayerController implements Controller { final HBox cards = (HBox) parent.lookup("#cardsHBox"); for(Card card : player.getPlayerDeck()) { - cards.getChildren().add(new CardController(card).render()); + cards.getChildren().add(new CardController(card, player).render()); } player.listeners().addPropertyChangeListener(Player.PROPERTY_PLAYER_DECK, e -> { cards.getChildren().clear(); for(Card card : player.getPlayerDeck()) { try { - cards.getChildren().add(new CardController(card).render()); + cards.getChildren().add(new CardController(card, player).render()); } catch (IOException ex) { throw new RuntimeException(ex); }