View Javadoc

1   package erland.game.tileadventure;
2   
3   import java.util.List;
4   import java.util.ArrayList;
5   
6   /*
7    * Copyright (C) 2004 Erland Isaksson (erland_i@hotmail.com)
8    *
9    * This program is free software; you can redistribute it and/or
10   * modify it under the terms of the GNU General Public License
11   * as published by the Free Software Foundation; either version 2
12   * of the License, or (at your option) any later version.
13   *
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   *
19   * You should have received a copy of the GNU General Public License
20   * along with this program; if not, write to the Free Software
21   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22   * 
23   */
24  
25  public class MapBlockContainer implements MapObjectContainerInterface {
26      private MapObjectInterface blocks[][][];
27      private List objects[][][];
28  
29      public MapBlockContainer(int sizeX, int sizeY, int sizeZ) {
30          blocks = new MapObjectInterface[sizeX][sizeY][sizeZ];
31          objects = new List[sizeX][sizeY][sizeZ];
32          for (int x = 0; x < blocks.length; x++) {
33              blocks[x] = new MapObjectInterface[sizeY][sizeZ];
34              objects[x] = new List[sizeY][sizeZ];
35              for (int y = 0; y < blocks[x].length; y++) {
36                  blocks[x][y] = new MapObjectInterface[sizeZ];
37                  objects[x][y] = new List[sizeZ];
38                  for (int z = 0; z < blocks[x][y].length; z++) {
39                      blocks[x][y][z] = null;
40                      objects[x][y][z] = new ArrayList(10);
41                  }
42              }
43          }
44      }
45  
46      private boolean isInsideMap(int x, int y, int z) {
47          if(x<blocks.length && y<blocks[0].length && z<blocks[0][0].length && x>=0 && y>=0 && z>=0) {
48              return true;
49          }else {
50              return false;
51          }
52      }
53  
54      public int getSizeX() {
55          return blocks.length;
56      }
57  
58      public int getSizeY() {
59          return blocks[0].length;
60      }
61  
62      public int getSizeZ() {
63          return blocks[0][0].length;
64      }
65  
66      public MapObjectInterface getBlock(int x, int y, int z) {
67          if(isInsideMap(x,y,z)) {
68              return blocks[x][y][z];
69          }else {
70              return null;
71          }
72      }
73  
74      public void setBlock(MapObjectInterface block, int x, int y, int z) {
75          if(isInsideMap(x,y,z)) {
76              blocks[x][y][z]=block;
77          }
78      }
79  
80      public void removeBlock(MapObjectInterface block, int x, int y, int z) {
81          if(isInsideMap(x,y,z)) {
82              if(blocks[x][y][z]==block) {
83                  blocks[x][y][z]=null;
84              }
85          }
86      }
87  
88      public void removeBlock(MapObjectInterface object) {
89          for (int x = 0; x < blocks.length; x++) {
90              for (int y = 0; y < blocks[x].length; y++) {
91                  for (int z = 0; z < blocks[x][y].length; z++) {
92                      if(object==blocks[x][y][z]) {
93                          blocks[x][y][z] = null;
94                      }
95                  }
96              }
97          }
98      }
99      public void addObject(MapObjectInterface object,int x, int y, int z) {
100         if(isInsideMap(x,y,z)) {
101             for(int i=0;i<objects[x][y][z].size();i++) {
102                 if(objects[x][y][z].get(i)==object) {
103                     return;
104                 }
105             }
106             //System.out.println("Add object ("+x+","+y+","+z+")="+object);
107             objects[x][y][z].add(object);
108         }
109     }
110     public void removeObject(MapObjectInterface object, int x, int y, int z) {
111         if(isInsideMap(x,y,z)) {
112             objects[x][y][z].remove(object);
113         }
114     }
115     public void removeObject(MapObjectInterface object) {
116         for (int x = 0; x < objects.length; x++) {
117             for (int y = 0; y < objects[x].length; y++) {
118                 for (int z = 0; z < objects[x][y].length; z++) {
119                     while(objects[x][y][z].remove(object));
120                 }
121             }
122         }
123     }
124     public List getObjects(int x, int y, int z) {
125         if(isInsideMap(x,y,z)) {
126             return objects[x][y][z];
127         }else {
128             return null;
129         }
130     }
131 }