View Javadoc

1   package de.jos.game.logic;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   import de.jos.game.Constants;
7   import de.jos.game.actions.ActionContainer;
8   import de.jos.game.logic.AbstractGameLogic.Direction;
9   import de.jos.game.objects.Bullet;
10  import de.jos.game.objects.ResourceContainer;
11  import de.jos.game.objects.SimplifiedBullet;
12  import de.jos.game.xml.Level;
13  
14  public abstract class AbstractGameBoard implements Constants {
15  
16    private Bullet[][] board = null;
17    private Level level = null;
18  
19    private List<Bullet> bulletList = null;
20  
21    private ResourceContainer resourceContainer = null;
22  
23    private ActionContainer actionContainer = null;
24  
25    private AbstractGameLogic gameLogic = null;
26  
27    public AbstractGameBoard() {
28      board = new Bullet[BOARD_WIDTH_X][BOARD_WIDTH_Y];
29      bulletList = new ArrayList<Bullet>();
30    }
31  
32    public void updateBoard() {
33      for (int x = 0; x < BOARD_WIDTH_X; x++)
34        for (int y = 0; y < BOARD_WIDTH_Y; y++) {
35          board[x][y] = null;
36        }
37  
38      for (Bullet tmpBullet : bulletList) {
39        board[tmpBullet.getBoardX()][tmpBullet.getBoardY()] = tmpBullet;
40      }
41      
42      /*
43      Iterator<Bullet> iter = bulletList.iterator();
44      while (iter.hasNext()) {
45        Bullet tmpBullet = iter.next();
46        board[tmpBullet.getBoardX()][tmpBullet.getBoardY()] = tmpBullet;
47      }
48      */
49    }
50  
51    public void initLevel(Level level) {
52      this.level = level;
53      bulletList.clear();
54      bulletList.addAll(level.getBullets());
55    }
56  
57    public Bullet getBullet(int x, int y) {
58      if ((x < BOARD_WIDTH_X && x >= 0) && (y < BOARD_WIDTH_Y && y >= 0)) {
59        return board[x][y];
60      }
61      return null;
62    }
63  
64    public void setBullet(int x, int y, Bullet bullet) {
65      board[x][y] = bullet;
66    }
67  
68    // TODO noch ueberarbeiten -> wo werden die bullets jetzt
69    // gehalten ? in der liste, oder im array
70    public void removeBullet(int x, int y) {
71      board[x][y] = null;
72    }
73  
74    public Object[] isMovePossible(Direction direction, Bullet bullet) {
75  
76      Object returnValues[] = new Object[5];
77      returnValues[0] = Boolean.FALSE;
78  
79      if (SimplifiedBullet.BulletSize.SMALL.equals(bullet.getStatus())) {
80        return returnValues;
81      }
82  
83      int x = bullet.getBoardX();
84      int y = bullet.getBoardY();
85  
86      int xOriginal = x;
87      int yOriginal = y;
88  
89      int xdiff = 0;
90      int ydiff = 0;
91  
92      if (direction == Direction.DOWN) {
93        ydiff = 1;
94      } else if (direction == Direction.UP) {
95        ydiff = -1;
96      } else if (direction == Direction.LEFT) {
97        xdiff = -1;
98      } else if (direction == Direction.RIGHT) {
99        xdiff = 1;
100     }
101 
102     boolean blockingBulletFound = false;
103 
104     // find the next bullet that will block the movement
105     while (true) {
106       x += xdiff;
107       y += ydiff;
108 
109       if (x < 2 || x > BOARD_WIDTH_X - 2 || y < 2 || y > BOARD_WIDTH_Y - 2) {
110         System.out.println("breaking");
111         break;
112       }
113 
114       if (board[x][y] != null) {
115         blockingBulletFound = true;
116         break;
117       }
118     }
119 
120     // if blocking bullet was found
121     if (blockingBulletFound == true) {
122       // check if the bullet was on the board
123       if (x < 2 || x > 10 || y < 2 || y > 10) {
124         blockingBulletFound = false;
125       }
126       // check if there is at least one empty space between the bullets
127       if (Math.abs(x - xOriginal) <= 1 && Math.abs(y - yOriginal) <= 1) {
128         blockingBulletFound = false;
129       }
130     }
131 
132     // returnValues array fuellen
133     returnValues[0] = Boolean.valueOf(blockingBulletFound);
134     returnValues[1] = Integer.valueOf(x - xdiff);
135     returnValues[2] = Integer.valueOf(y - ydiff);
136     returnValues[3] = Integer.valueOf(xdiff);
137     returnValues[4] = Integer.valueOf(ydiff);
138 
139     return returnValues;
140   }
141 
142   public void resetGlowAnimation(int ignoreX, int ignoreY) {
143     for (int x = 0; x < BOARD_WIDTH_X; x++)
144       for (int y = 0; y < BOARD_WIDTH_Y; y++) {
145         if (board[x][y] != null && !(ignoreX == x && ignoreY == y)) {
146           board[x][y].resetHoverAnimation();
147         }
148       }
149   }
150 
151   public ResourceContainer getResourceContainer() {
152     return resourceContainer;
153   }
154 
155   public void setResourceContainer(ResourceContainer resourceContainer) {
156     this.resourceContainer = resourceContainer;
157   }
158 
159   public List<Bullet> getBulletList() {
160     List<Bullet> bullets = new ArrayList<Bullet>();
161     for (int x = 0; x < BOARD_WIDTH_X; x++) {
162       for (int y = 0; y < BOARD_WIDTH_Y; y++) {
163         if (board[x][y] != null) {
164           bullets.add(board[x][y]);
165         }
166       }
167     }
168 
169     return bullets;
170   }
171 
172   public Bullet[][] getBoard() {
173     return board;
174   }
175 
176   public Level getLevel() {
177     return level;
178   }
179 
180   public ActionContainer getActionContainer() {
181     return actionContainer;
182   }
183 
184   public void setActionContainer(ActionContainer actionContainer) {
185     this.actionContainer = actionContainer;
186   }
187 
188   public AbstractGameLogic getGameLogic() {
189     return gameLogic;
190   }
191 
192   public void setGameLogic(AbstractGameLogic gameLogic) {
193     this.gameLogic = gameLogic;
194   }
195 
196 }