Skip to content

Commit

Permalink
Added PlayerHandZoneView subclass, you can now play cards from your h…
Browse files Browse the repository at this point in the history
…and by clicking on them
  • Loading branch information
bazola committed Sep 19, 2014
1 parent 99c1af8 commit 2541d82
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.cardshifter.client;

import com.cardshifter.server.outgoing.CardInfoMessage;
import com.cardshifter.server.outgoing.UseableActionMessage;
import java.net.URL;
import java.util.ResourceBundle;

Expand All @@ -27,9 +28,11 @@ public class CardHandDocumentController implements Initializable {
@FXML private Rectangle background;
@FXML private AnchorPane anchorPane;

//Initialization
private AnchorPane root;
private final CardInfoMessage card;
private final GameClientController controller;
private UseableActionMessage message;

public CardHandDocumentController(CardInfoMessage message, GameClientController controller) {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("CardHandDocument.fxml"));
Expand All @@ -41,25 +44,25 @@ public CardHandDocumentController(CardInfoMessage message, GameClientController
}

this.card = message;
this.controller = controller;
this.setCardId();
this.setCardLabels();
this.setActionForRootPane();
}

private void setActionForRootPane() {
this.anchorPane.setOnMouseClicked(this::actionOnClick);
}
private void actionOnClick(MouseEvent event) {
System.out.println("Action detected on card" + this.cardId.textProperty());
}

public AnchorPane getRootPane() {
return this.anchorPane;
}

public void setRectangleColorActive() {
public void setCardActive(UseableActionMessage message) {
this.message = message;
this.anchorPane.setOnMouseClicked(this::actionOnClick);
background.setFill(Color.YELLOW);
}

private void actionOnClick(MouseEvent event) {
System.out.println("Action detected on card" + this.cardId.textProperty());
this.controller.createAndSendMessage(this.message);
}

private void setCardId() {
int newId = card.getId();
Expand All @@ -80,38 +83,6 @@ private void setCardLabels() {

}
}

/*
List<ECSResourceData> keyList = new ArrayList<>();
Resources.processResources(card, data -> keyList.add(data));
for (ECSResourceData string : keyList) {
ECSResource resource = string.getResource();
int value = string.get();
if (resource == PhrancisResources.HEALTH) {
health.setText(String.valueOf(string.get()));
} else if (resource == PhrancisResources.ATTACK) {
strength.setText(String.valueOf(string.get()));
} else if (resource == PhrancisResources.MANA_COST) {
manaCost.setText(String.format("Mana Cost = %d", string.get()));
} else if (resource == PhrancisResources.SCRAP_COST) {
scrapCost.setText(String.format("Scrap Cost = %d", value));
}
// } else if (resource == PhrancisResources.st string.equals("enchStrength")) {
// enchStrength.setText(String.format("Enchantment Strength = %d", card.data.get("enchStrength").toint()));
// } else if (string.equals("enchHealth")) {
// enchHealth.setText(String.format("Enchantment Health = %d", card.data.get("enchHealth").toint()));
// }
}
if (card.hasComponent(CreatureTypeComponent.class)) {
creatureType.setText(card.getComponent(CreatureTypeComponent.class).getCreatureType());
}
// } else if (string.equals("cardType")) {
// cardType.setText(String.format(card.data.get("cardType").tojstring()));
*/
}

