Skip to content

Commit

Permalink
Merged with javafx
Browse files Browse the repository at this point in the history
  • Loading branch information
Zomis committed Sep 8, 2014
2 parents edcaf21 + 333c5a1 commit beba9bc
Show file tree
Hide file tree
Showing 8 changed files with 750 additions and 107 deletions.
Expand Up @@ -25,9 +25,25 @@ public UsableAction getAction(Player player) {
.filter(action -> setTargetIfPossible(action));
List<UsableAction> list = allActions.collect(Collectors.toList());

//return nothing if no actions are available
if (list.isEmpty()) {
return null;
}

//Do not scrap if it is the only thing you can do
if(list.size() == 1) {
for(UsableAction action : list) {
if(action.getName().equals("Scrap")) {
return null;
}
}
}

//parse the actions and return an appropriate one based on the actions available
//For example, if there are less than 3 creatures on the board, do not scrap any
//If any attacks are available, do those

//return a random action from the list
return list.get(random.nextInt(list.size()));
}

Expand Down
7 changes: 5 additions & 2 deletions cardshifter-core/src/main/java/com/cardshifter/core/Game.java
Expand Up @@ -48,7 +48,7 @@ public Game(InputStream file, Random random, StateChangeListener listener) {
this.players.add(new Player(this, "Player2", nextId()));
this.random = random;
this.listener = listener;
this.turnNumber = 0;
this.turnNumber = 1;
}

