Browse Source

Merge branch 'PlayerController' into 'main'

add basic Player controller

See merge request fdai7736/onses!27
main
fdai7736 11 months ago
parent
commit
85ff47c51d
  1. 10
      src/main/java/de/hsfulda/onses/controllers/GameController.java
  2. 30
      src/main/java/de/hsfulda/onses/controllers/PlayerController.java
  3. 6
      src/main/java/de/hsfulda/onses/models/Player.java
  4. 14
      src/main/resources/de/hsfulda/onses/views/game.fxml
  5. 22
      src/main/resources/de/hsfulda/onses/views/player.fxml
  6. 18
      src/test/java/de/hsfulda/onses/PlayerTest.java

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

@ -23,9 +23,13 @@ public class GameController implements Controller {
public Parent render() throws IOException { public Parent render() throws IOException {
final Parent parent = FXMLLoader.load(Objects.requireNonNull(Main.class.getResource("views/game.fxml"))); final Parent parent = FXMLLoader.load(Objects.requireNonNull(Main.class.getResource("views/game.fxml")));
final Pane lastPlayedCardPane = (Pane) parent.lookup("#lastPlayedCardPane"); final Pane lastPlayedCardPane = (Pane) parent.lookup("#lastPlayedCardPane");
final Pane enemyPane = (Pane) parent.lookup("#enemyPane");
final Pane playerPane = (Pane) parent.lookup("#playerPane");
final Button playButton = (Button) parent.lookup("#playCardBtn"); final Button playButton = (Button) parent.lookup("#playCardBtn");
CardController lastPlayedCardController = new CardController(new Card().setValue(Card.Value.FIVE).setColor(Card.Color.BLUE));
CardController lastPlayedCardController = new CardController(game.getLastPlayedCard());
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 -> { game.listeners().addPropertyChangeListener(Game.PROPERTY_LAST_PLAYED_CARD, e -> {
lastPlayedCardPane.getChildren().removeAll(); lastPlayedCardPane.getChildren().removeAll();
@ -42,6 +46,8 @@ public class GameController implements Controller {
lastPlayedCardPane.getChildren().add(lastPlayedCardController.render()); lastPlayedCardPane.getChildren().add(lastPlayedCardController.render());
playerPane.getChildren().add(playerController.render());
enemyPane.getChildren().add(enemyController.render());
return parent; return parent;
} }

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

@ -1,16 +1,46 @@
package de.hsfulda.onses.controllers; package de.hsfulda.onses.controllers;
import de.hsfulda.onses.Main; import de.hsfulda.onses.Main;
import de.hsfulda.onses.models.Card;
import de.hsfulda.onses.models.Player;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
import javafx.scene.Parent; import javafx.scene.Parent;
import javafx.scene.control.Label;
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;
public PlayerController(Player player) {
this.player = player;
}
@Override @Override
public Parent render() throws IOException { public Parent render() throws IOException {
final Parent parent = FXMLLoader.load(Objects.requireNonNull(Main.class.getResource("views/player.fxml"))); final Parent parent = FXMLLoader.load(Objects.requireNonNull(Main.class.getResource("views/player.fxml")));
final Label playerNameLabel = (Label) parent.lookup("#playerNameLabel");
final HBox cards = (HBox) parent.lookup("#cardsHBox");
for(Card card : player.getPlayerDeck()) {
cards.getChildren().add(new CardController(card).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());
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
});
playerNameLabel.setText("Test");
return parent; return parent;
} }

6
src/main/java/de/hsfulda/onses/models/Player.java

@ -27,6 +27,12 @@ public class Player {
this.firePropertyChange(PROPERTY_PLAYER_DECK, oldplayerDeck, playerDeck); this.firePropertyChange(PROPERTY_PLAYER_DECK, oldplayerDeck, playerDeck);
} }
public void removeCardFromPlayerDeck(Card card) {
final ArrayList<Card> oldPlayerDeck = new ArrayList<>(this.playerDeck);
this.playerDeck.remove(card);
this.firePropertyChange(PROPERTY_PLAYER_DECK, oldPlayerDeck, playerDeck);
}
public PlayerService getPlayerService() { public PlayerService getPlayerService() {
return playerService; return playerService;
} }

14
src/main/resources/de/hsfulda/onses/views/game.fxml

@ -8,33 +8,33 @@
<AnchorPane prefHeight="690.0" prefWidth="1254.0" xmlns="http://javafx.com/javafx/21" xmlns:fx="http://javafx.com/fxml/1"> <AnchorPane prefHeight="690.0" prefWidth="1254.0" xmlns="http://javafx.com/javafx/21" xmlns:fx="http://javafx.com/fxml/1">
<children> <children>
<Button fx:id="btnWishBlue" layoutX="891.0" layoutY="558.0" mnemonicParsing="false" style="-fx-background-color: blue; -fx-pref-width: 30px; -fx-pref-height: 30px;" AnchorPane.bottomAnchor="100.0" AnchorPane.rightAnchor="280.0">
<Button fx:id="btnWishBlue" layoutX="1120.0" layoutY="507.0" mnemonicParsing="false" style="-fx-background-color: blue; -fx-pref-width: 30px; -fx-pref-height: 30px;" AnchorPane.bottomAnchor="153.0" AnchorPane.rightAnchor="104.0">
<cursor> <cursor>
<Cursor fx:constant="HAND" /> <Cursor fx:constant="HAND" />
</cursor> </cursor>
</Button> </Button>
<Button fx:id="btnWishRed" layoutX="931.0" layoutY="558.0" mnemonicParsing="false" style="-fx-background-color: red; -fx-pref-width: 30px; -fx-pref-height: 30px; -fx-cursor: pointer;" AnchorPane.bottomAnchor="100.0" AnchorPane.rightAnchor="240.0">
<Button fx:id="btnWishRed" layoutX="1160.0" layoutY="507.0" mnemonicParsing="false" style="-fx-background-color: red; -fx-pref-width: 30px; -fx-pref-height: 30px; -fx-cursor: pointer;" AnchorPane.bottomAnchor="153.0" AnchorPane.rightAnchor="64.0">
<cursor> <cursor>
<Cursor fx:constant="HAND" /> <Cursor fx:constant="HAND" />
</cursor> </cursor>
</Button> </Button>
<Button fx:id="btnWishGreen" layoutX="891.0" layoutY="602.0" mnemonicParsing="false" style="-fx-background-color: green; -fx-pref-width: 30px; -fx-pref-height: 30px; -fx-cursor: pointer;" AnchorPane.bottomAnchor="58.0" AnchorPane.rightAnchor="240.0">
<Button fx:id="btnWishGreen" layoutX="1160.0" layoutY="549.0" mnemonicParsing="false" style="-fx-background-color: green; -fx-pref-width: 30px; -fx-pref-height: 30px; -fx-cursor: pointer;" AnchorPane.bottomAnchor="111.0" AnchorPane.rightAnchor="64.0">
<cursor> <cursor>
<Cursor fx:constant="HAND" /> <Cursor fx:constant="HAND" />
</cursor> </cursor>
</Button> </Button>
<Button fx:id="btnWishYellow" layoutX="891.0" layoutY="602.0" mnemonicParsing="false" style="-fx-background-color: yellow; -fx-pref-width: 30px; -fx-pref-height: 30px;" AnchorPane.bottomAnchor="58.0" AnchorPane.rightAnchor="280.0">
<Button fx:id="btnWishYellow" layoutX="1120.0" layoutY="549.0" mnemonicParsing="false" style="-fx-background-color: yellow; -fx-pref-width: 30px; -fx-pref-height: 30px;" AnchorPane.bottomAnchor="111.0" AnchorPane.rightAnchor="104.0">
<cursor> <cursor>
<Cursor fx:constant="HAND" /> <Cursor fx:constant="HAND" />
</cursor> </cursor>
</Button> </Button>
<Button fx:id="playCardBtn" layoutX="1085.0" layoutY="586.0" mnemonicParsing="false" text="Play Card">
<Button fx:id="playCardBtn" layoutX="1100.0" layoutY="616.0" mnemonicParsing="false" text="Play Card">
<font> <font>
<Font size="18.0" /> <Font size="18.0" />
</font> </font>
</Button> </Button>
<Pane fx:id="playerPane" layoutX="18.0" layoutY="460.0" prefHeight="217.0" prefWidth="891.0" style="-fx-border-color: red;" AnchorPane.bottomAnchor="13.0" AnchorPane.leftAnchor="18.0" AnchorPane.rightAnchor="345.0" />
<Pane fx:id="enemyPane" layoutX="18.0" layoutY="14.0" prefHeight="217.0" prefWidth="891.0" style="-fx-border-color: red;" />
<Pane fx:id="playerPane" layoutX="18.0" layoutY="460.0" prefHeight="228.0" prefWidth="950.0" style="-fx-border-color: red;" AnchorPane.bottomAnchor="10.0" AnchorPane.leftAnchor="18.0" AnchorPane.rightAnchor="286.0" />
<Pane fx:id="enemyPane" layoutX="18.0" layoutY="14.0" prefHeight="228.0" prefWidth="950.0" style="-fx-border-color: red;" AnchorPane.leftAnchor="18.0" AnchorPane.rightAnchor="286.0" AnchorPane.topAnchor="10.0" />
<Pane fx:id="lastPlayedCardPane" layoutX="112.0" layoutY="245.0" maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="200.0" prefWidth="130.0" AnchorPane.leftAnchor="112.0" AnchorPane.topAnchor="245.0" /> <Pane fx:id="lastPlayedCardPane" layoutX="112.0" layoutY="245.0" maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="200.0" prefWidth="130.0" AnchorPane.leftAnchor="112.0" AnchorPane.topAnchor="245.0" />
<Button layoutX="404.0" layoutY="329.0" mnemonicParsing="false" text="Draw Card" AnchorPane.leftAnchor="404.0" AnchorPane.topAnchor="329.0"> <Button layoutX="404.0" layoutY="329.0" mnemonicParsing="false" text="Draw Card" AnchorPane.leftAnchor="404.0" AnchorPane.topAnchor="329.0">
<font> <font>

22
src/main/resources/de/hsfulda/onses/views/player.fxml

@ -1,13 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml"
prefHeight="400.0" prefWidth="600.0">
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="228.0" prefWidth="950.0" xmlns="http://javafx.com/javafx/21" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label fx:id="playerNameLabel" layoutX="14.0" layoutY="14.0" text="Player Name" AnchorPane.leftAnchor="3.0" AnchorPane.topAnchor="1.0">
<font>
<Font size="17.0" />
</font>
</Label>
<HBox fx:id="cardsHBox" layoutY="22.0" prefHeight="200.0" prefWidth="940.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="3.0" AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="25.0" />
</children>
</AnchorPane> </AnchorPane>

18
src/test/java/de/hsfulda/onses/PlayerTest.java

@ -59,6 +59,23 @@ public class PlayerTest {
assertEquals(expected, answer2); assertEquals(expected, answer2);
} }
@Test
@DisplayName("remove card from player card deck")
void removeCardFromPlayerCardDeck() {
Player player = new Player();
Card card1 = new Card().setColor(Card.Color.RED).setValue(Card.Value.FIVE);
Card card2 = new Card().setColor(Card.Color.GREEN).setValue(Card.Value.ONE);
player.addCardToPlayerDeck(card1);
player.addCardToPlayerDeck(card2);
assertEquals(2, player.getPlayerDeck().size());
player.removeCardFromPlayerDeck(card1);
assertEquals(1, player.getPlayerDeck().size());
assertEquals(card2, player.getPlayerDeck().getFirst());
}
@Test @Test
@DisplayName("GivePlayerName") @DisplayName("GivePlayerName")
public void GivePlayerName() { public void GivePlayerName() {
@ -88,6 +105,7 @@ public class PlayerTest {
player.setEnemy(true); player.setEnemy(true);
assertTrue(player.isEnemy()); assertTrue(player.isEnemy());
} }
@Test @Test
@DisplayName("CurrentCard") @DisplayName("CurrentCard")
public void CurrentCard() { public void CurrentCard() {

Loading…
Cancel
Save