//Boilerplate code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ private void assignZoneIdForZoneMessage(ZoneMessage message) {
} else if (message.getName().equals("Hand")) {
if (message.getOwner() == this.playerId) {
this.playerHandId = message.getId();
this.zoneViewMap.put(message.getId(), new ZoneView(message.getId(), playerHandPane));
this.zoneViewMap.put(message.getId(), new PlayerHandZoneView(message.getId(), playerHandPane));
} else {
this.opponentHandId = message.getId();
this.zoneViewMap.put(this.opponentHandId, new ZoneView(message.getId(), opponentHandPane));
Expand Down Expand Up @@ -290,9 +290,9 @@ private void addCardToOpponentHandPane(CardInfoMessage message) {
private void addCardToPlayerBattlefieldPane(CardInfoMessage message) {
}
private void addCardToPlayerHandPane(CardInfoMessage message) {
ZoneView playerHand = this.zoneViewMap.get(playerHandId);
PlayerHandZoneView playerHand = (PlayerHandZoneView)this.zoneViewMap.get(playerHandId);
CardHandDocumentController card = new CardHandDocumentController(message, this);
playerHand.addPane(message.getId(), card.getRootPane());
playerHand.addCardHandController(message.getId(), card);
}

private void processUseableActionMessage(UseableActionMessage message) {
Expand All @@ -302,13 +302,17 @@ private void processUseableActionMessage(UseableActionMessage message) {
int maxActions = 8;
double actionWidth = paneWidth / maxActions;

ActionButton actionButton = new ActionButton(message, this, actionWidth, paneHeight);
actionBox.getChildren().add(actionButton);
if (message.getAction().equals("End Turn")) {
ActionButton actionButton = new ActionButton(message, this, actionWidth, paneHeight);
actionBox.getChildren().add(actionButton);
}

//testing highlighting cards
for (ZoneView zoneView : this.zoneViewMap.values()) {
if (zoneView.getAllIds().contains(message.getId())) {
zoneView.highlightCard(message.getId());
if (zoneView instanceof PlayerHandZoneView) {
((PlayerHandZoneView)zoneView).highlightCard(message.getId(), message);
}
}
}
}
Expand Down Expand Up @@ -341,13 +345,13 @@ private void processZoneChangeMessage(ZoneChangeMessage message) {
if (sourceZoneId == opponentHandId) {

} else if (sourceZoneId == playerHandId) {
ZoneView sourceZone = this.zoneViewMap.get(sourceZoneId);
Pane cardPane = sourceZone.getPane(cardId);
PlayerHandZoneView sourceZone = (PlayerHandZoneView)this.zoneViewMap.get(sourceZoneId);
CardHandDocumentController card = sourceZone.getCardHandController(cardId);

ZoneView destinationZone = this.zoneViewMap.get(destinationZoneId);
destinationZone.addPane(cardId, cardPane);
destinationZone.addPane(cardId, card.getRootPane());

sourceZone.removePane(cardId);
sourceZone.removeCardHandDocumentController(cardId);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.cardshifter.client;

import com.cardshifter.server.outgoing.UseableActionMessage;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javafx.scene.layout.Pane;

public class PlayerHandZoneView extends ZoneView {

private final Map<Integer, CardHandDocumentController> cardMap = new HashMap<>();

public PlayerHandZoneView(int cardId, Pane pane) {
super(cardId, pane);
}

public void addCardHandController(int cardId, CardHandDocumentController controller) {
this.cardMap.put(cardId, controller);
super.addPane(cardId, controller.getRootPane());
}

public void removeCardHandDocumentController(int cardId) {
super.removePane(cardId);
this.cardMap.remove(cardId);
}

public CardHandDocumentController getCardHandController(int cardId) {
return this.cardMap.get(cardId);
}

@Override
public int getSize() {
return this.cardMap.size();
}

@Override
public Set getAllIds() {
return this.cardMap.keySet();
}

public void highlightCard(int cardId, UseableActionMessage message) {
CardHandDocumentController card = this.cardMap.get(cardId);
card.setCardActive(message);
super.removePane(cardId);
super.addPane(cardId, card.getRootPane());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class ZoneView {

private final int zoneId;
private final Pane rootPane;
private final Map<Integer, Pane> zoneMap = new HashMap<>();
public final Map<Integer, Pane> zoneMap = new HashMap<>();

public ZoneView(int zoneId, Pane pane) {
this.zoneId = zoneId;
Expand Down Expand Up @@ -51,6 +51,7 @@ public Set getAllIds() {
return this.zoneMap.keySet();
}

//This causes a Null Pointer Exception, don't know why
public void highlightCard(int cardId) {
Pane pane = this.getPane(cardId);
List<Node> children = pane.getChildren();
Expand Down

0 comments on commit 2541d82

Please sign in to comment.