public Game(InputStream file) {
Expand Down Expand Up @@ -115,7 +115,10 @@ public void nextTurn() {

this.events.callEvent(Events.TURN_START, CoerceJavaToLua.coerce(this.currentPlayer), null);

turnNumber++;
//Quick hack to only advance the turn number when control passes back to player
if (this.currentPlayer == this.getLastPlayer()) {
turnNumber++;
}
}

public int randomInt(int count) {
Expand Down
79 changes: 50 additions & 29 deletions cardshifter-fx/src/main/java/com/cardshifter/fx/CardNode.java
Expand Up @@ -12,9 +12,16 @@
import javafx.scene.Group;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
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
*/

public class CardNode {

private final double sizeX;
Expand All @@ -25,9 +32,15 @@ public class CardNode {

private final Group cardGroup;

public CardNode(double sizeX, double sizeY, String name, Card card, FXMLGameController controller) {
this.sizeX = sizeX;
this.sizeY = sizeY;
public CardNode(Pane pane, int numCards, String name, Card card, FXMLGameController controller) {
//calculate card width based on pane size
double paneWidth = pane.getWidth();
//reduce card size if there are over a certain amount of them
int maxCards = Math.max(numCards, 8);
double cardWidth = paneWidth / maxCards;

this.sizeX = cardWidth;
this.sizeY = pane.getHeight();
this.name = name;
this.card = card;
this.controller = controller;
Expand All @@ -39,6 +52,10 @@ public Group getCardGroup() {
return this.cardGroup;
}

public double getWidth() {
return this.sizeX;
}

private void createCard() {
this.createCardBackground();
this.createCardArt();
Expand Down Expand Up @@ -69,6 +86,7 @@ private void createCardArt() {
private void createCardIDLabel() {
Label cardIdLabel = new Label();
cardIdLabel.setText(String.format("CardID = %d", card.getId()));
cardIdLabel.relocate(this.sizeX*0.15,0); //moving this over so it is out of the way of the temporary button
cardIdLabel.setTextFill(Color.WHITE);
cardGroup.getChildren().add(cardIdLabel);
}
Expand All @@ -78,15 +96,12 @@ private void createCardPropertyLabelsGroup() {
int stringIndex = 0;
List<String> stringList = new ArrayList<>();
LuaTools.processLuaTable(card.data.checktable(), (k, v) -> stringList.add(k + ": " + v));
for (String string : stringList) {
Group cardTextStrings = new Group();
cardTextStrings.setTranslateY(25 + (stringIndex * 25));
cardGroup.getChildren().add(cardTextStrings);

for (String string : stringList) {
Label cardStringLabel = new Label();
cardStringLabel.setText(string);
cardStringLabel.relocate(0, 25 + (stringIndex * 25));
cardStringLabel.setTextFill(Color.WHITE);
cardTextStrings.getChildren().add(cardStringLabel);
cardGroup.getChildren().add(cardStringLabel);
stringIndex++;
}
}
Expand All @@ -105,29 +120,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;
}

//TODO: add a check to make sure the target is valid//
Targetable target = targets.get(targetIndex);
targetAction.setTarget(target);
targetAction.perform();
List<Card>targetCards = new ArrayList<>();
for(Targetable target : targets) {
if (target instanceof Card) {
targetCards.add((Card)target);
}
}
this.controller.markTargets(targetCards);
this.controller.nextAction = targetAction;
} else {
action.perform();
this.controller.createData();
}
}
else action.perform();
this.controller.render();
}
this.controller.render();
}
}

Expand Down
@@ -0,0 +1,220 @@
package com.cardshifter.fx;

import com.cardshifter.core.Card;
import com.cardshifter.core.LuaTools;
import com.cardshifter.core.TargetAction;
import com.cardshifter.core.Targetable;
import com.cardshifter.core.UsableAction;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javafx.event.ActionEvent;
import javafx.scene.Group;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

public class CardNodeBattlefield extends Group {

private final double sizeX;
private final double sizeY;
private final String name;
private final Card card;
private final FXMLGameController controller;
private final boolean isPlayer;

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();
//reduce card size if there are over a certain amount of them
int maxCards = Math.max(numCards, 8);
double cardWidth = paneWidth / maxCards;

this.sizeX = cardWidth;
this.sizeY = pane.getHeight();
this.name = name;
this.card = card;
this.controller = controller;
this.isPlayer = isPlayer;
this.createCard();
}

public Group getCardGroup() {
return this;
}

public double getWidth() {
return this.sizeX;
}

public Card getCard() {
return this.card;
}

private void createCard() {
this.createCardBackground(false);
this.createCardArt();
this.createCardIDLabel();
this.createCardPropertyLabelsGroup();

//only make the button when the card is active
if(this.isCardActive() == true) {
if (this.isPlayer == true) {
this.createCardActivateButton();
}
}
}

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);

//change the background color based on the state
//this cannot be elseif because multiple states could be true
if(this.isCardActive()) {
activeBackground.setFill(Color.YELLOW);
}
if(this.canCardAttack()) {
activeBackground.setFill(Color.GREEN);
}
if(targetMode) {
activeBackground.setFill(Color.BLUE);
}

this.getChildren().add(activeBackground);
}
private boolean isCardActive() {
List<UsableAction> cardActions = card.getActions().values().stream().filter(UsableAction::isAllowed).collect(Collectors.toList());
return cardActions.size() > 0;
}
private boolean canCardAttack() {
List<UsableAction> cardActions = card.getActions().values().stream().filter(UsableAction::isAllowed).collect(Collectors.toList());
for (UsableAction action : cardActions) {
if (action.getName().equals("Attack")) {
return true;
}
}
return false;
}

private void createCardArt() {
Rectangle cardBack = new Rectangle(0,0,this.sizeX*0.95,this.sizeY*0.95);
cardBack.setFill(Color.FIREBRICK);
this.getChildren().add(cardBack);
}

private void createCardIDLabel() {
Label cardIdLabel = new Label();
cardIdLabel.setText(String.format("CardID = %d", card.getId()));
cardIdLabel.relocate(this.sizeX*0.15,0); //moving this over so it is out of the way of the temporary button
cardIdLabel.setTextFill(Color.WHITE);
this.getChildren().add(cardIdLabel);
}

private void createCardPropertyLabelsGroup() {
//Only these values will be loaded from Lua, and only if they are contained in the data
int health = 0;
int strength = 0;
int manaCost = 0;

List<String> keyList = new ArrayList<>();
LuaTools.processLuaTable(card.data.checktable(), (k, v) -> keyList.add(k + ""));
for (String string : keyList) {
if(string.equals("health")) {
health = card.data.get("health").toint();
} else if (string.equals("strength")) {
strength = card.data.get("strength").toint();
} else if (string.equals("manaCost")) {
manaCost = card.data.get("manaCost").toint();
}
}

//Need separate code for each label because they are in arbitrary locations
Label strengthLabel = new Label();
strengthLabel.setText(String.format("%d/", strength)); // do the "/" to have something between strength and health
strengthLabel.relocate(this.sizeX*0.75,this.sizeY*0.80);
strengthLabel.setTextFill(Color.WHITE);
this.getChildren().add(strengthLabel);

Label healthLabel = new Label();
healthLabel.setText(String.format("%d", health));
healthLabel.relocate(this.sizeX*0.88,this.sizeY*0.80);
healthLabel.setTextFill(Color.WHITE);
this.getChildren().add(healthLabel);

Label manaCostLabel = new Label();
manaCostLabel.setText(String.format("Cost = %d", manaCost));
manaCostLabel.relocate(this.sizeX*0.50,this.sizeY*0.15);
manaCostLabel.setTextFill(Color.WHITE);
this.getChildren().add(manaCostLabel);
}

private void createCardActivateButton() {
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);
this.getChildren().add(button);
}
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());

//If there is more than one action, create the choice box
//Otherwise, the action will be automaticcally performed if there is only one
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;
}

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

//TARGETING
public void createTargetButton() {
this.getChildren().clear();
this.createCardBackground(true);
this.createCardArt();
this.createCardIDLabel();
this.createCardPropertyLabelsGroup();
this.createCardTargetButton();
}
private void createCardTargetButton() {
Button button = new Button();
button.setOnAction(this::targetButtonClick);
this.getChildren().add(button);
}
private void targetButtonClick(ActionEvent event) {
this.controller.performNextAction(this.card);
}
}

0 comments on commit beba9bc

Please sign in to comment.