Skip to content

Commit

Permalink
separated rendering from creating hands/battlefields
Browse files Browse the repository at this point in the history
  • Loading branch information
bazola committed Sep 4, 2014
1 parent dc47333 commit 030ab99
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 74 deletions.
Expand Up @@ -149,6 +149,7 @@ private void buttonClick(ActionEvent event) {
}
else action.perform();
}
this.controller.createData();
this.controller.render();
}
}
Expand Down
Expand Up @@ -25,8 +25,6 @@ public class CardNodeBattlefield extends Group {
private final FXMLGameController controller;
private final boolean isPlayer;

//private final Group cardGroup;

public CardNodeBattlefield (Pane pane, int numCards, String name, Card card, FXMLGameController controller, boolean isPlayer) {
//calculate card width based on pane size
double paneWidth = pane.getWidth();
Expand All @@ -40,7 +38,6 @@ public CardNodeBattlefield (Pane pane, int numCards, String name, Card card, FXM
this.card = card;
this.controller = controller;
this.isPlayer = isPlayer;
//this.cardGroup = new Group();
this.createCard();
}

Expand Down Expand Up @@ -74,12 +71,12 @@ private void createCardBackground(boolean targetMode) {
//background border will be smaller for these and a ratio
Rectangle activeBackground = new Rectangle(-this.sizeX*0.02,-this.sizeY*0.02,this.sizeX, this.sizeY);
activeBackground.setFill(Color.BLACK);
if(this.isCardActive() == true) {
if(this.isCardActive()) {
activeBackground.setFill(Color.YELLOW);
}

if(targetMode) {
activeBackground.setFill(Color.AZURE);
activeBackground.setFill(Color.BLUE);
}

this.getChildren().add(activeBackground);
Expand Down
182 changes: 117 additions & 65 deletions cardshifter-fx/src/main/java/com/cardshifter/fx/FXMLGameController.java
Expand Up @@ -24,18 +24,18 @@
import com.cardshifter.core.Card;
import com.cardshifter.core.Game;
import com.cardshifter.core.Player;
import com.cardshifter.core.Targetable;
import com.cardshifter.core.UsableAction;
import com.cardshifter.core.Zone;
import com.cardshifter.core.console.CommandLineOptions;
import java.util.ArrayList;
import javafx.scene.Node;

public class FXMLGameController implements Initializable {

//INITIAL GAME SETUP
private final CardshifterAI opponent = new CompleteIdiot();
//need a forward declaration so that this is global to the class
Game game;
private Game game;
//hack to make the buttons work properly
private boolean gameHasStarted = false;
//I think this is a public constructor, this code initializes the Game
Expand All @@ -46,8 +46,7 @@ public FXMLGameController() throws Exception {
InputStream file = options.getScript() == null ? Game.class.getResourceAsStream("start.lua") : new FileInputStream(new File(options.getScript()));
game = new Game(file, options.getRandom());
}

//START GAME BUTTON
//GAME START
@FXML
private Label startGameLabel;
@FXML
Expand All @@ -57,14 +56,20 @@ private void startGameButtonAction(ActionEvent event) {
game.getEvents().startGame(game);
gameHasStarted = true;
turnLabel.setText(String.format("Turn Number %d", game.getTurnNumber()));
this.renderHands();

this.playerBattlefieldData = new ArrayList<>();
this.opponentBattlefieldData = new ArrayList<>();
this.playerHandData = new ArrayList<>();
this.opponentHandData = new ArrayList<>();

this.createData();
this.render();
}
}

//TODO: Create a fixed time step and render in that
//UPDATE LOOP
//RENDER - eventually change this to an Update method
public void render() {

//this is a hack to make the buttons disappear when the choice is made
//will not work with a fixed time step
Node choiceBoxPane = anchorPane.lookup("#choiceBoxPane");
Expand All @@ -73,46 +78,62 @@ public void render() {
this.renderHands();
this.renderBattlefields();
}

//RENDER HANDS
@FXML
private Pane opponentHandPane;
@FXML
private Pane playerHandPane;
private void renderHands() {
player02Pane.getChildren().clear();
player01Pane.getChildren().clear();
this.renderOpponentHand();
this.renderPlayerHand();
}
private void renderOpponentHand() {
opponentHandPane.getChildren().clear();
opponentHandPane.getChildren().addAll(opponentHandData);
}
private void renderPlayerHand() {
playerHandPane.getChildren().clear();
playerHandPane.getChildren().addAll(playerHandData);
}

//TODO: Convert this to mana totals for players
//TODO: Play rotation needs to be changed so that it does not revolve around player 1
//TURN LABEL
//RENDER BATTLEFIELDS
@FXML
private Label turnLabel;
//ADVANCE TURNS
Pane opponentBattlefieldPane;
@FXML
private void handleTurnButtonAction(ActionEvent event) {
if (gameHasStarted == true) {
game.nextTurn();

//This is the AI doing the turn action
while (game.getCurrentPlayer() == game.getLastPlayer()) {
UsableAction action = opponent.getAction(game.getCurrentPlayer());
if (action == null) {
System.out.println("Warning: Opponent did not properly end turn");
break;
}
action.perform();
}

turnLabel.setText(String.format("Turn Number %d", game.getTurnNumber()));
this.render();
}
Pane playerBattlefieldPane;
private void renderBattlefields() {
this.renderOpponentBattlefield();
this.renderPlayerBattlefield();
}
private void renderOpponentBattlefield() {
opponentBattlefieldPane.getChildren().clear();
opponentBattlefieldPane.getChildren().addAll(opponentBattlefieldData);
}
private void renderPlayerBattlefield() {
playerBattlefieldPane.getChildren().clear();
playerBattlefieldPane.getChildren().addAll(playerBattlefieldData);
}

//RENDER PLAYER 2 CARD BACKS
@FXML
Pane player02Pane;
private void renderOpponentHand() {
//Called at the end of turn, and when a card is played
public void createData() {
this.createHands();
this.createBattlefields();
}

//CREATE HANDS
private void createHands() {
this.createOpponentHand();
this.createPlayerHand();
}
//CREATE OPPONENT CARD BACKS
private List<Group> opponentHandData;
private void createOpponentHand() {
opponentHandData.clear();

//Opponent cards are rendered differently because the faces are not visible
double paneHeight = player02Pane.getHeight();
double paneWidth = player02Pane.getWidth();
double paneHeight = opponentHandPane.getHeight();
double paneWidth = opponentHandPane.getWidth();

int numCards = this.getOpponentCardCount();
int maxCards = Math.max(numCards, 8);
Expand All @@ -122,7 +143,7 @@ private void renderOpponentHand() {
while(currentCard < numCards) {
Group cardGroup = new Group();
cardGroup.setTranslateX(currentCard * (cardWidth*1.05)); //1.05 so there is space between cards
player02Pane.getChildren().add(cardGroup);
opponentHandData.add(cardGroup);

Rectangle cardBack = new Rectangle(0,0,cardWidth,paneHeight);
cardBack.setFill(Color.AQUAMARINE);
Expand All @@ -137,22 +158,22 @@ private int getOpponentCardCount() {
List<Card> cardsInHand = hand.getCards();
return cardsInHand.size();
}

//RENDER PLAYER ONE CARDS
@FXML
private Pane player01Pane;
private void renderPlayerHand() {
//CREATE PLAYER HAND
private List<Group> playerHandData;
private void createPlayerHand() {
playerHandData.clear();
List<Card> cardsInHand = this.getCurrentPlayerHand();
int numCards = cardsInHand.size();

int cardIndex = 0;
for (Card card : cardsInHand) {
CardNode cardNode = new CardNode(player01Pane, numCards, "testName", card, this);
CardNode cardNode = new CardNode(playerHandPane, numCards, "testName", card, this);
Group cardGroup = cardNode.getCardGroup();
cardGroup.setAutoSizeChildren(true); //NEW
cardGroup.setId(String.format("card%d", card.getId()));
cardGroup.setTranslateX(cardIndex * cardNode.getWidth());
player01Pane.getChildren().add(cardGroup);
playerHandData.add(cardGroup);

cardIndex++;
}
Expand All @@ -163,45 +184,47 @@ private List<Card> getCurrentPlayerHand() {
return hand.getCards();
}

//RENDER BATTLEFIELD
@FXML
Pane player02Battlefield;
@FXML
Pane player01Battlefield;
private void renderBattlefields() {
player02Battlefield.getChildren().clear();
player01Battlefield.getChildren().clear();
this.renderOpponentBattlefield();
this.renderPlayerBattlefield();
//CREATE BATTLEFIELDS
private void createBattlefields() {
this.createOpponentBattlefield();
this.createPlayerBattlefield();
}
private void renderOpponentBattlefield() {
//CREATE OPPONENT BATTLEFIELD
private List<Group> opponentBattlefieldData;
private void createOpponentBattlefield() {
opponentBattlefieldData.clear();

List<Card> cardsInBattlefield = this.getBattlefield(game.getLastPlayer());
int numCards = cardsInBattlefield.size();

int cardIndex = 0;
for (Card card : cardsInBattlefield) {
CardNodeBattlefield cardNode = new CardNodeBattlefield(player02Battlefield, numCards, "testName", card, this, false);
CardNodeBattlefield cardNode = new CardNodeBattlefield(opponentBattlefieldPane, numCards, "testName", card, this, false);
Group cardGroup = cardNode.getCardGroup();
cardGroup.setAutoSizeChildren(true); //NEW
cardGroup.setId(String.format("card%d", card.getId()));
cardGroup.setTranslateX(cardIndex * cardNode.getWidth());
player02Battlefield.getChildren().add(cardGroup);
opponentBattlefieldData.add(cardGroup);

cardIndex++;
}
}
private void renderPlayerBattlefield() {
//CREATE PLAYER BATTLEFIELD
private List<Group> playerBattlefieldData;
private void createPlayerBattlefield() {
playerBattlefieldData.clear();

List<Card> cardsInBattlefield = this.getBattlefield(game.getFirstPlayer());
int numCards = cardsInBattlefield.size();

int cardIndex = 0;
for (Card card : cardsInBattlefield) {
CardNodeBattlefield cardNode = new CardNodeBattlefield(player01Battlefield, numCards, "testName", card, this, true);
CardNodeBattlefield cardNode = new CardNodeBattlefield(playerBattlefieldPane, numCards, "testName", card, this, true);
Group cardGroup = cardNode.getCardGroup();
cardGroup.setAutoSizeChildren(true); //NEW
cardGroup.setId(String.format("card%d", card.getId()));
cardGroup.setTranslateX(cardIndex * cardNode.getWidth());
player01Battlefield.getChildren().add(cardGroup);
playerBattlefieldData.add(cardGroup);

cardIndex++;
}
Expand All @@ -211,6 +234,35 @@ private List<Card> getBattlefield(Player player) {
return battlefield.getCards();
}

//TODO: Convert this to mana totals for players
//TODO: Play rotation needs to be changed so that it does not revolve around player 1
//END TURN LABEL
@FXML
private Label turnLabel;
//ADVANCE TURNS
@FXML
private void handleTurnButtonAction(ActionEvent event) {
if (gameHasStarted == true) {
game.nextTurn();

//This is the AI doing the turn action
while (game.getCurrentPlayer() == game.getLastPlayer()) {
UsableAction action = opponent.getAction(game.getCurrentPlayer());
if (action == null) {
System.out.println("Warning: Opponent did not properly end turn");
break;
}
action.perform();
}

turnLabel.setText(String.format("Turn Number %d", game.getTurnNumber()));

//reload data at the start of a new turn
this.createData();
this.render();
}
}

//CHOICE BOX PANE
public void buildChoiceBoxPane(Card card, List<UsableAction> actionList) {
Pane choiceBoxPane = new Pane();
Expand Down Expand Up @@ -239,11 +291,11 @@ public void buildChoiceBoxPane(Card card, List<UsableAction> actionList) {

anchorPane.getChildren().add(choiceBoxPane);
}

//TARGETING
public void markTargets(List<Card> targets) {
List<Node> cardsInPlayerBattlefield = player01Battlefield.getChildren();
List<Node> cardsInOpponentBattlefield = player02Battlefield.getChildren();
List<Node> cardsInPlayerBattlefield = playerBattlefieldPane.getChildren();
List<Node> cardsInOpponentBattlefield = opponentBattlefieldPane.getChildren();
for (Card target : targets) {
for(Node node : cardsInPlayerBattlefield) {
if(node.getId().equals(String.format("card%d",target.getId())) == true) {
Expand Down
Expand Up @@ -14,14 +14,14 @@
<Label fx:id="startGameLabel" layoutX="1098.0" layoutY="564.0" minHeight="16" minWidth="69" />
<Button layoutX="1093.0" layoutY="367.0" mnemonicParsing="false" onAction="#handleTurnButtonAction" text="Next Turn" />
<Label fx:id="turnLabel" layoutX="1091.0" layoutY="412.0" text="Turn Number" />
<Pane fx:id="player01Pane" layoutX="14.0" layoutY="393.0" prefHeight="200.0" prefWidth="1066.0">
<Pane fx:id="playerHandPane" layoutX="14.0" layoutY="393.0" prefHeight="200.0" prefWidth="1066.0">
<children>
<QuadCurve fx:id="handGuide" controlY="-100.0" endX="100.0" fill="DODGERBLUE" layoutX="480.0" layoutY="200.0" opacity="0.0" startX="-100.0" stroke="BLACK" strokeType="INSIDE" />
</children>
</Pane>
<Pane fx:id="player02Pane" layoutX="153.0" layoutY="6.0" prefHeight="61.0" prefWidth="895.0">
<Pane fx:id="opponentHandPane" layoutX="153.0" layoutY="6.0" prefHeight="61.0" prefWidth="895.0">
</Pane>
<Pane fx:id="player01Battlefield" layoutX="47.0" layoutY="237.0" prefHeight="98.0" prefWidth="1001.0" />
<Pane fx:id="player02Battlefield" layoutX="47.0" layoutY="107.0" prefHeight="98.0" prefWidth="1001.0" />
<Pane fx:id="playerBattlefieldPane" layoutX="47.0" layoutY="237.0" prefHeight="98.0" prefWidth="1001.0" />
<Pane fx:id="opponentBattlefieldPane" layoutX="47.0" layoutY="107.0" prefHeight="98.0" prefWidth="1001.0" />
</children>
</AnchorPane>

0 comments on commit 030ab99

Please sign in to comment.