From cb66da946d05e505d22b099932ccc1ffc2c88499 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Fri, 17 Apr 2020 13:01:54 +0200 Subject: [PATCH] Splitter opp metoder for bedre leselighet MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../gamewrapper/screens/CardChoiceScreen.java | 99 +++++++++++-------- 1 file changed, 60 insertions(+), 39 deletions(-) diff --git a/src/main/java/inf112/fiasko/roborally/gamewrapper/screens/CardChoiceScreen.java b/src/main/java/inf112/fiasko/roborally/gamewrapper/screens/CardChoiceScreen.java index 949472c..4b7644d 100644 --- a/src/main/java/inf112/fiasko/roborally/gamewrapper/screens/CardChoiceScreen.java +++ b/src/main/java/inf112/fiasko/roborally/gamewrapper/screens/CardChoiceScreen.java @@ -8,6 +8,7 @@ import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.g2d.GlyphLayout; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; +import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Vector3; import com.badlogic.gdx.scenes.scene2d.Stage; 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.gamewrapper.RoboRallyWrapper; import inf112.fiasko.roborally.gamewrapper.SimpleButton; -import inf112.fiasko.roborally.objects.IDeck; import inf112.fiasko.roborally.objects.ProgrammingCard; import inf112.fiasko.roborally.objects.ProgrammingCardDeck; -import inf112.fiasko.roborally.utility.DeckLoaderUtil; -import java.awt.*; -import java.io.IOException; +import javax.swing.JOptionPane; import java.util.ArrayList; 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.WHITE; @@ -46,18 +44,18 @@ public class CardChoiceScreen extends InputAdapter implements Screen { private final int maxCards; private final Stage stage; private final InputMultiplexer inputMultiplexer; - private final ProgrammingCardDeck deck; /** * Instantiates a new card choice screen * @param roboRallyWrapper The Robo Rally wrapper which is parent of this screen */ public CardChoiceScreen(final RoboRallyWrapper roboRallyWrapper) { - this.deck = roboRallyWrapper.roboRallyGame.getPlayerHand(); + ProgrammingCardDeck deck = roboRallyWrapper.roboRallyGame.getPlayerHand(); this.roboRallyWrapper = roboRallyWrapper; camera = new OrthographicCamera(); int applicationWidth = 600; int applicationHeight = 800; + this.maxCards = roboRallyWrapper.roboRallyGame.getProgramSize(); camera.setToOrtho(false, applicationWidth, applicationHeight); viewport = new FitViewport(applicationWidth, applicationHeight, camera); cardRectangles = new ArrayList<>(); @@ -66,14 +64,9 @@ public class CardChoiceScreen extends InputAdapter implements Screen { inputMultiplexer = new InputMultiplexer(); - try { - generateCards(deck); - } catch (IOException e) { - e.printStackTrace(); - } + generateCards(deck); this.chosenCards = new ArrayList<>(); - this.maxCards = roboRallyWrapper.roboRallyGame.getProgramSize(); stage = new Stage(); 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.setX(15); - confirmCards.addListener(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; - } - return false; - } - }); + confirmCards.addListener(getConfirmListener()); stage.setViewport(viewport); inputMultiplexer.addProcessor(this); inputMultiplexer.addProcessor(stage); } - private List 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 getCards() { List program = new ArrayList<>(); chosenCards.forEach((cardRectangle -> program.add(cardRectangle.card))); return program; } /** - * Generates some placeholder cards for testing - * @throws IOException If programming cards cannot be loaded + * Calculates positions for cards in the given deck + * @param deck A deck containing cards which can be chosen */ - private void generateCards(ProgrammingCardDeck deck) throws IOException { - //Get 9 programming cards + private void generateCards(ProgrammingCardDeck deck) { List cardList = deck.getCards(); float cardWidth = viewport.getWorldWidth() / 3; float cardHeight = (viewport.getWorldHeight() - 30) / 3; - for (int i = 0; i < 9; i++) { - int x = (int)(((i % 3)*cardWidth) + 10); + 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; @@ -141,28 +148,42 @@ public class CardChoiceScreen extends InputAdapter implements Screen { shapeRenderer.setProjectionMatrix(camera.combined); 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) { if (cardRectangle.selected) { shapeRenderer.setColor(RED); shapeRenderer.rect(cardRectangle.rectangle.x - 10, cardRectangle.rectangle.y - 10, cardRectangle.rectangle.width + 20, cardRectangle.rectangle.height + 20); } - shapeRenderer.setColor(GREEN); + shapeRenderer.setColor(GRAY); shapeRenderer.rect(cardRectangle.rectangle.x, cardRectangle.rectangle.y, cardRectangle.rectangle.width, cardRectangle.rectangle.height); } + } - shapeRenderer.end(); - roboRallyWrapper.batch.begin(); + /** + * Renders the text displayed on cards + */ + private void renderCardText() { 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 fontY = cardRectangle.rectangle.y + cardRectangle.rectangle.height - 30; roboRallyWrapper.font.draw(roboRallyWrapper.batch, layout, fontX, fontY); drawCardSymbol(cardRectangle); } - roboRallyWrapper.batch.end(); - stage.draw(); } /**