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);
  }