Splitter opp metoder for bedre leselighet

Fikser en bug der størrelsen på kortstokken ikke blir tatt hensyn til
Legger til en informasjonsboks som forteller brukeren at de ikke har valgt nok kort
This commit is contained in:
Kristian Knarvik 2020-04-17 13:01:54 +02:00
parent 59a63a7ab8
commit cb66da946d

View File

@ -8,6 +8,7 @@ import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.GlyphLayout; import com.badlogic.gdx.graphics.g2d.GlyphLayout;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3; import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.InputListener; import com.badlogic.gdx.scenes.scene2d.InputListener;
@ -18,17 +19,14 @@ import com.badlogic.gdx.utils.viewport.Viewport;
import inf112.fiasko.roborally.elementproperties.GameState; import inf112.fiasko.roborally.elementproperties.GameState;
import inf112.fiasko.roborally.gamewrapper.RoboRallyWrapper; import inf112.fiasko.roborally.gamewrapper.RoboRallyWrapper;
import inf112.fiasko.roborally.gamewrapper.SimpleButton; import inf112.fiasko.roborally.gamewrapper.SimpleButton;
import inf112.fiasko.roborally.objects.IDeck;
import inf112.fiasko.roborally.objects.ProgrammingCard; import inf112.fiasko.roborally.objects.ProgrammingCard;
import inf112.fiasko.roborally.objects.ProgrammingCardDeck; import inf112.fiasko.roborally.objects.ProgrammingCardDeck;
import inf112.fiasko.roborally.utility.DeckLoaderUtil;
import java.awt.*; import javax.swing.JOptionPane;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static com.badlogic.gdx.graphics.Color.GREEN; import static com.badlogic.gdx.graphics.Color.GRAY;
import static com.badlogic.gdx.graphics.Color.RED; import static com.badlogic.gdx.graphics.Color.RED;
import static com.badlogic.gdx.graphics.Color.WHITE; import static com.badlogic.gdx.graphics.Color.WHITE;
@ -46,18 +44,18 @@ public class CardChoiceScreen extends InputAdapter implements Screen {
private final int maxCards; private final int maxCards;
private final Stage stage; private final Stage stage;
private final InputMultiplexer inputMultiplexer; private final InputMultiplexer inputMultiplexer;
private final ProgrammingCardDeck deck;
/** /**
* Instantiates a new card choice screen * Instantiates a new card choice screen
* @param roboRallyWrapper The Robo Rally wrapper which is parent of this screen * @param roboRallyWrapper The Robo Rally wrapper which is parent of this screen
*/ */
public CardChoiceScreen(final RoboRallyWrapper roboRallyWrapper) { public CardChoiceScreen(final RoboRallyWrapper roboRallyWrapper) {
this.deck = roboRallyWrapper.roboRallyGame.getPlayerHand(); ProgrammingCardDeck deck = roboRallyWrapper.roboRallyGame.getPlayerHand();
this.roboRallyWrapper = roboRallyWrapper; this.roboRallyWrapper = roboRallyWrapper;
camera = new OrthographicCamera(); camera = new OrthographicCamera();
int applicationWidth = 600; int applicationWidth = 600;
int applicationHeight = 800; int applicationHeight = 800;
this.maxCards = roboRallyWrapper.roboRallyGame.getProgramSize();
camera.setToOrtho(false, applicationWidth, applicationHeight); camera.setToOrtho(false, applicationWidth, applicationHeight);
viewport = new FitViewport(applicationWidth, applicationHeight, camera); viewport = new FitViewport(applicationWidth, applicationHeight, camera);
cardRectangles = new ArrayList<>(); cardRectangles = new ArrayList<>();
@ -66,14 +64,9 @@ public class CardChoiceScreen extends InputAdapter implements Screen {
inputMultiplexer = new InputMultiplexer(); inputMultiplexer = new InputMultiplexer();
try { generateCards(deck);
generateCards(deck);
} catch (IOException e) {
e.printStackTrace();
}
this.chosenCards = new ArrayList<>(); this.chosenCards = new ArrayList<>();
this.maxCards = roboRallyWrapper.roboRallyGame.getProgramSize();
stage = new Stage(); stage = new Stage();
TextButton confirmCards = new SimpleButton("Confirm cards", roboRallyWrapper.font).getButton(); TextButton confirmCards = new SimpleButton("Confirm cards", roboRallyWrapper.font).getButton();
@ -81,40 +74,54 @@ public class CardChoiceScreen extends InputAdapter implements Screen {
confirmCards.setY(viewport.getWorldHeight() - confirmCards.getHeight()); confirmCards.setY(viewport.getWorldHeight() - confirmCards.getHeight());
confirmCards.setX(15); confirmCards.setX(15);
confirmCards.addListener(new InputListener() { confirmCards.addListener(getConfirmListener());
@Override
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
if (chosenCards.size() == maxCards) {
roboRallyWrapper.roboRallyGame.setProgram(getCards());
roboRallyWrapper.roboRallyGame.setGameState(GameState.CHOOSING_POWER_DOWN);
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getPowerDownScreen(roboRallyWrapper));
return true;
}
return false;
}
});
stage.setViewport(viewport); stage.setViewport(viewport);
inputMultiplexer.addProcessor(this); inputMultiplexer.addProcessor(this);
inputMultiplexer.addProcessor(stage); inputMultiplexer.addProcessor(stage);
} }
private List<ProgrammingCard> getCards(){
/**
* Generates a listener for confirming cards
* @return An input listener
*/
private InputListener getConfirmListener() {
return new InputListener() {
@Override
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
if (chosenCards.size() == maxCards) {
roboRallyWrapper.roboRallyGame.setProgram(getCards());
roboRallyWrapper.roboRallyGame.setGameState(GameState.CHOOSING_POWER_DOWN);
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getPowerDownScreen(roboRallyWrapper));
return true;
} else {
JOptionPane.showMessageDialog(null, "You need to choose all your cards"
+ " before confirming.");
}
return false;
}
};
}
/**
* Gets a list of programming cards from the player's chosen cards
* @return A list of programming cards
*/
private List<ProgrammingCard> getCards() {
List<ProgrammingCard> program = new ArrayList<>(); List<ProgrammingCard> program = new ArrayList<>();
chosenCards.forEach((cardRectangle -> program.add(cardRectangle.card))); chosenCards.forEach((cardRectangle -> program.add(cardRectangle.card)));
return program; return program;
} }
/** /**
* Generates some placeholder cards for testing * Calculates positions for cards in the given deck
* @throws IOException If programming cards cannot be loaded * @param deck A deck containing cards which can be chosen
*/ */
private void generateCards(ProgrammingCardDeck deck) throws IOException { private void generateCards(ProgrammingCardDeck deck) {
//Get 9 programming cards
List<ProgrammingCard> cardList = deck.getCards(); List<ProgrammingCard> cardList = deck.getCards();
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 < 9; i++) { for (int i = 0; i < cardList.size(); i++) {
int x = (int)(((i % 3)*cardWidth) + 10); int x = (int)(((i % 3) * cardWidth) + 10);
int y = (int)(((i / 3) * cardHeight + 10)); int y = (int)(((i / 3) * cardHeight + 10));
Rectangle card = new Rectangle(); Rectangle card = new Rectangle();
card.x = x; card.x = x;
@ -141,28 +148,42 @@ public class CardChoiceScreen extends InputAdapter implements Screen {
shapeRenderer.setProjectionMatrix(camera.combined); shapeRenderer.setProjectionMatrix(camera.combined);
shapeRenderer.begin(ShapeRenderer.ShapeType.Filled); shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
renderCards();
shapeRenderer.end();
roboRallyWrapper.batch.begin();
renderCardText();
roboRallyWrapper.batch.end();
stage.draw();
}
/**
* Renders the base shape of cards
*/
private void renderCards() {
for (CardRectangle cardRectangle : cardRectangles) { for (CardRectangle cardRectangle : cardRectangles) {
if (cardRectangle.selected) { if (cardRectangle.selected) {
shapeRenderer.setColor(RED); shapeRenderer.setColor(RED);
shapeRenderer.rect(cardRectangle.rectangle.x - 10, cardRectangle.rectangle.y - 10, shapeRenderer.rect(cardRectangle.rectangle.x - 10, cardRectangle.rectangle.y - 10,
cardRectangle.rectangle.width + 20, cardRectangle.rectangle.height + 20); cardRectangle.rectangle.width + 20, cardRectangle.rectangle.height + 20);
} }
shapeRenderer.setColor(GREEN); shapeRenderer.setColor(GRAY);
shapeRenderer.rect(cardRectangle.rectangle.x, cardRectangle.rectangle.y, shapeRenderer.rect(cardRectangle.rectangle.x, cardRectangle.rectangle.y,
cardRectangle.rectangle.width, cardRectangle.rectangle.height); cardRectangle.rectangle.width, cardRectangle.rectangle.height);
} }
}
shapeRenderer.end(); /**
roboRallyWrapper.batch.begin(); * Renders the text displayed on cards
*/
private void renderCardText() {
for (CardRectangle cardRectangle : cardRectangles) { for (CardRectangle cardRectangle : cardRectangles) {
GlyphLayout layout = new GlyphLayout(roboRallyWrapper.font, Integer.toString(cardRectangle.card.getPriority())); GlyphLayout layout = new GlyphLayout(roboRallyWrapper.font,
Integer.toString(cardRectangle.card.getPriority()));
float fontX = (int)(cardRectangle.rectangle.x + (cardRectangle.rectangle.width - layout.width) / 2.0); float fontX = (int)(cardRectangle.rectangle.x + (cardRectangle.rectangle.width - layout.width) / 2.0);
float fontY = cardRectangle.rectangle.y + cardRectangle.rectangle.height - 30; float fontY = cardRectangle.rectangle.y + cardRectangle.rectangle.height - 30;
roboRallyWrapper.font.draw(roboRallyWrapper.batch, layout, fontX, fontY); roboRallyWrapper.font.draw(roboRallyWrapper.batch, layout, fontX, fontY);
drawCardSymbol(cardRectangle); drawCardSymbol(cardRectangle);
} }
roboRallyWrapper.batch.end();
stage.draw();
} }
/** /**