Skip to content

Commit

Permalink
add field displaying level win conditions
Browse files Browse the repository at this point in the history
Fixes #30.
  • Loading branch information
euclio committed Aug 2, 2015
1 parent b98a084 commit 933ac93
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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);

Expand Down Expand Up @@ -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() {
Expand Down
52 changes: 52 additions & 0 deletions app/src/main/java/org/gamefolk/roomfullofcats/game/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<CatType, Integer> 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() {
Expand Down
10 changes: 7 additions & 3 deletions app/src/main/resources/fxml/game.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@
</top>
<center>
<StackPane onMouseClicked="#handleInput">
<Pane fx:id="gameView">
<Canvas fx:id="canvas"/>
</Pane>
<VBox fx:id="gameView">
<Canvas fx:id="canvas" VBox.vgrow="ALWAYS" />
<VBox alignment="CENTER" VBox.vgrow="ALWAYS">
<Text text="Goal:" />
<Text fx:id="goal" VBox.vgrow="ALWAYS" textAlignment="CENTER" />
</VBox>
</VBox>
<BorderPane>
<center>
<VBox alignment="CENTER">
Expand Down

0 comments on commit 933ac93

Please sign in to comment.