Legger til visning av låste kort Closes #84

This commit is contained in:
Kristian Knarvik 2020-04-29 12:25:24 +02:00
parent 4aae3663ad
commit e5260a684d
4 changed files with 64 additions and 18 deletions

View File

@ -122,12 +122,7 @@ public class CardChoiceScreen extends InteractiveScreen implements Screen {
*/ */
private void confirmCards(Boolean requestPowerDown) { private void confirmCards(Boolean requestPowerDown) {
if (chosenCards.size() == maxCards) { if (chosenCards.size() == maxCards) {
List<ProgrammingCard> oldProgram = roboRallyWrapper.roboRallyGame.getProgram(); List<ProgrammingCard> newProgram = getChosenAndLockedCards();
int lockedCardsInt = 5 - maxCards;
List<ProgrammingCard> newProgram = getCards();
for (int i = 4; i > (4 - lockedCardsInt); i--) {
newProgram.add(oldProgram.get(i));
}
//Save the program to get locked cards later //Save the program to get locked cards later
roboRallyWrapper.roboRallyGame.setProgram(newProgram); roboRallyWrapper.roboRallyGame.setProgram(newProgram);
roboRallyWrapper.roboRallyGame.setGameState(GameState.WAITING_FOR_OTHER_PLAYERS_PROGRAMS); roboRallyWrapper.roboRallyGame.setGameState(GameState.WAITING_FOR_OTHER_PLAYERS_PROGRAMS);
@ -139,6 +134,15 @@ public class CardChoiceScreen extends InteractiveScreen implements Screen {
} }
} }
/**
* Gets the number of locked cards
*
* @return The number of locked cards
*/
private int getNumberOfLockedCards() {
return 5 - maxCards;
}
/** /**
* Gets a list of programming cards from the player's chosen cards * Gets a list of programming cards from the player's chosen cards
* *
@ -160,17 +164,39 @@ public class CardChoiceScreen extends InteractiveScreen implements Screen {
float cardWidth = viewport.getWorldWidth() / 3; float cardWidth = viewport.getWorldWidth() / 3;
float cardHeight = (viewport.getWorldHeight() - 30) / 3; float cardHeight = (viewport.getWorldHeight() - 30) / 3;
for (int i = 0; i < cardList.size(); i++) { for (int i = 0; i < cardList.size(); i++) {
int x = (int) (((i % 3) * cardWidth) + 10);
int y = (int) (((i / 3) * cardHeight + 10));
Rectangle card = new Rectangle();
card.x = x;
card.y = y;
card.width = (int) cardWidth - 20;
card.height = (int) cardHeight - 20;
ProgrammingCard programmingCard = cardList.get(i); ProgrammingCard programmingCard = cardList.get(i);
CardRectangle cardRectangle = new CardRectangle(card, programmingCard); generateCardRectangle(i, cardWidth, cardHeight, programmingCard, true);
cardRectangles.add(cardRectangle);
} }
List<ProgrammingCard> oldProgram = roboRallyWrapper.roboRallyGame.getProgram();
for (int i = cardList.size(); i < cardList.size() + getNumberOfLockedCards(); i++) {
ProgrammingCard programmingCard = oldProgram.get(4 - (i - cardList.size()));
generateCardRectangle(i, cardWidth, cardHeight, programmingCard, false);
}
}
/**
* Adds a new card rectangle containing the appropriate data
*
* @param i The index of the card rectangle relative to other card rectangles
* @param cardWidth The width of the card rectangle
* @param cardHeight The height of the card rectangle
* @param programmingCard The programming card belonging to the card rectangle
* @param selectable Whether the card rectangle is selectable
*/
private void generateCardRectangle(int i, float cardWidth, float cardHeight, ProgrammingCard programmingCard,
boolean selectable) {
int x = (int) (((i % 3) * cardWidth) + 10);
int y = (int) (((i / 3) * cardHeight + 10));
Rectangle card = new Rectangle();
card.x = x;
card.y = y;
card.width = (int) cardWidth - 20;
card.height = (int) cardHeight - 20;
CardRectangle cardRectangle = new CardRectangle(card, programmingCard);
cardRectangle.selectable = selectable;
cardRectangle.selected = !selectable;
cardRectangles.add(cardRectangle);
} }
@Override @Override
@ -236,6 +262,10 @@ public class CardChoiceScreen extends InteractiveScreen implements Screen {
float fontY = cardRectangle.rectangle.y + cardRectangle.rectangle.height - 21; float fontY = cardRectangle.rectangle.y + cardRectangle.rectangle.height - 21;
roboRallyWrapper.font.draw(roboRallyWrapper.batch, layout, fontX, fontY); roboRallyWrapper.font.draw(roboRallyWrapper.batch, layout, fontX, fontY);
int chosenIndex = chosenCards.indexOf(cardRectangle); int chosenIndex = chosenCards.indexOf(cardRectangle);
int totalIndex = getChosenAndLockedCards().indexOf(cardRectangle.card);
if (chosenIndex == -1 && totalIndex != -1) {
chosenIndex = totalIndex + maxCards - chosenCards.size();
}
if (chosenIndex != -1) { if (chosenIndex != -1) {
roboRallyWrapper.font.setColor(YELLOW); roboRallyWrapper.font.setColor(YELLOW);
roboRallyWrapper.font.draw(roboRallyWrapper.batch, String.valueOf(chosenIndex + 1), roboRallyWrapper.font.draw(roboRallyWrapper.batch, String.valueOf(chosenIndex + 1),
@ -246,6 +276,21 @@ public class CardChoiceScreen extends InteractiveScreen implements Screen {
roboRallyWrapper.font.setColor(WHITE); roboRallyWrapper.font.setColor(WHITE);
} }
/**
* Gets a list containing all chosen and locked programming cards
*
* @return A list of programming cards
*/
private List<ProgrammingCard> getChosenAndLockedCards() {
List<ProgrammingCard> oldProgram = roboRallyWrapper.roboRallyGame.getProgram();
int lockedCardsInt = getNumberOfLockedCards();
List<ProgrammingCard> newProgram = new ArrayList<>(getCards());
for (int i = 4; i > (4 - lockedCardsInt); i--) {
newProgram.add(oldProgram.get(i));
}
return newProgram;
}
@Override @Override
public void resize(int width, int height) { public void resize(int width, int height) {
viewport.update(width, height); viewport.update(width, height);
@ -275,7 +320,7 @@ public class CardChoiceScreen extends InteractiveScreen implements Screen {
public boolean touchUp(int screenX, int screenY, int pointer, int button) { public boolean touchUp(int screenX, int screenY, int pointer, int button) {
Vector3 transformed = viewport.unproject(new Vector3(screenX, screenY, 0)); Vector3 transformed = viewport.unproject(new Vector3(screenX, screenY, 0));
for (CardRectangle cardRectangle : cardRectangles) { for (CardRectangle cardRectangle : cardRectangles) {
if (cardRectangle.rectangle.contains(transformed.x, transformed.y)) { if (cardRectangle.rectangle.contains(transformed.x, transformed.y) && cardRectangle.selectable) {
if (!cardRectangle.selected && chosenCards.size() < maxCards) { if (!cardRectangle.selected && chosenCards.size() < maxCards) {
chosenCards.add(cardRectangle); chosenCards.add(cardRectangle);
cardRectangle.selected = true; cardRectangle.selected = true;

View File

@ -10,6 +10,7 @@ public class CardRectangle {
protected final Rectangle rectangle; protected final Rectangle rectangle;
protected final ProgrammingCard card; protected final ProgrammingCard card;
protected boolean selected = false; protected boolean selected = false;
protected boolean selectable = true;
/** /**
* Instantiates a new card rectangle * Instantiates a new card rectangle

View File

@ -11,7 +11,7 @@ import java.util.List;
/** /**
* A helper class containing helper methods fro a grid * A helper class containing helper methods fro a grid
*/ */
public class GridUtil { public final class GridUtil {
/** /**
* Gets all elements in a grid * Gets all elements in a grid

View File

@ -8,7 +8,7 @@ import inf112.fiasko.roborally.objects.Particle;
/** /**
* Helps with displaying laser beams * Helps with displaying laser beams
*/ */
public class LaserHelper { public final class LaserHelper {
/** /**
* Gets the correct particle type from a laser type * Gets the correct particle type from a laser type