View Javadoc

1   package de.jos.game.actions.cleanup;
2   
3   import java.util.List;
4   
5   import org.apache.commons.logging.Log;
6   import org.apache.commons.logging.LogFactory;
7   
8   import de.jos.game.Constants;
9   import de.jos.game.actions.ActionFadeBullets;
10  import de.jos.game.actions.ActionTextFader;
11  import de.jos.game.actions.permanent.ActionCanister;
12  import de.jos.game.logic.AbstractGameBoard;
13  import de.jos.game.logic.BoardUtils;
14  import de.jos.game.logic.InfiniteGameBoard;
15  import de.jos.game.objects.Bullet;
16  import de.jos.game.objects.Canister;
17  import de.jos.game.objects.ScoreCalculator;
18  
19  public class CleanupActionBulletMovingInfinite extends CleanupActionAbstractBulletMoving implements Constants {
20  
21    private static Log log = LogFactory.getLog(CleanupActionBulletMovingInfinite.class);
22  
23    public CleanupActionBulletMovingInfinite(AbstractGameBoard board, Bullet bullet) {
24      super(board, bullet);
25    }
26  
27    public void cleanupSpecific(List<Bullet> dissolveBulletList) {
28      // TODO kombinationen checken
29      if (dissolveBulletList != null) {
30        // bullets vom board entfernen und in neue action einbringen
31        for (Bullet tmpBullet : dissolveBulletList) {
32          getGameBoard().removeBullet(tmpBullet.getBoardX(), tmpBullet.getBoardY());
33        }
34  
35        if (dissolveBulletList.size() != 0) {
36          ScoreCalculator scoreCalculator = getResourceContainer().getScoreCalculator();
37          Integer color = (dissolveBulletList.get(0)).getColor();
38          // get the bullet in the middle of the list -> x, y coordinates
39          int size = dissolveBulletList.size();
40          Bullet tmpBullet = dissolveBulletList.get(size / 2);
41  
42          int aquiredScore = scoreCalculator.getScore(color, size);
43  
44          // add standard score
45          ActionTextFader atf = new ActionTextFader();
46          atf.setResourceContainer(getGameBoard().getResourceContainer());
47          atf.setX(tmpBullet.getSpriteAbsoluteX());
48          atf.setY(tmpBullet.getSpriteAbsoluteY() + 40);
49          atf.setText("SCORE " + aquiredScore);
50          atf.init();
51          getNewSpecialEventList().add(atf);
52          // cleanup action setzen
53          CleanupActionScore cas = new CleanupActionScore(getGameBoard(), aquiredScore);
54          atf.setCleanupAction(cas);
55  
56          // special score if the level was solved
57          if (BoardUtils.getBoardBullets(getGameBoard()).isEmpty()) {
58            atf = new ActionTextFader();
59            atf.setResourceContainer(getGameBoard().getResourceContainer());
60            atf.setX(LEVEL_SOLVE_SCORE_X);
61            atf.setY(LEVEL_SOLVE_SCORE_Y + 40);
62            atf.setText("SCORE " + getGameBoard().getLevel().getBoardClearScore().intValue());
63            atf.init();
64            getNewSpecialEventList().add(atf);
65            // cleanup action setzen
66            cas = new CleanupActionScore(getGameBoard(), getGameBoard().getLevel().getBoardClearScore().intValue());
67            atf.setCleanupAction(cas);
68          }
69  
70          // create new action
71          ActionFadeBullets actionDissolveBullets = new ActionFadeBullets(Bullet.State.DISSOLVING);
72          actionDissolveBullets.setResourceContainer(getGameBoard().getResourceContainer());
73          actionDissolveBullets.getBulletList().addAll(dissolveBulletList);
74          actionDissolveBullets.init();
75  
76          ActionCanister ac = getGameBoard().getActionContainer().getActionCanister();
77          int canisterNumber = ac.getNumberCanisters();
78  
79          Canister canister = new Canister();
80          canister.setResourceContainer(getGameBoard().getResourceContainer());
81          canister.init(canisterNumber, color);
82  
83          if (ac.combinationMatchPossible(canister, getGameBoard().getLevel().getAccumulatedCombinations()) == true) {
84            ac.addCanister(canister);
85          } else {
86            ac.clearCanisters();
87          }
88  
89          // add action to the new special event list
90          getNewSpecialEventList().add(actionDissolveBullets);
91        }
92      }
93      refillBorder();
94    }
95  
96    private InfiniteGameBoard getInfiniteGameBoard() {
97      AbstractGameBoard board = getGameBoard();
98      if (board instanceof InfiniteGameBoard) {
99        return (InfiniteGameBoard) board;
100     }
101     return null;
102   }
103 
104   private void refillBorder() {
105     Bullet[][] board = getGameBoard().getBoard();
106 
107     // check selectable boarder fields for refilling !!
108     for (int x = LEFT_BORDER; x < BOARD_WIDTH_X - RIGHT_BORDER; x++) {
109       if (board[x][TOP_BORDER - 1] == null) {
110         updateBoarderFieldY(TOP_BORDER - 2, TOP_BORDER - 1, x);
111       }
112       if (board[x][BOARD_WIDTH_Y - TOP_BORDER] == null) {
113         updateBoarderFieldY(BOARD_WIDTH_Y - TOP_BORDER + 1, BOARD_WIDTH_Y - TOP_BORDER, x);
114       }
115     }
116 
117     for (int y = TOP_BORDER; y < BOARD_WIDTH_Y - BOTTOM_BORDER; y++) {
118       if (board[LEFT_BORDER - 1][y] == null) {
119         updateBoarderFieldX(LEFT_BORDER - 2, LEFT_BORDER - 1, y);
120       }
121       if (board[BOARD_WIDTH_X - RIGHT_BORDER][y] == null) {
122         updateBoarderFieldX(BOARD_WIDTH_X - RIGHT_BORDER + 1, BOARD_WIDTH_X - RIGHT_BORDER, y);
123       }
124     }
125   }
126 
127   private void updateBoarderFieldY(int yFrom, int yTo, int x) {
128     // update the y coordinate of the bullet
129     InfiniteGameBoard infiniteGameBoard = getInfiniteGameBoard();
130 
131     Bullet tmpBullet = getGameBoard().getBullet(x, yFrom);
132     tmpBullet.setBoardY(yTo);
133     tmpBullet.updateSpriteCoordinates();
134     getGameBoard().setBullet(x, yTo, tmpBullet);
135 
136     Bullet newBullet = infiniteGameBoard.createRandomBullet(x, yFrom);
137     infiniteGameBoard.setBullet(x, yFrom, newBullet);
138   }
139 
140   private void updateBoarderFieldX(int xFrom, int xTo, int y) {
141     // update the x coordinate of the bullet
142     InfiniteGameBoard infiniteGameBoard = getInfiniteGameBoard();
143 
144     Bullet tmpBullet = getGameBoard().getBullet(xFrom, y);
145     tmpBullet.setBoardX(xTo);
146     tmpBullet.updateSpriteCoordinates();
147     getGameBoard().setBullet(xTo, y, tmpBullet);
148 
149     Bullet newBullet = infiniteGameBoard.createRandomBullet(xFrom, y);
150     infiniteGameBoard.setBullet(xFrom, y, newBullet);
151   }
152 
153 }