Skip to content

Commit

Permalink
finished prototype choice selection box
Browse files Browse the repository at this point in the history
  • Loading branch information
bazola committed Sep 4, 2014
1 parent 7341605 commit 1b7dda9
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 42 deletions.
53 changes: 34 additions & 19 deletions cardshifter-fx/src/main/java/com/cardshifter/fx/CardNode.java
Expand Up @@ -15,6 +15,15 @@
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

/* The purpose of this class is to take in certain values from the
Game controller and create a Group that the controller can
retrieve in order to render a Player card on the screen
*/

//TODO: Refactor this so that it takes a Pane and produces its own sizes

//TODO: make a maximum card size (get the card size based on pane size / how big cards should be

public class CardNode {

private final double sizeX;
Expand Down Expand Up @@ -105,29 +114,35 @@ private void createCardActivateButton() {
private void buttonClick(ActionEvent event) {
System.out.println("Trying to Perform Action");
List<UsableAction> cardActions = card.getActions().values().stream().filter(UsableAction::isAllowed).collect(Collectors.toList());
for (UsableAction action : cardActions) {
if (action.isAllowed()) {
if (action instanceof TargetAction) {
TargetAction targetAction = (TargetAction) action;
List<Targetable> targets = targetAction.findTargets();
if (targets.isEmpty()) {
return;
}

//If there is more than one action, create the choice box
if(cardActions.size() > 1) {
this.controller.buildChoiceBoxPane(card, cardActions);
} else if (cardActions.size() == 1) {
for (UsableAction action : cardActions) {
if (action.isAllowed()) {
if (action instanceof TargetAction) {
TargetAction targetAction = (TargetAction) action;
List<Targetable> targets = targetAction.findTargets();
if (targets.isEmpty()) {
return;
}

//int targetIndex = Integer.parseInt(input.nextLine());
int targetIndex = 0;
if (targetIndex < 0 || targetIndex >= cardActions.size()) {
return;
}
//int targetIndex = Integer.parseInt(input.nextLine());
int targetIndex = 0;
if (targetIndex < 0 || targetIndex >= cardActions.size()) {
return;
}

//TODO: add a check to make sure the target is valid//
Targetable target = targets.get(targetIndex);
targetAction.setTarget(target);
targetAction.perform();
//TODO: add a check to make sure the target is valid//
Targetable target = targets.get(targetIndex);
targetAction.setTarget(target);
targetAction.perform();
}
else action.perform();
}
else action.perform();
this.controller.render();
}
this.controller.render();
}
}

Expand Down
Expand Up @@ -110,29 +110,35 @@ private void createCardActivateButton() {
private void buttonClick(ActionEvent event) {
System.out.println("Trying to Perform Action");
List<UsableAction> cardActions = card.getActions().values().stream().filter(UsableAction::isAllowed).collect(Collectors.toList());
for (UsableAction action : cardActions) {
if (action.isAllowed()) {
if (action instanceof TargetAction) {
TargetAction targetAction = (TargetAction) action;
List<Targetable> targets = targetAction.findTargets();
if (targets.isEmpty()) {
return;
}

//If there is more than one action, create the choice box
if(cardActions.size() > 1) {
this.controller.buildChoiceBoxPane(card, cardActions);
} else if (cardActions.size() == 1) {
for (UsableAction action : cardActions) {
if (action.isAllowed()) {
if (action instanceof TargetAction) {
TargetAction targetAction = (TargetAction) action;
List<Targetable> targets = targetAction.findTargets();
if (targets.isEmpty()) {
return;
}

//int targetIndex = Integer.parseInt(input.nextLine());
int targetIndex = 0;
if (targetIndex < 0 || targetIndex >= cardActions.size()) {
return;
}
//int targetIndex = Integer.parseInt(input.nextLine());
int targetIndex = 0;
if (targetIndex < 0 || targetIndex >= cardActions.size()) {
return;
}

//TODO: add a check to make sure the target is valid//
Targetable target = targets.get(targetIndex);
targetAction.setTarget(target);
targetAction.perform();
//TODO: add a check to make sure the target is valid//
Targetable target = targets.get(targetIndex);
targetAction.setTarget(target);
targetAction.perform();
}
else action.perform();
}
else action.perform();
this.controller.render();
}
this.controller.render();
}
}

Expand Down
98 changes: 98 additions & 0 deletions cardshifter-fx/src/main/java/com/cardshifter/fx/ChoiceBoxNode.java
@@ -0,0 +1,98 @@
package com.cardshifter.fx;

import com.cardshifter.core.TargetAction;
import com.cardshifter.core.Targetable;
import com.cardshifter.core.UsableAction;
import java.util.List;
import javafx.event.ActionEvent;
import javafx.scene.Group;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

public class ChoiceBoxNode {

private final double sizeX;
private final double sizeY;
private final String name;
private final UsableAction action;
private final FXMLGameController controller;

private final Group choiceBoxGroup;

public ChoiceBoxNode(double sizeX, double sizeY, String name, UsableAction action, FXMLGameController controller) {
this.sizeX = sizeX;
this.sizeY = sizeY;
this.name = name;
this.action = action;
this.controller = controller;
this.choiceBoxGroup = new Group();
this.createChoiceBox();
}

public Group getChoiceBoxGroup() {
return this.choiceBoxGroup;
}

private void createChoiceBox() {
this.createChoiceBoxBackground();
this.createChoiceBoxFace();
this.createChoiceBoxText();
this.createActivateButton();
}

private void createChoiceBoxBackground() {
//could be changed to be some kind of animated background or jpeg
Rectangle activeBackground = new Rectangle(-this.sizeX*0.02, -this.sizeX*0.02, this.sizeX, this.sizeY);
activeBackground.setFill(Color.YELLOW);
choiceBoxGroup.getChildren().add(activeBackground);
}

private void createChoiceBoxFace() {
Rectangle choiceBoxFace = new Rectangle(0,0,this.sizeX*0.96,this.sizeY*0.96);
choiceBoxFace.setFill(Color.FIREBRICK);
choiceBoxGroup.getChildren().add(choiceBoxFace);
}

private void createChoiceBoxText() {
Label choiceBoxText = new Label();
choiceBoxText.setText(String.format("Action = \n %s", this.action.getName()));
choiceBoxText.setTextFill(Color.WHITE);
choiceBoxGroup.getChildren().add(choiceBoxText);
}

private void createActivateButton() {
Button button = new Button();
//button.setStyle("-fx-background-color:transparent");
//button.minWidth(100);
//button.minHeight(100);
//button.prefWidth(100);
//button.prefHeight(100);
button.setOnAction(this::buttonClick);
choiceBoxGroup.getChildren().add(button);
}

private void buttonClick(ActionEvent event) {
System.out.println("Trying to Perform Choice");
if (this.action.isAllowed()) {
if (this.action instanceof TargetAction) {
TargetAction targetAction = (TargetAction) this.action;
List<Targetable> targets = targetAction.findTargets();
if (targets.isEmpty()) {
return;
}

//int targetIndex = Integer.parseInt(input.nextLine());
int targetIndex = 0;

//TODO: add a check to make sure the target is valid//
Targetable target = targets.get(targetIndex);
targetAction.setTarget(target);
targetAction.perform();
}
else this.action.perform();
}
this.controller.render();
}
}
Expand Up @@ -27,6 +27,7 @@
import com.cardshifter.core.UsableAction;
import com.cardshifter.core.Zone;
import com.cardshifter.core.console.CommandLineOptions;
import javafx.scene.Node;

public class FXMLGameController implements Initializable {

Expand All @@ -37,6 +38,8 @@ public class FXMLGameController implements Initializable {
//hack to make the buttons work properly
private boolean gameHasStarted = false;
//I think this is a public constructor, this code initializes the Game
@FXML
Pane anchorPane;
public FXMLGameController() throws Exception {
CommandLineOptions options = new CommandLineOptions();
InputStream file = options.getScript() == null ? Game.class.getResourceAsStream("start.lua") : new FileInputStream(new File(options.getScript()));
Expand All @@ -57,8 +60,15 @@ private void startGameButtonAction(ActionEvent event) {
}
}

//TODO: Create a fixed time step and render in that
//UPDATE LOOP
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");
anchorPane.getChildren().remove(choiceBoxPane);

this.renderHands();
this.renderBattlefields();
}
Expand All @@ -69,7 +79,8 @@ private void renderHands() {
this.renderPlayerHand();
}

//TODO: Convert this to mana totals for players, and only increment every play rotation
//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
@FXML
private Label turnLabel;
Expand Down Expand Up @@ -203,13 +214,40 @@ private void renderPlayerBattlefield() {
cardIndex++;
}
}


private List<Card> getBattlefield(Player player) {
Zone battlefield = (Zone)CoerceLuaToJava.coerce(player.data.get("battlefield"), Zone.class);
return battlefield.getCards();
}

//CHOICE BOX PANE
public void buildChoiceBoxPane(Card card, List<UsableAction> actionList) {
Pane choiceBoxPane = new Pane();
choiceBoxPane.setPrefHeight(367);
choiceBoxPane.setPrefWidth(550);
choiceBoxPane.setTranslateX(326);
choiceBoxPane.setTranslateY(150);
choiceBoxPane.setId("choiceBoxPane");

choiceBoxPane.getChildren().clear();

int numChoices = actionList.size();
double paneHeight = choiceBoxPane.getPrefHeight();
double paneWidth = choiceBoxPane.getPrefWidth();
double choiceBoxWidth = paneWidth / numChoices;

int actionIndex = 0;
for(UsableAction action : actionList) {
ChoiceBoxNode choiceBox = new ChoiceBoxNode(choiceBoxWidth, paneHeight, "testName", action, this);
Group choiceBoxGroup = choiceBox.getChoiceBoxGroup();
choiceBoxGroup.setTranslateX(actionIndex * choiceBoxWidth);
choiceBoxPane.getChildren().add(choiceBox.getChoiceBoxGroup());

actionIndex++;
}

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

//BOILERPLATE
@Override
public void initialize(URL url, ResourceBundle rb) {
Expand Down
Expand Up @@ -8,7 +8,7 @@
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" maxHeight="600" maxWidth="1200" minHeight="600" minWidth="1200" prefHeight="600.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.cardshifter.fx.FXMLGameController">
<AnchorPane fx:id="anchorPane" maxHeight="600" maxWidth="1200" minHeight="600" minWidth="1200" prefHeight="600.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.cardshifter.fx.FXMLGameController">
<children>
<Button fx:id="button" layoutX="1098.0" layoutY="530.0" onAction="#startGameButtonAction" text="Start Game" />
<Label fx:id="startGameLabel" layoutX="1098.0" layoutY="564.0" minHeight="16" minWidth="69" />
Expand Down

0 comments on commit 1b7dda9

Please sign in to comment.