View Javadoc

1   package de.jos.game.logic;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   import org.apache.commons.lang.math.JVMRandom;
7   
8   import de.jos.game.objects.Bullet;
9   import de.jos.game.objects.Position;
10  import de.jos.game.xml.Level;
11  
12  public class InfiniteGameBoard extends AbstractGameBoard {
13  
14    private JVMRandom jvmRandom = new JVMRandom();
15  
16    public void initLevel(Level level) {
17      super.initLevel(level);
18  
19      // set the level in the action statistics
20      getActionContainer().getActionStatistics().setLevel(level);
21  
22      fillFieldsByRandom();
23      fillBorderFields();
24  
25      updateBoard();
26    }
27  
28    public void setNewLevel(Level level) {
29      // copy the bullets from the original level
30      // level.setBullets(new HashSet<Bullet>(getBulletList()));
31  
32      super.initLevel(level);
33  
34      getActionContainer().getActionStatistics().setLevel(level);
35    }
36  
37    public void updateBoard() {
38      super.updateBoard();
39  
40      fillBorderFields();
41      fillFieldsByRandom();
42    }
43  
44    public Bullet createRandomBullet(int x, int y) {
45      Bullet tmpBullet = new Bullet();
46      tmpBullet.setBoardX(x);
47      tmpBullet.setBoardY(y);
48      
49      int color = jvmRandom.nextInt() % getLevel().getNumberColors();
50      tmpBullet.setColor(Integer.valueOf(color));
51      tmpBullet.setResourceContainer(getResourceContainer());
52      tmpBullet.init();
53      return tmpBullet;
54    }
55  
56    protected boolean hasAdjoiningBullet(List<Position> tmpList, Position pos) {
57      boolean adjoining = false;
58  
59      int x = pos.getX();
60      int y = pos.getY();
61  
62      for (Position tmpPos : tmpList) {
63        int tmpX = tmpPos.getX();
64        int tmpY = tmpPos.getY();
65  
66        if ((x == tmpX && (y == (tmpY - 1) || y == (tmpY + 1))) || (y == tmpY && (x == (tmpX - 1) || x == (tmpX + 1)))) {
67          adjoining = true;
68          break;
69        }
70      }
71  
72      return adjoining;
73    }
74  
75    public void fillFieldsByRandom() {
76      List<Position> tmpList = new ArrayList<Position>();
77      Position pos = null;
78  
79      int counter = 0;
80      while (counter < 7) {
81        int randomX = jvmRandom.nextInt() % 9 + LEFT_BORDER;
82        int randomY = jvmRandom.nextInt() % 9 + TOP_BORDER;
83        pos = new Position(randomX, randomY);
84        if (tmpList.contains(pos) == false && hasAdjoiningBullet(tmpList, pos) == false) {
85          getBoard()[randomX][randomY] = createRandomBullet(randomX, randomY);
86          tmpList.add(pos);
87          counter++;
88        }
89      }
90      tmpList.clear();
91    }
92  
93    private void fillBorderFields() {
94  
95      Bullet[][] board = getBoard();
96  
97      for (int x = LEFT_BORDER; x < BOARD_WIDTH_X - RIGHT_BORDER; x++) {
98        for (int y = 0; y < TOP_BORDER; y++) {
99          if (board[x][y] == null) {
100           getBoard()[x][y] = createRandomBullet(x, y);
101         }
102 
103         if (board[x][BOARD_WIDTH_Y - y - 1] == null) {
104           getBoard()[x][BOARD_WIDTH_Y - y - 1] = createRandomBullet(x, BOARD_WIDTH_Y - y - 1);
105         }
106       }
107     }
108 
109     for (int y = TOP_BORDER; y < BOARD_WIDTH_Y - BOTTOM_BORDER; y++) {
110       for (int x = 0; x < LEFT_BORDER; x++) {
111         if (board[x][y] == null) {
112           getBoard()[x][y] = createRandomBullet(x, y);
113         }
114 
115         if (board[BOARD_WIDTH_X - x - 1][y] == null) {
116           getBoard()[BOARD_WIDTH_X - x - 1][y] = createRandomBullet(BOARD_WIDTH_X - x - 1, y);
117         }
118       }
119     }
120 
121   }
122 
123 }