Esempio n. 1
0
  /**
   * Generate new colors. This method is executed by the callback action when the player selects a
   * valid rectangle. The purpose of this method is to regenerate the board and apply any required
   * checks to make sure that the game doesn't enter in an infinite loop.
   *
   * @param bounds the bounds that have to be regenerated.
   */
  private void generate(Bounds bounds) {
    // Generate new balls;
    game.getState()
        .getBoard()
        .randomize(
            new Coordinate(bounds.minX, bounds.minY), new Coordinate(bounds.maxX, bounds.maxY));

    // Check the new board for valid combinations.
    CombinationFinder newFinder = CombinationFinder.create(game.getState().getBoard());
    if (newFinder.getPossibleBounds().size() == 1) {
      // Only one combination? This is trouble.
      Bounds newCombinationBounds = newFinder.getCombination();
      if (newCombinationBounds.equals(bounds)) {
        // Oh, oh, in the same spot! So, they must be of the same color.
        // Therefore, we need to randomize some balls to avoid enter
        // an infinite loop.
        timer.setRunning(false);
        board.setColoured(false);
        game.getState().resetBoard();
        board.addAction(
            Actions.sequence(
                board.shake(10, 5, 0.05f),
                Actions.run(
                    new Runnable() {
                      @Override
                      public void run() {
                        board.setColoured(true);
                        timer.setRunning(true);
                      }
                    })));
      }
    }
    board.addAction(board.showRegion(bounds));
  }
Esempio n. 2
0
  @Override
  public void onTimeOut() {
    // Disable any further interactions.
    board.clearSelection();
    board.setColoured(true);
    board.setTouchable(Touchable.disabled);
    timer.setRunning(false);
    game.player.playSound(SoundCode.GAME_OVER);

    // Update the score... and the record.
    int score = game.getState().getScore();
    int time = Math.round(game.getState().getElapsedTime());
    game.getPlatform().score().registerScore(score, time);
    game.getPlatform().score().flushData();

    // Save information about this game in the statistics.
    game.statistics.getTotalData().incrementValue("score", game.getState().getScore());
    game.statistics.getTotalData().incrementValue("games");
    game.statistics
        .getTotalData()
        .incrementValue("time", Math.round(game.getState().getElapsedTime()));
    game.getPlatform().statistics().saveStatistics(game.statistics);

    // Mark a combination that the user could do if he had enough time.
    if (game.getState().getWiggledBounds() == null) {
      CombinationFinder combo = CombinationFinder.create(game.getState().getBoard());
      game.getState().setWiggledBounds(combo.getCombination());
    }
    for (int y = 0; y < game.getState().getBoard().getSize(); y++) {
      for (int x = 0; x < game.getState().getBoard().getSize(); x++) {
        if (game.getState().getWiggledBounds() != null
            && !game.getState().getWiggledBounds().inBounds(x, y)) {
          board.getBall(x, y).addAction(Actions.color(Color.DARK_GRAY, 0.15f));
        }
      }
    }

    // Animate the transition to game over.
    board.addAction(Actions.delay(2f, board.hideBoard()));
    hud.addAction(Actions.delay(2f, Actions.fadeOut(0.25f)));

    // Head to the game over after all these animations have finished.
    getStage()
        .addAction(
            Actions.delay(
                2.5f,
                Actions.run(
                    new Runnable() {
                      @Override
                      public void run() {
                        getStage().getRoot().clearActions();
                        game.pushScreen(Screens.GAME_OVER);
                      }
                    })));

    // Mark the game as finished.
    game.getState().setTimeout(true);
  }
Esempio n. 3
0
  /**
   * This method pauses the game. It is executed in one of the following situations: first, the user
   * presses PAUSE button. Second, the user presses BACK button. Third, the user pauses the game
   * (when the game is restored the game is still paused).
   */
  private void pauseGame() {
    paused = true;

    // Show the pause dialog unless you have already stop the game.
    if (!game.getState().isTimeout()) {
      showPauseDialog();
    }

    // If the game has started, pause it.
    if (running && !game.getState().isTimeout()) {
      board.setColoured(false);
      board.setTouchable(Touchable.disabled);
      timer.setRunning(false);
    }
  }
Esempio n. 4
0
  /**
   * This method resumes the game. It is executed in one of the following situations: first, the
   * user presses CONTINUE button on pause screen. Second, the user presses BACK on the pause
   * screen.
   */
  private void resumeGame() {
    paused = false;

    // If the countdown has finished but the game is not running is a
    // condition that might be triggered in one of the following cases.
    // 1) The user has paused the game during the countdown. When the
    //    countdown finishes, because the game is paused, the game does
    //    not start.
    // 2) The user was leaving the game during the countdown. Same.
    if (!running && game.getState().isCountdownFinished()) {
      running = true;
      game.player.playSound(SoundCode.SUCCESS);
    }

    if (running && !game.getState().isTimeout()) {
      board.setColoured(true);
      board.setTouchable(Touchable.enabled);
      timer.setRunning(true);
    }
  }