From 933ac93f440c68b421c498f0761ecb45cd45f3cc Mon Sep 17 00:00:00 2001 From: Andy Russell Date: Sat, 1 Aug 2015 23:37:19 -0500 Subject: [PATCH] add field displaying level win conditions Fixes #30. --- .../roomfullofcats/GameController.java | 11 ++-- .../gamefolk/roomfullofcats/game/Game.java | 52 +++++++++++++++++++ app/src/main/resources/fxml/game.fxml | 10 ++-- 3 files changed, 67 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/org/gamefolk/roomfullofcats/GameController.java b/app/src/main/java/org/gamefolk/roomfullofcats/GameController.java index ed903fb..e64478f 100644 --- a/app/src/main/java/org/gamefolk/roomfullofcats/GameController.java +++ b/app/src/main/java/org/gamefolk/roomfullofcats/GameController.java @@ -10,6 +10,7 @@ import javafx.scene.input.MouseEvent; import javafx.scene.layout.Pane; import javafx.scene.text.Text; +import javafx.stage.Stage; import javafx.util.Duration; import javafx.util.converter.NumberStringConverter; import org.gamefolk.roomfullofcats.game.CatType; @@ -33,6 +34,7 @@ public class GameController implements Initializable { @FXML private Text message; @FXML private Text score; @FXML private Text time; + @FXML private Text goal; public GraphicsContext getGraphicsContext2D() { return canvas.getGraphicsContext2D(); @@ -41,12 +43,18 @@ public GraphicsContext getGraphicsContext2D() { public void startGame(Level level) { Log.info("Starting game."); + // Make sure the canvas is the correct width. + Stage stage = (Stage) root.getScene().getWindow(); + canvas.widthProperty().set(stage.getWidth()); + canvas.heightProperty().set(stage.getHeight() / 2); + game = new Game(getGraphicsContext2D()); game.setLevel(level); game.playMusic(); Bindings.bindBidirectional(score.textProperty(), game.scoreProperty(), new NumberStringConverter()); Bindings.bindBidirectional(time.textProperty(), game.timerProperty()); + Bindings.bindBidirectional(goal.textProperty(), game.goalProperty()); message.setWrappingWidth(root.getWidth() * 0.75); @@ -115,9 +123,6 @@ protected void interpolate(double frac) { @Override public void initialize(URL url, ResourceBundle resourceBundle) { loadResources(); - - canvas.heightProperty().bind(gameView.heightProperty()); - canvas.widthProperty().bind(gameView.widthProperty()); } private void loadResources() { diff --git a/app/src/main/java/org/gamefolk/roomfullofcats/game/Game.java b/app/src/main/java/org/gamefolk/roomfullofcats/game/Game.java index 45d3df8..cfcf8c4 100644 --- a/app/src/main/java/org/gamefolk/roomfullofcats/game/Game.java +++ b/app/src/main/java/org/gamefolk/roomfullofcats/game/Game.java @@ -16,6 +16,7 @@ import org.joda.time.format.PeriodFormatter; import org.joda.time.format.PeriodFormatterBuilder; +import java.util.Map; import java.util.logging.Logger; public class Game { @@ -27,6 +28,7 @@ public class Game { private final Settings settings = Settings.INSTANCE; private IntegerProperty score = new SimpleIntegerProperty(0); private StringProperty timer = new SimpleStringProperty(); + private StringProperty goal = new SimpleStringProperty(); private Interval gameTime; private Level currentLevel; private Cat[][] map; @@ -74,6 +76,10 @@ public StringProperty timerProperty() { return timer; } + public StringProperty goalProperty() { + return goal; + } + private Rectangle2D getCatBounds(int x, int y) { double xCoordinate = mapOrigin.getX() + catSize.getWidth() * x; double yCoordinate = mapOrigin.getY() + catSize.getHeight() * y; @@ -105,6 +111,52 @@ public void setLevel(Level level) { Log.info("Cat size set to " + catSize); Log.info("Map origin set to " + mapOrigin); + + goal.set(getGoal()); + } + + private String getGoal() { + PeriodFormatter levelTimeFormatter = new PeriodFormatterBuilder() + .appendMinutes() + .appendSuffix(" minute", " minutes") + .appendSeparator(" and ") + .appendSeconds() + .appendSuffix(" second", " seconds") + .toFormatter(); + + StringBuilder goal = new StringBuilder(); + goal.append(String.format("You need to get %d points.\n", currentLevel.requiredScore)); + goal.append(String.format("You have %s.\n", levelTimeFormatter.print(currentLevel.timeLimit.toPeriod()))); + if (currentLevel.fallTime.getMillis() < Level.DEFAULT_FALL_TIME) { + goal.append("Cats will fall faster than normal.\n"); + } else if (currentLevel.fallTime.getMillis() > Level.DEFAULT_FALL_TIME) { + goal.append("Cats will fall slower than normal.\n"); + } + + if (currentLevel.catsLimit != Level.DEFAULT_CATS_LIMIT) { + goal.append(String.format("%d cats fit in a basket.\n", currentLevel.catsLimit)); + } + + // Do we want to display remaining moves here? + if (currentLevel.moveLimit != Level.DEFAULT_MOVE_LIMIT) { + goal.append(String.format("You have only %d moves.\n", currentLevel.moveLimit)); + } + + // TODO: Make this print out nicer. + if (currentLevel.requiredMatches.size() > 0) { + goal.append("You need to get "); + for (Map.Entry entry : currentLevel.requiredMatches.entrySet()) { + CatType type = entry.getKey(); + int num = entry.getValue(); + + goal.append(String.format("%d %s match", num, type.toString())); + if (num > 1) { + goal.append("es"); + } + } + } + + return goal.toString(); } public void playMusic() { diff --git a/app/src/main/resources/fxml/game.fxml b/app/src/main/resources/fxml/game.fxml index 5d0d2c1..b91593a 100644 --- a/app/src/main/resources/fxml/game.fxml +++ b/app/src/main/resources/fxml/game.fxml @@ -20,9 +20,13 @@
- - - + + + + + + +