View Javadoc

1   package de.jos.game.aspect.plugins;
2   
3   import java.util.ArrayList;
4   import java.util.Collections;
5   import java.util.List;
6   
7   import org.apache.commons.lang.StringUtils;
8   
9   import de.jos.game.actions.permanent.ActionStatistics;
10  import de.jos.game.logic.AbstractGameBoard;
11  import de.jos.game.logic.InfiniteGameBoard;
12  import de.jos.game.objects.ResourceContainer;
13  import de.jos.game.objects.screen.AbstractGameObject;
14  import de.jos.game.objects.screen.TheGame;
15  import de.jos.game.xml.Combination;
16  import de.jos.game.xml.Level;
17  
18  public class LevelPlugin implements DebugPlugin {
19  
20    private static final String CMD_LEVEL = "LEVEL";
21    private static final String CMD_L = "L";
22  
23    public void executeCommand(AbstractGameObject gameObject, String command) {
24      if (gameObject instanceof TheGame) {
25        AbstractGameBoard board = ((TheGame) gameObject).getGameLogic().getBoard();
26        if (command.startsWith(CMD_L) && board instanceof InfiniteGameBoard) {
27          try {
28            InfiniteGameBoard infiniteGameBoard = (InfiniteGameBoard) board;
29            Integer levelStartScore = null;
30  
31            ResourceContainer resourceContainer = infiniteGameBoard.getResourceContainer();
32            int numberLevels = Integer.valueOf(command.substring(1).trim()).intValue();
33  
34            Level selectedLevel = resourceContainer.getInfiniteLevels().get(numberLevels - 1);
35            selectedLevel.getAccumulatedCombinations().clear();
36            selectedLevel.getAccumulatedCombinations().addAll(selectedLevel.getCombinations());
37  
38            for (int i = (numberLevels - 2); i >= 0; i--) {
39              Level level = resourceContainer.getInfiniteLevels().get(i);
40              selectedLevel.getAccumulatedCombinations().addAll(level.getCombinations());
41              if (levelStartScore == null) {
42                levelStartScore = level.getLevelAdvanceScore();
43              }
44            }
45            Collections.sort(selectedLevel.getAccumulatedCombinations(), Combination.COMBINATION_COMPARATOR);
46  
47            // finally set the level
48            infiniteGameBoard.setNewLevel(selectedLevel);
49            infiniteGameBoard.getGameLogic().setLevel(selectedLevel);
50  
51            // update the score
52            ActionStatistics actionStatistics = infiniteGameBoard.getActionContainer().getActionStatistics();
53            int score = actionStatistics.getScore();
54            levelStartScore = levelStartScore == null ? Integer.valueOf(0) : levelStartScore;
55            actionStatistics.updateScore(levelStartScore.intValue() - score);
56  
57          } catch (Exception e) {
58            e.printStackTrace();
59          }
60        }
61      }
62    }
63  
64    public List<String> getPluginHelp() {
65      List<String> helpStringList = new ArrayList<String>();
66      helpStringList.add(CMD_L + " <NEW LEVEL>");
67      return helpStringList;
68    }
69  
70    public String getPluginHelpCommand() {
71      return CMD_LEVEL;
72    }
73  
74    public String getPluginName() {
75      return StringUtils.upperCase(CMD_LEVEL + " - change the current level");
76    }
77  
78    public boolean possibleCommandMatch(String command) {
79      boolean match = false;
80      if (command.startsWith(CMD_L)) {
81        match = true;
82      }
83      return match;
84    }
85  
86  }